1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 
4 #include "vmlinux.h"
5 #include <asm/unistd.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 #include <bpf/bpf_core_read.h>
9 
10 #define MY_TV_NSEC 1337
11 
12 bool tp_called = false;
13 bool raw_tp_called = false;
14 bool tp_btf_called = false;
15 bool kprobe_called = false;
16 bool fentry_called = false;
17 
18 SEC("tp/syscalls/sys_enter_nanosleep")
19 int handle__tp(struct trace_event_raw_sys_enter *args)
20 {
21 	struct __kernel_timespec *ts;
22 
23 	if (args->id != __NR_nanosleep)
24 		return 0;
25 
26 	ts = (void *)args->args[0];
27 	if (BPF_CORE_READ(ts, tv_nsec) != MY_TV_NSEC)
28 		return 0;
29 
30 	tp_called = true;
31 	return 0;
32 }
33 
34 SEC("raw_tp/sys_enter")
35 int BPF_PROG(handle__raw_tp, struct pt_regs *regs, long id)
36 {
37 	struct __kernel_timespec *ts;
38 
39 	if (id != __NR_nanosleep)
40 		return 0;
41 
42 	ts = (void *)PT_REGS_PARM1_CORE(regs);
43 	if (BPF_CORE_READ(ts, tv_nsec) != MY_TV_NSEC)
44 		return 0;
45 
46 	raw_tp_called = true;
47 	return 0;
48 }
49 
50 SEC("tp_btf/sys_enter")
51 int BPF_PROG(handle__tp_btf, struct pt_regs *regs, long id)
52 {
53 	struct __kernel_timespec *ts;
54 
55 	if (id != __NR_nanosleep)
56 		return 0;
57 
58 	ts = (void *)PT_REGS_PARM1_CORE(regs);
59 	if (BPF_CORE_READ(ts, tv_nsec) != MY_TV_NSEC)
60 		return 0;
61 
62 	tp_btf_called = true;
63 	return 0;
64 }
65 
66 SEC("kprobe/hrtimer_nanosleep")
67 int BPF_KPROBE(handle__kprobe,
68 	       ktime_t rqtp, enum hrtimer_mode mode, clockid_t clockid)
69 {
70 	if (rqtp == MY_TV_NSEC)
71 		kprobe_called = true;
72 	return 0;
73 }
74 
75 SEC("fentry/hrtimer_nanosleep")
76 int BPF_PROG(handle__fentry,
77 	     ktime_t rqtp, enum hrtimer_mode mode, clockid_t clockid)
78 {
79 	if (rqtp == MY_TV_NSEC)
80 		fentry_called = true;
81 	return 0;
82 }
83 
84 char _license[] SEC("license") = "GPL";
85