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