From: Steven Rostedt <rostedt@goodmis.org>
To: Douglas Raillard <douglas.raillard@arm.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
Linux trace kernel <linux-trace-kernel@vger.kernel.org>,
bpf@vger.kernel.org, Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Mark Rutland <mark.rutland@arm.com>,
Peter Zijlstra <peterz@infradead.org>,
Namhyung Kim <namhyung@kernel.org>,
Takaya Saeki <takayas@google.com>,
Tom Zanussi <zanussi@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ian Rogers <irogers@google.com>,
aahringo@redhat.com
Subject: Re: [PATCH] tracing/probes: Allow use of BTF names to dereference pointers
Date: Thu, 31 Jul 2025 09:29:53 -0400 [thread overview]
Message-ID: <20250731092953.2d8eea47@gandalf.local.home> (raw)
In-Reply-To: <dc817ce7-9551-4365-bd94-3c102a6acda8@arm.com>
On Thu, 31 Jul 2025 12:44:49 +0100
Douglas Raillard <douglas.raillard@arm.com> wrote:
> > 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
>
> Not sure how hard that would be but the type of the expression could probably be inferred from
> BTF as well in some cases. Some cases may be ambiguous (like char* that could be either a buffer
> to display as hex or a null-terminated ASCII string) but BTF would still allow to restrict
> to something sensible (e.g. prevent u32 for a char*).
Hmm, should be possible, but would require passing that information back to
the caller of the BTF lookup function.
> > diff --git a/kernel/trace/trace_btf.c b/kernel/trace/trace_btf.c
> > index 5bbdbcbbde3c..b69404451410 100644
> > --- a/kernel/trace/trace_btf.c
> > +++ b/kernel/trace/trace_btf.c
> > @@ -120,3 +120,109 @@ 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 -1;
> > +
> > + for_each_member(i, t, member) {
> > + const char *tname = btf_name_by_offset(btf, member->name_off);
> > +
> > + if (strcmp(ptr, tname) == 0) {
> > + *type = btf_type_by_id(btf, member->type);
> > + return BITS_ROUNDDOWN_BYTES(member->offset);
>
> member->offset does not only contain the offset, and the offset may not be
> a multiple of 8:
> https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/btf.h#L126
>
> From the BTF spec (https://docs.kernel.org/bpf/btf.html):
>
> If the kind_flag is set, the btf_member.offset contains
> both member bitfield size and bit offset.
> The bitfield size and bit offset are calculated as below.:
>
> #define BTF_MEMBER_BITFIELD_SIZE(val) ((val) >> 24)
> #define BTF_MEMBER_BIT_OFFSET(val) ((val) & 0xffffff)
So basically just need to change that to:
if (strcmp(ptr, tname) == 0) {
int offset = BTF_MEMBER_BIT_OFFSET(member->offset);
*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);
And here too.
-- Steve
> > + }
> > + }
> > +
> > + return -1;
> > +}
> > +
next prev parent reply other threads:[~2025-07-31 13:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-29 15:33 Steven Rostedt
2025-07-29 15:40 ` Steven Rostedt
2025-07-30 13:53 ` Masami Hiramatsu
2025-07-30 14:33 ` Steven Rostedt
2025-07-31 11:44 ` Douglas Raillard
2025-07-31 13:29 ` Steven Rostedt [this message]
2025-07-31 15:42 ` Douglas Raillard
2025-07-31 21:52 ` Jiri Olsa
2025-07-31 22:15 ` Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250731092953.2d8eea47@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=aahringo@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=bpf@vger.kernel.org \
--cc=douglas.raillard@arm.com \
--cc=irogers@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=takayas@google.com \
--cc=tglx@linutronix.de \
--cc=zanussi@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®