blob: ab56073c5d6e4c21579e73cfd8ca813f09fba8de [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
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) \
15static 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
26DEFINE_PRINT_FN(warning, 0)
27DEFINE_PRINT_FN(info, 0)
28DEFINE_PRINT_FN(debug, 1)
29
30struct 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
51void 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}