Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2#include <errno.h>
  3#include <stdlib.h>
  4#include <bpf/bpf.h>
  5#include <bpf/btf.h>
  6#include <bpf/libbpf.h>
  7#include <linux/btf.h>
  8#include <linux/err.h>
  9#include <linux/string.h>
 10#include <internal/lib.h>
 11#include <symbol/kallsyms.h>
 12#include "bpf-event.h"
 
 13#include "debug.h"
 14#include "dso.h"
 15#include "symbol.h"
 16#include "machine.h"
 17#include "env.h"
 18#include "session.h"
 19#include "map.h"
 20#include "evlist.h"
 21#include "record.h"
 22#include "util/synthetic-events.h"
 23
 24#define ptr_to_u64(ptr)    ((__u64)(unsigned long)(ptr))
 25
 26static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)
 27{
 28	int ret = 0;
 29	size_t i;
 30
 31	for (i = 0; i < len; i++)
 32		ret += snprintf(buf + ret, size - ret, "%02x", data[i]);
 33	return ret;
 34}
 35
 36static int machine__process_bpf_event_load(struct machine *machine,
 37					   union perf_event *event,
 38					   struct perf_sample *sample __maybe_unused)
 39{
 40	struct bpf_prog_info_linear *info_linear;
 41	struct bpf_prog_info_node *info_node;
 42	struct perf_env *env = machine->env;
 
 43	int id = event->bpf.id;
 44	unsigned int i;
 45
 46	/* perf-record, no need to handle bpf-event */
 47	if (env == NULL)
 48		return 0;
 49
 50	info_node = perf_env__find_bpf_prog_info(env, id);
 51	if (!info_node)
 52		return 0;
 53	info_linear = info_node->info_linear;
 54
 55	for (i = 0; i < info_linear->info.nr_jited_ksyms; i++) {
 56		u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
 57		u64 addr = addrs[i];
 58		struct map *map = maps__find(&machine->kmaps, addr);
 59
 60		if (map) {
 61			map->dso->binary_type = DSO_BINARY_TYPE__BPF_PROG_INFO;
 62			map->dso->bpf_prog.id = id;
 63			map->dso->bpf_prog.sub_id = i;
 64			map->dso->bpf_prog.env = env;
 
 
 65		}
 66	}
 67	return 0;
 68}
 69
 70int machine__process_bpf(struct machine *machine, union perf_event *event,
 71			 struct perf_sample *sample)
 72{
 73	if (dump_trace)
 74		perf_event__fprintf_bpf(event, stdout);
 75
 76	switch (event->bpf.type) {
 77	case PERF_BPF_EVENT_PROG_LOAD:
 78		return machine__process_bpf_event_load(machine, event, sample);
 79
 80	case PERF_BPF_EVENT_PROG_UNLOAD:
 81		/*
 82		 * Do not free bpf_prog_info and btf of the program here,
 83		 * as annotation still need them. They will be freed at
 84		 * the end of the session.
 85		 */
 86		break;
 87	default:
 88		pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
 89		break;
 90	}
 91	return 0;
 92}
 93
 94static int perf_env__fetch_btf(struct perf_env *env,
 95			       u32 btf_id,
 96			       struct btf *btf)
 97{
 98	struct btf_node *node;
 99	u32 data_size;
100	const void *data;
101
102	data = btf__get_raw_data(btf, &data_size);
103
104	node = malloc(data_size + sizeof(struct btf_node));
105	if (!node)
106		return -1;
107
108	node->id = btf_id;
109	node->data_size = data_size;
110	memcpy(node->data, data, data_size);
111
112	perf_env__insert_btf(env, node);
 
 
 
 
113	return 0;
114}
115
116static int synthesize_bpf_prog_name(char *buf, int size,
117				    struct bpf_prog_info *info,
118				    struct btf *btf,
119				    u32 sub_id)
120{
121	u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
122	void *func_infos = (void *)(uintptr_t)(info->func_info);
123	u32 sub_prog_cnt = info->nr_jited_ksyms;
124	const struct bpf_func_info *finfo;
125	const char *short_name = NULL;
126	const struct btf_type *t;
127	int name_len;
128
129	name_len = snprintf(buf, size, "bpf_prog_");
130	name_len += snprintf_hex(buf + name_len, size - name_len,
131				 prog_tags[sub_id], BPF_TAG_SIZE);
132	if (btf) {
133		finfo = func_infos + sub_id * info->func_info_rec_size;
134		t = btf__type_by_id(btf, finfo->type_id);
135		short_name = btf__name_by_offset(btf, t->name_off);
136	} else if (sub_id == 0 && sub_prog_cnt == 1) {
137		/* no subprog */
138		if (info->name[0])
139			short_name = info->name;
140	} else
141		short_name = "F";
142	if (short_name)
143		name_len += snprintf(buf + name_len, size - name_len,
144				     "_%s", short_name);
145	return name_len;
146}
147
148/*
149 * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
150 * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
151 * one PERF_RECORD_KSYMBOL is generated for each sub program.
152 *
153 * Returns:
154 *    0 for success;
155 *   -1 for failures;
156 *   -2 for lack of kernel support.
157 */
158static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
159					       perf_event__handler_t process,
160					       struct machine *machine,
161					       int fd,
162					       union perf_event *event,
163					       struct record_opts *opts)
164{
165	struct perf_record_ksymbol *ksymbol_event = &event->ksymbol;
166	struct perf_record_bpf_event *bpf_event = &event->bpf;
167	struct bpf_prog_info_linear *info_linear;
168	struct perf_tool *tool = session->tool;
169	struct bpf_prog_info_node *info_node;
 
170	struct bpf_prog_info *info;
171	struct btf *btf = NULL;
172	struct perf_env *env;
173	u32 sub_prog_cnt, i;
174	int err = 0;
175	u64 arrays;
176
177	/*
178	 * for perf-record and perf-report use header.env;
179	 * otherwise, use global perf_env.
180	 */
181	env = session->data ? &session->header.env : &perf_env;
182
183	arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
184	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
185	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
186	arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
187	arrays |= 1UL << BPF_PROG_INFO_JITED_INSNS;
188	arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
189	arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
190
191	info_linear = bpf_program__get_prog_info_linear(fd, arrays);
192	if (IS_ERR_OR_NULL(info_linear)) {
193		info_linear = NULL;
194		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
195		return -1;
196	}
197
198	if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
199		free(info_linear);
200		pr_debug("%s: the kernel is too old, aborting\n", __func__);
201		return -2;
202	}
203
204	info = &info_linear->info;
205	if (!info->jited_ksyms) {
206		free(info_linear);
207		return -1;
208	}
209
210	/* number of ksyms, func_lengths, and tags should match */
211	sub_prog_cnt = info->nr_jited_ksyms;
212	if (sub_prog_cnt != info->nr_prog_tags ||
213	    sub_prog_cnt != info->nr_jited_func_lens) {
214		free(info_linear);
215		return -1;
216	}
217
218	/* check BTF func info support */
219	if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
220		/* btf func info number should be same as sub_prog_cnt */
221		if (sub_prog_cnt != info->nr_func_info) {
222			pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
223			free(info_linear);
224			return -1;
225		}
226		if (btf__get_from_id(info->btf_id, &btf)) {
 
227			pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
228			err = -1;
229			btf = NULL;
230			goto out;
231		}
232		perf_env__fetch_btf(env, info->btf_id, btf);
233	}
234
235	/* Synthesize PERF_RECORD_KSYMBOL */
236	for (i = 0; i < sub_prog_cnt; i++) {
237		__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
238		__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
239		int name_len;
240
241		*ksymbol_event = (struct perf_record_ksymbol) {
242			.header = {
243				.type = PERF_RECORD_KSYMBOL,
244				.size = offsetof(struct perf_record_ksymbol, name),
245			},
246			.addr = prog_addrs[i],
247			.len = prog_lens[i],
248			.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
249			.flags = 0,
250		};
251
252		name_len = synthesize_bpf_prog_name(ksymbol_event->name,
253						    KSYM_NAME_LEN, info, btf, i);
254		ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
255							 sizeof(u64));
256
257		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
258		event->header.size += machine->id_hdr_size;
259		err = perf_tool__process_synth_event(tool, event,
260						     machine, process);
261	}
262
263	if (!opts->no_bpf_event) {
264		/* Synthesize PERF_RECORD_BPF_EVENT */
265		*bpf_event = (struct perf_record_bpf_event) {
266			.header = {
267				.type = PERF_RECORD_BPF_EVENT,
268				.size = sizeof(struct perf_record_bpf_event),
269			},
270			.type = PERF_BPF_EVENT_PROG_LOAD,
271			.flags = 0,
272			.id = info->id,
273		};
274		memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
275		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
276		event->header.size += machine->id_hdr_size;
277
278		/* save bpf_prog_info to env */
279		info_node = malloc(sizeof(struct bpf_prog_info_node));
280		if (!info_node) {
281			err = -1;
282			goto out;
283		}
284
285		info_node->info_linear = info_linear;
286		perf_env__insert_bpf_prog_info(env, info_node);
287		info_linear = NULL;
288
289		/*
290		 * process after saving bpf_prog_info to env, so that
291		 * required information is ready for look up
292		 */
293		err = perf_tool__process_synth_event(tool, event,
294						     machine, process);
295	}
296
297out:
298	free(info_linear);
299	btf__free(btf);
300	return err ? -1 : 0;
301}
302
303struct kallsyms_parse {
304	union perf_event	*event;
305	perf_event__handler_t	 process;
306	struct machine		*machine;
307	struct perf_tool	*tool;
308};
309
310static int
311process_bpf_image(char *name, u64 addr, struct kallsyms_parse *data)
312{
313	struct machine *machine = data->machine;
314	union perf_event *event = data->event;
315	struct perf_record_ksymbol *ksymbol;
316	int len;
317
318	ksymbol = &event->ksymbol;
319
320	*ksymbol = (struct perf_record_ksymbol) {
321		.header = {
322			.type = PERF_RECORD_KSYMBOL,
323			.size = offsetof(struct perf_record_ksymbol, name),
324		},
325		.addr      = addr,
326		.len       = page_size,
327		.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
328		.flags     = 0,
329	};
330
331	len = scnprintf(ksymbol->name, KSYM_NAME_LEN, "%s", name);
332	ksymbol->header.size += PERF_ALIGN(len + 1, sizeof(u64));
333	memset((void *) event + event->header.size, 0, machine->id_hdr_size);
334	event->header.size += machine->id_hdr_size;
335
336	return perf_tool__process_synth_event(data->tool, event, machine,
337					      data->process);
338}
339
340static int
341kallsyms_process_symbol(void *data, const char *_name,
342			char type __maybe_unused, u64 start)
343{
344	char disp[KSYM_NAME_LEN];
345	char *module, *name;
346	unsigned long id;
347	int err = 0;
348
349	module = strchr(_name, '\t');
350	if (!module)
351		return 0;
352
353	/* We are going after [bpf] module ... */
354	if (strcmp(module + 1, "[bpf]"))
355		return 0;
356
357	name = memdup(_name, (module - _name) + 1);
358	if (!name)
359		return -ENOMEM;
360
361	name[module - _name] = 0;
362
363	/* .. and only for trampolines and dispatchers */
364	if ((sscanf(name, "bpf_trampoline_%lu", &id) == 1) ||
365	    (sscanf(name, "bpf_dispatcher_%s", disp) == 1))
366		err = process_bpf_image(name, start, data);
367
368	free(name);
369	return err;
370}
371
372int perf_event__synthesize_bpf_events(struct perf_session *session,
373				      perf_event__handler_t process,
374				      struct machine *machine,
375				      struct record_opts *opts)
376{
377	const char *kallsyms_filename = "/proc/kallsyms";
378	struct kallsyms_parse arg;
379	union perf_event *event;
380	__u32 id = 0;
381	int err;
382	int fd;
383
 
 
 
384	event = malloc(sizeof(event->bpf) + KSYM_NAME_LEN + machine->id_hdr_size);
385	if (!event)
386		return -1;
387
388	/* Synthesize all the bpf programs in system. */
389	while (true) {
390		err = bpf_prog_get_next_id(id, &id);
391		if (err) {
392			if (errno == ENOENT) {
393				err = 0;
394				break;
395			}
396			pr_debug("%s: can't get next program: %s%s\n",
397				 __func__, strerror(errno),
398				 errno == EINVAL ? " -- kernel too old?" : "");
399			/* don't report error on old kernel or EPERM  */
400			err = (errno == EINVAL || errno == EPERM) ? 0 : -1;
401			break;
402		}
403		fd = bpf_prog_get_fd_by_id(id);
404		if (fd < 0) {
405			pr_debug("%s: failed to get fd for prog_id %u\n",
406				 __func__, id);
407			continue;
408		}
409
410		err = perf_event__synthesize_one_bpf_prog(session, process,
411							  machine, fd,
412							  event, opts);
413		close(fd);
414		if (err) {
415			/* do not return error for old kernel */
416			if (err == -2)
417				err = 0;
418			break;
419		}
420	}
421
422	/* Synthesize all the bpf images - trampolines/dispatchers. */
423	if (symbol_conf.kallsyms_name != NULL)
424		kallsyms_filename = symbol_conf.kallsyms_name;
425
426	arg = (struct kallsyms_parse) {
427		.event   = event,
428		.process = process,
429		.machine = machine,
430		.tool    = session->tool,
431	};
432
433	if (kallsyms__parse(kallsyms_filename, &arg, kallsyms_process_symbol)) {
434		pr_err("%s: failed to synthesize bpf images: %s\n",
435		       __func__, strerror(errno));
436	}
437
438	free(event);
439	return err;
440}
441
442static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
443{
444	struct bpf_prog_info_linear *info_linear;
445	struct bpf_prog_info_node *info_node;
 
446	struct btf *btf = NULL;
447	u64 arrays;
448	u32 btf_id;
449	int fd;
450
451	fd = bpf_prog_get_fd_by_id(id);
452	if (fd < 0)
453		return;
454
455	arrays = 1UL << BPF_PROG_INFO_JITED_KSYMS;
456	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
457	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
458	arrays |= 1UL << BPF_PROG_INFO_PROG_TAGS;
459	arrays |= 1UL << BPF_PROG_INFO_JITED_INSNS;
460	arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
461	arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
462
463	info_linear = bpf_program__get_prog_info_linear(fd, arrays);
464	if (IS_ERR_OR_NULL(info_linear)) {
465		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
466		goto out;
467	}
468
469	btf_id = info_linear->info.btf_id;
470
471	info_node = malloc(sizeof(struct bpf_prog_info_node));
472	if (info_node) {
473		info_node->info_linear = info_linear;
474		perf_env__insert_bpf_prog_info(env, info_node);
475	} else
476		free(info_linear);
477
478	if (btf_id == 0)
479		goto out;
480
481	if (btf__get_from_id(btf_id, &btf)) {
 
482		pr_debug("%s: failed to get BTF of id %u, aborting\n",
483			 __func__, btf_id);
484		goto out;
485	}
486	perf_env__fetch_btf(env, btf_id, btf);
487
488out:
489	btf__free(btf);
490	close(fd);
491}
492
493static int bpf_event__sb_cb(union perf_event *event, void *data)
494{
495	struct perf_env *env = data;
496
497	if (event->header.type != PERF_RECORD_BPF_EVENT)
498		return -1;
499
500	switch (event->bpf.type) {
501	case PERF_BPF_EVENT_PROG_LOAD:
502		perf_env__add_bpf_info(env, event->bpf.id);
503
504	case PERF_BPF_EVENT_PROG_UNLOAD:
505		/*
506		 * Do not free bpf_prog_info and btf of the program here,
507		 * as annotation still need them. They will be freed at
508		 * the end of the session.
509		 */
510		break;
511	default:
512		pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
513		break;
514	}
515
516	return 0;
517}
518
519int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)
520{
521	struct perf_event_attr attr = {
522		.type	          = PERF_TYPE_SOFTWARE,
523		.config           = PERF_COUNT_SW_DUMMY,
524		.sample_id_all    = 1,
525		.watermark        = 1,
526		.bpf_event        = 1,
527		.size	   = sizeof(attr), /* to capture ABI version */
528	};
529
530	/*
531	 * Older gcc versions don't support designated initializers, like above,
532	 * for unnamed union members, such as the following:
533	 */
534	attr.wakeup_watermark = 1;
535
536	return evlist__add_sb_event(evlist, &attr, bpf_event__sb_cb, env);
537}
538
539void bpf_event__print_bpf_prog_info(struct bpf_prog_info *info,
540				    struct perf_env *env,
541				    FILE *fp)
542{
543	__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
544	__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
545	char name[KSYM_NAME_LEN];
546	struct btf *btf = NULL;
547	u32 sub_prog_cnt, i;
548
549	sub_prog_cnt = info->nr_jited_ksyms;
550	if (sub_prog_cnt != info->nr_prog_tags ||
551	    sub_prog_cnt != info->nr_jited_func_lens)
552		return;
553
554	if (info->btf_id) {
555		struct btf_node *node;
556
557		node = perf_env__find_btf(env, info->btf_id);
558		if (node)
559			btf = btf__new((__u8 *)(node->data),
560				       node->data_size);
561	}
562
563	if (sub_prog_cnt == 1) {
564		synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, 0);
565		fprintf(fp, "# bpf_prog_info %u: %s addr 0x%llx size %u\n",
566			info->id, name, prog_addrs[0], prog_lens[0]);
567		return;
568	}
569
570	fprintf(fp, "# bpf_prog_info %u:\n", info->id);
571	for (i = 0; i < sub_prog_cnt; i++) {
572		synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, i);
573
574		fprintf(fp, "# \tsub_prog %u: %s addr 0x%llx size %u\n",
575			i, name, prog_addrs[i], prog_lens[i]);
576	}
 
 
577}
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2#include <errno.h>
  3#include <stdlib.h>
  4#include <bpf/bpf.h>
  5#include <bpf/btf.h>
  6#include <bpf/libbpf.h>
  7#include <linux/btf.h>
  8#include <linux/err.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"
 14#include "debug.h"
 15#include "dso.h"
 16#include "symbol.h"
 17#include "machine.h"
 18#include "env.h"
 19#include "session.h"
 20#include "map.h"
 21#include "evlist.h"
 22#include "record.h"
 23#include "util/synthetic-events.h"
 24
 
 
 25static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)
 26{
 27	int ret = 0;
 28	size_t i;
 29
 30	for (i = 0; i < len; i++)
 31		ret += snprintf(buf + ret, size - ret, "%02x", data[i]);
 32	return ret;
 33}
 34
 35static int machine__process_bpf_event_load(struct machine *machine,
 36					   union perf_event *event,
 37					   struct perf_sample *sample __maybe_unused)
 38{
 
 39	struct bpf_prog_info_node *info_node;
 40	struct perf_env *env = machine->env;
 41	struct perf_bpil *info_linear;
 42	int id = event->bpf.id;
 43	unsigned int i;
 44
 45	/* perf-record, no need to handle bpf-event */
 46	if (env == NULL)
 47		return 0;
 48
 49	info_node = perf_env__find_bpf_prog_info(env, id);
 50	if (!info_node)
 51		return 0;
 52	info_linear = info_node->info_linear;
 53
 54	for (i = 0; i < info_linear->info.nr_jited_ksyms; i++) {
 55		u64 *addrs = (u64 *)(uintptr_t)(info_linear->info.jited_ksyms);
 56		u64 addr = addrs[i];
 57		struct map *map = maps__find(machine__kernel_maps(machine), addr);
 58
 59		if (map) {
 60			struct dso *dso = map__dso(map);
 61
 62			dso->binary_type = DSO_BINARY_TYPE__BPF_PROG_INFO;
 63			dso->bpf_prog.id = id;
 64			dso->bpf_prog.sub_id = i;
 65			dso->bpf_prog.env = env;
 66		}
 67	}
 68	return 0;
 69}
 70
 71int machine__process_bpf(struct machine *machine, union perf_event *event,
 72			 struct perf_sample *sample)
 73{
 74	if (dump_trace)
 75		perf_event__fprintf_bpf(event, stdout);
 76
 77	switch (event->bpf.type) {
 78	case PERF_BPF_EVENT_PROG_LOAD:
 79		return machine__process_bpf_event_load(machine, event, sample);
 80
 81	case PERF_BPF_EVENT_PROG_UNLOAD:
 82		/*
 83		 * Do not free bpf_prog_info and btf of the program here,
 84		 * as annotation still need them. They will be freed at
 85		 * the end of the session.
 86		 */
 87		break;
 88	default:
 89		pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
 90		break;
 91	}
 92	return 0;
 93}
 94
 95static int perf_env__fetch_btf(struct perf_env *env,
 96			       u32 btf_id,
 97			       struct btf *btf)
 98{
 99	struct btf_node *node;
100	u32 data_size;
101	const void *data;
102
103	data = btf__raw_data(btf, &data_size);
104
105	node = malloc(data_size + sizeof(struct btf_node));
106	if (!node)
107		return -1;
108
109	node->id = btf_id;
110	node->data_size = data_size;
111	memcpy(node->data, data, data_size);
112
113	if (!perf_env__insert_btf(env, node)) {
114		/* Insertion failed because of a duplicate. */
115		free(node);
116		return -1;
117	}
118	return 0;
119}
120
121static int synthesize_bpf_prog_name(char *buf, int size,
122				    struct bpf_prog_info *info,
123				    struct btf *btf,
124				    u32 sub_id)
125{
126	u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
127	void *func_infos = (void *)(uintptr_t)(info->func_info);
128	u32 sub_prog_cnt = info->nr_jited_ksyms;
129	const struct bpf_func_info *finfo;
130	const char *short_name = NULL;
131	const struct btf_type *t;
132	int name_len;
133
134	name_len = snprintf(buf, size, "bpf_prog_");
135	name_len += snprintf_hex(buf + name_len, size - name_len,
136				 prog_tags[sub_id], BPF_TAG_SIZE);
137	if (btf) {
138		finfo = func_infos + sub_id * info->func_info_rec_size;
139		t = btf__type_by_id(btf, finfo->type_id);
140		short_name = btf__name_by_offset(btf, t->name_off);
141	} else if (sub_id == 0 && sub_prog_cnt == 1) {
142		/* no subprog */
143		if (info->name[0])
144			short_name = info->name;
145	} else
146		short_name = "F";
147	if (short_name)
148		name_len += snprintf(buf + name_len, size - name_len,
149				     "_%s", short_name);
150	return name_len;
151}
152
153/*
154 * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
155 * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
156 * one PERF_RECORD_KSYMBOL is generated for each sub program.
157 *
158 * Returns:
159 *    0 for success;
160 *   -1 for failures;
161 *   -2 for lack of kernel support.
162 */
163static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
164					       perf_event__handler_t process,
165					       struct machine *machine,
166					       int fd,
167					       union perf_event *event,
168					       struct record_opts *opts)
169{
170	struct perf_record_ksymbol *ksymbol_event = &event->ksymbol;
171	struct perf_record_bpf_event *bpf_event = &event->bpf;
 
172	struct perf_tool *tool = session->tool;
173	struct bpf_prog_info_node *info_node;
174	struct perf_bpil *info_linear;
175	struct bpf_prog_info *info;
176	struct btf *btf = NULL;
177	struct perf_env *env;
178	u32 sub_prog_cnt, i;
179	int err = 0;
180	u64 arrays;
181
182	/*
183	 * for perf-record and perf-report use header.env;
184	 * otherwise, use global perf_env.
185	 */
186	env = session->data ? &session->header.env : &perf_env;
187
188	arrays = 1UL << PERF_BPIL_JITED_KSYMS;
189	arrays |= 1UL << PERF_BPIL_JITED_FUNC_LENS;
190	arrays |= 1UL << PERF_BPIL_FUNC_INFO;
191	arrays |= 1UL << PERF_BPIL_PROG_TAGS;
192	arrays |= 1UL << PERF_BPIL_JITED_INSNS;
193	arrays |= 1UL << PERF_BPIL_LINE_INFO;
194	arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
195
196	info_linear = get_bpf_prog_info_linear(fd, arrays);
197	if (IS_ERR_OR_NULL(info_linear)) {
198		info_linear = NULL;
199		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
200		return -1;
201	}
202
203	if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) {
204		free(info_linear);
205		pr_debug("%s: the kernel is too old, aborting\n", __func__);
206		return -2;
207	}
208
209	info = &info_linear->info;
210	if (!info->jited_ksyms) {
211		free(info_linear);
212		return -1;
213	}
214
215	/* number of ksyms, func_lengths, and tags should match */
216	sub_prog_cnt = info->nr_jited_ksyms;
217	if (sub_prog_cnt != info->nr_prog_tags ||
218	    sub_prog_cnt != info->nr_jited_func_lens) {
219		free(info_linear);
220		return -1;
221	}
222
223	/* check BTF func info support */
224	if (info->btf_id && info->nr_func_info && info->func_info_rec_size) {
225		/* btf func info number should be same as sub_prog_cnt */
226		if (sub_prog_cnt != info->nr_func_info) {
227			pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__);
228			free(info_linear);
229			return -1;
230		}
231		btf = btf__load_from_kernel_by_id(info->btf_id);
232		if (libbpf_get_error(btf)) {
233			pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id);
234			err = -1;
 
