12 #include <linux/bpf.h>
13 #include <linux/filter.h>
14 #include <linux/perf_event.h>
15 #include <sys/syscall.h>
16 #include <sys/ioctl.h>
21 #include "bpf_helpers.h"
24 #define DEBUGFS "/sys/kernel/debug/tracing/"
26 static char license
[128];
27 static int kern_version
;
28 static bool processed_sec
[128];
30 int prog_fd
[MAX_PROGS
];
31 int event_fd
[MAX_PROGS
];
33 int prog_array_fd
= -1;
35 static int populate_prog_array(const char *event
, int prog_fd
)
37 int ind
= atoi(event
), err
;
39 err
= bpf_update_elem(prog_array_fd
, &ind
, &prog_fd
, BPF_ANY
);
41 printf("failed to store prog_fd in prog_array\n");
47 static int load_and_attach(const char *event
, struct bpf_insn
*prog
, int size
)
49 bool is_socket
= strncmp(event
, "socket", 6) == 0;
50 bool is_kprobe
= strncmp(event
, "kprobe/", 7) == 0;
51 bool is_kretprobe
= strncmp(event
, "kretprobe/", 10) == 0;
52 bool is_tracepoint
= strncmp(event
, "tracepoint/", 11) == 0;
53 bool is_xdp
= strncmp(event
, "xdp", 3) == 0;
54 enum bpf_prog_type prog_type
;
57 struct perf_event_attr attr
= {};
59 attr
.type
= PERF_TYPE_TRACEPOINT
;
60 attr
.sample_type
= PERF_SAMPLE_RAW
;
61 attr
.sample_period
= 1;
62 attr
.wakeup_events
= 1;
65 prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
66 } else if (is_kprobe
|| is_kretprobe
) {
67 prog_type
= BPF_PROG_TYPE_KPROBE
;
68 } else if (is_tracepoint
) {
69 prog_type
= BPF_PROG_TYPE_TRACEPOINT
;
71 prog_type
= BPF_PROG_TYPE_XDP
;
73 printf("Unknown event '%s'\n", event
);
77 fd
= bpf_prog_load(prog_type
, prog
, size
, license
, kern_version
);
79 printf("bpf_prog_load() err=%d\n%s", errno
, bpf_log_buf
);
83 prog_fd
[prog_cnt
++] = fd
;
93 if (!isdigit(*event
)) {
94 printf("invalid prog number\n");
97 return populate_prog_array(event
, fd
);
100 if (is_kprobe
|| is_kretprobe
) {
107 printf("event name cannot be empty\n");
112 return populate_prog_array(event
, fd
);
114 snprintf(buf
, sizeof(buf
),
115 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
116 is_kprobe
? 'p' : 'r', event
, event
);
119 printf("failed to create kprobe '%s' error '%s'\n",
120 event
, strerror(errno
));
124 strcpy(buf
, DEBUGFS
);
125 strcat(buf
, "events/kprobes/");
128 } else if (is_tracepoint
) {
132 printf("event name cannot be empty\n");
135 strcpy(buf
, DEBUGFS
);
136 strcat(buf
, "events/");
141 efd
= open(buf
, O_RDONLY
, 0);
143 printf("failed to open event %s\n", event
);
147 err
= read(efd
, buf
, sizeof(buf
));
148 if (err
< 0 || err
>= sizeof(buf
)) {
149 printf("read from '%s' failed '%s'\n", event
, strerror(errno
));
159 efd
= perf_event_open(&attr
, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
161 printf("event %d fd %d err %s\n", id
, efd
, strerror(errno
));
164 event_fd
[prog_cnt
- 1] = efd
;
165 ioctl(efd
, PERF_EVENT_IOC_ENABLE
, 0);
166 ioctl(efd
, PERF_EVENT_IOC_SET_BPF
, fd
);
171 static int load_maps(struct bpf_map_def
*maps
, int len
)
175 for (i
= 0; i
< len
/ sizeof(struct bpf_map_def
); i
++) {
177 map_fd
[i
] = bpf_create_map(maps
[i
].type
,
183 printf("failed to create a map: %d %s\n",
184 errno
, strerror(errno
));
188 if (maps
[i
].type
== BPF_MAP_TYPE_PROG_ARRAY
)
189 prog_array_fd
= map_fd
[i
];
194 static int get_sec(Elf
*elf
, int i
, GElf_Ehdr
*ehdr
, char **shname
,
195 GElf_Shdr
*shdr
, Elf_Data
**data
)
199 scn
= elf_getscn(elf
, i
);
203 if (gelf_getshdr(scn
, shdr
) != shdr
)
206 *shname
= elf_strptr(elf
, ehdr
->e_shstrndx
, shdr
->sh_name
);
207 if (!*shname
|| !shdr
->sh_size
)
210 *data
= elf_getdata(scn
, 0);
211 if (!*data
|| elf_getdata(scn
, *data
) != NULL
)
217 static int parse_relo_and_apply(Elf_Data
*data
, Elf_Data
*symbols
,
218 GElf_Shdr
*shdr
, struct bpf_insn
*insn
)
222 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
224 for (i
= 0; i
< nrels
; i
++) {
227 unsigned int insn_idx
;
229 gelf_getrel(data
, i
, &rel
);
231 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
233 gelf_getsym(symbols
, GELF_R_SYM(rel
.r_info
), &sym
);
235 if (insn
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
236 printf("invalid relo for insn[%d].code 0x%x\n",
237 insn_idx
, insn
[insn_idx
].code
);
240 insn
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
241 insn
[insn_idx
].imm
= map_fd
[sym
.st_value
/ sizeof(struct bpf_map_def
)];
247 int load_bpf_file(char *path
)
252 GElf_Shdr shdr
, shdr_prog
;
253 Elf_Data
*data
, *data_prog
, *symbols
= NULL
;
254 char *shname
, *shname_prog
;
256 if (elf_version(EV_CURRENT
) == EV_NONE
)
259 fd
= open(path
, O_RDONLY
, 0);
263 elf
= elf_begin(fd
, ELF_C_READ
, NULL
);
268 if (gelf_getehdr(elf
, &ehdr
) != &ehdr
)
271 /* clear all kprobes */
272 i
= system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
274 /* scan over all elf sections to get license and map info */
275 for (i
= 1; i
< ehdr
.e_shnum
; i
++) {
277 if (get_sec(elf
, i
, &ehdr
, &shname
, &shdr
, &data
))
280 if (0) /* helpful for llvm debugging */
281 printf("section %d:%s data %p size %zd link %d flags %d\n",
282 i
, shname
, data
->d_buf
, data
->d_size
,
283 shdr
.sh_link
, (int) shdr
.sh_flags
);
285 if (strcmp(shname
, "license") == 0) {
286 processed_sec
[i
] = true;
287 memcpy(license
, data
->d_buf
, data
->d_size
);
288 } else if (strcmp(shname
, "version") == 0) {
289 processed_sec
[i
] = true;
290 if (data
->d_size
!= sizeof(int)) {
291 printf("invalid size of version section %zd\n",
295 memcpy(&kern_version
, data
->d_buf
, sizeof(int));
296 } else if (strcmp(shname
, "maps") == 0) {
297 processed_sec
[i
] = true;
298 if (load_maps(data
->d_buf
, data
->d_size
))
300 } else if (shdr
.sh_type
== SHT_SYMTAB
) {
305 /* load programs that need map fixup (relocations) */
306 for (i
= 1; i
< ehdr
.e_shnum
; i
++) {
308 if (get_sec(elf
, i
, &ehdr
, &shname
, &shdr
, &data
))
310 if (shdr
.sh_type
== SHT_REL
) {
311 struct bpf_insn
*insns
;
313 if (get_sec(elf
, shdr
.sh_info
, &ehdr
, &shname_prog
,
314 &shdr_prog
, &data_prog
))
317 insns
= (struct bpf_insn
*) data_prog
->d_buf
;
319 processed_sec
[shdr
.sh_info
] = true;
320 processed_sec
[i
] = true;
322 if (parse_relo_and_apply(data
, symbols
, &shdr
, insns
))
325 if (memcmp(shname_prog
, "kprobe/", 7) == 0 ||
326 memcmp(shname_prog
, "kretprobe/", 10) == 0 ||
327 memcmp(shname_prog
, "tracepoint/", 11) == 0 ||
328 memcmp(shname_prog
, "xdp", 3) == 0 ||
329 memcmp(shname_prog
, "socket", 6) == 0)
330 load_and_attach(shname_prog
, insns
, data_prog
->d_size
);
334 /* load programs that don't use maps */
335 for (i
= 1; i
< ehdr
.e_shnum
; i
++) {
337 if (processed_sec
[i
])
340 if (get_sec(elf
, i
, &ehdr
, &shname
, &shdr
, &data
))
343 if (memcmp(shname
, "kprobe/", 7) == 0 ||
344 memcmp(shname
, "kretprobe/", 10) == 0 ||
345 memcmp(shname
, "tracepoint/", 11) == 0 ||
346 memcmp(shname
, "xdp", 3) == 0 ||
347 memcmp(shname
, "socket", 6) == 0)
348 load_and_attach(shname
, data
->d_buf
, data
->d_size
);
355 void read_trace_pipe(void)
359 trace_fd
= open(DEBUGFS
"trace_pipe", O_RDONLY
, 0);
364 static char buf
[4096];
367 sz
= read(trace_fd
, buf
, sizeof(buf
));
375 #define MAX_SYMS 300000
376 static struct ksym syms
[MAX_SYMS
];
379 static int ksym_cmp(const void *p1
, const void *p2
)
381 return ((struct ksym
*)p1
)->addr
- ((struct ksym
*)p2
)->addr
;
384 int load_kallsyms(void)
386 FILE *f
= fopen("/proc/kallsyms", "r");
387 char func
[256], buf
[256];
396 if (!fgets(buf
, sizeof(buf
), f
))
398 if (sscanf(buf
, "%p %c %s", &addr
, &symbol
, func
) != 3)
402 syms
[i
].addr
= (long) addr
;
403 syms
[i
].name
= strdup(func
);
407 qsort(syms
, sym_cnt
, sizeof(struct ksym
), ksym_cmp
);
411 struct ksym
*ksym_search(long key
)
413 int start
= 0, end
= sym_cnt
;
416 while (start
< end
) {
417 size_t mid
= start
+ (end
- start
) / 2;
419 result
= key
- syms
[mid
].addr
;
428 if (start
>= 1 && syms
[start
- 1].addr
< key
&&
429 key
< syms
[start
].addr
)
431 return &syms
[start
- 1];
433 /* out of range. return _stext */