| 1 | |
| 2 | #include <linux/btf.h>
|
| 3 | #include <linux/kernel.h>
|
| 4 | #include <linux/slab.h>
|
| 5 |
|
| 6 | #include "trace_btf.h"
|
| 7 |
|
| 8 | |
| 9 | * Find a function proto type by name, and return the btf_type with its btf
|
| 10 | * in *@btf_p. Return NULL if not found.
|
| 11 | * Note that caller has to call btf_put(*@btf_p) after using the btf_type.
|
| 12 |
|
| 13 | const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p)
|
| 14 | {
|
| 15 | const struct btf_type *t;
|
| 16 | s32 id;
|
| 17 |
|
| 18 | id = bpf_find_btf_id(func_name, BTF_KIND_FUNC, btf_p);
|
| 19 | if (id < 0)
|
| 20 | return NULL;
|
| 21 |
|
| 22 |
|
| 23 | t = btf_type_by_id(*btf_p, id);
|
| 24 | if (!t || !btf_type_is_func(t))
|
| 25 | goto err;
|
| 26 |
|
| 27 |
|
| 28 | t = btf_type_by_id(*btf_p, t->type);
|
| 29 | if (!t || !btf_type_is_func_proto(t))
|
| 30 | goto err;
|
| 31 |
|
| 32 | return t;
|
| 33 | err:
|
| 34 | btf_put(*btf_p);
|
| 35 | return NULL;
|
| 36 | }
|
| 37 |
|
| 38 | |
| 39 | * Get function parameter with the number of parameters.
|
| 40 | * This can return NULL if the function has no parameters.
|
| 41 | * It can return -EINVAL if the @func_proto is not a function proto type.
|
| 42 |
|
| 43 | const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr)
|
| 44 | {
|
| 45 | if (!btf_type_is_func_proto(func_proto))
|
| 46 | return ERR_PTR(-EINVAL);
|
| 47 |
|
| 48 | *nr = btf_type_vlen(func_proto);
|
| 49 | if (*nr > 0)
|
| 50 | return (const struct btf_param *)(func_proto + 1);
|
| 51 | else
|
| 52 | return NULL;
|
| 53 | }
|
| 54 |
|
| 55 | #define BTF_ANON_STACK_MAX 16
|
| 56 |
|
| 57 | struct btf_anon_stack {
|
| 58 | u32 tid;
|
| 59 | u32 offset;
|
| 60 | };
|
| 61 |
|
| 62 | |
| 63 | * Find a member of data structure/union by name and return it.
|
| 64 | * Return NULL if not found, or ERR_PTR(-EINVAL) if parameter is invalid.
|
| 65 | * If the member is a member of an anonymous union/structure, the bit offset
|
| 66 | * of that anonymous union/structure is stored into @anon_offset.
|
| 67 | * If @member_type is non-NULL, the actual containing structure/union type
|
| 68 | * of the found member is stored into @member_type.
|
| 69 |
|
| 70 | const struct btf_member *btf_find_struct_member(struct btf *btf,
|
| 71 | const struct btf_type *type,
|
| 72 | const char *member_name,
|
| 73 | u32 *anon_offset,
|
| 74 | const struct btf_type **member_type)
|
| 75 | {
|
| 76 | struct btf_anon_stack *anon_stack;
|
| 77 | const struct btf_member *member;
|
| 78 | const struct btf_type *mtype;
|
| 79 | u32 tid, cur_offset = 0;
|
| 80 | const char *name;
|
| 81 | int i, top = 0;
|
| 82 |
|
| 83 | if (!btf_type_is_struct(type))
|
| 84 | return ERR_PTR(-EINVAL);
|
| 85 |
|
| 86 | anon_stack = kzalloc_objs(*anon_stack, BTF_ANON_STACK_MAX);
|
| 87 | if (!anon_stack)
|
| 88 | return ERR_PTR(-ENOMEM);
|
| 89 |
|
| 90 | retry:
|
| 91 | for_each_member(i, type, member) {
|
| 92 | if (!member->name_off) {
|
| 93 |
|
| 94 | mtype = btf_type_skip_modifiers(btf, member->type, &tid);
|
| 95 | if (mtype && btf_type_is_struct(mtype) &&
|
| 96 | top < BTF_ANON_STACK_MAX) {
|
| 97 | anon_stack[top].tid = tid;
|
| 98 | anon_stack[top++].offset = cur_offset +
|
| 99 | __btf_member_bit_offset(type, member);
|
| 100 | }
|
| 101 | } else {
|
| 102 | name = btf_name_by_offset(btf, member->name_off);
|
| 103 | if (name && !strcmp(member_name, name)) {
|
| 104 | if (anon_offset)
|
| 105 | *anon_offset = cur_offset;
|
| 106 | if (member_type)
|
| 107 | *member_type = type;
|
| 108 | goto out;
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 | if (top > 0) {
|
| 113 |
|
| 114 | tid = anon_stack[--top].tid;
|
| 115 | cur_offset = anon_stack[top].offset;
|
| 116 | type = btf_type_by_id(btf, tid);
|
| 117 | goto retry;
|
| 118 | }
|
| 119 | member = NULL;
|
| 120 |
|
| 121 | out:
|
| 122 | kfree(anon_stack);
|
| 123 | return member;
|
| 124 | }
|
| 125 |
|