1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
10 #include <bpf/libbpf.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
18 #include "bpf-loader.h"
19 #include "bpf-prologue.h"
20 #include "probe-event.h"
21 #include "probe-finder.h" // for MAX_PROBES
22 #include "parse-events.h"
23 #include "strfilter.h"
24 #include "llvm-utils.h"
25 #include "c++/clang-c.h"
27 #define DEFINE_PRINT_FN(name, level) \
28 static int libbpf_##name(const char *fmt, ...) \
33 va_start(args, fmt); \
34 ret = veprintf(level, verbose, pr_fmt(fmt), args);\
39 DEFINE_PRINT_FN(warning, 1)
40 DEFINE_PRINT_FN(info, 1)
41 DEFINE_PRINT_FN(debug, 1)
43 struct bpf_prog_priv {
47 struct perf_probe_event pev;
49 struct bpf_insn *insns_buf;
54 static bool libbpf_initialized;
57 bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
59 struct bpf_object *obj;
61 if (!libbpf_initialized) {
62 libbpf_set_print(libbpf_warning,
65 libbpf_initialized = true;
68 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
69 if (IS_ERR_OR_NULL(obj)) {
70 pr_debug("bpf: failed to load buffer\n");
71 return ERR_PTR(-EINVAL);
77 struct bpf_object *bpf__prepare_load(const char *filename, bool source)
79 struct bpf_object *obj;
81 if (!libbpf_initialized) {
82 libbpf_set_print(libbpf_warning,
85 libbpf_initialized = true;
94 err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
95 perf_clang__cleanup();
97 pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err);
98 err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
100 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
102 pr_debug("bpf: successfull builtin compilation\n");
103 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
105 if (!IS_ERR_OR_NULL(obj) && llvm_param.dump_obj)
106 llvm__dump_obj(filename, obj_buf, obj_buf_sz);
110 obj = bpf_object__open(filename);
112 if (IS_ERR_OR_NULL(obj)) {
113 pr_debug("bpf: failed to load %s\n", filename);
120 void bpf__clear(void)
122 struct bpf_object *obj, *tmp;
124 bpf_object__for_each_safe(obj, tmp) {
126 bpf_object__close(obj);
131 clear_prog_priv(struct bpf_program *prog __maybe_unused,
134 struct bpf_prog_priv *priv = _priv;
136 cleanup_perf_probe_events(&priv->pev, 1);
137 zfree(&priv->insns_buf);
138 zfree(&priv->type_mapping);
139 zfree(&priv->sys_name);
140 zfree(&priv->evt_name);
145 prog_config__exec(const char *value, struct perf_probe_event *pev)
148 pev->target = strdup(value);
155 prog_config__module(const char *value, struct perf_probe_event *pev)
157 pev->uprobes = false;
158 pev->target = strdup(value);
165 prog_config__bool(const char *value, bool *pbool, bool invert)
173 err = strtobool(value, &bool_value);
177 *pbool = invert ? !bool_value : bool_value;
182 prog_config__inlines(const char *value,
183 struct perf_probe_event *pev __maybe_unused)
185 return prog_config__bool(value, &probe_conf.no_inlines, true);
189 prog_config__force(const char *value,
190 struct perf_probe_event *pev __maybe_unused)
192 return prog_config__bool(value, &probe_conf.force_add, false);
199 int (*func)(const char *, struct perf_probe_event *);
200 } bpf_prog_config_terms[] = {
203 .usage = "exec=<full path of file>",
204 .desc = "Set uprobe target",
205 .func = prog_config__exec,
209 .usage = "module=<module name> ",
210 .desc = "Set kprobe module",
211 .func = prog_config__module,
215 .usage = "inlines=[yes|no] ",
216 .desc = "Probe at inline symbol",
217 .func = prog_config__inlines,
221 .usage = "force=[yes|no] ",
222 .desc = "Forcibly add events with existing name",
223 .func = prog_config__force,
228 do_prog_config(const char *key, const char *value,
229 struct perf_probe_event *pev)
233 pr_debug("config bpf program: %s=%s\n", key, value);
234 for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
235 if (strcmp(key, bpf_prog_config_terms[i].key) == 0)
236 return bpf_prog_config_terms[i].func(value, pev);
238 pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
241 pr_debug("\nHint: Valid options are:\n");
242 for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
243 pr_debug("\t%s:\t%s\n", bpf_prog_config_terms[i].usage,
244 bpf_prog_config_terms[i].desc);
247 return -BPF_LOADER_ERRNO__PROGCONF_TERM;
251 parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev)
253 char *text = strdup(config_str);
255 const char *main_str = NULL;
259 pr_debug("Not enough memory: dup config_str failed\n");
260 return ERR_PTR(-ENOMEM);
264 while ((sep = strchr(line, ';'))) {
268 equ = strchr(line, '=');
270 pr_warning("WARNING: invalid config in BPF object: %s\n",
272 pr_warning("\tShould be 'key=value'.\n");
277 err = do_prog_config(line, equ + 1, pev);
285 main_str = config_str + (line - text);
288 return err ? ERR_PTR(err) : main_str;
292 parse_prog_config(const char *config_str, const char **p_main_str,
293 bool *is_tp, struct perf_probe_event *pev)
296 const char *main_str = parse_prog_config_kvpair(config_str, pev);
298 if (IS_ERR(main_str))
299 return PTR_ERR(main_str);
301 *p_main_str = main_str;
302 if (!strchr(main_str, '=')) {
303 /* Is a tracepoint event? */
304 const char *s = strchr(main_str, ':');
307 pr_debug("bpf: '%s' is not a valid tracepoint\n",
309 return -BPF_LOADER_ERRNO__CONFIG;
317 err = parse_perf_probe_command(main_str, pev);
319 pr_debug("bpf: '%s' is not a valid config string\n",
321 /* parse failed, don't need clear pev. */
322 return -BPF_LOADER_ERRNO__CONFIG;
328 config_bpf_program(struct bpf_program *prog)
330 struct perf_probe_event *pev = NULL;
331 struct bpf_prog_priv *priv = NULL;
332 const char *config_str, *main_str;
336 /* Initialize per-program probing setting */
337 probe_conf.no_inlines = false;
338 probe_conf.force_add = false;
340 config_str = bpf_program__title(prog, false);
341 if (IS_ERR(config_str)) {
342 pr_debug("bpf: unable to get title for program\n");
343 return PTR_ERR(config_str);
346 priv = calloc(sizeof(*priv), 1);
348 pr_debug("bpf: failed to alloc priv\n");
353 pr_debug("bpf: config program '%s'\n", config_str);
354 err = parse_prog_config(config_str, &main_str, &is_tp, pev);
359 char *s = strchr(main_str, ':');
362 priv->sys_name = strndup(main_str, s - main_str);
363 priv->evt_name = strdup(s + 1);
367 if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
368 pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
369 config_str, PERF_BPF_PROBE_GROUP);
370 err = -BPF_LOADER_ERRNO__GROUP;
372 } else if (!pev->group)
373 pev->group = strdup(PERF_BPF_PROBE_GROUP);
376 pr_debug("bpf: strdup failed\n");
382 pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
384 err = -BPF_LOADER_ERRNO__EVENTNAME;
387 pr_debug("bpf: config '%s' is ok\n", config_str);
390 err = bpf_program__set_priv(prog, priv, clear_prog_priv);
392 pr_debug("Failed to set priv for program '%s'\n", config_str);
400 clear_perf_probe_event(pev);
405 static int bpf__prepare_probe(void)
408 static bool initialized = false;
411 * Make err static, so if init failed the first, bpf__prepare_probe()
412 * fails each time without calling init_probe_symbol_maps multiple
419 err = init_probe_symbol_maps(false);
421 pr_debug("Failed to init_probe_symbol_maps\n");
422 probe_conf.max_probes = MAX_PROBES;
427 preproc_gen_prologue(struct bpf_program *prog, int n,
428 struct bpf_insn *orig_insns, int orig_insns_cnt,
429 struct bpf_prog_prep_result *res)
431 struct bpf_prog_priv *priv = bpf_program__priv(prog);
432 struct probe_trace_event *tev;
433 struct perf_probe_event *pev;
434 struct bpf_insn *buf;
435 size_t prologue_cnt = 0;
438 if (IS_ERR(priv) || !priv || priv->is_tp)
443 if (n < 0 || n >= priv->nr_types)
446 /* Find a tev belongs to that type */
447 for (i = 0; i < pev->ntevs; i++) {
448 if (priv->type_mapping[i] == n)
452 if (i >= pev->ntevs) {
453 pr_debug("Internal error: prologue type %d not found\n", n);
454 return -BPF_LOADER_ERRNO__PROLOGUE;
459 buf = priv->insns_buf;
460 err = bpf__gen_prologue(tev->args, tev->nargs,
462 BPF_MAXINSNS - orig_insns_cnt);
466 title = bpf_program__title(prog, false);
470 pr_debug("Failed to generate prologue for program %s\n",
475 memcpy(&buf[prologue_cnt], orig_insns,
476 sizeof(struct bpf_insn) * orig_insns_cnt);
478 res->new_insn_ptr = buf;
479 res->new_insn_cnt = prologue_cnt + orig_insns_cnt;
484 pr_debug("Internal error in preproc_gen_prologue\n");
485 return -BPF_LOADER_ERRNO__PROLOGUE;
489 * compare_tev_args is reflexive, transitive and antisymmetric.
490 * I can proof it but this margin is too narrow to contain.
492 static int compare_tev_args(const void *ptev1, const void *ptev2)
495 const struct probe_trace_event *tev1 =
496 *(const struct probe_trace_event **)ptev1;
497 const struct probe_trace_event *tev2 =
498 *(const struct probe_trace_event **)ptev2;
500 ret = tev2->nargs - tev1->nargs;
504 for (i = 0; i < tev1->nargs; i++) {
505 struct probe_trace_arg *arg1, *arg2;
506 struct probe_trace_arg_ref *ref1, *ref2;
508 arg1 = &tev1->args[i];
509 arg2 = &tev2->args[i];
511 ret = strcmp(arg1->value, arg2->value);
518 while (ref1 && ref2) {
519 ret = ref2->offset - ref1->offset;
528 return ref2 ? 1 : -1;
535 * Assign a type number to each tevs in a pev.
536 * mapping is an array with same slots as tevs in that pev.
537 * nr_types will be set to number of types.
539 static int map_prologue(struct perf_probe_event *pev, int *mapping,
543 struct probe_trace_event **ptevs;
545 size_t array_sz = sizeof(*ptevs) * pev->ntevs;
547 ptevs = malloc(array_sz);
549 pr_debug("Not enough memory: alloc ptevs failed\n");
553 pr_debug("In map_prologue, ntevs=%d\n", pev->ntevs);
554 for (i = 0; i < pev->ntevs; i++)
555 ptevs[i] = &pev->tevs[i];
557 qsort(ptevs, pev->ntevs, sizeof(*ptevs),
560 for (i = 0; i < pev->ntevs; i++) {
563 n = ptevs[i] - pev->tevs;
566 pr_debug("mapping[%d]=%d\n", n, type);
570 if (compare_tev_args(ptevs + i, ptevs + i - 1) == 0)
575 pr_debug("mapping[%d]=%d\n", n, mapping[n]);
578 *nr_types = type + 1;
583 static int hook_load_preprocessor(struct bpf_program *prog)
585 struct bpf_prog_priv *priv = bpf_program__priv(prog);
586 struct perf_probe_event *pev;
587 bool need_prologue = false;
590 if (IS_ERR(priv) || !priv) {
591 pr_debug("Internal error when hook preprocessor\n");
592 return -BPF_LOADER_ERRNO__INTERNAL;
596 priv->need_prologue = false;
601 for (i = 0; i < pev->ntevs; i++) {
602 struct probe_trace_event *tev = &pev->tevs[i];
604 if (tev->nargs > 0) {
605 need_prologue = true;
611 * Since all tevs don't have argument, we don't need generate
614 if (!need_prologue) {
615 priv->need_prologue = false;
619 priv->need_prologue = true;
620 priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS);
621 if (!priv->insns_buf) {
622 pr_debug("Not enough memory: alloc insns_buf failed\n");
626 priv->type_mapping = malloc(sizeof(int) * pev->ntevs);
627 if (!priv->type_mapping) {
628 pr_debug("Not enough memory: alloc type_mapping failed\n");
631 memset(priv->type_mapping, -1,
632 sizeof(int) * pev->ntevs);
634 err = map_prologue(pev, priv->type_mapping, &priv->nr_types);
638 err = bpf_program__set_prep(prog, priv->nr_types,
639 preproc_gen_prologue);
643 int bpf__probe(struct bpf_object *obj)
646 struct bpf_program *prog;
647 struct bpf_prog_priv *priv;
648 struct perf_probe_event *pev;
650 err = bpf__prepare_probe();
652 pr_debug("bpf__prepare_probe failed\n");
656 bpf_object__for_each_program(prog, obj) {
657 err = config_bpf_program(prog);
661 priv = bpf_program__priv(prog);
662 if (IS_ERR(priv) || !priv) {
668 bpf_program__set_tracepoint(prog);
672 bpf_program__set_kprobe(prog);
675 err = convert_perf_probe_events(pev, 1);
677 pr_debug("bpf_probe: failed to convert perf probe events\n");
681 err = apply_perf_probe_events(pev, 1);
683 pr_debug("bpf_probe: failed to apply perf probe events\n");
688 * After probing, let's consider prologue, which
689 * adds program fetcher to BPF programs.
691 * hook_load_preprocessorr() hooks pre-processor
692 * to bpf_program, let it generate prologue
693 * dynamically during loading.
695 err = hook_load_preprocessor(prog);
700 return err < 0 ? err : 0;
703 #define EVENTS_WRITE_BUFSIZE 4096
704 int bpf__unprobe(struct bpf_object *obj)
707 struct bpf_program *prog;
709 bpf_object__for_each_program(prog, obj) {
710 struct bpf_prog_priv *priv = bpf_program__priv(prog);
713 if (IS_ERR(priv) || !priv || priv->is_tp)
716 for (i = 0; i < priv->pev.ntevs; i++) {
717 struct probe_trace_event *tev = &priv->pev.tevs[i];
718 char name_buf[EVENTS_WRITE_BUFSIZE];
719 struct strfilter *delfilter;
721 snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
722 "%s:%s", tev->group, tev->event);
723 name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
725 delfilter = strfilter__new(name_buf, NULL);
727 pr_debug("Failed to create filter for unprobing\n");
732 err = del_perf_probe_events(delfilter);
733 strfilter__delete(delfilter);
735 pr_debug("Failed to delete %s\n", name_buf);
744 int bpf__load(struct bpf_object *obj)
748 err = bpf_object__load(obj);
751 libbpf_strerror(err, bf, sizeof(bf));
752 pr_debug("bpf: load objects failed: err=%d: (%s)\n", err, bf);
758 int bpf__foreach_event(struct bpf_object *obj,
759 bpf_prog_iter_callback_t func,
762 struct bpf_program *prog;
765 bpf_object__for_each_program(prog, obj) {
766 struct bpf_prog_priv *priv = bpf_program__priv(prog);
767 struct probe_trace_event *tev;
768 struct perf_probe_event *pev;
771 if (IS_ERR(priv) || !priv) {
772 pr_debug("bpf: failed to get private field\n");
773 return -BPF_LOADER_ERRNO__INTERNAL;
777 fd = bpf_program__fd(prog);
778 err = (*func)(priv->sys_name, priv->evt_name, fd, arg);
780 pr_debug("bpf: tracepoint call back failed, stop iterate\n");
787 for (i = 0; i < pev->ntevs; i++) {
790 if (priv->need_prologue) {
791 int type = priv->type_mapping[i];
793 fd = bpf_program__nth_fd(prog, type);
795 fd = bpf_program__fd(prog);
799 pr_debug("bpf: failed to get file descriptor\n");
803 err = (*func)(tev->group, tev->event, fd, arg);
805 pr_debug("bpf: call back failed, stop iterate\n");
813 enum bpf_map_op_type {
814 BPF_MAP_OP_SET_VALUE,
815 BPF_MAP_OP_SET_EVSEL,
818 enum bpf_map_key_type {
824 struct list_head list;
825 enum bpf_map_op_type op_type;
826 enum bpf_map_key_type key_type;
828 struct parse_events_array array;
832 struct perf_evsel *evsel;
836 struct bpf_map_priv {
837 struct list_head ops_list;
841 bpf_map_op__delete(struct bpf_map_op *op)
843 if (!list_empty(&op->list))
845 if (op->key_type == BPF_MAP_KEY_RANGES)
846 parse_events__clear_array(&op->k.array);
851 bpf_map_priv__purge(struct bpf_map_priv *priv)
853 struct bpf_map_op *pos, *n;
855 list_for_each_entry_safe(pos, n, &priv->ops_list, list) {
856 list_del_init(&pos->list);
857 bpf_map_op__delete(pos);
862 bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
865 struct bpf_map_priv *priv = _priv;
867 bpf_map_priv__purge(priv);
872 bpf_map_op_setkey(struct bpf_map_op *op, struct parse_events_term *term)
874 op->key_type = BPF_MAP_KEY_ALL;
878 if (term->array.nr_ranges) {
879 size_t memsz = term->array.nr_ranges *
880 sizeof(op->k.array.ranges[0]);
882 op->k.array.ranges = memdup(term->array.ranges, memsz);
883 if (!op->k.array.ranges) {
884 pr_debug("Not enough memory to alloc indices for map\n");
887 op->key_type = BPF_MAP_KEY_RANGES;
888 op->k.array.nr_ranges = term->array.nr_ranges;
893 static struct bpf_map_op *
894 bpf_map_op__new(struct parse_events_term *term)
896 struct bpf_map_op *op;
899 op = zalloc(sizeof(*op));
901 pr_debug("Failed to alloc bpf_map_op\n");
902 return ERR_PTR(-ENOMEM);
904 INIT_LIST_HEAD(&op->list);
906 err = bpf_map_op_setkey(op, term);
914 static struct bpf_map_op *
915 bpf_map_op__clone(struct bpf_map_op *op)
917 struct bpf_map_op *newop;
919 newop = memdup(op, sizeof(*op));
921 pr_debug("Failed to alloc bpf_map_op\n");
925 INIT_LIST_HEAD(&newop->list);
926 if (op->key_type == BPF_MAP_KEY_RANGES) {
927 size_t memsz = op->k.array.nr_ranges *
928 sizeof(op->k.array.ranges[0]);
930 newop->k.array.ranges = memdup(op->k.array.ranges, memsz);
931 if (!newop->k.array.ranges) {
932 pr_debug("Failed to alloc indices for map\n");
941 static struct bpf_map_priv *
942 bpf_map_priv__clone(struct bpf_map_priv *priv)
944 struct bpf_map_priv *newpriv;
945 struct bpf_map_op *pos, *newop;
947 newpriv = zalloc(sizeof(*newpriv));
949 pr_debug("Not enough memory to alloc map private\n");
952 INIT_LIST_HEAD(&newpriv->ops_list);
954 list_for_each_entry(pos, &priv->ops_list, list) {
955 newop = bpf_map_op__clone(pos);
957 bpf_map_priv__purge(newpriv);
960 list_add_tail(&newop->list, &newpriv->ops_list);
967 bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
969 const char *map_name = bpf_map__name(map);
970 struct bpf_map_priv *priv = bpf_map__priv(map);
973 pr_debug("Failed to get private from map %s\n", map_name);
974 return PTR_ERR(priv);
978 priv = zalloc(sizeof(*priv));
980 pr_debug("Not enough memory to alloc map private\n");
983 INIT_LIST_HEAD(&priv->ops_list);
985 if (bpf_map__set_priv(map, priv, bpf_map_priv__clear)) {
987 return -BPF_LOADER_ERRNO__INTERNAL;
991 list_add_tail(&op->list, &priv->ops_list);
995 static struct bpf_map_op *
996 bpf_map__add_newop(struct bpf_map *map, struct parse_events_term *term)
998 struct bpf_map_op *op;
1001 op = bpf_map_op__new(term);
1005 err = bpf_map__add_op(map, op);
1007 bpf_map_op__delete(op);
1008 return ERR_PTR(err);
1014 __bpf_map__config_value(struct bpf_map *map,
1015 struct parse_events_term *term)
1017 struct bpf_map_op *op;
1018 const char *map_name = bpf_map__name(map);
1019 const struct bpf_map_def *def = bpf_map__def(map);
1022 pr_debug("Unable to get map definition from '%s'\n",
1024 return -BPF_LOADER_ERRNO__INTERNAL;
1027 if (def->type != BPF_MAP_TYPE_ARRAY) {
1028 pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n",
1030 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1032 if (def->key_size < sizeof(unsigned int)) {
1033 pr_debug("Map %s has incorrect key size\n", map_name);
1034 return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE;
1036 switch (def->value_size) {
1043 pr_debug("Map %s has incorrect value size\n", map_name);
1044 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1047 op = bpf_map__add_newop(map, term);
1050 op->op_type = BPF_MAP_OP_SET_VALUE;
1051 op->v.value = term->val.num;
1056 bpf_map__config_value(struct bpf_map *map,
1057 struct parse_events_term *term,
1058 struct perf_evlist *evlist __maybe_unused)
1060 if (!term->err_val) {
1061 pr_debug("Config value not set\n");
1062 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1065 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) {
1066 pr_debug("ERROR: wrong value type for 'value'\n");
1067 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1070 return __bpf_map__config_value(map, term);
1074 __bpf_map__config_event(struct bpf_map *map,
1075 struct parse_events_term *term,
1076 struct perf_evlist *evlist)
1078 struct perf_evsel *evsel;
1079 const struct bpf_map_def *def;
1080 struct bpf_map_op *op;
1081 const char *map_name = bpf_map__name(map);
1083 evsel = perf_evlist__find_evsel_by_str(evlist, term->val.str);
1085 pr_debug("Event (for '%s') '%s' doesn't exist\n",
1086 map_name, term->val.str);
1087 return -BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT;
1090 def = bpf_map__def(map);
1092 pr_debug("Unable to get map definition from '%s'\n",
1094 return PTR_ERR(def);
1098 * No need to check key_size and value_size:
1099 * kernel has already checked them.
1101 if (def->type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
1102 pr_debug("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
1104 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1107 op = bpf_map__add_newop(map, term);
1110 op->op_type = BPF_MAP_OP_SET_EVSEL;
1111 op->v.evsel = evsel;
1116 bpf_map__config_event(struct bpf_map *map,
1117 struct parse_events_term *term,
1118 struct perf_evlist *evlist)
1120 if (!term->err_val) {
1121 pr_debug("Config value not set\n");
1122 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1125 if (term->type_val != PARSE_EVENTS__TERM_TYPE_STR) {
1126 pr_debug("ERROR: wrong value type for 'event'\n");
1127 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1130 return __bpf_map__config_event(map, term, evlist);
1133 struct bpf_obj_config__map_func {
1134 const char *config_opt;
1135 int (*config_func)(struct bpf_map *, struct parse_events_term *,
1136 struct perf_evlist *);
1139 struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = {
1140 {"value", bpf_map__config_value},
1141 {"event", bpf_map__config_event},
1145 config_map_indices_range_check(struct parse_events_term *term,
1146 struct bpf_map *map,
1147 const char *map_name)
1149 struct parse_events_array *array = &term->array;
1150 const struct bpf_map_def *def;
1153 if (!array->nr_ranges)
1155 if (!array->ranges) {
1156 pr_debug("ERROR: map %s: array->nr_ranges is %d but range array is NULL\n",
1157 map_name, (int)array->nr_ranges);
1158 return -BPF_LOADER_ERRNO__INTERNAL;
1161 def = bpf_map__def(map);
1163 pr_debug("ERROR: Unable to get map definition from '%s'\n",
1165 return -BPF_LOADER_ERRNO__INTERNAL;
1168 for (i = 0; i < array->nr_ranges; i++) {
1169 unsigned int start = array->ranges[i].start;
1170 size_t length = array->ranges[i].length;
1171 unsigned int idx = start + length - 1;
1173 if (idx >= def->max_entries) {
1174 pr_debug("ERROR: index %d too large\n", idx);
1175 return -BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG;
1182 bpf__obj_config_map(struct bpf_object *obj,
1183 struct parse_events_term *term,
1184 struct perf_evlist *evlist,
1187 /* key is "map:<mapname>.<config opt>" */
1188 char *map_name = strdup(term->config + sizeof("map:") - 1);
1189 struct bpf_map *map;
1190 int err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1197 map_opt = strchr(map_name, '.');
1199 pr_debug("ERROR: Invalid map config: %s\n", map_name);
1204 if (*map_opt == '\0') {
1205 pr_debug("ERROR: Invalid map option: %s\n", term->config);
1209 map = bpf_object__find_map_by_name(obj, map_name);
1211 pr_debug("ERROR: Map %s doesn't exist\n", map_name);
1212 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST;
1216 *key_scan_pos += strlen(map_opt);
1217 err = config_map_indices_range_check(term, map, map_name);
1220 *key_scan_pos -= strlen(map_opt);
1222 for (i = 0; i < ARRAY_SIZE(bpf_obj_config__map_funcs); i++) {
1223 struct bpf_obj_config__map_func *func =
1224 &bpf_obj_config__map_funcs[i];
1226 if (strcmp(map_opt, func->config_opt) == 0) {
1227 err = func->config_func(map, term, evlist);
1232 pr_debug("ERROR: Invalid map config option '%s'\n", map_opt);
1233 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT;
1237 key_scan_pos += strlen(map_opt);
1241 int bpf__config_obj(struct bpf_object *obj,
1242 struct parse_events_term *term,
1243 struct perf_evlist *evlist,
1246 int key_scan_pos = 0;
1249 if (!obj || !term || !term->config)
1252 if (strstarts(term->config, "map:")) {
1253 key_scan_pos = sizeof("map:") - 1;
1254 err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
1257 err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1260 *error_pos = key_scan_pos;
1265 typedef int (*map_config_func_t)(const char *name, int map_fd,
1266 const struct bpf_map_def *pdef,
1267 struct bpf_map_op *op,
1268 void *pkey, void *arg);
1271 foreach_key_array_all(map_config_func_t func,
1272 void *arg, const char *name,
1273 int map_fd, const struct bpf_map_def *pdef,
1274 struct bpf_map_op *op)
1279 for (i = 0; i < pdef->max_entries; i++) {
1280 err = func(name, map_fd, pdef, op, &i, arg);
1282 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1291 foreach_key_array_ranges(map_config_func_t func, void *arg,
1292 const char *name, int map_fd,
1293 const struct bpf_map_def *pdef,
1294 struct bpf_map_op *op)
1299 for (i = 0; i < op->k.array.nr_ranges; i++) {
1300 unsigned int start = op->k.array.ranges[i].start;
1301 size_t length = op->k.array.ranges[i].length;
1303 for (j = 0; j < length; j++) {
1304 unsigned int idx = start + j;
1306 err = func(name, map_fd, pdef, op, &idx, arg);
1308 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1318 bpf_map_config_foreach_key(struct bpf_map *map,
1319 map_config_func_t func,
1323 struct bpf_map_op *op;
1324 const struct bpf_map_def *def;
1325 const char *name = bpf_map__name(map);
1326 struct bpf_map_priv *priv = bpf_map__priv(map);
1329 pr_debug("ERROR: failed to get private from map %s\n", name);
1330 return -BPF_LOADER_ERRNO__INTERNAL;
1332 if (!priv || list_empty(&priv->ops_list)) {
1333 pr_debug("INFO: nothing to config for map %s\n", name);
1337 def = bpf_map__def(map);
1339 pr_debug("ERROR: failed to get definition from map %s\n", name);
1340 return -BPF_LOADER_ERRNO__INTERNAL;
1342 map_fd = bpf_map__fd(map);
1344 pr_debug("ERROR: failed to get fd from map %s\n", name);
1348 list_for_each_entry(op, &priv->ops_list, list) {
1349 switch (def->type) {
1350 case BPF_MAP_TYPE_ARRAY:
1351 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1352 switch (op->key_type) {
1353 case BPF_MAP_KEY_ALL:
1354 err = foreach_key_array_all(func, arg, name,
1357 case BPF_MAP_KEY_RANGES:
1358 err = foreach_key_array_ranges(func, arg, name,
1363 pr_debug("ERROR: keytype for map '%s' invalid\n",
1365 return -BPF_LOADER_ERRNO__INTERNAL;
1371 pr_debug("ERROR: type of '%s' incorrect\n", name);
1372 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1380 apply_config_value_for_key(int map_fd, void *pkey,
1381 size_t val_size, u64 val)
1387 u8 _val = (u8)(val);
1388 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1392 u16 _val = (u16)(val);
1393 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1397 u32 _val = (u32)(val);
1398 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1402 err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY);
1406 pr_debug("ERROR: invalid value size\n");
1407 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1415 apply_config_evsel_for_key(const char *name, int map_fd, void *pkey,
1416 struct perf_evsel *evsel)
1418 struct xyarray *xy = evsel->fd;
1419 struct perf_event_attr *attr;
1420 unsigned int key, events;
1421 bool check_pass = false;
1426 pr_debug("ERROR: evsel not ready for map %s\n", name);
1427 return -BPF_LOADER_ERRNO__INTERNAL;
1430 if (xy->row_size / xy->entry_size != 1) {
1431 pr_debug("ERROR: Dimension of target event is incorrect for map %s\n",
1433 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM;
1436 attr = &evsel->attr;
1437 if (attr->inherit) {
1438 pr_debug("ERROR: Can't put inherit event into map %s\n", name);
1439 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH;
1442 if (perf_evsel__is_bpf_output(evsel))
1444 if (attr->type == PERF_TYPE_RAW)
1446 if (attr->type == PERF_TYPE_HARDWARE)
1449 pr_debug("ERROR: Event type is wrong for map %s\n", name);
1450 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE;
1453 events = xy->entries / (xy->row_size / xy->entry_size);
1454 key = *((unsigned int *)pkey);
1455 if (key >= events) {
1456 pr_debug("ERROR: there is no event %d for map %s\n",
1458 return -BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE;
1460 evt_fd = xyarray__entry(xy, key, 0);
1461 err = bpf_map_update_elem(map_fd, pkey, evt_fd, BPF_ANY);
1468 apply_obj_config_map_for_key(const char *name, int map_fd,
1469 const struct bpf_map_def *pdef,
1470 struct bpf_map_op *op,
1471 void *pkey, void *arg __maybe_unused)
1475 switch (op->op_type) {
1476 case BPF_MAP_OP_SET_VALUE:
1477 err = apply_config_value_for_key(map_fd, pkey,
1481 case BPF_MAP_OP_SET_EVSEL:
1482 err = apply_config_evsel_for_key(name, map_fd, pkey,
1486 pr_debug("ERROR: unknown value type for '%s'\n", name);
1487 err = -BPF_LOADER_ERRNO__INTERNAL;
1493 apply_obj_config_map(struct bpf_map *map)
1495 return bpf_map_config_foreach_key(map,
1496 apply_obj_config_map_for_key,
1501 apply_obj_config_object(struct bpf_object *obj)
1503 struct bpf_map *map;
1506 bpf_map__for_each(map, obj) {
1507 err = apply_obj_config_map(map);
1514 int bpf__apply_obj_config(void)
1516 struct bpf_object *obj, *tmp;
1519 bpf_object__for_each_safe(obj, tmp) {
1520 err = apply_obj_config_object(obj);
1528 #define bpf__for_each_map(pos, obj, objtmp) \
1529 bpf_object__for_each_safe(obj, objtmp) \
1530 bpf_map__for_each(pos, obj)
1532 #define bpf__for_each_map_named(pos, obj, objtmp, name) \
1533 bpf__for_each_map(pos, obj, objtmp) \
1534 if (bpf_map__name(pos) && \
1536 bpf_map__name(pos)) == 0))
1538 struct perf_evsel *bpf__setup_output_event(struct perf_evlist *evlist, const char *name)
1540 struct bpf_map_priv *tmpl_priv = NULL;
1541 struct bpf_object *obj, *tmp;
1542 struct perf_evsel *evsel = NULL;
1543 struct bpf_map *map;
1545 bool need_init = false;
1547 bpf__for_each_map_named(map, obj, tmp, name) {
1548 struct bpf_map_priv *priv = bpf_map__priv(map);
1551 return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
1554 * No need to check map type: type should have been
1555 * verified by kernel.
1557 if (!need_init && !priv)
1559 if (!tmpl_priv && priv)
1567 char *event_definition = NULL;
1569 if (asprintf(&event_definition, "bpf-output/no-inherit=1,name=%s/", name) < 0)
1570 return ERR_PTR(-ENOMEM);
1572 err = parse_events(evlist, event_definition, NULL);
1573 free(event_definition);
1576 pr_debug("ERROR: failed to create the \"%s\" bpf-output event\n", name);
1577 return ERR_PTR(-err);
1580 evsel = perf_evlist__last(evlist);
1583 bpf__for_each_map_named(map, obj, tmp, name) {
1584 struct bpf_map_priv *priv = bpf_map__priv(map);
1587 return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
1592 priv = bpf_map_priv__clone(tmpl_priv);
1594 return ERR_PTR(-ENOMEM);
1596 err = bpf_map__set_priv(map, priv, bpf_map_priv__clear);
1598 bpf_map_priv__clear(map, priv);
1599 return ERR_PTR(err);
1602 struct bpf_map_op *op;
1604 op = bpf_map__add_newop(map, NULL);
1606 return ERR_PTR(PTR_ERR(op));
1607 op->op_type = BPF_MAP_OP_SET_EVSEL;
1608 op->v.evsel = evsel;
1615 int bpf__setup_stdout(struct perf_evlist *evlist)
1617 struct perf_evsel *evsel = bpf__setup_output_event(evlist, "__bpf_stdout__");
1618 return IS_ERR(evsel) ? PTR_ERR(evsel) : 0;
1621 #define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
1622 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
1623 #define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
1625 static const char *bpf_loader_strerror_table[NR_ERRNO] = {
1626 [ERRCODE_OFFSET(CONFIG)] = "Invalid config string",
1627 [ERRCODE_OFFSET(GROUP)] = "Invalid group name",
1628 [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string",
1629 [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error",
1630 [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet",
1631 [ERRCODE_OFFSET(PROGCONF_TERM)] = "Invalid program config term in config string",
1632 [ERRCODE_OFFSET(PROLOGUE)] = "Failed to generate prologue",
1633 [ERRCODE_OFFSET(PROLOGUE2BIG)] = "Prologue too big for program",
1634 [ERRCODE_OFFSET(PROLOGUEOOB)] = "Offset out of bound for prologue",
1635 [ERRCODE_OFFSET(OBJCONF_OPT)] = "Invalid object config option",
1636 [ERRCODE_OFFSET(OBJCONF_CONF)] = "Config value not set (missing '=')",
1637 [ERRCODE_OFFSET(OBJCONF_MAP_OPT)] = "Invalid object map config option",
1638 [ERRCODE_OFFSET(OBJCONF_MAP_NOTEXIST)] = "Target map doesn't exist",
1639 [ERRCODE_OFFSET(OBJCONF_MAP_VALUE)] = "Incorrect value type for map",
1640 [ERRCODE_OFFSET(OBJCONF_MAP_TYPE)] = "Incorrect map type",
1641 [ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)] = "Incorrect map key size",
1642 [ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size",
1643 [ERRCODE_OFFSET(OBJCONF_MAP_NOEVT)] = "Event not found for map setting",
1644 [ERRCODE_OFFSET(OBJCONF_MAP_MAPSIZE)] = "Invalid map size for event setting",
1645 [ERRCODE_OFFSET(OBJCONF_MAP_EVTDIM)] = "Event dimension too large",
1646 [ERRCODE_OFFSET(OBJCONF_MAP_EVTINH)] = "Doesn't support inherit event",
1647 [ERRCODE_OFFSET(OBJCONF_MAP_EVTTYPE)] = "Wrong event type for map",
1648 [ERRCODE_OFFSET(OBJCONF_MAP_IDX2BIG)] = "Index too large",
1652 bpf_loader_strerror(int err, char *buf, size_t size)
1654 char sbuf[STRERR_BUFSIZE];
1660 err = err > 0 ? err : -err;
1662 if (err >= __LIBBPF_ERRNO__START)
1663 return libbpf_strerror(err, buf, size);
1665 if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) {
1666 msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)];
1667 snprintf(buf, size, "%s", msg);
1668 buf[size - 1] = '\0';
1672 if (err >= __BPF_LOADER_ERRNO__END)
1673 snprintf(buf, size, "Unknown bpf loader error %d", err);
1675 snprintf(buf, size, "%s",
1676 str_error_r(err, sbuf, sizeof(sbuf)));
1678 buf[size - 1] = '\0';
1682 #define bpf__strerror_head(err, buf, size) \
1683 char sbuf[STRERR_BUFSIZE], *emsg;\
1688 bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
1692 scnprintf(buf, size, "%s", emsg);\
1695 #define bpf__strerror_entry(val, fmt...)\
1697 scnprintf(buf, size, fmt);\
1701 #define bpf__strerror_end(buf, size)\
1703 buf[size - 1] = '\0';
1705 int bpf__strerror_prepare_load(const char *filename, bool source,
1706 int err, char *buf, size_t size)
1711 n = snprintf(buf, size, "Failed to load %s%s: ",
1712 filename, source ? " from source" : "");
1714 buf[size - 1] = '\0';
1720 ret = bpf_loader_strerror(err, buf, size);
1721 buf[size - 1] = '\0';
1725 int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
1726 int err, char *buf, size_t size)
1728 bpf__strerror_head(err, buf, size);
1729 case BPF_LOADER_ERRNO__PROGCONF_TERM: {
1730 scnprintf(buf, size, "%s (add -v to see detail)", emsg);
1733 bpf__strerror_entry(EEXIST, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'");
1734 bpf__strerror_entry(EACCES, "You need to be root");
1735 bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");
1736 bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file");
1737 bpf__strerror_end(buf, size);
1741 int bpf__strerror_load(struct bpf_object *obj,
1742 int err, char *buf, size_t size)
1744 bpf__strerror_head(err, buf, size);
1745 case LIBBPF_ERRNO__KVER: {
1746 unsigned int obj_kver = bpf_object__kversion(obj);
1747 unsigned int real_kver;
1749 if (fetch_kernel_version(&real_kver, NULL, 0)) {
1750 scnprintf(buf, size, "Unable to fetch kernel version");
1754 if (obj_kver != real_kver) {
1755 scnprintf(buf, size,
1756 "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")",
1757 KVER_PARAM(obj_kver),
1758 KVER_PARAM(real_kver));
1762 scnprintf(buf, size, "Failed to load program for unknown reason");
1765 bpf__strerror_end(buf, size);
1769 int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
1770 struct parse_events_term *term __maybe_unused,
1771 struct perf_evlist *evlist __maybe_unused,
1772 int *error_pos __maybe_unused, int err,
1773 char *buf, size_t size)
1775 bpf__strerror_head(err, buf, size);
1776 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE,
1777 "Can't use this config term with this map type");
1778 bpf__strerror_end(buf, size);
1782 int bpf__strerror_apply_obj_config(int err, char *buf, size_t size)
1784 bpf__strerror_head(err, buf, size);
1785 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM,
1786 "Cannot set event to BPF map in multi-thread tracing");
1787 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH,
1788 "%s (Hint: use -i to turn off inherit)", emsg);
1789 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE,
1790 "Can only put raw, hardware and BPF output event into a BPF map");
1791 bpf__strerror_end(buf, size);
1795 int bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused,
1796 int err, char *buf, size_t size)
1798 bpf__strerror_head(err, buf, size);
1799 bpf__strerror_end(buf, size);