1 // SPDX-License-Identifier: GPL-2.0
6 #include <bpf/libbpf.h>
9 #include <linux/string.h>
10 #include <internal/lib.h>
11 #include <symbol/kallsyms.h>
12 #include "bpf-event.h"
13 #include "bpf-utils.h"
23 #include "util/synthetic-events.h"
25 #ifndef HAVE_LIBBPF_BTF__LOAD_FROM_KERNEL_BY_ID
26 struct btf *btf__load_from_kernel_by_id(__u32 id)
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31 int err = btf__get_from_id(id, &btf);
32 #pragma GCC diagnostic pop
34 return err ? ERR_PTR(err) : btf;
38 #ifndef HAVE_LIBBPF_BPF_PROG_LOAD
39 int bpf_prog_load(enum bpf_prog_type prog_type,
40 const char *prog_name __maybe_unused,
42 const struct bpf_insn *insns, size_t insn_cnt,
43 const struct bpf_prog_load_opts *opts)
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
47 return bpf_load_program(prog_type, insns, insn_cnt, license,
48 opts->kern_version, opts->log_buf, opts->log_size);
49 #pragma GCC diagnostic pop
53 #ifndef HAVE_LIBBPF_BPF_OBJECT__NEXT_PROGRAM
55 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
57 #pragma GCC diagnostic push
58 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59 return bpf_program__next(prev, obj);
60 #pragma GCC diagnostic pop
64 #ifndef HAVE_LIBBPF_BPF_OBJECT__NEXT_MAP
66 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
70 return bpf_map__next(prev, obj);
71 #pragma GCC diagnostic pop
75 #ifndef HAVE_LIBBPF_BTF__RAW_DATA
77 btf__raw_data(const struct btf *btf_ro, __u32 *size)
79 #pragma GCC diagnostic push
80 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
81 return btf__get_raw_data(btf_ro, size);
82 #pragma GCC diagnostic pop
86 static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)
91 for (i = 0; i < len; i++)
92 ret += snprintf(buf + ret, size - ret, "%02x", data[i]);
96 static int machine__process_bpf_event_load(struct machine *machine,
97 union perf_event *event,
98 struct perf_sample *sample __maybe_unused)
100 struct bpf_prog_info_node *info_node;
101 struct perf_env *env = machine->env;
102 struct perf_bpil *info_linear;
103 int id = event->bpf.id;
106 /* perf-record, no need to handle bpf-event */
110 info_node = perf_env__find_bpf_prog_info(env, id);
113 info_linear = info_node->info_linear;
115 for (i = 0; i < info_linear->info.nr_jited_ksyms; i++) {
116 u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
118 struct map *map = maps__find(machine__kernel_maps(machine), addr);
121 map->dso->binary_type = DSO_BINARY_TYPE__BPF_PROG_INFO;
122 map->dso->bpf_prog.id = id;
123 map->dso->bpf_prog.sub_id = i;
124 map->dso->bpf_prog.env = env;
130 int machine__process_bpf(struct machine *machine, union perf_event *event,
131 struct perf_sample *sample)
134 perf_event__fprintf_bpf(event, stdout);
136 switch (event->bpf.type) {
137 case PERF_BPF_EVENT_PROG_LOAD:
138 return machine__process_bpf_event_load(machine, event, sample);
140 case PERF_BPF_EVENT_PROG_UNLOAD:
142 * Do not free bpf_prog_info and btf of the program here,
143 * as annotation still need them. They will be freed at
144 * the end of the session.
148 pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
154 static int perf_env__fetch_btf(struct perf_env *env,
158 struct btf_node *node;
162 data = btf__raw_data(btf, &data_size);
164 node = malloc(data_size + sizeof(struct btf_node));
169 node->data_size = data_size;
170 memcpy(node->data, data, data_size);
172 if (!perf_env__insert_btf(env, node)) {
173 /* Insertion failed because of a duplicate. */
180 static int synthesize_bpf_prog_name(char *buf, int size,
181 struct bpf_prog_info *info,
185 u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
186 void *func_infos = (void *)(uintptr_t)(info->func_info);
187 u32 sub_prog_cnt = info->nr_jited_ksyms;
188 const struct bpf_func_info *finfo;
189 const char *short_name = NULL;
190 const struct btf_type *t;
193 name_len = snprintf(buf, size, "bpf_prog_");
194 name_len += snprintf_hex(buf + name_len, size - name_len,
195 prog_tags[sub_id], BPF_TAG_SIZE);
197 finfo = func_infos + sub_id * info->func_info_rec_size;
198 t = btf__type_by_id(btf, finfo->type_id);
199 short_name = btf__name_by_offset(btf, t->name_off);
200 } else if (sub_id == 0 && sub_prog_cnt == 1) {
203 short_name = info->name;
207 name_len += snprintf(buf + name_len, size - name_len,
213 * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
214 * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
215 * one PERF_RECORD_KSYMBOL is generated for each sub program.
220 * -2 for lack of kernel support.
222 static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
223 perf_event__handler_t process,
224 struct machine *machine,
226 union perf_event *event,
227 struct record_opts *opts)
229 struct perf_record_ksymbol *ksymbol_event = &event->ksymbol;
230 struct perf_record_bpf_event *bpf_event = &event->bpf;
231 struct perf_tool *tool = session->tool;
232 struct bpf_prog_info_node *info_node;
233 struct perf_bpil *info_linear;
234 struct bpf_prog_info *info;
235 struct btf *btf = NULL;
236 struct perf_env *env;
242 * for perf-record and perf-report use header.env;
243 * otherwise, use global perf_env.
245 env = session->data ? &session->header.env : &perf_env;
247 arrays = 1UL << PERF_BPIL_JITED_KSYMS;
248 arrays |= 1UL << PERF_BPIL_JITED_FUNC_LENS;
249 arrays |= 1UL << PERF_BPIL_FUNC_INFO;
250 arrays |= 1UL << PERF_BPIL_PROG_TAGS;
251 arrays |= 1UL << PERF_BPIL_JITED_INSNS;
252 arrays |= 1UL << PERF_BPIL_LINE_INFO;
253 arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
255 info_linear = get_bpf_prog_info_linear(fd, arrays);
256 if (IS_ERR_OR_NULL(info_linear)) {
258 pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
262 if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
264 pr_debug("%s: the kernel is too old, aborting\n", __func__);
268 info = &info_linear->info;
269 if (!info->jited_ksyms) {
274 /* number of ksyms, func_lengths, and tags should match */
275 sub_prog_cnt = info->nr_jited_ksyms;
276 if (sub_prog_cnt != info->nr_prog_tags ||
277 sub_prog_cnt != info->nr_jited_func_lens) {
282 /* check BTF func info support */
283 if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
284 /* btf func info number should be same as sub_prog_cnt */
285 if (sub_prog_cnt != info->nr_func_info) {
286 pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
290 btf = btf__load_from_kernel_by_id(info->btf_id);
291 if (libbpf_get_error(btf)) {
292 pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
296 perf_env__fetch_btf(env, info->btf_id, btf);
299 /* Synthesize PERF_RECORD_KSYMBOL */
300 for (i = 0; i < sub_prog_cnt; i++) {
301 __u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
302 __u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
305 *ksymbol_event = (struct perf_record_ksymbol) {
307 .type = PERF_RECORD_KSYMBOL,
308 .size = offsetof(struct perf_record_ksymbol, name),
310 .addr = prog_addrs[i],
312 .ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
316 name_len = synthesize_bpf_prog_name(ksymbol_event->name,
317 KSYM_NAME_LEN, info, btf, i);
318 ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
321 memset((void *)event + event->header.size, 0, machine->id_hdr_size);
322 event->header.size += machine->id_hdr_size;
323 err = perf_tool__process_synth_event(tool, event,
327 if (!opts->no_bpf_event) {
328 /* Synthesize PERF_RECORD_BPF_EVENT */
329 *bpf_event = (struct perf_record_bpf_event) {
331 .type = PERF_RECORD_BPF_EVENT,
332 .size = sizeof(struct perf_record_bpf_event),
334 .type = PERF_BPF_EVENT_PROG_LOAD,
338 memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
339 memset((void *)event + event->header.size, 0, machine->id_hdr_size);
340 event->header.size += machine->id_hdr_size;
342 /* save bpf_prog_info to env */
343 info_node = malloc(sizeof(struct bpf_prog_info_node));
349 info_node->info_linear = info_linear;
350 perf_env__insert_bpf_prog_info(env, info_node);
354 * process after saving bpf_prog_info to env, so that
355 * required information is ready for look up
357 err = perf_tool__process_synth_event(tool, event,
367 struct kallsyms_parse {
368 union perf_event *event;
369 perf_event__handler_t process;
370 struct machine *machine;
371 struct perf_tool *tool;
375 process_bpf_image(char *name, u64 addr, struct kallsyms_parse *data)
377 struct machine *machine = data->machine;
378 union perf_event *event = data->event;
379 struct perf_record_ksymbol *ksymbol;
382 ksymbol = &event->ksymbol;
384 *ksymbol = (struct perf_record_ksymbol) {
386 .type = PERF_RECORD_KSYMBOL,
387 .size = offsetof(struct perf_record_ksymbol, name),
391 .ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
395 len = scnprintf(ksymbol->name, KSYM_NAME_LEN, "%s", name);
396 ksymbol->header.size += PERF_ALIGN(len + 1, sizeof(u64));
397 memset((void *) event + event->header.size, 0, machine->id_hdr_size);
398 event->header.size += machine->id_hdr_size;
400 return perf_tool__process_synth_event(data->tool, event, machine,
405 kallsyms_process_symbol(void *data, const char *_name,
406 char type __maybe_unused, u64 start)
408 char disp[KSYM_NAME_LEN];
413 module = strchr(_name, '\t');
417 /* We are going after [bpf] module ... */
418 if (strcmp(module + 1, "[bpf]"))
421 name = memdup(_name, (module - _name) + 1);
425 name[module - _name] = 0;
427 /* .. and only for trampolines and dispatchers */
428 if ((sscanf(name, "bpf_trampoline_%lu", &id) == 1) ||
429 (sscanf(name, "bpf_dispatcher_%s", disp) == 1))
430 err = process_bpf_image(name, start, data);
436 int perf_event__synthesize_bpf_events(struct perf_session *session,
437 perf_event__handler_t process,
438 struct machine *machine,
439 struct record_opts *opts)
441 const char *kallsyms_filename = "/proc/kallsyms";
442 struct kallsyms_parse arg;
443 union perf_event *event;
448 event = malloc(sizeof(event->bpf) + KSYM_NAME_LEN + machine->id_hdr_size);
452 /* Synthesize all the bpf programs in system. */
454 err = bpf_prog_get_next_id(id, &id);
456 if (errno == ENOENT) {
460 pr_debug("%s: can't get next program: %s%s\n",
461 __func__, strerror(errno),
462 errno == EINVAL ? " -- kernel too old?" : "");
463 /* don't report error on old kernel or EPERM */
464 err = (errno == EINVAL || errno == EPERM) ? 0 : -1;
467 fd = bpf_prog_get_fd_by_id(id);
469 pr_debug("%s: failed to get fd for prog_id %u\n",
474 err = perf_event__synthesize_one_bpf_prog(session, process,
479 /* do not return error for old kernel */
486 /* Synthesize all the bpf images - trampolines/dispatchers. */
487 if (symbol_conf.kallsyms_name != NULL)
488 kallsyms_filename = symbol_conf.kallsyms_name;
490 arg = (struct kallsyms_parse) {
494 .tool = session->tool,
497 if (kallsyms__parse(kallsyms_filename, &arg, kallsyms_process_symbol)) {
498 pr_err("%s: failed to synthesize bpf images: %s\n",
499 __func__, strerror(errno));
506 static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
508 struct bpf_prog_info_node *info_node;
509 struct perf_bpil *info_linear;
510 struct btf *btf = NULL;
515 fd = bpf_prog_get_fd_by_id(id);
519 arrays = 1UL << PERF_BPIL_JITED_KSYMS;
520 arrays |= 1UL << PERF_BPIL_JITED_FUNC_LENS;
521 arrays |= 1UL << PERF_BPIL_FUNC_INFO;
522 arrays |= 1UL << PERF_BPIL_PROG_TAGS;
523 arrays |= 1UL << PERF_BPIL_JITED_INSNS;
524 arrays |= 1UL << PERF_BPIL_LINE_INFO;
525 arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
527 info_linear = get_bpf_prog_info_linear(fd, arrays);
528 if (IS_ERR_OR_NULL(info_linear)) {
529 pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
533 btf_id = info_linear->info.btf_id;
535 info_node = malloc(sizeof(struct bpf_prog_info_node));
537 info_node->info_linear = info_linear;
538 perf_env__insert_bpf_prog_info(env, info_node);
545 btf = btf__load_from_kernel_by_id(btf_id);
546 if (libbpf_get_error(btf)) {
547 pr_debug("%s: failed to get BTF of id %u, aborting\n",
551 perf_env__fetch_btf(env, btf_id, btf);
558 static int bpf_event__sb_cb(union perf_event *event, void *data)
560 struct perf_env *env = data;
562 if (event->header.type != PERF_RECORD_BPF_EVENT)
565 switch (event->bpf.type) {
566 case PERF_BPF_EVENT_PROG_LOAD:
567 perf_env__add_bpf_info(env, event->bpf.id);
569 case PERF_BPF_EVENT_PROG_UNLOAD:
571 * Do not free bpf_prog_info and btf of the program here,
572 * as annotation still need them. They will be freed at
573 * the end of the session.
577 pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
584 int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)
586 struct perf_event_attr attr = {
587 .type = PERF_TYPE_SOFTWARE,
588 .config = PERF_COUNT_SW_DUMMY,
592 .size = sizeof(attr), /* to capture ABI version */
596 * Older gcc versions don't support designated initializers, like above,
597 * for unnamed union members, such as the following:
599 attr.wakeup_watermark = 1;
601 return evlist__add_sb_event(evlist, &attr, bpf_event__sb_cb, env);
604 void bpf_event__print_bpf_prog_info(struct bpf_prog_info *info,
605 struct perf_env *env,
608 __u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
609 __u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
610 char name[KSYM_NAME_LEN];
611 struct btf *btf = NULL;
614 sub_prog_cnt = info->nr_jited_ksyms;
615 if (sub_prog_cnt != info->nr_prog_tags ||
616 sub_prog_cnt != info->nr_jited_func_lens)
620 struct btf_node *node;
622 node = perf_env__find_btf(env, info->btf_id);
624 btf = btf__new((__u8 *)(node->data),
628 if (sub_prog_cnt == 1) {
629 synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, 0);
630 fprintf(fp, "# bpf_prog_info %u: %s addr 0x%llx size %u\n",
631 info->id, name, prog_addrs[0], prog_lens[0]);
635 fprintf(fp, "# bpf_prog_info %u:\n", info->id);
636 for (i = 0; i < sub_prog_cnt; i++) {
637 synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, i);
639 fprintf(fp, "# \tsub_prog %u: %s addr 0x%llx size %u\n",
640 i, name, prog_addrs[i], prog_lens[i]);