xref: /linux/tools/testing/selftests/bpf/progs/test_attach_probe.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Facebook
3 
4 #include <linux/ptrace.h>
5 #include <linux/bpf.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 #include "bpf_misc.h"
9 
10 int kprobe_res = 0;
11 int kprobe2_res = 0;
12 int kretprobe_res = 0;
13 int kretprobe2_res = 0;
14 int uprobe_res = 0;
15 int uretprobe_res = 0;
16 int uprobe_byname_res = 0;
17 int uretprobe_byname_res = 0;
18 int uprobe_byname2_res = 0;
19 int uretprobe_byname2_res = 0;
20 
21 SEC("kprobe")
22 int handle_kprobe(struct pt_regs *ctx)
23 {
24 	kprobe_res = 1;
25 	return 0;
26 }
27 
28 SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
29 int BPF_KPROBE(handle_kprobe_auto)
30 {
31 	kprobe2_res = 11;
32 	return 0;
33 }
34 
35 SEC("kretprobe")
36 int handle_kretprobe(struct pt_regs *ctx)
37 {
38 	kretprobe_res = 2;
39 	return 0;
40 }
41 
42 SEC("kretprobe/" SYS_PREFIX "sys_nanosleep")
43 int BPF_KRETPROBE(handle_kretprobe_auto)
44 {
45 	kretprobe2_res = 22;
46 	return 0;
47 }
48 
49 SEC("uprobe")
50 int handle_uprobe(struct pt_regs *ctx)
51 {
52 	uprobe_res = 3;
53 	return 0;
54 }
55 
56 SEC("uretprobe")
57 int handle_uretprobe(struct pt_regs *ctx)
58 {
59 	uretprobe_res = 4;
60 	return 0;
61 }
62 
63 SEC("uprobe")
64 int handle_uprobe_byname(struct pt_regs *ctx)
65 {
66 	uprobe_byname_res = 5;
67 	return 0;
68 }
69 
70 /* use auto-attach format for section definition. */
71 SEC("uretprobe//proc/self/exe:trigger_func2")
72 int handle_uretprobe_byname(struct pt_regs *ctx)
73 {
74 	uretprobe_byname_res = 6;
75 	return 0;
76 }
77 
78 SEC("uprobe")
79 int handle_uprobe_byname2(struct pt_regs *ctx)
80 {
81 	unsigned int size = PT_REGS_PARM1(ctx);
82 
83 	/* verify malloc size */
84 	if (size == 1)
85 		uprobe_byname2_res = 7;
86 	return 0;
87 }
88 
89 SEC("uretprobe")
90 int handle_uretprobe_byname2(struct pt_regs *ctx)
91 {
92 	uretprobe_byname2_res = 8;
93 	return 0;
94 }
95 
96 char _license[] SEC("license") = "GPL";
97