symbol.h 6.8 KB

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