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