blob: ccd8d7fd79d3443bde634534580883a19adde53e [file] [log] [blame]
Wang Nan69d262a2015-10-14 12:41:13 +00001/*
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>
Wang Nan4edf30e2015-10-14 12:41:17 +000011#include "probe-event.h"
Wang Nan69d262a2015-10-14 12:41:13 +000012#include "debug.h"
13
14struct bpf_object;
Wang Nanaa3abf32015-10-14 12:41:15 +000015#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
Wang Nan69d262a2015-10-14 12:41:13 +000016
Wang Nan4edf30e2015-10-14 12:41:17 +000017typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
18 int fd, void *arg);
19
Wang Nan69d262a2015-10-14 12:41:13 +000020#ifdef HAVE_LIBBPF_SUPPORT
Wang Nand509db02015-10-14 12:41:20 +000021struct bpf_object *bpf__prepare_load(const char *filename, bool source);
Wang Nan69d262a2015-10-14 12:41:13 +000022
23void bpf__clear(void);
Wang Nanaa3abf32015-10-14 12:41:15 +000024
25int bpf__probe(struct bpf_object *obj);
26int bpf__unprobe(struct bpf_object *obj);
27int bpf__strerror_probe(struct bpf_object *obj, int err,
28 char *buf, size_t size);
29
Wang Nan1e5e3ee2015-10-14 12:41:16 +000030int bpf__load(struct bpf_object *obj);
31int bpf__strerror_load(struct bpf_object *obj, int err,
32 char *buf, size_t size);
Wang Nan4edf30e2015-10-14 12:41:17 +000033int bpf__foreach_tev(struct bpf_object *obj,
34 bpf_prog_iter_callback_t func, void *arg);
Wang Nan69d262a2015-10-14 12:41:13 +000035#else
36static inline struct bpf_object *
Wang Nand509db02015-10-14 12:41:20 +000037bpf__prepare_load(const char *filename __maybe_unused,
38 bool source __maybe_unused)
Wang Nan69d262a2015-10-14 12:41:13 +000039{
40 pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
41 return ERR_PTR(-ENOTSUP);
42}
43
44static inline void bpf__clear(void) { }
Wang Nanaa3abf32015-10-14 12:41:15 +000045
46static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
47static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
Wang Nan1e5e3ee2015-10-14 12:41:16 +000048static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
Wang Nanaa3abf32015-10-14 12:41:15 +000049
50static inline int
Wang Nan4edf30e2015-10-14 12:41:17 +000051bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
52 bpf_prog_iter_callback_t func __maybe_unused,
53 void *arg __maybe_unused)
54{
55 return 0;
56}
57
58static inline int
Wang Nanaa3abf32015-10-14 12:41:15 +000059__bpf_strerror(char *buf, size_t size)
60{
61 if (!size)
62 return 0;
63 strncpy(buf,
64 "ERROR: eBPF object loading is disabled during compiling.\n",
65 size);
66 buf[size - 1] = '\0';
67 return 0;
68}
69
70static inline int
71bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
72 int err __maybe_unused,
73 char *buf, size_t size)
74{
75 return __bpf_strerror(buf, size);
76}
Wang Nan1e5e3ee2015-10-14 12:41:16 +000077
78static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
79 int err __maybe_unused,
80 char *buf, size_t size)
81{
82 return __bpf_strerror(buf, size);
83}
Wang Nan69d262a2015-10-14 12:41:13 +000084#endif
85#endif