Skip to content

Commit d9f3d2f

Browse files
Phoenix500526Kernel Patches Daemon
authored andcommitted
libbpf: fix USDT SIB argument handling causing unrecognized register error
On x86-64, USDT arguments can be specified using Scale-Index-Base (SIB) addressing, e.g. "1@-96(%rbp,%rax,8)". The current USDT implementation in libbpf cannot parse this format, causing `bpf_program__attach_usdt()` to fail with -ENOENT (unrecognized register). This patch fixes this by implementing the necessary changes: - add correct handling for SIB-addressed arguments in `bpf_usdt_arg`. - add adaptive support to `__bpf_usdt_arg_type` and `__bpf_usdt_arg_spec` to represent SIB addressing parameters. Signed-off-by: Jiawei Zhao <[email protected]>
1 parent cffbb10 commit d9f3d2f

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed

tools/lib/bpf/usdt.bpf.h

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define __USDT_BPF_H__
55

66
#include <linux/errno.h>
7+
#include <asm/byteorder.h>
78
#include "bpf_helpers.h"
89
#include "bpf_tracing.h"
910

@@ -34,13 +35,34 @@ enum __bpf_usdt_arg_type {
3435
BPF_USDT_ARG_CONST,
3536
BPF_USDT_ARG_REG,
3637
BPF_USDT_ARG_REG_DEREF,
38+
BPF_USDT_ARG_SIB,
3739
};
3840

41+
/*
42+
* This struct layout is designed specifically to be backwards/forward
43+
* compatible between libbpf versions for ARG_CONST, ARG_REG, and
44+
* ARG_REG_DEREF modes. ARG_SIB requires libbpf v1.7+.
45+
*/
3946
struct __bpf_usdt_arg_spec {
4047
/* u64 scalar interpreted depending on arg_type, see below */
4148
__u64 val_off;
4249
/* arg location case, see bpf_usdt_arg() for details */
43-
enum __bpf_usdt_arg_type arg_type;
50+
enum __bpf_usdt_arg_type arg_type: 8;
51+
#if defined(__LITTLE_ENDIAN_BITFIELD)
52+
/* index register offset within struct pt_regs (high 12 bits) */
53+
__u16 idx_reg_off: 12,
54+
/* scale factor for index register (1, 2, 4, or 8) (low 4 bits) */
55+
scale: 4;
56+
#elif defined(__BIG_ENDIAN_BITFIELD)
57+
/* scale factor for index register (1, 2, 4, or 8) (high 4 bits) */
58+
__u16 scale: 4,
59+
/* index register offset within struct pt_regs (low 12 bits) */
60+
idx_reg_off: 12;
61+
#else
62+
#error "Please fix <asm/byteorder.h>"
63+
#endif
64+
/* reserved for future use, keeps reg_off offset stable */
65+
__u8 reserved;
4466
/* offset of referenced register within struct pt_regs */
4567
short reg_off;
4668
/* whether arg should be interpreted as signed value */
@@ -149,7 +171,7 @@ int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res)
149171
{
150172
struct __bpf_usdt_spec *spec;
151173
struct __bpf_usdt_arg_spec *arg_spec;
152-
unsigned long val;
174+
unsigned long val, idx;
153175
int err, spec_id;
154176

155177
*res = 0;
@@ -202,6 +224,27 @@ int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res)
202224
return err;
203225
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
204226
val >>= arg_spec->arg_bitshift;
227+
#endif
228+
break;
229+
case BPF_USDT_ARG_SIB:
230+
/* Arg is in memory addressed by SIB (Scale-Index-Base) mode
231+
* (e.g., "-1@-96(%rbp,%rax,8)" in USDT arg spec). We first
232+
* fetch the base register contents and the index register
233+
* contents from pt_regs. Then we calculate the final address
234+
* as base + (index * scale) + offset, and do a user-space
235+
* probe read to fetch the argument value.
236+
*/
237+
err = bpf_probe_read_kernel(&val, sizeof(val), (void *)ctx + arg_spec->reg_off);
238+
if (err)
239+
return err;
240+
err = bpf_probe_read_kernel(&idx, sizeof(idx), (void *)ctx + arg_spec->idx_reg_off);
241+
if (err)
242+
return err;
243+
err = bpf_probe_read_user(&val, sizeof(val), (void *)(val + (idx * arg_spec->scale) + arg_spec->val_off));
244+
if (err)
245+
return err;
246+
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
247+
val >>= arg_spec->arg_bitshift;
205248
#endif
206249
break;
207250
default:

