Eunomia  0.1.0
A lightweight eBPF-based CloudNative Monitor tool for Container Security and Observability
trace_helpers.h
Go to the documentation of this file.
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);
41 
42 struct partition {
43  char *name;
44  unsigned int dev;
45 };
46 
47 struct partitions;
48 
49 struct partitions *partitions__load(void);
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 */
partitions__get_by_dev
const struct partition * partitions__get_by_dev(const struct partitions *partitions, unsigned int dev)
Definition: trace_helpers.c:880
ksyms__get_symbol
const struct ksym * ksyms__get_symbol(const struct ksyms *ksyms, const char *name)
Definition: trace_helpers.c:178
partitions
Definition: trace_helpers.c:807
partition
Definition: trace_helpers.h:42
syms_cache__free
void syms_cache__free(struct syms_cache *syms_cache)
Definition: trace_helpers.c:774
sym::start
unsigned long start
Definition: trace_helpers.h:25
syms__load_pid
struct syms * syms__load_pid(int tgid)
ksyms__free
void ksyms__free(struct ksyms *ksyms)
Definition: trace_helpers.c:146
partitions__free
void partitions__free(struct partitions *partitions)
Definition: trace_helpers.c:867
ksyms__map_addr
const struct ksym * ksyms__map_addr(const struct ksyms *ksyms, unsigned long addr)
Definition: trace_helpers.c:156
is_kernel_module
bool is_kernel_module(const char *name)
Definition: trace_helpers.c:1002
sym::name
const char * name
Definition: trace_helpers.h:24
kprobe_exists
bool kprobe_exists(const char *name)
Definition: trace_helpers.c:1098
sym
Definition: trace_helpers.h:23
ksym::name
const char * name
Definition: trace_helpers.h:10
syms
Definition: trace_helpers.c:240
partitions__get_by_name
const struct partition * partitions__get_by_name(const struct partitions *partitions, const char *name)
Definition: trace_helpers.c:893
syms__free
void syms__free(struct syms *syms)
Definition: trace_helpers.c:728
partition::dev
unsigned int dev
Definition: trace_helpers.h:44
print_log2_hist
void print_log2_hist(unsigned int *vals, int vals_size, const char *val_type)
Definition: trace_helpers.c:923
syms_cache__new
struct syms_cache * syms_cache__new(int nr)
Definition: trace_helpers.c:762
ksyms__load
struct ksyms * ksyms__load(void)
Definition: trace_helpers.c:104
partitions__load
struct partitions * partitions__load(void)
Definition: trace_helpers.c:830
print_linear_hist
void print_linear_hist(unsigned int *vals, int vals_size, unsigned int base, unsigned int step, const char *val_type)
Definition: trace_helpers.c:963
vmlinux_btf_exists
bool vmlinux_btf_exists(void)
Definition: trace_helpers.c:1154
ksym
Definition: trace_helpers.h:9
ksyms
Definition: trace_helpers.c:42
syms_cache::nr
int nr
Definition: trace_helpers.c:759
module_btf_exists
bool module_btf_exists(const char *mod)
Definition: trace_helpers.c:1161
syms__map_addr
const struct sym * syms__map_addr(const struct syms *syms, unsigned long addr)
Definition: trace_helpers.c:741
syms_cache__get_syms
struct syms * syms_cache__get_syms(struct syms_cache *syms_cache, int tgid)
Definition: trace_helpers.c:787
partition::name
char * name
Definition: trace_helpers.h:43
get_ktime_ns
unsigned long long get_ktime_ns(void)
Definition: trace_helpers.c:994
syms__load_file
struct syms * syms__load_file(const char *fname)
Definition: trace_helpers.c:663
syms_cache
Definition: trace_helpers.c:752
sym::size
unsigned long size
Definition: trace_helpers.h:26
fentry_can_attach
bool fentry_can_attach(const char *name, const char *mod)
Definition: trace_helpers.c:1058
ksym::addr
unsigned long addr
Definition: trace_helpers.h:11