Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * bpf-loader.c |
| 3 | * |
| 4 | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
| 5 | * Copyright (C) 2015 Huawei Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <bpf/libbpf.h> |
| 9 | #include <linux/err.h> |
| 10 | #include "perf.h" |
| 11 | #include "debug.h" |
| 12 | #include "bpf-loader.h" |
| 13 | |
| 14 | #define DEFINE_PRINT_FN(name, level) \ |
| 15 | static int libbpf_##name(const char *fmt, ...) \ |
| 16 | { \ |
| 17 | va_list args; \ |
| 18 | int ret; \ |
| 19 | \ |
| 20 | va_start(args, fmt); \ |
| 21 | ret = veprintf(level, verbose, pr_fmt(fmt), args);\ |
| 22 | va_end(args); \ |
| 23 | return ret; \ |
| 24 | } |
| 25 | |
| 26 | DEFINE_PRINT_FN(warning, 0) |
| 27 | DEFINE_PRINT_FN(info, 0) |
| 28 | DEFINE_PRINT_FN(debug, 1) |
| 29 | |
| 30 | struct bpf_object *bpf__prepare_load(const char *filename) |
| 31 | { |
| 32 | struct bpf_object *obj; |
| 33 | static bool libbpf_initialized; |
| 34 | |
| 35 | if (!libbpf_initialized) { |
| 36 | libbpf_set_print(libbpf_warning, |
| 37 | libbpf_info, |
| 38 | libbpf_debug); |
| 39 | libbpf_initialized = true; |
| 40 | } |
| 41 | |
| 42 | obj = bpf_object__open(filename); |
| 43 | if (!obj) { |
| 44 | pr_debug("bpf: failed to load %s\n", filename); |
| 45 | return ERR_PTR(-EINVAL); |
| 46 | } |
| 47 | |
| 48 | return obj; |
| 49 | } |
| 50 | |
| 51 | void bpf__clear(void) |
| 52 | { |
| 53 | struct bpf_object *obj, *tmp; |
| 54 | |
| 55 | bpf_object__for_each_safe(obj, tmp) |
| 56 | bpf_object__close(obj); |
| 57 | } |