• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
28 
29 struct syms;
30 
31 struct syms *syms__load_pid(int tgid);
32 struct syms *syms__load_file(const char *fname);
33 void syms__free(struct syms *syms);
34 const struct sym *syms__map_addr(const struct syms *syms, unsigned long addr);
35 
36 struct syms_cache;
37 
38 struct syms_cache *syms_cache__new(int nr);
39 struct syms *syms_cache__get_syms(struct syms_cache *syms_cache, int tgid);
40 void syms_cache__free(struct syms_cache *syms_cache);
41 
42 struct partition {
43 	char *name;
44 	unsigned int dev;
45 };
46 
47 struct partitions;
48 
49 struct partitions *partitions__load(void);
50 void partitions__free(struct partitions *partitions);
51 const struct partition *
52 partitions__get_by_dev(const struct partitions *partitions, unsigned int dev);
53 const struct partition *
54 partitions__get_by_name(const struct partitions *partitions, const char *name);
55 
56 void print_log2_hist(unsigned int *vals, int vals_size, const char *val_type);
57 void print_linear_hist(unsigned int *vals, int vals_size, unsigned int base,
58 		unsigned int step, const char *val_type);
59 
60 unsigned long long get_ktime_ns(void);
61 
62 bool is_kernel_module(const char *name);
63 
64 /*
65  * When attempting to use kprobe/kretprobe, please check out new fentry/fexit
66  * probes, as they provide better performance and usability. But in some
67  * situations we have to fallback to kprobe/kretprobe probes. This helper
68  * is used to detect fentry/fexit support for the specified kernel function.
69  *
70  *	1. A gap between kernel versions, kernel BTF is exposed
71  * 	   starting from 5.4 kernel. but fentry/fexit is actually
72  * 	   supported starting from 5.5.
73  *	2. Whether kernel supports module BTF or not
74  *
75  * *name* is the name of a kernel function to be attached to, which can be
76  * from vmlinux or a kernel module.
77  * *mod* is a hint that indicates the *name* may reside in module BTF,
78  * if NULL, it means *name* belongs to vmlinux.
79  */
80 bool fentry_can_attach(const char *name, const char *mod);
81 
82 /*
83  * The name of a kernel function to be attached to may be changed between
84  * kernel releases. This helper is used to confirm whether the target kernel
85  * uses a certain function name before attaching.
86  *
87  * It is achieved by scaning
88  * 	/sys/kernel/debug/tracing/available_filter_functions
89  * If this file does not exist, it fallbacks to parse /proc/kallsyms,
90  * which is slower.
91  */
92 bool kprobe_exists(const char *name);
93 
94 bool vmlinux_btf_exists(void);
95 bool module_btf_exists(const char *mod);
96 
97 #endif /* __TRACE_HELPERS_H */
98