Skip to content

Commit

Permalink
selftests/bpf: Add selftest for dynamic tracepoint
Browse files Browse the repository at this point in the history
The result is as follows,

  $ tools/testing/selftests/bpf/test_progs --name=dynamic_tp
  torvalds#85      dynamic_tp:OK
  Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
  • Loading branch information
laoar authored and intel-lab-lkp committed Jan 12, 2025
1 parent a27e9e7 commit efa3043
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/test_dynamic_tp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: GPL-2.0-only

#include <test_progs.h>
#include <bpf/btf.h>
#include <bpf/bpf.h>

#include "dynamic_tp.skel.h"

int dynamic_tp(const char *cmd)
{
const char *kprobe_file = "/sys/kernel/debug/tracing/kprobe_events";
ssize_t bytes_written;
int fd, err;

fd = open(kprobe_file, O_WRONLY | O_APPEND);
if (!ASSERT_GE(fd, 0, "open kprobe_events"))
return -1;

bytes_written = write(fd, cmd, strlen(cmd));
if (!ASSERT_GT(bytes_written, 0, "write kprobe_events")) {
close(fd);
return -1;
}

err = close(fd);
if (!ASSERT_OK(err, "close kprobe_events"))
return -1;
return 0;
}

void test_dynamic_tp(void)
{
struct dynamic_tp *skel;
pid_t child_pid;
int status, err;

/* create a dynamic tracepoint */
err = dynamic_tp("p:my_dynamic_tp kernel_clone");
if (!ASSERT_OK(err, "create dynamic tp"))
return;

skel = dynamic_tp__open_and_load();
if (!ASSERT_OK_PTR(skel, "load progs"))
goto remove_tp;
skel->bss->pid = getpid();
err = dynamic_tp__attach(skel);
if (!ASSERT_OK(err, "attach progs"))
goto cleanup;

/* trigger the dynamic tracepoint */
child_pid = fork();
if (!ASSERT_GT(child_pid, -1, "child_pid"))
goto cleanup;
if (child_pid == 0)
_exit(0);
waitpid(child_pid, &status, 0);

ASSERT_EQ(skel->bss->result, 1, "result");

cleanup:
dynamic_tp__destroy(skel);
remove_tp:
dynamic_tp("-:my_dynamic_tp kernel_clone");
}
27 changes: 27 additions & 0 deletions tools/testing/selftests/bpf/progs/dynamic_tp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-2.0

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

char _license[] SEC("license") = "GPL";

#define MAX_STACK_TRACE_DEPTH 32
unsigned long entries[MAX_STACK_TRACE_DEPTH] = {};
#define SIZE_OF_ULONG (sizeof(unsigned long))

int result, pid;

SEC("kprobe/kprobes/my_dynamic_tp")
int dynamic_tp(struct pt_regs *ctx)
{
int ret;

ret = bpf_get_stack(ctx, entries, MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, 0);
if (ret < 0) {
result = -1;
return ret;
}
if (bpf_get_current_pid_tgid() >> 32 == pid)
result = 1;
return 0;
}

0 comments on commit efa3043

Please sign in to comment.