Skip to contentSkip to conversation

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

PATCH In review Steven Rostedt 4 participants 13 messages May 22 – May 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v6 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 v5: https://patch.msgid.link/20260519130144.40e71a00@fedora

- Restructure parse_btf_arg() to only use typecast when TEVENT flag is set (eprobes).

- Move found_type label to still test for (!type) in case ctx->last_struct is NULL for some reason.

- Restructure the code to be more specific to when to use ctx->struct_btf and use ctx->btf for places that the typecast will not be used.

- Move parse_trace_event() function outside of BTF #ifdef as it is used outside of it as well.

- Move the code in the '(' switch statement into a helper function called handle_typecast(). This will be a stub when BTF is not configured in.

- Have the ctx->last_struct and ctx->struct_btf only set within the handle_typecast() function, and have that function reset it too.

- Add comments to the #else and #endif of the #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS to know what they are for.

 Documentation/trace/eprobetrace.rst |   4 +
 kernel/trace/trace_probe.c          | 171 +++++++++++++++++++++++-----
 kernel/trace/trace_probe.h          |   7 +-
 3 files changed, 152 insertions(+), 30 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 +142 -29

265 lines not on this page · raw message

kernel/trace/trace_probe.h +6 -1
index 262d8707a3df..952e3d7582b8 100644
@@ -394,6 +394,7 @@ static inline int traceprobe_get_entry_data_size(struct trace_probe *tp)
  * TPARG_FL_KERNEL and TPARG_FL_USER are also mutually exclusive.
  * TPARG_FL_FPROBE and TPARG_FL_TPOINT are optional but it should be with
  * TPARG_FL_KERNEL.
+ * TPARG_FL_TYPECAST is set if an argument was typecast to a structure.
  */
 #define TPARG_FL_RETURN BIT(0)
 #define TPARG_FL_KERNEL BIT(1)
@@ -402,6 +403,7 @@ static inline int traceprobe_get_entry_data_size(struct trace_probe *tp)
 #define TPARG_FL_USER   BIT(4)
 #define TPARG_FL_FPROBE BIT(5)
 #define TPARG_FL_TPOINT BIT(6)
+#define TPARG_FL_TYPECAST BIT(7)
 #define TPARG_FL_LOC_MASK	GENMASK(4, 0)
 
 static inline bool tparg_is_function_entry(unsigned int flags)
@@ -422,7 +424,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 +567,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
maintainer
#1 lore raw

[ Replying to Sashiko: https://sashiko.dev/?list=org.kernel.vger.linux-trace-kernel#/patchset/20260521225033.56458336%40fedora ]

Quoted 4 lines
Quoted 4 lines
kernel/trace/trace_probe.c +0 -0
@@ -653,6 +686,20 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
Quoted 12 lines

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 694

Quoted 6 lines
kernel/trace/trace_probe.c +6 -0
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;
+
+		ret = parse_trace_event(varname, code, ctx);
+		if (ret < 0)
+			return ret;

When parse_trace_event() returns a negative error code (such as -EINVAL or -ENOENT) because a field name is invalid, the error is propagated back up the stack. Does this path miss calling trace_probe_log_err()? If so, users might receive a generic failure without context or a caret pointing to the specific syntax error.

Hmm, there's a comment in the parse_trace_event() that sets ctx->offset for backward compatibility. I'll investigate to see if we can fix that now.

Quoted 32 lines
Quoted 7 lines
kernel/trace/trace_probe.c +7 -0
+
+		if (ctx->flags & TPARG_FL_TYPECAST) {
+			type = ctx->last_struct;
+			goto found_type;
+		}
+		return 0;
+	}

...

Quoted 8 lines
kernel/trace/trace_probe.c +4 -0
@@ -758,7 +806,70 @@ static int parse_btf_bitfield(struct fetch_insn **pcode,
 	return 0;
 }
 
