symbol.h 8.5 KB

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