Skip to content

Commit 6c938df

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. Change since v1(https://lore.kernel.org/lkml/[email protected]/): - refactor the code to make it more readable - modify the commit message to explain why and how Change since v2: - fix the `scale` uninitialized error Signed-off-by: Jiawei Zhao <[email protected]>
1 parent 9879802 commit 6c938df

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
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: 23 additions & 3 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,28 @@ 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;
1289+
char reg_name[16], idx_reg_off, idx_reg_name[16];
1290+
int len, reg_off, scale;
12881291
long off;
12891292

1290-
if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", arg_sz, &off, reg_name, &len) == 3) {
1293+
if (sscanf(arg_str, " %d @ %ld ( %%%15[^,] , %%%15[^,] , %d ) %n",
1294+
arg_sz, &off, reg_name, idx_reg_name, &scale, &len) == 5) {
1295+
/* Scale Index Base case, e.g., 1@-96(%rbp,%rax,8)*/
1296+
arg->arg_type = USDT_ARG_SIB;
1297+
arg->val_off = off;
1298+
arg->scale = scale;
1299+
1300+
reg_off = calc_pt_regs_off(reg_name);
1301+
if (reg_off < 0)
1302+
return reg_off;
1303+
arg->reg_off = reg_off;
1304+
1305+
idx_reg_off = calc_pt_regs_off(idx_reg_name);
1306+
if (idx_reg_off < 0)
1307+
return idx_reg_off;
1308+
arg->idx_reg_off = idx_reg_off;
1309+
} else if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n",
1310+
arg_sz, &off, reg_name, &len) == 3) {
12911311
/* Memory dereference case, e.g., -4@-20(%rbp) */
12921312
arg->arg_type = USDT_ARG_REG_DEREF;
12931313
arg->val_off = off;

0 commit comments

Comments
 (0)