Skip to content

Commit b62dff1

Browse files
theihoranakryiko
authored andcommitted
libbpf: Implement bpf_usdt_arg_size BPF function
Information about USDT argument size is implicitly stored in __bpf_usdt_arg_spec, but currently it's not accessbile to BPF programs that use USDT. Implement bpf_sdt_arg_size() that returns the size of an USDT argument in bytes. v1->v2: * do not add __bpf_usdt_arg_spec() helper v1: https://lore.kernel.org/bpf/[email protected]/ Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Ihor Solodrai <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4580f4e commit b62dff1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tools/lib/bpf/usdt.bpf.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ int bpf_usdt_arg_cnt(struct pt_regs *ctx)
108108
return spec->arg_cnt;
109109
}
110110

111+
/* Returns the size in bytes of the #*arg_num* (zero-indexed) USDT argument.
112+
* Returns negative error if argument is not found or arg_num is invalid.
113+
*/
114+
static __always_inline
115+
int bpf_usdt_arg_size(struct pt_regs *ctx, __u64 arg_num)
116+
{
117+
struct __bpf_usdt_arg_spec *arg_spec;
118+
struct __bpf_usdt_spec *spec;
119+
int spec_id;
120+
121+
spec_id = __bpf_usdt_spec_id(ctx);
122+
if (spec_id < 0)
123+
return -ESRCH;
124+
125+
spec = bpf_map_lookup_elem(&__bpf_usdt_specs, &spec_id);
126+
if (!spec)
127+
return -ESRCH;
128+
129+
if (arg_num >= BPF_USDT_MAX_ARG_CNT)
130+
return -ENOENT;
131+
barrier_var(arg_num);
132+
if (arg_num >= spec->arg_cnt)
133+
return -ENOENT;
134+
135+
arg_spec = &spec->args[arg_num];
136+
137+
/* arg_spec->arg_bitshift = 64 - arg_sz * 8
138+
* so: arg_sz = (64 - arg_spec->arg_bitshift) / 8
139+
*/
140+
return (unsigned int)(64 - arg_spec->arg_bitshift) / 8;
141+
}
142+
111143
/* Fetch USDT argument #*arg_num* (zero-indexed) and put its value into *res.
112144
* Returns 0 on success; negative error, otherwise.
113145
* On error *res is guaranteed to be set to zero.

0 commit comments

Comments
 (0)