Skip to content

Commit b8c54ea

Browse files
Alexei Starovoitovborkmann
authored andcommitted
libbpf: Add support to attach to fentry/fexit tracing progs
Teach libbpf to recognize tracing programs types and attach them to fentry/fexit. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Song Liu <songliubraving@fb.com> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20191114185720.1641606-7-ast@kernel.org
1 parent 1442e28 commit b8c54ea

4 files changed

Lines changed: 82 additions & 25 deletions

File tree

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ enum bpf_attach_type {
201201
BPF_CGROUP_GETSOCKOPT,
202202
BPF_CGROUP_SETSOCKOPT,
203203
BPF_TRACE_RAW_TP,
204+
BPF_TRACE_FENTRY,
205+
BPF_TRACE_FEXIT,
204206
__MAX_BPF_ATTACH_TYPE
205207
};
206208

‎tools/lib/bpf/libbpf.c‎

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,7 +3856,8 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)
38563856
return 0;
38573857
}
38583858

3859-
static int libbpf_attach_btf_id_by_name(const char *name, __u32 *btf_id);
3859+
static int libbpf_attach_btf_id_by_name(const char *name,
3860+
enum bpf_attach_type attach_type);
38603861

38613862
static struct bpf_object *
38623863
__bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
@@ -3910,7 +3911,6 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
39103911
bpf_object__for_each_program(prog, obj) {
39113912
enum bpf_prog_type prog_type;
39123913
enum bpf_attach_type attach_type;
3913-
__u32 btf_id;
39143914

39153915
err = libbpf_prog_type_by_name(prog->section_name, &prog_type,
39163916
&attach_type);
@@ -3923,10 +3923,11 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
39233923
bpf_program__set_type(prog, prog_type);
39243924
bpf_program__set_expected_attach_type(prog, attach_type);
39253925
if (prog_type == BPF_PROG_TYPE_TRACING) {
3926-
err = libbpf_attach_btf_id_by_name(prog->section_name, &btf_id);
3927-
if (err)
3926+
err = libbpf_attach_btf_id_by_name(prog->section_name,
3927+
attach_type);
3928+
if (err <= 0)
39283929
goto out;
3929-
prog->attach_btf_id = btf_id;
3930+
prog->attach_btf_id = err;
39303931
}
39313932
}
39323933

@@ -4935,6 +4936,10 @@ static const struct {
49354936
BPF_PROG_SEC("raw_tp/", BPF_PROG_TYPE_RAW_TRACEPOINT),
49364937
BPF_PROG_BTF("tp_btf/", BPF_PROG_TYPE_TRACING,
49374938
BPF_TRACE_RAW_TP),
4939+
BPF_PROG_BTF("fentry/", BPF_PROG_TYPE_TRACING,
4940+
BPF_TRACE_FENTRY),
4941+
BPF_PROG_BTF("fexit/", BPF_PROG_TYPE_TRACING,
4942+
BPF_TRACE_FEXIT),
49384943
BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
49394944
BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
49404945
BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
@@ -5052,43 +5057,56 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
50525057
}
50535058

