symbol.h 7.9 KB

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