symbol.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. 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. #define DSO__NAME_KALLSYMS "[kernel.kallsyms]"
  41. #define DSO__NAME_KCORE "[kernel.kcore]"
  42. /** struct symbol - symtab entry
  43. *
  44. * @ignore - resolvable but tools ignore it (e.g. idle routines)
  45. */
  46. struct symbol {
  47. struct rb_node rb_node;
  48. u64 start;
  49. u64 end;
  50. u16 namelen;
  51. u8 binding;
  52. u8 idle:1;
  53. u8 ignore:1;
  54. u8 arch_sym;
  55. char name[0];
  56. };
  57. void symbol__delete(struct symbol *sym);
  58. void symbols__delete(struct rb_root *symbols);
  59. /* symbols__for_each_entry - iterate over symbols (rb_root)
  60. *
  61. * @symbols: the rb_root of symbols
  62. * @pos: the 'struct symbol *' to use as a loop cursor
  63. * @nd: the 'struct rb_node *' to use as a temporary storage
  64. */
  65. #define symbols__for_each_entry(symbols, pos, nd) \
  66. for (nd = rb_first(symbols); \
  67. nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
  68. nd = rb_next(nd))
  69. static inline size_t symbol__size(const struct symbol *sym)
  70. {
  71. return sym->end - sym->start;
  72. }
  73. struct strlist;
  74. struct intlist;
  75. struct symbol_conf {
  76. unsigned short priv_size;
  77. unsigned short nr_events;
  78. bool try_vmlinux_path,
  79. init_annotation,
  80. force,
  81. ignore_vmlinux,
  82. ignore_vmlinux_buildid,
  83. show_kernel_path,
  84. use_modules,
  85. allow_aliases,
  86. sort_by_name,
  87. show_nr_samples,
  88. show_total_period,
  89. use_callchain,
  90. cumulate_callchain,
  91. show_branchflag_count,
  92. exclude_other,
  93. show_cpu_utilization,
  94. initialized,
  95. kptr_restrict,
  96. annotate_asm_raw,
  97. annotate_src,
  98. event_group,
  99. demangle,
  100. demangle_kernel,
  101. filter_relative,
  102. show_hist_headers,
  103. branch_callstack,
  104. has_filter,
  105. show_ref_callgraph,
  106. hide_unresolved,
  107. raw_trace,
  108. report_hierarchy;
  109. const char *vmlinux_name,
  110. *kallsyms_name,
  111. *source_prefix,
  112. *field_sep;
  113. const char *default_guest_vmlinux_name,
  114. *default_guest_kallsyms,
  115. *default_guest_modules;
  116. const char *guestmount;
  117. const char *dso_list_str,
  118. *comm_list_str,
  119. *pid_list_str,
  120. *tid_list_str,
  121. *sym_list_str,
  122. *col_width_list_str,
  123. *bt_stop_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. *bt_stop_list;
  132. struct intlist *pid_list,
  133. *tid_list;
  134. const char *symfs;
  135. };
  136. extern struct symbol_conf symbol_conf;
  137. struct symbol_name_rb_node {
  138. struct rb_node rb_node;
  139. struct symbol sym;
  140. };
  141. static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
  142. {
  143. return path__join(bf, size, symbol_conf.symfs, path);
  144. }
  145. #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
  146. extern int vmlinux_path__nr_entries;
  147. extern char **vmlinux_path;
  148. static inline void *symbol__priv(struct symbol *sym)
  149. {
  150. return ((void *)sym) - symbol_conf.priv_size;
  151. }
  152. struct ref_reloc_sym {
  153. const char *name;
  154. u64 addr;
  155. u64 unrelocated_addr;
  156. };
  157. struct map_symbol {
  158. struct map *map;
  159. struct symbol *sym;
  160. };
  161. struct addr_map_symbol {
  162. struct map *map;
  163. struct symbol *sym;
  164. u64 addr;
  165. u64 al_addr;
  166. };
  167. struct branch_info {
  168. struct addr_map_symbol from;
  169. struct addr_map_symbol to;
  170. struct branch_flags flags;
  171. char *srcline_from;
  172. char *srcline_to;
  173. };
  174. struct mem_info {
  175. struct addr_map_symbol iaddr;
  176. struct addr_map_symbol daddr;
  177. union perf_mem_data_src data_src;
  178. };
  179. struct addr_location {
  180. struct machine *machine;
  181. struct thread *thread;
  182. struct map *map;
  183. struct symbol *sym;
  184. u64 addr;
  185. char level;
  186. u8 filtered;
  187. u8 cpumode;
  188. s32 cpu;
  189. s32 socket;
  190. };
  191. struct symsrc {
  192. char *name;
  193. int fd;
  194. enum dso_binary_type type;
  195. #ifdef HAVE_LIBELF_SUPPORT
  196. Elf *elf;
  197. GElf_Ehdr ehdr;
  198. Elf_Scn *opdsec;
  199. size_t opdidx;
  200. GElf_Shdr opdshdr;
  201. Elf_Scn *symtab;
  202. GElf_Shdr symshdr;
  203. Elf_Scn *dynsym;
  204. size_t dynsym_idx;
  205. GElf_Shdr dynshdr;
  206. bool adjust_symbols;
  207. bool is_64_bit;
  208. #endif
  209. };
  210. void symsrc__destroy(struct symsrc *ss);
  211. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  212. enum dso_binary_type type);
  213. bool symsrc__has_symtab(struct symsrc *ss);
  214. bool symsrc__possibly_runtime(struct symsrc *ss);
  215. int dso__load(struct dso *dso, struct map *map);
  216. int dso__load_vmlinux(struct dso *dso, struct map *map,
  217. const char *vmlinux, bool vmlinux_allocated);
  218. int dso__load_vmlinux_path(struct dso *dso, struct map *map);
  219. int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  220. bool no_kcore);
  221. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map);
  222. void dso__insert_symbol(struct dso *dso, enum map_type type,
  223. struct symbol *sym);
  224. struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
  225. u64 addr);
  226. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  227. const char *name);
  228. struct symbol *symbol__next_by_name(struct symbol *sym);
  229. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
  230. struct symbol *dso__last_symbol(struct dso *dso, enum map_type type);
  231. struct symbol *dso__next_symbol(struct symbol *sym);
  232. enum dso_type dso__type_fd(int fd);
  233. int filename__read_build_id(const char *filename, void *bf, size_t size);
  234. int sysfs__read_build_id(const char *filename, void *bf, size_t size);
  235. int modules__parse(const char *filename, void *arg,
  236. int (*process_module)(void *arg, const char *name,
  237. u64 start));
  238. int filename__read_debuglink(const char *filename, char *debuglink,
  239. size_t size);
  240. struct perf_env;
  241. int symbol__init(struct perf_env *env);
  242. void symbol__exit(void);
  243. void symbol__elf_init(void);
  244. int symbol__annotation_init(void);
  245. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
  246. size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
  247. const struct addr_location *al,
  248. bool unknown_as_addr,
  249. bool print_offsets, FILE *fp);
  250. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  251. const struct addr_location *al, FILE *fp);
  252. size_t __symbol__fprintf_symname(const struct symbol *sym,
  253. const struct addr_location *al,
  254. bool unknown_as_addr, FILE *fp);
  255. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  256. size_t symbol__fprintf(struct symbol *sym, FILE *fp);
  257. bool symbol_type__is_a(char symbol_type, enum map_type map_type);
  258. bool symbol__restricted_filename(const char *filename,
  259. const char *restricted_filename);
  260. int symbol__config_symfs(const struct option *opt __maybe_unused,
  261. const char *dir, int unset __maybe_unused);
  262. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  263. struct symsrc *runtime_ss, int kmodule);
  264. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
  265. struct map *map);
  266. void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel);
  267. void symbols__insert(struct rb_root *symbols, struct symbol *sym);
  268. void symbols__fixup_duplicate(struct rb_root *symbols);
  269. void symbols__fixup_end(struct rb_root *symbols);
  270. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
  271. typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
  272. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  273. bool *is_64_bit);
  274. #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
  275. struct kcore_extract {
  276. char *kcore_filename;
  277. u64 addr;
  278. u64 offs;
  279. u64 len;
  280. char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
  281. int fd;
  282. };
  283. int kcore_extract__create(struct kcore_extract *kce);
  284. void kcore_extract__delete(struct kcore_extract *kce);
  285. int kcore_copy(const char *from_dir, const char *to_dir);
  286. int compare_proc_modules(const char *from, const char *to);
  287. int setup_list(struct strlist **list, const char *list_str,
  288. const char *list_name);
  289. int setup_intlist(struct intlist **list, const char *list_str,
  290. const char *list_name);
  291. #ifdef HAVE_LIBELF_SUPPORT
  292. bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
  293. void arch__sym_update(struct symbol *s, GElf_Sym *sym);
  294. #endif
  295. #define SYMBOL_A 0
  296. #define SYMBOL_B 1
  297. int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
  298. /* structure containing an SDT note's info */
  299. struct sdt_note {
  300. char *name; /* name of the note*/
  301. char *provider; /* provider name */
  302. bool bit32; /* whether the location is 32 bits? */
  303. union { /* location, base and semaphore addrs */
  304. Elf64_Addr a64[3];
  305. Elf32_Addr a32[3];
  306. } addr;
  307. struct list_head note_list; /* SDT notes' list */
  308. };
  309. int get_sdt_note_list(struct list_head *head, const char *target);
  310. int cleanup_sdt_note_list(struct list_head *sdt_notes);
  311. int sdt_notes__get_count(struct list_head *start);
  312. #define SDT_BASE_SCN ".stapsdt.base"
  313. #define SDT_NOTE_SCN ".note.stapsdt"
  314. #define SDT_NOTE_TYPE 3
  315. #define SDT_NOTE_NAME "stapsdt"
  316. #define NR_ADDR 3
  317. #endif /* __PERF_SYMBOL */