Skip to content

Commit e3c2409

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 715d6cb commit e3c2409

File tree

2 files changed

+108
-7
lines changed

2 files changed

+108
-7
lines changed

tools/lib/bpf/usdt.bpf.h

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,31 @@ enum __bpf_usdt_arg_type {
3434
BPF_USDT_ARG_CONST,
3535
BPF_USDT_ARG_REG,
3636
BPF_USDT_ARG_REG_DEREF,
37+
BPF_USDT_ARG_SIB,
3738
};
3839

40+
/*
41+
* To preserve overall layout and avoid growing this struct while adding SIB
42+
* extras, we keep 4 bytes worth of space after val_off:
43+
*
44+
* - arg_type is stored as a single byte (values from enum below)
45+
* - idx_packed is a 16-bit field packing idx_reg_off (high 12 bits)
46+
* and scale shift (low 4 bits, i.e., scale = 1 << shift)
47+
* - reserved is one spare byte for future use
48+
*
49+
* This keeps the offset of reg_off identical to the historical layout
50+
* (val_off:8 + 4 bytes here), ensuring backwards/forwards compatibility for
51+
* non-SIB modes that only rely on val_off/arg_type/reg_off/... offsets.
52+
*/
3953
struct __bpf_usdt_arg_spec {
4054
/* u64 scalar interpreted depending on arg_type, see below */
4155
__u64 val_off;
4256
/* arg location case, see bpf_usdt_arg() for details */
43-
enum __bpf_usdt_arg_type arg_type;
57+
__u8 arg_type;
58+
/* packed: [15:4] idx_reg_off, [3:0] scale_shift */
59+
__u16 idx_packed;
60+
/* reserved for future use, keeps reg_off offset stable */
61+
__u8 reserved;
4462
/* offset of referenced register within struct pt_regs */
4563
short reg_off;
4664
/* whether arg should be interpreted as signed value */
@@ -52,6 +70,10 @@ struct __bpf_usdt_arg_spec {
5270
char arg_bitshift;
5371
};
5472

73+
/* Helpers to (un)pack SIB extras from idx_packed without relying on bitfields. */
74+
#define USDT_IDX_OFF(packed) ((packed) >> 4)
75+
#define USDT_IDX_SCALE_SHIFT(packed) ((packed) & 0x000f)
76+
5577
/* should match USDT_MAX_ARG_CNT in usdt.c exactly */
5678
#define BPF_USDT_MAX_ARG_CNT 12
5779
struct __bpf_usdt_spec {
@@ -149,8 +171,9 @@ 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;
176+
int idx_off = 0, scale = 0;
154177

155178
*res = 0;
156179

@@ -202,6 +225,33 @@ int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res)
202225
return err;
203226
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
204227
val >>= arg_spec->arg_bitshift;
228+
#endif
229+
break;
230+
case BPF_USDT_ARG_SIB:
231+
/* Arg is in memory addressed by SIB (Scale-Index-Base) mode
232+
* (e.g., "-1@-96(%rbp,%rax,8)" in USDT arg spec). Register
233+
* is identified like with BPF_USDT_ARG_SIB case, the offset
234+
* is in arg_spec->val_off, the scale factor is in arg_spec->scale.
235+
* Firstly, we fetch the base register contents and the index
236+
* register contents from pt_regs. Secondly, we multiply the
237+
* index register contents by the scale factor, then add the
238+
* base address and the offset to get the final address. Finally,
239+
* we do another user-space probe read to fetch argument value
240+
* itself.
241+
*/
242+
idx_off = USDT_IDX_OFF(arg_spec->idx_packed);
243+
scale = 1UL << USDT_IDX_SCALE_SHIFT(arg_spec->idx_packed);
244+
err = bpf_probe_read_kernel(&val, sizeof(val), (void *)ctx + arg_spec->reg_off);
245+
if (err)
246+
return err;
247+
err = bpf_probe_read_kernel(&idx, sizeof(idx), (void *)ctx + idx_off);
248+
if (err)
249+
return err;
250+
err = bpf_probe_read_user(&val, sizeof(val), (void *)val + idx * scale + arg_spec->val_off);
251+
if (err)
252+
return err;
253+
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
254+
val >>= arg_spec->arg_bitshift;
205255
#endif
206256
break;
207257
default:

tools/lib/bpf/usdt.c

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,15 @@ enum usdt_arg_type {
200200
USDT_ARG_CONST,
201201
USDT_ARG_REG,
202202
USDT_ARG_REG_DEREF,
203+
USDT_ARG_SIB,
203204
};
204205

205206
/* should match exactly struct __bpf_usdt_arg_spec from usdt.bpf.h */
206207
struct usdt_arg_spec {
207208
__u64 val_off;
208-
enum usdt_arg_type arg_type;
209+
__u8 arg_type; /* enum value stored as u8 */
210+
__u16 idx_packed; /* [15:4]=idx_reg_off, [3:0]=scale_shift */
211+
__u8 reserved; /* keep reg_off offset stable */
209212
short reg_off;
210213
bool arg_signed;
211214
char arg_bitshift;
@@ -214,6 +217,10 @@ struct usdt_arg_spec {
214217
/* should match BPF_USDT_MAX_ARG_CNT in usdt.bpf.h */
215218
#define USDT_MAX_ARG_CNT 12
216219

220+
/* Helpers to (un)pack SIB extras from idx_packed without relying on bitfields. */
221+
#define BPF_USDT_IDX_PACK(idx_off, scale_shift) \
222+
((__u16)(((__u16)((idx_off) & 0x0fff)) << 4) | (__u16)((scale_shift) & 0x000f))
223+
217224
/* should match struct __bpf_usdt_spec from usdt.bpf.h */
218225
struct usdt_spec {
219226
struct usdt_arg_spec args[USDT_MAX_ARG_CNT];
@@ -1283,11 +1290,54 @@ static int calc_pt_regs_off(const char *reg_name)
12831290

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

1290-
if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", arg_sz, &off, reg_name, &len) == 3) {
1317+
reg_off = calc_pt_regs_off(reg_name);
1318+
if (reg_off < 0)
1319+
return reg_off;
1320+
arg->reg_off = reg_off;
1321+
1322+
idx_reg_off = calc_pt_regs_off(idx_reg_name);
1323+
if (idx_reg_off < 0)
1324+
return idx_reg_off;
1325+
/* pack idx_reg_off and scale shift (scale in {1,2,4,8}) */
1326+
if (scale == 1)
1327+
scale_shift = 0;
1328+
else if (scale == 2)
1329+
scale_shift = 1;
1330+
else if (scale == 4)
1331+
scale_shift = 2;
1332+
else if (scale == 8)
1333+
scale_shift = 3;
1334+
else {
1335+
pr_warn("usdt: invalid SIB scale %d, expected 1,2,4,8; defaulting to 1\n", scale);
1336+
return -EINVAL;
1337+
}
1338+
arg->idx_packed = BPF_USDT_IDX_PACK(idx_reg_off, scale_shift);
1339+
} else if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n",
1340+
arg_sz, &off, reg_name, &len) == 3) {
12911341
/* Memory dereference case, e.g., -4@-20(%rbp) */
12921342
arg->arg_type = USDT_ARG_REG_DEREF;
12931343
arg->val_off = off;
@@ -1306,6 +1356,7 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
13061356
} else if (sscanf(arg_str, " %d @ %%%15s %n", arg_sz, reg_name, &len) == 2) {
13071357
/* Register read case, e.g., -4@%eax */
13081358
arg->arg_type = USDT_ARG_REG;
1359+
/* register read has no memory offset */
13091360
arg->val_off = 0;
13101361

13111362
reg_off = calc_pt_regs_off(reg_name);

0 commit comments

Comments
 (0)