symbol.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 type:4;
  53. u8 binding:4;
  54. u8 idle:1;
  55. u8 ignore:1;
  56. u8 inlined:1;
  57. u8 arch_sym;
  58. char name[0];
  59. };
  60. void symbol__delete(struct symbol *sym);
  61. void symbols__delete(struct rb_root *symbols);
  62. /* symbols__for_each_entry - iterate over symbols (rb_root)
  63. *
  64. * @symbols: the rb_root of symbols
  65. * @pos: the 'struct symbol *' to use as a loop cursor
  66. * @nd: the 'struct rb_node *' to use as a temporary storage
  67. */
  68. #define symbols__for_each_entry(symbols, pos, nd) \
  69. for (nd = rb_first(symbols); \
  70. nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
  71. nd = rb_next(nd))
  72. static inline size_t symbol__size(const struct symbol *sym)
  73. {
  74. return sym->end - sym->start;
  75. }
  76. struct strlist;
  77. struct intlist;
  78. struct symbol_conf {
  79. unsigned short priv_size;
  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. event_group,
  99. demangle,
  100. demangle_kernel,
  101. filter_relative,
  102. show_hist_headers,
  103. branch_callstack,
  104. has_filter,
  105. show_ref_callgraph,
  106. hide_unresolved,
  107. raw_trace,
  108. report_hierarchy,
  109. inline_name;
  110. const char *vmlinux_name,
  111. *kallsyms_name,
  112. *source_prefix,
  113. *field_sep;
  114. const char *default_guest_vmlinux_name,
  115. *default_guest_kallsyms,
  116. *default_guest_modules;
  117. const char *guestmount;
  118. const char *dso_list_str,
  119. *comm_list_str,
  120. *pid_list_str,
  121. *tid_list_str,
  122. *sym_list_str,
  123. *col_width_list_str,
  124. *bt_stop_list_str;
  125. struct strlist *dso_list,
  126. *comm_list,
  127. *sym_list,
  128. *dso_from_list,
  129. *dso_to_list,
  130. *sym_from_list,
  131. *sym_to_list,
  132. *bt_stop_list;
  133. struct intlist *pid_list,
  134. *tid_list;
  135. const char *symfs;
  136. };
  137. extern struct symbol_conf symbol_conf;
  138. struct symbol_name_rb_node {
  139. struct rb_node rb_node;
  140. struct symbol sym;
  141. };
  142. static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
  143. {
  144. return path__join(bf, size, symbol_conf.symfs, path);
  145. }
  146. #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
  147. extern int vmlinux_path__nr_entries;
  148. extern char **vmlinux_path;
  149. static inline void *symbol__priv(struct symbol *sym)
  150. {
  151. return ((void *)sym) - symbol_conf.priv_size;
  152. }
  153. struct ref_reloc_sym {
  154. const char *name;
  155. u64 addr;
  156. u64 unrelocated_addr;
  157. };
  158. struct map_symbol {
  159. struct map *map;
  160. struct symbol *sym;
  161. };
  162. struct addr_map_symbol {
  163. struct map *map;
  164. struct symbol *sym;
  165. u64 addr;
  166. u64 al_addr;
  167. u64 phys_addr;
  168. };
  169. struct branch_info {
  170. struct addr_map_symbol from;
  171. struct addr_map_symbol to;
  172. struct branch_flags flags;
  173. char *srcline_from;
  174. char *srcline_to;
  175. };
  176. struct mem_info {
  177. struct addr_map_symbol iaddr;
  178. struct addr_map_symbol daddr;
  179. union perf_mem_data_src data_src;
  180. refcount_t refcnt;
  181. };
  182. struct addr_location {
  183. struct machine *machine;
  184. struct thread *thread;
  185. struct map *map;
  186. struct symbol *sym;
  187. const char *srcline;
  188. u64 addr;
  189. char level;
  190. u8 filtered;
  191. u8 cpumode;
  192. s32 cpu;
  193. s32 socket;
  194. };
  195. struct symsrc {
  196. char *name;
  197. int fd;
  198. enum dso_binary_type type;
  199. #ifdef HAVE_LIBELF_SUPPORT
  200. Elf *elf;
  201. GElf_Ehdr ehdr;
  202. Elf_Scn *opdsec;
  203. size_t opdidx;
  204. GElf_Shdr opdshdr;
  205. Elf_Scn *symtab;
  206. GElf_Shdr symshdr;
  207. Elf_Scn *dynsym;
  208. size_t dynsym_idx;
  209. GElf_Shdr dynshdr;
  210. bool adjust_symbols;
  211. bool is_64_bit;
  212. #endif
  213. };
  214. void symsrc__destroy(struct symsrc *ss);
  215. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  216. enum dso_binary_type type);
  217. bool symsrc__has_symtab(struct symsrc *ss);
  218. bool symsrc__possibly_runtime(struct symsrc *ss);
  219. int dso__load(struct dso *dso, struct map *map);
  220. int dso__load_vmlinux(struct dso *dso, struct map *map,
  221. const char *vmlinux, bool vmlinux_allocated);
  222. int dso__load_vmlinux_path(struct dso *dso, struct map *map);
  223. int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  224. bool no_kcore);
  225. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map);
  226. void dso__insert_symbol(struct dso *dso,
  227. struct symbol *sym);
  228. struct symbol *dso__find_symbol(struct dso *dso, u64 addr);
  229. struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name);
  230. struct symbol *symbol__next_by_name(struct symbol *sym);
  231. struct symbol *dso__first_symbol(struct dso *dso);
  232. struct symbol *dso__last_symbol(struct dso *dso);
  233. struct symbol *dso__next_symbol(struct symbol *sym);
  234. enum dso_type dso__type_fd(int fd);
  235. int filename__read_build_id(const char *filename, void *bf, size_t size);
  236. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  237. int modules__parse(const char *filename, void *arg,
  238. int (*process_module)(void *arg, const char *name,
  239. u64 start, u64 size));
  240. int filename__read_debuglink(const char *filename, char *debuglink,
  241. size_t size);
  242. struct perf_env;
  243. int symbol__init(struct perf_env *env);
  244. void symbol__exit(void);
  245. void symbol__elf_init(void);
  246. int symbol__annotation_init(void);
  247. struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name);
  248. size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
  249. const struct addr_location *al,
  250. bool unknown_as_addr,
  251. bool print_offsets, FILE *fp);
  252. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  253. const struct addr_location *al, FILE *fp);
  254. size_t __symbol__fprintf_symname(const struct symbol *sym,
  255. const struct addr_location *al,
  256. bool unknown_as_addr, FILE *fp);
  257. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  258. size_t symbol__fprintf(struct symbol *sym, FILE *fp);
  259. bool symbol__restricted_filename(const char *filename,
  260. const char *restricted_filename);
  261. int symbol__config_symfs(const struct option *opt __maybe_unused,
  262. const char *dir, int unset __maybe_unused);
  263. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  264. struct symsrc *runtime_ss, int kmodule);
  265. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
  266. char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
  267. void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel);
  268. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  269. void symbols__fixup_duplicate(struct rb_root *symbols);
  270. void symbols__fixup_end(struct rb_root *symbols);
  271. void map_groups__fixup_end(struct map_groups *mg);
  272. typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
  273. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  274. bool *is_64_bit);
  275. #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
  276. struct kcore_extract {
  277. char *kcore_filename;
  278. u64 addr;
  279. u64 offs;
  280. u64 len;
  281. char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
  282. int fd;
  283. };
  284. int kcore_extract__create(struct kcore_extract *kce);
  285. void kcore_extract__delete(struct kcore_extract *kce);
  286. int kcore_copy(const char *from_dir, const char *to_dir);
  287. int compare_proc_modules(const char *from, const char *to);
  288. int setup_list(struct strlist **list, const char *list_str,
  289. const char *list_name);
  290. int setup_intlist(struct intlist **list, const char *list_str,
  291. const char *list_name);
  292. #ifdef HAVE_LIBELF_SUPPORT
  293. bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
  294. void arch__sym_update(struct symbol *s, GElf_Sym *sym);
  295. #endif
  296. const char *arch__normalize_symbol_name(const char *name);
  297. #define SYMBOL_A 0
  298. #define SYMBOL_B 1
  299. void arch__symbols__fixup_end(struct symbol *p, struct symbol *c);
  300. int arch__compare_symbol_names(const char *namea, const char *nameb);
  301. int arch__compare_symbol_names_n(const char *namea, const char *nameb,
  302. unsigned int n);
  303. int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
  304. enum symbol_tag_include {
  305. SYMBOL_TAG_INCLUDE__NONE = 0,
  306. SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
  307. };
  308. int symbol__match_symbol_name(const char *namea, const char *nameb,
  309. enum symbol_tag_include includes);
  310. /* structure containing an SDT note's info */
  311. struct sdt_note {
  312. char *name; /* name of the note*/
  313. char *provider; /* provider name */
  314. char *args;
  315. bool bit32; /* whether the location is 32 bits? */
  316. union { /* location, base and semaphore addrs */
  317. Elf64_Addr a64[3];
  318. Elf32_Addr a32[3];
  319. } addr;
  320. struct list_head note_list; /* SDT notes' list */
  321. };
  322. int get_sdt_note_list(struct list_head *head, const char *target);
  323. int cleanup_sdt_note_list(struct list_head *sdt_notes);
  324. int sdt_notes__get_count(struct list_head *start);
  325. #define SDT_BASE_SCN ".stapsdt.base"
  326. #define SDT_NOTE_SCN ".note.stapsdt"
  327. #define SDT_NOTE_TYPE 3
  328. #define SDT_NOTE_NAME "stapsdt"
  329. #define NR_ADDR 3
  330. struct mem_info *mem_info__new(void);
  331. struct mem_info *mem_info__get(struct mem_info *mi);
  332. void mem_info__put(struct mem_info *mi);
  333. static inline void __mem_info__zput(struct mem_info **mi)
  334. {
  335. mem_info__put(*mi);
  336. *mi = NULL;
  337. }
  338. #define mem_info__zput(mi) __mem_info__zput(&mi)
  339. #endif /* __PERF_SYMBOL */