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 int usdt0_arg_size; 15 16 SEC("usdt") 17 int 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 34 int usdt3_called; 35 u64 usdt3_cookie; 36 int usdt3_arg_cnt; 37 int usdt3_arg_rets[3]; 38 u64 usdt3_args[3]; 39 int usdt3_arg_sizes[3]; 40 41 SEC("usdt//proc/self/exe:test:usdt3") 42 int 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 69 int usdt12_called; 70 u64 usdt12_cookie; 71 int usdt12_arg_cnt; 72 u64 usdt12_args[12]; 73 int usdt12_arg_sizes[12]; 74 75 SEC("usdt//proc/self/exe:test:usdt12") 76 int 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 110 int usdt_sib_called; 111 u64 usdt_sib_cookie; 112 int usdt_sib_arg_cnt; 113 int usdt_sib_arg_ret; 114 short usdt_sib_arg; 115 int 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 */ 121 SEC("usdt") 122 int 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 142 int executed; 143 unsigned long expected_ip; 144 145 SEC("usdt") 146 int usdt_executed(struct pt_regs *ctx) 147 { 148 if (expected_ip == ctx->ip) 149 executed++; 150 return 0; 151 } 152 153 int arg_total; 154 int arg_bad; 155 long arg_last[3]; 156 long expected_arg[3]; 157 int expected_pid; 158 159 SEC("usdt") 160 int BPF_USDT(usdt_check_arg, long arg1, long arg2, long arg3) 161 { 162 if (expected_pid != (bpf_get_current_pid_tgid() >> 32)) 163 return 0; 164 165 __sync_fetch_and_add(&arg_total, 1); 166 arg_last[0] = arg1; 167 arg_last[1] = arg2; 168 arg_last[2] = arg3; 169 170 if (arg1 != expected_arg[0] || 171 arg2 != expected_arg[1] || 172 arg3 != expected_arg[2]) 173 __sync_fetch_and_add(&arg_bad, 1); 174 175 return 0; 176 } 177 #endif 178 char _license[] SEC("license") = "GPL"; 179