Skip to content

Commit 570a277

Browse files
Phoenix500526theihor
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 ed19bc9 commit 570a277

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

tools/lib/bpf/usdt.bpf.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ 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

3940
struct __bpf_usdt_arg_spec {
@@ -43,6 +44,10 @@ struct __bpf_usdt_arg_spec {
4344
enum __bpf_usdt_arg_type arg_type;
4445
/* offset of referenced register within struct pt_regs */
4546
short reg_off;
47+
/* offset of index register in pt_regs, only used in SIB mode */
48+
short idx_reg_off;
49+
/* scale factor for index register, only used in SIB mode */
50+
short scale;
4651
/* whether arg should be interpreted as signed value */
4752
bool arg_signed;
4853
/* number of bits that need to be cleared and, optionally,
@@ -149,7 +154,7 @@ int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res)
149154
{
150155
struct __bpf_usdt_spec *spec;
151156
struct __bpf_usdt_arg_spec *arg_spec;
152-
unsigned long val;
157+
unsigned long val, idx;
153158
int err, spec_id;
154159

155160
*res = 0;
@@ -202,6 +207,32 @@ int bpf_usdt_arg(struct pt_regs *ctx, __u64 arg_num, long *res)
202207
return err;
203208
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
204209
val >>= arg_spec->arg_bitshift;
210+
#endif
211+
break;
212+
case BPF_USDT_ARG_SIB:
213+
/* Arg is in memory addressed by SIB (Scale-Index-Base) mode
214+
* (e.g., "-1@-96(%rbp,%rax,8)" in USDT arg spec). Register
215+
* is identified like with BPF_USDT_ARG_SIB case, the offset
216+
* is in arg_spec->val_off, the scale factor is in arg_spec->scale.
217+
* Firstly, we fetch the base register contents and the index
218+
* register contents from pt_regs. Secondly, we multiply the
219+
* index register contents by the scale factor, then add the
220+
* base address and the offset to get the final address. Finally,
221+
* we do another user-space probe read to fetch argument value
222+
* itself.
223+
*/
224+
err = bpf_probe_read_kernel(&val, sizeof(val), (void *)ctx + arg_spec->reg_off);
225+
if (err)
226+
return err;
227+
err = bpf_probe_read_kernel(&idx, sizeof(idx), (void *)ctx + arg_spec->idx_reg_off);
228+
if (err)
229+
return err;
230+
err = bpf_probe_read_user(&val, sizeof(val),
231+
(void *)val + idx * arg_spec->scale + arg_spec->val_off);
232+
if (err)
233+
return err;
234+
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
235+
val >>= arg_spec->arg_bitshift;
205236
#endif
206237
break;
207238
default:

tools/lib/bpf/usdt.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,16 @@ 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;
208209
enum usdt_arg_type arg_type;
209210
short reg_off;
211+
short idx_reg_off;
212+
short scale;
210213
bool arg_signed;
211214
char arg_bitshift;
212215
};
@@ -1283,11 +1286,39 @@ static int calc_pt_regs_off(const char *reg_name)
12831286

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

1290-
if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", arg_sz, &off, reg_name, &len) == 3) {
1316+
idx_reg_off = calc_pt_regs_off(idx_reg_name);
1317+
if (idx_reg_off < 0)
1318+
return idx_reg_off;
1319+
arg->idx_reg_off = idx_reg_off;
1320+
} else if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n",
1321+
arg_sz, &off, reg_name, &len) == 3) {
12911322
/* Memory dereference case, e.g., -4@-20(%rbp) */
12921323
arg->arg_type = USDT_ARG_REG_DEREF;
12931324
arg->val_off = off;
@@ -1298,15 +1329,15 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec
12981329
} else if (sscanf(arg_str, " %d @ ( %%%15[^)] ) %n", arg_sz, reg_name, &len) == 2) {
12991330
/* Memory dereference case without offset, e.g., 8@(%rsp) */
13001331
arg->arg_type = USDT_ARG_REG_DEREF;
1301-
arg->val_off = 0;
1332+
arg->val_off = off;
13021333
reg_off = calc_pt_regs_off(reg_name);
13031334
if (reg_off < 0)
13041335
return reg_off;
13051336
arg->reg_off = reg_off;
13061337
} else if (sscanf(arg_str, " %d @ %%%15s %n", arg_sz, reg_name, &len) == 2) {
13071338
/* Register read case, e.g., -4@%eax */
13081339
arg->arg_type = USDT_ARG_REG;
1309-
arg->val_off = 0;
1340+
arg->val_off = off;
13101341

13111342
reg_off = calc_pt_regs_off(reg_name);
13121343
if (reg_off < 0)

0 commit comments

Comments
 (0)