tools/lib/bpf/usdt.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <unistd.h>
1010
#include <linux/ptrace.h>
1111
#include <linux/kernel.h>
12+
#include <asm/byteorder.h>
1213

1314
/* s8 will be marked as poison while it's a reg of riscv */
1415
#if defined(__riscv)
@@ -200,12 +201,23 @@ enum usdt_arg_type {
200201
USDT_ARG_CONST,
201202
USDT_ARG_REG,
202203
USDT_ARG_REG_DEREF,
204+
USDT_ARG_SIB,
203205
};
204206

205207
/* should match exactly struct __bpf_usdt_arg_spec from usdt.bpf.h */
206208
struct usdt_arg_spec {
207209
__u64 val_off;
208-
enum usdt_arg_type arg_type;
210+
enum usdt_arg_type arg_type: 8;
211+
#if defined(__LITTLE_ENDIAN_BITFIELD)
212+
__u16 idx_reg_off: 12,
213+
scale: 4;
214+
#elif defined(__BIG_ENDIAN_BITFIELD)
215+
__u16 scale: 4,
216+
idx_reg_off: 12;
217+
#else
218+
#error "Please fix <asm/byteorder.h>"
219+
#endif
220+
__u8 reserved; /* keep reg_off offset stable */
209221
short reg_off;
210222
bool arg_signed;
211223
char arg_bitshift;
@@ -1283,11 +1295,46 @@ static int calc_pt_regs_off(const char *reg_name)
12831295

12841296
static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
12851297
{
1286-
char reg_name[16];
1287-
int len, reg_off;
1288-
long off;
1298+
char reg_name[16] = {0}, idx_reg_name[16] = {0};
1299+
int len, reg_off, idx_reg_off, scale = 1;
1300+
long off = 0;
1301+
1302+
if (sscanf(arg_str, " %d @ %ld ( %%%15[^,] , %%%15[^,] , %d ) %n",
1303+
arg_sz, &off, reg_name, idx_reg_name, &scale, &len) == 5 ||
1304+
sscanf(arg_str, " %d @ ( %%%15[^,] , %%%15[^,] , %d ) %n",
1305+
arg_sz, reg_name, idx_reg_name, &scale, &len) == 4 ||
1306+
sscanf(arg_str, " %d @ %ld ( %%%15[^,] , %%%15[^)] ) %n",
1307+
arg_sz, &off, reg_name, idx_reg_name, &len) == 4 ||
1308+
sscanf(arg_str, " %d @ ( %%%15[^,] , %%%15[^)] ) %n",
1309+
arg_sz, reg_name, idx_reg_name, &len) == 3
1310+
) {
1311+
/*
1312+
* Scale Index Base case:
1313+
* 1@-96(%rbp,%rax,8)
1314+
* 1@(%rbp,%rax,8)
1315+
* 1@-96(%rbp,%rax)
1316+
* 1@(%rbp,%rax)
1317+
*/
1318+
arg->arg_type = USDT_ARG_SIB;
1319+
arg->val_off = off;
12891320

1290-
if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", arg_sz, &off, reg_name, &len) == 3) {
1321+
reg_off = calc_pt_regs_off(reg_name);
1322+
if (reg_off < 0)
1323+
return reg_off;
1324+
arg->reg_off = reg_off;
1325+
1326+
idx_reg_off = calc_pt_regs_off(idx_reg_name);
1327+
if (idx_reg_off < 0)
1328+
return idx_reg_off;
1329+
/* validate scale factor and set fields directly */
1330+
if (scale != 1 && scale != 2 && scale != 4 && scale != 8) {
1331+
pr_warn("usdt: invalid SIB scale %d, expected 1,2,4,8; defaulting to 1\n", scale);
1332+
return -EINVAL;
1333+
}
1334+
arg->idx_reg_off = idx_reg_off;
1335+
arg->scale = scale;
1336+
} else if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n",
1337+
arg_sz, &off, reg_name, &len) == 3) {
12911338
/* Memory dereference case, e.g., -4@-20(%rbp) */
12921339
arg->arg_type = USDT_ARG_REG_DEREF;
12931340
arg->val_off = off;
@@ -1306,6 +1353,7 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
13061353
} else if (sscanf(arg_str, " %d @ %%%15s %n", arg_sz, reg_name, &len) == 2) {
13071354
/* Register read case, e.g., -4@%eax */
13081355
arg->arg_type = USDT_ARG_REG;
1356+
/* register read has no memory offset */
13091357
arg->val_off = 0;
13101358

13111359
reg_off = calc_pt_regs_off(reg_name);

0 commit comments

Comments
 (0)