symbol.h 6.6 KB

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