Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 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> |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 11 | #include "probe-event.h" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 12 | #include "debug.h" |
| 13 | |
| 14 | struct bpf_object; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 15 | #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 16 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 17 | typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, |
| 18 | int fd, void *arg); |
| 19 | |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 20 | #ifdef HAVE_LIBBPF_SUPPORT |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame^] | 21 | struct bpf_object *bpf__prepare_load(const char *filename, bool source); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 22 | |
| 23 | void bpf__clear(void); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 24 | |
| 25 | int bpf__probe(struct bpf_object *obj); |
| 26 | int bpf__unprobe(struct bpf_object *obj); |
| 27 | int bpf__strerror_probe(struct bpf_object *obj, int err, |
| 28 | char *buf, size_t size); |
| 29 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 30 | int bpf__load(struct bpf_object *obj); |
| 31 | int bpf__strerror_load(struct bpf_object *obj, int err, |
| 32 | char *buf, size_t size); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 33 | int bpf__foreach_tev(struct bpf_object *obj, |
| 34 | bpf_prog_iter_callback_t func, void *arg); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 35 | #else |
| 36 | static inline struct bpf_object * |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame^] | 37 | bpf__prepare_load(const char *filename __maybe_unused, |
| 38 | bool source __maybe_unused) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 39 | { |
| 40 | pr_debug("ERROR: eBPF object loading is disabled during compiling.\n"); |
| 41 | return ERR_PTR(-ENOTSUP); |
| 42 | } |
| 43 | |
| 44 | static inline void bpf__clear(void) { } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 45 | |
| 46 | static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;} |
| 47 | static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;} |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 48 | static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; } |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 49 | |
| 50 | static inline int |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 51 | bpf__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 | |
| 58 | static inline int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 59 | __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 | |
| 70 | static inline int |
| 71 | bpf__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 Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 77 | |
| 78 | static 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 Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 84 | #endif |
| 85 | #endif |