Skip to contentSkip to conversation

tracing/eprobes: Allow use of BTF names to dereference pointers

PATCH Superseded Steven Rostedt 2 participants 3 messages Jun 1 – Jun 2

Subsystems (2)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH v8 1/1 tracing/eprobes: Allow use of BTF names to dereference pointers

From: Steven Rostedt <[email protected]>

Add syntax to the parsing of eprobes to be able to typecast a trace event field that is a pointer to a structure.

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 dereference.

But for event probes that records a field that happens to be a pointer to a structure, it cannot dereference these values with BTF naming, but must use numerical offsets.

For example, to find out what device a sk_buff is pointing to in the net_dev_xmit trace event, one must first use gdb to find the offsets of the members of the structures:

(gdb) p &((struct sk_buff *)0)->dev
$1 = (struct net_device **) 0x10
(gdb) p &((struct net_device *)0)->name
$2 = (char (*)[16]) 0x118

And then use the raw numbers to dereference:

  # echo 'e:xmit net.net_dev_xmit +0x118(+0x10($skbaddr)):string' >> dynamic_events

If BTF is in the kernel, then instead, the skbaddr can be typecast to sk_buff and use the normal dereference logic.

  # echo 'e:xmit net.net_dev_xmit (sk_buff)skbaddr->dev->name:string' >> dynamic_events
  # echo 1 > events/eprobes/xmit/enable
  # cat trace
[..]
    sshd-session-1022    [000] b..2.   860.249343: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.250061: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.250142: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.263553: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.283820: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.302716: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.322905: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.342828: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.362268: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.382335: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.400856: xmit: (net.net_dev_xmit) arg1="enp7s0"
    sshd-session-1022    [000] b..2.   860.419893: xmit: (net.net_dev_xmit) arg1="enp7s0"

The syntax is simply: (STRUCT)(FIELD)->MEMBER[->MEMBER..]

Also add comments around the #else and #endif of #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS to know what they are for.

---
Changes since v7: https://patch.msgid.link/20260529110442.0967a64c@fedora

- Add error message in parse_btf_args() for failed parsing of TEVENT. (Sashiko)

- Remove TPARG_FL_TYPECAST and just use ctx->struct_btf instead. The flag was redundant and added unnecessary complexity.

- Restructure to keep the lifetime of the TYPECAST to the end of traceprobe_parse_probe_arg_body(). This allows the last_type to stay around in case there's not a type parameter and then btf can still be used.
(Sashiko and Masami Hiramatsu)

 Documentation/trace/eprobetrace.rst |   4 +
 kernel/trace/trace_probe.c          | 173 +++++++++++++++++++++++-----
 kernel/trace/trace_probe.h          |   5 +-
 3 files changed, 154 insertions(+), 28 deletions(-)
Documentation/trace/eprobetrace.rst +4 -0
index 89b5157cfab8..fe3602540569 100644
@@ -46,6 +46,10 @@ Synopsis of eprobe_events
 		  (x8/x16/x32/x64), VFS layer common type(%pd/%pD), "char",
                   "string", "ustring", "symbol", "symstr" and "bitfield" are
                   supported.
+  (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to
+                  a pointer to STRUCT and then derference the pointer defined by
+                  ->MEMBER. Note that when this is used, the FIELD name does not
+                  need to be prefixed with a '$'.
 
 Types
 -----
kernel/trace/trace_probe.c +146 -27

273 lines not on this page · raw message

kernel/trace/trace_probe.h +4 -1
index 1076f1df347b..15758cc11fc6 100644
@@ -422,7 +422,9 @@ struct traceprobe_parse_context {
 	const struct btf_param *params;	/* Parameter of the function */
 	s32 nr_params;			/* The number of the parameters */
 	struct btf *btf;		/* The BTF to be used */
+	struct btf *struct_btf;		/* The BTF to be used for structs */
 	const struct btf_type *last_type;	/* Saved type */
+	const struct btf_type *last_struct;	/* Saved structure */
 	u32 last_bitoffs;		/* Saved bitoffs */
 	u32 last_bitsize;		/* Saved bitsize */
 	struct trace_probe *tp;
@@ -563,7 +565,8 @@ 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(TYPECAST_NOT_EVENT,	"Typecasts are only for eprobe fields"),
 
 #undef C
 #define C(a, b)		TP_ERR_##a
Signature · 1 line
2.53.0
author maintainer
#1 lore raw
Quoted 5 lines · On Mon, 1 Jun 2026 13:07:46 -0400 Steven Rostedt <[email protected]> wrote:

Changes since v7: https://patch.msgid.link/20260529110442.0967a64c@fedora

- Add error message in parse_btf_args() for failed parsing of TEVENT. (Sashiko)

Quoted 8 lines

- Remove TPARG_FL_TYPECAST and just use ctx->struct_btf instead. The flag was redundant and added unnecessary complexity.

- Restructure to keep the lifetime of the TYPECAST to the end of traceprobe_parse_probe_arg_body(). This allows the last_type to stay around in case there's not a type parameter and then btf can still be used.
(Sashiko and Masami Hiramatsu)

And I rebased onto probes/for-next

-- Steve

#2 lore raw
Quoted 11 lines · On Mon, 1 Jun 2026 13:31:29 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 8 lines · On Mon, 1 Jun 2026 13:07:46 -0400 Steven Rostedt <[email protected]> wrote:

Changes since v7: https://patch.msgid.link/20260529110442.0967a64c@fedora

- Add error message in parse_btf_args() for failed parsing of TEVENT. (Sashiko)

- Remove TPARG_FL_TYPECAST and just use ctx->struct_btf instead. The flag was redundant and added unnecessary complexity.

Quoted 8 lines
Quoted 5 lines

- Restructure to keep the lifetime of the TYPECAST to the end of traceprobe_parse_probe_arg_body(). This allows the last_type to stay around in case there's not a type parameter and then btf can still be used.
(Sashiko and Masami Hiramatsu)

And I rebased onto probes/for-next

Thanks, but it seems Sashiko failed to apply (because it is using linux-trace/HEAD branch?) Hmm, we may always need "base-id" tag.

Thanks,

Quoted 1 line

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>