xref: /linux-6.15/kernel/trace/trace_btf.c (revision ebeed8d4)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/btf.h>
3 #include <linux/kernel.h>
4 
5 #include "trace_btf.h"
6 
7 /*
8  * Find a function proto type by name, and return the btf_type with its btf
9  * in *@btf_p. Return NULL if not found.
10  * Note that caller has to call btf_put(*@btf_p) after using the btf_type.
11  */
12 const struct btf_type *btf_find_func_proto(const char *func_name, struct btf **btf_p)
13 {
14 	const struct btf_type *t;
15 	s32 id;
16 
17 	id = bpf_find_btf_id(func_name, BTF_KIND_FUNC, btf_p);
18 	if (id < 0)
19 		return NULL;
20 
21 	/* Get BTF_KIND_FUNC type */
22 	t = btf_type_by_id(*btf_p, id);
23 	if (!t || !btf_type_is_func(t))
24 		goto err;
25 
26 	/* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */
27 	t = btf_type_by_id(*btf_p, t->type);
28 	if (!t || !btf_type_is_func_proto(t))
29 		goto err;
30 
31 	return t;
32 err:
33 	btf_put(*btf_p);
34 	return NULL;
35 }
36 
37 /*
38  * Get function parameter with the number of parameters.
39  * This can return NULL if the function has no parameters.
40  * It can return -EINVAL if the @func_proto is not a function proto type.
41  */
42 const struct btf_param *btf_get_func_param(const struct btf_type *func_proto, s32 *nr)
43 {
44 	if (!btf_type_is_func_proto(func_proto))
45 		return ERR_PTR(-EINVAL);
46 
47 	*nr = btf_type_vlen(func_proto);
48 	if (*nr > 0)
49 		return (const struct btf_param *)(func_proto + 1);
50 	else
51 		return NULL;
52 }
53 
54