Skip to content

Commit 84bf5e1

Browse files
anakryikoborkmann
authored andcommitted
libbpf: add raw tracepoint attach API
Add a wrapper utilizing bpf_link "infrastructure" to allow attaching BPF programs to raw tracepoints. Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Song Liu <[email protected]> Reviewed-by: Stanislav Fomichev <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent f6de59c commit 84bf5e1

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,6 +4269,45 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
42694269
return link;
42704270
}
42714271

4272+
static int bpf_link__destroy_fd(struct bpf_link *link)
4273+
{
4274+
struct bpf_link_fd *l = (void *)link;
4275+
4276+
return close(l->fd);
4277+
}
4278+
4279+
struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
4280+
const char *tp_name)
4281+
{
4282+
char errmsg[STRERR_BUFSIZE];
4283+
struct bpf_link_fd *link;
4284+
int prog_fd, pfd;
4285+
4286+
prog_fd = bpf_program__fd(prog);
4287+
if (prog_fd < 0) {
4288+
pr_warning("program '%s': can't attach before loaded\n",
4289+
bpf_program__title(prog, false));
4290+
return ERR_PTR(-EINVAL);
4291+
}
4292+
4293+
link = malloc(sizeof(*link));
4294+
if (!link)
4295+
return ERR_PTR(-ENOMEM);
4296+
link->link.destroy = &bpf_link__destroy_fd;
4297+
4298+
pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
4299+
if (pfd < 0) {
4300+
pfd = -errno;
4301+
free(link);
4302+
pr_warning("program '%s': failed to attach to raw tracepoint '%s': %s\n",
4303+
bpf_program__title(prog, false), tp_name,
4304+
libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
4305+
return ERR_PTR(pfd);
4306+
}
4307+
link->fd = pfd;
4308+
return (struct bpf_link *)link;
4309+
}
4310+
42724311
enum bpf_perf_event_ret
42734312
bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
42744313
void **copy_mem, size_t *copy_size,

tools/lib/bpf/libbpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ LIBBPF_API struct bpf_link *
182182
bpf_program__attach_tracepoint(struct bpf_program *prog,
183183
const char *tp_category,
184184
const char *tp_name);
185+
LIBBPF_API struct bpf_link *
186+
bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
187+
const char *tp_name);
185188

186189
struct bpf_insn;
187190

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ LIBBPF_0.0.4 {
171171
bpf_object__load_xattr;
172172
bpf_program__attach_kprobe;
173173
bpf_program__attach_perf_event;
174+
bpf_program__attach_raw_tracepoint;
174175
bpf_program__attach_tracepoint;
175176
bpf_program__attach_uprobe;
176177
btf_dump__dump_type;

0 commit comments

Comments
 (0)