symbol.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_SYMBOL
  3. #define __PERF_SYMBOL 1
  4. #include <linux/types.h>
  5. #include <stdbool.h>
  6. #include <stdint.h>
  7. #include "map.h"
  8. #include "../perf.h"
  9. #include <linux/list.h>
  10. #include <linux/rbtree.h>
  11. #include <stdio.h>
  12. #include <byteswap.h>
  13. #include <libgen.h>
  14. #include "build-id.h"
  15. #include "event.h"
  16. #include "path.h"
  17. #ifdef HAVE_LIBELF_SUPPORT
  18. #include <libelf.h>
  19. #include <gelf.h>
  20. #endif
  21. #include <elf.h>
  22. #include "dso.h"
  23. /*
  24. * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
  25. * for newer versions we can use mmap to reduce memory usage:
  26. */
  27. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  28. # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  29. #else
  30. # define PERF_ELF_C_READ_MMAP ELF_C_READ
  31. #endif
  32. #ifdef HAVE_LIBELF_SUPPORT
  33. Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  34. GElf_Shdr *shp, const char *name, size_t *idx);
  35. #endif
  36. #ifndef DMGL_PARAMS
  37. #define DMGL_NO_OPTS 0 /* For readability... */
  38. #define DMGL_PARAMS (1 << 0) /* Include function args */
  39. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  40. #endif
  41. #define DSO__NAME_KALLSYMS "[kernel.kallsyms]"
  42. #define DSO__NAME_KCORE "[kernel.kcore]"
  43. /** struct symbol - symtab entry
  44. *
  45. * @ignore - resolvable but tools ignore it (e.g. idle routines)
  46. */
  47. struct symbol {
  48. struct rb_node rb_node;
  49. u64 start;
  50. u64 end;
  51. u16 namelen;
  52. u8 binding;
  53. u8 idle:1;
  54. u8 ignore:1;
  55. u8 inlined:1;
  56. u8 arch_sym;
  57. char name[0];
  58. };
  59. void symbol__delete(struct symbol *sym);
  60. void symbols__delete(struct rb_root *symbols);
  61. /* symbols__for_each_entry - iterate over symbols (rb_root)
  62. *
  63. * @symbols: the rb_root of symbols
  64. * @pos: the 'struct symbol *' to use as a loop cursor
  65. * @nd: the 'struct rb_node *' to use as a temporary storage
  66. */
  67. #define symbols__for_each_entry(symbols, pos, nd) \
  68. for (nd = rb_first(symbols); \
  69. nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
  70. nd = rb_next(nd))
  71. static inline size_t symbol__size(const struct symbol *sym)
  72. {
  73. return sym->end - sym->start;
  74. }
  75. struct strlist;
  76. struct intlist;
  77. struct symbol_conf {
  78. unsigned short priv_size;
  79. unsigned short nr_events;
  80. bool try_vmlinux_path,
  81. init_annotation,
  82. force,
  83. ignore_vmlinux,
  84. ignore_vmlinux_buildid,
  85. show_kernel_path,
  86. use_modules,
  87. allow_aliases,
  88. sort_by_name,
  89. show_nr_samples,
  90. show_total_period,
  91. use_callchain,
  92. cumulate_callchain,
  93. show_branchflag_count,
  94. exclude_other,
  95. show_cpu_utilization,
  96. initialized,
  97. kptr_restrict,
  98. annotate_asm_raw,
  99. annotate_src,
  100. event_group,
  101. demangle,
  102. demangle_kernel,
  103. filter_relative,
  104. show_hist_headers,
  105. branch_callstack,
  106. has_filter,
  107. show_ref_callgraph,
  108. hide_unresolved,
  109. raw_trace,
  110. report_hierarchy,
  111. inline_name;
  112. const char *vmlinux_name,
  113. *kallsyms_name,
  114. *source_prefix,
  115. *field_sep;
  116. const char *default_guest_vmlinux_name,
  117. *default_guest_kallsyms,
  118. *default_guest_modules;
  119. const char *guestmount;
  120. const char *dso_list_str,
  121. *comm_list_str,
  122. *pid_list_str,
  123. *tid_list_str,
  124. *sym_list_str,
  125. *col_width_list_str,
  126. *bt_stop_list_str;
  127. struct strlist *dso_list,
  128. *comm_list,
  129. *sym_list,
  130. *dso_from_list,
  131. *dso_to_list,
  132. *sym_from_list,
  133. *sym_to_list,
  134. *bt_stop_list;
  135. struct intlist *pid_list,
  136. *tid_list;
  137. const char *symfs;
  138. };
  139. extern struct symbol_conf symbol_conf;
  140. struct symbol_name_rb_node {
  141. struct rb_node rb_node;
  142. struct symbol sym;
  143. };
  144. static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
  145. {
  146. return path__join(bf, size, symbol_conf.symfs, path);
  147. }
  148. #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
  149. extern int vmlinux_path__nr_entries;
  150. extern char **vmlinux_path;
  151. static inline void *symbol__priv(struct symbol *sym)
  152. {
  153. return ((void *)sym) - symbol_conf.priv_size;
  154. }
  155. struct ref_reloc_sym {
  156. const char *name;
  157. u64 addr;
  158. u64 unrelocated_addr;
  159. };
  160. struct map_symbol {
  161. struct map *map;
  162. struct symbol *sym;
  163. };
  164. struct addr_map_symbol {
  165. struct map *map;
  166. struct symbol *sym;
  167. u64 addr;
  168. u64 al_addr;
  169. u64 phys_addr;
  170. };
  171. struct branch_info {
  172. struct addr_map_symbol from;
  173. struct addr_map_symbol to;
  174. struct branch_flags flags;
  175. char *srcline_from;
  176. char *srcline_to;
  177. };
  178. struct mem_info {
  179. struct addr_map_symbol iaddr;
  180. struct addr_map_symbol daddr;
  181. union perf_mem_data_src data_src;
  182. refcount_t refcnt;
  183. };
  184. struct addr_location {
  185. struct machine *machine;
  186. struct thread *thread;
  187. struct map *map;
  188. struct symbol *sym;
  189. const char *srcline;
  190. u64 addr;
  191. char level;
  192. u8 filtered;
  193. u8 cpumode;
  194. s32 cpu;
  195. s32 socket;
  196. };
  197. struct symsrc {
  198. char *name;
  199. int fd;
  200. enum dso_binary_type type;
  201. #ifdef HAVE_LIBELF_SUPPORT
  202. Elf *elf;
  203. GElf_Ehdr ehdr;
  204. Elf_Scn *opdsec;
  205. size_t opdidx;
  206. GElf_Shdr opdshdr;
  207. Elf_Scn *symtab;
  208. GElf_Shdr symshdr;
  209. Elf_Scn *dynsym;
  210. size_t dynsym_idx;
  211. GElf_Shdr dynshdr;
  212. bool adjust_symbols;
  213. bool is_64_bit;
  214. #endif
  215. };
  216. void symsrc__destroy(struct symsrc *ss);
  217. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  218. enum dso_binary_type type);
  219. bool symsrc__has_symtab(struct symsrc *ss);
  220. bool symsrc__possibly_runtime(struct symsrc *ss);
  221. int dso__load(struct dso *dso, struct map *map);
  222. int dso__load_vmlinux(struct dso *dso, struct map *map,
  223. const char *vmlinux, bool vmlinux_allocated);
  224. int dso__load_vmlinux_path(struct dso *dso, struct map *map);
  225. int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  226. bool no_kcore);
  227. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map);
  228. void dso__insert_symbol(struct dso *dso, enum map_type type,
  229. struct symbol *sym);
  230. struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
  231. u64 addr);
  232. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  233. const char *name);
  234. struct symbol *symbol__next_by_name(struct symbol *sym);
  235. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
  236. struct symbol *dso__last_symbol(struct dso *dso, enum map_type type);
  237. struct symbol *dso__next_symbol(struct symbol *sym);
  238. enum dso_type dso__type_fd(int fd);
  239. int filename__read_build_id(const char *filename, void *bf, size_t size);
  240. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  241. int modules__parse(const char *filename, void *arg,
  242. int (*process_module)(void *arg, const char *name,
  243. u64 start, u64 size));
  244. int filename__read_debuglink(const char *filename, char *debuglink,
  245. size_t size);
  246. struct perf_env;
  247. int symbol__init(struct perf_env *env);
  248. void symbol__exit(void);
  249. void symbol__elf_init(void);
  250. int symbol__annotation_init(void);
  251. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
  252. size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
  253. const struct addr_location *al,
  254. bool unknown_as_addr,
  255. bool print_offsets, FILE *fp);
  256. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  257. const struct addr_location *al, FILE *fp);
  258. size_t __symbol__fprintf_symname(const struct symbol *sym,
  259. const struct addr_location *al,
  260. bool unknown_as_addr, FILE *fp);
  261. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  262. size_t symbol__fprintf(struct symbol *sym, FILE *fp);
  263. bool symbol_type__is_a(char symbol_type, enum map_type map_type);
  264. bool symbol__restricted_filename(const char *filename,
  265. const char *restricted_filename);
  266. int symbol__config_symfs(const struct option *opt __maybe_unused,
  267. const char *dir, int unset __maybe_unused);
  268. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  269. struct symsrc *runtime_ss, int kmodule);
  270. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
  271. struct map *map);
  272. char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
  273. void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel);
  274. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  275. void symbols__fixup_duplicate(struct rb_root *symbols);
  276. void symbols__fixup_end(struct rb_root *symbols);
  277. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
  278. typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
  279. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  280. bool *is_64_bit);
  281. #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
  282. struct kcore_extract {
  283. char *kcore_filename;
  284. u64 addr;
  285. u64 offs;
  286. u64 len;
  287. char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
  288. int fd;
  289. };
  290. int kcore_extract__create(struct kcore_extract *kce);
  291. void kcore_extract__delete(struct kcore_extract *kce);
  292. int kcore_copy(const char *from_dir, const char *to_dir);
  293. int compare_proc_modules(const char *from, const char *to);
  294. int setup_list(struct strlist **list, const char *list_str,
  295. const char *list_name);
  296. int setup_intlist(struct intlist **list, const char *list_str,
  297. const char *list_name);
  298. #ifdef HAVE_LIBELF_SUPPORT
  299. bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
  300. void arch__sym_update(struct symbol *s, GElf_Sym *sym);
  301. #endif
  302. const char *arch__normalize_symbol_name(const char *name);
  303. #define SYMBOL_A 0
  304. #define SYMBOL_B 1
  305. int arch__compare_symbol_names(const char *namea, const char *nameb);
  306. int arch__compare_symbol_names_n(const char *namea, const char *nameb,
  307. unsigned int n);
  308. int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
  309. enum symbol_tag_include {
  310. SYMBOL_TAG_INCLUDE__NONE = 0,
  311. SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
  312. };
  313. int symbol__match_symbol_name(const char *namea, const char *nameb,
  314. enum symbol_tag_include includes);
  315. /* structure containing an SDT note's info */
  316. struct sdt_note {
  317. char *name; /* name of the note*/
  318. char *provider; /* provider name */
  319. char *args;
  320. bool bit32; /* whether the location is 32 bits? */
  321. union { /* location, base and semaphore addrs */
  322. Elf64_Addr a64[3];
  323. Elf32_Addr a32[3];
  324. } addr;
  325. struct list_head note_list; /* SDT notes' list */
  326. };
  327. int get_sdt_note_list(struct list_head *head, const char *target);
  328. int cleanup_sdt_note_list(struct list_head *sdt_notes);
  329. int sdt_notes__get_count(struct list_head *start);
  330. #define SDT_BASE_SCN ".stapsdt.base"
  331. #define SDT_NOTE_SCN ".note.stapsdt"
  332. #define SDT_NOTE_TYPE 3
  333. #define SDT_NOTE_NAME "stapsdt"
  334. #define NR_ADDR 3
  335. struct mem_info *mem_info__new(void);
  336. struct mem_info *mem_info__get(struct mem_info *mi);
  337. void mem_info__put(struct mem_info *mi);
  338. static inline void __mem_info__zput(struct mem_info **mi)
  339. {
  340. mem_info__put(*mi);
  341. *mi = NULL;
  342. }
  343. #define mem_info__zput(mi) __mem_info__zput(&mi)
  344. #endif /* __PERF_SYMBOL */