symbol.h 7.2 KB

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