blob: f9ae1a993806bff475fef7b0558f85f710d36a82 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Wang Nan69d262a2015-10-14 12:41:13 +00002/*
3 * bpf-loader.c
4 *
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
7 */
8
Wang Nana08357d2015-11-16 12:10:13 +00009#include <linux/bpf.h>
Wang Nan69d262a2015-10-14 12:41:13 +000010#include <bpf/libbpf.h>
Wang Nan8690a2a2016-02-22 09:10:32 +000011#include <bpf/bpf.h>
Wang Nan69d262a2015-10-14 12:41:13 +000012#include <linux/err.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030013#include <linux/kernel.h>
Wang Nan03e01f52015-11-16 12:10:08 +000014#include <linux/string.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030015#include <errno.h>
Wang Nan69d262a2015-10-14 12:41:13 +000016#include "perf.h"
17#include "debug.h"
18#include "bpf-loader.h"
Wang Nana08357d2015-11-16 12:10:13 +000019#include "bpf-prologue.h"
Wang Nanaa3abf32015-10-14 12:41:15 +000020#include "probe-event.h"
21#include "probe-finder.h" // for MAX_PROBES
Wang Nan2d055bf2016-02-22 09:10:34 +000022#include "parse-events.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030023#include "strfilter.h"
Wang Nand509db02015-10-14 12:41:20 +000024#include "llvm-utils.h"
Wang Nanedd695b2016-11-26 07:03:39 +000025#include "c++/clang-c.h"
Wang Nan69d262a2015-10-14 12:41:13 +000026
27#define DEFINE_PRINT_FN(name, level) \
28static 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}
38
Wang Nan7a011942015-11-03 10:44:43 +000039DEFINE_PRINT_FN(warning, 1)
40DEFINE_PRINT_FN(info, 1)
Wang Nan69d262a2015-10-14 12:41:13 +000041DEFINE_PRINT_FN(debug, 1)
42
Wang Nanaa3abf32015-10-14 12:41:15 +000043struct bpf_prog_priv {
Wang Nanb4ee6d42016-07-13 10:44:05 +000044 bool is_tp;
45 char *sys_name;
46 char *evt_name;
Wang Nanaa3abf32015-10-14 12:41:15 +000047 struct perf_probe_event pev;
Wang Nana08357d2015-11-16 12:10:13 +000048 bool need_prologue;
49 struct bpf_insn *insns_buf;
Wang Nand35b3282015-11-17 08:32:47 +000050 int nr_types;
51 int *type_mapping;
Wang Nanaa3abf32015-10-14 12:41:15 +000052};
53
Wang Nanba1fae42015-11-06 13:49:43 +000054static bool libbpf_initialized;
55
56struct bpf_object *
57bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
58{
59 struct bpf_object *obj;
60
61 if (!libbpf_initialized) {
62 libbpf_set_print(libbpf_warning,
63 libbpf_info,
64 libbpf_debug);
65 libbpf_initialized = true;
66 }
67
68 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
YueHaibingab4e32f2018-05-11 19:21:42 +080069 if (IS_ERR_OR_NULL(obj)) {
Wang Nanba1fae42015-11-06 13:49:43 +000070 pr_debug("bpf: failed to load buffer\n");
71 return ERR_PTR(-EINVAL);
72 }
73
74 return obj;
75}
76
Wang Nand509db02015-10-14 12:41:20 +000077struct bpf_object *bpf__prepare_load(const char *filename, bool source)
Wang Nan69d262a2015-10-14 12:41:13 +000078{
79 struct bpf_object *obj;
Wang Nan69d262a2015-10-14 12:41:13 +000080
81 if (!libbpf_initialized) {
82 libbpf_set_print(libbpf_warning,
83 libbpf_info,
84 libbpf_debug);
85 libbpf_initialized = true;
86 }
87
Wang Nand509db02015-10-14 12:41:20 +000088 if (source) {
89 int err;
90 void *obj_buf;
91 size_t obj_buf_sz;
92
Wang Nanedd695b2016-11-26 07:03:39 +000093 perf_clang__init();
94 err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
95 perf_clang__cleanup();
96 if (err) {
Arnaldo Carvalho de Melo87252322018-01-18 13:07:00 -030097 pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err);
Wang Nanedd695b2016-11-26 07:03:39 +000098 err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
99 if (err)
100 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
101 } else
102 pr_debug("bpf: successfull builtin compilation\n");
Wang Nand509db02015-10-14 12:41:20 +0000103 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
Wang Nan2bd42de2016-11-26 07:03:30 +0000104
YueHaibingab4e32f2018-05-11 19:21:42 +0800105 if (!IS_ERR_OR_NULL(obj) && llvm_param.dump_obj)
Wang Nan2bd42de2016-11-26 07:03:30 +0000106 llvm__dump_obj(filename, obj_buf, obj_buf_sz);
107
Wang Nand509db02015-10-14 12:41:20 +0000108 free(obj_buf);
109 } else
110 obj = bpf_object__open(filename);
111
YueHaibingab4e32f2018-05-11 19:21:42 +0800112 if (IS_ERR_OR_NULL(obj)) {
Wang Nan69d262a2015-10-14 12:41:13 +0000113 pr_debug("bpf: failed to load %s\n", filename);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000114 return obj;
Wang Nan69d262a2015-10-14 12:41:13 +0000115 }
116
117 return obj;
118}
119
120void bpf__clear(void)
121{
122 struct bpf_object *obj, *tmp;
123
Wang Nanaa3abf32015-10-14 12:41:15 +0000124 bpf_object__for_each_safe(obj, tmp) {
125 bpf__unprobe(obj);
Wang Nan69d262a2015-10-14 12:41:13 +0000126 bpf_object__close(obj);
Wang Nanaa3abf32015-10-14 12:41:15 +0000127 }
128}
129
130static void
Wang Nan80cdce72016-02-19 11:43:51 +0000131clear_prog_priv(struct bpf_program *prog __maybe_unused,
132 void *_priv)
Wang Nanaa3abf32015-10-14 12:41:15 +0000133{
134 struct bpf_prog_priv *priv = _priv;
135
136 cleanup_perf_probe_events(&priv->pev, 1);
Wang Nana08357d2015-11-16 12:10:13 +0000137 zfree(&priv->insns_buf);
Wang Nand35b3282015-11-17 08:32:47 +0000138 zfree(&priv->type_mapping);
Wang Nanb4ee6d42016-07-13 10:44:05 +0000139 zfree(&priv->sys_name);
140 zfree(&priv->evt_name);
Wang Nanaa3abf32015-10-14 12:41:15 +0000141 free(priv);
142}
143
144static int
Wang Nan0bb93492015-11-27 08:47:37 +0000145prog_config__exec(const char *value, struct perf_probe_event *pev)
Wang Nan361f2b12015-11-16 12:10:05 +0000146{
147 pev->uprobes = true;
148 pev->target = strdup(value);
149 if (!pev->target)
150 return -ENOMEM;
151 return 0;
152}
153
Wang Nan5dbd16c2015-11-16 12:10:06 +0000154static int
Wang Nan0bb93492015-11-27 08:47:37 +0000155prog_config__module(const char *value, struct perf_probe_event *pev)
Wang Nan5dbd16c2015-11-16 12:10:06 +0000156{
157 pev->uprobes = false;
158 pev->target = strdup(value);
159 if (!pev->target)
160 return -ENOMEM;
161 return 0;
162}
163
Wang Nan03e01f52015-11-16 12:10:08 +0000164static int
Wang Nan0bb93492015-11-27 08:47:37 +0000165prog_config__bool(const char *value, bool *pbool, bool invert)
Wang Nan03e01f52015-11-16 12:10:08 +0000166{
167 int err;
168 bool bool_value;
169
170 if (!pbool)
171 return -EINVAL;
172
173 err = strtobool(value, &bool_value);
174 if (err)
175 return err;
176
177 *pbool = invert ? !bool_value : bool_value;
178 return 0;
179}
180
181static int
Wang Nan0bb93492015-11-27 08:47:37 +0000182prog_config__inlines(const char *value,
183 struct perf_probe_event *pev __maybe_unused)
Wang Nan03e01f52015-11-16 12:10:08 +0000184{
Wang Nan0bb93492015-11-27 08:47:37 +0000185 return prog_config__bool(value, &probe_conf.no_inlines, true);
Wang Nan03e01f52015-11-16 12:10:08 +0000186}
187
188static int
Wang Nan0bb93492015-11-27 08:47:37 +0000189prog_config__force(const char *value,
190 struct perf_probe_event *pev __maybe_unused)
Wang Nan03e01f52015-11-16 12:10:08 +0000191{
Wang Nan0bb93492015-11-27 08:47:37 +0000192 return prog_config__bool(value, &probe_conf.force_add, false);
Wang Nan03e01f52015-11-16 12:10:08 +0000193}
194
Wang Nan361f2b12015-11-16 12:10:05 +0000195static struct {
196 const char *key;
197 const char *usage;
198 const char *desc;
199 int (*func)(const char *, struct perf_probe_event *);
Wang Nan0bb93492015-11-27 08:47:37 +0000200} bpf_prog_config_terms[] = {
Wang Nan361f2b12015-11-16 12:10:05 +0000201 {
202 .key = "exec",
203 .usage = "exec=<full path of file>",
204 .desc = "Set uprobe target",
Wang Nan0bb93492015-11-27 08:47:37 +0000205 .func = prog_config__exec,
Wang Nan361f2b12015-11-16 12:10:05 +0000206 },
Wang Nan5dbd16c2015-11-16 12:10:06 +0000207 {
208 .key = "module",
209 .usage = "module=<module name> ",
210 .desc = "Set kprobe module",
Wang Nan0bb93492015-11-27 08:47:37 +0000211 .func = prog_config__module,
Wang Nan03e01f52015-11-16 12:10:08 +0000212 },
213 {
214 .key = "inlines",
215 .usage = "inlines=[yes|no] ",
216 .desc = "Probe at inline symbol",
Wang Nan0bb93492015-11-27 08:47:37 +0000217 .func = prog_config__inlines,
Wang Nan03e01f52015-11-16 12:10:08 +0000218 },
219 {
220 .key = "force",
221 .usage = "force=[yes|no] ",
222 .desc = "Forcibly add events with existing name",
Wang Nan0bb93492015-11-27 08:47:37 +0000223 .func = prog_config__force,
Wang Nan03e01f52015-11-16 12:10:08 +0000224 },
Wang Nan361f2b12015-11-16 12:10:05 +0000225};
226
227static int
Wang Nan0bb93492015-11-27 08:47:37 +0000228do_prog_config(const char *key, const char *value,
229 struct perf_probe_event *pev)
Wang Nan361f2b12015-11-16 12:10:05 +0000230{
231 unsigned int i;
232
233 pr_debug("config bpf program: %s=%s\n", key, value);
Wang Nan0bb93492015-11-27 08:47:37 +0000234 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);
Wang Nan361f2b12015-11-16 12:10:05 +0000237
Wang Nan0bb93492015-11-27 08:47:37 +0000238 pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
Wang Nan361f2b12015-11-16 12:10:05 +0000239 key, value);
240
Wang Nan0bb93492015-11-27 08:47:37 +0000241 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);
Wang Nan361f2b12015-11-16 12:10:05 +0000245 pr_debug("\n");
246
Wang Nan0bb93492015-11-27 08:47:37 +0000247 return -BPF_LOADER_ERRNO__PROGCONF_TERM;
Wang Nan361f2b12015-11-16 12:10:05 +0000248}
249
250static const char *
Wang Nan0bb93492015-11-27 08:47:37 +0000251parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev)
Wang Nan361f2b12015-11-16 12:10:05 +0000252{
253 char *text = strdup(config_str);
254 char *sep, *line;
255 const char *main_str = NULL;
256 int err = 0;
257
258 if (!text) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200259 pr_debug("Not enough memory: dup config_str failed\n");
Wang Nan361f2b12015-11-16 12:10:05 +0000260 return ERR_PTR(-ENOMEM);
261 }
262
263 line = text;
264 while ((sep = strchr(line, ';'))) {
265 char *equ;
266
267 *sep = '\0';
268 equ = strchr(line, '=');
269 if (!equ) {
270 pr_warning("WARNING: invalid config in BPF object: %s\n",
271 line);
272 pr_warning("\tShould be 'key=value'.\n");
273 goto nextline;
274 }
275 *equ = '\0';
276
Wang Nan0bb93492015-11-27 08:47:37 +0000277 err = do_prog_config(line, equ + 1, pev);
Wang Nan361f2b12015-11-16 12:10:05 +0000278 if (err)
279 break;
280nextline:
281 line = sep + 1;
282 }
283
284 if (!err)
285 main_str = config_str + (line - text);
286 free(text);
287
288 return err ? ERR_PTR(err) : main_str;
289}
290
291static int
Wang Nanb4ee6d42016-07-13 10:44:05 +0000292parse_prog_config(const char *config_str, const char **p_main_str,
293 bool *is_tp, struct perf_probe_event *pev)
Wang Nan361f2b12015-11-16 12:10:05 +0000294{
295 int err;
Wang Nan0bb93492015-11-27 08:47:37 +0000296 const char *main_str = parse_prog_config_kvpair(config_str, pev);
Wang Nan361f2b12015-11-16 12:10:05 +0000297
298 if (IS_ERR(main_str))
299 return PTR_ERR(main_str);
300
Wang Nanb4ee6d42016-07-13 10:44:05 +0000301 *p_main_str = main_str;
302 if (!strchr(main_str, '=')) {
303 /* Is a tracepoint event? */
304 const char *s = strchr(main_str, ':');
305
306 if (!s) {
307 pr_debug("bpf: '%s' is not a valid tracepoint\n",
308 config_str);
309 return -BPF_LOADER_ERRNO__CONFIG;
310 }
311
312 *is_tp = true;
313 return 0;
314 }
315
316 *is_tp = false;
Wang Nan361f2b12015-11-16 12:10:05 +0000317 err = parse_perf_probe_command(main_str, pev);
318 if (err < 0) {
319 pr_debug("bpf: '%s' is not a valid config string\n",
320 config_str);
321 /* parse failed, don't need clear pev. */
322 return -BPF_LOADER_ERRNO__CONFIG;
323 }
324 return 0;
325}
326
327static int
Wang Nanaa3abf32015-10-14 12:41:15 +0000328config_bpf_program(struct bpf_program *prog)
329{
330 struct perf_probe_event *pev = NULL;
331 struct bpf_prog_priv *priv = NULL;
Wang Nanb4ee6d42016-07-13 10:44:05 +0000332 const char *config_str, *main_str;
333 bool is_tp = false;
Wang Nanaa3abf32015-10-14 12:41:15 +0000334 int err;
335
Wang Nan03e01f52015-11-16 12:10:08 +0000336 /* Initialize per-program probing setting */
337 probe_conf.no_inlines = false;
338 probe_conf.force_add = false;
339
Wang Nanaa3abf32015-10-14 12:41:15 +0000340 config_str = bpf_program__title(prog, false);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000341 if (IS_ERR(config_str)) {
Wang Nanaa3abf32015-10-14 12:41:15 +0000342 pr_debug("bpf: unable to get title for program\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000343 return PTR_ERR(config_str);
Wang Nanaa3abf32015-10-14 12:41:15 +0000344 }
345
346 priv = calloc(sizeof(*priv), 1);
347 if (!priv) {
348 pr_debug("bpf: failed to alloc priv\n");
349 return -ENOMEM;
350 }
351 pev = &priv->pev;
352
353 pr_debug("bpf: config program '%s'\n", config_str);
Wang Nanb4ee6d42016-07-13 10:44:05 +0000354 err = parse_prog_config(config_str, &main_str, &is_tp, pev);
Wang Nan361f2b12015-11-16 12:10:05 +0000355 if (err)
Wang Nanaa3abf32015-10-14 12:41:15 +0000356 goto errout;
Wang Nanaa3abf32015-10-14 12:41:15 +0000357
Wang Nanb4ee6d42016-07-13 10:44:05 +0000358 if (is_tp) {
359 char *s = strchr(main_str, ':');
360
361 priv->is_tp = true;
362 priv->sys_name = strndup(main_str, s - main_str);
363 priv->evt_name = strdup(s + 1);
364 goto set_priv;
365 }
366
Wang Nanaa3abf32015-10-14 12:41:15 +0000367 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);
Wang Nand3e0ce32015-11-06 13:58:09 +0000370 err = -BPF_LOADER_ERRNO__GROUP;
Wang Nanaa3abf32015-10-14 12:41:15 +0000371 goto errout;
372 } else if (!pev->group)
373 pev->group = strdup(PERF_BPF_PROBE_GROUP);
374
375 if (!pev->group) {
376 pr_debug("bpf: strdup failed\n");
377 err = -ENOMEM;
378 goto errout;
379 }
380
381 if (!pev->event) {
Wang Nand3e0ce32015-11-06 13:58:09 +0000382 pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
Wang Nanaa3abf32015-10-14 12:41:15 +0000383 config_str);
Wang Nand3e0ce32015-11-06 13:58:09 +0000384 err = -BPF_LOADER_ERRNO__EVENTNAME;
Wang Nanaa3abf32015-10-14 12:41:15 +0000385 goto errout;
386 }
387 pr_debug("bpf: config '%s' is ok\n", config_str);
388
Wang Nanb4ee6d42016-07-13 10:44:05 +0000389set_priv:
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -0300390 err = bpf_program__set_priv(prog, priv, clear_prog_priv);
Wang Nanaa3abf32015-10-14 12:41:15 +0000391 if (err) {
392 pr_debug("Failed to set priv for program '%s'\n", config_str);
393 goto errout;
394 }
395
396 return 0;
397
398errout:
399 if (pev)
400 clear_perf_probe_event(pev);
401 free(priv);
402 return err;
403}
404
405static int bpf__prepare_probe(void)
406{
407 static int err = 0;
408 static bool initialized = false;
409
410 /*
411 * Make err static, so if init failed the first, bpf__prepare_probe()
412 * fails each time without calling init_probe_symbol_maps multiple
413 * times.
414 */
415 if (initialized)
416 return err;
417
418 initialized = true;
419 err = init_probe_symbol_maps(false);
420 if (err < 0)
421 pr_debug("Failed to init_probe_symbol_maps\n");
422 probe_conf.max_probes = MAX_PROBES;
423 return err;
424}
425
Wang Nana08357d2015-11-16 12:10:13 +0000426static int
427preproc_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)
430{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300431 struct bpf_prog_priv *priv = bpf_program__priv(prog);
Wang Nana08357d2015-11-16 12:10:13 +0000432 struct probe_trace_event *tev;
433 struct perf_probe_event *pev;
Wang Nana08357d2015-11-16 12:10:13 +0000434 struct bpf_insn *buf;
435 size_t prologue_cnt = 0;
Wang Nand35b3282015-11-17 08:32:47 +0000436 int i, err;
Wang Nana08357d2015-11-16 12:10:13 +0000437
Wang Nanb4ee6d42016-07-13 10:44:05 +0000438 if (IS_ERR(priv) || !priv || priv->is_tp)
Wang Nana08357d2015-11-16 12:10:13 +0000439 goto errout;
440
441 pev = &priv->pev;
442
Wang Nand35b3282015-11-17 08:32:47 +0000443 if (n < 0 || n >= priv->nr_types)
Wang Nana08357d2015-11-16 12:10:13 +0000444 goto errout;
445
Wang Nand35b3282015-11-17 08:32:47 +0000446 /* Find a tev belongs to that type */
447 for (i = 0; i < pev->ntevs; i++) {
448 if (priv->type_mapping[i] == n)
449 break;
450 }
451
452 if (i >= pev->ntevs) {
453 pr_debug("Internal error: prologue type %d not found\n", n);
454 return -BPF_LOADER_ERRNO__PROLOGUE;
455 }
456
457 tev = &pev->tevs[i];
Wang Nana08357d2015-11-16 12:10:13 +0000458
459 buf = priv->insns_buf;
460 err = bpf__gen_prologue(tev->args, tev->nargs,
461 buf, &prologue_cnt,
462 BPF_MAXINSNS - orig_insns_cnt);
463 if (err) {
464 const char *title;
465
466 title = bpf_program__title(prog, false);
467 if (!title)
468 title = "[unknown]";
469
470 pr_debug("Failed to generate prologue for program %s\n",
471 title);
472 return err;
473 }
474
475 memcpy(&buf[prologue_cnt], orig_insns,
476 sizeof(struct bpf_insn) * orig_insns_cnt);
477
478 res->new_insn_ptr = buf;
479 res->new_insn_cnt = prologue_cnt + orig_insns_cnt;
480 res->pfd = NULL;
481 return 0;
482
483errout:
484 pr_debug("Internal error in preproc_gen_prologue\n");
485 return -BPF_LOADER_ERRNO__PROLOGUE;
486}
487
Wang Nand35b3282015-11-17 08:32:47 +0000488/*
489 * compare_tev_args is reflexive, transitive and antisymmetric.
490 * I can proof it but this margin is too narrow to contain.
491 */
492static int compare_tev_args(const void *ptev1, const void *ptev2)
493{
494 int i, ret;
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;
499
500 ret = tev2->nargs - tev1->nargs;
501 if (ret)
502 return ret;
503
504 for (i = 0; i < tev1->nargs; i++) {
505 struct probe_trace_arg *arg1, *arg2;
506 struct probe_trace_arg_ref *ref1, *ref2;
507
508 arg1 = &tev1->args[i];
509 arg2 = &tev2->args[i];
510
511 ret = strcmp(arg1->value, arg2->value);
512 if (ret)
513 return ret;
514
515 ref1 = arg1->ref;
516 ref2 = arg2->ref;
517
518 while (ref1 && ref2) {
519 ret = ref2->offset - ref1->offset;
520 if (ret)
521 return ret;
522
523 ref1 = ref1->next;
524 ref2 = ref2->next;
525 }
526
527 if (ref1 || ref2)
528 return ref2 ? 1 : -1;
529 }
530
531 return 0;
532}
533
534/*
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.
538 */
539static int map_prologue(struct perf_probe_event *pev, int *mapping,
540 int *nr_types)
541{
542 int i, type = 0;
543 struct probe_trace_event **ptevs;
544
545 size_t array_sz = sizeof(*ptevs) * pev->ntevs;
546
547 ptevs = malloc(array_sz);
548 if (!ptevs) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200549 pr_debug("Not enough memory: alloc ptevs failed\n");
Wang Nand35b3282015-11-17 08:32:47 +0000550 return -ENOMEM;
551 }
552
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];
556
557 qsort(ptevs, pev->ntevs, sizeof(*ptevs),
558 compare_tev_args);
559
560 for (i = 0; i < pev->ntevs; i++) {
561 int n;
562
563 n = ptevs[i] - pev->tevs;
564 if (i == 0) {
565 mapping[n] = type;
566 pr_debug("mapping[%d]=%d\n", n, type);
567 continue;
568 }
569
570 if (compare_tev_args(ptevs + i, ptevs + i - 1) == 0)
571 mapping[n] = type;
572 else
573 mapping[n] = ++type;
574
575 pr_debug("mapping[%d]=%d\n", n, mapping[n]);
576 }
577 free(ptevs);
578 *nr_types = type + 1;
579
580 return 0;
581}
582
Wang Nana08357d2015-11-16 12:10:13 +0000583static int hook_load_preprocessor(struct bpf_program *prog)
584{
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300585 struct bpf_prog_priv *priv = bpf_program__priv(prog);
Wang Nana08357d2015-11-16 12:10:13 +0000586 struct perf_probe_event *pev;
Wang Nana08357d2015-11-16 12:10:13 +0000587 bool need_prologue = false;
588 int err, i;
589
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300590 if (IS_ERR(priv) || !priv) {
Wang Nana08357d2015-11-16 12:10:13 +0000591 pr_debug("Internal error when hook preprocessor\n");
592 return -BPF_LOADER_ERRNO__INTERNAL;
593 }
594
Wang Nanb4ee6d42016-07-13 10:44:05 +0000595 if (priv->is_tp) {
596 priv->need_prologue = false;
597 return 0;
598 }
599
Wang Nana08357d2015-11-16 12:10:13 +0000600 pev = &priv->pev;
601 for (i = 0; i < pev->ntevs; i++) {
602 struct probe_trace_event *tev = &pev->tevs[i];
603
604 if (tev->nargs > 0) {
605 need_prologue = true;
606 break;
607 }
608 }
609
610 /*
611 * Since all tevs don't have argument, we don't need generate
612 * prologue.
613 */
614 if (!need_prologue) {
615 priv->need_prologue = false;
616 return 0;
617 }
618
619 priv->need_prologue = true;
620 priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS);
621 if (!priv->insns_buf) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200622 pr_debug("Not enough memory: alloc insns_buf failed\n");
Wang Nana08357d2015-11-16 12:10:13 +0000623 return -ENOMEM;
624 }
625
Wang Nand35b3282015-11-17 08:32:47 +0000626 priv->type_mapping = malloc(sizeof(int) * pev->ntevs);
627 if (!priv->type_mapping) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200628 pr_debug("Not enough memory: alloc type_mapping failed\n");
Wang Nand35b3282015-11-17 08:32:47 +0000629 return -ENOMEM;
630 }
631 memset(priv->type_mapping, -1,
632 sizeof(int) * pev->ntevs);
633
634 err = map_prologue(pev, priv->type_mapping, &priv->nr_types);
635 if (err)
636 return err;
637
638 err = bpf_program__set_prep(prog, priv->nr_types,
Wang Nana08357d2015-11-16 12:10:13 +0000639 preproc_gen_prologue);
640 return err;
641}
642
Wang Nanaa3abf32015-10-14 12:41:15 +0000643int bpf__probe(struct bpf_object *obj)
644{
645 int err = 0;
646 struct bpf_program *prog;
647 struct bpf_prog_priv *priv;
648 struct perf_probe_event *pev;
649
650 err = bpf__prepare_probe();
651 if (err) {
652 pr_debug("bpf__prepare_probe failed\n");
653 return err;
654 }
655
656 bpf_object__for_each_program(prog, obj) {
657 err = config_bpf_program(prog);
658 if (err)
659 goto out;
660
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300661 priv = bpf_program__priv(prog);
662 if (IS_ERR(priv) || !priv) {
663 err = PTR_ERR(priv);
Wang Nanaa3abf32015-10-14 12:41:15 +0000664 goto out;
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300665 }
Wang Nanb4ee6d42016-07-13 10:44:05 +0000666
667 if (priv->is_tp) {
668 bpf_program__set_tracepoint(prog);
669 continue;
670 }
671
672 bpf_program__set_kprobe(prog);
Wang Nanaa3abf32015-10-14 12:41:15 +0000673 pev = &priv->pev;
674
675 err = convert_perf_probe_events(pev, 1);
676 if (err < 0) {
He Kuang4d416432017-02-07 07:34:12 +0000677 pr_debug("bpf_probe: failed to convert perf probe events\n");
Wang Nanaa3abf32015-10-14 12:41:15 +0000678 goto out;
679 }
680
681 err = apply_perf_probe_events(pev, 1);
682 if (err < 0) {
He Kuang4d416432017-02-07 07:34:12 +0000683 pr_debug("bpf_probe: failed to apply perf probe events\n");
Wang Nanaa3abf32015-10-14 12:41:15 +0000684 goto out;
685 }
Wang Nana08357d2015-11-16 12:10:13 +0000686
687 /*
688 * After probing, let's consider prologue, which
689 * adds program fetcher to BPF programs.
690 *
691 * hook_load_preprocessorr() hooks pre-processor
692 * to bpf_program, let it generate prologue
693 * dynamically during loading.
694 */
695 err = hook_load_preprocessor(prog);
696 if (err)
697 goto out;
Wang Nanaa3abf32015-10-14 12:41:15 +0000698 }
699out:
700 return err < 0 ? err : 0;
701}
702
703#define EVENTS_WRITE_BUFSIZE 4096
704int bpf__unprobe(struct bpf_object *obj)
705{
706 int err, ret = 0;
707 struct bpf_program *prog;
Wang Nanaa3abf32015-10-14 12:41:15 +0000708
709 bpf_object__for_each_program(prog, obj) {
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300710 struct bpf_prog_priv *priv = bpf_program__priv(prog);
Wang Nanaa3abf32015-10-14 12:41:15 +0000711 int i;
712
Wang Nanb4ee6d42016-07-13 10:44:05 +0000713 if (IS_ERR(priv) || !priv || priv->is_tp)
Wang Nanaa3abf32015-10-14 12:41:15 +0000714 continue;
715
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;
720
721 snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
722 "%s:%s", tev->group, tev->event);
723 name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
724
725 delfilter = strfilter__new(name_buf, NULL);
726 if (!delfilter) {
727 pr_debug("Failed to create filter for unprobing\n");
728 ret = -ENOMEM;
729 continue;
730 }
731
732 err = del_perf_probe_events(delfilter);
733 strfilter__delete(delfilter);
734 if (err) {
735 pr_debug("Failed to delete %s\n", name_buf);
736 ret = err;
737 continue;
738 }
739 }
740 }
741 return ret;
742}
743
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000744int bpf__load(struct bpf_object *obj)
745{
746 int err;
747
748 err = bpf_object__load(obj);
749 if (err) {
Arnaldo Carvalho de Melo739e2ed2018-07-31 11:58:57 -0300750 char bf[128];
751 libbpf_strerror(err, bf, sizeof(bf));
752 pr_debug("bpf: load objects failed: err=%d: (%s)\n", err, bf);
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000753 return err;
754 }
755 return 0;
756}
757
Wang Nancd102d72016-07-13 10:44:04 +0000758int bpf__foreach_event(struct bpf_object *obj,
759 bpf_prog_iter_callback_t func,
760 void *arg)
Wang Nan4edf30e2015-10-14 12:41:17 +0000761{
762 struct bpf_program *prog;
763 int err;
764
765 bpf_object__for_each_program(prog, obj) {
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300766 struct bpf_prog_priv *priv = bpf_program__priv(prog);
Wang Nan4edf30e2015-10-14 12:41:17 +0000767 struct probe_trace_event *tev;
768 struct perf_probe_event *pev;
Wang Nan4edf30e2015-10-14 12:41:17 +0000769 int i, fd;
770
Arnaldo Carvalho de Melobe834ff2016-06-03 12:36:39 -0300771 if (IS_ERR(priv) || !priv) {
Wang Nan4edf30e2015-10-14 12:41:17 +0000772 pr_debug("bpf: failed to get private field\n");
Wang Nand3e0ce32015-11-06 13:58:09 +0000773 return -BPF_LOADER_ERRNO__INTERNAL;
Wang Nan4edf30e2015-10-14 12:41:17 +0000774 }
775
Wang Nanb4ee6d42016-07-13 10:44:05 +0000776 if (priv->is_tp) {
777 fd = bpf_program__fd(prog);
778 err = (*func)(priv->sys_name, priv->evt_name, fd, arg);
779 if (err) {
780 pr_debug("bpf: tracepoint call back failed, stop iterate\n");
781 return err;
782 }
783 continue;
784 }
785
Wang Nan4edf30e2015-10-14 12:41:17 +0000786 pev = &priv->pev;
787 for (i = 0; i < pev->ntevs; i++) {
788 tev = &pev->tevs[i];
789
Wang Nand35b3282015-11-17 08:32:47 +0000790 if (priv->need_prologue) {
791 int type = priv->type_mapping[i];
792
793 fd = bpf_program__nth_fd(prog, type);
794 } else {
Wang Nana08357d2015-11-16 12:10:13 +0000795 fd = bpf_program__fd(prog);
Wang Nand35b3282015-11-17 08:32:47 +0000796 }
Wang Nana08357d2015-11-16 12:10:13 +0000797
Wang Nan4edf30e2015-10-14 12:41:17 +0000798 if (fd < 0) {
799 pr_debug("bpf: failed to get file descriptor\n");
800 return fd;
801 }
802
Wang Nancd102d72016-07-13 10:44:04 +0000803 err = (*func)(tev->group, tev->event, fd, arg);
Wang Nan4edf30e2015-10-14 12:41:17 +0000804 if (err) {
805 pr_debug("bpf: call back failed, stop iterate\n");
806 return err;
807 }
808 }
809 }
810 return 0;
811}
812
Wang Nan066dacb2016-02-22 09:10:30 +0000813enum bpf_map_op_type {
814 BPF_MAP_OP_SET_VALUE,
Wang Nan7630b3e2016-02-22 09:10:33 +0000815 BPF_MAP_OP_SET_EVSEL,
Wang Nan066dacb2016-02-22 09:10:30 +0000816};
817
818enum bpf_map_key_type {
819 BPF_MAP_KEY_ALL,
Wang Nan2d055bf2016-02-22 09:10:34 +0000820 BPF_MAP_KEY_RANGES,
Wang Nan066dacb2016-02-22 09:10:30 +0000821};
822
823struct bpf_map_op {
824 struct list_head list;
825 enum bpf_map_op_type op_type;
826 enum bpf_map_key_type key_type;
827 union {
Wang Nan2d055bf2016-02-22 09:10:34 +0000828 struct parse_events_array array;
829 } k;
830 union {
Wang Nan066dacb2016-02-22 09:10:30 +0000831 u64 value;
Wang Nan7630b3e2016-02-22 09:10:33 +0000832 struct perf_evsel *evsel;
Wang Nan066dacb2016-02-22 09:10:30 +0000833 } v;
834};
835
836struct bpf_map_priv {
837 struct list_head ops_list;
838};
839
840static void
841bpf_map_op__delete(struct bpf_map_op *op)
842{
843 if (!list_empty(&op->list))
844 list_del(&op->list);
Wang Nan2d055bf2016-02-22 09:10:34 +0000845 if (op->key_type == BPF_MAP_KEY_RANGES)
846 parse_events__clear_array(&op->k.array);
Wang Nan066dacb2016-02-22 09:10:30 +0000847 free(op);
848}
849
850static void
851bpf_map_priv__purge(struct bpf_map_priv *priv)
852{
853 struct bpf_map_op *pos, *n;
854
855 list_for_each_entry_safe(pos, n, &priv->ops_list, list) {
856 list_del_init(&pos->list);
857 bpf_map_op__delete(pos);
858 }
859}
860
861static void
862bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
863 void *_priv)
864{
865 struct bpf_map_priv *priv = _priv;
866
867 bpf_map_priv__purge(priv);
868 free(priv);
869}
870
Wang Nan2d055bf2016-02-22 09:10:34 +0000871static int
872bpf_map_op_setkey(struct bpf_map_op *op, struct parse_events_term *term)
873{
874 op->key_type = BPF_MAP_KEY_ALL;
875 if (!term)
876 return 0;
877
878 if (term->array.nr_ranges) {
879 size_t memsz = term->array.nr_ranges *
880 sizeof(op->k.array.ranges[0]);
881
882 op->k.array.ranges = memdup(term->array.ranges, memsz);
883 if (!op->k.array.ranges) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200884 pr_debug("Not enough memory to alloc indices for map\n");
Wang Nan2d055bf2016-02-22 09:10:34 +0000885 return -ENOMEM;
886 }
887 op->key_type = BPF_MAP_KEY_RANGES;
888 op->k.array.nr_ranges = term->array.nr_ranges;
889 }
890 return 0;
891}
892
Wang Nan066dacb2016-02-22 09:10:30 +0000893static struct bpf_map_op *
Wang Nan2d055bf2016-02-22 09:10:34 +0000894bpf_map_op__new(struct parse_events_term *term)
Wang Nan066dacb2016-02-22 09:10:30 +0000895{
896 struct bpf_map_op *op;
Wang Nan2d055bf2016-02-22 09:10:34 +0000897 int err;
Wang Nan066dacb2016-02-22 09:10:30 +0000898
899 op = zalloc(sizeof(*op));
900 if (!op) {
901 pr_debug("Failed to alloc bpf_map_op\n");
902 return ERR_PTR(-ENOMEM);
903 }
904 INIT_LIST_HEAD(&op->list);
905
Wang Nan2d055bf2016-02-22 09:10:34 +0000906 err = bpf_map_op_setkey(op, term);
907 if (err) {
908 free(op);
909 return ERR_PTR(err);
910 }
Wang Nan066dacb2016-02-22 09:10:30 +0000911 return op;
912}
913
Wang Nand7888572016-04-08 15:07:24 +0000914static struct bpf_map_op *
915bpf_map_op__clone(struct bpf_map_op *op)
916{
917 struct bpf_map_op *newop;
918
919 newop = memdup(op, sizeof(*op));
920 if (!newop) {
921 pr_debug("Failed to alloc bpf_map_op\n");
922 return NULL;
923 }
924
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]);
929
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");
933 free(newop);
934 return NULL;
935 }
936 }
937
938 return newop;
939}
940
941static struct bpf_map_priv *
942bpf_map_priv__clone(struct bpf_map_priv *priv)
943{
944 struct bpf_map_priv *newpriv;
945 struct bpf_map_op *pos, *newop;
946
947 newpriv = zalloc(sizeof(*newpriv));
948 if (!newpriv) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200949 pr_debug("Not enough memory to alloc map private\n");
Wang Nand7888572016-04-08 15:07:24 +0000950 return NULL;
951 }
952 INIT_LIST_HEAD(&newpriv->ops_list);
953
954 list_for_each_entry(pos, &priv->ops_list, list) {
955 newop = bpf_map_op__clone(pos);
956 if (!newop) {
957 bpf_map_priv__purge(newpriv);
958 return NULL;
959 }
960 list_add_tail(&newop->list, &newpriv->ops_list);
961 }
962
963 return newpriv;
964}
965
Wang Nan066dacb2016-02-22 09:10:30 +0000966static int
967bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
968{
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -0300969 const char *map_name = bpf_map__name(map);
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -0300970 struct bpf_map_priv *priv = bpf_map__priv(map);
Wang Nan066dacb2016-02-22 09:10:30 +0000971
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -0300972 if (IS_ERR(priv)) {
Wang Nan066dacb2016-02-22 09:10:30 +0000973 pr_debug("Failed to get private from map %s\n", map_name);
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -0300974 return PTR_ERR(priv);
Wang Nan066dacb2016-02-22 09:10:30 +0000975 }
976
977 if (!priv) {
978 priv = zalloc(sizeof(*priv));
979 if (!priv) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200980 pr_debug("Not enough memory to alloc map private\n");
Wang Nan066dacb2016-02-22 09:10:30 +0000981 return -ENOMEM;
982 }
983 INIT_LIST_HEAD(&priv->ops_list);
984
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -0300985 if (bpf_map__set_priv(map, priv, bpf_map_priv__clear)) {
Wang Nan066dacb2016-02-22 09:10:30 +0000986 free(priv);
987 return -BPF_LOADER_ERRNO__INTERNAL;
988 }
989 }
990
991 list_add_tail(&op->list, &priv->ops_list);
992 return 0;
993}
994
Wang Nan7630b3e2016-02-22 09:10:33 +0000995static struct bpf_map_op *
Wang Nan2d055bf2016-02-22 09:10:34 +0000996bpf_map__add_newop(struct bpf_map *map, struct parse_events_term *term)
Wang Nan7630b3e2016-02-22 09:10:33 +0000997{
998 struct bpf_map_op *op;
999 int err;
1000
Wang Nan2d055bf2016-02-22 09:10:34 +00001001 op = bpf_map_op__new(term);
Wang Nan7630b3e2016-02-22 09:10:33 +00001002 if (IS_ERR(op))
1003 return op;
1004
1005 err = bpf_map__add_op(map, op);
1006 if (err) {
1007 bpf_map_op__delete(op);
1008 return ERR_PTR(err);
1009 }
1010 return op;
1011}
1012
Wang Nan066dacb2016-02-22 09:10:30 +00001013static int
1014__bpf_map__config_value(struct bpf_map *map,
1015 struct parse_events_term *term)
1016{
Wang Nan066dacb2016-02-22 09:10:30 +00001017 struct bpf_map_op *op;
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001018 const char *map_name = bpf_map__name(map);
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001019 const struct bpf_map_def *def = bpf_map__def(map);
Wang Nan066dacb2016-02-22 09:10:30 +00001020
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001021 if (IS_ERR(def)) {
Wang Nan066dacb2016-02-22 09:10:30 +00001022 pr_debug("Unable to get map definition from '%s'\n",
1023 map_name);
1024 return -BPF_LOADER_ERRNO__INTERNAL;
1025 }
1026
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001027 if (def->type != BPF_MAP_TYPE_ARRAY) {
Wang Nan066dacb2016-02-22 09:10:30 +00001028 pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n",
1029 map_name);
1030 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1031 }
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001032 if (def->key_size < sizeof(unsigned int)) {
Wang Nan066dacb2016-02-22 09:10:30 +00001033 pr_debug("Map %s has incorrect key size\n", map_name);
1034 return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE;
1035 }
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001036 switch (def->value_size) {
Wang Nan066dacb2016-02-22 09:10:30 +00001037 case 1:
1038 case 2:
1039 case 4:
1040 case 8:
1041 break;
1042 default:
1043 pr_debug("Map %s has incorrect value size\n", map_name);
1044 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1045 }
1046
Wang Nan2d055bf2016-02-22 09:10:34 +00001047 op = bpf_map__add_newop(map, term);
Wang Nan066dacb2016-02-22 09:10:30 +00001048 if (IS_ERR(op))
1049 return PTR_ERR(op);
1050 op->op_type = BPF_MAP_OP_SET_VALUE;
1051 op->v.value = term->val.num;
Wang Nan7630b3e2016-02-22 09:10:33 +00001052 return 0;
Wang Nan066dacb2016-02-22 09:10:30 +00001053}
1054
1055static int
1056bpf_map__config_value(struct bpf_map *map,
1057 struct parse_events_term *term,
1058 struct perf_evlist *evlist __maybe_unused)
1059{
1060 if (!term->err_val) {
1061 pr_debug("Config value not set\n");
1062 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1063 }
1064
1065 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) {
Wang Nan7630b3e2016-02-22 09:10:33 +00001066 pr_debug("ERROR: wrong value type for 'value'\n");
Wang Nan066dacb2016-02-22 09:10:30 +00001067 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1068 }
1069
1070 return __bpf_map__config_value(map, term);
1071}
1072
Wang Nan7630b3e2016-02-22 09:10:33 +00001073static int
1074__bpf_map__config_event(struct bpf_map *map,
1075 struct parse_events_term *term,
1076 struct perf_evlist *evlist)
1077{
1078 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001079 const struct bpf_map_def *def;
Wang Nan7630b3e2016-02-22 09:10:33 +00001080 struct bpf_map_op *op;
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001081 const char *map_name = bpf_map__name(map);
Wang Nan7630b3e2016-02-22 09:10:33 +00001082
Wang Nan7630b3e2016-02-22 09:10:33 +00001083 evsel = perf_evlist__find_evsel_by_str(evlist, term->val.str);
1084 if (!evsel) {
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;
1088 }
1089
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001090 def = bpf_map__def(map);
1091 if (IS_ERR(def)) {
Wang Nan7630b3e2016-02-22 09:10:33 +00001092 pr_debug("Unable to get map definition from '%s'\n",
1093 map_name);
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001094 return PTR_ERR(def);
Wang Nan7630b3e2016-02-22 09:10:33 +00001095 }
1096
1097 /*
1098 * No need to check key_size and value_size:
1099 * kernel has already checked them.
1100 */
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001101 if (def->type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
Wang Nan7630b3e2016-02-22 09:10:33 +00001102 pr_debug("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
1103 map_name);
1104 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1105 }
1106
Wang Nan2d055bf2016-02-22 09:10:34 +00001107 op = bpf_map__add_newop(map, term);
Wang Nan7630b3e2016-02-22 09:10:33 +00001108 if (IS_ERR(op))
1109 return PTR_ERR(op);
1110 op->op_type = BPF_MAP_OP_SET_EVSEL;
1111 op->v.evsel = evsel;
1112 return 0;
1113}
1114
1115static int
1116bpf_map__config_event(struct bpf_map *map,
1117 struct parse_events_term *term,
1118 struct perf_evlist *evlist)
1119{
1120 if (!term->err_val) {
1121 pr_debug("Config value not set\n");
1122 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1123 }
1124
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;
1128 }
1129
1130 return __bpf_map__config_event(map, term, evlist);
1131}
1132
Wang Nan066dacb2016-02-22 09:10:30 +00001133struct 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 *);
1137};
1138
1139struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = {
1140 {"value", bpf_map__config_value},
Wang Nan7630b3e2016-02-22 09:10:33 +00001141 {"event", bpf_map__config_event},
Wang Nan066dacb2016-02-22 09:10:30 +00001142};
1143
1144static int
Wang Nan2d055bf2016-02-22 09:10:34 +00001145config_map_indices_range_check(struct parse_events_term *term,
1146 struct bpf_map *map,
1147 const char *map_name)
1148{
1149 struct parse_events_array *array = &term->array;
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001150 const struct bpf_map_def *def;
Wang Nan2d055bf2016-02-22 09:10:34 +00001151 unsigned int i;
Wang Nan2d055bf2016-02-22 09:10:34 +00001152
1153 if (!array->nr_ranges)
1154 return 0;
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;
1159 }
1160
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001161 def = bpf_map__def(map);
1162 if (IS_ERR(def)) {
Wang Nan2d055bf2016-02-22 09:10:34 +00001163 pr_debug("ERROR: Unable to get map definition from '%s'\n",
1164 map_name);
1165 return -BPF_LOADER_ERRNO__INTERNAL;
1166 }
1167
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;
1172
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001173 if (idx >= def->max_entries) {
Wang Nan2d055bf2016-02-22 09:10:34 +00001174 pr_debug("ERROR: index %d too large\n", idx);
1175 return -BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG;
1176 }
1177 }
1178 return 0;
1179}
1180
1181static int
Wang Nan066dacb2016-02-22 09:10:30 +00001182bpf__obj_config_map(struct bpf_object *obj,
1183 struct parse_events_term *term,
1184 struct perf_evlist *evlist,
1185 int *key_scan_pos)
1186{
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;
1191 char *map_opt;
1192 size_t i;
1193
1194 if (!map_name)
1195 return -ENOMEM;
1196
1197 map_opt = strchr(map_name, '.');
1198 if (!map_opt) {
1199 pr_debug("ERROR: Invalid map config: %s\n", map_name);
1200 goto out;
1201 }
1202
1203 *map_opt++ = '\0';
1204 if (*map_opt == '\0') {
1205 pr_debug("ERROR: Invalid map option: %s\n", term->config);
1206 goto out;
1207 }
1208
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001209 map = bpf_object__find_map_by_name(obj, map_name);
Wang Nan066dacb2016-02-22 09:10:30 +00001210 if (!map) {
1211 pr_debug("ERROR: Map %s doesn't exist\n", map_name);
1212 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST;
1213 goto out;
1214 }
1215
Wang Nan2d055bf2016-02-22 09:10:34 +00001216 *key_scan_pos += strlen(map_opt);
1217 err = config_map_indices_range_check(term, map, map_name);
1218 if (err)
1219 goto out;
1220 *key_scan_pos -= strlen(map_opt);
1221
Wang Nan066dacb2016-02-22 09:10:30 +00001222 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];
1225
1226 if (strcmp(map_opt, func->config_opt) == 0) {
1227 err = func->config_func(map, term, evlist);
1228 goto out;
1229 }
1230 }
1231
1232 pr_debug("ERROR: Invalid map config option '%s'\n", map_opt);
1233 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT;
1234out:
1235 free(map_name);
1236 if (!err)
1237 key_scan_pos += strlen(map_opt);
1238 return err;
1239}
1240
1241int bpf__config_obj(struct bpf_object *obj,
1242 struct parse_events_term *term,
1243 struct perf_evlist *evlist,
1244 int *error_pos)
1245{
1246 int key_scan_pos = 0;
1247 int err;
1248
1249 if (!obj || !term || !term->config)
1250 return -EINVAL;
1251
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -03001252 if (strstarts(term->config, "map:")) {
Wang Nan066dacb2016-02-22 09:10:30 +00001253 key_scan_pos = sizeof("map:") - 1;
1254 err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
1255 goto out;
1256 }
1257 err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1258out:
1259 if (error_pos)
1260 *error_pos = key_scan_pos;
1261 return err;
1262
1263}
1264
Wang Nan8690a2a2016-02-22 09:10:32 +00001265typedef int (*map_config_func_t)(const char *name, int map_fd,
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001266 const struct bpf_map_def *pdef,
Wang Nan8690a2a2016-02-22 09:10:32 +00001267 struct bpf_map_op *op,
1268 void *pkey, void *arg);
1269
1270static int
1271foreach_key_array_all(map_config_func_t func,
1272 void *arg, const char *name,
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001273 int map_fd, const struct bpf_map_def *pdef,
Wang Nan8690a2a2016-02-22 09:10:32 +00001274 struct bpf_map_op *op)
1275{
1276 unsigned int i;
1277 int err;
1278
1279 for (i = 0; i < pdef->max_entries; i++) {
1280 err = func(name, map_fd, pdef, op, &i, arg);
1281 if (err) {
1282 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1283 name, i);
1284 return err;
1285 }
1286 }
1287 return 0;
1288}
1289
1290static int
Wang Nan2d055bf2016-02-22 09:10:34 +00001291foreach_key_array_ranges(map_config_func_t func, void *arg,
1292 const char *name, int map_fd,
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001293 const struct bpf_map_def *pdef,
Wang Nan2d055bf2016-02-22 09:10:34 +00001294 struct bpf_map_op *op)
1295{
1296 unsigned int i, j;
1297 int err;
1298
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;
1302
1303 for (j = 0; j < length; j++) {
1304 unsigned int idx = start + j;
1305
1306 err = func(name, map_fd, pdef, op, &idx, arg);
1307 if (err) {
1308 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1309 name, idx);
1310 return err;
1311 }
1312 }
1313 }
1314 return 0;
1315}
1316
1317static int
Wang Nan8690a2a2016-02-22 09:10:32 +00001318bpf_map_config_foreach_key(struct bpf_map *map,
1319 map_config_func_t func,
1320 void *arg)
1321{
1322 int err, map_fd;
Wang Nan8690a2a2016-02-22 09:10:32 +00001323 struct bpf_map_op *op;
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001324 const struct bpf_map_def *def;
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001325 const char *name = bpf_map__name(map);
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001326 struct bpf_map_priv *priv = bpf_map__priv(map);
Wang Nan8690a2a2016-02-22 09:10:32 +00001327
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001328 if (IS_ERR(priv)) {
Wang Nan8690a2a2016-02-22 09:10:32 +00001329 pr_debug("ERROR: failed to get private from map %s\n", name);
1330 return -BPF_LOADER_ERRNO__INTERNAL;
1331 }
1332 if (!priv || list_empty(&priv->ops_list)) {
1333 pr_debug("INFO: nothing to config for map %s\n", name);
1334 return 0;
1335 }
1336
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001337 def = bpf_map__def(map);
1338 if (IS_ERR(def)) {
Wang Nan8690a2a2016-02-22 09:10:32 +00001339 pr_debug("ERROR: failed to get definition from map %s\n", name);
1340 return -BPF_LOADER_ERRNO__INTERNAL;
1341 }
Arnaldo Carvalho de Melo6e009e652016-06-03 12:15:52 -03001342 map_fd = bpf_map__fd(map);
Wang Nan8690a2a2016-02-22 09:10:32 +00001343 if (map_fd < 0) {
1344 pr_debug("ERROR: failed to get fd from map %s\n", name);
1345 return map_fd;
1346 }
1347
1348 list_for_each_entry(op, &priv->ops_list, list) {
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001349 switch (def->type) {
Wang Nan8690a2a2016-02-22 09:10:32 +00001350 case BPF_MAP_TYPE_ARRAY:
Wang Nan7630b3e2016-02-22 09:10:33 +00001351 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
Wang Nan8690a2a2016-02-22 09:10:32 +00001352 switch (op->key_type) {
1353 case BPF_MAP_KEY_ALL:
1354 err = foreach_key_array_all(func, arg, name,
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001355 map_fd, def, op);
Wang Nan2d055bf2016-02-22 09:10:34 +00001356 break;
1357 case BPF_MAP_KEY_RANGES:
1358 err = foreach_key_array_ranges(func, arg, name,
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001359 map_fd, def,
Wang Nan2d055bf2016-02-22 09:10:34 +00001360 op);
Wang Nan8690a2a2016-02-22 09:10:32 +00001361 break;
1362 default:
1363 pr_debug("ERROR: keytype for map '%s' invalid\n",
1364 name);
1365 return -BPF_LOADER_ERRNO__INTERNAL;
1366 }
Wang Nan2d055bf2016-02-22 09:10:34 +00001367 if (err)
1368 return err;
Wang Nan8690a2a2016-02-22 09:10:32 +00001369 break;
1370 default:
1371 pr_debug("ERROR: type of '%s' incorrect\n", name);
1372 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1373 }
1374 }
1375
1376 return 0;
1377}
1378
1379static int
1380apply_config_value_for_key(int map_fd, void *pkey,
1381 size_t val_size, u64 val)
1382{
1383 int err = 0;
1384
1385 switch (val_size) {
1386 case 1: {
1387 u8 _val = (u8)(val);
1388 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1389 break;
1390 }
1391 case 2: {
1392 u16 _val = (u16)(val);
1393 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1394 break;
1395 }
1396 case 4: {
1397 u32 _val = (u32)(val);
1398 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1399 break;
1400 }
1401 case 8: {
1402 err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY);
1403 break;
1404 }
1405 default:
1406 pr_debug("ERROR: invalid value size\n");
1407 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1408 }
1409 if (err && errno)
1410 err = -errno;
1411 return err;
1412}
1413
1414static int
Wang Nan7630b3e2016-02-22 09:10:33 +00001415apply_config_evsel_for_key(const char *name, int map_fd, void *pkey,
1416 struct perf_evsel *evsel)
1417{
1418 struct xyarray *xy = evsel->fd;
1419 struct perf_event_attr *attr;
1420 unsigned int key, events;
1421 bool check_pass = false;
1422 int *evt_fd;
1423 int err;
1424
1425 if (!xy) {
1426 pr_debug("ERROR: evsel not ready for map %s\n", name);
1427 return -BPF_LOADER_ERRNO__INTERNAL;
1428 }
1429
1430 if (xy->row_size / xy->entry_size != 1) {
1431 pr_debug("ERROR: Dimension of target event is incorrect for map %s\n",
1432 name);
1433 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM;
1434 }
1435
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;
1440 }
1441
Wang Nan03e0a7d2016-02-22 09:10:37 +00001442 if (perf_evsel__is_bpf_output(evsel))
1443 check_pass = true;
Wang Nan7630b3e2016-02-22 09:10:33 +00001444 if (attr->type == PERF_TYPE_RAW)
1445 check_pass = true;
1446 if (attr->type == PERF_TYPE_HARDWARE)
1447 check_pass = true;
Wang Nan7630b3e2016-02-22 09:10:33 +00001448 if (!check_pass) {
1449 pr_debug("ERROR: Event type is wrong for map %s\n", name);
1450 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE;
1451 }
1452
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",
1457 key, name);
1458 return -BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE;
1459 }
1460 evt_fd = xyarray__entry(xy, key, 0);
1461 err = bpf_map_update_elem(map_fd, pkey, evt_fd, BPF_ANY);
1462 if (err && errno)
1463 err = -errno;
1464 return err;
1465}
1466
1467static int
Wang Nan8690a2a2016-02-22 09:10:32 +00001468apply_obj_config_map_for_key(const char *name, int map_fd,
Arnaldo Carvalho de Melo53897a72016-06-02 14:21:06 -03001469 const struct bpf_map_def *pdef,
Wang Nan8690a2a2016-02-22 09:10:32 +00001470 struct bpf_map_op *op,
1471 void *pkey, void *arg __maybe_unused)
1472{
1473 int err;
1474
1475 switch (op->op_type) {
1476 case BPF_MAP_OP_SET_VALUE:
1477 err = apply_config_value_for_key(map_fd, pkey,
1478 pdef->value_size,
1479 op->v.value);
1480 break;
Wang Nan7630b3e2016-02-22 09:10:33 +00001481 case BPF_MAP_OP_SET_EVSEL:
1482 err = apply_config_evsel_for_key(name, map_fd, pkey,
1483 op->v.evsel);
1484 break;
Wang Nan8690a2a2016-02-22 09:10:32 +00001485 default:
1486 pr_debug("ERROR: unknown value type for '%s'\n", name);
1487 err = -BPF_LOADER_ERRNO__INTERNAL;
1488 }
1489 return err;
1490}
1491
1492static int
1493apply_obj_config_map(struct bpf_map *map)
1494{
1495 return bpf_map_config_foreach_key(map,
1496 apply_obj_config_map_for_key,
1497 NULL);
1498}
1499
1500static int
1501apply_obj_config_object(struct bpf_object *obj)
1502{
1503 struct bpf_map *map;
1504 int err;
1505
1506 bpf_map__for_each(map, obj) {
1507 err = apply_obj_config_map(map);
1508 if (err)
1509 return err;
1510 }
1511 return 0;
1512}
1513
1514int bpf__apply_obj_config(void)
1515{
1516 struct bpf_object *obj, *tmp;
1517 int err;
1518
1519 bpf_object__for_each_safe(obj, tmp) {
1520 err = apply_obj_config_object(obj);
1521 if (err)
1522 return err;
1523 }
1524
1525 return 0;
1526}
1527
Wang Nand7888572016-04-08 15:07:24 +00001528#define bpf__for_each_map(pos, obj, objtmp) \
1529 bpf_object__for_each_safe(obj, objtmp) \
1530 bpf_map__for_each(pos, obj)
1531
Arnaldo Carvalho de Melo5941d852018-08-06 09:46:33 -03001532#define bpf__for_each_map_named(pos, obj, objtmp, name) \
Wang Nand7888572016-04-08 15:07:24 +00001533 bpf__for_each_map(pos, obj, objtmp) \
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001534 if (bpf_map__name(pos) && \
Arnaldo Carvalho de Melo5941d852018-08-06 09:46:33 -03001535 (strcmp(name, \
Arnaldo Carvalho de Melo009ad5d2016-06-02 11:02:05 -03001536 bpf_map__name(pos)) == 0))
Wang Nand7888572016-04-08 15:07:24 +00001537
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001538struct perf_evsel *bpf__setup_output_event(struct perf_evlist *evlist, const char *name)
Wang Nand7888572016-04-08 15:07:24 +00001539{
1540 struct bpf_map_priv *tmpl_priv = NULL;
1541 struct bpf_object *obj, *tmp;
Wang Nan72c08092016-04-08 15:07:25 +00001542 struct perf_evsel *evsel = NULL;
Wang Nand7888572016-04-08 15:07:24 +00001543 struct bpf_map *map;
1544 int err;
1545 bool need_init = false;
1546
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001547 bpf__for_each_map_named(map, obj, tmp, name) {
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001548 struct bpf_map_priv *priv = bpf_map__priv(map);
Wang Nand7888572016-04-08 15:07:24 +00001549
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001550 if (IS_ERR(priv))
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001551 return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
Wang Nand7888572016-04-08 15:07:24 +00001552
1553 /*
1554 * No need to check map type: type should have been
1555 * verified by kernel.
1556 */
1557 if (!need_init && !priv)
1558 need_init = !priv;
1559 if (!tmpl_priv && priv)
1560 tmpl_priv = priv;
1561 }
1562
1563 if (!need_init)
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001564 return NULL;
Wang Nand7888572016-04-08 15:07:24 +00001565
Wang Nan72c08092016-04-08 15:07:25 +00001566 if (!tmpl_priv) {
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001567 char *event_definition = NULL;
1568
1569 if (asprintf(&event_definition, "bpf-output/no-inherit=1,name=%s/", name) < 0)
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001570 return ERR_PTR(-ENOMEM);
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001571
1572 err = parse_events(evlist, event_definition, NULL);
1573 free(event_definition);
1574
Wang Nan72c08092016-04-08 15:07:25 +00001575 if (err) {
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001576 pr_debug("ERROR: failed to create the \"%s\" bpf-output event\n", name);
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001577 return ERR_PTR(-err);
Wang Nan72c08092016-04-08 15:07:25 +00001578 }
1579
1580 evsel = perf_evlist__last(evlist);
1581 }
Wang Nand7888572016-04-08 15:07:24 +00001582
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001583 bpf__for_each_map_named(map, obj, tmp, name) {
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001584 struct bpf_map_priv *priv = bpf_map__priv(map);
Wang Nand7888572016-04-08 15:07:24 +00001585
Arnaldo Carvalho de Melob4cbfa52016-06-02 10:51:59 -03001586 if (IS_ERR(priv))
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001587 return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
Wang Nand7888572016-04-08 15:07:24 +00001588 if (priv)
1589 continue;
1590
Wang Nan72c08092016-04-08 15:07:25 +00001591 if (tmpl_priv) {
1592 priv = bpf_map_priv__clone(tmpl_priv);
1593 if (!priv)
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001594 return ERR_PTR(-ENOMEM);
Wang Nand7888572016-04-08 15:07:24 +00001595
Arnaldo Carvalho de Meloedb13ed2016-06-03 12:38:21 -03001596 err = bpf_map__set_priv(map, priv, bpf_map_priv__clear);
Wang Nan72c08092016-04-08 15:07:25 +00001597 if (err) {
1598 bpf_map_priv__clear(map, priv);
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001599 return ERR_PTR(err);
Wang Nan72c08092016-04-08 15:07:25 +00001600 }
1601 } else if (evsel) {
1602 struct bpf_map_op *op;
1603
1604 op = bpf_map__add_newop(map, NULL);
1605 if (IS_ERR(op))
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001606 return ERR_PTR(PTR_ERR(op));
Wang Nan72c08092016-04-08 15:07:25 +00001607 op->op_type = BPF_MAP_OP_SET_EVSEL;
1608 op->v.evsel = evsel;
Wang Nand7888572016-04-08 15:07:24 +00001609 }
1610 }
1611
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001612 return evsel;
Wang Nand7888572016-04-08 15:07:24 +00001613}
1614
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001615int bpf__setup_stdout(struct perf_evlist *evlist)
1616{
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03001617 struct perf_evsel *evsel = bpf__setup_output_event(evlist, "__bpf_stdout__");
Ding Xiange381d1c22018-09-07 09:34:42 +08001618 return PTR_ERR_OR_ZERO(evsel);
Arnaldo Carvalho de Melo92bbe8d2018-08-06 09:53:35 -03001619}
1620
Wang Nand3e0ce32015-11-06 13:58:09 +00001621#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)
1624
1625static 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",
Wang Nan0bb93492015-11-27 08:47:37 +00001631 [ERRCODE_OFFSET(PROGCONF_TERM)] = "Invalid program config term in config string",
He Kuangbfc077b2015-11-16 12:10:12 +00001632 [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",
Wang Nan066dacb2016-02-22 09:10:30 +00001635 [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",
Wang Nan7630b3e2016-02-22 09:10:33 +00001643 [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",
Wang Nan2d055bf2016-02-22 09:10:34 +00001648 [ERRCODE_OFFSET(OBJCONF_MAP_IDX2BIG)] = "Index too large",
Wang Nand3e0ce32015-11-06 13:58:09 +00001649};
1650
Wang Nan6371ca3b2015-11-06 13:49:37 +00001651static int
1652bpf_loader_strerror(int err, char *buf, size_t size)
1653{
1654 char sbuf[STRERR_BUFSIZE];
1655 const char *msg;
1656
1657 if (!buf || !size)
1658 return -1;
1659
1660 err = err > 0 ? err : -err;
1661
1662 if (err >= __LIBBPF_ERRNO__START)
1663 return libbpf_strerror(err, buf, size);
1664
Wang Nand3e0ce32015-11-06 13:58:09 +00001665 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';
1669 return 0;
1670 }
1671
1672 if (err >= __BPF_LOADER_ERRNO__END)
1673 snprintf(buf, size, "Unknown bpf loader error %d", err);
1674 else
1675 snprintf(buf, size, "%s",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001676 str_error_r(err, sbuf, sizeof(sbuf)));
Wang Nand3e0ce32015-11-06 13:58:09 +00001677
Wang Nan6371ca3b2015-11-06 13:49:37 +00001678 buf[size - 1] = '\0';
Wang Nand3e0ce32015-11-06 13:58:09 +00001679 return -1;
Wang Nan6371ca3b2015-11-06 13:49:37 +00001680}
1681
Wang Nanaa3abf32015-10-14 12:41:15 +00001682#define bpf__strerror_head(err, buf, size) \
1683 char sbuf[STRERR_BUFSIZE], *emsg;\
1684 if (!size)\
1685 return 0;\
1686 if (err < 0)\
1687 err = -err;\
Wang Nan6371ca3b2015-11-06 13:49:37 +00001688 bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
1689 emsg = sbuf;\
Wang Nanaa3abf32015-10-14 12:41:15 +00001690 switch (err) {\
1691 default:\
1692 scnprintf(buf, size, "%s", emsg);\
1693 break;
1694
1695#define bpf__strerror_entry(val, fmt...)\
1696 case val: {\
1697 scnprintf(buf, size, fmt);\
1698 break;\
1699 }
1700
1701#define bpf__strerror_end(buf, size)\
1702 }\
1703 buf[size - 1] = '\0';
1704
Wang Nand3e0ce32015-11-06 13:58:09 +00001705int bpf__strerror_prepare_load(const char *filename, bool source,
1706 int err, char *buf, size_t size)
1707{
1708 size_t n;
1709 int ret;
1710
1711 n = snprintf(buf, size, "Failed to load %s%s: ",
1712 filename, source ? " from source" : "");
1713 if (n >= size) {
1714 buf[size - 1] = '\0';
1715 return 0;
1716 }
1717 buf += n;
1718 size -= n;
1719
1720 ret = bpf_loader_strerror(err, buf, size);
1721 buf[size - 1] = '\0';
1722 return ret;
1723}
1724
Wang Nanaa3abf32015-10-14 12:41:15 +00001725int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
1726 int err, char *buf, size_t size)
1727{
1728 bpf__strerror_head(err, buf, size);
Wang Nan0bb93492015-11-27 08:47:37 +00001729 case BPF_LOADER_ERRNO__PROGCONF_TERM: {
Wang Nan361f2b12015-11-16 12:10:05 +00001730 scnprintf(buf, size, "%s (add -v to see detail)", emsg);
1731 break;
1732 }
Wang Nan03e01f52015-11-16 12:10:08 +00001733 bpf__strerror_entry(EEXIST, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'");
Wang Nand3e0ce32015-11-06 13:58:09 +00001734 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");
Wang Nanaa3abf32015-10-14 12:41:15 +00001737 bpf__strerror_end(buf, size);
1738 return 0;
Wang Nan69d262a2015-10-14 12:41:13 +00001739}
Wang Nan1e5e3ee2015-10-14 12:41:16 +00001740
Wang Nand3e0ce32015-11-06 13:58:09 +00001741int bpf__strerror_load(struct bpf_object *obj,
Wang Nan1e5e3ee2015-10-14 12:41:16 +00001742 int err, char *buf, size_t size)
1743{
1744 bpf__strerror_head(err, buf, size);
Wang Nand3e0ce32015-11-06 13:58:09 +00001745 case LIBBPF_ERRNO__KVER: {
Arnaldo Carvalho de Meloa7fe0452016-06-03 12:22:51 -03001746 unsigned int obj_kver = bpf_object__kversion(obj);
Wang Nand3e0ce32015-11-06 13:58:09 +00001747 unsigned int real_kver;
1748
1749 if (fetch_kernel_version(&real_kver, NULL, 0)) {
1750 scnprintf(buf, size, "Unable to fetch kernel version");
1751 break;
1752 }
1753
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));
1759 break;
1760 }
1761
1762 scnprintf(buf, size, "Failed to load program for unknown reason");
1763 break;
1764 }
Wang Nan1e5e3ee2015-10-14 12:41:16 +00001765 bpf__strerror_end(buf, size);
1766 return 0;
1767}
Wang Nan066dacb2016-02-22 09:10:30 +00001768
1769int 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)
1774{
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);
1779 return 0;
1780}
Wang Nan8690a2a2016-02-22 09:10:32 +00001781
1782int bpf__strerror_apply_obj_config(int err, char *buf, size_t size)
1783{
1784 bpf__strerror_head(err, buf, size);
Wang Nan7630b3e2016-02-22 09:10:33 +00001785 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");
Wang Nan8690a2a2016-02-22 09:10:32 +00001791 bpf__strerror_end(buf, size);
1792 return 0;
1793}
Wang Nand7888572016-04-08 15:07:24 +00001794
Arnaldo Carvalho de Meloaa31be32018-08-06 11:35:37 -03001795int bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused,
1796 int err, char *buf, size_t size)
Wang Nand7888572016-04-08 15:07:24 +00001797{
1798 bpf__strerror_head(err, buf, size);
1799 bpf__strerror_end(buf, size);
1800 return 0;
1801}