forked from libbpf/libbpf-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new example to demonstrate the usage of libbpf USDT functionality. Closes: libbpf#80 Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
- Loading branch information
1 parent
2b5b2bb
commit 4f0d020
Showing
5 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
/.output | ||
/bootstrap | ||
/minimal | ||
/minimal_legacy | ||
/uprobe | ||
/kprobe | ||
/fentry | ||
/profile | ||
/usdt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
/* Copyright (c) 2022 Hengqi Chen */ | ||
#include <vmlinux.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/usdt.bpf.h> | ||
|
||
pid_t my_pid; | ||
|
||
SEC("usdt/libc.so.6:libc:setjmp") | ||
int BPF_USDT(usdt_auto_attach, void *arg1, int arg2, void *arg3) | ||
{ | ||
pid_t pid = bpf_get_current_pid_tgid() >> 32; | ||
|
||
if (pid != my_pid) | ||
return 0; | ||
|
||
bpf_printk("USDT auto attach to libc:setjmp: arg1 = %lx, arg2 = %d, arg3 = %lx", arg1, arg2, arg3); | ||
return 0; | ||
} | ||
|
||
SEC("usdt") | ||
int BPF_USDT(usdt_manual_attach, void *arg1, int arg2, void *arg3) | ||
{ | ||
bpf_printk("USDT manual attach to libc:setjmp: arg1 = %lx, arg2 = %d, arg3 = %lx", arg1, arg2, arg3); | ||
return 0; | ||
} | ||
|
||
char LICENSE[] SEC("license") = "Dual BSD/GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
/* Copyright (c) 2022 Hengqi Chen */ | ||
#include <signal.h> | ||
#include <unistd.h> | ||
#include <setjmp.h> | ||
#include <linux/limits.h> | ||
#include "usdt.skel.h" | ||
|
||
static volatile sig_atomic_t exiting; | ||
static jmp_buf env; | ||
|
||
static void sig_int(int signo) | ||
{ | ||
exiting = 1; | ||
} | ||
|
||
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | ||
{ | ||
return vfprintf(stderr, format, args); | ||
} | ||
|
||
static void usdt_trigger() { | ||
setjmp(env); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct usdt_bpf *skel; | ||
int err; | ||
|
||
libbpf_set_strict_mode(LIBBPF_STRICT_ALL); | ||
libbpf_set_print(libbpf_print_fn); | ||
|
||
skel = usdt_bpf__open(); | ||
if (!skel) { | ||
fprintf(stderr, "Failed to open BPF skeleton\n"); | ||
return 1; | ||
} | ||
|
||
skel->bss->my_pid = getpid(); | ||
|
||
err = usdt_bpf__load(skel); | ||
if (!skel) { | ||
fprintf(stderr, "Failed to load BPF skeleton\n"); | ||
return 1; | ||
} | ||
|
||
/* | ||
* Manually attach to libc.so we find. | ||
* We specify pid here, so we don't have to do pid filtering in BPF program. | ||
*/ | ||
skel->links.usdt_manual_attach = bpf_program__attach_usdt(skel->progs.usdt_manual_attach, getpid(), | ||
"libc.so.6", "libc", "setjmp", NULL); | ||
if (!skel->links.usdt_manual_attach) { | ||
err = errno; | ||
fprintf(stderr, "Failed to attach BPF program `usdt_manual_attach`\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* | ||
* Auto attach by libbpf, libbpf should be able to find libc.so in your system. | ||
* By default, auto attach does NOT specify pid, so we do pid filtering in BPF program | ||
*/ | ||
err = usdt_bpf__attach(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to attach BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
if (signal(SIGINT, sig_int) == SIG_ERR) { | ||
err = errno; | ||
fprintf(stderr, "can't set signal handler: %s\n", strerror(errno)); | ||
goto cleanup; | ||
} | ||
|
||
printf("Successfully started! Please run `sudo cat /sys/kernel/debug/tracing/trace_pipe` " | ||
"to see output of the BPF programs.\n"); | ||
|
||
while (!exiting) { | ||
/* trigger our BPF programs */ | ||
usdt_trigger(); | ||
fprintf(stderr, "."); | ||
sleep(1); | ||
} | ||
|
||
cleanup: | ||
usdt_bpf__destroy(skel); | ||
return -err; | ||
} |