Commit | Line | Data |
---|---|---|
69d262a9 WN |
1 | /* |
2 | * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com> | |
3 | * Copyright (C) 2015, Huawei Inc. | |
4 | */ | |
5 | #ifndef __BPF_LOADER_H | |
6 | #define __BPF_LOADER_H | |
7 | ||
8 | #include <linux/compiler.h> | |
9 | #include <linux/err.h> | |
10 | #include <string.h> | |
d3e0ce39 | 11 | #include <bpf/libbpf.h> |
4edf30e3 | 12 | #include "probe-event.h" |
69d262a9 WN |
13 | #include "debug.h" |
14 | ||
d3e0ce39 WN |
15 | enum bpf_loader_errno { |
16 | __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100, | |
17 | /* Invalid config string */ | |
18 | BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START, | |
19 | BPF_LOADER_ERRNO__GROUP, /* Invalid group name */ | |
20 | BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */ | |
21 | BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */ | |
22 | BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */ | |
361f2b1d | 23 | BPF_LOADER_ERRNO__CONFIG_TERM, /* Invalid config term in config term */ |
d3e0ce39 WN |
24 | __BPF_LOADER_ERRNO__END, |
25 | }; | |
26 | ||
69d262a9 | 27 | struct bpf_object; |
aa3abf30 | 28 | #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" |
69d262a9 | 29 | |
4edf30e3 WN |
30 | typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, |
31 | int fd, void *arg); | |
32 | ||
69d262a9 | 33 | #ifdef HAVE_LIBBPF_SUPPORT |
d509db04 | 34 | struct bpf_object *bpf__prepare_load(const char *filename, bool source); |
d3e0ce39 WN |
35 | int bpf__strerror_prepare_load(const char *filename, bool source, |
36 | int err, char *buf, size_t size); | |
69d262a9 | 37 | |
ba1fae43 WN |
38 | struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, |
39 | const char *name); | |
40 | ||
69d262a9 | 41 | void bpf__clear(void); |
aa3abf30 WN |
42 | |
43 | int bpf__probe(struct bpf_object *obj); | |
44 | int bpf__unprobe(struct bpf_object *obj); | |
45 | int bpf__strerror_probe(struct bpf_object *obj, int err, | |
46 | char *buf, size_t size); | |
47 | ||
1e5e3ee8 WN |
48 | int bpf__load(struct bpf_object *obj); |
49 | int bpf__strerror_load(struct bpf_object *obj, int err, | |
50 | char *buf, size_t size); | |
4edf30e3 WN |
51 | int bpf__foreach_tev(struct bpf_object *obj, |
52 | bpf_prog_iter_callback_t func, void *arg); | |
69d262a9 WN |
53 | #else |
54 | static inline struct bpf_object * | |
d509db04 WN |
55 | bpf__prepare_load(const char *filename __maybe_unused, |
56 | bool source __maybe_unused) | |
69d262a9 WN |
57 | { |
58 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); | |
59 | return ERR_PTR(-ENOTSUP); | |
60 | } | |
61 | ||
ba1fae43 WN |
62 | static inline struct bpf_object * |
63 | bpf__prepare_load_buffer(void *obj_buf __maybe_unused, | |
64 | size_t obj_buf_sz __maybe_unused) | |
65 | { | |
66 | return ERR_PTR(-ENOTSUP); | |
67 | } | |
68 | ||
69d262a9 | 69 | static inline void bpf__clear(void) { } |
aa3abf30 WN |
70 | |
71 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} | |
72 | static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;} | |
1e5e3ee8 | 73 | static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; } |
aa3abf30 | 74 | |
4edf30e3 WN |
75 | static inline int |
76 | bpf__foreach_tev(struct bpf_object *obj __maybe_unused, | |
77 | bpf_prog_iter_callback_t func __maybe_unused, | |
78 | void *arg __maybe_unused) | |
79 | { | |
80 | return 0; | |
81 | } | |
82 | ||
aa3abf30 WN |
83 | static inline int |
84 | __bpf_strerror(char *buf, size_t size) | |
85 | { | |
86 | if (!size) | |
87 | return 0; | |
88 | strncpy(buf, | |
89 | "ERROR: eBPF object loading is disabled during compiling.\n", | |
90 | size); | |
91 | buf[size - 1] = '\0'; | |
92 | return 0; | |
93 | } | |
94 | ||
d3e0ce39 WN |
95 | static inline |
96 | int bpf__strerror_prepare_load(const char *filename __maybe_unused, | |
97 | bool source __maybe_unused, | |
98 | int err __maybe_unused, | |
99 | char *buf, size_t size) | |
100 | { | |
101 | return __bpf_strerror(buf, size); | |
102 | } | |
103 | ||
aa3abf30 WN |
104 | static inline int |
105 | bpf__strerror_probe(struct bpf_object *obj __maybe_unused, | |
106 | int err __maybe_unused, | |
107 | char *buf, size_t size) | |
108 | { | |
109 | return __bpf_strerror(buf, size); | |
110 | } | |
1e5e3ee8 WN |
111 | |
112 | static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, | |
113 | int err __maybe_unused, | |
114 | char *buf, size_t size) | |
115 | { | |
116 | return __bpf_strerror(buf, size); | |
117 | } | |
69d262a9 WN |
118 | #endif |
119 | #endif |