50545059
#define BTF_PREFIX "btf_trace_"
5055-
static int libbpf_attach_btf_id_by_name(const char *name, __u32 *btf_id)
5060+
int libbpf_find_vmlinux_btf_id(const char *name,
5061+
enum bpf_attach_type attach_type)
50565062
{
50575063
struct btf *btf = bpf_core_find_kernel_btf();
5058-
char raw_tp_btf_name[128] = BTF_PREFIX;
5059-
char *dst = raw_tp_btf_name + sizeof(BTF_PREFIX) - 1;
5060-
int ret, i, err = -EINVAL;
5064+
char raw_tp_btf[128] = BTF_PREFIX;
5065+
char *dst = raw_tp_btf + sizeof(BTF_PREFIX) - 1;
5066+
const char *btf_name;
5067+
int err = -EINVAL;
5068+
u32 kind;
50615069

50625070
if (IS_ERR(btf)) {
50635071
pr_warn("vmlinux BTF is not found\n");
50645072
return -EINVAL;
50655073
}
50665074

5075+
if (attach_type == BPF_TRACE_RAW_TP) {
5076+
/* prepend "btf_trace_" prefix per kernel convention */
5077+
strncat(dst, name, sizeof(raw_tp_btf) - sizeof(BTF_PREFIX));
5078+
btf_name = raw_tp_btf;
5079+
kind = BTF_KIND_TYPEDEF;
5080+
} else {
5081+
btf_name = name;
5082+
kind = BTF_KIND_FUNC;
5083+
}
5084+
err = btf__find_by_name_kind(btf, btf_name, kind);
5085+
btf__free(btf);
5086+
return err;
5087+
}
5088+
5089+
static int libbpf_attach_btf_id_by_name(const char *name,
5090+
enum bpf_attach_type attach_type)
5091+
{
5092+
int i, err;
5093+
50675094
if (!name)
5068-
goto out;
5095+
return -EINVAL;
50695096

50705097
for (i = 0; i < ARRAY_SIZE(section_names); i++) {
50715098
if (!section_names[i].is_attach_btf)
50725099
continue;
50735100
if (strncmp(name, section_names[i].sec, section_names[i].len))
50745101
continue;
5075-
/* prepend "btf_trace_" prefix per kernel convention */
5076-
strncat(dst, name + section_names[i].len,
5077-
sizeof(raw_tp_btf_name) - sizeof(BTF_PREFIX));
5078-
ret = btf__find_by_name(btf, raw_tp_btf_name);
5079-
if (ret <= 0) {
5080-
pr_warn("%s is not found in vmlinux BTF\n", dst);
5081-
goto out;
5082-
}
5083-
*btf_id = ret;
5084-
err = 0;
5085-
goto out;
5102+
err = libbpf_find_vmlinux_btf_id(name + section_names[i].len,
5103+
attach_type);
5104+
if (err <= 0)
5105+
pr_warn("%s is not found in vmlinux BTF\n", name);
5106+
return err;
50865107
}
50875108
pr_warn("failed to identify btf_id based on ELF section name '%s'\n", name);
5088-
err = -ESRCH;
5089-
out:
5090-
btf__free(btf);
5091-
return err;
5109+
return -ESRCH;
50925110
}
50935111

50945112
int libbpf_attach_type_by_name(const char *name,
@@ -5716,6 +5734,37 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
57165734
return (struct bpf_link *)link;
57175735
}
57185736

5737+
struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog)
5738+
{
5739+
char errmsg[STRERR_BUFSIZE];
5740+
struct bpf_link_fd *link;
5741+
int prog_fd, pfd;
5742+
5743+
prog_fd = bpf_program__fd(prog);
5744+
if (prog_fd < 0) {
5745+
pr_warn("program '%s': can't attach before loaded\n",
5746+
bpf_program__title(prog, false));
5747+
return ERR_PTR(-EINVAL);
5748+
}
5749+
5750+
link = malloc(sizeof(*link));
5751+
if (!link)
5752+
return ERR_PTR(-ENOMEM);
5753+
link->link.destroy = &bpf_link__destroy_fd;
5754+
5755+
pfd = bpf_raw_tracepoint_open(NULL, prog_fd);
5756+
if (pfd < 0) {
5757+
pfd = -errno;
5758+
free(link);
5759+
pr_warn("program '%s': failed to attach to trace: %s\n",
5760+
bpf_program__title(prog, false),
5761+
libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
5762+
return ERR_PTR(pfd);
5763+
}
5764+
link->fd = pfd;
5765+
return (struct bpf_link *)link;
5766+
}
5767+
57195768
enum bpf_perf_event_ret
57205769
bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
57215770
void **copy_mem, size_t *copy_size,

‎tools/lib/bpf/libbpf.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
188188
enum bpf_attach_type *expected_attach_type);
189189
LIBBPF_API int libbpf_attach_type_by_name(const char *name,
190190
enum bpf_attach_type *attach_type);
191+
LIBBPF_API int libbpf_find_vmlinux_btf_id(const char *name,
192+
enum bpf_attach_type attach_type);
191193

192194
/* Accessors of bpf_program */
193195
struct bpf_program;
@@ -251,6 +253,8 @@ LIBBPF_API struct bpf_link *
251253
bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
252254
const char *tp_name);
253255

256+
LIBBPF_API struct bpf_link *
257+
bpf_program__attach_trace(struct bpf_program *prog);
254258
struct bpf_insn;
255259

256260
/*

‎tools/lib/bpf/libbpf.map‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,12 @@ LIBBPF_0.0.6 {
199199
bpf_map__set_pin_path;
200200
bpf_object__open_file;
201201
bpf_object__open_mem;
202+
bpf_program__attach_trace;
202203
bpf_program__get_expected_attach_type;
203204
bpf_program__get_type;
204205
bpf_program__is_tracing;
205206
bpf_program__set_tracing;
206207
bpf_program__size;
207208
btf__find_by_name_kind;
209+
libbpf_find_vmlinux_btf_id;
208210
} LIBBPF_0.0.5;

0 commit comments

Comments
 (0)