1 // SPDX-License-Identifier: GPL-2.0
6 #include <bpf/libbpf.h>
18 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
20 static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)
25 for (i = 0; i < len; i++)
26 ret += snprintf(buf + ret, size - ret, "%02x", data[i]);
30 static int machine__process_bpf_event_load(struct machine *machine,
31 union perf_event *event,
32 struct perf_sample *sample __maybe_unused)
34 struct bpf_prog_info_linear *info_linear;
35 struct bpf_prog_info_node *info_node;
36 struct perf_env *env = machine->env;
37 int id = event->bpf_event.id;
40 /* perf-record, no need to handle bpf-event */
44 info_node = perf_env__find_bpf_prog_info(env, id);
47 info_linear = info_node->info_linear;
49 for (i = 0; i < info_linear->info.nr_jited_ksyms; i++) {
50 u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
54 map = map_groups__find(&machine->kmaps, addr);
57 map->dso->binary_type = DSO_BINARY_TYPE__BPF_PROG_INFO;
58 map->dso->bpf_prog.id = id;
59 map->dso->bpf_prog.sub_id = i;
60 map->dso->bpf_prog.env = env;
66 int machine__process_bpf_event(struct machine *machine __maybe_unused,
67 union perf_event *event,
68 struct perf_sample *sample __maybe_unused)
71 perf_event__fprintf_bpf_event(event, stdout);
73 switch (event->bpf_event.type) {
74 case PERF_BPF_EVENT_PROG_LOAD:
75 return machine__process_bpf_event_load(machine, event, sample);
77 case PERF_BPF_EVENT_PROG_UNLOAD:
79 * Do not free bpf_prog_info and btf of the program here,
80 * as annotation still need them. They will be freed at
81 * the end of the session.
85 pr_debug("unexpected bpf_event type of %d\n",
86 event->bpf_event.type);
92 static int perf_env__fetch_btf(struct perf_env *env,
96 struct btf_node *node;
100 data = btf__get_raw_data(btf, &data_size);
102 node = malloc(data_size + sizeof(struct btf_node));
107 node->data_size = data_size;
108 memcpy(node->data, data, data_size);
110 perf_env__insert_btf(env, node);
115 * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
116 * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
117 * one PERF_RECORD_KSYMBOL is generated for each sub program.
122 * -2 for lack of kernel support.
124 static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
125 perf_event__handler_t process,
126 struct machine *machine,
128 union perf_event *event,
129 struct record_opts *opts)
131 struct ksymbol_event *ksymbol_event = &event->ksymbol_event;
132 struct bpf_event *bpf_event = &event->bpf_event;
133 struct bpf_prog_info_linear *info_linear;
134 struct perf_tool *tool = session->tool;
135 struct bpf_prog_info_node *info_node;
136 struct bpf_prog_info *info;
137 struct btf *btf = NULL;
138 bool has_btf = false;
139 struct perf_env *env;
145 * for perf-record and perf-report use header.env;
146 * otherwise, use global perf_env.
148 env = session->data ? &session->header.env : &perf_env;
150 arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
151 arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
152 arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
153 arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
154 arrays |= 1UL << BPF_PROG_INFO_JITED_INSNS;
155 arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
156 arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
158 info_linear = bpf_program__get_prog_info_linear(fd, arrays);
159 if (IS_ERR_OR_NULL(info_linear)) {
161 pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
165 if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
166 pr_debug("%s: the kernel is too old, aborting\n", __func__);
170 info = &info_linear->info;
172 /* number of ksyms, func_lengths, and tags should match */
173 sub_prog_cnt = info->nr_jited_ksyms;
174 if (sub_prog_cnt != info->nr_prog_tags ||
175 sub_prog_cnt != info->nr_jited_func_lens)
178 /* check BTF func info support */
179 if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
180 /* btf func info number should be same as sub_prog_cnt */
181 if (sub_prog_cnt != info->nr_func_info) {
182 pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
186 if (btf__get_from_id(info->btf_id, &btf)) {
187 pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
193 perf_env__fetch_btf(env, info->btf_id, btf);
196 /* Synthesize PERF_RECORD_KSYMBOL */
197 for (i = 0; i < sub_prog_cnt; i++) {
198 u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
199 __u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
200 __u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
201 void *func_infos = (void *)(uintptr_t)(info->func_info);
202 const struct bpf_func_info *finfo;
203 const char *short_name = NULL;
204 const struct btf_type *t;
207 *ksymbol_event = (struct ksymbol_event){
209 .type = PERF_RECORD_KSYMBOL,
210 .size = offsetof(struct ksymbol_event, name),
212 .addr = prog_addrs[i],
214 .ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
217 name_len = snprintf(ksymbol_event->name, KSYM_NAME_LEN,
219 name_len += snprintf_hex(ksymbol_event->name + name_len,
220 KSYM_NAME_LEN - name_len,
221 prog_tags[i], BPF_TAG_SIZE);
223 finfo = func_infos + i * info->func_info_rec_size;
224 t = btf__type_by_id(btf, finfo->type_id);
225 short_name = btf__name_by_offset(btf, t->name_off);
226 } else if (i == 0 && sub_prog_cnt == 1) {
229 short_name = info->name;
233 name_len += snprintf(ksymbol_event->name + name_len,
234 KSYM_NAME_LEN - name_len,
237 ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
240 memset((void *)event + event->header.size, 0, machine->id_hdr_size);
241 event->header.size += machine->id_hdr_size;
242 err = perf_tool__process_synth_event(tool, event,
246 if (!opts->no_bpf_event) {
247 /* Synthesize PERF_RECORD_BPF_EVENT */
248 *bpf_event = (struct bpf_event){
250 .type = PERF_RECORD_BPF_EVENT,
251 .size = sizeof(struct bpf_event),
253 .type = PERF_BPF_EVENT_PROG_LOAD,
257 memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
258 memset((void *)event + event->header.size, 0, machine->id_hdr_size);
259 event->header.size += machine->id_hdr_size;
261 /* save bpf_prog_info to env */
262 info_node = malloc(sizeof(struct bpf_prog_info_node));
268 info_node->info_linear = info_linear;
269 perf_env__insert_bpf_prog_info(env, info_node);
273 * process after saving bpf_prog_info to env, so that
274 * required information is ready for look up
276 err = perf_tool__process_synth_event(tool, event,
286 int perf_event__synthesize_bpf_events(struct perf_session *session,
287 perf_event__handler_t process,
288 struct machine *machine,
289 struct record_opts *opts)
291 union perf_event *event;
296 event = malloc(sizeof(event->bpf_event) + KSYM_NAME_LEN + machine->id_hdr_size);
300 err = bpf_prog_get_next_id(id, &id);
302 if (errno == ENOENT) {
306 pr_debug("%s: can't get next program: %s%s\n",
307 __func__, strerror(errno),
308 errno == EINVAL ? " -- kernel too old?" : "");
309 /* don't report error on old kernel or EPERM */
310 err = (errno == EINVAL || errno == EPERM) ? 0 : -1;
313 fd = bpf_prog_get_fd_by_id(id);
315 pr_debug("%s: failed to get fd for prog_id %u\n",
320 err = perf_event__synthesize_one_bpf_prog(session, process,
325 /* do not return error for old kernel */
335 static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
337 struct bpf_prog_info_linear *info_linear;
338 struct bpf_prog_info_node *info_node;
339 struct btf *btf = NULL;
344 fd = bpf_prog_get_fd_by_id(id);
348 arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
349 arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
350 arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
351 arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
352 arrays |= 1UL << BPF_PROG_INFO_JITED_INSNS;
353 arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
354 arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
356 info_linear = bpf_program__get_prog_info_linear(fd, arrays);
357 if (IS_ERR_OR_NULL(info_linear)) {
358 pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
362 btf_id = info_linear->info.btf_id;
364 info_node = malloc(sizeof(struct bpf_prog_info_node));
366 info_node->info_linear = info_linear;
367 perf_env__insert_bpf_prog_info(env, info_node);
374 if (btf__get_from_id(btf_id, &btf)) {
375 pr_debug("%s: failed to get BTF of id %u, aborting\n",
379 perf_env__fetch_btf(env, btf_id, btf);
386 static int bpf_event__sb_cb(union perf_event *event, void *data)
388 struct perf_env *env = data;
390 if (event->header.type != PERF_RECORD_BPF_EVENT)
393 switch (event->bpf_event.type) {
394 case PERF_BPF_EVENT_PROG_LOAD:
395 perf_env__add_bpf_info(env, event->bpf_event.id);
397 case PERF_BPF_EVENT_PROG_UNLOAD:
399 * Do not free bpf_prog_info and btf of the program here,
400 * as annotation still need them. They will be freed at
401 * the end of the session.
405 pr_debug("unexpected bpf_event type of %d\n",
406 event->bpf_event.type);
413 int bpf_event__add_sb_event(struct perf_evlist **evlist,
414 struct perf_env *env)
416 struct perf_event_attr attr = {
417 .type = PERF_TYPE_SOFTWARE,
418 .config = PERF_COUNT_SW_DUMMY,
422 .size = sizeof(attr), /* to capture ABI version */
426 * Older gcc versions don't support designated initializers, like above,
427 * for unnamed union members, such as the following:
429 attr.wakeup_watermark = 1;
431 return perf_evlist__add_sb_event(evlist, &attr, bpf_event__sb_cb, env);