symbol.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. bool symsrc__has_symtab(struct symsrc *ss);
  223. #define DSO__SWAP(dso, type, val) \
  224. ({ \
  225. type ____r = val; \
  226. BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \
  227. if (dso->needs_swap == DSO_SWAP__YES) { \
  228. switch (sizeof(____r)) { \
  229. case 2: \
  230. ____r = bswap_16(val); \
  231. break; \
  232. case 4: \
  233. ____r = bswap_32(val); \
  234. break; \
  235. case 8: \
  236. ____r = bswap_64(val); \
  237. break; \
  238. default: \
  239. BUG_ON(1); \
  240. } \
  241. } \
  242. ____r; \
  243. })
  244. struct dso *dso__new(const char *name);
  245. void dso__delete(struct dso *dso);
  246. int dso__name_len(const struct dso *dso);
  247. bool dso__loaded(const struct dso *dso, enum map_type type);
  248. bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
  249. static inline void dso__set_loaded(struct dso *dso, enum map_type type)
  250. {
  251. dso->loaded |= (1 << type);
  252. }
  253. void dso__sort_by_name(struct dso *dso, enum map_type type);
  254. void dsos__add(struct list_head *head, struct dso *dso);
  255. struct dso *__dsos__findnew(struct list_head *head, const char *name);
  256. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
  257. int dso__load_vmlinux(struct dso *dso, struct map *map,
  258. const char *vmlinux, symbol_filter_t filter);
  259. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  260. symbol_filter_t filter);
  261. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  262. symbol_filter_t filter);
  263. int machine__load_kallsyms(struct machine *machine, const char *filename,
  264. enum map_type type, symbol_filter_t filter);
  265. int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
  266. symbol_filter_t filter);
  267. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  268. size_t machine__fprintf_dsos_buildid(struct machine *machine,
  269. FILE *fp, bool with_hits);
  270. size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp);
  271. size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
  272. FILE *fp, bool with_hits);
  273. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  274. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  275. enum map_type type, FILE *fp);
  276. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
  277. char dso__symtab_origin(const struct dso *dso);
  278. void dso__set_long_name(struct dso *dso, char *name);
  279. void dso__set_build_id(struct dso *dso, void *build_id);
  280. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  281. void dso__read_running_kernel_build_id(struct dso *dso,
  282. struct machine *machine);
  283. struct map *dso__new_map(const char *name);
  284. struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
  285. u64 addr);
  286. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  287. const char *name);
  288. int filename__read_build_id(const char *filename, void *bf, size_t size);
  289. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  290. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  291. int build_id__sprintf(const u8 *build_id, int len, char *bf);
  292. int kallsyms__parse(const char *filename, void *arg,
  293. int (*process_symbol)(void *arg, const char *name,
  294. char type, u64 start));
  295. int filename__read_debuglink(const char *filename, char *debuglink,
  296. size_t size);
  297. void machine__destroy_kernel_maps(struct machine *machine);
  298. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
  299. int machine__create_kernel_maps(struct machine *machine);
  300. int machines__create_kernel_maps(struct rb_root *machines, pid_t pid);
  301. int machines__create_guest_kernel_maps(struct rb_root *machines);
  302. void machines__destroy_guest_kernel_maps(struct rb_root *machines);
  303. int symbol__init(void);
  304. void symbol__exit(void);
  305. void symbol__elf_init(void);
  306. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
  307. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  308. const struct addr_location *al, FILE *fp);
  309. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  310. bool symbol_type__is_a(char symbol_type, enum map_type map_type);
  311. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
  312. int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
  313. char *root_dir, char *file, size_t size);
  314. int dso__data_fd(struct dso *dso, struct machine *machine);
  315. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  316. u64 offset, u8 *data, ssize_t size);
  317. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  318. struct machine *machine, u64 addr,
  319. u8 *data, ssize_t size);
  320. int dso__test_data(void);
  321. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  322. struct symsrc *runtime_ss, symbol_filter_t filter,
  323. int kmodule);
  324. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
  325. struct map *map, symbol_filter_t filter);
  326. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  327. void symbols__fixup_duplicate(struct rb_root *symbols);
  328. void symbols__fixup_end(struct rb_root *symbols);
  329. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
  330. #endif /* __PERF_SYMBOL */