1
2
3
4
5
6#ifndef __BPF_LOADER_H
7#define __BPF_LOADER_H
8
9#include <linux/compiler.h>
10#include <linux/err.h>
11#include <string.h>
12#include <bpf/libbpf.h>
13#include "probe-event.h"
14#include "evlist.h"
15#include "debug.h"
16
17enum bpf_loader_errno {
18 __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
19
20 BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
21 BPF_LOADER_ERRNO__GROUP,
22 BPF_LOADER_ERRNO__EVENTNAME,
23 BPF_LOADER_ERRNO__INTERNAL,
24 BPF_LOADER_ERRNO__COMPILE,
25 BPF_LOADER_ERRNO__PROGCONF_TERM,
26 BPF_LOADER_ERRNO__PROLOGUE,
27 BPF_LOADER_ERRNO__PROLOGUE2BIG,
28 BPF_LOADER_ERRNO__PROLOGUEOOB,
29 BPF_LOADER_ERRNO__OBJCONF_OPT,
30 BPF_LOADER_ERRNO__OBJCONF_CONF,
31 BPF_LOADER_ERRNO__OBJCONF_MAP_OPT,
32 BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST,
33 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE,
34 BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE,
35 BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE,
36 BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,
37 BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT,
38 BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE,
39 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM,
40 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH,
41 BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE,
42 BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG,
43 __BPF_LOADER_ERRNO__END,
44};
45
46struct bpf_object;
47struct parse_events_term;
48#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
49
50typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event,
51 int fd, void *arg);
52
53#ifdef HAVE_LIBBPF_SUPPORT
54struct bpf_object *bpf__prepare_load(const char *filename, bool source);
55int bpf__strerror_prepare_load(const char *filename, bool source,
56 int err, char *buf, size_t size);
57
58struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
59 const char *name);
60
61void bpf__clear(void);
62
63int bpf__probe(struct bpf_object *obj);
64int bpf__unprobe(struct bpf_object *obj);
65int bpf__strerror_probe(struct bpf_object *obj, int err,
66 char *buf, size_t size);
67
68int bpf__load(struct bpf_object *obj);
69int bpf__strerror_load(struct bpf_object *obj, int err,
70 char *buf, size_t size);
71int bpf__foreach_event(struct bpf_object *obj,
72 bpf_prog_iter_callback_t func, void *arg);
73
74int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
75 struct perf_evlist *evlist, int *error_pos);
76int bpf__strerror_config_obj(struct bpf_object *obj,
77 struct parse_events_term *term,
78 struct perf_evlist *evlist,
79 int *error_pos, int err, char *buf,
80 size_t size);
81int bpf__apply_obj_config(void);
82int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
83
84int bpf__setup_stdout(struct perf_evlist *evlist);
85int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err,
86 char *buf, size_t size);
87
88#else
89#include <errno.h>
90
91static inline struct bpf_object *
92bpf__prepare_load(const char *filename __maybe_unused,
93 bool source __maybe_unused)
94{
95 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
96 return ERR_PTR(-ENOTSUP);
97}
98
99static inline struct bpf_object *
100bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
101 size_t obj_buf_sz __maybe_unused)
102{
103 return ERR_PTR(-ENOTSUP);
104}
105
106static inline void bpf__clear(void) { }
107
108static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
109static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
110static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
111
112static inline int
113bpf__foreach_event(struct bpf_object *obj __maybe_unused,
114 bpf_prog_iter_callback_t func __maybe_unused,
115 void *arg __maybe_unused)
116{
117 return 0;
118}
119
120static inline int
121bpf__config_obj(struct bpf_object *obj __maybe_unused,
122 struct parse_events_term *term __maybe_unused,
123 struct perf_evlist *evlist __maybe_unused,
124 int *error_pos __maybe_unused)
125{
126 return 0;
127}
128
129static inline int
130bpf__apply_obj_config(void)
131{
132 return 0;
133}
134
135static inline int
136bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
137{
138 return 0;
139}
140
141static inline int
142__bpf_strerror(char *buf, size_t size)
143{
144 if (!size)
145 return 0;
146 strncpy(buf,
147 "ERROR: eBPF object loading is disabled during compiling.\n",
148 size);
149 buf[size - 1] = '\0';
150 return 0;
151}
152
153static inline
154int bpf__strerror_prepare_load(const char *filename __maybe_unused,
155 bool source __maybe_unused,
156 int err __maybe_unused,
157 char *buf, size_t size)
158{
159 return __bpf_strerror(buf, size);
160}
161
162static inline int
163bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
164 int err __maybe_unused,
165 char *buf, size_t size)
166{
167 return __bpf_strerror(buf, size);
168}
169
170static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
171 int err __maybe_unused,
172 char *buf, size_t size)
173{
174 return __bpf_strerror(buf, size);
175}
176
177static inline int
178bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
179 struct parse_events_term *term __maybe_unused,
180 struct perf_evlist *evlist __maybe_unused,
181 int *error_pos __maybe_unused,
182 int err __maybe_unused,
183 char *buf, size_t size)
184{
185 return __bpf_strerror(buf, size);
186}
187
188static inline int
189bpf__strerror_apply_obj_config(int err __maybe_unused,
190 char *buf, size_t size)
191{
192 return __bpf_strerror(buf, size);
193}
194
195static inline int
196bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,
197 int err __maybe_unused, char *buf,
198 size_t size)
199{
200 return __bpf_strerror(buf, size);
201}
202#endif
203#endif
204