hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/tools/perf/util/bpf-loader.c
....@@ -12,33 +12,28 @@
1212 #include <linux/err.h>
1313 #include <linux/kernel.h>
1414 #include <linux/string.h>
15
+#include <linux/zalloc.h>
1516 #include <errno.h>
16
-#include "perf.h"
17
+#include <stdlib.h>
1718 #include "debug.h"
19
+#include "evlist.h"
1820 #include "bpf-loader.h"
1921 #include "bpf-prologue.h"
2022 #include "probe-event.h"
2123 #include "probe-finder.h" // for MAX_PROBES
2224 #include "parse-events.h"
2325 #include "strfilter.h"
26
+#include "util.h"
2427 #include "llvm-utils.h"
2528 #include "c++/clang-c.h"
2629
27
-#define DEFINE_PRINT_FN(name, level) \
28
-static int libbpf_##name(const char *fmt, ...) \
29
-{ \
30
- va_list args; \
31
- int ret; \
32
- \
33
- va_start(args, fmt); \
34
- ret = veprintf(level, verbose, pr_fmt(fmt), args);\
35
- va_end(args); \
36
- return ret; \
37
-}
30
+#include <internal/xyarray.h>
3831
39
-DEFINE_PRINT_FN(warning, 1)
40
-DEFINE_PRINT_FN(info, 1)
41
-DEFINE_PRINT_FN(debug, 1)
32
+static int libbpf_perf_print(enum libbpf_print_level level __attribute__((unused)),
33
+ const char *fmt, va_list args)
34
+{
35
+ return veprintf(1, verbose, pr_fmt(fmt), args);
36
+}
4237
4338 struct bpf_prog_priv {
4439 bool is_tp;
....@@ -59,9 +54,7 @@
5954 struct bpf_object *obj;
6055
6156 if (!libbpf_initialized) {
62
- libbpf_set_print(libbpf_warning,
63
- libbpf_info,
64
- libbpf_debug);
57
+ libbpf_set_print(libbpf_perf_print);
6558 libbpf_initialized = true;
6659 }
6760
....@@ -79,9 +72,7 @@
7972 struct bpf_object *obj;
8073
8174 if (!libbpf_initialized) {
82
- libbpf_set_print(libbpf_warning,
83
- libbpf_info,
84
- libbpf_debug);
75
+ libbpf_set_print(libbpf_perf_print);
8576 libbpf_initialized = true;
8677 }
8778
....@@ -99,7 +90,7 @@
9990 if (err)
10091 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
10192 } else
102
- pr_debug("bpf: successfull builtin compilation\n");
93
+ pr_debug("bpf: successful builtin compilation\n");
10394 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
10495
10596 if (!IS_ERR_OR_NULL(obj) && llvm_param.dump_obj)
....@@ -337,12 +328,6 @@
337328 probe_conf.no_inlines = false;
338329 probe_conf.force_add = false;
339330
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);
344
- }
345
-
346331 priv = calloc(sizeof(*priv), 1);
347332 if (!priv) {
348333 pr_debug("bpf: failed to alloc priv\n");
....@@ -350,6 +335,7 @@
350335 }
351336 pev = &priv->pev;
352337
338
+ config_str = bpf_program__section_name(prog);
353339 pr_debug("bpf: config program '%s'\n", config_str);
354340 err = parse_prog_config(config_str, &main_str, &is_tp, pev);
355341 if (err)
....@@ -463,10 +449,7 @@
463449 if (err) {
464450 const char *title;
465451
466
- title = bpf_program__title(prog, false);
467
- if (!title)
468
- title = "[unknown]";
469
-
452
+ title = bpf_program__section_name(prog);
470453 pr_debug("Failed to generate prologue for program %s\n",
471454 title);
472455 return err;
....@@ -775,7 +758,7 @@
775758
776759 if (priv->is_tp) {
777760 fd = bpf_program__fd(prog);
778
- err = (*func)(priv->sys_name, priv->evt_name, fd, arg);
761
+ err = (*func)(priv->sys_name, priv->evt_name, fd, obj, arg);
779762 if (err) {
780763 pr_debug("bpf: tracepoint call back failed, stop iterate\n");
781764 return err;
....@@ -800,7 +783,7 @@
800783 return fd;
801784 }
802785
803
- err = (*func)(tev->group, tev->event, fd, arg);
786
+ err = (*func)(tev->group, tev->event, fd, obj, arg);
804787 if (err) {
805788 pr_debug("bpf: call back failed, stop iterate\n");
806789 return err;
....@@ -829,7 +812,7 @@
829812 } k;
830813 union {
831814 u64 value;
832
- struct perf_evsel *evsel;
815
+ struct evsel *evsel;
833816 } v;
834817 };
835818
....@@ -841,7 +824,7 @@
841824 bpf_map_op__delete(struct bpf_map_op *op)
842825 {
843826 if (!list_empty(&op->list))
844
- list_del(&op->list);
827
+ list_del_init(&op->list);
845828 if (op->key_type == BPF_MAP_KEY_RANGES)
846829 parse_events__clear_array(&op->k.array);
847830 free(op);
....@@ -1055,7 +1038,7 @@
10551038 static int
10561039 bpf_map__config_value(struct bpf_map *map,
10571040 struct parse_events_term *term,
1058
- struct perf_evlist *evlist __maybe_unused)
1041
+ struct evlist *evlist __maybe_unused)
10591042 {
10601043 if (!term->err_val) {
10611044 pr_debug("Config value not set\n");
....@@ -1073,9 +1056,9 @@
10731056 static int
10741057 __bpf_map__config_event(struct bpf_map *map,
10751058 struct parse_events_term *term,
1076
- struct perf_evlist *evlist)
1059
+ struct evlist *evlist)
10771060 {
1078
- struct perf_evsel *evsel;
1061
+ struct evsel *evsel;
10791062 const struct bpf_map_def *def;
10801063 struct bpf_map_op *op;
10811064 const char *map_name = bpf_map__name(map);
....@@ -1115,7 +1098,7 @@
11151098 static int
11161099 bpf_map__config_event(struct bpf_map *map,
11171100 struct parse_events_term *term,
1118
- struct perf_evlist *evlist)
1101
+ struct evlist *evlist)
11191102 {
11201103 if (!term->err_val) {
11211104 pr_debug("Config value not set\n");
....@@ -1133,7 +1116,7 @@
11331116 struct bpf_obj_config__map_func {
11341117 const char *config_opt;
11351118 int (*config_func)(struct bpf_map *, struct parse_events_term *,
1136
- struct perf_evlist *);
1119
+ struct evlist *);
11371120 };
11381121
11391122 struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = {
....@@ -1181,7 +1164,7 @@
11811164 static int
11821165 bpf__obj_config_map(struct bpf_object *obj,
11831166 struct parse_events_term *term,
1184
- struct perf_evlist *evlist,
1167
+ struct evlist *evlist,
11851168 int *key_scan_pos)
11861169 {
11871170 /* key is "map:<mapname>.<config opt>" */
....@@ -1232,15 +1215,16 @@
12321215 pr_debug("ERROR: Invalid map config option '%s'\n", map_opt);
12331216 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT;
12341217 out:
1235
- free(map_name);
12361218 if (!err)
1237
- key_scan_pos += strlen(map_opt);
1219
+ *key_scan_pos += strlen(map_opt);
1220
+
1221
+ free(map_name);
12381222 return err;
12391223 }
12401224
12411225 int bpf__config_obj(struct bpf_object *obj,
12421226 struct parse_events_term *term,
1243
- struct perf_evlist *evlist,
1227
+ struct evlist *evlist,
12441228 int *error_pos)
12451229 {
12461230 int key_scan_pos = 0;
....@@ -1413,9 +1397,9 @@
14131397
14141398 static int
14151399 apply_config_evsel_for_key(const char *name, int map_fd, void *pkey,
1416
- struct perf_evsel *evsel)
1400
+ struct evsel *evsel)
14171401 {
1418
- struct xyarray *xy = evsel->fd;
1402
+ struct xyarray *xy = evsel->core.fd;
14191403 struct perf_event_attr *attr;
14201404 unsigned int key, events;
14211405 bool check_pass = false;
....@@ -1433,13 +1417,13 @@
14331417 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM;
14341418 }
14351419
1436
- attr = &evsel->attr;
1420
+ attr = &evsel->core.attr;
14371421 if (attr->inherit) {
14381422 pr_debug("ERROR: Can't put inherit event into map %s\n", name);
14391423 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH;
14401424 }
14411425
1442
- if (perf_evsel__is_bpf_output(evsel))
1426
+ if (evsel__is_bpf_output(evsel))
14431427 check_pass = true;
14441428 if (attr->type == PERF_TYPE_RAW)
14451429 check_pass = true;
....@@ -1503,7 +1487,7 @@
15031487 struct bpf_map *map;
15041488 int err;
15051489
1506
- bpf_map__for_each(map, obj) {
1490
+ bpf_object__for_each_map(map, obj) {
15071491 err = apply_obj_config_map(map);
15081492 if (err)
15091493 return err;
....@@ -1527,7 +1511,7 @@
15271511
15281512 #define bpf__for_each_map(pos, obj, objtmp) \
15291513 bpf_object__for_each_safe(obj, objtmp) \
1530
- bpf_map__for_each(pos, obj)
1514
+ bpf_object__for_each_map(pos, obj)
15311515
15321516 #define bpf__for_each_map_named(pos, obj, objtmp, name) \
15331517 bpf__for_each_map(pos, obj, objtmp) \
....@@ -1535,11 +1519,11 @@
15351519 (strcmp(name, \
15361520 bpf_map__name(pos)) == 0))
15371521
1538
-struct perf_evsel *bpf__setup_output_event(struct perf_evlist *evlist, const char *name)
1522
+struct evsel *bpf__setup_output_event(struct evlist *evlist, const char *name)
15391523 {
15401524 struct bpf_map_priv *tmpl_priv = NULL;
15411525 struct bpf_object *obj, *tmp;
1542
- struct perf_evsel *evsel = NULL;
1526
+ struct evsel *evsel = NULL;
15431527 struct bpf_map *map;
15441528 int err;
15451529 bool need_init = false;
....@@ -1577,7 +1561,7 @@
15771561 return ERR_PTR(-err);
15781562 }
15791563
1580
- evsel = perf_evlist__last(evlist);
1564
+ evsel = evlist__last(evlist);
15811565 }
15821566
15831567 bpf__for_each_map_named(map, obj, tmp, name) {
....@@ -1603,7 +1587,7 @@
16031587
16041588 op = bpf_map__add_newop(map, NULL);
16051589 if (IS_ERR(op))
1606
- return ERR_PTR(PTR_ERR(op));
1590
+ return ERR_CAST(op);
16071591 op->op_type = BPF_MAP_OP_SET_EVSEL;
16081592 op->v.evsel = evsel;
16091593 }
....@@ -1612,10 +1596,10 @@
16121596 return evsel;
16131597 }
16141598
1615
-int bpf__setup_stdout(struct perf_evlist *evlist)
1599
+int bpf__setup_stdout(struct evlist *evlist)
16161600 {
1617
- struct perf_evsel *evsel = bpf__setup_output_event(evlist, "__bpf_stdout__");
1618
- return IS_ERR(evsel) ? PTR_ERR(evsel) : 0;
1601
+ struct evsel *evsel = bpf__setup_output_event(evlist, "__bpf_stdout__");
1602
+ return PTR_ERR_OR_ZERO(evsel);
16191603 }
16201604
16211605 #define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
....@@ -1768,7 +1752,7 @@
17681752
17691753 int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
17701754 struct parse_events_term *term __maybe_unused,
1771
- struct perf_evlist *evlist __maybe_unused,
1755
+ struct evlist *evlist __maybe_unused,
17721756 int *error_pos __maybe_unused, int err,
17731757 char *buf, size_t size)
17741758 {
....@@ -1792,7 +1776,7 @@
17921776 return 0;
17931777 }
17941778
1795
-int bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused,
1779
+int bpf__strerror_setup_output_event(struct evlist *evlist __maybe_unused,
17961780 int err, char *buf, size_t size)
17971781 {
17981782 bpf__strerror_head(err, buf, size);