symbol.h 10 KB

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