bpf-loader.h 3.2 KB
Newer Older
W
Wang Nan 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
 * Copyright (C) 2015, Huawei Inc.
 */
#ifndef __BPF_LOADER_H
#define __BPF_LOADER_H

#include <linux/compiler.h>
#include <linux/err.h>
#include <string.h>
11
#include <bpf/libbpf.h>
12
#include "probe-event.h"
W
Wang Nan 已提交
13 14
#include "debug.h"

15 16 17 18 19 20 21 22 23 24 25
enum bpf_loader_errno {
	__BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
	/* Invalid config string */
	BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
	BPF_LOADER_ERRNO__GROUP,	/* Invalid group name */
	BPF_LOADER_ERRNO__EVENTNAME,	/* Event name is missing */
	BPF_LOADER_ERRNO__INTERNAL,	/* BPF loader internal error */
	BPF_LOADER_ERRNO__COMPILE,	/* Error when compiling BPF scriptlet */
	__BPF_LOADER_ERRNO__END,
};

W
Wang Nan 已提交
26
struct bpf_object;
27
#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
W
Wang Nan 已提交
28

29 30 31
typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
					int fd, void *arg);

W
Wang Nan 已提交
32
#ifdef HAVE_LIBBPF_SUPPORT
33
struct bpf_object *bpf__prepare_load(const char *filename, bool source);
34 35
int bpf__strerror_prepare_load(const char *filename, bool source,
			       int err, char *buf, size_t size);
W
Wang Nan 已提交
36

W
Wang Nan 已提交
37 38 39
struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
					    const char *name);

W
Wang Nan 已提交
40
void bpf__clear(void);
41 42 43 44 45 46

int bpf__probe(struct bpf_object *obj);
int bpf__unprobe(struct bpf_object *obj);
int bpf__strerror_probe(struct bpf_object *obj, int err,
			char *buf, size_t size);

47 48 49
int bpf__load(struct bpf_object *obj);
int bpf__strerror_load(struct bpf_object *obj, int err,
		       char *buf, size_t size);
50 51
int bpf__foreach_tev(struct bpf_object *obj,
		     bpf_prog_iter_callback_t func, void *arg);
W
Wang Nan 已提交
52 53
#else
static inline struct bpf_object *
54 55
bpf__prepare_load(const char *filename __maybe_unused,
		  bool source __maybe_unused)
W
Wang Nan 已提交
56 57 58 59 60
{
	pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
	return ERR_PTR(-ENOTSUP);
}

W
Wang Nan 已提交
61 62 63 64 65 66 67
static inline struct bpf_object *
bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
					   size_t obj_buf_sz __maybe_unused)
{
	return ERR_PTR(-ENOTSUP);
}

W
Wang Nan 已提交
68
static inline void bpf__clear(void) { }
69 70 71

static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
72
static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
73

74 75 76 77 78 79 80 81
static inline int
bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
		 bpf_prog_iter_callback_t func __maybe_unused,
		 void *arg __maybe_unused)
{
	return 0;
}

82 83 84 85 86 87 88 89 90 91 92 93
static inline int
__bpf_strerror(char *buf, size_t size)
{
	if (!size)
		return 0;
	strncpy(buf,
		"ERROR: eBPF object loading is disabled during compiling.\n",
		size);
	buf[size - 1] = '\0';
	return 0;
}

94 95 96 97 98 99 100 101 102
static inline
int bpf__strerror_prepare_load(const char *filename __maybe_unused,
			       bool source __maybe_unused,
			       int err __maybe_unused,
			       char *buf, size_t size)
{
	return __bpf_strerror(buf, size);
}

103 104 105 106 107 108 109
static inline int
bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
		    int err __maybe_unused,
		    char *buf, size_t size)
{
	return __bpf_strerror(buf, size);
}
110 111 112 113 114 115 116

static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
				     int err __maybe_unused,
				     char *buf, size_t size)
{
	return __bpf_strerror(buf, size);
}
W
Wang Nan 已提交
117 118
#endif
#endif