+static int handle_typecast(char *arg, struct fetch_insn **pcode,
+			   struct fetch_insn *end,
+			   struct traceprobe_parse_context *ctx)
+{

...

kernel/trace/trace_probe.c static int parse_btf_bitfield(struct fetch_insn **pcode, · line 864

Quoted 6 lines
kernel/trace/trace_probe.c +6 -0
+	ctx->flags |= TPARG_FL_TYPECAST;
+	tmp++;
+
+	ctx->offset += tmp - arg;
+	ret = parse_btf_arg(tmp, pcode, end, ctx);
+	ctx->flags &= ~TPARG_FL_TYPECAST;

Since TPARG_FL_TYPECAST is cleared here, will it break automatic type inference later?
The traceprobe_parse_context is used for subsequent steps like find_fetch_type_from_btf_type() and check_prepare_btf_string_fetch(), which execute in traceprobe_parse_probe_arg() after this function returns. These functions rely on ctx_btf(), which needs TPARG_FL_TYPECAST to be set to return ctx->struct_btf.

Quoted 12 lines

kernel/trace/trace_probe.c static int parse_btf_bitfield(struct fetch_insn **pcode, · line 867

Quoted 3 lines
kernel/trace/trace_probe.c +3 -0
+	ctx->last_struct = NULL;
+out_put:
+	btf_put(ctx->struct_btf);

Because ctx->struct_btf is not set to NULL after calling btf_put(), can this cause a refcount underflow and use-after-free?
If a user creates an eprobe with multiple typecasted arguments, the shared traceprobe_parse_context means the second argument will enter query_btf_struct() and see ctx->struct_btf is not NULL. It will skip acquiring a new reference but still use the pointer. At the end of parsing the second argument, btf_put(ctx->struct_btf) will be called again unconditionally.

Oops, I forgot to do:

	ctx->struct_buf = NULL;

here.

Will fix.

Thanks,

-- Steve

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+	return ret;
+}
#2 lore raw
Quoted 17 lines · On Fri, 22 May 2026 07:23:22 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines
kernel/trace/trace_probe.c +0 -0
@@ -653,6 +686,20 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
Quoted 12 lines

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 694

Quoted 6 lines
kernel/trace/trace_probe.c +6 -0
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;
+
+		ret = parse_trace_event(varname, code, ctx);
+		if (ret < 0)
+			return ret;  

When parse_trace_event() returns a negative error code (such as -EINVAL or -ENOENT) because a field name is invalid, the error is propagated back up the stack. Does this path miss calling trace_probe_log_err()? If so, users might receive a generic failure without context or a caret pointing to the specific syntax error.

Quoted 2 lines

Hmm, there's a comment in the parse_trace_event() that sets ctx->offset for backward compatibility. I'll investigate to see if we can fix that now.

Masami,

I looked at the code for parse_trace_event() that has:

	/* backward compatibility */
	ctx->offset = 0;
	return -EINVAL;

And it was originally introduced by commit 1b8b0cd754cd ("tracing/probes:
Move event parameter fetching code to common parser"), with:

+ ret = parse_trace_event_arg(arg, code, ctx);
+ if (!ret)
+ return 0;
+ if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
+ code->op = FETCH_OP_COMM;
+ return 0;

+               }
+               /* backward compatibility */
+               ctx->offset = 0;
+               goto inval;

+ }
+

What was the reason for the "backward compatibility"? Can we make it a real error now?

-- Steve

#3 lore raw
Quoted 48 lines · On Fri, 22 May 2026 10:45:21 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 17 lines · On Fri, 22 May 2026 07:23:22 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines
kernel/trace/trace_probe.c +0 -0
@@ -653,6 +686,20 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
Quoted 12 lines

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 694

Quoted 6 lines
kernel/trace/trace_probe.c +6 -0
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;
+
+		ret = parse_trace_event(varname, code, ctx);
+		if (ret < 0)
+			return ret;  

When parse_trace_event() returns a negative error code (such as -EINVAL or -ENOENT) because a field name is invalid, the error is propagated back up the stack. Does this path miss calling trace_probe_log_err()? If so, users might receive a generic failure without context or a caret pointing to the specific syntax error.

Quoted 2 lines

Hmm, there's a comment in the parse_trace_event() that sets ctx->offset for backward compatibility. I'll investigate to see if we can fix that now.

Masami,

I looked at the code for parse_trace_event() that has:

	/* backward compatibility */
	ctx->offset = 0;
	return -EINVAL;

And it was originally introduced by commit 1b8b0cd754cd ("tracing/probes:
Move event parameter fetching code to common parser"), with:

kernel/trace/trace_probe.c +12 -0
+               ret = parse_trace_event_arg(arg, code, ctx);
+               if (!ret)
+                       return 0;
+               if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
+                       code->op = FETCH_OP_COMM;
+                       return 0;
+               }
+               /* backward compatibility */
+               ctx->offset = 0;
+               goto inval;
+       }
+
Quoted 2 lines

