1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| struct{
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(u32));
__uint(value_size, sizeof(u8)*5);
__uint(max_entries, 100);
} info_map SEC(".maps");
struct event{
u64 pid_tgid;
u8 info[5]; // 这里的成员长度,请结合 obj.go 来看
uintptr_t addr;
long res;
};
typedef struct event event_t;
// Force emitting struct event into the ELF.
const struct event *unused __attribute__((unused));
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
} events SEC(".maps");
SEC("uprobe/main_set")
int uprobe__main_set(struct pt_regs *ctx){
uintptr_t info_p = 0;
u8 info[5];
u64 pid_tgid = bpf_get_current_pid_tgid();
SARG(ctx, 0, info_p);
bpf_probe_read(&info, sizeof(info), (const void*)info_p);
u32 tgid = (u32)(pid_tgid >> 32);
bpf_map_update_elem(&info_map, &tgid, &info, BPF_ANY);
event_t event = {};
event.pid_tgid = pid_tgid;
memcpy(event.info, info, sizeof(info));
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
return 0;
}
SEC("uprobe/main_get")
int uprobe__main_get(struct pt_regs *ctx){
uintptr_t info_p = 0;
u64 pid_tgid = bpf_get_current_pid_tgid();
void* r_info_p = NULL;
u32 tgid = (u32)(pid_tgid >> 32);
r_info_p = bpf_map_lookup_elem(&info_map, &tgid);
if (r_info_p == NULL){
return 0;
}
event_t event = {};
event.pid_tgid = pid_tgid;
SARG(ctx, 0, info_p);
u8 info[5];
memcpy(info, r_info_p, sizeof(info));
memcpy(event.info, info, sizeof(event.info));
event.res = bpf_probe_write_user((u8*)info_p, info, sizeof(info));
event.addr = info_p;
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
return 0;
}
|