[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <168960742712.34107.9849785489776347376.stgit@devnote2>
Date: Tue, 18 Jul 2023 00:23:47 +0900
From: "Masami Hiramatsu (Google)" <mhiramat@...nel.org>
To: linux-trace-kernel@...r.kernel.org
Cc: linux-kernel@...r.kernel.org, Steven Rostedt <rostedt@...dmis.org>,
mhiramat@...nel.org, Martin KaFai Lau <martin.lau@...ux.dev>,
bpf@...r.kernel.org, Sven Schnelle <svens@...ux.ibm.com>,
Alexei Starovoitov <ast@...nel.org>
Subject: [PATCH v2 3/9] bpf/btf: Add a function to search a member of a struct/union
From: Masami Hiramatsu (Google) <mhiramat@...nel.org>
Add btf_find_struct_member() API to search a member of a given data structure
or union from the member's name.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@...nel.org>
---
include/linux/btf.h | 3 +++
kernel/bpf/btf.c | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
diff --git a/include/linux/btf.h b/include/linux/btf.h
index 98fbbcdd72ec..097fe9b51562 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -225,6 +225,9 @@ const struct btf_type *btf_find_func_proto(struct btf *btf,
const char *func_name);
const struct btf_param *btf_get_func_param(const struct btf_type *func_proto,
s32 *nr);
+const struct btf_member *btf_find_struct_member(struct btf *btf,
+ const struct btf_type *type,
+ const char *member_name);
#define for_each_member(i, struct_type, member) \
for (i = 0, member = btf_type_member(struct_type); \
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index e015b52956cb..452ffb0393d6 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -1992,6 +1992,44 @@ const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s3
return NULL;
}
+/*
+ * Find a member of data structure/union by name and return it.
+ * Return NULL if not found, or -EINVAL if parameter is invalid.
+ */
+const struct btf_member *btf_find_struct_member(struct btf *btf,
+ const struct btf_type *type,
+ const char *member_name)
+{
+ const struct btf_member *members, *ret;
+ const char *name;
+ int i, vlen;
+
+ if (!btf || !member_name || !btf_type_is_struct(type))
+ return ERR_PTR(-EINVAL);
+
+ vlen = btf_type_vlen(type);
+ members = (const struct btf_member *)(type + 1);
+
+ for (i = 0; i < vlen; i++) {
+ if (!members[i].name_off) {
+ /* unnamed union: dig deeper */
+ type = btf_type_by_id(btf, members[i].type);
+ if (!IS_ERR_OR_NULL(type)) {
+ ret = btf_find_struct_member(btf, type,
+ member_name);
+ if (!IS_ERR_OR_NULL(ret))
+ return ret;
+ }
+ } else {
+ name = btf_name_by_offset(btf, members[i].name_off);
+ if (name && !strcmp(member_name, name))
+ return &members[i];
+ }
+ }
+
+ return NULL;
+}
+
static u32 btf_resolved_type_id(const struct btf *btf, u32 type_id)
{
while (type_id < btf->start_id)
Powered by blists - more mailing lists