What was the reason for the "backward compatibility"? Can we make it a real error now?

This is because a wrong eprobe syntax parser error position indicator.

In tools/testing/selftests/ftrace/test.d/dynevent/eprobes_syntax_errors.tc:

check_error 'e:foo/bar syscalls/sys_enter_openat arg=^dfd' # BAD_FETCH_ARG
check_error 'e:foo/bar syscalls/sys_enter_openat ^arg=$foo' # BAD_ATTACH_ARG

BAD_FETCH_ARG points the fetcharg name correctly, but the BAD_ATTACH_ARG points wrong place in the test case. I think we should fix test case. (Previously, since it was a cleanup, I didn't changed it)

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#4 lore raw
In reply to Steven Rostedt
Quoted 12 lines · On Thu, 21 May 2026 22:50:33 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +12 -0
+static int handle_typecast(char *arg, struct fetch_insn **pcode,
+			   struct fetch_insn *end,
+			   struct traceprobe_parse_context *ctx)
+{
+	char *tmp;
+	int ret;
+
+	/* Currently this only works for eprobes */
+	if (!(ctx->flags & TPARG_FL_TEVENT)) {
+		trace_probe_log_err(ctx->offset, TYPECAST_NOT_EVENT);
+		return -EINVAL;
+	}

kernel/trace/trace_probe.c static int parse_btf_bitfield(struct fetch_insn **pcode, · line 851

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+
+	tmp = strchr(arg, ')');
+	if (!tmp) {
+		trace_probe_log_err(ctx->offset + strlen(arg),
+				    DEREF_OPEN_BRACE);
+		return -EINVAL;
+	}
+	*tmp = '\0';
+	ret = query_btf_struct(arg + 1, ctx);
+	*tmp = ')';

BTW, is there any reason to recover this? The @arg is copied string, see traceprobe_parse_probe_arg_body().

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#6 lore raw
In reply to Steven Rostedt
Quoted 10 lines · On Thu, 21 May 2026 22:50:33 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +1 -1
@@ -640,7 +673,7 @@ static int parse_btf_arg(char *varname,
 	int i, is_ptr, ret;
 	u32 tid;
 
-	if (WARN_ON_ONCE(!ctx->funcname))
+	if (WARN_ON_ONCE(!ctx->funcname && !(ctx->flags & TPARG_FL_TEVENT)))
 		return -EINVAL;
 
 	is_ptr = split_next_field(varname, &field, ctx);

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 690

Quoted 5 lines
kernel/trace/trace_probe.c +2 -0
@@ -653,6 +686,20 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;

nit: parse_btf_arg already declared @ret. So we don't need this.

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#5 lore raw
In reply to Masami Hiramatsu (Google)
Quoted 20 lines · On Mon, 25 May 2026 23:55:07 +0900 Masami Hiramatsu (Google)…
Quoted 17 lines · On Thu, 21 May 2026 22:50:33 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +17 -0
+static int handle_typecast(char *arg, struct fetch_insn **pcode,
+			   struct fetch_insn *end,
+			   struct traceprobe_parse_context *ctx)
+{
+	char *tmp;
+	int ret;
+
+	/* Currently this only works for eprobes */
+	if (!(ctx->flags & TPARG_FL_TEVENT)) {
+		trace_probe_log_err(ctx->offset, TYPECAST_NOT_EVENT);
+		return -EINVAL;
+	}
+
+	tmp = strchr(arg, ')');
+	if (!tmp) {
+		trace_probe_log_err(ctx->offset + strlen(arg),
+				    DEREF_OPEN_BRACE);
Quoted 8 lines

kernel/trace/trace_probe.c static int parse_btf_bitfield(struct fetch_insn **pcode, · line 851

Quoted 5 lines
kernel/trace/trace_probe.c +5 -0
+		return -EINVAL;
+	}
+	*tmp = '\0';
+	ret = query_btf_struct(arg + 1, ctx);
+	*tmp = ')';  

BTW, is there any reason to recover this? The @arg is copied string, see traceprobe_parse_probe_arg_body().

Yeah I know. But it's just something I prefer to do to keep code more robust. That is, don't leave side effects if you can help it. It likely doesn't matter here, but I've always errored on the side of caution. ;-)

-- Steve

#7 lore raw
In reply to Masami Hiramatsu (Google)
Quoted 16 lines · On Tue, 26 May 2026 09:09:10 +0900 Masami Hiramatsu (Google)…
Quoted 13 lines · On Thu, 21 May 2026 22:50:33 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +1 -1
@@ -640,7 +673,7 @@ static int parse_btf_arg(char *varname,
 	int i, is_ptr, ret;
 	u32 tid;
 
-	if (WARN_ON_ONCE(!ctx->funcname))
+	if (WARN_ON_ONCE(!ctx->funcname && !(ctx->flags & TPARG_FL_TEVENT)))
 		return -EINVAL;
 
 	is_ptr = split_next_field(varname, &field, ctx);
@@ -653,6 +686,20 @@ static int parse_btf_arg(char *varname,
 		return -EOPNOTSUPP;
 	}
 
Quoted 4 lines

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 690

Quoted 2 lines
kernel/trace/trace_probe.c +2 -0
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;  

nit: parse_btf_arg already declared @ret. So we don't need this.

Ah, will fix.

Thanks,

-- Steve

#8 lore raw
In reply to Steven Rostedt
Quoted 46 lines · Steven Rostedt

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"
Quoted 4 lines

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.

hi,
this seems to be supported only for argument (pointer) stored in the trace record, not the actual arguments to the tracepoint, is that right?

so I can deref worker from sched.sched_kthread_work_queue_work, like:

echo 'e:myprobe sched.sched_kthread_work_queue_work (kthread_worker)worker->flags (kthread_work)work->canceling' > dynamic_events

but I can't deref sched.sched_process_exec p->pid, like:

  # echo 'e:myprobe sched.sched_process_exec (task_struct)p->pid' > dynamic_events
  bash: echo: write error: Invalid argument

SNIP

Quoted 28 lines
kernel/trace/trace_probe.c +28 -0
+static int handle_typecast(char *arg, struct fetch_insn **pcode,
+			   struct fetch_insn *end,
+			   struct traceprobe_parse_context *ctx)
+{
+	char *tmp;
+	int ret;
+
+	/* Currently this only works for eprobes */
+	if (!(ctx->flags & TPARG_FL_TEVENT)) {
+		trace_probe_log_err(ctx->offset, TYPECAST_NOT_EVENT);
+		return -EINVAL;
+	}
+
+	tmp = strchr(arg, ')');
+	if (!tmp) {
+		trace_probe_log_err(ctx->offset + strlen(arg),
+				    DEREF_OPEN_BRACE);
+		return -EINVAL;
+	}
+	*tmp = '\0';
+	ret = query_btf_struct(arg + 1, ctx);
+	*tmp = ')';
+
+	if (ret < 0) {
+		trace_probe_log_err(ctx->offset + 1, NO_PTR_STRCT);
+		ret = -EINVAL;
+		goto out_put;
+	}

kernel/trace/trace_probe.c static int parse_btf_bitfield(struct fetch_insn **pcode, · line 867

Quoted 10 lines
kernel/trace/trace_probe.c +10 -0
+
+	ctx->flags |= TPARG_FL_TYPECAST;
+	tmp++;
+
+	ctx->offset += tmp - arg;
+	ret = parse_btf_arg(tmp, pcode, end, ctx);
+	ctx->flags &= ~TPARG_FL_TYPECAST;
+	ctx->last_struct = NULL;
+out_put:
+	btf_put(ctx->struct_btf);

should we zero ctx->struct_btf in case there's more type casts, so query_btf_struct would re-init it?

jirka

#9 lore raw
Quoted 8 lines · On Wed, 27 May 2026 10:59:02 +0200 Jiri Olsa <[email protected]> wrote:

hi,
this seems to be supported only for argument (pointer) stored in the trace record, not the actual arguments to the tracepoint, is that right?

so I can deref worker from sched.sched_kthread_work_queue_work, like:

echo 'e:myprobe sched.sched_kthread_work_queue_work (kthread_worker)worker->flags (kthread_work)work->canceling' > dynamic_events

Correct, that is because eprobes "e:" works on the output of a trace event.

Quoted 5 lines

but I can't deref sched.sched_process_exec p->pid, like:

  # echo 'e:myprobe sched.sched_process_exec (task_struct)p->pid' > dynamic_events
  bash: echo: write error: Invalid argument

For function prototypes of a tracepoint, you would use a tprobe "t:"

 # echo 't:exec sched_process_exec pid=p->pid' > dynamic_events
 # echo 1 > events/tracepoints/exec/enable
 # cat trace
# tracer: nop
#
# entries-in-buffer/entries-written: 7/7   #P:8
#
#                                _-----=> irqs-off/BH-disabled
#                               / _----=> need-resched
#                              | / _---=> hardirq/softirq
#                              || / _--=> preempt-depth
#                              ||| / _-=> migrate-disable
#                              |||| /     delay
#           TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
#              | |         |   |||||     |         |
    rtkit-daemon-1935    [005] .....   105.350235: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1935
    rtkit-daemon-1935    [005] .....   105.376609: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1935
 pkla-check-auth-1939    [000] .....   105.404491: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1939
 at-spi-bus-laun-1953    [000] .....   105.914139: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1953
     dbus-daemon-1959    [002] .....   105.919123: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1959
        Xwayland-1961    [006] .....   106.175491: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1961
           <...>-1962    [005] .....   107.406472: exec: (__probestub_sched_process_exec+0x4/0x10) pid=1962

No need for typecasting either ;-)

Quoted 10 lines

kernel/trace/trace_probe.c static int parse_btf_bitfield(struct fetch_insn **pcode, · line 867

Quoted 6 lines
kernel/trace/trace_probe.c +6 -0
+	ctx->offset += tmp - arg;
+	ret = parse_btf_arg(tmp, pcode, end, ctx);
+	ctx->flags &= ~TPARG_FL_TYPECAST;
+	ctx->last_struct = NULL;
+out_put:
+	btf_put(ctx->struct_btf);  

should we zero ctx->struct_btf in case there's more type casts, so query_btf_struct would re-init it?

Yeah, I already mentioned that mistake:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 7 lines

Oops, I forgot to do:

	ctx->struct_buf = NULL;

here.

Will fix.

Thanks for the review!

-- Steve

maintainer
#10 lore raw
Quoted 11 lines · On Wed, 27 May 2026 10:08:15 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines

this seems to be supported only for argument (pointer) stored in the trace record, not the actual arguments to the tracepoint, is that right?

so I can deref worker from sched.sched_kthread_work_queue_work, like:

echo 'e:myprobe sched.sched_kthread_work_queue_work (kthread_worker)worker->flags (kthread_work)work->canceling' > dynamic_events

Correct, that is because eprobes "e:" works on the output of a trace event.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

but I can't deref sched.sched_process_exec p->pid, like:

  # echo 'e:myprobe sched.sched_process_exec (task_struct)p->pid' > dynamic_events
  bash: echo: write error: Invalid argument  

For function prototypes of a tracepoint, you would use a tprobe "t:"

 # echo 't:exec sched_process_exec pid=p->pid' > dynamic_events

Hmm, this is why I need to write a book ;-)

Thanks for helping with what content I need to add!

-- Steve

#11 lore raw
In reply to Steven Rostedt

kernel/trace/trace_probe.c static int parse_btf_arg(char *varname, · line 700

Quoted 12 lines · On Thu, 21 May 2026 22:50:33 -0400 Steven Rostedt <[email protected]> wrote:
kernel/trace/trace_probe.c +12 -0
+	if (ctx->flags & TPARG_FL_TEVENT) {
+		int ret;
+
+		ret = parse_trace_event(varname, code, ctx);
+		if (ret < 0)
+			return ret;
+
+		if (ctx->flags & TPARG_FL_TYPECAST) {
+			type = ctx->last_struct;
+			goto found_type;
+		}
+		return 0;

Here is a bit complicated but a buggy case.

parse_btf_arg() is not used for eprobe arguments because those requires '$' prefix. However, only if it has a typecast, this parse_btf_arg() is called for eprobes.

Thus, we should report a kernel bug if !ctx->flags & TPARG_FL_TYPECAST here. Something like this:

		if (WARN_ONCE(!(ctx->flags & TPARG_FL_TYPECAST)))
			return -EINVAL;
		type = ctx->last_struct;
		goto found_type;

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#12 lore raw
Quoted 7 lines · On Fri, 29 May 2026 13:25:08 +0900 Masami Hiramatsu (Google)…

Thus, we should report a kernel bug if !ctx->flags & TPARG_FL_TYPECAST here. Something like this:

		if (WARN_ONCE(!(ctx->flags & TPARG_FL_TYPECAST)))
			return -EINVAL;
		type = ctx->last_struct;
		goto found_type;

OK, will update it in v7.

Thanks,

-- Steve