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