blob: 190a1c7f0649f1c867d5a46917d20f2f3eaa6b5e [file] [log] [blame]
Wang Nan69d262a2015-10-14 12:41:13 +00001/*
2 * bpf-loader.c
3 *
4 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
5 * Copyright (C) 2015 Huawei Inc.
6 */
7
Wang Nana08357d2015-11-16 12:10:13 +00008#include <linux/bpf.h>
Wang Nan69d262a2015-10-14 12:41:13 +00009#include <bpf/libbpf.h>
10#include <linux/err.h>
Wang Nan03e01f52015-11-16 12:10:08 +000011#include <linux/string.h>
Wang Nan69d262a2015-10-14 12:41:13 +000012#include "perf.h"
13#include "debug.h"
14#include "bpf-loader.h"
Wang Nana08357d2015-11-16 12:10:13 +000015#include "bpf-prologue.h"
16#include "llvm-utils.h"
Wang Nanaa3abf32015-10-14 12:41:15 +000017#include "probe-event.h"
18#include "probe-finder.h" // for MAX_PROBES
Wang Nand509db02015-10-14 12:41:20 +000019#include "llvm-utils.h"
Wang Nan69d262a2015-10-14 12:41:13 +000020
21#define DEFINE_PRINT_FN(name, level) \
22static int libbpf_##name(const char *fmt, ...) \
23{ \
24 va_list args; \
25 int ret; \
26 \
27 va_start(args, fmt); \
28 ret = veprintf(level, verbose, pr_fmt(fmt), args);\
29 va_end(args); \
30 return ret; \
31}
32
Wang Nan7a011942015-11-03 10:44:43 +000033DEFINE_PRINT_FN(warning, 1)
34DEFINE_PRINT_FN(info, 1)
Wang Nan69d262a2015-10-14 12:41:13 +000035DEFINE_PRINT_FN(debug, 1)
36
Wang Nanaa3abf32015-10-14 12:41:15 +000037struct bpf_prog_priv {
38 struct perf_probe_event pev;
Wang Nana08357d2015-11-16 12:10:13 +000039 bool need_prologue;
40 struct bpf_insn *insns_buf;
Wang Nanaa3abf32015-10-14 12:41:15 +000041};
42
Wang Nanba1fae42015-11-06 13:49:43 +000043static bool libbpf_initialized;
44
45struct bpf_object *
46bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
47{
48 struct bpf_object *obj;
49
50 if (!libbpf_initialized) {
51 libbpf_set_print(libbpf_warning,
52 libbpf_info,
53 libbpf_debug);
54 libbpf_initialized = true;
55 }
56
57 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
58 if (IS_ERR(obj)) {
59 pr_debug("bpf: failed to load buffer\n");
60 return ERR_PTR(-EINVAL);
61 }
62
63 return obj;
64}
65
Wang Nand509db02015-10-14 12:41:20 +000066struct bpf_object *bpf__prepare_load(const char *filename, bool source)
Wang Nan69d262a2015-10-14 12:41:13 +000067{
68 struct bpf_object *obj;
Wang Nan69d262a2015-10-14 12:41:13 +000069
70 if (!libbpf_initialized) {
71 libbpf_set_print(libbpf_warning,
72 libbpf_info,
73 libbpf_debug);
74 libbpf_initialized = true;
75 }
76
Wang Nand509db02015-10-14 12:41:20 +000077 if (source) {
78 int err;
79 void *obj_buf;
80 size_t obj_buf_sz;
81
82 err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
83 if (err)
Wang Nand3e0ce32015-11-06 13:58:09 +000084 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
Wang Nand509db02015-10-14 12:41:20 +000085 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
86 free(obj_buf);
87 } else
88 obj = bpf_object__open(filename);
89
Wang Nan6371ca3b2015-11-06 13:49:37 +000090 if (IS_ERR(obj)) {
Wang Nan69d262a2015-10-14 12:41:13 +000091 pr_debug("bpf: failed to load %s\n", filename);
Wang Nan6371ca3b2015-11-06 13:49:37 +000092 return obj;
Wang Nan69d262a2015-10-14 12:41:13 +000093 }
94
95 return obj;
96}
97
98void bpf__clear(void)
99{
100 struct bpf_object *obj, *tmp;
101
Wang Nanaa3abf32015-10-14 12:41:15 +0000102 bpf_object__for_each_safe(obj, tmp) {
103 bpf__unprobe(obj);
Wang Nan69d262a2015-10-14 12:41:13 +0000104 bpf_object__close(obj);
Wang Nanaa3abf32015-10-14 12:41:15 +0000105 }
106}
107
108static void
109bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,
110 void *_priv)
111{
112 struct bpf_prog_priv *priv = _priv;
113
114 cleanup_perf_probe_events(&priv->pev, 1);
Wang Nana08357d2015-11-16 12:10:13 +0000115 zfree(&priv->insns_buf);
Wang Nanaa3abf32015-10-14 12:41:15 +0000116 free(priv);
117}
118
119static int
Wang Nan361f2b12015-11-16 12:10:05 +0000120config__exec(const char *value, struct perf_probe_event *pev)
121{
122 pev->uprobes = true;
123 pev->target = strdup(value);
124 if (!pev->target)
125 return -ENOMEM;
126 return 0;
127}
128
Wang Nan5dbd16c2015-11-16 12:10:06 +0000129static int
130config__module(const char *value, struct perf_probe_event *pev)
131{
132 pev->uprobes = false;
133 pev->target = strdup(value);
134 if (!pev->target)
135 return -ENOMEM;
136 return 0;
137}
138
Wang Nan03e01f52015-11-16 12:10:08 +0000139static int
140config__bool(const char *value,
141 bool *pbool, bool invert)
142{
143 int err;
144 bool bool_value;
145
146 if (!pbool)
147 return -EINVAL;
148
149 err = strtobool(value, &bool_value);
150 if (err)
151 return err;
152
153 *pbool = invert ? !bool_value : bool_value;
154 return 0;
155}
156
157static int
158config__inlines(const char *value,
159 struct perf_probe_event *pev __maybe_unused)
160{
161 return config__bool(value, &probe_conf.no_inlines, true);
162}
163
164static int
165config__force(const char *value,
166 struct perf_probe_event *pev __maybe_unused)
167{
168 return config__bool(value, &probe_conf.force_add, false);
169}
170
Wang Nan361f2b12015-11-16 12:10:05 +0000171static struct {
172 const char *key;
173 const char *usage;
174 const char *desc;
175 int (*func)(const char *, struct perf_probe_event *);
176} bpf_config_terms[] = {
177 {
178 .key = "exec",
179 .usage = "exec=<full path of file>",
180 .desc = "Set uprobe target",
181 .func = config__exec,
182 },
Wang Nan5dbd16c2015-11-16 12:10:06 +0000183 {
184 .key = "module",
185 .usage = "module=<module name> ",
186 .desc = "Set kprobe module",
187 .func = config__module,
Wang Nan03e01f52015-11-16 12:10:08 +0000188 },
189 {
190 .key = "inlines",
191 .usage = "inlines=[yes|no] ",
192 .desc = "Probe at inline symbol",
193 .func = config__inlines,
194 },
195 {
196 .key = "force",
197 .usage = "force=[yes|no] ",
198 .desc = "Forcibly add events with existing name",
199 .func = config__force,
200 },
Wang Nan361f2b12015-11-16 12:10:05 +0000201};
202
203static int
204do_config(const char *key, const char *value,
205 struct perf_probe_event *pev)
206{
207 unsigned int i;
208
209 pr_debug("config bpf program: %s=%s\n", key, value);
210 for (i = 0; i < ARRAY_SIZE(bpf_config_terms); i++)
211 if (strcmp(key, bpf_config_terms[i].key) == 0)
212 return bpf_config_terms[i].func(value, pev);
213
214 pr_debug("BPF: ERROR: invalid config option in object: %s=%s\n",
215 key, value);
216
217 pr_debug("\nHint: Currently valid options are:\n");
218 for (i = 0; i < ARRAY_SIZE(bpf_config_terms); i++)
219 pr_debug("\t%s:\t%s\n", bpf_config_terms[i].usage,
220 bpf_config_terms[i].desc);
221 pr_debug("\n");
222
223 return -BPF_LOADER_ERRNO__CONFIG_TERM;
224}
225
226static const char *
227parse_config_kvpair(const char *config_str, struct perf_probe_event *pev)
228{
229 char *text = strdup(config_str);
230 char *sep, *line;
231 const char *main_str = NULL;
232 int err = 0;
233
234 if (!text) {
235 pr_debug("No enough memory: dup config_str failed\n");
236 return ERR_PTR(-ENOMEM);
237 }
238
239 line = text;
240 while ((sep = strchr(line, ';'))) {
241 char *equ;
242
243 *sep = '\0';
244 equ = strchr(line, '=');
245 if (!equ) {
246 pr_warning("WARNING: invalid config in BPF object: %s\n",
247 line);
248 pr_warning("\tShould be 'key=value'.\n");
249 goto nextline;
250 }
251 *equ = '\0';
252
253 err = do_config(line, equ + 1, pev);
254 if (err)
255 break;
256nextline:
257 line = sep + 1;
258 }
259
260 if (!err)
261 main_str = config_str + (line - text);
262 free(text);
263
264 return err ? ERR_PTR(err) : main_str;
265}
266
267static int
268parse_config(const char *config_str, struct perf_probe_event *pev)
269{
270 int err;
271 const char *main_str = parse_config_kvpair(config_str, pev);
272
273 if (IS_ERR(main_str))
274 return PTR_ERR(main_str);
275
276 err = parse_perf_probe_command(main_str, pev);
277 if (err < 0) {
278 pr_debug("bpf: '%s' is not a valid config string\n",
279 config_str);
280 /* parse failed, don't need clear pev. */
281 return -BPF_LOADER_ERRNO__CONFIG;
282 }
283 return 0;
284}
285
286static int
Wang Nanaa3abf32015-10-14 12:41:15 +0000287config_bpf_program(struct bpf_program *prog)
288{
289 struct perf_probe_event *pev = NULL;
290 struct bpf_prog_priv *priv = NULL;
291 const char *config_str;
292 int err;
293
Wang Nan03e01f52015-11-16 12:10:08 +0000294 /* Initialize per-program probing setting */
295 probe_conf.no_inlines = false;
296 probe_conf.force_add = false;
297
Wang Nanaa3abf32015-10-14 12:41:15 +0000298 config_str = bpf_program__title(prog, false);
Wang Nan6371ca3b2015-11-06 13:49:37 +0000299 if (IS_ERR(config_str)) {
Wang Nanaa3abf32015-10-14 12:41:15 +0000300 pr_debug("bpf: unable to get title for program\n");
Wang Nan6371ca3b2015-11-06 13:49:37 +0000301 return PTR_ERR(config_str);
Wang Nanaa3abf32015-10-14 12:41:15 +0000302 }
303
304 priv = calloc(sizeof(*priv), 1);
305 if (!priv) {
306 pr_debug("bpf: failed to alloc priv\n");
307 return -ENOMEM;
308 }
309 pev = &priv->pev;
310
311 pr_debug("bpf: config program '%s'\n", config_str);
Wang Nan361f2b12015-11-16 12:10:05 +0000312 err = parse_config(config_str, pev);
313 if (err)
Wang Nanaa3abf32015-10-14 12:41:15 +0000314 goto errout;
Wang Nanaa3abf32015-10-14 12:41:15 +0000315
316 if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
317 pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
318 config_str, PERF_BPF_PROBE_GROUP);
Wang Nand3e0ce32015-11-06 13:58:09 +0000319 err = -BPF_LOADER_ERRNO__GROUP;
Wang Nanaa3abf32015-10-14 12:41:15 +0000320 goto errout;
321 } else if (!pev->group)
322 pev->group = strdup(PERF_BPF_PROBE_GROUP);
323
324 if (!pev->group) {
325 pr_debug("bpf: strdup failed\n");
326 err = -ENOMEM;
327 goto errout;
328 }
329
330 if (!pev->event) {
Wang Nand3e0ce32015-11-06 13:58:09 +0000331 pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
Wang Nanaa3abf32015-10-14 12:41:15 +0000332 config_str);
Wang Nand3e0ce32015-11-06 13:58:09 +0000333 err = -BPF_LOADER_ERRNO__EVENTNAME;
Wang Nanaa3abf32015-10-14 12:41:15 +0000334 goto errout;
335 }
336 pr_debug("bpf: config '%s' is ok\n", config_str);
337
338 err = bpf_program__set_private(prog, priv, bpf_prog_priv__clear);
339 if (err) {
340 pr_debug("Failed to set priv for program '%s'\n", config_str);
341 goto errout;
342 }
343
344 return 0;
345
346errout:
347 if (pev)
348 clear_perf_probe_event(pev);
349 free(priv);
350 return err;
351}
352
353static int bpf__prepare_probe(void)
354{
355 static int err = 0;
356 static bool initialized = false;
357
358 /*
359 * Make err static, so if init failed the first, bpf__prepare_probe()
360 * fails each time without calling init_probe_symbol_maps multiple
361 * times.
362 */
363 if (initialized)
364 return err;
365
366 initialized = true;
367 err = init_probe_symbol_maps(false);
368 if (err < 0)
369 pr_debug("Failed to init_probe_symbol_maps\n");
370 probe_conf.max_probes = MAX_PROBES;
371 return err;
372}
373
Wang Nana08357d2015-11-16 12:10:13 +0000374static int
375preproc_gen_prologue(struct bpf_program *prog, int n,
376 struct bpf_insn *orig_insns, int orig_insns_cnt,
377 struct bpf_prog_prep_result *res)
378{
379 struct probe_trace_event *tev;
380 struct perf_probe_event *pev;
381 struct bpf_prog_priv *priv;
382 struct bpf_insn *buf;
383 size_t prologue_cnt = 0;
384 int err;
385
386 err = bpf_program__get_private(prog, (void **)&priv);
387 if (err || !priv)
388 goto errout;
389
390 pev = &priv->pev;
391
392 if (n < 0 || n >= pev->ntevs)
393 goto errout;
394
395 tev = &pev->tevs[n];
396
397 buf = priv->insns_buf;
398 err = bpf__gen_prologue(tev->args, tev->nargs,
399 buf, &prologue_cnt,
400 BPF_MAXINSNS - orig_insns_cnt);
401 if (err) {
402 const char *title;
403
404 title = bpf_program__title(prog, false);
405 if (!title)
406 title = "[unknown]";
407
408 pr_debug("Failed to generate prologue for program %s\n",
409 title);
410 return err;
411 }
412
413 memcpy(&buf[prologue_cnt], orig_insns,
414 sizeof(struct bpf_insn) * orig_insns_cnt);
415
416 res->new_insn_ptr = buf;
417 res->new_insn_cnt = prologue_cnt + orig_insns_cnt;
418 res->pfd = NULL;
419 return 0;
420
421errout:
422 pr_debug("Internal error in preproc_gen_prologue\n");
423 return -BPF_LOADER_ERRNO__PROLOGUE;
424}
425
426static int hook_load_preprocessor(struct bpf_program *prog)
427{
428 struct perf_probe_event *pev;
429 struct bpf_prog_priv *priv;
430 bool need_prologue = false;
431 int err, i;
432
433 err = bpf_program__get_private(prog, (void **)&priv);
434 if (err || !priv) {
435 pr_debug("Internal error when hook preprocessor\n");
436 return -BPF_LOADER_ERRNO__INTERNAL;
437 }
438
439 pev = &priv->pev;
440 for (i = 0; i < pev->ntevs; i++) {
441 struct probe_trace_event *tev = &pev->tevs[i];
442
443 if (tev->nargs > 0) {
444 need_prologue = true;
445 break;
446 }
447 }
448
449 /*
450 * Since all tevs don't have argument, we don't need generate
451 * prologue.
452 */
453 if (!need_prologue) {
454 priv->need_prologue = false;
455 return 0;
456 }
457
458 priv->need_prologue = true;
459 priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS);
460 if (!priv->insns_buf) {
461 pr_debug("No enough memory: alloc insns_buf failed\n");
462 return -ENOMEM;
463 }
464
465 err = bpf_program__set_prep(prog, pev->ntevs,
466 preproc_gen_prologue);
467 return err;
468}
469
Wang Nanaa3abf32015-10-14 12:41:15 +0000470int bpf__probe(struct bpf_object *obj)
471{
472 int err = 0;
473 struct bpf_program *prog;
474 struct bpf_prog_priv *priv;
475 struct perf_probe_event *pev;
476
477 err = bpf__prepare_probe();
478 if (err) {
479 pr_debug("bpf__prepare_probe failed\n");
480 return err;
481 }
482
483 bpf_object__for_each_program(prog, obj) {
484 err = config_bpf_program(prog);
485 if (err)
486 goto out;
487
488 err = bpf_program__get_private(prog, (void **)&priv);
489 if (err || !priv)
490 goto out;
491 pev = &priv->pev;
492
493 err = convert_perf_probe_events(pev, 1);
494 if (err < 0) {
495 pr_debug("bpf_probe: failed to convert perf probe events");
496 goto out;
497 }
498
499 err = apply_perf_probe_events(pev, 1);
500 if (err < 0) {
501 pr_debug("bpf_probe: failed to apply perf probe events");
502 goto out;
503 }
Wang Nana08357d2015-11-16 12:10:13 +0000504
505 /*
506 * After probing, let's consider prologue, which
507 * adds program fetcher to BPF programs.
508 *
509 * hook_load_preprocessorr() hooks pre-processor
510 * to bpf_program, let it generate prologue
511 * dynamically during loading.
512 */
513 err = hook_load_preprocessor(prog);
514 if (err)
515 goto out;
Wang Nanaa3abf32015-10-14 12:41:15 +0000516 }
517out:
518 return err < 0 ? err : 0;
519}
520
521#define EVENTS_WRITE_BUFSIZE 4096
522int bpf__unprobe(struct bpf_object *obj)
523{
524 int err, ret = 0;
525 struct bpf_program *prog;
526 struct bpf_prog_priv *priv;
527
528 bpf_object__for_each_program(prog, obj) {
529 int i;
530
531 err = bpf_program__get_private(prog, (void **)&priv);
532 if (err || !priv)
533 continue;
534
535 for (i = 0; i < priv->pev.ntevs; i++) {
536 struct probe_trace_event *tev = &priv->pev.tevs[i];
537 char name_buf[EVENTS_WRITE_BUFSIZE];
538 struct strfilter *delfilter;
539
540 snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
541 "%s:%s", tev->group, tev->event);
542 name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
543
544 delfilter = strfilter__new(name_buf, NULL);
545 if (!delfilter) {
546 pr_debug("Failed to create filter for unprobing\n");
547 ret = -ENOMEM;
548 continue;
549 }
550
551 err = del_perf_probe_events(delfilter);
552 strfilter__delete(delfilter);
553 if (err) {
554 pr_debug("Failed to delete %s\n", name_buf);
555 ret = err;
556 continue;
557 }
558 }
559 }
560 return ret;
561}
562
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000563int bpf__load(struct bpf_object *obj)
564{
565 int err;
566
567 err = bpf_object__load(obj);
568 if (err) {
569 pr_debug("bpf: load objects failed\n");
570 return err;
571 }
572 return 0;
573}
574
Wang Nan4edf30e2015-10-14 12:41:17 +0000575int bpf__foreach_tev(struct bpf_object *obj,
576 bpf_prog_iter_callback_t func,
577 void *arg)
578{
579 struct bpf_program *prog;
580 int err;
581
582 bpf_object__for_each_program(prog, obj) {
583 struct probe_trace_event *tev;
584 struct perf_probe_event *pev;
585 struct bpf_prog_priv *priv;
586 int i, fd;
587
588 err = bpf_program__get_private(prog,
589 (void **)&priv);
590 if (err || !priv) {
591 pr_debug("bpf: failed to get private field\n");
Wang Nand3e0ce32015-11-06 13:58:09 +0000592 return -BPF_LOADER_ERRNO__INTERNAL;
Wang Nan4edf30e2015-10-14 12:41:17 +0000593 }
594
595 pev = &priv->pev;
596 for (i = 0; i < pev->ntevs; i++) {
597 tev = &pev->tevs[i];
598
Wang Nana08357d2015-11-16 12:10:13 +0000599 if (priv->need_prologue)
600 fd = bpf_program__nth_fd(prog, i);
601 else
602 fd = bpf_program__fd(prog);
603
Wang Nan4edf30e2015-10-14 12:41:17 +0000604 if (fd < 0) {
605 pr_debug("bpf: failed to get file descriptor\n");
606 return fd;
607 }
608
609 err = (*func)(tev, fd, arg);
610 if (err) {
611 pr_debug("bpf: call back failed, stop iterate\n");
612 return err;
613 }
614 }
615 }
616 return 0;
617}
618
Wang Nand3e0ce32015-11-06 13:58:09 +0000619#define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
620#define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
621#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
622
623static const char *bpf_loader_strerror_table[NR_ERRNO] = {
624 [ERRCODE_OFFSET(CONFIG)] = "Invalid config string",
625 [ERRCODE_OFFSET(GROUP)] = "Invalid group name",
626 [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string",
627 [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error",
628 [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet",
Wang Nan361f2b12015-11-16 12:10:05 +0000629 [ERRCODE_OFFSET(CONFIG_TERM)] = "Invalid config term in config string",
He Kuangbfc077b2015-11-16 12:10:12 +0000630 [ERRCODE_OFFSET(PROLOGUE)] = "Failed to generate prologue",
631 [ERRCODE_OFFSET(PROLOGUE2BIG)] = "Prologue too big for program",
632 [ERRCODE_OFFSET(PROLOGUEOOB)] = "Offset out of bound for prologue",
Wang Nand3e0ce32015-11-06 13:58:09 +0000633};
634
Wang Nan6371ca3b2015-11-06 13:49:37 +0000635static int
636bpf_loader_strerror(int err, char *buf, size_t size)
637{
638 char sbuf[STRERR_BUFSIZE];
639 const char *msg;
640
641 if (!buf || !size)
642 return -1;
643
644 err = err > 0 ? err : -err;
645
646 if (err >= __LIBBPF_ERRNO__START)
647 return libbpf_strerror(err, buf, size);
648
Wang Nand3e0ce32015-11-06 13:58:09 +0000649 if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) {
650 msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)];
651 snprintf(buf, size, "%s", msg);
652 buf[size - 1] = '\0';
653 return 0;
654 }
655
656 if (err >= __BPF_LOADER_ERRNO__END)
657 snprintf(buf, size, "Unknown bpf loader error %d", err);
658 else
659 snprintf(buf, size, "%s",
660 strerror_r(err, sbuf, sizeof(sbuf)));
661
Wang Nan6371ca3b2015-11-06 13:49:37 +0000662 buf[size - 1] = '\0';
Wang Nand3e0ce32015-11-06 13:58:09 +0000663 return -1;
Wang Nan6371ca3b2015-11-06 13:49:37 +0000664}
665
Wang Nanaa3abf32015-10-14 12:41:15 +0000666#define bpf__strerror_head(err, buf, size) \
667 char sbuf[STRERR_BUFSIZE], *emsg;\
668 if (!size)\
669 return 0;\
670 if (err < 0)\
671 err = -err;\
Wang Nan6371ca3b2015-11-06 13:49:37 +0000672 bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
673 emsg = sbuf;\
Wang Nanaa3abf32015-10-14 12:41:15 +0000674 switch (err) {\
675 default:\
676 scnprintf(buf, size, "%s", emsg);\
677 break;
678
679#define bpf__strerror_entry(val, fmt...)\
680 case val: {\
681 scnprintf(buf, size, fmt);\
682 break;\
683 }
684
685#define bpf__strerror_end(buf, size)\
686 }\
687 buf[size - 1] = '\0';
688
Wang Nand3e0ce32015-11-06 13:58:09 +0000689int bpf__strerror_prepare_load(const char *filename, bool source,
690 int err, char *buf, size_t size)
691{
692 size_t n;
693 int ret;
694
695 n = snprintf(buf, size, "Failed to load %s%s: ",
696 filename, source ? " from source" : "");
697 if (n >= size) {
698 buf[size - 1] = '\0';
699 return 0;
700 }
701 buf += n;
702 size -= n;
703
704 ret = bpf_loader_strerror(err, buf, size);
705 buf[size - 1] = '\0';
706 return ret;
707}
708
Wang Nanaa3abf32015-10-14 12:41:15 +0000709int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
710 int err, char *buf, size_t size)
711{
712 bpf__strerror_head(err, buf, size);
Wang Nan361f2b12015-11-16 12:10:05 +0000713 case BPF_LOADER_ERRNO__CONFIG_TERM: {
714 scnprintf(buf, size, "%s (add -v to see detail)", emsg);
715 break;
716 }
Wang Nan03e01f52015-11-16 12:10:08 +0000717 bpf__strerror_entry(EEXIST, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'");
Wang Nand3e0ce32015-11-06 13:58:09 +0000718 bpf__strerror_entry(EACCES, "You need to be root");
719 bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");
720 bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file");
Wang Nanaa3abf32015-10-14 12:41:15 +0000721 bpf__strerror_end(buf, size);
722 return 0;
Wang Nan69d262a2015-10-14 12:41:13 +0000723}
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000724
Wang Nand3e0ce32015-11-06 13:58:09 +0000725int bpf__strerror_load(struct bpf_object *obj,
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000726 int err, char *buf, size_t size)
727{
728 bpf__strerror_head(err, buf, size);
Wang Nand3e0ce32015-11-06 13:58:09 +0000729 case LIBBPF_ERRNO__KVER: {
730 unsigned int obj_kver = bpf_object__get_kversion(obj);
731 unsigned int real_kver;
732
733 if (fetch_kernel_version(&real_kver, NULL, 0)) {
734 scnprintf(buf, size, "Unable to fetch kernel version");
735 break;
736 }
737
738 if (obj_kver != real_kver) {
739 scnprintf(buf, size,
740 "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")",
741 KVER_PARAM(obj_kver),
742 KVER_PARAM(real_kver));
743 break;
744 }
745
746 scnprintf(buf, size, "Failed to load program for unknown reason");
747 break;
748 }
Wang Nan1e5e3ee2015-10-14 12:41:16 +0000749 bpf__strerror_end(buf, size);
750 return 0;
751}