1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 int my_tid;
9 
10 int kprobe_res;
11 int kprobe_multi_res;
12 int kretprobe_res;
13 int uprobe_res;
14 int uretprobe_res;
15 int tp_res;
16 int pe_res;
17 
18 static void update(void *ctx, int *res)
19 {
20 	if (my_tid != (u32)bpf_get_current_pid_tgid())
21 		return;
22 
23 	*res |= bpf_get_attach_cookie(ctx);
24 }
25 
26 SEC("kprobe/sys_nanosleep")
27 int handle_kprobe(struct pt_regs *ctx)
28 {
29 	update(ctx, &kprobe_res);
30 	return 0;
31 }
32 
33 SEC("kretprobe/sys_nanosleep")
34 int handle_kretprobe(struct pt_regs *ctx)
35 {
36 	update(ctx, &kretprobe_res);
37 	return 0;
38 }
39 
40 SEC("uprobe/trigger_func")
41 int handle_uprobe(struct pt_regs *ctx)
42 {
43 	update(ctx, &uprobe_res);
44 	return 0;
45 }
46 
47 SEC("uretprobe/trigger_func")
48 int handle_uretprobe(struct pt_regs *ctx)
49 {
50 	update(ctx, &uretprobe_res);
51 	return 0;
52 }
53 
54 /* bpf_prog_array, used by kernel internally to keep track of attached BPF
55  * programs to a given BPF hook (e.g., for tracepoints) doesn't allow the same
56  * BPF program to be attached multiple times. So have three identical copies
57  * ready to attach to the same tracepoint.
58  */
59 SEC("tp/syscalls/sys_enter_nanosleep")
60 int handle_tp1(struct pt_regs *ctx)
61 {
62 	update(ctx, &tp_res);
63 	return 0;
64 }
65 SEC("tp/syscalls/sys_enter_nanosleep")
66 int handle_tp2(struct pt_regs *ctx)
67 {
68 	update(ctx, &tp_res);
69 	return 0;
70 }
71 SEC("tp/syscalls/sys_enter_nanosleep")
72 int handle_tp3(void *ctx)
73 {
74 	update(ctx, &tp_res);
75 	return 1;
76 }
77 
78 SEC("perf_event")
79 int handle_pe(struct pt_regs *ctx)
80 {
81 	update(ctx, &pe_res);
82 	return 0;
83 }
84 
85 char _license[] SEC("license") = "GPL";
86