symbol.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #include "build-id.h"
  14. #include "event.h"
  15. #include "util.h"
  16. #ifdef HAVE_LIBELF_SUPPORT
  17. #include <libelf.h>
  18. #include <gelf.h>
  19. #endif
  20. #include <elf.h>
  21. #include "dso.h"
  22. #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
  23. extern char *cplus_demangle(const char *, int);
  24. static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
  25. {
  26. return cplus_demangle(c, i);
  27. }
  28. #else
  29. #ifdef NO_DEMANGLE
  30. static inline char *bfd_demangle(void __maybe_unused *v,
  31. const char __maybe_unused *c,
  32. int __maybe_unused i)
  33. {
  34. return NULL;
  35. }
  36. #else
  37. #define PACKAGE 'perf'
  38. #include <bfd.h>
  39. #endif
  40. #endif
  41. /*
  42. * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
  43. * for newer versions we can use mmap to reduce memory usage:
  44. */
  45. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  46. # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  47. #else
  48. # define PERF_ELF_C_READ_MMAP ELF_C_READ
  49. #endif
  50. #ifdef HAVE_LIBELF_SUPPORT
  51. extern Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  52. GElf_Shdr *shp, const char *name, size_t *idx);
  53. #endif
  54. #ifndef DMGL_PARAMS
  55. #define DMGL_NO_OPTS 0 /* For readability... */
  56. #define DMGL_PARAMS (1 << 0) /* Include function args */
  57. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  58. #endif
  59. /** struct symbol - symtab entry
  60. *
  61. * @ignore - resolvable but tools ignore it (e.g. idle routines)
  62. */
  63. struct symbol {
  64. struct rb_node rb_node;
  65. u64 start;
  66. u64 end;
  67. u16 namelen;
  68. u8 binding;
  69. bool ignore;
  70. char name[0];
  71. };
  72. void symbol__delete(struct symbol *sym);
  73. void symbols__delete(struct rb_root *symbols);
  74. /* symbols__for_each_entry - iterate over symbols (rb_root)
  75. *
  76. * @symbols: the rb_root of symbols
  77. * @pos: the 'struct symbol *' to use as a loop cursor
  78. * @nd: the 'struct rb_node *' to use as a temporary storage
  79. */
  80. #define symbols__for_each_entry(symbols, pos, nd) \
  81. for (nd = rb_first(symbols); \
  82. nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
  83. nd = rb_next(nd))
  84. static inline size_t symbol__size(const struct symbol *sym)
  85. {
  86. return sym->end - sym->start;
  87. }
  88. struct strlist;
  89. struct symbol_conf {
  90. unsigned short priv_size;
  91. unsigned short nr_events;
  92. bool try_vmlinux_path,
  93. ignore_vmlinux,
  94. show_kernel_path,
  95. use_modules,
  96. sort_by_name,
  97. show_nr_samples,
  98. show_total_period,
  99. use_callchain,
  100. cumulate_callchain,
  101. exclude_other,
  102. show_cpu_utilization,
  103. initialized,
  104. kptr_restrict,
  105. annotate_asm_raw,
  106. annotate_src,
  107. event_group,
  108. demangle,
  109. demangle_kernel,
  110. filter_relative,
  111. show_hist_headers;
  112. const char *vmlinux_name,
  113. *kallsyms_name,
  114. *source_prefix,
  115. *field_sep;
  116. const char *default_guest_vmlinux_name,
  117. *default_guest_kallsyms,
  118. *default_guest_modules;
  119. const char *guestmount;
  120. const char *dso_list_str,
  121. *comm_list_str,
  122. *sym_list_str,
  123. *col_width_list_str;
  124. struct strlist *dso_list,
  125. *comm_list,
  126. *sym_list,
  127. *dso_from_list,
  128. *dso_to_list,
  129. *sym_from_list,
  130. *sym_to_list;
  131. const char *symfs;
  132. };
  133. extern struct symbol_conf symbol_conf;
  134. static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
  135. {
  136. return path__join(bf, size, symbol_conf.symfs, path);
  137. }
  138. #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
  139. extern int vmlinux_path__nr_entries;
  140. extern char **vmlinux_path;
  141. static inline void *symbol__priv(struct symbol *sym)
  142. {
  143. return ((void *)sym) - symbol_conf.priv_size;
  144. }
  145. struct ref_reloc_sym {
  146. const char *name;
  147. u64 addr;
  148. u64 unrelocated_addr;
  149. };
  150. struct map_symbol {
  151. struct map *map;
  152. struct symbol *sym;
  153. bool unfolded;
  154. bool has_children;
  155. };
  156. struct addr_map_symbol {
  157. struct map *map;
  158. struct symbol *sym;
  159. u64 addr;
  160. u64 al_addr;
  161. };
  162. struct branch_info {
  163. struct addr_map_symbol from;
  164. struct addr_map_symbol to;
  165. struct branch_flags flags;
  166. };
  167. struct mem_info {
  168. struct addr_map_symbol iaddr;
  169. struct addr_map_symbol daddr;
  170. union perf_mem_data_src data_src;
  171. };
  172. struct addr_location {
  173. struct machine *machine;
  174. struct thread *thread;
  175. struct map *map;
  176. struct symbol *sym;
  177. u64 addr;
  178. char level;
  179. u8 filtered;
  180. u8 cpumode;
  181. s32 cpu;
  182. };
  183. struct symsrc {
  184. char *name;
  185. int fd;
  186. enum dso_binary_type type;
  187. #ifdef HAVE_LIBELF_SUPPORT
  188. Elf *elf;
  189. GElf_Ehdr ehdr;
  190. Elf_Scn *opdsec;
  191. size_t opdidx;
  192. GElf_Shdr opdshdr;
  193. Elf_Scn *symtab;
  194. GElf_Shdr symshdr;
  195. Elf_Scn *dynsym;
  196. size_t dynsym_idx;
  197. GElf_Shdr dynshdr;
  198. bool adjust_symbols;
  199. bool is_64_bit;
  200. #endif
  201. };
  202. void symsrc__destroy(struct symsrc *ss);
  203. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  204. enum dso_binary_type type);
  205. bool symsrc__has_symtab(struct symsrc *ss);
  206. bool symsrc__possibly_runtime(struct symsrc *ss);
  207. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
  208. int dso__load_vmlinux(struct dso *dso, struct map *map,
  209. const char *vmlinux, bool vmlinux_allocated,
  210. symbol_filter_t filter);
  211. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  212. symbol_filter_t filter);
  213. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  214. symbol_filter_t filter);
  215. struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
  216. u64 addr);
  217. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  218. const char *name);
  219. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
  220. struct symbol *dso__next_symbol(struct symbol *sym);
  221. enum dso_type dso__type_fd(int fd);
  222. int filename__read_build_id(const char *filename, void *bf, size_t size);
  223. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  224. int modules__parse(const char *filename, void *arg,
  225. int (*process_module)(void *arg, const char *name,
  226. u64 start));
  227. int filename__read_debuglink(const char *filename, char *debuglink,
  228. size_t size);
  229. struct perf_session_env;
  230. int symbol__init(struct perf_session_env *env);
  231. void symbol__exit(void);
  232. void symbol__elf_init(void);
  233. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
  234. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  235. const struct addr_location *al, FILE *fp);
  236. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  237. size_t symbol__fprintf(struct symbol *sym, FILE *fp);
  238. bool symbol_type__is_a(char symbol_type, enum map_type map_type);
  239. bool symbol__restricted_filename(const char *filename,
  240. const char *restricted_filename);
  241. bool symbol__is_idle(struct symbol *sym);
  242. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  243. struct symsrc *runtime_ss, symbol_filter_t filter,
  244. int kmodule);
  245. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
  246. struct map *map, symbol_filter_t filter);
  247. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  248. void symbols__fixup_duplicate(struct rb_root *symbols);
  249. void symbols__fixup_end(struct rb_root *symbols);
  250. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
  251. typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
  252. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  253. bool *is_64_bit);
  254. #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
  255. struct kcore_extract {
  256. char *kcore_filename;
  257. u64 addr;
  258. u64 offs;
  259. u64 len;
  260. char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
  261. int fd;
  262. };
  263. int kcore_extract__create(struct kcore_extract *kce);
  264. void kcore_extract__delete(struct kcore_extract *kce);
  265. int kcore_copy(const char *from_dir, const char *to_dir);
  266. int compare_proc_modules(const char *from, const char *to);
  267. int setup_list(struct strlist **list, const char *list_str,
  268. const char *list_name);
  269. #endif /* __PERF_SYMBOL */