235			goto out;
236		}
237		perf_env__fetch_btf(env, info->btf_id, btf);
238	}
239
240	/* Synthesize PERF_RECORD_KSYMBOL */
241	for (i = 0; i < sub_prog_cnt; i++) {
242		__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
243		__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
244		int name_len;
245
246		*ksymbol_event = (struct perf_record_ksymbol) {
247			.header = {
248				.type = PERF_RECORD_KSYMBOL,
249				.size = offsetof(struct perf_record_ksymbol, name),
250			},
251			.addr = prog_addrs[i],
252			.len = prog_lens[i],
253			.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
254			.flags = 0,
255		};
256
257		name_len = synthesize_bpf_prog_name(ksymbol_event->name,
258						    KSYM_NAME_LEN, info, btf, i);
259		ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
260							 sizeof(u64));
261
262		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
263		event->header.size += machine->id_hdr_size;
264		err = perf_tool__process_synth_event(tool, event,
265						     machine, process);
266	}
267
268	if (!opts->no_bpf_event) {
269		/* Synthesize PERF_RECORD_BPF_EVENT */
270		*bpf_event = (struct perf_record_bpf_event) {
271			.header = {
272				.type = PERF_RECORD_BPF_EVENT,
273				.size = sizeof(struct perf_record_bpf_event),
274			},
275			.type = PERF_BPF_EVENT_PROG_LOAD,
276			.flags = 0,
277			.id = info->id,
278		};
279		memcpy(bpf_event->tag, info->tag, BPF_TAG_SIZE);
280		memset((void *)event + event->header.size, 0, machine->id_hdr_size);
281		event->header.size += machine->id_hdr_size;
282
283		/* save bpf_prog_info to env */
284		info_node = malloc(sizeof(struct bpf_prog_info_node));
285		if (!info_node) {
286			err = -1;
287			goto out;
288		}
289
290		info_node->info_linear = info_linear;
291		perf_env__insert_bpf_prog_info(env, info_node);
292		info_linear = NULL;
293
294		/*
295		 * process after saving bpf_prog_info to env, so that
296		 * required information is ready for look up
297		 */
298		err = perf_tool__process_synth_event(tool, event,
299						     machine, process);
300	}
301
302out:
303	free(info_linear);
304	btf__free(btf);
305	return err ? -1 : 0;
306}
307
308struct kallsyms_parse {
309	union perf_event	*event;
310	perf_event__handler_t	 process;
311	struct machine		*machine;
312	struct perf_tool	*tool;
313};
314
315static int
316process_bpf_image(char *name, u64 addr, struct kallsyms_parse *data)
317{
318	struct machine *machine = data->machine;
319	union perf_event *event = data->event;
320	struct perf_record_ksymbol *ksymbol;
321	int len;
322
323	ksymbol = &event->ksymbol;
324
325	*ksymbol = (struct perf_record_ksymbol) {
326		.header = {
327			.type = PERF_RECORD_KSYMBOL,
328			.size = offsetof(struct perf_record_ksymbol, name),
329		},
330		.addr      = addr,
331		.len       = page_size,
332		.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
333		.flags     = 0,
334	};
335
336	len = scnprintf(ksymbol->name, KSYM_NAME_LEN, "%s", name);
337	ksymbol->header.size += PERF_ALIGN(len + 1, sizeof(u64));
338	memset((void *) event + event->header.size, 0, machine->id_hdr_size);
339	event->header.size += machine->id_hdr_size;
340
341	return perf_tool__process_synth_event(data->tool, event, machine,
342					      data->process);
343}
344
345static int
346kallsyms_process_symbol(void *data, const char *_name,
347			char type __maybe_unused, u64 start)
348{
349	char disp[KSYM_NAME_LEN];
350	char *module, *name;
351	unsigned long id;
352	int err = 0;
353
354	module = strchr(_name, '\t');
355	if (!module)
356		return 0;
357
358	/* We are going after [bpf] module ... */
359	if (strcmp(module + 1, "[bpf]"))
360		return 0;
361
362	name = memdup(_name, (module - _name) + 1);
363	if (!name)
364		return -ENOMEM;
365
366	name[module - _name] = 0;
367
368	/* .. and only for trampolines and dispatchers */
369	if ((sscanf(name, "bpf_trampoline_%lu", &id) == 1) ||
370	    (sscanf(name, "bpf_dispatcher_%s", disp) == 1))
371		err = process_bpf_image(name, start, data);
372
373	free(name);
374	return err;
375}
376
377int perf_event__synthesize_bpf_events(struct perf_session *session,
378				      perf_event__handler_t process,
379				      struct machine *machine,
380				      struct record_opts *opts)
381{
382	const char *kallsyms_filename = "/proc/kallsyms";
383	struct kallsyms_parse arg;
384	union perf_event *event;
385	__u32 id = 0;
386	int err;
387	int fd;
388
389	if (opts->no_bpf_event)
390		return 0;
391
392	event = malloc(sizeof(event->bpf) + KSYM_NAME_LEN + machine->id_hdr_size);
393	if (!event)
394		return -1;
395
396	/* Synthesize all the bpf programs in system. */
397	while (true) {
398		err = bpf_prog_get_next_id(id, &id);
399		if (err) {
400			if (errno == ENOENT) {
401				err = 0;
402				break;
403			}
404			pr_debug("%s: can't get next program: %s%s\n",
405				 __func__, strerror(errno),
406				 errno == EINVAL ? " -- kernel too old?" : "");
407			/* don't report error on old kernel or EPERM  */
408			err = (errno == EINVAL || errno == EPERM) ? 0 : -1;
409			break;
410		}
411		fd = bpf_prog_get_fd_by_id(id);
412		if (fd < 0) {
413			pr_debug("%s: failed to get fd for prog_id %u\n",
414				 __func__, id);
415			continue;
416		}
417
418		err = perf_event__synthesize_one_bpf_prog(session, process,
419							  machine, fd,
420							  event, opts);
421		close(fd);
422		if (err) {
423			/* do not return error for old kernel */
424			if (err == -2)
425				err = 0;
426			break;
427		}
428	}
429
430	/* Synthesize all the bpf images - trampolines/dispatchers. */
431	if (symbol_conf.kallsyms_name != NULL)
432		kallsyms_filename = symbol_conf.kallsyms_name;
433
434	arg = (struct kallsyms_parse) {
435		.event   = event,
436		.process = process,
437		.machine = machine,
438		.tool    = session->tool,
439	};
440
441	if (kallsyms__parse(kallsyms_filename, &arg, kallsyms_process_symbol)) {
442		pr_err("%s: failed to synthesize bpf images: %s\n",
443		       __func__, strerror(errno));
444	}
445
446	free(event);
447	return err;
448}
449
450static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
451{
 
452	struct bpf_prog_info_node *info_node;
453	struct perf_bpil *info_linear;
454	struct btf *btf = NULL;
455	u64 arrays;
456	u32 btf_id;
457	int fd;
458
459	fd = bpf_prog_get_fd_by_id(id);
460	if (fd < 0)
461		return;
462
463	arrays = 1UL << PERF_BPIL_JITED_KSYMS;
464	arrays |= 1UL << PERF_BPIL_JITED_FUNC_LENS;
465	arrays |= 1UL << PERF_BPIL_FUNC_INFO;
466	arrays |= 1UL << PERF_BPIL_PROG_TAGS;
467	arrays |= 1UL << PERF_BPIL_JITED_INSNS;
468	arrays |= 1UL << PERF_BPIL_LINE_INFO;
469	arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
470
471	info_linear = get_bpf_prog_info_linear(fd, arrays);
472	if (IS_ERR_OR_NULL(info_linear)) {
473		pr_debug("%s: failed to get BPF program info. aborting\n", __func__);
474		goto out;
475	}
476
477	btf_id = info_linear->info.btf_id;
478
479	info_node = malloc(sizeof(struct bpf_prog_info_node));
480	if (info_node) {
481		info_node->info_linear = info_linear;
482		perf_env__insert_bpf_prog_info(env, info_node);
483	} else
484		free(info_linear);
485
486	if (btf_id == 0)
487		goto out;
488
489	btf = btf__load_from_kernel_by_id(btf_id);
490	if (libbpf_get_error(btf)) {
491		pr_debug("%s: failed to get BTF of id %u, aborting\n",
492			 __func__, btf_id);
493		goto out;
494	}
495	perf_env__fetch_btf(env, btf_id, btf);
496
497out:
498	btf__free(btf);
499	close(fd);
500}
501
502static int bpf_event__sb_cb(union perf_event *event, void *data)
503{
504	struct perf_env *env = data;
505
506	if (event->header.type != PERF_RECORD_BPF_EVENT)
507		return -1;
508
509	switch (event->bpf.type) {
510	case PERF_BPF_EVENT_PROG_LOAD:
511		perf_env__add_bpf_info(env, event->bpf.id);
512
513	case PERF_BPF_EVENT_PROG_UNLOAD:
514		/*
515		 * Do not free bpf_prog_info and btf of the program here,
516		 * as annotation still need them. They will be freed at
517		 * the end of the session.
518		 */
519		break;
520	default:
521		pr_debug("unexpected bpf event type of %d\n", event->bpf.type);
522		break;
523	}
524
525	return 0;
526}
527
528int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)
529{
530	struct perf_event_attr attr = {
531		.type	          = PERF_TYPE_SOFTWARE,
532		.config           = PERF_COUNT_SW_DUMMY,
533		.sample_id_all    = 1,
534		.watermark        = 1,
535		.bpf_event        = 1,
536		.size	   = sizeof(attr), /* to capture ABI version */
537	};
538
539	/*
540	 * Older gcc versions don't support designated initializers, like above,
541	 * for unnamed union members, such as the following:
542	 */
543	attr.wakeup_watermark = 1;
544
545	return evlist__add_sb_event(evlist, &attr, bpf_event__sb_cb, env);
546}
547
548void __bpf_event__print_bpf_prog_info(struct bpf_prog_info *info,
549				      struct perf_env *env,
550				      FILE *fp)
551{
552	__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
553	__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
554	char name[KSYM_NAME_LEN];
555	struct btf *btf = NULL;
556	u32 sub_prog_cnt, i;
557
558	sub_prog_cnt = info->nr_jited_ksyms;
559	if (sub_prog_cnt != info->nr_prog_tags ||
560	    sub_prog_cnt != info->nr_jited_func_lens)
561		return;
562
563	if (info->btf_id) {
564		struct btf_node *node;
565
566		node = __perf_env__find_btf(env, info->btf_id);
567		if (node)
568			btf = btf__new((__u8 *)(node->data),
569				       node->data_size);
570	}
571
572	if (sub_prog_cnt == 1) {
573		synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, 0);
574		fprintf(fp, "# bpf_prog_info %u: %s addr 0x%llx size %u\n",
575			info->id, name, prog_addrs[0], prog_lens[0]);
576		goto out;
577	}
578
579	fprintf(fp, "# bpf_prog_info %u:\n", info->id);
580	for (i = 0; i < sub_prog_cnt; i++) {
581		synthesize_bpf_prog_name(name, KSYM_NAME_LEN, info, btf, i);
582
583		fprintf(fp, "# \tsub_prog %u: %s addr 0x%llx size %u\n",
584			i, name, prog_addrs[i], prog_lens[i]);
585	}
586out:
587	btf__free(btf);
588}