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 enum bpf_prog_type prog_type
;
55 struct perf_event_attr attr
= {};
57 attr
.type
= PERF_TYPE_TRACEPOINT
;
58 attr
.sample_type
= PERF_SAMPLE_RAW
;
59 attr
.sample_period
= 1;
60 attr
.wakeup_events
= 1;
63 prog_type
= BPF_PROG_TYPE_SOCKET_FILTER
;
64 } else if (is_kprobe
|| is_kretprobe
) {
65 prog_type
= BPF_PROG_TYPE_KPROBE
;
67 printf("Unknown event '%s'\n", event
);
71 fd
= bpf_prog_load(prog_type
, prog
, size
, license
, kern_version
);
73 printf("bpf_prog_load() err=%d\n%s", errno
, bpf_log_buf
);
77 prog_fd
[prog_cnt
++] = fd
;
84 if (!isdigit(*event
)) {
85 printf("invalid prog number\n");
88 return populate_prog_array(event
, fd
);
91 if (is_kprobe
|| is_kretprobe
) {
98 printf("event name cannot be empty\n");
103 return populate_prog_array(event
, fd
);
105 snprintf(buf
, sizeof(buf
),
106 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
107 is_kprobe
? 'p' : 'r', event
, event
);
110 printf("failed to create kprobe '%s' error '%s'\n",
111 event
, strerror(errno
));
116 strcpy(buf
, DEBUGFS
);
117 strcat(buf
, "events/kprobes/");
121 efd
= open(buf
, O_RDONLY
, 0);
123 printf("failed to open event %s\n", event
);
127 err
= read(efd
, buf
, sizeof(buf
));
128 if (err
< 0 || err
>= sizeof(buf
)) {
129 printf("read from '%s' failed '%s'\n", event
, strerror(errno
));
139 efd
= perf_event_open(&attr
, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
141 printf("event %d fd %d err %s\n", id
, efd
, strerror(errno
));
144 event_fd
[prog_cnt
- 1] = efd
;
145 ioctl(efd
, PERF_EVENT_IOC_ENABLE
, 0);
146 ioctl(efd
, PERF_EVENT_IOC_SET_BPF
, fd
);
151 static int load_maps(struct bpf_map_def
*maps
, int len
)
155 for (i
= 0; i
< len
/ sizeof(struct bpf_map_def
); i
++) {
157 map_fd
[i
] = bpf_create_map(maps
[i
].type
,
160 maps
[i
].max_entries
);
164 if (maps
[i
].type
== BPF_MAP_TYPE_PROG_ARRAY
)
165 prog_array_fd
= map_fd
[i
];
170 static int get_sec(Elf
*elf
, int i
, GElf_Ehdr
*ehdr
, char **shname
,
171 GElf_Shdr
*shdr
, Elf_Data
**data
)
175 scn
= elf_getscn(elf
, i
);
179 if (gelf_getshdr(scn
, shdr
) != shdr
)
182 *shname
= elf_strptr(elf
, ehdr
->e_shstrndx
, shdr
->sh_name
);
183 if (!*shname
|| !shdr
->sh_size
)
186 *data
= elf_getdata(scn
, 0);
187 if (!*data
|| elf_getdata(scn
, *data
) != NULL
)
193 static int parse_relo_and_apply(Elf_Data
*data
, Elf_Data
*symbols
,
194 GElf_Shdr
*shdr
, struct bpf_insn
*insn
)
198 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
200 for (i
= 0; i
< nrels
; i
++) {
203 unsigned int insn_idx
;
205 gelf_getrel(data
, i
, &rel
);
207 insn_idx
= rel
.r_offset
/ sizeof(struct bpf_insn
);
209 gelf_getsym(symbols
, GELF_R_SYM(rel
.r_info
), &sym
);
211 if (insn
[insn_idx
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
)) {
212 printf("invalid relo for insn[%d].code 0x%x\n",
213 insn_idx
, insn
[insn_idx
].code
);
216 insn
[insn_idx
].src_reg
= BPF_PSEUDO_MAP_FD
;
217 insn
[insn_idx
].imm
= map_fd
[sym
.st_value
/ sizeof(struct bpf_map_def
)];
223 int load_bpf_file(char *path
)
228 GElf_Shdr shdr
, shdr_prog
;
229 Elf_Data
*data
, *data_prog
, *symbols
= NULL
;
230 char *shname
, *shname_prog
;
232 if (elf_version(EV_CURRENT
) == EV_NONE
)
235 fd
= open(path
, O_RDONLY
, 0);
239 elf
= elf_begin(fd
, ELF_C_READ
, NULL
);
244 if (gelf_getehdr(elf
, &ehdr
) != &ehdr
)
247 /* clear all kprobes */
248 i
= system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
250 /* scan over all elf sections to get license and map info */
251 for (i
= 1; i
< ehdr
.e_shnum
; i
++) {
253 if (get_sec(elf
, i
, &ehdr
, &shname
, &shdr
, &data
))
256 if (0) /* helpful for llvm debugging */
257 printf("section %d:%s data %p size %zd link %d flags %d\n",
258 i
, shname
, data
->d_buf
, data
->d_size
,
259 shdr
.sh_link
, (int) shdr
.sh_flags
);
261 if (strcmp(shname
, "license") == 0) {
262 processed_sec
[i
] = true;
263 memcpy(license
, data
->d_buf
, data
->d_size
);
264 } else if (strcmp(shname
, "version") == 0) {
265 processed_sec
[i
] = true;
266 if (data
->d_size
!= sizeof(int)) {
267 printf("invalid size of version section %zd\n",
271 memcpy(&kern_version
, data
->d_buf
, sizeof(int));
272 } else if (strcmp(shname
, "maps") == 0) {
273 processed_sec
[i
] = true;
274 if (load_maps(data
->d_buf
, data
->d_size
))
276 } else if (shdr
.sh_type
== SHT_SYMTAB
) {
281 /* load programs that need map fixup (relocations) */
282 for (i
= 1; i
< ehdr
.e_shnum
; i
++) {
284 if (get_sec(elf
, i
, &ehdr
, &shname
, &shdr
, &data
))
286 if (shdr
.sh_type
== SHT_REL
) {
287 struct bpf_insn
*insns
;
289 if (get_sec(elf
, shdr
.sh_info
, &ehdr
, &shname_prog
,
290 &shdr_prog
, &data_prog
))
293 insns
= (struct bpf_insn
*) data_prog
->d_buf
;
295 processed_sec
[shdr
.sh_info
] = true;
296 processed_sec
[i
] = true;
298 if (parse_relo_and_apply(data
, symbols
, &shdr
, insns
))
301 if (memcmp(shname_prog
, "kprobe/", 7) == 0 ||
302 memcmp(shname_prog
, "kretprobe/", 10) == 0 ||
303 memcmp(shname_prog
, "socket", 6) == 0)
304 load_and_attach(shname_prog
, insns
, data_prog
->d_size
);
308 /* load programs that don't use maps */
309 for (i
= 1; i
< ehdr
.e_shnum
; i
++) {
311 if (processed_sec
[i
])
314 if (get_sec(elf
, i
, &ehdr
, &shname
, &shdr
, &data
))
317 if (memcmp(shname
, "kprobe/", 7) == 0 ||
318 memcmp(shname
, "kretprobe/", 10) == 0 ||
319 memcmp(shname
, "socket", 6) == 0)
320 load_and_attach(shname
, data
->d_buf
, data
->d_size
);
327 void read_trace_pipe(void)
331 trace_fd
= open(DEBUGFS
"trace_pipe", O_RDONLY
, 0);
336 static char buf
[4096];
339 sz
= read(trace_fd
, buf
, sizeof(buf
));