1ebeed8d4SMasami Hiramatsu (Google) // SPDX-License-Identifier: GPL-2.0
2ebeed8d4SMasami Hiramatsu (Google) #include <linux/btf.h>
3ebeed8d4SMasami Hiramatsu (Google) #include <linux/kernel.h>
4302db0f5SMasami Hiramatsu (Google) #include <linux/slab.h>
5ebeed8d4SMasami Hiramatsu (Google)
6ebeed8d4SMasami Hiramatsu (Google) #include "trace_btf.h"
7ebeed8d4SMasami Hiramatsu (Google)
8ebeed8d4SMasami Hiramatsu (Google) /*
9ebeed8d4SMasami Hiramatsu (Google) * Find a function proto type by name, and return the btf_type with its btf
10ebeed8d4SMasami Hiramatsu (Google) * in *@btf_p. Return NULL if not found.
11ebeed8d4SMasami Hiramatsu (Google) * Note that caller has to call btf_put(*@btf_p) after using the btf_type.
12ebeed8d4SMasami Hiramatsu (Google) */
btf_find_func_proto(const char * func_name,struct btf ** btf_p)13ebeed8d4SMasami Hiramatsu (Google) const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p)
14ebeed8d4SMasami Hiramatsu (Google) {
15ebeed8d4SMasami Hiramatsu (Google) const struct btf_type *t;
16ebeed8d4SMasami Hiramatsu (Google) s32 id;
17ebeed8d4SMasami Hiramatsu (Google)
18ebeed8d4SMasami Hiramatsu (Google) id = bpf_find_btf_id(func_name, BTF_KIND_FUNC, btf_p);
19ebeed8d4SMasami Hiramatsu (Google) if (id < 0)
20ebeed8d4SMasami Hiramatsu (Google) return NULL;
21ebeed8d4SMasami Hiramatsu (Google)
22ebeed8d4SMasami Hiramatsu (Google) /* Get BTF_KIND_FUNC type */
23ebeed8d4SMasami Hiramatsu (Google) t = btf_type_by_id(*btf_p, id);
24ebeed8d4SMasami Hiramatsu (Google) if (!t || !btf_type_is_func(t))
25ebeed8d4SMasami Hiramatsu (Google) goto err;
26ebeed8d4SMasami Hiramatsu (Google)
27ebeed8d4SMasami Hiramatsu (Google) /* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */
28ebeed8d4SMasami Hiramatsu (Google) t = btf_type_by_id(*btf_p, t->type);
29ebeed8d4SMasami Hiramatsu (Google) if (!t || !btf_type_is_func_proto(t))
30ebeed8d4SMasami Hiramatsu (Google) goto err;
31ebeed8d4SMasami Hiramatsu (Google)
32ebeed8d4SMasami Hiramatsu (Google) return t;
33ebeed8d4SMasami Hiramatsu (Google) err:
34ebeed8d4SMasami Hiramatsu (Google) btf_put(*btf_p);
35ebeed8d4SMasami Hiramatsu (Google) return NULL;
36ebeed8d4SMasami Hiramatsu (Google) }
37ebeed8d4SMasami Hiramatsu (Google)
38ebeed8d4SMasami Hiramatsu (Google) /*
39ebeed8d4SMasami Hiramatsu (Google) * Get function parameter with the number of parameters.
40ebeed8d4SMasami Hiramatsu (Google) * This can return NULL if the function has no parameters.
41ebeed8d4SMasami Hiramatsu (Google) * It can return -EINVAL if the @func_proto is not a function proto type.
42ebeed8d4SMasami Hiramatsu (Google) */
btf_get_func_param(const struct btf_type * func_proto,s32 * nr)43ebeed8d4SMasami Hiramatsu (Google) const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr)
44ebeed8d4SMasami Hiramatsu (Google) {
45ebeed8d4SMasami Hiramatsu (Google) if (!btf_type_is_func_proto(func_proto))
46ebeed8d4SMasami Hiramatsu (Google) return ERR_PTR(-EINVAL);
47ebeed8d4SMasami Hiramatsu (Google)
48ebeed8d4SMasami Hiramatsu (Google) *nr = btf_type_vlen(func_proto);
49ebeed8d4SMasami Hiramatsu (Google) if (*nr > 0)
50ebeed8d4SMasami Hiramatsu (Google) return (const struct btf_param *)(func_proto + 1);
51ebeed8d4SMasami Hiramatsu (Google) else
52ebeed8d4SMasami Hiramatsu (Google) return NULL;
53ebeed8d4SMasami Hiramatsu (Google) }
54ebeed8d4SMasami Hiramatsu (Google)
55302db0f5SMasami Hiramatsu (Google) #define BTF_ANON_STACK_MAX 16
56302db0f5SMasami Hiramatsu (Google)
57302db0f5SMasami Hiramatsu (Google) struct btf_anon_stack {
58302db0f5SMasami Hiramatsu (Google) u32 tid;
59302db0f5SMasami Hiramatsu (Google) u32 offset;
60302db0f5SMasami Hiramatsu (Google) };
61302db0f5SMasami Hiramatsu (Google)
62302db0f5SMasami Hiramatsu (Google) /*
63302db0f5SMasami Hiramatsu (Google) * Find a member of data structure/union by name and return it.
64302db0f5SMasami Hiramatsu (Google) * Return NULL if not found, or -EINVAL if parameter is invalid.
65302db0f5SMasami Hiramatsu (Google) * If the member is an member of anonymous union/structure, the offset
66302db0f5SMasami Hiramatsu (Google) * of that anonymous union/structure is stored into @anon_offset. Caller
67302db0f5SMasami Hiramatsu (Google) * can calculate the correct offset from the root data structure by
68302db0f5SMasami Hiramatsu (Google) * adding anon_offset to the member's offset.
69302db0f5SMasami Hiramatsu (Google) */
btf_find_struct_member(struct btf * btf,const struct btf_type * type,const char * member_name,u32 * anon_offset)70302db0f5SMasami Hiramatsu (Google) const struct btf_member *btf_find_struct_member(struct btf *btf,
71302db0f5SMasami Hiramatsu (Google) const struct btf_type *type,
72302db0f5SMasami Hiramatsu (Google) const char *member_name,
73302db0f5SMasami Hiramatsu (Google) u32 *anon_offset)
74302db0f5SMasami Hiramatsu (Google) {
75302db0f5SMasami Hiramatsu (Google) struct btf_anon_stack *anon_stack;
76302db0f5SMasami Hiramatsu (Google) const struct btf_member *member;
77302db0f5SMasami Hiramatsu (Google) u32 tid, cur_offset = 0;
78302db0f5SMasami Hiramatsu (Google) const char *name;
79302db0f5SMasami Hiramatsu (Google) int i, top = 0;
80302db0f5SMasami Hiramatsu (Google)
81302db0f5SMasami Hiramatsu (Google) anon_stack = kcalloc(BTF_ANON_STACK_MAX, sizeof(*anon_stack), GFP_KERNEL);
82302db0f5SMasami Hiramatsu (Google) if (!anon_stack)
83302db0f5SMasami Hiramatsu (Google) return ERR_PTR(-ENOMEM);
84302db0f5SMasami Hiramatsu (Google)
85302db0f5SMasami Hiramatsu (Google) retry:
86302db0f5SMasami Hiramatsu (Google) if (!btf_type_is_struct(type)) {
87302db0f5SMasami Hiramatsu (Google) member = ERR_PTR(-EINVAL);
88302db0f5SMasami Hiramatsu (Google) goto out;
89302db0f5SMasami Hiramatsu (Google) }
90302db0f5SMasami Hiramatsu (Google)
91302db0f5SMasami Hiramatsu (Google) for_each_member(i, type, member) {
92302db0f5SMasami Hiramatsu (Google) if (!member->name_off) {
93302db0f5SMasami Hiramatsu (Google) /* Anonymous union/struct: push it for later use */
94*d7679293SMasami Hiramatsu (Google) if (btf_type_skip_modifiers(btf, member->type, &tid) &&
95*d7679293SMasami Hiramatsu (Google) top < BTF_ANON_STACK_MAX) {
96302db0f5SMasami Hiramatsu (Google) anon_stack[top].tid = tid;
97302db0f5SMasami Hiramatsu (Google) anon_stack[top++].offset =
98302db0f5SMasami Hiramatsu (Google) cur_offset + member->offset;
99302db0f5SMasami Hiramatsu (Google) }
100302db0f5SMasami Hiramatsu (Google) } else {
101302db0f5SMasami Hiramatsu (Google) name = btf_name_by_offset(btf, member->name_off);
102302db0f5SMasami Hiramatsu (Google) if (name && !strcmp(member_name, name)) {
103302db0f5SMasami Hiramatsu (Google) if (anon_offset)
104302db0f5SMasami Hiramatsu (Google) *anon_offset = cur_offset;
105302db0f5SMasami Hiramatsu (Google) goto out;
106302db0f5SMasami Hiramatsu (Google) }
107302db0f5SMasami Hiramatsu (Google) }
108302db0f5SMasami Hiramatsu (Google) }
109302db0f5SMasami Hiramatsu (Google) if (top > 0) {
110302db0f5SMasami Hiramatsu (Google) /* Pop from the anonymous stack and retry */
111302db0f5SMasami Hiramatsu (Google) tid = anon_stack[--top].tid;
112302db0f5SMasami Hiramatsu (Google) cur_offset = anon_stack[top].offset;
113302db0f5SMasami Hiramatsu (Google) type = btf_type_by_id(btf, tid);
114302db0f5SMasami Hiramatsu (Google) goto retry;
115302db0f5SMasami Hiramatsu (Google) }
116302db0f5SMasami Hiramatsu (Google) member = NULL;
117302db0f5SMasami Hiramatsu (Google)
118302db0f5SMasami Hiramatsu (Google) out:
119302db0f5SMasami Hiramatsu (Google) kfree(anon_stack);
120302db0f5SMasami Hiramatsu (Google) return member;
121302db0f5SMasami Hiramatsu (Google) }
122302db0f5SMasami Hiramatsu (Google)
123