xref: /linux/tools/testing/selftests/bpf/progs/test_usdt.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 
8 int my_pid;
9 
10 int usdt0_called;
11 u64 usdt0_cookie;
12 int usdt0_arg_cnt;
13 int usdt0_arg_ret;
14 
15 SEC("usdt")
usdt0(struct pt_regs * ctx)16 int usdt0(struct pt_regs *ctx)
17 {
18 	long tmp;
19 
20 	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
21 		return 0;
22 
23 	__sync_fetch_and_add(&usdt0_called, 1);
24 
25 	usdt0_cookie = bpf_usdt_cookie(ctx);
26 	usdt0_arg_cnt = bpf_usdt_arg_cnt(ctx);
27 	/* should return -ENOENT for any arg_num */
28 	usdt0_arg_ret = bpf_usdt_arg(ctx, bpf_get_prandom_u32(), &tmp);
29 	return 0;
30 }
31 
32 int usdt3_called;
33 u64 usdt3_cookie;
34 int usdt3_arg_cnt;
35 int usdt3_arg_rets[3];
36 u64 usdt3_args[3];
37 
38 SEC("usdt//proc/self/exe:test:usdt3")
usdt3(struct pt_regs * ctx)39 int usdt3(struct pt_regs *ctx)
40 {
41 	long tmp;
42 
43 	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
44 		return 0;
45 
46 	__sync_fetch_and_add(&usdt3_called, 1);
47 
48 	usdt3_cookie = bpf_usdt_cookie(ctx);
49 	usdt3_arg_cnt = bpf_usdt_arg_cnt(ctx);
50 
51 	usdt3_arg_rets[0] = bpf_usdt_arg(ctx, 0, &tmp);
52 	usdt3_args[0] = (int)tmp;
53 
54 	usdt3_arg_rets[1] = bpf_usdt_arg(ctx, 1, &tmp);
55 	usdt3_args[1] = (long)tmp;
56 
57 	usdt3_arg_rets[2] = bpf_usdt_arg(ctx, 2, &tmp);
58 	usdt3_args[2] = (uintptr_t)tmp;
59 
60 	return 0;
61 }
62 
63 int usdt12_called;
64 u64 usdt12_cookie;
65 int usdt12_arg_cnt;
66 u64 usdt12_args[12];
67 
68 SEC("usdt//proc/self/exe:test:usdt12")
BPF_USDT(usdt12,int a1,int a2,long a3,long a4,unsigned a5,long a6,__u64 a7,uintptr_t a8,int a9,short a10,short a11,signed char a12)69 int BPF_USDT(usdt12, int a1, int a2, long a3, long a4, unsigned a5,
70 		     long a6, __u64 a7, uintptr_t a8, int a9, short a10,
71 		     short a11, signed char a12)
72 {
73 	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
74 		return 0;
75 
76 	__sync_fetch_and_add(&usdt12_called, 1);
77 
78 	usdt12_cookie = bpf_usdt_cookie(ctx);
79 	usdt12_arg_cnt = bpf_usdt_arg_cnt(ctx);
80 
81 	usdt12_args[0] = a1;
82 	usdt12_args[1] = a2;
83 	usdt12_args[2] = a3;
84 	usdt12_args[3] = a4;
85 	usdt12_args[4] = a5;
86 	usdt12_args[5] = a6;
87 	usdt12_args[6] = a7;
88 	usdt12_args[7] = a8;
89 	usdt12_args[8] = a9;
90 	usdt12_args[9] = a10;
91 	usdt12_args[10] = a11;
92 	usdt12_args[11] = a12;
93 	return 0;
94 }
95 
96 char _license[] SEC("license") = "GPL";
97