Skip to content

Commit

Permalink
Add libbpf.c support for uprobes
Browse files Browse the repository at this point in the history
The base calling convention for uprobes is the same as kprobes, but just
the path in debug/tracing/ is slightly different. Add a new API for this
and slightly refactor the code.

Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Jan 28, 2016
1 parent 22c7f1b commit 68e2d14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
49 changes: 37 additions & 12 deletions src/cc/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path,
return -1;
}

void * bpf_attach_kprobe(int progfd, const char *event,
const char *event_desc, pid_t pid,
int cpu, int group_fd, perf_reader_cb cb,
void *cb_cookie) {
static void * bpf_attach_probe(int progfd, const char *event,
const char *event_desc, const char *event_type,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie) {
int kfd = -1;
char buf[256];
struct perf_reader *reader = NULL;
Expand All @@ -272,20 +272,21 @@ void * bpf_attach_kprobe(int progfd, const char *event,
if (!reader)
goto error;

kfd = open("/sys/kernel/debug/tracing/kprobe_events", O_WRONLY | O_APPEND, 0);
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type);
kfd = open(buf, O_WRONLY | O_APPEND, 0);
if (kfd < 0) {
perror("open(kprobe_events)");
fprintf(stderr, "open(%s): %s\n", buf, strerror(errno));
goto error;
}

if (write(kfd, event_desc, strlen(event_desc)) < 0) {
fprintf(stderr, "write of \"%s\" into kprobe_events failed: %s\n", event_desc, strerror(errno));
fprintf(stderr, "write(%s, \"%s\") failed: %s\n", buf, event_desc, strerror(errno));
if (errno == EINVAL)
fprintf(stderr, "check dmesg output for possible cause\n");
goto error;
}

snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/kprobes/%s", event);
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/%ss/%s", event_type, event);
if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0)
goto error;

Expand All @@ -300,17 +301,33 @@ void * bpf_attach_kprobe(int progfd, const char *event,
return NULL;
}

int bpf_detach_kprobe(const char *event_desc) {
void * bpf_attach_kprobe(int progfd, const char *event,
const char *event_desc,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie) {
return bpf_attach_probe(progfd, event, event_desc, "kprobe", pid, cpu, group_fd, cb, cb_cookie);
}

void * bpf_attach_uprobe(int progfd, const char *event,
const char *event_desc,
pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie) {
return bpf_attach_probe(progfd, event, event_desc, "uprobe", pid, cpu, group_fd, cb, cb_cookie);
}

static int bpf_detach_probe(const char *event_desc, const char *event_type) {
int kfd = -1;

kfd = open("/sys/kernel/debug/tracing/kprobe_events", O_WRONLY | O_APPEND, 0);
char buf[256];
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/%s_events", event_type);
kfd = open(buf, O_WRONLY | O_APPEND, 0);
if (kfd < 0) {
perror("open(kprobe_events)");
fprintf(stderr, "open(%s): %s\n", buf, strerror(errno));
goto error;
}

if (write(kfd, event_desc, strlen(event_desc)) < 0) {
perror("write(kprobe_events)");
fprintf(stderr, "write(%s): %s\n", buf, strerror(errno));
goto error;
}

Expand All @@ -323,6 +340,14 @@ int bpf_detach_kprobe(const char *event_desc) {
return -1;
}

int bpf_detach_kprobe(const char *event_desc) {
return bpf_detach_probe(event_desc, "kprobe");
}

int bpf_detach_uprobe(const char *event_desc) {
return bpf_detach_probe(event_desc, "uprobe");
}

void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, void *cb_cookie, int pid, int cpu) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0)
int pfd;
Expand Down
6 changes: 6 additions & 0 deletions src/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void * bpf_attach_kprobe(int progfd, const char *event, const char *event_desc,
int pid, int cpu, int group_fd, perf_reader_cb cb,
void *cb_cookie);
int bpf_detach_kprobe(const char *event_desc);

void * bpf_attach_uprobe(int progfd, const char *event, const char *event_desc,
int pid, int cpu, int group_fd, perf_reader_cb cb,
void *cb_cookie);
int bpf_detach_uprobe(const char *event_desc);

void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, void *cb_cookie, int pid, int cpu);

#define LOG_BUF_SIZE 65536
Expand Down

0 comments on commit 68e2d14

Please sign in to comment.