tracing/probes: Allow use of BTF names to dereference pointers
Subsystems (3)
Steven Rostedt
maintainerPATCH v2 1/1 tracing/probes: Allow use of BTF names to dereference pointers
From: Steven Rostedt <[email protected]>
Add syntax to the FETCHARGS parsing of probes to allow the use of structure and member names to get the offsets to dereference pointers.
Currently, a dereference must be a number, where the user has to figure out manually the offset of a member of a structure that they want to reference. For example, to get the size of a kmem_cache that was passed to the function kmem_cache_alloc_noprof, one would need to do:
# cd /sys/kernel/tracing # echo 'f:cache kmem_cache_alloc_noprof size=+0x18($arg1):u32' >> dynamic_events
This requires knowing that the offset of size is 0x18, which can be found with gdb:
(gdb) p &((struct kmem_cache *)0)->size
$1 = (unsigned int *) 0x18
If BTF is in the kernel, it can be used to find this with names, where the user doesn't need to find the actual offset:
# echo 'f:cache kmem_cache_alloc_noprof size=+kmem_cache.size($arg1):u32' >> dynamic_events
Instead of the "+0x18", it would have "+kmem_cache.size" where the format is:
+STRUCT.MEMBER[.MEMBER[..]]
The delimiter is '.' and the first item is the structure name. Then the member of the structure to get the offset of. If that member is an embedded structure, another '.MEMBER' may be added to get the offset of its members with respect to the original value.
"+kmem_cache.size($arg1)" is equivalent to:
(*(struct kmem_cache *)$arg1).size
Anonymous structures are also handled:
# echo 'e:xmit net.net_dev_xmit +net_device.name(+sk_buff.dev($skbaddr)):string' >> dynamic_events
Where "+net_device.name(+sk_buff.dev($skbaddr))" is equivalent to:
(*(struct net_device *)((*(struct sk_buff *)($skbaddr)).dev)->name)
Note that "dev" of struct sk_buff is inside an anonymous structure:
struct sk_buff {
union {
struct {
/* These two members must be first to match sk_buff_head. */
struct sk_buff *next;
struct sk_buff *prev;
union {
struct net_device *dev;
[..]
};
};
[..]
};This will allow up to three deep of anonymous structures before it will fail to find a member.
The above produces:
sshd-session-1080 [000] b..5. 1526.337161: xmit: (net.net_dev_xmit) arg1="enp7s0"
And nested structures can be found by adding more members to the arg:
# echo 'f:read filemap_readahead.isra.0 file=+0(+dentry.d_name.name(+file.f_path.dentry($arg2))):string' >> dynamic_events
The above is equivalent to:
*((*(struct dentry *)(*(struct file *)$arg2).f_path.dentry)->d_name.name)
And produces:
trace-cmd-1381 [002] ...1. 2082.676268: read: (filemap_readahead.isra.0+0x0/0x150) file="trace.dat"
---
Changes since v1: https://patch.msgid.link/[email protected]
- Pass error info back so that error_log can display what went wrong (Masami Hiramatsu)
- Use __btf_member_bit_offset() to get nested struct member offsets (Douglas Raillard)
- Add btf_put(btf) (Jiri Olsa)
Documentation/trace/kprobetrace.rst | 3 + kernel/trace/trace_btf.c | 111 ++++++++++++++++++++++++++++ kernel/trace/trace_btf.h | 10 +++ kernel/trace/trace_probe.c | 19 ++++- kernel/trace/trace_probe.h | 4 +- 5 files changed, 144 insertions(+), 3 deletions(-)
Documentation/trace/kprobetrace.rst +3 -0
@@ -54,6 +54,8 @@ Synopsis of kprobe_events $retval : Fetch return value.(\*2) $comm : Fetch current task comm. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*3)(\*4) + +STRUCT.MEMBER[.MEMBER[..]](FETCHARG) : If BTF is supported, Fetch memory + at FETCHARG + the offset of MEMBER inside of STRUCT.(\*5) \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG. FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
@@ -70,6 +72,7 @@ Synopsis of kprobe_events accesses one register. (\*3) this is useful for fetching a field of data structures. (\*4) "u" means user-space dereference. See :ref:`user_mem_access`. + (\*5) +STRUCT.MEMBER(FETCHARG) is equivalent to (*(struct STRUCT *)(FETCHARG)).MEMBER Function arguments at kretprobe -------------------------------
kernel/trace/trace_btf.c +111 -0
114 lines not on this page · raw message
kernel/trace/trace_btf.h +10 -0
@@ -9,3 +9,13 @@ const struct btf_member *btf_find_struct_member(struct btf *btf,const struct btf_type *type, const char *member_name, u32 *anon_offset); + +#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS +/* Will modify arg, but will put it back before returning. */ +int btf_find_offset (char *arg,long *offset); +#else +static inline int btf_find_offset (char *arg,long *offset) +{ +return -EINVAL ; +} +#endif
kernel/trace/trace_probe.c +17 -2
@@ -1165,7 +1165,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,case '+' :/* deref memory */ case '-' : -if (arg[1 ] =='u' ) { +if (arg[1 ] =='u' &&isdigit (arg[2 ])) { deref =FETCH_OP_UDEREF ; arg[1 ] = arg[0 ]; arg++;
@@ -1178,7 +1178,22 @@ parse_probe_arg(char *arg, const struct fetch_type *type,return -EINVAL ; } *tmp ='\0' ; - ret =kstrtol (arg,0 , &offset); +if (arg[0 ] !='-' && !isdigit (*arg)) { +int err =0 ; + ret =btf_find_offset (arg, &offset); +switch (ret) { +case -ENODEV : err =TP_ERR_NOSUP_BTFARG ;break ; +case -E2BIG : err =TP_ERR_MEMBER_TOO_DEEP ;break ; +case -EINVAL : err =TP_ERR_BAD_STRUCT_FMT ;break ; +case -ENXIO : err =TP_ERR_BAD_BTF_TID ;break ; + } +if (err) +__trace_probe_log_err (ctx->offset, err); +if (ret <0 ) +return ret; + }else { + ret =kstrtol (arg,0 , &offset); + }if (ret) {trace_probe_log_err (ctx->offset,BAD_DEREF_OFFS );break ;
kernel/trace/trace_probe.h +3 -1
@@ -563,7 +563,9 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (NEED_STRING_TYPE ,"$comm and immediate-string only accepts string type" ),\C (TOO_MANY_ARGS ,"Too many arguments are specified" ), \C (TOO_MANY_EARGS ,"Too many entry arguments specified" ), \ -C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), +C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), \ +C (MEMBER_TOO_DEEP ,"Too many indirections of anonymous structure" ), \ +C (BAD_STRUCT_FMT ,"Unknown BTF structure" ), #undef C #define C(a, b) TP_ERR_##a
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Uninitialized variable `btf` in `btf_find_offset()` causes a kernel crash/panic on error paths.…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Uninitialized variable btf in btf_find_offset() causes a kernel crash/panic on error paths.
- [Medium] Nested structure resolution fails if members use BTF modifiers (const, volatile) or typedefs.
- [Medium] The updated user-space dereference parser misidentifies valid BTF struct accesses and breaks negative user-space offsets.
- [Low] Missing error mapping for -ENOENT suppresses trace log errors when a struct member is not found.
- [Low] The -E2BIG error for excessively deep anonymous structures is silently dropped, creating dead code in the error handler. --
commit 59c817c69d1d406c3cfa1e1e3659ff16a7e33c9d
Author: Steven Rostedt <[email protected]>
Subject: tracing/probes: Allow use of BTF names to dereference pointers
This patch adds syntax to the FETCHARGS parsing of probes to allow the use of structure and member names to get the offsets to dereference pointers, using BTF.
Quoted 4 lines
kernel/trace/trace_btf.c +0
-0
[ ... ]
Quoted 3 lines
kernel/trace/trace_btf.c +3
-0+static int find_member (const char *ptr,struct btf *btf, +const struct btf_type **type,int level) +{
[ ... ]
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 154
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+/* Handle anonymous structures */ +if (strlen (tname)) +continue ; + + *type =btf_type_by_id (btf, member->type); +if (btf_type_is_struct (*type)) { +int offset =find_member (ptr, btf, type, level +1 ); + +if (offset <0 ) +continue ;
If find_member() returns -E2BIG due to deep anonymous structure nesting, this check appears to swallow the error and just skip to the next member. Does this prevent -E2BIG from ever reaching the caller and make the TP_ERR_MEMBER_TOO_DEEP error handling dead code?
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+ +return offset +BITS_ROUNDDOWN_BYTES (member->offset); + } + } + +return -ENOENT ; +} + +/** +* btf_find_offset - Find an offset of a member for a structure
[ ... ]
Quoted 9 lines
kernel/trace/trace_btf.c +9
-0+int btf_find_offset (char *arg,long *offset_p) +{ +const struct btf_type *t; +struct btf *btf; +long offset =0 ; +char *ptr; +int ret; +s32 id; +
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 199
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+ ptr =strchr (arg,'.' ); +if (!ptr) +return -EINVAL ; + + *ptr ='\0' ; + + ret = -ENXIO ; + id =bpf_find_btf_id (arg,BTF_KIND_STRUCT , &btf); +if (id <0 ) + goto error;
If bpf_find_btf_id() fails here, it returns an error code and does not initialize the btf pointer.
Does this cause btf_put(btf) to dereference an uninitialized stack pointer in the error path below, potentially leading to a panic?
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 208
Quoted 9 lines
kernel/trace/trace_btf.c +9
-0+ +/* Get BTF_KIND_FUNC type */ + t =btf_type_by_id (btf, id); + +/* May allow more than one member, as long as they are structures */ + do { + ret = -ENXIO; + if (!t || !btf_type_is_struct(t)) + goto error;
If a structure member is a typedef or has a modifier (like const), the updated type from find_member() will be BTF_KIND_TYPEDEF or BTF_KIND_CONST.
Will btf_type_is_struct() then fail on the next iteration of this loop? Should btf_type_skip_modifiers() be used to resolve the underlying structure type first?
[ ... ]
Quoted 6 lines
kernel/trace/trace_btf.c +6
-0+error: +btf_put (btf); +if (ptr) + *ptr ='.' ; +return ret; +}
[ ... ]
kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1168
Quoted 10 lines
kernel/trace/trace_probe.c +1
-1@@ -1165,7 +1165,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,case '+' :/* deref memory */ case '-' : -if (arg[1 ] =='u' ) { +if (arg[1 ] =='u' &&isdigit (arg[2 ])) {
Does this new condition misidentify valid BTF structure lookups that happen to start with 'u' followed by a digit, such as +u16_port.id($arg)?
Also, does it break previously valid negative user-space offsets like +u-10($arg) since arg[2] would be a hyphen instead of a digit?
Quoted 7 lines
kernel/trace/trace_probe.c +0
-0deref =FETCH_OP_UDEREF ; arg[1 ] = arg[0 ]; arg++;@@ -1178,7 +1178,22 @@ parse_probe_arg(char *arg, const struct fetch_type *type,return -EINVAL ; } *tmp ='\0' ;
kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1189
Quoted 10 lines
kernel/trace/trace_probe.c +9
-1- ret =kstrtol (arg,0 , &offset); +if (arg[0 ] !='-' && !isdigit (*arg)) { +int err =0 ; + ret =btf_find_offset (arg, &offset); +switch (ret) { +case -ENODEV : err =TP_ERR_NOSUP_BTFARG ;break ; +case -E2BIG : err =TP_ERR_MEMBER_TOO_DEEP ;break ; +case -EINVAL : err =TP_ERR_BAD_STRUCT_FMT ;break ; +case -ENXIO : err =TP_ERR_BAD_BTF_TID ;break ; + }
If find_member() cannot find the specified member, it returns -ENOENT. Since -ENOENT is missing from this switch statement, err remains 0 and __trace_probe_log_err() is skipped.
Should there be a mapping for -ENOENT (perhaps to TP_ERR_NO_BTF_FIELD) so users get a specific error log when they misspell a field name?
Quoted 8 lines
kernel/trace/trace_probe.c +7
-0+if (err) +__trace_probe_log_err (ctx->offset, err); +if (ret <0 ) +return ret; + }else { + ret =kstrtol (arg,0 , &offset); + }if (ret) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260516173310.1dbad146@fedora?part=1
Steven Rostedt
maintainer[ /me finds it weird talking to a bot ]
Quoted 17 lines · On Sat, 16 May 2026 21:58:33 +0000 [email protected] wrote:
[ ... ]
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 154
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+/* Handle anonymous structures */ +if (strlen (tname)) +continue ; + + *type =btf_type_by_id (btf, member->type); +if (btf_type_is_struct (*type)) { +int offset =find_member (ptr, btf, type, level +1 ); + +if (offset <0 ) +continue ;If
find_member()returns-E2BIGdue to deep anonymous structure nesting, this check appears to swallow the error and just skip to the next member. Does this prevent-E2BIGfrom ever reaching the caller and make theTP_ERR_MEMBER_TOO_DEEPerror handling dead code?
Ah yeah, this needs to be:
if (offset < 0) {
if (offset == -ENOENT)
continue;
return offset;
}Quoted 37 lines
Quoted 11 lines
kernel/trace/trace_btf.c +10
-0+ +return offset +BITS_ROUNDDOWN_BYTES (member->offset); + } + } + +return -ENOENT ; +} + +/** +* btf_find_offset - Find an offset of a member for a structure [ ... ]
Quoted 9 lines
kernel/trace/trace_btf.c +9
-0+int btf_find_offset (char *arg,long *offset_p) +{ +const struct btf_type *t; +struct btf *btf; +long offset =0 ; +char *ptr; +int ret; +s32 id; +kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 199
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+ ptr =strchr (arg,'.' ); +if (!ptr) +return -EINVAL ; + + *ptr ='\0' ; + + ret = -ENXIO ; + id =bpf_find_btf_id (arg,BTF_KIND_STRUCT , &btf); +if (id <0 ) + goto error;If
bpf_find_btf_id()fails here, it returns an error code and does not initialize the btf pointer.
Quoted 2 lines
Does this cause
btf_put(btf)to dereference an uninitialized stack pointer in the error path below, potentially leading to a panic?
Probably. I have to add another label.
Quoted 14 lines
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 208
Quoted 10 lines
kernel/trace/trace_btf.c +9
-0+ +/* Get BTF_KIND_FUNC type */ + t =btf_type_by_id (btf, id); + +/* May allow more than one member, as long as they are structures */ + do { + ret = -ENXIO; + if (!t || !btf_type_is_struct(t)) + goto error;If a structure member is a typedef or has a modifier (like const), the updated type from
find_member()will beBTF_KIND_TYPEDEForBTF_KIND_CONST.
Quoted 3 lines
Will
btf_type_is_struct()then fail on the next iteration of this loop? Shouldbtf_type_skip_modifiers()be used to resolve the underlying structure type first?
Probably.
Quoted 26 lines
[ ... ]
Quoted 6 lines
kernel/trace/trace_btf.c +6
-0+error: +btf_put (btf); +if (ptr) + *ptr ='.' ; +return ret; +}[ ... ]
kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1168
Quoted 10 lines
kernel/trace/trace_probe.c +1
-1@@ -1165,7 +1165,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,case '+' :/* deref memory */ case '-' : -if (arg[1 ] =='u' ) { +if (arg[1 ] =='u' &&isdigit (arg[2 ])) {Does this new condition misidentify valid BTF structure lookups that happen to start with 'u' followed by a digit, such as +u16_port.id($arg)?
Quoted 2 lines
Also, does it break previously valid negative user-space offsets like +u-10($arg) since arg[2] would be a hyphen instead of a digit?
No, because any sane person should write that as "-u10($arg)" ;-)
Quoted 23 lines
Quoted 8 lines
kernel/trace/trace_probe.c +0
-0deref =FETCH_OP_UDEREF ; arg[1 ] = arg[0 ]; arg++;@@ -1178,7 +1178,22 @@ parse_probe_arg(char *arg, const struct fetch_type *type,return -EINVAL ; } *tmp ='\0' ;kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1189
Quoted 10 lines
kernel/trace/trace_probe.c +9
-1- ret =kstrtol (arg,0 , &offset); +if (arg[0 ] !='-' && !isdigit (*arg)) { +int err =0 ; + ret =btf_find_offset (arg, &offset); +switch (ret) { +case -ENODEV : err =TP_ERR_NOSUP_BTFARG ;break ; +case -E2BIG : err =TP_ERR_MEMBER_TOO_DEEP ;break ; +case -EINVAL : err =TP_ERR_BAD_STRUCT_FMT ;break ; +case -ENXIO : err =TP_ERR_BAD_BTF_TID ;break ; + }If
find_member()cannot find the specified member, it returns-ENOENT. Since-ENOENTis missing from this switch statement, err remains 0 and__trace_probe_log_err()is skipped.
Quoted 2 lines
Should there be a mapping for
-ENOENT(perhaps toTP_ERR_NO_BTF_FIELD) so users get a specific error log when they misspell a field name?
I guess we could add that.
/me feels like he's a character in "Ex Machina"!
Will send a v3 soon.
-- Steve
Quoted 10 lines
Quoted 9 lines
kernel/trace/trace_probe.c +7
-0+if (err) +__trace_probe_log_err (ctx->offset, err); +if (ret <0 ) +return ret; + }else { + ret =kstrtol (arg,0 , &offset); + }if (ret) {
Masami Hiramatsu (Google)
maintainerQuoted 75 lines · On Sat, 16 May 2026 17:33:09 -0400 Steven Rostedt <[email protected]> wrote:
From: Steven Rostedt <[email protected]>
Add syntax to the FETCHARGS parsing of probes to allow the use of structure and member names to get the offsets to dereference pointers.
Currently, a dereference must be a number, where the user has to figure out manually the offset of a member of a structure that they want to reference. For example, to get the size of a
kmem_cachethat was passed to the functionkmem_cache_alloc_noprof, one would need to do:# cd /sys/kernel/tracing # echo 'f:cache kmem_cache_alloc_noprof size=+0x18($arg1):u32' >> dynamic_eventsThis requires knowing that the offset of size is
0x18, which can be found with gdb:(gdb) p &((struct
kmem_cache*)0)->size
$1 = (unsigned int *)0x18If BTF is in the kernel, it can be used to find this with names, where the user doesn't need to find the actual offset:
# echo 'f:cache kmem_cache_alloc_noprof size=+kmem_cache.size($arg1):u32' >> dynamic_eventsInstead of the "+0x18", it would have "+kmem_cache.size" where the format is:
+STRUCT.MEMBER[.MEMBER[..]]
The delimiter is '.' and the first item is the structure name. Then the member of the structure to get the offset of. If that member is an embedded structure, another '.MEMBER' may be added to get the offset of its members with respect to the original value.
"+kmem_cache.size($arg1)" is equivalent to:
(*(struct
kmem_cache*)$arg1).sizeAnonymous structures are also handled:
# echo 'e:xmit net.net_dev_xmit +net_device.name(+sk_buff.dev($skbaddr)):string' >> dynamic_eventsWhere "+net_device.name(+sk_buff.dev($skbaddr))" is equivalent to:
(*(struct
net_device*)((*(structsk_buff*)($skbaddr)).dev)->name)Note that "dev" of struct
sk_buffis inside an anonymous structure:struct sk_buff { union { struct { /* These two members must be first to match sk_buff_head. */ struct sk_buff *next; struct sk_buff *prev; union { struct net_device *dev; [..] }; }; [..] };This will allow up to three deep of anonymous structures before it will fail to find a member.
The above produces:
sshd-session-1080 [000] b..5. 1526.337161: xmit: (net.net_dev_xmit) arg1="enp7s0"And nested structures can be found by adding more members to the arg:
# echo 'f:read filemap_readahead.isra.0 file=+0(+dentry.d_name.name(+file.f_path.dentry($arg2))):string' >> dynamic_eventsThe above is equivalent to:
Quoted 7 lines
*((*(struct dentry *)(*(struct file *)$arg2).f_path.dentry)->d_name.name)
And produces:
trace-cmd-1381 [002] ...1. 2082.676268: read: (filemap_readahead.isra.0+0x0/0x150) file="trace.dat"Signed-off-by Steven Rostedt (Google) <[email protected]>
Thanks for updating it! I think this looks good to me. Let me test it. I think we also need updating selftests.
Thank you,
Quoted 231 lines
---
Changes since v1: https://patch.msgid.link/[email protected]- Pass error info back so that
error_logcan display what went wrong (Masami Hiramatsu)- Use
__btf_member_bit_offset()to get nested struct member offsets (Douglas Raillard)- Add
btf_put(btf)(Jiri Olsa)Documentation/trace/kprobetrace.rst | 3 + kernel/trace/trace_btf.c | 111 ++++++++++++++++++++++++++++ kernel/trace/trace_btf.h | 10 +++ kernel/trace/trace_probe.c | 19 ++++- kernel/trace/trace_probe.h | 4 +- 5 files changed, 144 insertions(+), 3 deletions(-)Documentation/trace/kprobetrace.rst +3
-0@@ -54,6 +54,8 @@ Synopsis of kprobe_events $retval : Fetch return value.(\*2) $comm : Fetch current task comm. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*3)(\*4) + +STRUCT.MEMBER[.MEMBER[..]](FETCHARG) : If BTF is supported, Fetch memory + at FETCHARG + the offset of MEMBER inside of STRUCT.(\*5) \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG. FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types@@ -70,6 +72,7 @@ Synopsis of kprobe_events accesses one register. (\*3) this is useful for fetching a field of data structures. (\*4) "u" means user-space dereference. See :ref:`user_mem_access`. + (\*5) +STRUCT.MEMBER(FETCHARG) is equivalent to (*(struct STRUCT *)(FETCHARG)).MEMBER Function arguments at kretprobe -------------------------------kernel/trace/trace_btf.c +111
-0@@ -120,3 +120,114 @@ const struct btf_member *btf_find_struct_member(struct btf *btf,return member; } +#define BITS_ROUNDDOWN_BYTES (bits) ((bits) >> 3) + +static int find_member (const char *ptr,struct btf *btf, +const struct btf_type **type,int level) +{ +const struct btf_member *member; +const struct btf_type *t = *type; +int i; + +/* Max of 3 depth of anonymous structures */ +if (level >3 ) +return -E2BIG ; + +for_each_member (i, t, member) { +const char *tname =btf_name_by_offset (btf, member->name_off); + +if (strcmp (ptr, tname) ==0 ) { +int offset =__btf_member_bit_offset (t, member); + *type =btf_type_by_id (btf, member->type); +return BITS_ROUNDDOWN_BYTES (offset); + } + +/* Handle anonymous structures */ +if (strlen (tname)) +continue ; + + *type =btf_type_by_id (btf, member->type); +if (btf_type_is_struct (*type)) { +int offset =find_member (ptr, btf, type, level +1 ); + +if (offset <0 ) +continue ; + +return offset +BITS_ROUNDDOWN_BYTES (member->offset); + } + } + +return -ENOENT ; +} + +/** +* btf_find_offset - Find an offset of a member for a structure +* @arg: A structure name followed by one or more members +* @offset_p: A pointer to where to store the offset +* +* Will parse @arg with the expected format of: struct.member[[.member]..] +* It is delimited by '.'. The first item must be a structure type. +* The next are its members. If the member is also of a structure type it +* another member may follow ".member". +* +* Note, @arg is modified but will be put back to what it was on return. +* +* Returns: 0 on success and -EINVAL if no '.' is present +* or -ENXIO if the structure or member is not found. +* Returns -EINVAL if BTF is not defined. +* On success, @offset_p will contain the offset of the member specified +* by @arg. +*/ +int btf_find_offset (char *arg,long *offset_p) +{ +const struct btf_type *t; +struct btf *btf; +long offset =0 ; +char *ptr; +int ret; +s32 id; + + ptr =strchr (arg,'.' ); +if (!ptr) +return -EINVAL ; + + *ptr ='\0' ; + + ret = -ENXIO ; + id =bpf_find_btf_id (arg,BTF_KIND_STRUCT , &btf); +if (id <0 ) + goto error; + +/* Get BTF_KIND_FUNC type */ + t =btf_type_by_id (btf, id); + +/* May allow more than one member, as long as they are structures */ +do { + ret = -ENXIO ; +if (!t || !btf_type_is_struct (t)) + goto error; + + *ptr++ ='.' ; + arg = ptr; + ptr =strchr (ptr,'.' ); +if (ptr) + *ptr ='\0' ; + + ret =find_member (arg, btf, &t,0 ); +if (ret <0 ) + goto error; + + offset += ret; + + }while (ptr); + +btf_put (btf); + *offset_p = offset; +return 0 ; + +error: +btf_put (btf); +if (ptr) + *ptr ='.' ; +return ret; +}kernel/trace/trace_btf.h +10
-0@@ -9,3 +9,13 @@ const struct btf_member *btf_find_struct_member(struct btf *btf,const struct btf_type *type, const char *member_name, u32 *anon_offset); + +#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS +/* Will modify arg, but will put it back before returning. */ +int btf_find_offset (char *arg,long *offset); +#else +static inline int btf_find_offset (char *arg,long *offset) +{ +return -EINVAL ; +} +#endif kernel/trace/trace_probe.c +17
-2@@ -1165,7 +1165,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,case '+' :/* deref memory */ case '-' : -if (arg[1 ] =='u' ) { +if (arg[1 ] =='u' &&isdigit (arg[2 ])) { deref =FETCH_OP_UDEREF ; arg[1 ] = arg[0 ]; arg++;@@ -1178,7 +1178,22 @@ parse_probe_arg(char *arg, const struct fetch_type *type,return -EINVAL ; } *tmp ='\0' ; - ret =kstrtol (arg,0 , &offset); +if (arg[0 ] !='-' && !isdigit (*arg)) { +int err =0 ; + ret =btf_find_offset (arg, &offset); +switch (ret) { +case -ENODEV : err =TP_ERR_NOSUP_BTFARG ;break ; +case -E2BIG : err =TP_ERR_MEMBER_TOO_DEEP ;break ; +case -EINVAL : err =TP_ERR_BAD_STRUCT_FMT ;break ; +case -ENXIO : err =TP_ERR_BAD_BTF_TID ;break ; + } +if (err) +__trace_probe_log_err (ctx->offset, err); +if (ret <0 ) +return ret; + }else { + ret =kstrtol (arg,0 , &offset); + }if (ret) {trace_probe_log_err (ctx->offset,BAD_DEREF_OFFS );break ;kernel/trace/trace_probe.h +3
-1@@ -563,7 +563,9 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (NEED_STRING_TYPE ,"$comm and immediate-string only accepts string type" ),\C (TOO_MANY_ARGS ,"Too many arguments are specified" ), \C (TOO_MANY_EARGS ,"Too many entry arguments specified" ), \ -C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), +C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), \ +C (MEMBER_TOO_DEEP ,"Too many indirections of anonymous structure" ), \ +C (BAD_STRUCT_FMT ,"Unknown BTF structure" ), #undef C #define C(a, b) TP_ERR_##aSignature · 1 line
2.53.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerQuoted 7 lines · On Sat, 16 May 2026 17:33:09 -0400 Steven Rostedt <[email protected]> wrote:
Function arguments at kretprobe
-------------------------------kernel/trace/trace_btf.c +0
-0
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 126
Quoted 7 lines
kernel/trace/trace_btf.c +4
-0@@ -120,3 +120,114 @@ const struct btf_member *btf_find_struct_member(struct btf *btf,return member; } +#define BITS_ROUNDDOWN_BYTES (bits) ((bits) >> 3) + +static int find_member (const char *ptr,struct btf *btf, +const struct btf_type **type,int level)
We already have a similar function.
/*
* Find a member of data structure/union by name and return it.
* Return NULL if not found, or -EINVAL if parameter is invalid.
* If the member is an member of anonymous union/structure, the offset
* of that anonymous union/structure is stored into @anon_offset. Caller
* can calculate the correct offset from the root data structure by
* adding anon_offset to the member's offset.
*/
const struct btf_member *btf_find_struct_member(struct btf *btf,
const struct btf_type *type, const char *member_name, u32 *anon_offset)
And this seems useful for you,
Quoted 45 lines
kernel/trace/trace_btf.c +45
-0+{ +const struct btf_member *member; +const struct btf_type *t = *type; +int i; + +/* Max of 3 depth of anonymous structures */ +if (level >3 ) +return -E2BIG ; + +for_each_member (i, t, member) { +const char *tname =btf_name_by_offset (btf, member->name_off); + +if (strcmp (ptr, tname) ==0 ) { +int offset =__btf_member_bit_offset (t, member); + *type =btf_type_by_id (btf, member->type); +return BITS_ROUNDDOWN_BYTES (offset); + } + +/* Handle anonymous structures */ +if (strlen (tname)) +continue ; + + *type =btf_type_by_id (btf, member->type); +if (btf_type_is_struct (*type)) { +int offset =find_member (ptr, btf, type, level +1 ); + +if (offset <0 ) +continue ; + +return offset +BITS_ROUNDDOWN_BYTES (member->offset); + } + } + +return -ENOENT ; +} + +/** +* btf_find_offset - Find an offset of a member for a structure +* @arg: A structure name followed by one or more members +* @offset_p: A pointer to where to store the offset +* +* Will parse @arg with the expected format of: struct.member[[.member]..] +* It is delimited by '.'. The first item must be a structure type. +* The next are its members. If the member is also of a structure type it +* another member may follow ".member".
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 181
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+* +* Note, @arg is modified but will be put back to what it was on return. +* +* Returns: 0 on success and -EINVAL if no '.' is present +* or -ENXIO if the structure or member is not found. +* Returns -EINVAL if BTF is not defined. +* On success, @offset_p will contain the offset of the member specified +* by @arg. +*/ +int btf_find_offset (char *arg,long *offset_p)
And this can share the inner loop of parse_btf_field(). (BTW, parse_btf_field() supports bitfield, but this does not.)
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 184
Quoted 3 lines
kernel/trace/trace_btf.c +3
-0+{ +const struct btf_type *t; +struct btf *btf;
As Sashiko pointed, btf = NULL here.
But I think we would better to have DEFINE_FREE(btf_put).
Quoted 21 lines
kernel/trace/trace_btf.c +21
-0+long offset =0 ; +char *ptr; +int ret; +s32 id; + + ptr =strchr (arg,'.' ); +if (!ptr) +return -EINVAL ; + + *ptr ='\0' ; + + ret = -ENXIO ; + id =bpf_find_btf_id (arg,BTF_KIND_STRUCT , &btf); +if (id <0 ) + goto error; + +/* Get BTF_KIND_FUNC type */ + t =btf_type_by_id (btf, id); + +/* May allow more than one member, as long as they are structures */ + do {
kernel/trace/trace_btf.c const struct btf_member *btf_find_struct_member(struct btf *btf, · line 215
Quoted 10 lines
kernel/trace/trace_btf.c +10
-0+ ret = -ENXIO ; +if (!t || !btf_type_is_struct (t)) + goto error; + + *ptr++ ='.' ; + arg = ptr; + ptr =strchr (ptr,'.' ); +if (ptr) + *ptr ='\0' ; +
So, if you don't share the inner loop, you can just reuse btf_field_struct_member() as below (this should be equivalent to find_member()):
field = btf_find_struct_member(btf, t, arg, &anon_offs);
if (IS_ERR_OR_NULL(field)) {
ret = field ? PTR_ERR(field) : -ENOENT;
goto error;
}
offset += field->offset + anon_offs;Quoted 36 lines
kernel/trace/trace_btf.c +13
-0+ + }while (ptr); + +btf_put (btf); + *offset_p = offset; +return 0 ; + +error: +btf_put (btf); +if (ptr) + *ptr ='.' ; +return ret; +}kernel/trace/trace_btf.h +10
-0@@ -9,3 +9,13 @@ const struct btf_member *btf_find_struct_member(struct btf *btf,const struct btf_type *type, const char *member_name, u32 *anon_offset); + +#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS +/* Will modify arg, but will put it back before returning. */ +int btf_find_offset (char *arg,long *offset); +#else +static inline int btf_find_offset (char *arg,long *offset) +{ +return -EINVAL ; +} +#endif kernel/trace/trace_probe.c +0
-0
kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1168
Quoted 5 lines
kernel/trace/trace_probe.c +1
-1@@ -1165,7 +1165,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type,case '+' :/* deref memory */ case '-' : -if (arg[1 ] =='u' ) { +if (arg[1 ] =='u' &&isdigit (arg[2 ])) {
As Sashiko pointed, This can be broken if STRUCT name starts with "u[0-9]". What about using "()" for this case?
e.g.
+u(user_unreg.disable_addr)(uarg)
Quoted 6 lines
kernel/trace/trace_probe.c +0
-0deref =FETCH_OP_UDEREF ; arg[1 ] = arg[0 ]; arg++;@@ -1178,7 +1178,22 @@ parse_probe_arg(char *arg, const struct fetch_type *type,return -EINVAL ; }
kernel/trace/trace_probe.c parse_probe_arg(char *arg, const struct fetch_type *type, · line 1188
Quoted 10 lines
kernel/trace/trace_probe.c +8
-1*tmp ='\0' ; - ret =kstrtol (arg,0 , &offset); +if (arg[0 ] !='-' && !isdigit (*arg)) { +int err =0 ; + ret =btf_find_offset (arg, &offset); +switch (ret) { +case -ENODEV : err =TP_ERR_NOSUP_BTFARG ;break ; +case -E2BIG : err =TP_ERR_MEMBER_TOO_DEEP ;break ; +case -EINVAL : err =TP_ERR_BAD_STRUCT_FMT ;break ; +case -ENXIO : err =TP_ERR_BAD_BTF_TID ;break ;
This should have -ENOENT and default case for unknown error. (There is TP_ERR_NO_BTF_FIELD already)
Quoted 29 lines
kernel/trace/trace_probe.c +8
-0+ } +if (err) +__trace_probe_log_err (ctx->offset, err); +if (ret <0 ) +return ret; + }else { + ret =kstrtol (arg,0 , &offset); + }if (ret) {trace_probe_log_err (ctx->offset,BAD_DEREF_OFFS );break ;kernel/trace/trace_probe.h +3
-1@@ -563,7 +563,9 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (NEED_STRING_TYPE ,"$comm and immediate-string only accepts string type" ),\C (TOO_MANY_ARGS ,"Too many arguments are specified" ), \C (TOO_MANY_EARGS ,"Too many entry arguments specified" ), \ -C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), +C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), \ +C (MEMBER_TOO_DEEP ,"Too many indirections of anonymous structure" ), \ +C (BAD_STRUCT_FMT ,"Unknown BTF structure" ), #undef C #define C(a, b) TP_ERR_##aSignature · 1 line
2.53.0
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 73 lines · Steven Rostedt
From: Steven Rostedt <[email protected]>
Add syntax to the FETCHARGS parsing of probes to allow the use of structure and member names to get the offsets to dereference pointers.
Currently, a dereference must be a number, where the user has to figure out manually the offset of a member of a structure that they want to reference. For example, to get the size of a
kmem_cachethat was passed to the functionkmem_cache_alloc_noprof, one would need to do:# cd /sys/kernel/tracing # echo 'f:cache kmem_cache_alloc_noprof size=+0x18($arg1):u32' >> dynamic_eventsThis requires knowing that the offset of size is
0x18, which can be found with gdb:(gdb) p &((struct
kmem_cache*)0)->size
$1 = (unsigned int *)0x18If BTF is in the kernel, it can be used to find this with names, where the user doesn't need to find the actual offset:
# echo 'f:cache kmem_cache_alloc_noprof size=+kmem_cache.size($arg1):u32' >> dynamic_eventsInstead of the "+0x18", it would have "+kmem_cache.size" where the format is:
+STRUCT.MEMBER[.MEMBER[..]]
The delimiter is '.' and the first item is the structure name. Then the member of the structure to get the offset of. If that member is an embedded structure, another '.MEMBER' may be added to get the offset of its members with respect to the original value.
"+kmem_cache.size($arg1)" is equivalent to:
(*(struct
kmem_cache*)$arg1).sizeAnonymous structures are also handled:
# echo 'e:xmit net.net_dev_xmit +net_device.name(+sk_buff.dev($skbaddr)):string' >> dynamic_eventsWhere "+net_device.name(+sk_buff.dev($skbaddr))" is equivalent to:
(*(struct
net_device*)((*(structsk_buff*)($skbaddr)).dev)->name)Note that "dev" of struct
sk_buffis inside an anonymous structure:struct sk_buff { union { struct { /* These two members must be first to match sk_buff_head. */ struct sk_buff *next; struct sk_buff *prev; union { struct net_device *dev; [..] }; }; [..] };This will allow up to three deep of anonymous structures before it will fail to find a member.
The above produces:
sshd-session-1080 [000] b..5. 1526.337161: xmit: (net.net_dev_xmit) arg1="enp7s0"And nested structures can be found by adding more members to the arg:
# echo 'f:read filemap_readahead.isra.0 file=+0(+dentry.d_name.name(+file.f_path.dentry($arg2))):string' >> dynamic_events
Quoted 8 lines
The above is equivalent to:
*((*(struct dentry *)(*(struct file *)$arg2).f_path.dentry)->d_name.name)
And produces:
trace-cmd-1381 [002] ...1. 2082.676268: read: (filemap_readahead.isra.0+0x0/0x150) file="trace.dat"
Hi Steve,
Great to see that BTF is going to be nested into trace.
I'm glad to share my BPF tool, bpfsnoop [1], that utilizes the similar way to inspect argument's data.
Read device name:
bpfsnoop -t net_dev_xmit --output-arg 'str(skb->dev->name)' --limit-events 20
- net_dev_xmit[tp] args=((struct sk_buff *)skb=0xffff88818821d4e8, (int)rc=0, (struct net_device *)dev=0xffff88984ba64000, (unsigned int)skb_len=0x1f2/498) cpu=2 process=(0:swapper/2) timestamp=18:06:17.309492697
Arg attrs: (array(char[16]))'str(skb->dev->name)'="eth0"
Read dentry name:
bpfsnoop -k 'vfs_read' --output-arg 'str((file->f_path.dentry)->d_name.name)' --limit-events 20
← vfs_read args=((struct file *)file=0xffff888175e08400, (char *)buf=0x55c7a1168400(0x0/0), (size_t)count=0x10000/65536, (loff_t *)pos=0xffffc9000f707bb0(0)) retval=(long int)510 cpu=3 process=(339834:sudo) timestamp=18:24:16.22021166
Arg attrs: (unsigned char *)'str((file->f_path.dentry)->d_name.name)'="ptmx"
In bpfsnoop, it provides a friendly way to inspect argument's data using C expressions. Under the hood, it compiles the C expressions, specified by --filter-arg/--output-arg, into BPF byte code by parsing the struct/union member access with BTF. (I'm too lazy to write documents to explain its internal details. But you can study it with AI assistance.)
Insanely, after developing such feature for bpfsnoop, I wondered whether to embed a light-weight C compiler into trace tool in order to compile C expression into BPF byte code, and then load the BPF program to filter/output argument. Finally, users are able to filter/output arguments using C expressions. It seemed too crazy for me to post such idea to trace mailing list at that time, as I wasn't familiar with trace infrastructure.
[1] https://github.com/bpfsnoop/bpfsnoop/
Thanks,
Leon