1
2
3
4
5
6
7
8
9#include <linux/bpf.h>
10#include <bpf/libbpf.h>
11#include <bpf/bpf.h>
12#include <linux/err.h>
13#include <linux/kernel.h>
14#include <linux/string.h>
15#include <errno.h>
16#include "perf.h"
17#include "debug.h"
18#include "bpf-loader.h"
19#include "bpf-prologue.h"
20#include "probe-event.h"
21#include "probe-finder.h"
22#include "parse-events.h"
23#include "strfilter.h"
24#include "llvm-utils.h"
25#include "c++/clang-c.h"
26
27static int libbpf_perf_print(enum libbpf_print_level level __attribute__((unused)),
28 const char *fmt, va_list args)
29{
30 return veprintf(1, verbose, pr_fmt(fmt), args);
31}
32
33struct bpf_prog_priv {
34 bool is_tp;
35 char *sys_name;
36 char *evt_name;
37 struct perf_probe_event pev;
38 bool need_prologue;
39 struct bpf_insn *insns_buf;
40 int nr_types;
41 int *type_mapping;
42};
43
44static bool libbpf_initialized;
45
46struct bpf_object *
47bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
48{
49 struct bpf_object *obj;
50
51 if (!libbpf_initialized) {
52 libbpf_set_print(libbpf_perf_print);
53 libbpf_initialized = true;
54 }
55
56 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
57 if (IS_ERR_OR_NULL(obj)) {
58 pr_debug("bpf: failed to load buffer\n");
59 return ERR_PTR(-EINVAL);
60 }
61
62 return obj;
63}
64
65struct bpf_object *bpf__prepare_load(const char *filename, bool source)
66{
67 struct bpf_object *obj;
68
69 if (!libbpf_initialized) {
70 libbpf_set_print(libbpf_perf_print);
71 libbpf_initialized = true;
72 }
73
74 if (source) {
75 int err;
76 void *obj_buf;
77 size_t obj_buf_sz;
78
79 perf_clang__init();
80 err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
81 perf_clang__cleanup();
82 if (err) {
83 pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err);
84 err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
85 if (err)
86 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
87 } else
88 pr_debug("bpf: successful builtin compilation\n");
89 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
90
91 if (!IS_ERR_OR_NULL(obj) && llvm_param.dump_obj)
92 llvm__dump_obj(filename, obj_buf, obj_buf_sz);
93
94 free(obj_buf);
95 } else
96 obj = bpf_object__open(filename);
97
98 if (IS_ERR_OR_NULL(obj)) {
99 pr_debug("bpf: failed to load %s\n", filename);
100 return obj;
101 }
102
103 return obj;
104}
105
106void bpf__clear(void)
107{
108 struct bpf_object *obj, *tmp;
109
110 bpf_object__for_each_safe(obj, tmp) {
111 bpf__unprobe(obj);
112 bpf_object__close(obj);
113 }
114}
115
116static void
117clear_prog_priv(struct bpf_program *prog __maybe_unused,
118 void *_priv)
119{
120 struct bpf_prog_priv *priv = _priv;
121
122 cleanup_perf_probe_events(&priv->pev, 1);
123 zfree(&priv->insns_buf);
124 zfree(&priv->type_mapping);
125 zfree(&priv->sys_name);
126 zfree(&priv->evt_name);
127 free(priv);
128}
129
130static int
131prog_config__exec(const char *value, struct perf_probe_event *pev)
132{
133 pev->uprobes = true;
134 pev->target = strdup(value);
135 if (!pev->target)
136 return -ENOMEM;
137 return 0;
138}
139
140static int
141prog_config__module(const char *value, struct perf_probe_event *pev)
142{
143 pev->uprobes = false;
144 pev->target = strdup(value);
145 if (!pev->target)
146 return -ENOMEM;
147 return 0;
148}
149
150static int
151prog_config__bool(const char *value, bool *pbool, bool invert)
152{
153 int err;
154 bool bool_value;
155
156 if (!pbool)
157 return -EINVAL;
158
159 err = strtobool(value, &bool_value);
160 if (err)
161 return err;
162
163 *pbool = invert ? !bool_value : bool_value;
164 return 0;
165}
166
167static int
168prog_config__inlines(const char *value,
169 struct perf_probe_event *pev __maybe_unused)
170{
171 return prog_config__bool(value, &probe_conf.no_inlines, true);
172}
173
174static int
175prog_config__force(const char *value,
176 struct perf_probe_event *pev __maybe_unused)
177{
178 return prog_config__bool(value, &probe_conf.force_add, false);
179}
180
181static struct {
182 const char *key;
183 const char *usage;
184 const char *desc;
185 int (*func)(const char *, struct perf_probe_event *);
186} bpf_prog_config_terms[] = {
187 {
188 .key = "exec",
189 .usage = "exec=<full path of file>",
190 .desc = "Set uprobe target",
191 .func = prog_config__exec,
192 },
193 {
194 .key = "module",
195 .usage = "module=<module name> ",
196 .desc = "Set kprobe module",
197 .func = prog_config__module,
198 },
199 {
200 .key = "inlines",
201 .usage = "inlines=[yes|no] ",
202 .desc = "Probe at inline symbol",
203 .func = prog_config__inlines,
204 },
205 {
206 .key = "force",
207 .usage = "force=[yes|no] ",
208 .desc = "Forcibly add events with existing name",
209 .func = prog_config__force,
210 },
211};
212
213static int
214do_prog_config(const char *key, const char *value,
215 struct perf_probe_event *pev)
216{
217 unsigned int i;
218
219 pr_debug("config bpf program: %s=%s\n", key, value);
220 for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
221 if (strcmp(key, bpf_prog_config_terms[i].key) == 0)
222 return bpf_prog_config_terms[i].func(value, pev);
223
224 pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
225 key, value);
226
227 pr_debug("\nHint: Valid options are:\n");
228 for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
229 pr_debug("\t%s:\t%s\n", bpf_prog_config_terms[i].usage,
230 bpf_prog_config_terms[i].desc);
231 pr_debug("\n");
232
233 return -BPF_LOADER_ERRNO__PROGCONF_TERM;
234}
235
236static const char *
237parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev)
238{
239 char *text = strdup(config_str);
240 char *sep, *line;
241 const char *main_str = NULL;
242 int err = 0;
243
244 if (!text) {
245 pr_debug("Not enough memory: dup config_str failed\n");
246 return ERR_PTR(-ENOMEM);
247 }
248
249 line = text;
250 while ((sep = strchr(line, ';'))) {
251 char *equ;
252
253 *sep = '\0';
254 equ = strchr(line, '=');
255 if (!equ) {
256 pr_warning("WARNING: invalid config in BPF object: %s\n",
257 line);
258 pr_warning("\tShould be 'key=value'.\n");
259 goto nextline;
260 }
261 *equ = '\0';
262
263 err = do_prog_config(line, equ + 1, pev);
264 if (err)
265 break;
266nextline:
267 line = sep + 1;
268 }
269
270 if (!err)
271 main_str = config_str + (line - text);
272 free(text);
273
274 return err ? ERR_PTR(err) : main_str;
275}
276
277static int
278parse_prog_config(const char *config_str, const char **p_main_str,
279 bool *is_tp, struct perf_probe_event *pev)
280{
281 int err;
282 const char *main_str = parse_prog_config_kvpair(config_str, pev);
283
284 if (IS_ERR(main_str))
285 return PTR_ERR(main_str);
286
287 *p_main_str = main_str;
288 if (!strchr(main_str, '=')) {
289
290 const char *s = strchr(main_str, ':');
291
292 if (!s) {
293 pr_debug("bpf: '%s' is not a valid tracepoint\n",
294 config_str);
295 return -BPF_LOADER_ERRNO__CONFIG;
296 }
297
298 *is_tp = true;
299 return 0;
300 }
301
302 *is_tp = false;
303 err = parse_perf_probe_command(main_str, pev);
304 if (err < 0) {
305 pr_debug("bpf: '%s' is not a valid config string\n",
306 config_str);
307
308 return -BPF_LOADER_ERRNO__CONFIG;
309 }
310 return 0;
311}
312
313static int
314config_bpf_program(struct bpf_program *prog)
315{
316 struct perf_probe_event *pev = NULL;
317 struct bpf_prog_priv *priv = NULL;
318 const char *config_str, *main_str;
319 bool is_tp = false;
320 int err;
321
322
323 probe_conf.no_inlines = false;
324 probe_conf.force_add = false;
325
326 config_str = bpf_program__title(prog, false);
327 if (IS_ERR(config_str)) {
328 pr_debug("bpf: unable to get title for program\n");
329 return PTR_ERR(config_str);
330 }
331
332 priv = calloc(sizeof(*priv), 1);
333 if (!priv) {
334 pr_debug("bpf: failed to alloc priv\n");
335 return -ENOMEM;
336 }
337 pev = &priv->pev;
338
339 pr_debug("bpf: config program '%s'\n", config_str);
340 err = parse_prog_config(config_str, &main_str, &is_tp, pev);
341 if (err)
342 goto errout;
343
344 if (is_tp) {
345 char *s = strchr(main_str, ':');
346
347 priv->is_tp = true;
348 priv->sys_name = strndup(main_str, s - main_str);
349 priv->evt_name = strdup(s + 1);
350 goto set_priv;
351 }
352
353 if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
354 pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
355 config_str, PERF_BPF_PROBE_GROUP);
356 err = -BPF_LOADER_ERRNO__GROUP;
357 goto errout;
358 } else if (!pev->group)
359 pev->group = strdup(PERF_BPF_PROBE_GROUP);
360
361 if (!pev->group) {
362 pr_debug("bpf: strdup failed\n");
363 err = -ENOMEM;
364 goto errout;
365 }
366
367 if (!pev->event) {
368 pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
369 config_str);
370 err = -BPF_LOADER_ERRNO__EVENTNAME;
371 goto errout;
372 }
373 pr_debug("bpf: config '%s' is ok\n", config_str);
374
375set_priv:
376 err = bpf_program__set_priv(prog, priv, clear_prog_priv);
377 if (err) {
378 pr_debug("Failed to set priv for program '%s'\n", config_str);
379 goto errout;
380 }
381
382 return 0;
383
384errout:
385 if (pev)
386 clear_perf_probe_event(pev);
387 free(priv);
388 return err;
389}
390
391static int bpf__prepare_probe(void)
392{
393 static int err = 0;
394 static bool initialized = false;
395
396
397
398
399
400
401 if (initialized)
402 return err;
403
404 initialized = true;
405 err = init_probe_symbol_maps(false);
406 if (err < 0)
407 pr_debug("Failed to init_probe_symbol_maps\n");
408 probe_conf.max_probes = MAX_PROBES;
409 return err;
410}
411
412static int
413preproc_gen_prologue(struct bpf_program *prog, int n,
414 struct bpf_insn *orig_insns, int orig_insns_cnt,
415 struct bpf_prog_prep_result *res)
416{
417 struct bpf_prog_priv *priv = bpf_program__priv(prog);
418 struct probe_trace_event *tev;
419 struct perf_probe_event *pev;
420 struct bpf_insn *buf;
421 size_t prologue_cnt = 0;
422 int i, err;
423
424 if (IS_ERR(priv) || !priv || priv->is_tp)
425 goto errout;
426
427 pev = &priv->pev;
428
429 if (n < 0 || n >= priv->nr_types)
430 goto errout;
431
432
433 for (i = 0; i < pev->ntevs; i++) {
434 if (priv->type_mapping[i] == n)
435 break;
436 }
437
438 if (i >= pev->ntevs) {
439 pr_debug("Internal error: prologue type %d not found\n", n);
440 return -BPF_LOADER_ERRNO__PROLOGUE;
441 }
442
443 tev = &pev->tevs[i];
444
445 buf = priv->insns_buf;
446 err = bpf__gen_prologue(tev->args, tev->nargs,
447 buf, &prologue_cnt,
448 BPF_MAXINSNS - orig_insns_cnt);
449 if (err) {
450 const char *title;
451
452 title = bpf_program__title(prog, false);
453 if (!title)
454 title = "[unknown]";
455
456 pr_debug("Failed to generate prologue for program %s\n",
457 title);
458 return err;
459 }
460
461 memcpy(&buf[prologue_cnt], orig_insns,
462 sizeof(struct bpf_insn) * orig_insns_cnt);
463
464 res->new_insn_ptr = buf;
465 res->new_insn_cnt = prologue_cnt + orig_insns_cnt;
466 res->pfd = NULL;
467 return 0;
468
469errout:
470 pr_debug("Internal error in preproc_gen_prologue\n");
471 return -BPF_LOADER_ERRNO__PROLOGUE;
472}
473
474
475
476
477
478static int compare_tev_args(const void *ptev1, const void *ptev2)
479{
480 int i, ret;
481 const struct probe_trace_event *tev1 =
482 *(const struct probe_trace_event **)ptev1;
483 const struct probe_trace_event *tev2 =
484 *(const struct probe_trace_event **)ptev2;
485
486 ret = tev2->nargs - tev1->nargs;
487 if (ret)
488 return ret;
489
490 for (i = 0; i < tev1->nargs; i++) {
491 struct probe_trace_arg *arg1, *arg2;
492 struct probe_trace_arg_ref *ref1, *ref2;
493
494 arg1 = &tev1->args[i];
495 arg2 = &tev2->args[i];
496
497 ret = strcmp(arg1->value, arg2->value);
498 if (ret)
499 return ret;
500
501 ref1 = arg1->ref;
502 ref2 = arg2->ref;
503
504 while (ref1 && ref2) {
505 ret = ref2->offset - ref1->offset;
506 if (ret)
507 return ret;
508
509 ref1 = ref1->next;
510 ref2 = ref2->next;
511 }
512
513 if (ref1 || ref2)
514 return ref2 ? 1 : -1;
515 }
516
517 return 0;
518}
519
520
521
522
523
524
525static int map_prologue(struct perf_probe_event *pev, int *mapping,
526 int *nr_types)
527{
528 int i, type = 0;
529 struct probe_trace_event **ptevs;
530
531 size_t array_sz = sizeof(*ptevs) * pev->ntevs;
532
533 ptevs = malloc(array_sz);
534 if (!ptevs) {
535 pr_debug("Not enough memory: alloc ptevs failed\n");
536 return -ENOMEM;
537 }
538
539 pr_debug("In map_prologue, ntevs=%d\n", pev->ntevs);
540 for (i = 0; i < pev->ntevs; i++)
541 ptevs[i] = &pev->tevs[i];
542
543 qsort(ptevs, pev->ntevs, sizeof(*ptevs),
544 compare_tev_args);
545
546 for (i = 0; i < pev->ntevs; i++) {
547 int n;
548
549 n = ptevs[i] - pev->tevs;
550 if (i == 0) {
551 mapping[n] = type;
552 pr_debug("mapping[%d]=%d\n", n, type);
553 continue;
554 }
555
556 if (compare_tev_args(ptevs + i, ptevs + i - 1) == 0)
557 mapping[n] = type;
558 else
559 mapping[n] = ++type;
560
561 pr_debug("mapping[%d]=%d\n", n, mapping[n]);
562 }
563 free(ptevs);
564 *nr_types = type + 1;
565
566 return 0;
567}
568
569static int hook_load_preprocessor(struct bpf_program *prog)
570{
571 struct bpf_prog_priv *priv = bpf_program__priv(prog);
572 struct perf_probe_event *pev;
573 bool need_prologue = false;
574 int err, i;
575
576 if (IS_ERR(priv) || !priv) {
577 pr_debug("Internal error when hook preprocessor\n");
578 return -BPF_LOADER_ERRNO__INTERNAL;
579 }
580
581 if (priv->is_tp) {
582 priv->need_prologue = false;
583 return 0;
584 }
585
586 pev = &priv->pev;
587 for (i = 0; i < pev->ntevs; i++) {
588 struct probe_trace_event *tev = &pev->tevs[i];
589
590 if (tev->nargs > 0) {
591 need_prologue = true;
592 break;
593 }
594 }
595
596
597
598
599
600 if (!need_prologue) {
601 priv->need_prologue = false;
602 return 0;
603 }
604
605 priv->need_prologue = true;
606 priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS);
607 if (!priv->insns_buf) {
608 pr_debug("Not enough memory: alloc insns_buf failed\n");
609 return -ENOMEM;
610 }
611
612 priv->type_mapping = malloc(sizeof(int) * pev->ntevs);
613 if (!priv->type_mapping) {
614 pr_debug("Not enough memory: alloc type_mapping failed\n");
615 return -ENOMEM;
616 }
617 memset(priv->type_mapping, -1,
618 sizeof(int) * pev->ntevs);
619
620 err = map_prologue(pev, priv->type_mapping, &priv->nr_types);
621 if (err)
622 return err;
623
624 err = bpf_program__set_prep(prog, priv->nr_types,
625 preproc_gen_prologue);
626 return err;
627}
628
629int bpf__probe(struct bpf_object *obj)
630{
631 int err = 0;
632 struct bpf_program *prog;
633 struct bpf_prog_priv *priv;
634 struct perf_probe_event *pev;
635
636 err = bpf__prepare_probe();
637 if (err) {
638 pr_debug("bpf__prepare_probe failed\n");
639 return err;
640 }
641
642 bpf_object__for_each_program(prog, obj) {
643 err = config_bpf_program(prog);
644 if (err)
645 goto out;
646
647 priv = bpf_program__priv(prog);
648 if (IS_ERR(priv) || !priv) {
649 err = PTR_ERR(priv);
650 goto out;
651 }
652
653 if (priv->is_tp) {
654 bpf_program__set_tracepoint(prog);
655 continue;
656 }
657
658 bpf_program__set_kprobe(prog);
659 pev = &priv->pev;
660
661 err = convert_perf_probe_events(pev, 1);
662 if (err < 0) {
663 pr_debug("bpf_probe: failed to convert perf probe events\n");
664 goto out;
665 }
666
667 err = apply_perf_probe_events(pev, 1);
668 if (err < 0) {
669 pr_debug("bpf_probe: failed to apply perf probe events\n");
670 goto out;
671 }
672
673
674
675
676
677
678
679
680
681 err = hook_load_preprocessor(prog);
682 if (err)
683 goto out;
684 }
685out:
686 return err < 0 ? err : 0;
687}
688
689#define EVENTS_WRITE_BUFSIZE 4096
690int bpf__unprobe(struct bpf_object *obj)
691{
692 int err, ret = 0;
693 struct bpf_program *prog;
694
695 bpf_object__for_each_program(prog, obj) {
696 struct bpf_prog_priv *priv = bpf_program__priv(prog);
697 int i;
698
699 if (IS_ERR(priv) || !priv || priv->is_tp)
700 continue;
701
702 for (i = 0; i < priv->pev.ntevs; i++) {
703 struct probe_trace_event *tev = &priv->pev.tevs[i];
704 char name_buf[EVENTS_WRITE_BUFSIZE];
705 struct strfilter *delfilter;
706
707 snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
708 "%s:%s", tev->group, tev->event);
709 name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
710
711 delfilter = strfilter__new(name_buf, NULL);
712 if (!delfilter) {
713 pr_debug("Failed to create filter for unprobing\n");
714 ret = -ENOMEM;
715 continue;
716 }
717
718 err = del_perf_probe_events(delfilter);
719 strfilter__delete(delfilter);
720 if (err) {
721 pr_debug("Failed to delete %s\n", name_buf);
722 ret = err;
723 continue;
724 }
725 }
726 }
727 return ret;
728}
729
730int bpf__load(struct bpf_object *obj)
731{
732 int err;
733
734 err = bpf_object__load(obj);
735 if (err) {
736 char bf[128];
737 libbpf_strerror(err, bf, sizeof(bf));
738 pr_debug("bpf: load objects failed: err=%d: (%s)\n", err, bf);
739 return err;
740 }
741 return 0;
742}
743
744int bpf__foreach_event(struct bpf_object *obj,
745 bpf_prog_iter_callback_t func,
746 void *arg)
747{
748 struct bpf_program *prog;
749 int err;
750
751 bpf_object__for_each_program(prog, obj) {
752 struct bpf_prog_priv *priv = bpf_program__priv(prog);
753 struct probe_trace_event *tev;
754 struct perf_probe_event *pev;
755 int i, fd;
756
757 if (IS_ERR(priv) || !priv) {
758 pr_debug("bpf: failed to get private field\n");
759 return -BPF_LOADER_ERRNO__INTERNAL;
760 }
761
762 if (priv->is_tp) {
763 fd = bpf_program__fd(prog);
764 err = (*func)(priv->sys_name, priv->evt_name, fd, arg);
765 if (err) {
766 pr_debug("bpf: tracepoint call back failed, stop iterate\n");
767 return err;
768 }
769 continue;
770 }
771
772 pev = &priv->pev;
773 for (i = 0; i < pev->ntevs; i++) {
774 tev = &pev->tevs[i];
775
776 if (priv->need_prologue) {
777 int type = priv->type_mapping[i];
778
779 fd = bpf_program__nth_fd(prog, type);
780 } else {
781 fd = bpf_program__fd(prog);
782 }
783
784 if (fd < 0) {
785 pr_debug("bpf: failed to get file descriptor\n");
786 return fd;
787 }
788
789 err = (*func)(tev->group, tev->event, fd, arg);
790 if (err) {
791 pr_debug("bpf: call back failed, stop iterate\n");
792 return err;
793 }
794 }
795 }
796 return 0;
797}
798
799enum bpf_map_op_type {
800 BPF_MAP_OP_SET_VALUE,
801 BPF_MAP_OP_SET_EVSEL,
802};
803
804enum bpf_map_key_type {
805 BPF_MAP_KEY_ALL,
806 BPF_MAP_KEY_RANGES,
807};
808
809struct bpf_map_op {
810 struct list_head list;
811 enum bpf_map_op_type op_type;
812 enum bpf_map_key_type key_type;
813 union {
814 struct parse_events_array array;
815 } k;
816 union {
817 u64 value;
818 struct perf_evsel *evsel;
819 } v;
820};
821
822struct bpf_map_priv {
823 struct list_head ops_list;
824};
825
826static void
827bpf_map_op__delete(struct bpf_map_op *op)
828{
829 if (!list_empty(&op->list))
830 list_del(&op->list);
831 if (op->key_type == BPF_MAP_KEY_RANGES)
832 parse_events__clear_array(&op->k.array);
833 free(op);
834}
835
836static void
837bpf_map_priv__purge(struct bpf_map_priv *priv)
838{
839 struct bpf_map_op *pos, *n;
840
841 list_for_each_entry_safe(pos, n, &priv->ops_list, list) {
842 list_del_init(&pos->list);
843 bpf_map_op__delete(pos);
844 }
845}
846
847static void
848bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
849 void *_priv)
850{
851 struct bpf_map_priv *priv = _priv;
852
853 bpf_map_priv__purge(priv);
854 free(priv);
855}
856
857static int
858bpf_map_op_setkey(struct bpf_map_op *op, struct parse_events_term *term)
859{
860 op->key_type = BPF_MAP_KEY_ALL;
861 if (!term)
862 return 0;
863
864 if (term->array.nr_ranges) {
865 size_t memsz = term->array.nr_ranges *
866 sizeof(op->k.array.ranges[0]);
867
868 op->k.array.ranges = memdup(term->array.ranges, memsz);
869 if (!op->k.array.ranges) {
870 pr_debug("Not enough memory to alloc indices for map\n");
871 return -ENOMEM;
872 }
873 op->key_type = BPF_MAP_KEY_RANGES;
874 op->k.array.nr_ranges = term->array.nr_ranges;
875 }
876 return 0;
877}
878
879static struct bpf_map_op *
880bpf_map_op__new(struct parse_events_term *term)
881{
882 struct bpf_map_op *op;
883 int err;
884
885 op = zalloc(sizeof(*op));
886 if (!op) {
887 pr_debug("Failed to alloc bpf_map_op\n");
888 return ERR_PTR(-ENOMEM);
889 }
890 INIT_LIST_HEAD(&op->list);
891
892 err = bpf_map_op_setkey(op, term);
893 if (err) {
894 free(op);
895 return ERR_PTR(err);
896 }
897 return op;
898}
899
900static struct bpf_map_op *
901bpf_map_op__clone(struct bpf_map_op *op)
902{
903 struct bpf_map_op *newop;
904
905 newop = memdup(op, sizeof(*op));
906 if (!newop) {
907 pr_debug("Failed to alloc bpf_map_op\n");
908 return NULL;
909 }
910
911 INIT_LIST_HEAD(&newop->list);
912 if (op->key_type == BPF_MAP_KEY_RANGES) {
913 size_t memsz = op->k.array.nr_ranges *
914 sizeof(op->k.array.ranges[0]);
915
916 newop->k.array.ranges = memdup(op->k.array.ranges, memsz);
917 if (!newop->k.array.ranges) {
918 pr_debug("Failed to alloc indices for map\n");
919 free(newop);
920 return NULL;
921 }
922 }
923
924 return newop;
925}
926
927static struct bpf_map_priv *
928bpf_map_priv__clone(struct bpf_map_priv *priv)
929{
930 struct bpf_map_priv *newpriv;
931 struct bpf_map_op *pos, *newop;
932
933 newpriv = zalloc(sizeof(*newpriv));
934 if (!newpriv) {
935 pr_debug("Not enough memory to alloc map private\n");
936 return NULL;
937 }
938 INIT_LIST_HEAD(&newpriv->ops_list);
939
940 list_for_each_entry(pos, &priv->ops_list, list) {
941 newop = bpf_map_op__clone(pos);
942 if (!newop) {
943 bpf_map_priv__purge(newpriv);
944 return NULL;
945 }
946 list_add_tail(&newop->list, &newpriv->ops_list);
947 }
948
949 return newpriv;
950}
951
952static int
953bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
954{
955 const char *map_name = bpf_map__name(map);
956 struct bpf_map_priv *priv = bpf_map__priv(map);
957
958 if (IS_ERR(priv)) {
959 pr_debug("Failed to get private from map %s\n", map_name);
960 return PTR_ERR(priv);
961 }
962
963 if (!priv) {
964 priv = zalloc(sizeof(*priv));
965 if (!priv) {
966 pr_debug("Not enough memory to alloc map private\n");
967 return -ENOMEM;
968 }
969 INIT_LIST_HEAD(&priv->ops_list);
970
971 if (bpf_map__set_priv(map, priv, bpf_map_priv__clear)) {
972 free(priv);
973 return -BPF_LOADER_ERRNO__INTERNAL;
974 }
975 }
976
977 list_add_tail(&op->list, &priv->ops_list);
978 return 0;
979}
980
981static struct bpf_map_op *
982bpf_map__add_newop(struct bpf_map *map, struct parse_events_term *term)
983{
984 struct bpf_map_op *op;
985 int err;
986
987 op = bpf_map_op__new(term);
988 if (IS_ERR(op))
989 return op;
990
991 err = bpf_map__add_op(map, op);
992 if (err) {
993 bpf_map_op__delete(op);
994 return ERR_PTR(err);
995 }
996 return op;
997}
998
999static int
1000__bpf_map__config_value(struct bpf_map *map,
1001 struct parse_events_term *term)
1002{
1003 struct bpf_map_op *op;
1004 const char *map_name = bpf_map__name(map);
1005 const struct bpf_map_def *def = bpf_map__def(map);
1006
1007 if (IS_ERR(def)) {
1008 pr_debug("Unable to get map definition from '%s'\n",
1009 map_name);
1010 return -BPF_LOADER_ERRNO__INTERNAL;
1011 }
1012
1013 if (def->type != BPF_MAP_TYPE_ARRAY) {
1014 pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n",
1015 map_name);
1016 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1017 }
1018 if (def->key_size < sizeof(unsigned int)) {
1019 pr_debug("Map %s has incorrect key size\n", map_name);
1020 return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE;
1021 }
1022 switch (def->value_size) {
1023 case 1:
1024 case 2:
1025 case 4:
1026 case 8:
1027 break;
1028 default:
1029 pr_debug("Map %s has incorrect value size\n", map_name);
1030 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1031 }
1032
1033 op = bpf_map__add_newop(map, term);
1034 if (IS_ERR(op))
1035 return PTR_ERR(op);
1036 op->op_type = BPF_MAP_OP_SET_VALUE;
1037 op->v.value = term->val.num;
1038 return 0;
1039}
1040
1041static int
1042bpf_map__config_value(struct bpf_map *map,
1043 struct parse_events_term *term,
1044 struct perf_evlist *evlist __maybe_unused)
1045{
1046 if (!term->err_val) {
1047 pr_debug("Config value not set\n");
1048 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1049 }
1050
1051 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) {
1052 pr_debug("ERROR: wrong value type for 'value'\n");
1053 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1054 }
1055
1056 return __bpf_map__config_value(map, term);
1057}
1058
1059static int
1060__bpf_map__config_event(struct bpf_map *map,
1061 struct parse_events_term *term,
1062 struct perf_evlist *evlist)
1063{
1064 struct perf_evsel *evsel;
1065 const struct bpf_map_def *def;
1066 struct bpf_map_op *op;
1067 const char *map_name = bpf_map__name(map);
1068
1069 evsel = perf_evlist__find_evsel_by_str(evlist, term->val.str);
1070 if (!evsel) {
1071 pr_debug("Event (for '%s') '%s' doesn't exist\n",
1072 map_name, term->val.str);
1073 return -BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT;
1074 }
1075
1076 def = bpf_map__def(map);
1077 if (IS_ERR(def)) {
1078 pr_debug("Unable to get map definition from '%s'\n",
1079 map_name);
1080 return PTR_ERR(def);
1081 }
1082
1083
1084
1085
1086
1087 if (def->type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
1088 pr_debug("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
1089 map_name);
1090 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1091 }
1092
1093 op = bpf_map__add_newop(map, term);
1094 if (IS_ERR(op))
1095 return PTR_ERR(op);
1096 op->op_type = BPF_MAP_OP_SET_EVSEL;
1097 op->v.evsel = evsel;
1098 return 0;
1099}
1100
1101static int
1102bpf_map__config_event(struct bpf_map *map,
1103 struct parse_events_term *term,
1104 struct perf_evlist *evlist)
1105{
1106 if (!term->err_val) {
1107 pr_debug("Config value not set\n");
1108 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1109 }
1110
1111 if (term->type_val != PARSE_EVENTS__TERM_TYPE_STR) {
1112 pr_debug("ERROR: wrong value type for 'event'\n");
1113 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1114 }
1115
1116 return __bpf_map__config_event(map, term, evlist);
1117}
1118
1119struct bpf_obj_config__map_func {
1120 const char *config_opt;
1121 int (*config_func)(struct bpf_map *, struct parse_events_term *,
1122 struct perf_evlist *);
1123};
1124
1125struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = {
1126 {"value", bpf_map__config_value},
1127 {"event", bpf_map__config_event},
1128};
1129
1130static int
1131config_map_indices_range_check(struct parse_events_term *term,
1132 struct bpf_map *map,
1133 const char *map_name)
1134{
1135 struct parse_events_array *array = &term->array;
1136 const struct bpf_map_def *def;
1137 unsigned int i;
1138
1139 if (!array->nr_ranges)
1140 return 0;
1141 if (!array->ranges) {
1142 pr_debug("ERROR: map %s: array->nr_ranges is %d but range array is NULL\n",
1143 map_name, (int)array->nr_ranges);
1144 return -BPF_LOADER_ERRNO__INTERNAL;
1145 }
1146
1147 def = bpf_map__def(map);
1148 if (IS_ERR(def)) {
1149 pr_debug("ERROR: Unable to get map definition from '%s'\n",
1150 map_name);
1151 return -BPF_LOADER_ERRNO__INTERNAL;
1152 }
1153
1154 for (i = 0; i < array->nr_ranges; i++) {
1155 unsigned int start = array->ranges[i].start;
1156 size_t length = array->ranges[i].length;
1157 unsigned int idx = start + length - 1;
1158
1159 if (idx >= def->max_entries) {
1160 pr_debug("ERROR: index %d too large\n", idx);
1161 return -BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG;
1162 }
1163 }
1164 return 0;
1165}
1166
1167static int
1168bpf__obj_config_map(struct bpf_object *obj,
1169 struct parse_events_term *term,
1170 struct perf_evlist *evlist,
1171 int *key_scan_pos)
1172{
1173
1174 char *map_name = strdup(term->config + sizeof("map:") - 1);
1175 struct bpf_map *map;
1176 int err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1177 char *map_opt;
1178 size_t i;
1179
1180 if (!map_name)
1181 return -ENOMEM;
1182
1183 map_opt = strchr(map_name, '.');
1184 if (!map_opt) {
1185 pr_debug("ERROR: Invalid map config: %s\n", map_name);
1186 goto out;
1187 }
1188
1189 *map_opt++ = '\0';
1190 if (*map_opt == '\0') {
1191 pr_debug("ERROR: Invalid map option: %s\n", term->config);
1192 goto out;
1193 }
1194
1195 map = bpf_object__find_map_by_name(obj, map_name);
1196 if (!map) {
1197 pr_debug("ERROR: Map %s doesn't exist\n", map_name);
1198 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST;
1199 goto out;
1200 }
1201
1202 *key_scan_pos += strlen(map_opt);
1203 err = config_map_indices_range_check(term, map, map_name);
1204 if (err)
1205 goto out;
1206 *key_scan_pos -= strlen(map_opt);
1207
1208 for (i = 0; i < ARRAY_SIZE(bpf_obj_config__map_funcs); i++) {
1209 struct bpf_obj_config__map_func *func =
1210 &bpf_obj_config__map_funcs[i];
1211
1212 if (strcmp(map_opt, func->config_opt) == 0) {
1213 err = func->config_func(map, term, evlist);
1214 goto out;
1215 }
1216 }
1217
1218 pr_debug("ERROR: Invalid map config option '%s'\n", map_opt);
1219 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT;
1220out:
1221 free(map_name);
1222 if (!err)
1223 key_scan_pos += strlen(map_opt);
1224 return err;
1225}
1226
1227int bpf__config_obj(struct bpf_object *obj,
1228 struct parse_events_term *term,
1229 struct perf_evlist *evlist,
1230 int *error_pos)
1231{
1232 int key_scan_pos = 0;
1233 int err;
1234
1235 if (!obj || !term || !term->config)
1236 return -EINVAL;
1237
1238 if (strstarts(term->config, "map:")) {
1239 key_scan_pos = sizeof("map:") - 1;
1240 err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
1241 goto out;
1242 }
1243 err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1244out:
1245 if (error_pos)
1246 *error_pos = key_scan_pos;
1247 return err;
1248
1249}
1250
1251typedef int (*map_config_func_t)(const char *name, int map_fd,
1252 const struct bpf_map_def *pdef,
1253 struct bpf_map_op *op,
1254 void *pkey, void *arg);
1255
1256static int
1257foreach_key_array_all(map_config_func_t func,
1258 void *arg, const char *name,
1259 int map_fd, const struct bpf_map_def *pdef,
1260 struct bpf_map_op *op)
1261{
1262 unsigned int i;
1263 int err;
1264
1265 for (i = 0; i < pdef->max_entries; i++) {
1266 err = func(name, map_fd, pdef, op, &i, arg);
1267 if (err) {
1268 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1269 name, i);
1270 return err;
1271 }
1272 }
1273 return 0;
1274}
1275
1276static int
1277foreach_key_array_ranges(map_config_func_t func, void *arg,
1278 const char *name, int map_fd,
1279 const struct bpf_map_def *pdef,
1280 struct bpf_map_op *op)
1281{
1282 unsigned int i, j;
1283 int err;
1284
1285 for (i = 0; i < op->k.array.nr_ranges; i++) {
1286 unsigned int start = op->k.array.ranges[i].start;
1287 size_t length = op->k.array.ranges[i].length;
1288
1289 for (j = 0; j < length; j++) {
1290 unsigned int idx = start + j;
1291
1292 err = func(name, map_fd, pdef, op, &idx, arg);
1293 if (err) {
1294 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1295 name, idx);
1296 return err;
1297 }
1298 }
1299 }
1300 return 0;
1301}
1302
1303static int
1304bpf_map_config_foreach_key(struct bpf_map *map,
1305 map_config_func_t func,
1306 void *arg)
1307{
1308 int err, map_fd;
1309 struct bpf_map_op *op;
1310 const struct bpf_map_def *def;
1311 const char *name = bpf_map__name(map);
1312 struct bpf_map_priv *priv = bpf_map__priv(map);
1313
1314 if (IS_ERR(priv)) {
1315 pr_debug("ERROR: failed to get private from map %s\n", name);
1316 return -BPF_LOADER_ERRNO__INTERNAL;
1317 }
1318 if (!priv || list_empty(&priv->ops_list)) {
1319 pr_debug("INFO: nothing to config for map %s\n", name);
1320 return 0;
1321 }
1322
1323 def = bpf_map__def(map);
1324 if (IS_ERR(def)) {
1325 pr_debug("ERROR: failed to get definition from map %s\n", name);
1326 return -BPF_LOADER_ERRNO__INTERNAL;
1327 }
1328 map_fd = bpf_map__fd(map);
1329 if (map_fd < 0) {
1330 pr_debug("ERROR: failed to get fd from map %s\n", name);
1331 return map_fd;
1332 }
1333
1334 list_for_each_entry(op, &priv->ops_list, list) {
1335 switch (def->type) {
1336 case BPF_MAP_TYPE_ARRAY:
1337 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1338 switch (op->key_type) {
1339 case BPF_MAP_KEY_ALL:
1340 err = foreach_key_array_all(func, arg, name,
1341 map_fd, def, op);
1342 break;
1343 case BPF_MAP_KEY_RANGES:
1344 err = foreach_key_array_ranges(func, arg, name,
1345 map_fd, def,
1346 op);
1347 break;
1348 default:
1349 pr_debug("ERROR: keytype for map '%s' invalid\n",
1350 name);
1351 return -BPF_LOADER_ERRNO__INTERNAL;
1352 }
1353 if (err)
1354 return err;
1355 break;
1356 default:
1357 pr_debug("ERROR: type of '%s' incorrect\n", name);
1358 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1359 }
1360 }
1361
1362 return 0;
1363}
1364
1365static int
1366apply_config_value_for_key(int map_fd, void *pkey,
1367 size_t val_size, u64 val)
1368{
1369 int err = 0;
1370
1371 switch (val_size) {
1372 case 1: {
1373 u8 _val = (u8)(val);
1374 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1375 break;
1376 }
1377 case 2: {
1378 u16 _val = (u16)(val);
1379 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1380 break;
1381 }
1382 case 4: {
1383 u32 _val = (u32)(val);
1384 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1385 break;
1386 }
1387 case 8: {
1388 err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY);
1389 break;
1390 }
1391 default:
1392 pr_debug("ERROR: invalid value size\n");
1393 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1394 }
1395 if (err && errno)
1396 err = -errno;
1397 return err;
1398}
1399
1400static int
1401apply_config_evsel_for_key(const char *name, int map_fd, void *pkey,
1402 struct perf_evsel *evsel)
1403{
1404 struct xyarray *xy = evsel->fd;
1405 struct perf_event_attr *attr;
1406 unsigned int key, events;
1407 bool check_pass = false;
1408 int *evt_fd;
1409 int err;
1410
1411 if (!xy) {
1412 pr_debug("ERROR: evsel not ready for map %s\n", name);
1413 return -BPF_LOADER_ERRNO__INTERNAL;
1414 }
1415
1416 if (xy->row_size / xy->entry_size != 1) {
1417 pr_debug("ERROR: Dimension of target event is incorrect for map %s\n",
1418 name);
1419 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM;
1420 }
1421
1422 attr = &evsel->attr;
1423 if (attr->inherit) {
1424 pr_debug("ERROR: Can't put inherit event into map %s\n", name);
1425 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH;
1426 }
1427
1428 if (perf_evsel__is_bpf_output(evsel))
1429 check_pass = true;
1430 if (attr->type == PERF_TYPE_RAW)
1431 check_pass = true;
1432 if (attr->type == PERF_TYPE_HARDWARE)
1433 check_pass = true;
1434 if (!check_pass) {
1435 pr_debug("ERROR: Event type is wrong for map %s\n", name);
1436 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE;
1437 }
1438
1439 events = xy->entries / (xy->row_size / xy->entry_size);
1440 key = *((unsigned int *)pkey);
1441 if (key >= events) {
1442 pr_debug("ERROR: there is no event %d for map %s\n",
1443 key, name);
1444 return -BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE;
1445 }
1446 evt_fd = xyarray__entry(xy, key, 0);
1447 err = bpf_map_update_elem(map_fd, pkey, evt_fd, BPF_ANY);
1448 if (err && errno)
1449 err = -errno;
1450 return err;
1451}
1452
1453static int
1454apply_obj_config_map_for_key(const char *name, int map_fd,
1455 const struct bpf_map_def *pdef,
1456 struct bpf_map_op *op,
1457 void *pkey, void *arg __maybe_unused)
1458{
1459 int err;
1460
1461 switch (op->op_type) {
1462 case BPF_MAP_OP_SET_VALUE:
1463 err = apply_config_value_for_key(map_fd, pkey,
1464 pdef->value_size,
1465 op->v.value);
1466 break;
1467 case BPF_MAP_OP_SET_EVSEL:
1468 err = apply_config_evsel_for_key(name, map_fd, pkey,
1469 op->v.evsel);
1470 break;
1471 default:
1472 pr_debug("ERROR: unknown value type for '%s'\n", name);
1473 err = -BPF_LOADER_ERRNO__INTERNAL;
1474 }
1475 return err;
1476}
1477
1478static int
1479apply_obj_config_map(struct bpf_map *map)
1480{
1481 return bpf_map_config_foreach_key(map,
1482 apply_obj_config_map_for_key,
1483 NULL);
1484}
1485
1486static int
1487apply_obj_config_object(struct bpf_object *obj)
1488{
1489 struct bpf_map *map;
1490 int err;
1491
1492 bpf_map__for_each(map, obj) {
1493 err = apply_obj_config_map(map);
1494 if (err)
1495 return err;
1496 }
1497 return 0;
1498}
1499
1500int bpf__apply_obj_config(void)
1501{
1502 struct bpf_object *obj, *tmp;
1503 int err;
1504
1505 bpf_object__for_each_safe(obj, tmp) {
1506 err = apply_obj_config_object(obj);
1507 if (err)
1508 return err;
1509 }
1510
1511 return 0;
1512}
1513
1514#define bpf__for_each_map(pos, obj, objtmp) \
1515 bpf_object__for_each_safe(obj, objtmp) \
1516 bpf_map__for_each(pos, obj)
1517
1518#define bpf__for_each_map_named(pos, obj, objtmp, name) \
1519 bpf__for_each_map(pos, obj, objtmp) \
1520 if (bpf_map__name(pos) && \
1521 (strcmp(name, \
1522 bpf_map__name(pos)) == 0))
1523
1524struct perf_evsel *bpf__setup_output_event(struct perf_evlist *evlist, const char *name)
1525{
1526 struct bpf_map_priv *tmpl_priv = NULL;
1527 struct bpf_object *obj, *tmp;
1528 struct perf_evsel *evsel = NULL;
1529 struct bpf_map *map;
1530 int err;
1531 bool need_init = false;
1532
1533 bpf__for_each_map_named(map, obj, tmp, name) {
1534 struct bpf_map_priv *priv = bpf_map__priv(map);
1535
1536 if (IS_ERR(priv))
1537 return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
1538
1539
1540
1541
1542
1543 if (!need_init && !priv)
1544 need_init = !priv;
1545 if (!tmpl_priv && priv)
1546 tmpl_priv = priv;
1547 }
1548
1549 if (!need_init)
1550 return NULL;
1551
1552 if (!tmpl_priv) {
1553 char *event_definition = NULL;
1554
1555 if (asprintf(&event_definition, "bpf-output/no-inherit=1,name=%s/", name) < 0)
1556 return ERR_PTR(-ENOMEM);
1557
1558 err = parse_events(evlist, event_definition, NULL);
1559 free(event_definition);
1560
1561 if (err) {
1562 pr_debug("ERROR: failed to create the \"%s\" bpf-output event\n", name);
1563 return ERR_PTR(-err);
1564 }
1565
1566 evsel = perf_evlist__last(evlist);
1567 }
1568
1569 bpf__for_each_map_named(map, obj, tmp, name) {
1570 struct bpf_map_priv *priv = bpf_map__priv(map);
1571
1572 if (IS_ERR(priv))
1573 return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
1574 if (priv)
1575 continue;
1576
1577 if (tmpl_priv) {
1578 priv = bpf_map_priv__clone(tmpl_priv);
1579 if (!priv)
1580 return ERR_PTR(-ENOMEM);
1581
1582 err = bpf_map__set_priv(map, priv, bpf_map_priv__clear);
1583 if (err) {
1584 bpf_map_priv__clear(map, priv);
1585 return ERR_PTR(err);
1586 }
1587 } else if (evsel) {
1588 struct bpf_map_op *op;
1589
1590 op = bpf_map__add_newop(map, NULL);
1591 if (IS_ERR(op))
1592 return ERR_CAST(op);
1593 op->op_type = BPF_MAP_OP_SET_EVSEL;
1594 op->v.evsel = evsel;
1595 }
1596 }
1597
1598 return evsel;
1599}
1600
1601int bpf__setup_stdout(struct perf_evlist *evlist)
1602{
1603 struct perf_evsel *evsel = bpf__setup_output_event(evlist, "__bpf_stdout__");
1604 return PTR_ERR_OR_ZERO(evsel);
1605}
1606
1607#define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
1608#define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
1609#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
1610
1611static const char *bpf_loader_strerror_table[NR_ERRNO] = {
1612 [ERRCODE_OFFSET(CONFIG)] = "Invalid config string",
1613 [ERRCODE_OFFSET(GROUP)] = "Invalid group name",
1614 [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string",
1615 [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error",
1616 [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet",
1617 [ERRCODE_OFFSET(PROGCONF_TERM)] = "Invalid program config term in config string",
1618 [ERRCODE_OFFSET(PROLOGUE)] = "Failed to generate prologue",
1619 [ERRCODE_OFFSET(PROLOGUE2BIG)] = "Prologue too big for program",
1620 [ERRCODE_OFFSET(PROLOGUEOOB)] = "Offset out of bound for prologue",
1621 [ERRCODE_OFFSET(OBJCONF_OPT)] = "Invalid object config option",
1622 [ERRCODE_OFFSET(OBJCONF_CONF)] = "Config value not set (missing '=')",
1623 [ERRCODE_OFFSET(OBJCONF_MAP_OPT)] = "Invalid object map config option",
1624 [ERRCODE_OFFSET(OBJCONF_MAP_NOTEXIST)] = "Target map doesn't exist",
1625 [ERRCODE_OFFSET(OBJCONF_MAP_VALUE)] = "Incorrect value type for map",
1626 [ERRCODE_OFFSET(OBJCONF_MAP_TYPE)] = "Incorrect map type",
1627 [ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)] = "Incorrect map key size",
1628 [ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size",
1629 [ERRCODE_OFFSET(OBJCONF_MAP_NOEVT)] = "Event not found for map setting",
1630 [ERRCODE_OFFSET(OBJCONF_MAP_MAPSIZE)] = "Invalid map size for event setting",
1631 [ERRCODE_OFFSET(OBJCONF_MAP_EVTDIM)] = "Event dimension too large",
1632 [ERRCODE_OFFSET(OBJCONF_MAP_EVTINH)] = "Doesn't support inherit event",
1633 [ERRCODE_OFFSET(OBJCONF_MAP_EVTTYPE)] = "Wrong event type for map",
1634 [ERRCODE_OFFSET(OBJCONF_MAP_IDX2BIG)] = "Index too large",
1635};
1636
1637static int
1638bpf_loader_strerror(int err, char *buf, size_t size)
1639{
1640 char sbuf[STRERR_BUFSIZE];
1641 const char *msg;
1642
1643 if (!buf || !size)
1644 return -1;
1645
1646 err = err > 0 ? err : -err;
1647
1648 if (err >= __LIBBPF_ERRNO__START)
1649 return libbpf_strerror(err, buf, size);
1650
1651 if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) {
1652 msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)];
1653 snprintf(buf, size, "%s", msg);
1654 buf[size - 1] = '\0';
1655 return 0;
1656 }
1657
1658 if (err >= __BPF_LOADER_ERRNO__END)
1659 snprintf(buf, size, "Unknown bpf loader error %d", err);
1660 else
1661 snprintf(buf, size, "%s",
1662 str_error_r(err, sbuf, sizeof(sbuf)));
1663
1664 buf[size - 1] = '\0';
1665 return -1;
1666}
1667
1668#define bpf__strerror_head(err, buf, size) \
1669 char sbuf[STRERR_BUFSIZE], *emsg;\
1670 if (!size)\
1671 return 0;\
1672 if (err < 0)\
1673 err = -err;\
1674 bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
1675 emsg = sbuf;\
1676 switch (err) {\
1677 default:\
1678 scnprintf(buf, size, "%s", emsg);\
1679 break;
1680
1681#define bpf__strerror_entry(val, fmt...)\
1682 case val: {\
1683 scnprintf(buf, size, fmt);\
1684 break;\
1685 }
1686
1687#define bpf__strerror_end(buf, size)\
1688 }\
1689 buf[size - 1] = '\0';
1690
1691int bpf__strerror_prepare_load(const char *filename, bool source,
1692 int err, char *buf, size_t size)
1693{
1694 size_t n;
1695 int ret;
1696
1697 n = snprintf(buf, size, "Failed to load %s%s: ",
1698 filename, source ? " from source" : "");
1699 if (n >= size) {
1700 buf[size - 1] = '\0';
1701 return 0;
1702 }
1703 buf += n;
1704 size -= n;
1705
1706 ret = bpf_loader_strerror(err, buf, size);
1707 buf[size - 1] = '\0';
1708 return ret;
1709}
1710
1711int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
1712 int err, char *buf, size_t size)
1713{
1714 bpf__strerror_head(err, buf, size);
1715 case BPF_LOADER_ERRNO__PROGCONF_TERM: {
1716 scnprintf(buf, size, "%s (add -v to see detail)", emsg);
1717 break;
1718 }
1719 bpf__strerror_entry(EEXIST, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'");
1720 bpf__strerror_entry(EACCES, "You need to be root");
1721 bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");
1722 bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file");
1723 bpf__strerror_end(buf, size);
1724 return 0;
1725}
1726
1727int bpf__strerror_load(struct bpf_object *obj,
1728 int err, char *buf, size_t size)
1729{
1730 bpf__strerror_head(err, buf, size);
1731 case LIBBPF_ERRNO__KVER: {
1732 unsigned int obj_kver = bpf_object__kversion(obj);
1733 unsigned int real_kver;
1734
1735 if (fetch_kernel_version(&real_kver, NULL, 0)) {
1736 scnprintf(buf, size, "Unable to fetch kernel version");
1737 break;
1738 }
1739
1740 if (obj_kver != real_kver) {
1741 scnprintf(buf, size,
1742 "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")",
1743 KVER_PARAM(obj_kver),
1744 KVER_PARAM(real_kver));
1745 break;
1746 }
1747
1748 scnprintf(buf, size, "Failed to load program for unknown reason");
1749 break;
1750 }
1751 bpf__strerror_end(buf, size);
1752 return 0;
1753}
1754
1755int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
1756 struct parse_events_term *term __maybe_unused,
1757 struct perf_evlist *evlist __maybe_unused,
1758 int *error_pos __maybe_unused, int err,
1759 char *buf, size_t size)
1760{
1761 bpf__strerror_head(err, buf, size);
1762 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE,
1763 "Can't use this config term with this map type");
1764 bpf__strerror_end(buf, size);
1765 return 0;
1766}
1767
1768int bpf__strerror_apply_obj_config(int err, char *buf, size_t size)
1769{
1770 bpf__strerror_head(err, buf, size);
1771 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM,
1772 "Cannot set event to BPF map in multi-thread tracing");
1773 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH,
1774 "%s (Hint: use -i to turn off inherit)", emsg);
1775 bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE,
1776 "Can only put raw, hardware and BPF output event into a BPF map");
1777 bpf__strerror_end(buf, size);
1778 return 0;
1779}
1780
1781int bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused,
1782 int err, char *buf, size_t size)
1783{
1784 bpf__strerror_head(err, buf, size);
1785 bpf__strerror_end(buf, size);
1786 return 0;
1787}
1788