Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3
4#include "vmlinux.h"
5#include <bpf/bpf_helpers.h>
6#include <bpf/usdt.bpf.h>
7
8int my_pid;
9
10int usdt0_called;
11u64 usdt0_cookie;
12int usdt0_arg_cnt;
13int usdt0_arg_ret;
14int usdt0_arg_size;
15
16SEC("usdt")
17int usdt0(struct pt_regs *ctx)
18{
19 long tmp;
20
21 if (my_pid != (bpf_get_current_pid_tgid() >> 32))
22 return 0;
23
24 __sync_fetch_and_add(&usdt0_called, 1);
25
26 usdt0_cookie = bpf_usdt_cookie(ctx);
27 usdt0_arg_cnt = bpf_usdt_arg_cnt(ctx);
28 /* should return -ENOENT for any arg_num */
29 usdt0_arg_ret = bpf_usdt_arg(ctx, bpf_get_prandom_u32(), &tmp);
30 usdt0_arg_size = bpf_usdt_arg_size(ctx, bpf_get_prandom_u32());
31 return 0;
32}
33
34int usdt3_called;
35u64 usdt3_cookie;
36int usdt3_arg_cnt;
37int usdt3_arg_rets[3];
38u64 usdt3_args[3];
39int usdt3_arg_sizes[3];
40
41SEC("usdt//proc/self/exe:test:usdt3")
42int usdt3(struct pt_regs *ctx)
43{
44 long tmp;
45
46 if (my_pid != (bpf_get_current_pid_tgid() >> 32))
47 return 0;
48
49 __sync_fetch_and_add(&usdt3_called, 1);
50
51 usdt3_cookie = bpf_usdt_cookie(ctx);
52 usdt3_arg_cnt = bpf_usdt_arg_cnt(ctx);
53
54 usdt3_arg_rets[0] = bpf_usdt_arg(ctx, 0, &tmp);
55 usdt3_args[0] = (int)tmp;
56 usdt3_arg_sizes[0] = bpf_usdt_arg_size(ctx, 0);
57
58 usdt3_arg_rets[1] = bpf_usdt_arg(ctx, 1, &tmp);
59 usdt3_args[1] = (long)tmp;
60 usdt3_arg_sizes[1] = bpf_usdt_arg_size(ctx, 1);
61
62 usdt3_arg_rets[2] = bpf_usdt_arg(ctx, 2, &tmp);
63 usdt3_args[2] = (uintptr_t)tmp;
64 usdt3_arg_sizes[2] = bpf_usdt_arg_size(ctx, 2);
65
66 return 0;
67}
68
69int usdt12_called;
70u64 usdt12_cookie;
71int usdt12_arg_cnt;
72u64 usdt12_args[12];
73int usdt12_arg_sizes[12];
74
75SEC("usdt//proc/self/exe:test:usdt12")
76int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
77 long a6, __u64 a7, uintptr_t a8, int a9, short a10,
78 short a11, signed char a12)
79{
80 int i;
81
82 if (my_pid != (bpf_get_current_pid_tgid() >> 32))
83 return 0;
84
85 __sync_fetch_and_add(&usdt12_called, 1);
86
87 usdt12_cookie = bpf_usdt_cookie(ctx);
88 usdt12_arg_cnt = bpf_usdt_arg_cnt(ctx);
89
90 usdt12_args[0] = a1;
91 usdt12_args[1] = a2;
92 usdt12_args[2] = a3;
93 usdt12_args[3] = a4;
94 usdt12_args[4] = a5;
95 usdt12_args[5] = a6;
96 usdt12_args[6] = a7;
97 usdt12_args[7] = a8;
98 usdt12_args[8] = a9;
99 usdt12_args[9] = a10;
100 usdt12_args[10] = a11;
101 usdt12_args[11] = a12;
102
103 bpf_for(i, 0, 12) {
104 usdt12_arg_sizes[i] = bpf_usdt_arg_size(ctx, i);
105 }
106
107 return 0;
108}
109
110int usdt_sib_called;
111u64 usdt_sib_cookie;
112int usdt_sib_arg_cnt;
113int usdt_sib_arg_ret;
114short usdt_sib_arg;
115int usdt_sib_arg_size;
116
117/*
118 * usdt_sib is only tested on x86-related architectures, so it requires
119 * manual attach since auto-attach will panic tests under other architectures
120 */
121SEC("usdt")
122int usdt_sib(struct pt_regs *ctx)
123{
124 long tmp;
125
126 if (my_pid != (bpf_get_current_pid_tgid() >> 32))
127 return 0;
128
129 __sync_fetch_and_add(&usdt_sib_called, 1);
130
131 usdt_sib_cookie = bpf_usdt_cookie(ctx);
132 usdt_sib_arg_cnt = bpf_usdt_arg_cnt(ctx);
133
134 usdt_sib_arg_ret = bpf_usdt_arg(ctx, 0, &tmp);
135 usdt_sib_arg = (short)tmp;
136 usdt_sib_arg_size = bpf_usdt_arg_size(ctx, 0);
137
138 return 0;
139}
140
141#ifdef __TARGET_ARCH_x86
142int executed;
143unsigned long expected_ip;
144
145SEC("usdt")
146int usdt_executed(struct pt_regs *ctx)
147{
148 if (expected_ip == ctx->ip)
149 executed++;
150 return 0;
151}
152#endif
153char _license[] SEC("license") = "GPL";