symbol.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_SYMBOL
  3. #define __PERF_SYMBOL 1
  4. #include <linux/types.h>
  5. #include <linux/refcount.h>
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <linux/list.h>
  9. #include <linux/rbtree.h>
  10. #include <stdio.h>
  11. #include "path.h"
  12. #include "symbol_conf.h"
  13. #include "spark.h"
  14. #ifdef HAVE_LIBELF_SUPPORT
  15. #include <libelf.h>
  16. #include <gelf.h>
  17. #endif
  18. #include <elf.h>
  19. struct dso;
  20. struct map;
  21. struct maps;
  22. struct option;
  23. struct build_id;
  24. /*
  25. * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
  26. * for newer versions we can use mmap to reduce memory usage:
  27. */
  28. #ifdef ELF_C_READ_MMAP
  29. # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  30. #else
  31. # define PERF_ELF_C_READ_MMAP ELF_C_READ
  32. #endif
  33. #ifdef HAVE_LIBELF_SUPPORT
  34. Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  35. GElf_Shdr *shp, const char *name, size_t *idx);
  36. #endif
  37. /** struct symbol - symtab entry
  38. *
  39. * @ignore - resolvable but tools ignore it (e.g. idle routines)
  40. */
  41. struct symbol {
  42. struct rb_node rb_node;
  43. u64 start;
  44. u64 end;
  45. u16 namelen;
  46. u8 type:4;
  47. u8 binding:4;
  48. u8 idle:1;
  49. u8 ignore:1;
  50. u8 inlined:1;
  51. u8 arch_sym;
  52. bool annotate2;
  53. char name[];
  54. };
  55. void symbol__delete(struct symbol *sym);
  56. void symbols__delete(struct rb_root_cached *symbols);
  57. /* symbols__for_each_entry - iterate over symbols (rb_root)
  58. *
  59. * @symbols: the rb_root of symbols
  60. * @pos: the 'struct symbol *' to use as a loop cursor
  61. * @nd: the 'struct rb_node *' to use as a temporary storage
  62. */
  63. #define symbols__for_each_entry(symbols, pos, nd) \
  64. for (nd = rb_first_cached(symbols); \
  65. nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
  66. nd = rb_next(nd))
  67. static inline size_t symbol__size(const struct symbol *sym)
  68. {
  69. return sym->end - sym->start;
  70. }
  71. struct strlist;
  72. struct intlist;
  73. struct symbol_name_rb_node {
  74. struct rb_node rb_node;
  75. struct symbol sym;
  76. };
  77. static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
  78. {
  79. return path__join(bf, size, symbol_conf.symfs, path);
  80. }
  81. #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
  82. extern int vmlinux_path__nr_entries;
  83. extern char **vmlinux_path;
  84. static inline void *symbol__priv(struct symbol *sym)
  85. {
  86. return ((void *)sym) - symbol_conf.priv_size;
  87. }
  88. struct ref_reloc_sym {
  89. const char *name;
  90. u64 addr;
  91. u64 unrelocated_addr;
  92. };
  93. struct addr_location {
  94. struct thread *thread;
  95. struct maps *maps;
  96. struct map *map;
  97. struct symbol *sym;
  98. const char *srcline;
  99. u64 addr;
  100. char level;
  101. u8 filtered;
  102. u8 cpumode;
  103. s32 cpu;
  104. s32 socket;
  105. };
  106. int dso__load(struct dso *dso, struct map *map);
  107. int dso__load_vmlinux(struct dso *dso, struct map *map,
  108. const char *vmlinux, bool vmlinux_allocated);
  109. int dso__load_vmlinux_path(struct dso *dso, struct map *map);
  110. int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
  111. bool no_kcore);
  112. int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map);
  113. void dso__insert_symbol(struct dso *dso,
  114. struct symbol *sym);
  115. void dso__delete_symbol(struct dso *dso,
  116. struct symbol *sym);
  117. struct symbol *dso__find_symbol(struct dso *dso, u64 addr);
  118. struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name);
  119. struct symbol *symbol__next_by_name(struct symbol *sym);
  120. struct symbol *dso__first_symbol(struct dso *dso);
  121. struct symbol *dso__last_symbol(struct dso *dso);
  122. struct symbol *dso__next_symbol(struct symbol *sym);
  123. enum dso_type dso__type_fd(int fd);
  124. int filename__read_build_id(const char *filename, struct build_id *id);
  125. int sysfs__read_build_id(const char *filename, struct build_id *bid);
  126. int modules__parse(const char *filename, void *arg,
  127. int (*process_module)(void *arg, const char *name,
  128. u64 start, u64 size));
  129. int filename__read_debuglink(const char *filename, char *debuglink,
  130. size_t size);
  131. struct perf_env;
  132. int symbol__init(struct perf_env *env);
  133. void symbol__exit(void);
  134. void symbol__elf_init(void);
  135. int symbol__annotation_init(void);
  136. struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name);
  137. size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
  138. const struct addr_location *al,
  139. bool unknown_as_addr,
  140. bool print_offsets, FILE *fp);
  141. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  142. const struct addr_location *al, FILE *fp);
  143. size_t __symbol__fprintf_symname(const struct symbol *sym,
  144. const struct addr_location *al,
  145. bool unknown_as_addr, FILE *fp);
  146. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
  147. size_t symbol__fprintf(struct symbol *sym, FILE *fp);
  148. bool symbol__restricted_filename(const char *filename,
  149. const char *restricted_filename);
  150. int symbol__config_symfs(const struct option *opt __maybe_unused,
  151. const char *dir, int unset __maybe_unused);
  152. struct symsrc;
  153. #ifdef HAVE_LIBBFD_SUPPORT
  154. int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
  155. #endif
  156. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  157. struct symsrc *runtime_ss, int kmodule);
  158. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
  159. char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
  160. void __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym,
  161. bool kernel);
  162. void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym);
  163. void symbols__fixup_duplicate(struct rb_root_cached *symbols);
  164. void symbols__fixup_end(struct rb_root_cached *symbols);
  165. void maps__fixup_end(struct maps *maps);
  166. typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
  167. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  168. bool *is_64_bit);
  169. #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
  170. struct kcore_extract {
  171. char *kcore_filename;
  172. u64 addr;
  173. u64 offs;
  174. u64 len;
  175. char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
  176. int fd;
  177. };
  178. int kcore_extract__create(struct kcore_extract *kce);
  179. void kcore_extract__delete(struct kcore_extract *kce);
  180. int kcore_copy(const char *from_dir, const char *to_dir);
  181. int compare_proc_modules(const char *from, const char *to);
  182. int setup_list(struct strlist **list, const char *list_str,
  183. const char *list_name);
  184. int setup_intlist(struct intlist **list, const char *list_str,
  185. const char *list_name);
  186. #ifdef HAVE_LIBELF_SUPPORT
  187. bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
  188. void arch__sym_update(struct symbol *s, GElf_Sym *sym);
  189. #endif
  190. const char *arch__normalize_symbol_name(const char *name);
  191. #define SYMBOL_A 0
  192. #define SYMBOL_B 1
  193. void arch__symbols__fixup_end(struct symbol *p, struct symbol *c);
  194. int arch__compare_symbol_names(const char *namea, const char *nameb);
  195. int arch__compare_symbol_names_n(const char *namea, const char *nameb,
  196. unsigned int n);
  197. int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
  198. enum symbol_tag_include {
  199. SYMBOL_TAG_INCLUDE__NONE = 0,
  200. SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
  201. };
  202. int symbol__match_symbol_name(const char *namea, const char *nameb,
  203. enum symbol_tag_include includes);
  204. /* structure containing an SDT note's info */
  205. struct sdt_note {
  206. char *name; /* name of the note*/
  207. char *provider; /* provider name */
  208. char *args;
  209. bool bit32; /* whether the location is 32 bits? */
  210. union { /* location, base and semaphore addrs */
  211. Elf64_Addr a64[3];
  212. Elf32_Addr a32[3];
  213. } addr;
  214. struct list_head note_list; /* SDT notes' list */
  215. };
  216. int get_sdt_note_list(struct list_head *head, const char *target);
  217. int cleanup_sdt_note_list(struct list_head *sdt_notes);
  218. int sdt_notes__get_count(struct list_head *start);
  219. #define SDT_PROBES_SCN ".probes"
  220. #define SDT_BASE_SCN ".stapsdt.base"
  221. #define SDT_NOTE_SCN ".note.stapsdt"
  222. #define SDT_NOTE_TYPE 3
  223. #define SDT_NOTE_NAME "stapsdt"
  224. #define NR_ADDR 3
  225. enum {
  226. SDT_NOTE_IDX_LOC = 0,
  227. SDT_NOTE_IDX_BASE,
  228. SDT_NOTE_IDX_REFCTR,
  229. };
  230. struct mem_info *mem_info__new(void);
  231. struct mem_info *mem_info__get(struct mem_info *mi);
  232. void mem_info__put(struct mem_info *mi);
  233. static inline void __mem_info__zput(struct mem_info **mi)
  234. {
  235. mem_info__put(*mi);
  236. *mi = NULL;
  237. }
  238. #define mem_info__zput(mi) __mem_info__zput(&mi)
  239. #endif /* __PERF_SYMBOL */