linux/tools/testing/selftests/bpf/progs/test_vmlinux.c
<<
>>
Prefs
   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
  12bool tp_called = false;
  13bool raw_tp_called = false;
  14bool tp_btf_called = false;
  15bool kprobe_called = false;
  16bool fentry_called = false;
  17
  18SEC("tp/syscalls/sys_enter_nanosleep")
  19int 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
  34SEC("raw_tp/sys_enter")
  35int 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
  50SEC("tp_btf/sys_enter")
  51int 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
  66SEC("kprobe/hrtimer_start_range_ns")
  67int BPF_KPROBE(handle__kprobe, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
  68               const enum hrtimer_mode mode)
  69{
  70        if (tim == MY_TV_NSEC)
  71                kprobe_called = true;
  72        return 0;
  73}
  74
  75SEC("fentry/hrtimer_start_range_ns")
  76int BPF_PROG(handle__fentry, struct hrtimer *timer, ktime_t tim, u64 delta_ns,
  77             const enum hrtimer_mode mode)
  78{
  79        if (tim == MY_TV_NSEC)
  80                fentry_called = true;
  81        return 0;
  82}
  83
  84char _license[] SEC("license") = "GPL";
  85