4 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
5 * Copyright (C) 2015 Huawei Inc.
9 #include <bpf/libbpf.h>
10 #include <linux/err.h>
11 #include <linux/string.h>
14 #include "bpf-loader.h"
15 #include "bpf-prologue.h"
16 #include "llvm-utils.h"
17 #include "probe-event.h"
18 #include "probe-finder.h" // for MAX_PROBES
19 #include "llvm-utils.h"
21 #define DEFINE_PRINT_FN(name, level) \
22 static int libbpf_##name(const char *fmt, ...) \
27 va_start(args, fmt); \
28 ret = veprintf(level, verbose, pr_fmt(fmt), args);\
33 DEFINE_PRINT_FN(warning
, 1)
34 DEFINE_PRINT_FN(info
, 1)
35 DEFINE_PRINT_FN(debug
, 1)
37 struct bpf_prog_priv
{
38 struct perf_probe_event pev
;
40 struct bpf_insn
*insns_buf
;
45 static bool libbpf_initialized
;
48 bpf__prepare_load_buffer(void *obj_buf
, size_t obj_buf_sz
, const char *name
)
50 struct bpf_object
*obj
;
52 if (!libbpf_initialized
) {
53 libbpf_set_print(libbpf_warning
,
56 libbpf_initialized
= true;
59 obj
= bpf_object__open_buffer(obj_buf
, obj_buf_sz
, name
);
61 pr_debug("bpf: failed to load buffer\n");
62 return ERR_PTR(-EINVAL
);
68 struct bpf_object
*bpf__prepare_load(const char *filename
, bool source
)
70 struct bpf_object
*obj
;
72 if (!libbpf_initialized
) {
73 libbpf_set_print(libbpf_warning
,
76 libbpf_initialized
= true;
84 err
= llvm__compile_bpf(filename
, &obj_buf
, &obj_buf_sz
);
86 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE
);
87 obj
= bpf_object__open_buffer(obj_buf
, obj_buf_sz
, filename
);
90 obj
= bpf_object__open(filename
);
93 pr_debug("bpf: failed to load %s\n", filename
);
100 void bpf__clear(void)
102 struct bpf_object
*obj
, *tmp
;
104 bpf_object__for_each_safe(obj
, tmp
) {
106 bpf_object__close(obj
);
111 bpf_prog_priv__clear(struct bpf_program
*prog __maybe_unused
,
114 struct bpf_prog_priv
*priv
= _priv
;
116 cleanup_perf_probe_events(&priv
->pev
, 1);
117 zfree(&priv
->insns_buf
);
118 zfree(&priv
->type_mapping
);
123 prog_config__exec(const char *value
, struct perf_probe_event
*pev
)
126 pev
->target
= strdup(value
);
133 prog_config__module(const char *value
, struct perf_probe_event
*pev
)
135 pev
->uprobes
= false;
136 pev
->target
= strdup(value
);
143 prog_config__bool(const char *value
, bool *pbool
, bool invert
)
151 err
= strtobool(value
, &bool_value
);
155 *pbool
= invert
? !bool_value
: bool_value
;
160 prog_config__inlines(const char *value
,
161 struct perf_probe_event
*pev __maybe_unused
)
163 return prog_config__bool(value
, &probe_conf
.no_inlines
, true);
167 prog_config__force(const char *value
,
168 struct perf_probe_event
*pev __maybe_unused
)
170 return prog_config__bool(value
, &probe_conf
.force_add
, false);
177 int (*func
)(const char *, struct perf_probe_event
*);
178 } bpf_prog_config_terms
[] = {
181 .usage
= "exec=<full path of file>",
182 .desc
= "Set uprobe target",
183 .func
= prog_config__exec
,
187 .usage
= "module=<module name> ",
188 .desc
= "Set kprobe module",
189 .func
= prog_config__module
,
193 .usage
= "inlines=[yes|no] ",
194 .desc
= "Probe at inline symbol",
195 .func
= prog_config__inlines
,
199 .usage
= "force=[yes|no] ",
200 .desc
= "Forcibly add events with existing name",
201 .func
= prog_config__force
,
206 do_prog_config(const char *key
, const char *value
,
207 struct perf_probe_event
*pev
)
211 pr_debug("config bpf program: %s=%s\n", key
, value
);
212 for (i
= 0; i
< ARRAY_SIZE(bpf_prog_config_terms
); i
++)
213 if (strcmp(key
, bpf_prog_config_terms
[i
].key
) == 0)
214 return bpf_prog_config_terms
[i
].func(value
, pev
);
216 pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
219 pr_debug("\nHint: Valid options are:\n");
220 for (i
= 0; i
< ARRAY_SIZE(bpf_prog_config_terms
); i
++)
221 pr_debug("\t%s:\t%s\n", bpf_prog_config_terms
[i
].usage
,
222 bpf_prog_config_terms
[i
].desc
);
225 return -BPF_LOADER_ERRNO__PROGCONF_TERM
;
229 parse_prog_config_kvpair(const char *config_str
, struct perf_probe_event
*pev
)
231 char *text
= strdup(config_str
);
233 const char *main_str
= NULL
;
237 pr_debug("No enough memory: dup config_str failed\n");
238 return ERR_PTR(-ENOMEM
);
242 while ((sep
= strchr(line
, ';'))) {
246 equ
= strchr(line
, '=');
248 pr_warning("WARNING: invalid config in BPF object: %s\n",
250 pr_warning("\tShould be 'key=value'.\n");
255 err
= do_prog_config(line
, equ
+ 1, pev
);
263 main_str
= config_str
+ (line
- text
);
266 return err
? ERR_PTR(err
) : main_str
;
270 parse_prog_config(const char *config_str
, struct perf_probe_event
*pev
)
273 const char *main_str
= parse_prog_config_kvpair(config_str
, pev
);
275 if (IS_ERR(main_str
))
276 return PTR_ERR(main_str
);
278 err
= parse_perf_probe_command(main_str
, pev
);
280 pr_debug("bpf: '%s' is not a valid config string\n",
282 /* parse failed, don't need clear pev. */
283 return -BPF_LOADER_ERRNO__CONFIG
;
289 config_bpf_program(struct bpf_program
*prog
)
291 struct perf_probe_event
*pev
= NULL
;
292 struct bpf_prog_priv
*priv
= NULL
;
293 const char *config_str
;
296 /* Initialize per-program probing setting */
297 probe_conf
.no_inlines
= false;
298 probe_conf
.force_add
= false;
300 config_str
= bpf_program__title(prog
, false);
301 if (IS_ERR(config_str
)) {
302 pr_debug("bpf: unable to get title for program\n");
303 return PTR_ERR(config_str
);
306 priv
= calloc(sizeof(*priv
), 1);
308 pr_debug("bpf: failed to alloc priv\n");
313 pr_debug("bpf: config program '%s'\n", config_str
);
314 err
= parse_prog_config(config_str
, pev
);
318 if (pev
->group
&& strcmp(pev
->group
, PERF_BPF_PROBE_GROUP
)) {
319 pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
320 config_str
, PERF_BPF_PROBE_GROUP
);
321 err
= -BPF_LOADER_ERRNO__GROUP
;
323 } else if (!pev
->group
)
324 pev
->group
= strdup(PERF_BPF_PROBE_GROUP
);
327 pr_debug("bpf: strdup failed\n");
333 pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
335 err
= -BPF_LOADER_ERRNO__EVENTNAME
;
338 pr_debug("bpf: config '%s' is ok\n", config_str
);
340 err
= bpf_program__set_private(prog
, priv
, bpf_prog_priv__clear
);
342 pr_debug("Failed to set priv for program '%s'\n", config_str
);
350 clear_perf_probe_event(pev
);
355 static int bpf__prepare_probe(void)
358 static bool initialized
= false;
361 * Make err static, so if init failed the first, bpf__prepare_probe()
362 * fails each time without calling init_probe_symbol_maps multiple
369 err
= init_probe_symbol_maps(false);
371 pr_debug("Failed to init_probe_symbol_maps\n");
372 probe_conf
.max_probes
= MAX_PROBES
;
377 preproc_gen_prologue(struct bpf_program
*prog
, int n
,
378 struct bpf_insn
*orig_insns
, int orig_insns_cnt
,
379 struct bpf_prog_prep_result
*res
)
381 struct probe_trace_event
*tev
;
382 struct perf_probe_event
*pev
;
383 struct bpf_prog_priv
*priv
;
384 struct bpf_insn
*buf
;
385 size_t prologue_cnt
= 0;
388 err
= bpf_program__get_private(prog
, (void **)&priv
);
394 if (n
< 0 || n
>= priv
->nr_types
)
397 /* Find a tev belongs to that type */
398 for (i
= 0; i
< pev
->ntevs
; i
++) {
399 if (priv
->type_mapping
[i
] == n
)
403 if (i
>= pev
->ntevs
) {
404 pr_debug("Internal error: prologue type %d not found\n", n
);
405 return -BPF_LOADER_ERRNO__PROLOGUE
;
410 buf
= priv
->insns_buf
;
411 err
= bpf__gen_prologue(tev
->args
, tev
->nargs
,
413 BPF_MAXINSNS
- orig_insns_cnt
);
417 title
= bpf_program__title(prog
, false);
421 pr_debug("Failed to generate prologue for program %s\n",
426 memcpy(&buf
[prologue_cnt
], orig_insns
,
427 sizeof(struct bpf_insn
) * orig_insns_cnt
);
429 res
->new_insn_ptr
= buf
;
430 res
->new_insn_cnt
= prologue_cnt
+ orig_insns_cnt
;
435 pr_debug("Internal error in preproc_gen_prologue\n");
436 return -BPF_LOADER_ERRNO__PROLOGUE
;
440 * compare_tev_args is reflexive, transitive and antisymmetric.
441 * I can proof it but this margin is too narrow to contain.
443 static int compare_tev_args(const void *ptev1
, const void *ptev2
)
446 const struct probe_trace_event
*tev1
=
447 *(const struct probe_trace_event
**)ptev1
;
448 const struct probe_trace_event
*tev2
=
449 *(const struct probe_trace_event
**)ptev2
;
451 ret
= tev2
->nargs
- tev1
->nargs
;
455 for (i
= 0; i
< tev1
->nargs
; i
++) {
456 struct probe_trace_arg
*arg1
, *arg2
;
457 struct probe_trace_arg_ref
*ref1
, *ref2
;
459 arg1
= &tev1
->args
[i
];
460 arg2
= &tev2
->args
[i
];
462 ret
= strcmp(arg1
->value
, arg2
->value
);
469 while (ref1
&& ref2
) {
470 ret
= ref2
->offset
- ref1
->offset
;
479 return ref2
? 1 : -1;
486 * Assign a type number to each tevs in a pev.
487 * mapping is an array with same slots as tevs in that pev.
488 * nr_types will be set to number of types.
490 static int map_prologue(struct perf_probe_event
*pev
, int *mapping
,
494 struct probe_trace_event
**ptevs
;
496 size_t array_sz
= sizeof(*ptevs
) * pev
->ntevs
;
498 ptevs
= malloc(array_sz
);
500 pr_debug("No ehough memory: alloc ptevs failed\n");
504 pr_debug("In map_prologue, ntevs=%d\n", pev
->ntevs
);
505 for (i
= 0; i
< pev
->ntevs
; i
++)
506 ptevs
[i
] = &pev
->tevs
[i
];
508 qsort(ptevs
, pev
->ntevs
, sizeof(*ptevs
),
511 for (i
= 0; i
< pev
->ntevs
; i
++) {
514 n
= ptevs
[i
] - pev
->tevs
;
517 pr_debug("mapping[%d]=%d\n", n
, type
);
521 if (compare_tev_args(ptevs
+ i
, ptevs
+ i
- 1) == 0)
526 pr_debug("mapping[%d]=%d\n", n
, mapping
[n
]);
529 *nr_types
= type
+ 1;
534 static int hook_load_preprocessor(struct bpf_program
*prog
)
536 struct perf_probe_event
*pev
;
537 struct bpf_prog_priv
*priv
;
538 bool need_prologue
= false;
541 err
= bpf_program__get_private(prog
, (void **)&priv
);
543 pr_debug("Internal error when hook preprocessor\n");
544 return -BPF_LOADER_ERRNO__INTERNAL
;
548 for (i
= 0; i
< pev
->ntevs
; i
++) {
549 struct probe_trace_event
*tev
= &pev
->tevs
[i
];
551 if (tev
->nargs
> 0) {
552 need_prologue
= true;
558 * Since all tevs don't have argument, we don't need generate
561 if (!need_prologue
) {
562 priv
->need_prologue
= false;
566 priv
->need_prologue
= true;
567 priv
->insns_buf
= malloc(sizeof(struct bpf_insn
) * BPF_MAXINSNS
);
568 if (!priv
->insns_buf
) {
569 pr_debug("No enough memory: alloc insns_buf failed\n");
573 priv
->type_mapping
= malloc(sizeof(int) * pev
->ntevs
);
574 if (!priv
->type_mapping
) {
575 pr_debug("No enough memory: alloc type_mapping failed\n");
578 memset(priv
->type_mapping
, -1,
579 sizeof(int) * pev
->ntevs
);
581 err
= map_prologue(pev
, priv
->type_mapping
, &priv
->nr_types
);
585 err
= bpf_program__set_prep(prog
, priv
->nr_types
,
586 preproc_gen_prologue
);
590 int bpf__probe(struct bpf_object
*obj
)
593 struct bpf_program
*prog
;
594 struct bpf_prog_priv
*priv
;
595 struct perf_probe_event
*pev
;
597 err
= bpf__prepare_probe();
599 pr_debug("bpf__prepare_probe failed\n");
603 bpf_object__for_each_program(prog
, obj
) {
604 err
= config_bpf_program(prog
);
608 err
= bpf_program__get_private(prog
, (void **)&priv
);
613 err
= convert_perf_probe_events(pev
, 1);
615 pr_debug("bpf_probe: failed to convert perf probe events");
619 err
= apply_perf_probe_events(pev
, 1);
621 pr_debug("bpf_probe: failed to apply perf probe events");
626 * After probing, let's consider prologue, which
627 * adds program fetcher to BPF programs.
629 * hook_load_preprocessorr() hooks pre-processor
630 * to bpf_program, let it generate prologue
631 * dynamically during loading.
633 err
= hook_load_preprocessor(prog
);
638 return err
< 0 ? err
: 0;
641 #define EVENTS_WRITE_BUFSIZE 4096
642 int bpf__unprobe(struct bpf_object
*obj
)
645 struct bpf_program
*prog
;
646 struct bpf_prog_priv
*priv
;
648 bpf_object__for_each_program(prog
, obj
) {
651 err
= bpf_program__get_private(prog
, (void **)&priv
);
655 for (i
= 0; i
< priv
->pev
.ntevs
; i
++) {
656 struct probe_trace_event
*tev
= &priv
->pev
.tevs
[i
];
657 char name_buf
[EVENTS_WRITE_BUFSIZE
];
658 struct strfilter
*delfilter
;
660 snprintf(name_buf
, EVENTS_WRITE_BUFSIZE
,
661 "%s:%s", tev
->group
, tev
->event
);
662 name_buf
[EVENTS_WRITE_BUFSIZE
- 1] = '\0';
664 delfilter
= strfilter__new(name_buf
, NULL
);
666 pr_debug("Failed to create filter for unprobing\n");
671 err
= del_perf_probe_events(delfilter
);
672 strfilter__delete(delfilter
);
674 pr_debug("Failed to delete %s\n", name_buf
);
683 int bpf__load(struct bpf_object
*obj
)
687 err
= bpf_object__load(obj
);
689 pr_debug("bpf: load objects failed\n");
695 int bpf__foreach_tev(struct bpf_object
*obj
,
696 bpf_prog_iter_callback_t func
,
699 struct bpf_program
*prog
;
702 bpf_object__for_each_program(prog
, obj
) {
703 struct probe_trace_event
*tev
;
704 struct perf_probe_event
*pev
;
705 struct bpf_prog_priv
*priv
;
708 err
= bpf_program__get_private(prog
,
711 pr_debug("bpf: failed to get private field\n");
712 return -BPF_LOADER_ERRNO__INTERNAL
;
716 for (i
= 0; i
< pev
->ntevs
; i
++) {
719 if (priv
->need_prologue
) {
720 int type
= priv
->type_mapping
[i
];
722 fd
= bpf_program__nth_fd(prog
, type
);
724 fd
= bpf_program__fd(prog
);
728 pr_debug("bpf: failed to get file descriptor\n");
732 err
= (*func
)(tev
, fd
, arg
);
734 pr_debug("bpf: call back failed, stop iterate\n");
742 #define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
743 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
744 #define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
746 static const char *bpf_loader_strerror_table
[NR_ERRNO
] = {
747 [ERRCODE_OFFSET(CONFIG
)] = "Invalid config string",
748 [ERRCODE_OFFSET(GROUP
)] = "Invalid group name",
749 [ERRCODE_OFFSET(EVENTNAME
)] = "No event name found in config string",
750 [ERRCODE_OFFSET(INTERNAL
)] = "BPF loader internal error",
751 [ERRCODE_OFFSET(COMPILE
)] = "Error when compiling BPF scriptlet",
752 [ERRCODE_OFFSET(PROGCONF_TERM
)] = "Invalid program config term in config string",
753 [ERRCODE_OFFSET(PROLOGUE
)] = "Failed to generate prologue",
754 [ERRCODE_OFFSET(PROLOGUE2BIG
)] = "Prologue too big for program",
755 [ERRCODE_OFFSET(PROLOGUEOOB
)] = "Offset out of bound for prologue",
759 bpf_loader_strerror(int err
, char *buf
, size_t size
)
761 char sbuf
[STRERR_BUFSIZE
];
767 err
= err
> 0 ? err
: -err
;
769 if (err
>= __LIBBPF_ERRNO__START
)
770 return libbpf_strerror(err
, buf
, size
);
772 if (err
>= __BPF_LOADER_ERRNO__START
&& err
< __BPF_LOADER_ERRNO__END
) {
773 msg
= bpf_loader_strerror_table
[ERRNO_OFFSET(err
)];
774 snprintf(buf
, size
, "%s", msg
);
775 buf
[size
- 1] = '\0';
779 if (err
>= __BPF_LOADER_ERRNO__END
)
780 snprintf(buf
, size
, "Unknown bpf loader error %d", err
);
782 snprintf(buf
, size
, "%s",
783 strerror_r(err
, sbuf
, sizeof(sbuf
)));
785 buf
[size
- 1] = '\0';
789 #define bpf__strerror_head(err, buf, size) \
790 char sbuf[STRERR_BUFSIZE], *emsg;\
795 bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
799 scnprintf(buf, size, "%s", emsg);\
802 #define bpf__strerror_entry(val, fmt...)\
804 scnprintf(buf, size, fmt);\
808 #define bpf__strerror_end(buf, size)\
810 buf[size - 1] = '\0';
812 int bpf__strerror_prepare_load(const char *filename
, bool source
,
813 int err
, char *buf
, size_t size
)
818 n
= snprintf(buf
, size
, "Failed to load %s%s: ",
819 filename
, source
? " from source" : "");
821 buf
[size
- 1] = '\0';
827 ret
= bpf_loader_strerror(err
, buf
, size
);
828 buf
[size
- 1] = '\0';
832 int bpf__strerror_probe(struct bpf_object
*obj __maybe_unused
,
833 int err
, char *buf
, size_t size
)
835 bpf__strerror_head(err
, buf
, size
);
836 case BPF_LOADER_ERRNO__PROGCONF_TERM
: {
837 scnprintf(buf
, size
, "%s (add -v to see detail)", emsg
);
840 bpf__strerror_entry(EEXIST
, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'");
841 bpf__strerror_entry(EACCES
, "You need to be root");
842 bpf__strerror_entry(EPERM
, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");
843 bpf__strerror_entry(ENOENT
, "You need to check probing points in BPF file");
844 bpf__strerror_end(buf
, size
);
848 int bpf__strerror_load(struct bpf_object
*obj
,
849 int err
, char *buf
, size_t size
)
851 bpf__strerror_head(err
, buf
, size
);
852 case LIBBPF_ERRNO__KVER
: {
853 unsigned int obj_kver
= bpf_object__get_kversion(obj
);
854 unsigned int real_kver
;
856 if (fetch_kernel_version(&real_kver
, NULL
, 0)) {
857 scnprintf(buf
, size
, "Unable to fetch kernel version");
861 if (obj_kver
!= real_kver
) {
863 "'version' ("KVER_FMT
") doesn't match running kernel ("KVER_FMT
")",
864 KVER_PARAM(obj_kver
),
865 KVER_PARAM(real_kver
));
869 scnprintf(buf
, size
, "Failed to load program for unknown reason");
872 bpf__strerror_end(buf
, size
);