symbol.h 10 KB

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