Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common eBPF ELF object loading operations. |
| 3 | * |
| 4 | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
| 5 | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
| 6 | * Copyright (C) 2015 Huawei Inc. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame^] | 10 | #include <stdio.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <string.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | #include <asm/unistd.h> |
| 15 | #include <linux/bpf.h> |
| 16 | |
| 17 | #include "libbpf.h" |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame^] | 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 | } |