symbol.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef __PERF_SYMBOL
  2. #define __PERF_SYMBOL 1
  3. #include <linux/types.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include "map.h"
  7. #include "../perf.h"
  8. #include <linux/list.h>
  9. #include <linux/rbtree.h>
  10. #include <stdio.h>
  11. #include <byteswap.h>
  12. #include <libgen.h>
  13. #include "build-id.h"
  14. #include "event.h"
  15. #ifdef HAVE_LIBELF_SUPPORT
  16. #include <libelf.h>
  17. #include <gelf.h>
  18. #endif
  19. #include <elf.h>
  20. #include "dso.h"
  21. #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
  22. extern char *cplus_demangle(const char *, int);
  23. static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
  24. {
  25. return cplus_demangle(c, i);
  26. }
  27. #else
  28. #ifdef NO_DEMANGLE
  29. static inline char *bfd_demangle(void __maybe_unused *v,
  30. const char __maybe_unused *c,
  31. int __maybe_unused i)
  32. {
  33. return NULL;
  34. }
  35. #else
  36. #define PACKAGE 'perf'
  37. #include <bfd.h>
  38. #endif
  39. #endif
  40. /*
  41. * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
  42. * for newer versions we can use mmap to reduce memory usage:
  43. */
  44. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  45. # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  46. #else
  47. # define PERF_ELF_C_READ_MMAP ELF_C_READ
  48. #endif
  49. #ifdef HAVE_LIBELF_SUPPORT
  50. extern Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  51. GElf_Shdr *shp, const char *name, size_t *idx);
  52. #endif
  53. #ifndef DMGL_PARAMS
  54. #define DMGL_PARAMS (1 << 0) /* Include function args */
  55. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  56. #endif
  57. /** struct symbol - symtab entry
  58. *
  59. * @ignore - resolvable but tools ignore it (e.g. idle routines)
  60. */
  61. struct symbol {
  62. struct rb_node rb_node;
  63. u64 start;
  64. u64 end;
  65. u16 namelen;
  66. u8 binding;
  67. bool ignore;
  68. char name[0];
  69. };
  70. void symbol__delete(struct symbol *sym);
  71. void symbols__delete(struct rb_root *symbols);
  72. /* symbols__for_each_entry - iterate over symbols (rb_root)
  73. *
  74. * @symbols: the rb_root of symbols
  75. * @pos: the 'struct symbol *' to use as a loop cursor
  76. * @nd: the 'struct rb_node *' to use as a temporary storage
  77. */
  78. #define symbols__for_each_entry(symbols, pos, nd) \
  79. for (nd = rb_first(symbols); \
  80. nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
  81. nd = rb_next(nd))
  82. static inline size_t symbol__size(const struct symbol *sym)
  83. {
  84. return sym->end - sym->start + 1;
  85. }
  86. struct strlist;
  87. struct symbol_conf {
  88. unsigned short priv_size;
  89. unsigned short nr_events;
  90. bool try_vmlinux_path,
  91. ignore_vmlinux,
  92. show_kernel_path,
  93. use_modules,
  94. sort_by_name,
  95. show_nr_samples,
  96. show_total_period,
  97. use_callchain,
  98. cumulate_callchain,
  99. exclude_other,
  100. show_cpu_utilization,
  101. initialized,
  102. kptr_restrict,
  103. annotate_asm_raw,
  104. annotate_src,
  105. event_group,
  106. demangle,
  107. filter_relative,
  108. show_hist_headers;
  109. const char *vmlinux_name,
  110. *kallsyms_name,
  111. *source_prefix,
  112. *field_sep;
  113. const char *default_guest_vmlinux_name,
  114. *default_guest_kallsyms,
  115. *default_guest_modules;
  116. const char *guestmount;
  117. const char *dso_list_str,
  118. *comm_list_str,
  119. *sym_list_str,
  120. *col_width_list_str;
  121. struct strlist *dso_list,
  122. *comm_list,
  123. *sym_list,
  124. *dso_from_list,
  125. *dso_to_list,
  126. *sym_from_list,
  127. *sym_to_list;
  128. const char *symfs;
  129. };
  130. extern struct symbol_conf symbol_conf;
  131. extern int vmlinux_path__nr_entries;
  132. extern char **vmlinux_path;
  133. static inline void *symbol__priv(struct symbol *sym)
  134. {
  135. return ((void *)sym) - symbol_conf.priv_size;
  136. }
  137. struct ref_reloc_sym {
  138. const char *name;
  139. u64 addr;
  140. u64 unrelocated_addr;
  141. };
  142. struct map_symbol {
  143. struct map *map;
  144. struct symbol *sym;
  145. bool unfolded;
  146. bool has_children;
  147. };
  148. struct addr_map_symbol {
  149. struct map *map;
  150. struct symbol *sym;
  151. u64 addr;
  152. u64 al_addr;
  153. };
  154. struct branch_info {
  155. struct addr_map_symbol from;
  156. struct addr_map_symbol to;
  157. struct branch_flags flags;
  158. };
  159. struct mem_info {
  160. struct addr_map_symbol iaddr;
  161. struct addr_map_symbol daddr;
  162. union perf_mem_data_src data_src;
  163. };
  164. struct addr_location {
  165. struct machine *machine;
  166. struct thread *thread;
  167. struct map *map;
  168. struct symbol *sym;
  169. u64 addr;
  170. char level;
  171. u8 filtered;
  172. u8 cpumode;
  173. s32 cpu;
  174. };
  175. struct symsrc {
  176. char *name;
  177. int fd;
  178. enum dso_binary_type type;
  179. #ifdef HAVE_LIBELF_SUPPORT
  180. Elf *elf;
  181. GElf_Ehdr ehdr;
  182. Elf_Scn *opdsec;
  183. size_t opdidx;
  184. GElf_Shdr opdshdr;
  185. Elf_Scn *symtab;
  186. GElf_Shdr symshdr;
  187. Elf_Scn *dynsym;
  188. size_t dynsym_idx;
  189. GElf_Shdr dynshdr;
  190. bool adjust_symbols;
  191. bool is_64_bit;
  192. #endif
  193. };
  194. void symsrc__destroy(struct symsrc *ss);
  195. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  196. enum dso_binary_type type);
  197. bool symsrc__has_symtab(struct symsrc *ss);
  198. bool symsrc__possibly_runtime(struct symsrc *ss);
  199. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
  200. int dso__load_vmlinux(struct dso *dso, struct map *map,
  201. const char *vmlinux, bool vmlinux_allocated,
  202. symbol_filter_t filter);
  203. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  204. symbol_filter_t filter);
  205. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  206. symbol_filter_t filter);
  207. struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
  208. u64 addr);
  209. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  210. const char *name);
  211. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
  212. struct symbol *dso__next_symbol(struct symbol *sym);
  213. enum dso_type dso__type_fd(int fd);
  214. int filename__read_build_id(const char *filename, void *bf, size_t size);
  215. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  216. int modules__parse(const char *filename, void *arg,
  217. int (*process_module)(void *arg, const char *name,
  218. u64 start));
  219. int filename__read_debuglink(const char *filename, char *debuglink,
  220. size_t size);
  221. int symbol__init(void);
  222. void symbol__exit(void);
  223. void symbol__elf_init(void);
  224. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
  225. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  226. const struct addr_location *al, FILE *fp);
  227. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  228. size_t symbol__fprintf(struct symbol *sym, FILE *fp);
  229. bool symbol_type__is_a(char symbol_type, enum map_type map_type);
  230. bool symbol__restricted_filename(const char *filename,
  231. const char *restricted_filename);
  232. bool symbol__is_idle(struct symbol *sym);
  233. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  234. struct symsrc *runtime_ss, symbol_filter_t filter,
  235. int kmodule);
  236. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
  237. struct map *map, symbol_filter_t filter);
  238. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  239. void symbols__fixup_duplicate(struct rb_root *symbols);
  240. void symbols__fixup_end(struct rb_root *symbols);
  241. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
  242. typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
  243. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  244. bool *is_64_bit);
  245. #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
  246. struct kcore_extract {
  247. char *kcore_filename;
  248. u64 addr;
  249. u64 offs;
  250. u64 len;
  251. char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
  252. int fd;
  253. };
  254. int kcore_extract__create(struct kcore_extract *kce);
  255. void kcore_extract__delete(struct kcore_extract *kce);
  256. int kcore_copy(const char *from_dir, const char *to_dir);
  257. int compare_proc_modules(const char *from, const char *to);
  258. int setup_list(struct strlist **list, const char *list_str,
  259. const char *list_name);
  260. #endif /* __PERF_SYMBOL */