symbol.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #ifndef NO_LIBELF_SUPPORT
  13. #include <libelf.h>
  14. #include <gelf.h>
  15. #include <elf.h>
  16. #endif
  17. #ifdef HAVE_CPLUS_DEMANGLE
  18. extern char *cplus_demangle(const char *, int);
  19. static inline char *bfd_demangle(void __used *v, const char *c, int i)
  20. {
  21. return cplus_demangle(c, i);
  22. }
  23. #else
  24. #ifdef NO_DEMANGLE
  25. static inline char *bfd_demangle(void __used *v, const char __used *c,
  26. int __used i)
  27. {
  28. return NULL;
  29. }
  30. #else
  31. #include <bfd.h>
  32. #endif
  33. #endif
  34. int hex2u64(const char *ptr, u64 *val);
  35. char *strxfrchar(char *s, char from, char to);
  36. /*
  37. * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
  38. * for newer versions we can use mmap to reduce memory usage:
  39. */
  40. #ifdef LIBELF_NO_MMAP
  41. # define PERF_ELF_C_READ_MMAP ELF_C_READ
  42. #else
  43. # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  44. #endif
  45. #ifndef DMGL_PARAMS
  46. #define DMGL_PARAMS (1 << 0) /* Include function args */
  47. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  48. #endif
  49. #define BUILD_ID_SIZE 20
  50. /** struct symbol - symtab entry
  51. *
  52. * @ignore - resolvable but tools ignore it (e.g. idle routines)
  53. */
  54. struct symbol {
  55. struct rb_node rb_node;
  56. u64 start;
  57. u64 end;
  58. u16 namelen;
  59. u8 binding;
  60. bool ignore;
  61. char name[0];
  62. };
  63. void symbol__delete(struct symbol *sym);
  64. static inline size_t symbol__size(const struct symbol *sym)
  65. {
  66. return sym->end - sym->start + 1;
  67. }
  68. struct strlist;
  69. struct symbol_conf {
  70. unsigned short priv_size;
  71. unsigned short nr_events;
  72. bool try_vmlinux_path,
  73. show_kernel_path,
  74. use_modules,
  75. sort_by_name,
  76. show_nr_samples,
  77. show_total_period,
  78. use_callchain,
  79. exclude_other,
  80. show_cpu_utilization,
  81. initialized,
  82. kptr_restrict,
  83. annotate_asm_raw,
  84. annotate_src;
  85. const char *vmlinux_name,
  86. *kallsyms_name,
  87. *source_prefix,
  88. *field_sep;
  89. const char *default_guest_vmlinux_name,
  90. *default_guest_kallsyms,
  91. *default_guest_modules;
  92. const char *guestmount;
  93. const char *dso_list_str,
  94. *comm_list_str,
  95. *sym_list_str,
  96. *col_width_list_str;
  97. struct strlist *dso_list,
  98. *comm_list,
  99. *sym_list,
  100. *dso_from_list,
  101. *dso_to_list,
  102. *sym_from_list,
  103. *sym_to_list;
  104. const char *symfs;
  105. };
  106. extern struct symbol_conf symbol_conf;
  107. static inline void *symbol__priv(struct symbol *sym)
  108. {
  109. return ((void *)sym) - symbol_conf.priv_size;
  110. }
  111. struct ref_reloc_sym {
  112. const char *name;
  113. u64 addr;
  114. u64 unrelocated_addr;
  115. };
  116. struct map_symbol {
  117. struct map *map;
  118. struct symbol *sym;
  119. bool unfolded;
  120. bool has_children;
  121. };
  122. struct addr_map_symbol {
  123. struct map *map;
  124. struct symbol *sym;
  125. u64 addr;
  126. u64 al_addr;
  127. };
  128. struct branch_info {
  129. struct addr_map_symbol from;
  130. struct addr_map_symbol to;
  131. struct branch_flags flags;
  132. };
  133. struct addr_location {
  134. struct thread *thread;
  135. struct map *map;
  136. struct symbol *sym;
  137. u64 addr;
  138. char level;
  139. bool filtered;
  140. u8 cpumode;
  141. s32 cpu;
  142. };
  143. enum dso_binary_type {
  144. DSO_BINARY_TYPE__KALLSYMS = 0,
  145. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  146. DSO_BINARY_TYPE__VMLINUX,
  147. DSO_BINARY_TYPE__GUEST_VMLINUX,
  148. DSO_BINARY_TYPE__JAVA_JIT,
  149. DSO_BINARY_TYPE__DEBUGLINK,
  150. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  151. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  152. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  153. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  154. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  155. DSO_BINARY_TYPE__GUEST_KMODULE,
  156. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  157. DSO_BINARY_TYPE__NOT_FOUND,
  158. };
  159. enum dso_kernel_type {
  160. DSO_TYPE_USER = 0,
  161. DSO_TYPE_KERNEL,
  162. DSO_TYPE_GUEST_KERNEL
  163. };
  164. enum dso_swap_type {
  165. DSO_SWAP__UNSET,
  166. DSO_SWAP__NO,
  167. DSO_SWAP__YES,
  168. };
  169. #define DSO__DATA_CACHE_SIZE 4096
  170. #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
  171. struct dso_cache {
  172. struct rb_node rb_node;
  173. u64 offset;
  174. u64 size;
  175. char data[0];
  176. };
  177. struct dso {
  178. struct list_head node;
  179. struct rb_root symbols[MAP__NR_TYPES];
  180. struct rb_root symbol_names[MAP__NR_TYPES];
  181. struct rb_root cache;
  182. enum dso_kernel_type kernel;
  183. enum dso_swap_type needs_swap;
  184. enum dso_binary_type symtab_type;
  185. enum dso_binary_type data_type;
  186. u8 adjust_symbols:1;
  187. u8 has_build_id:1;
  188. u8 hit:1;
  189. u8 annotate_warned:1;
  190. u8 sname_alloc:1;
  191. u8 lname_alloc:1;
  192. u8 sorted_by_name;
  193. u8 loaded;
  194. u8 build_id[BUILD_ID_SIZE];
  195. const char *short_name;
  196. char *long_name;
  197. u16 long_name_len;
  198. u16 short_name_len;
  199. char name[0];
  200. };
  201. struct symsrc {
  202. char *name;
  203. int fd;
  204. enum dso_binary_type type;
  205. #ifndef NO_LIBELF_SUPPORT
  206. Elf *elf;
  207. GElf_Ehdr ehdr;
  208. Elf_Scn *opdsec;
  209. size_t opdidx;
  210. GElf_Shdr opdshdr;
  211. Elf_Scn *symtab;
  212. GElf_Shdr symshdr;
  213. Elf_Scn *dynsym;
  214. size_t dynsym_idx;
  215. GElf_Shdr dynshdr;
  216. bool adjust_symbols;
  217. #endif
  218. };
  219. void symsrc__destroy(struct symsrc *ss);
  220. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  221. enum dso_binary_type type);
  222. #define DSO__SWAP(dso, type, val) \
  223. ({ \
  224. type ____r = val; \
  225. BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \
  226. if (dso->needs_swap == DSO_SWAP__YES) { \
  227. switch (sizeof(____r)) { \
  228. case 2: \
  229. ____r = bswap_16(val); \
  230. break; \
  231. case 4: \
  232. ____r = bswap_32(val); \
  233. break; \
  234. case 8: \
  235. ____r = bswap_64(val); \
  236. break; \
  237. default: \
  238. BUG_ON(1); \
  239. } \
  240. } \
  241. ____r; \
  242. })
  243. struct dso *dso__new(const char *name);
  244. void dso__delete(struct dso *dso);
  245. int dso__name_len(const struct dso *dso);
  246. bool dso__loaded(const struct dso *dso, enum map_type type);
  247. bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
  248. static inline void dso__set_loaded(struct dso *dso, enum map_type type)
  249. {
  250. dso->loaded |= (1 << type);
  251. }
  252. void dso__sort_by_name(struct dso *dso, enum map_type type);
  253. void dsos__add(struct list_head *head, struct dso *dso);
  254. struct dso *__dsos__findnew(struct list_head *head, const char *name);
  255. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
  256. int dso__load_vmlinux(struct dso *dso, struct map *map,
  257. const char *vmlinux, symbol_filter_t filter);
  258. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  259. symbol_filter_t filter);
  260. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  261. symbol_filter_t filter);
  262. int machine__load_kallsyms(struct machine *machine, const char *filename,
  263. enum map_type type, symbol_filter_t filter);
  264. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  265. symbol_filter_t filter);
  266. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  267. size_t machine__fprintf_dsos_buildid(struct machine *machine,
  268. FILE *fp, bool with_hits);
  269. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp);
  270. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  271. FILE *fp, bool with_hits);
  272. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  273. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  274. enum map_type type, FILE *fp);
  275. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
  276. char dso__symtab_origin(const struct dso *dso);
  277. void dso__set_long_name(struct dso *dso, char *name);
  278. void dso__set_build_id(struct dso *dso, void *build_id);
  279. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  280. void dso__read_running_kernel_build_id(struct dso *dso,
  281. struct machine *machine);
  282. struct map *dso__new_map(const char *name);
  283. struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
  284. u64 addr);
  285. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  286. const char *name);
  287. int filename__read_build_id(const char *filename, void *bf, size_t size);
  288. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  289. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  290. int build_id__sprintf(const u8 *build_id, int len, char *bf);
  291. int kallsyms__parse(const char *filename, void *arg,
  292. int (*process_symbol)(void *arg, const char *name,
  293. char type, u64 start));
  294. int filename__read_debuglink(const char *filename, char *debuglink,
  295. size_t size);
  296. void machine__destroy_kernel_maps(struct machine *machine);
  297. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
  298. int machine__create_kernel_maps(struct machine *machine);
  299. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid);
  300. int machines__create_guest_kernel_maps(struct rb_root *machines);
  301. void machines__destroy_guest_kernel_maps(struct rb_root *machines);
  302. int symbol__init(void);
  303. void symbol__exit(void);
  304. void symbol__elf_init(void);
  305. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
  306. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  307. const struct addr_location *al, FILE *fp);
  308. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  309. bool symbol_type__is_a(char symbol_type, enum map_type map_type);
  310. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
  311. int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
  312. char *root_dir, char *file, size_t size);
  313. int dso__data_fd(struct dso *dso, struct machine *machine);
  314. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  315. u64 offset, u8 *data, ssize_t size);
  316. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  317. struct machine *machine, u64 addr,
  318. u8 *data, ssize_t size);
  319. int dso__test_data(void);
  320. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *ss,
  321. symbol_filter_t filter, int kmodule, int want_symtab);
  322. int dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
  323. symbol_filter_t filter);
  324. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  325. void symbols__fixup_duplicate(struct rb_root *symbols);
  326. void symbols__fixup_end(struct rb_root *symbols);
  327. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
  328. #endif /* __PERF_SYMBOL */