Re: [PATCH] tracing/probes: Allow use of BTF names to dereference pointers
From: Jiri Olsa <hidden>
Date: 2025-07-31 21:52:59
Also in:
bpf, lkml
On Tue, Jul 29, 2025 at 11:33:35AM -0400, Steven Rostedt wrote: SNIP
+/**
+ * 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';
+
+ id = bpf_find_btf_id(arg, BTF_KIND_STRUCT, &btf);hi, I think you need to call btf_put(btf) before return jirka
+ 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 {
+ 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);
+
+ *offset_p = offset;
+ return 0;
+
+error:
+ if (ptr)
+ *ptr = '.';
+ return -ENXIO;
+}SNIP