18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef __PERF_SYMBOL 38c2ecf20Sopenharmony_ci#define __PERF_SYMBOL 1 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/types.h> 68c2ecf20Sopenharmony_ci#include <linux/refcount.h> 78c2ecf20Sopenharmony_ci#include <stdbool.h> 88c2ecf20Sopenharmony_ci#include <stdint.h> 98c2ecf20Sopenharmony_ci#include <linux/list.h> 108c2ecf20Sopenharmony_ci#include <linux/rbtree.h> 118c2ecf20Sopenharmony_ci#include <stdio.h> 128c2ecf20Sopenharmony_ci#include "path.h" 138c2ecf20Sopenharmony_ci#include "symbol_conf.h" 148c2ecf20Sopenharmony_ci#include "spark.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#ifdef HAVE_LIBELF_SUPPORT 178c2ecf20Sopenharmony_ci#include <libelf.h> 188c2ecf20Sopenharmony_ci#include <gelf.h> 198c2ecf20Sopenharmony_ci#endif 208c2ecf20Sopenharmony_ci#include <elf.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistruct dso; 238c2ecf20Sopenharmony_cistruct map; 248c2ecf20Sopenharmony_cistruct maps; 258c2ecf20Sopenharmony_cistruct option; 268c2ecf20Sopenharmony_cistruct build_id; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* 298c2ecf20Sopenharmony_ci * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP; 308c2ecf20Sopenharmony_ci * for newer versions we can use mmap to reduce memory usage: 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci#ifdef ELF_C_READ_MMAP 338c2ecf20Sopenharmony_ci# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP 348c2ecf20Sopenharmony_ci#else 358c2ecf20Sopenharmony_ci# define PERF_ELF_C_READ_MMAP ELF_C_READ 368c2ecf20Sopenharmony_ci#endif 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#ifdef HAVE_LIBELF_SUPPORT 398c2ecf20Sopenharmony_ciElf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, 408c2ecf20Sopenharmony_ci GElf_Shdr *shp, const char *name, size_t *idx); 418c2ecf20Sopenharmony_ci#endif 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/** struct symbol - symtab entry 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * @ignore - resolvable but tools ignore it (e.g. idle routines) 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_cistruct symbol { 488c2ecf20Sopenharmony_ci struct rb_node rb_node; 498c2ecf20Sopenharmony_ci u64 start; 508c2ecf20Sopenharmony_ci u64 end; 518c2ecf20Sopenharmony_ci u16 namelen; 528c2ecf20Sopenharmony_ci u8 type:4; 538c2ecf20Sopenharmony_ci u8 binding:4; 548c2ecf20Sopenharmony_ci u8 idle:1; 558c2ecf20Sopenharmony_ci u8 ignore:1; 568c2ecf20Sopenharmony_ci u8 inlined:1; 578c2ecf20Sopenharmony_ci u8 arch_sym; 588c2ecf20Sopenharmony_ci bool annotate2; 598c2ecf20Sopenharmony_ci char name[]; 608c2ecf20Sopenharmony_ci}; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_civoid symbol__delete(struct symbol *sym); 638c2ecf20Sopenharmony_civoid symbols__delete(struct rb_root_cached *symbols); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* symbols__for_each_entry - iterate over symbols (rb_root) 668c2ecf20Sopenharmony_ci * 678c2ecf20Sopenharmony_ci * @symbols: the rb_root of symbols 688c2ecf20Sopenharmony_ci * @pos: the 'struct symbol *' to use as a loop cursor 698c2ecf20Sopenharmony_ci * @nd: the 'struct rb_node *' to use as a temporary storage 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_ci#define symbols__for_each_entry(symbols, pos, nd) \ 728c2ecf20Sopenharmony_ci for (nd = rb_first_cached(symbols); \ 738c2ecf20Sopenharmony_ci nd && (pos = rb_entry(nd, struct symbol, rb_node)); \ 748c2ecf20Sopenharmony_ci nd = rb_next(nd)) 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic inline size_t symbol__size(const struct symbol *sym) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci return sym->end - sym->start; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistruct strlist; 828c2ecf20Sopenharmony_cistruct intlist; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistruct symbol_name_rb_node { 858c2ecf20Sopenharmony_ci struct rb_node rb_node; 868c2ecf20Sopenharmony_ci struct symbol sym; 878c2ecf20Sopenharmony_ci}; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic inline int __symbol__join_symfs(char *bf, size_t size, const char *path) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci return path__join(bf, size, symbol_conf.symfs, path); 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path) 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ciextern int vmlinux_path__nr_entries; 978c2ecf20Sopenharmony_ciextern char **vmlinux_path; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic inline void *symbol__priv(struct symbol *sym) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci return ((void *)sym) - symbol_conf.priv_size; 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistruct ref_reloc_sym { 1058c2ecf20Sopenharmony_ci const char *name; 1068c2ecf20Sopenharmony_ci u64 addr; 1078c2ecf20Sopenharmony_ci u64 unrelocated_addr; 1088c2ecf20Sopenharmony_ci}; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistruct addr_location { 1118c2ecf20Sopenharmony_ci struct thread *thread; 1128c2ecf20Sopenharmony_ci struct maps *maps; 1138c2ecf20Sopenharmony_ci struct map *map; 1148c2ecf20Sopenharmony_ci struct symbol *sym; 1158c2ecf20Sopenharmony_ci const char *srcline; 1168c2ecf20Sopenharmony_ci u64 addr; 1178c2ecf20Sopenharmony_ci char level; 1188c2ecf20Sopenharmony_ci u8 filtered; 1198c2ecf20Sopenharmony_ci u8 cpumode; 1208c2ecf20Sopenharmony_ci s32 cpu; 1218c2ecf20Sopenharmony_ci s32 socket; 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ciint dso__load(struct dso *dso, struct map *map); 1258c2ecf20Sopenharmony_ciint dso__load_vmlinux(struct dso *dso, struct map *map, 1268c2ecf20Sopenharmony_ci const char *vmlinux, bool vmlinux_allocated); 1278c2ecf20Sopenharmony_ciint dso__load_vmlinux_path(struct dso *dso, struct map *map); 1288c2ecf20Sopenharmony_ciint __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map, 1298c2ecf20Sopenharmony_ci bool no_kcore); 1308c2ecf20Sopenharmony_ciint dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_civoid dso__insert_symbol(struct dso *dso, 1338c2ecf20Sopenharmony_ci struct symbol *sym); 1348c2ecf20Sopenharmony_civoid dso__delete_symbol(struct dso *dso, 1358c2ecf20Sopenharmony_ci struct symbol *sym); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistruct symbol *dso__find_symbol(struct dso *dso, u64 addr); 1388c2ecf20Sopenharmony_cistruct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistruct symbol *symbol__next_by_name(struct symbol *sym); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistruct symbol *dso__first_symbol(struct dso *dso); 1438c2ecf20Sopenharmony_cistruct symbol *dso__last_symbol(struct dso *dso); 1448c2ecf20Sopenharmony_cistruct symbol *dso__next_symbol(struct symbol *sym); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cienum dso_type dso__type_fd(int fd); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ciint filename__read_build_id(const char *filename, struct build_id *id); 1498c2ecf20Sopenharmony_ciint sysfs__read_build_id(const char *filename, struct build_id *bid); 1508c2ecf20Sopenharmony_ciint modules__parse(const char *filename, void *arg, 1518c2ecf20Sopenharmony_ci int (*process_module)(void *arg, const char *name, 1528c2ecf20Sopenharmony_ci u64 start, u64 size)); 1538c2ecf20Sopenharmony_ciint filename__read_debuglink(const char *filename, char *debuglink, 1548c2ecf20Sopenharmony_ci size_t size); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistruct perf_env; 1578c2ecf20Sopenharmony_ciint symbol__init(struct perf_env *env); 1588c2ecf20Sopenharmony_civoid symbol__exit(void); 1598c2ecf20Sopenharmony_civoid symbol__elf_init(void); 1608c2ecf20Sopenharmony_ciint symbol__annotation_init(void); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistruct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name); 1638c2ecf20Sopenharmony_cisize_t __symbol__fprintf_symname_offs(const struct symbol *sym, 1648c2ecf20Sopenharmony_ci const struct addr_location *al, 1658c2ecf20Sopenharmony_ci bool unknown_as_addr, 1668c2ecf20Sopenharmony_ci bool print_offsets, FILE *fp); 1678c2ecf20Sopenharmony_cisize_t symbol__fprintf_symname_offs(const struct symbol *sym, 1688c2ecf20Sopenharmony_ci const struct addr_location *al, FILE *fp); 1698c2ecf20Sopenharmony_cisize_t __symbol__fprintf_symname(const struct symbol *sym, 1708c2ecf20Sopenharmony_ci const struct addr_location *al, 1718c2ecf20Sopenharmony_ci bool unknown_as_addr, FILE *fp); 1728c2ecf20Sopenharmony_cisize_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp); 1738c2ecf20Sopenharmony_cisize_t symbol__fprintf(struct symbol *sym, FILE *fp); 1748c2ecf20Sopenharmony_cibool symbol__restricted_filename(const char *filename, 1758c2ecf20Sopenharmony_ci const char *restricted_filename); 1768c2ecf20Sopenharmony_ciint symbol__config_symfs(const struct option *opt __maybe_unused, 1778c2ecf20Sopenharmony_ci const char *dir, int unset __maybe_unused); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistruct symsrc; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci#ifdef HAVE_LIBBFD_SUPPORT 1828c2ecf20Sopenharmony_ciint dso__load_bfd_symbols(struct dso *dso, const char *debugfile); 1838c2ecf20Sopenharmony_ci#endif 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ciint dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, 1868c2ecf20Sopenharmony_ci struct symsrc *runtime_ss, int kmodule); 1878c2ecf20Sopenharmony_ciint dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cichar *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_civoid __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym, 1928c2ecf20Sopenharmony_ci bool kernel); 1938c2ecf20Sopenharmony_civoid symbols__insert(struct rb_root_cached *symbols, struct symbol *sym); 1948c2ecf20Sopenharmony_civoid symbols__fixup_duplicate(struct rb_root_cached *symbols); 1958c2ecf20Sopenharmony_civoid symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms); 1968c2ecf20Sopenharmony_civoid maps__fixup_end(struct maps *maps); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_citypedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data); 1998c2ecf20Sopenharmony_ciint file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data, 2008c2ecf20Sopenharmony_ci bool *is_64_bit); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci#define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX" 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cistruct kcore_extract { 2058c2ecf20Sopenharmony_ci char *kcore_filename; 2068c2ecf20Sopenharmony_ci u64 addr; 2078c2ecf20Sopenharmony_ci u64 offs; 2088c2ecf20Sopenharmony_ci u64 len; 2098c2ecf20Sopenharmony_ci char extract_filename[sizeof(PERF_KCORE_EXTRACT)]; 2108c2ecf20Sopenharmony_ci int fd; 2118c2ecf20Sopenharmony_ci}; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciint kcore_extract__create(struct kcore_extract *kce); 2148c2ecf20Sopenharmony_civoid kcore_extract__delete(struct kcore_extract *kce); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ciint kcore_copy(const char *from_dir, const char *to_dir); 2178c2ecf20Sopenharmony_ciint compare_proc_modules(const char *from, const char *to); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciint setup_list(struct strlist **list, const char *list_str, 2208c2ecf20Sopenharmony_ci const char *list_name); 2218c2ecf20Sopenharmony_ciint setup_intlist(struct intlist **list, const char *list_str, 2228c2ecf20Sopenharmony_ci const char *list_name); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci#ifdef HAVE_LIBELF_SUPPORT 2258c2ecf20Sopenharmony_cibool elf__needs_adjust_symbols(GElf_Ehdr ehdr); 2268c2ecf20Sopenharmony_civoid arch__sym_update(struct symbol *s, GElf_Sym *sym); 2278c2ecf20Sopenharmony_ci#endif 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ciconst char *arch__normalize_symbol_name(const char *name); 2308c2ecf20Sopenharmony_ci#define SYMBOL_A 0 2318c2ecf20Sopenharmony_ci#define SYMBOL_B 1 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ciint arch__compare_symbol_names(const char *namea, const char *nameb); 2348c2ecf20Sopenharmony_ciint arch__compare_symbol_names_n(const char *namea, const char *nameb, 2358c2ecf20Sopenharmony_ci unsigned int n); 2368c2ecf20Sopenharmony_ciint arch__choose_best_symbol(struct symbol *syma, struct symbol *symb); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cienum symbol_tag_include { 2398c2ecf20Sopenharmony_ci SYMBOL_TAG_INCLUDE__NONE = 0, 2408c2ecf20Sopenharmony_ci SYMBOL_TAG_INCLUDE__DEFAULT_ONLY 2418c2ecf20Sopenharmony_ci}; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ciint symbol__match_symbol_name(const char *namea, const char *nameb, 2448c2ecf20Sopenharmony_ci enum symbol_tag_include includes); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci/* structure containing an SDT note's info */ 2478c2ecf20Sopenharmony_cistruct sdt_note { 2488c2ecf20Sopenharmony_ci char *name; /* name of the note*/ 2498c2ecf20Sopenharmony_ci char *provider; /* provider name */ 2508c2ecf20Sopenharmony_ci char *args; 2518c2ecf20Sopenharmony_ci bool bit32; /* whether the location is 32 bits? */ 2528c2ecf20Sopenharmony_ci union { /* location, base and semaphore addrs */ 2538c2ecf20Sopenharmony_ci Elf64_Addr a64[3]; 2548c2ecf20Sopenharmony_ci Elf32_Addr a32[3]; 2558c2ecf20Sopenharmony_ci } addr; 2568c2ecf20Sopenharmony_ci struct list_head note_list; /* SDT notes' list */ 2578c2ecf20Sopenharmony_ci}; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ciint get_sdt_note_list(struct list_head *head, const char *target); 2608c2ecf20Sopenharmony_ciint cleanup_sdt_note_list(struct list_head *sdt_notes); 2618c2ecf20Sopenharmony_ciint sdt_notes__get_count(struct list_head *start); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci#define SDT_PROBES_SCN ".probes" 2648c2ecf20Sopenharmony_ci#define SDT_BASE_SCN ".stapsdt.base" 2658c2ecf20Sopenharmony_ci#define SDT_NOTE_SCN ".note.stapsdt" 2668c2ecf20Sopenharmony_ci#define SDT_NOTE_TYPE 3 2678c2ecf20Sopenharmony_ci#define SDT_NOTE_NAME "stapsdt" 2688c2ecf20Sopenharmony_ci#define NR_ADDR 3 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cienum { 2718c2ecf20Sopenharmony_ci SDT_NOTE_IDX_LOC = 0, 2728c2ecf20Sopenharmony_ci SDT_NOTE_IDX_BASE, 2738c2ecf20Sopenharmony_ci SDT_NOTE_IDX_REFCTR, 2748c2ecf20Sopenharmony_ci}; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_cistruct mem_info *mem_info__new(void); 2778c2ecf20Sopenharmony_cistruct mem_info *mem_info__get(struct mem_info *mi); 2788c2ecf20Sopenharmony_civoid mem_info__put(struct mem_info *mi); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cistatic inline void __mem_info__zput(struct mem_info **mi) 2818c2ecf20Sopenharmony_ci{ 2828c2ecf20Sopenharmony_ci mem_info__put(*mi); 2838c2ecf20Sopenharmony_ci *mi = NULL; 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci#define mem_info__zput(mi) __mem_info__zput(&mi) 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci#endif /* __PERF_SYMBOL */ 289