1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <test_progs.h>
4 #include <linux/ptrace.h>
5 #include "test_task_pt_regs.skel.h"
6 
7 void test_task_pt_regs(void)
8 {
9 	struct test_task_pt_regs *skel;
10 	struct bpf_link *uprobe_link;
11 	size_t uprobe_offset;
12 	ssize_t base_addr;
13 	bool match;
14 
15 	base_addr = get_base_addr();
16 	if (!ASSERT_GT(base_addr, 0, "get_base_addr"))
17 		return;
18 	uprobe_offset = get_uprobe_offset(&get_base_addr, base_addr);
19 
20 	skel = test_task_pt_regs__open_and_load();
21 	if (!ASSERT_OK_PTR(skel, "skel_open"))
22 		return;
23 	if (!ASSERT_OK_PTR(skel->bss, "check_bss"))
24 		goto cleanup;
25 
26 	uprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uprobe,
27 						 false /* retprobe */,
28 						 0 /* self pid */,
29 						 "/proc/self/exe",
30 						 uprobe_offset);
31 	if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe"))
32 		goto cleanup;
33 	skel->links.handle_uprobe = uprobe_link;
34 
35 	/* trigger & validate uprobe */
36 	get_base_addr();
37 
38 	if (!ASSERT_EQ(skel->bss->uprobe_res, 1, "check_uprobe_res"))
39 		goto cleanup;
40 
41 	match = !memcmp(&skel->bss->current_regs, &skel->bss->ctx_regs,
42 			sizeof(skel->bss->current_regs));
43 	ASSERT_TRUE(match, "check_regs_match");
44 
45 cleanup:
46 	test_task_pt_regs__destroy(skel);
47 }
48