diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/lib/bpf/libbpf.c | 40 | ||||
-rw-r--r-- | tools/lib/bpf/libbpf.h | 12 |
2 files changed, 52 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index c08d6bca1c22..6f0c13a1fb3c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -7,8 +7,48 @@ | |||
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
10 | #include <stdio.h> | ||
11 | #include <stdarg.h> | ||
12 | #include <string.h> | ||
10 | #include <unistd.h> | 13 | #include <unistd.h> |
11 | #include <asm/unistd.h> | 14 | #include <asm/unistd.h> |
12 | #include <linux/bpf.h> | 15 | #include <linux/bpf.h> |
13 | 16 | ||
14 | #include "libbpf.h" | 17 | #include "libbpf.h" |
18 | |||
19 | #define __printf(a, b) __attribute__((format(printf, a, b))) | ||
20 | |||
21 | __printf(1, 2) | ||
22 | static int __base_pr(const char *format, ...) | ||
23 | { | ||
24 | va_list args; | ||
25 | int err; | ||
26 | |||
27 | va_start(args, format); | ||
28 | err = vfprintf(stderr, format, args); | ||
29 | va_end(args); | ||
30 | return err; | ||
31 | } | ||
32 | |||
33 | static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr; | ||
34 | static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr; | ||
35 | static __printf(1, 2) libbpf_print_fn_t __pr_debug; | ||
36 | |||
37 | #define __pr(func, fmt, ...) \ | ||
38 | do { \ | ||
39 | if ((func)) \ | ||
40 | (func)("libbpf: " fmt, ##__VA_ARGS__); \ | ||
41 | } while (0) | ||
42 | |||
43 | #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__) | ||
44 | #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__) | ||
45 | #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__) | ||
46 | |||
47 | void libbpf_set_print(libbpf_print_fn_t warn, | ||
48 | libbpf_print_fn_t info, | ||
49 | libbpf_print_fn_t debug) | ||
50 | { | ||
51 | __pr_warning = warn; | ||
52 | __pr_info = info; | ||
53 | __pr_debug = debug; | ||
54 | } | ||
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index a6f46d908097..8d1eebafa958 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h | |||
@@ -8,4 +8,16 @@ | |||
8 | #ifndef __BPF_LIBBPF_H | 8 | #ifndef __BPF_LIBBPF_H |
9 | #define __BPF_LIBBPF_H | 9 | #define __BPF_LIBBPF_H |
10 | 10 | ||
11 | /* | ||
12 | * In include/linux/compiler-gcc.h, __printf is defined. However | ||
13 | * it should be better if libbpf.h doesn't depend on Linux header file. | ||
14 | * So instead of __printf, here we use gcc attribute directly. | ||
15 | */ | ||
16 | typedef int (*libbpf_print_fn_t)(const char *, ...) | ||
17 | __attribute__((format(printf, 1, 2))); | ||
18 | |||
19 | void libbpf_set_print(libbpf_print_fn_t warn, | ||
20 | libbpf_print_fn_t info, | ||
21 | libbpf_print_fn_t debug); | ||
22 | |||
11 | #endif | 23 | #endif |