xref: /linux/tools/testing/selftests/bpf/progs/uretprobe_stack.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/usdt.bpf.h>
7 
8 char _license[] SEC("license") = "GPL";
9 
10 __u64 entry_stack1[32], exit_stack1[32];
11 __u64 entry_stack1_recur[32], exit_stack1_recur[32];
12 __u64 entry_stack2[32];
13 __u64 entry_stack3[32];
14 __u64 entry_stack4[32], exit_stack4[32];
15 __u64 usdt_stack[32];
16 
17 int entry1_len, exit1_len;
18 int entry1_recur_len, exit1_recur_len;
19 int entry2_len, exit2_len;
20 int entry3_len, exit3_len;
21 int entry4_len, exit4_len;
22 int usdt_len;
23 
24 #define SZ sizeof(usdt_stack)
25 
26 SEC("uprobe//proc/self/exe:target_1")
27 int BPF_UPROBE(uprobe_1)
28 {
29 	/* target_1 is recursive wit depth of 2, so we capture two separate
30 	 * stack traces, depending on which occurence it is
31 	 */
32 	static bool recur = false;
33 
34 	if (!recur)
35 		entry1_len = bpf_get_stack(ctx, &entry_stack1, SZ, BPF_F_USER_STACK);
36 	else
37 		entry1_recur_len = bpf_get_stack(ctx, &entry_stack1_recur, SZ, BPF_F_USER_STACK);
38 
39 	recur = true;
40 	return 0;
41 }
42 
43 SEC("uretprobe//proc/self/exe:target_1")
44 int BPF_URETPROBE(uretprobe_1)
45 {
46 	/* see above, target_1 is recursive */
47 	static bool recur = false;
48 
49 	/* NOTE: order of returns is reversed to order of entries */
50 	if (!recur)
51 		exit1_recur_len = bpf_get_stack(ctx, &exit_stack1_recur, SZ, BPF_F_USER_STACK);
52 	else
53 		exit1_len = bpf_get_stack(ctx, &exit_stack1, SZ, BPF_F_USER_STACK);
54 
55 	recur = true;
56 	return 0;
57 }
58 
59 SEC("uprobe//proc/self/exe:target_2")
60 int BPF_UPROBE(uprobe_2)
61 {
62 	entry2_len = bpf_get_stack(ctx, &entry_stack2, SZ, BPF_F_USER_STACK);
63 	return 0;
64 }
65 
66 /* no uretprobe for target_2 */
67 
68 SEC("uprobe//proc/self/exe:target_3")
69 int BPF_UPROBE(uprobe_3)
70 {
71 	entry3_len = bpf_get_stack(ctx, &entry_stack3, SZ, BPF_F_USER_STACK);
72 	return 0;
73 }
74 
75 /* no uretprobe for target_3 */
76 
77 SEC("uprobe//proc/self/exe:target_4")
78 int BPF_UPROBE(uprobe_4)
79 {
80 	entry4_len = bpf_get_stack(ctx, &entry_stack4, SZ, BPF_F_USER_STACK);
81 	return 0;
82 }
83 
84 SEC("uretprobe//proc/self/exe:target_4")
85 int BPF_URETPROBE(uretprobe_4)
86 {
87 	exit4_len = bpf_get_stack(ctx, &exit_stack4, SZ, BPF_F_USER_STACK);
88 	return 0;
89 }
90 
91 SEC("usdt//proc/self/exe:uretprobe_stack:target")
92 int BPF_USDT(usdt_probe)
93 {
94 	usdt_len = bpf_get_stack(ctx, &usdt_stack, SZ, BPF_F_USER_STACK);
95 	return 0;
96 }
97