symbol.h 10 KB

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