xref: /aosp_15_r20/external/bcc/libbpf-tools/trace_helpers.h (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __TRACE_HELPERS_H
3 #define __TRACE_HELPERS_H
4 
5 #include <stdbool.h>
6 
7 #define NSEC_PER_SEC		1000000000ULL
8 
9 struct ksym {
10 	const char *name;
11 	unsigned long addr;
12 };
13 
14 struct ksyms;
15 
16 struct ksyms *ksyms__load(void);
17 void ksyms__free(struct ksyms *ksyms);
18 const struct ksym *ksyms__map_addr(const struct ksyms *ksyms,
19 				   unsigned long addr);
20 const struct ksym *ksyms__get_symbol(const struct ksyms *ksyms,
21 				     const char *name);
22 
23 struct sym {
24 	const char *name;
25 	unsigned long start;
26 	unsigned long size;
27 	unsigned long offset;
28 };
29 
30 struct syms;
31 
32 struct syms *syms__load_pid(int tgid);
33 struct syms *syms__load_file(const char *fname);
34 void syms__free(struct syms *syms);
35 const struct sym *syms__map_addr(const struct syms *syms, unsigned long addr);
36 const struct sym *syms__map_addr_dso(const struct syms *syms, unsigned long addr,
37 				     char **dso_name, unsigned long *dso_offset);
38 
39 struct syms_cache;
40 
41 struct syms_cache *syms_cache__new(int nr);
42 struct syms *syms_cache__get_syms(struct syms_cache *syms_cache, int tgid);
43 void syms_cache__free(struct syms_cache *syms_cache);
44 
45 struct partition {
46 	char *name;
47 	unsigned int dev;
48 };
49 
50 struct partitions;
51 
52 struct partitions *partitions__load(void);
53 void partitions__free(struct partitions *partitions);
54 const struct partition *
55 partitions__get_by_dev(const struct partitions *partitions, unsigned int dev);
56 const struct partition *
57 partitions__get_by_name(const struct partitions *partitions, const char *name);
58 
59 void print_log2_hist(unsigned int *vals, int vals_size, const char *val_type);
60 void print_linear_hist(unsigned int *vals, int vals_size, unsigned int base,
61 		unsigned int step, const char *val_type);
62 
63 unsigned long long get_ktime_ns(void);
64 
65 bool is_kernel_module(const char *name);
66 
67 /*
68  * When attempting to use kprobe/kretprobe, please check out new fentry/fexit
69  * probes, as they provide better performance and usability. But in some
70  * situations we have to fallback to kprobe/kretprobe probes. This helper
71  * is used to detect fentry/fexit support for the specified kernel function.
72  *
73  *	1. A gap between kernel versions, kernel BTF is exposed
74  * 	   starting from 5.4 kernel. but fentry/fexit is actually
75  * 	   supported starting from 5.5.
76  *	2. Whether kernel supports module BTF or not
77  *
78  * *name* is the name of a kernel function to be attached to, which can be
79  * from vmlinux or a kernel module.
80  * *mod* is a hint that indicates the *name* may reside in module BTF,
81  * if NULL, it means *name* belongs to vmlinux.
82  */
83 bool fentry_can_attach(const char *name, const char *mod);
84 
85 /*
86  * The name of a kernel function to be attached to may be changed between
87  * kernel releases. This helper is used to confirm whether the target kernel
88  * uses a certain function name before attaching.
89  *
90  * It is achieved by scaning
91  * 	/sys/kernel/debug/tracing/available_filter_functions
92  * If this file does not exist, it fallbacks to parse /proc/kallsyms,
93  * which is slower.
94  */
95 bool kprobe_exists(const char *name);
96 bool tracepoint_exists(const char *category, const char *event);
97 
98 bool vmlinux_btf_exists(void);
99 bool module_btf_exists(const char *mod);
100 
101 bool probe_tp_btf(const char *name);
102 bool probe_ringbuf();
103 
104 #endif /* __TRACE_HELPERS_H */
105