xref: /linux/tools/perf/util/symbol.h (revision 08ec212c0f92cbf30e3ecc7349f18151714041d6)
1 #ifndef __PERF_SYMBOL
2 #define __PERF_SYMBOL 1
3 
4 #include <linux/types.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include "map.h"
8 #include "../perf.h"
9 #include <linux/list.h>
10 #include <linux/rbtree.h>
11 #include <stdio.h>
12 #include <byteswap.h>
13 #include <libgen.h>
14 
15 #ifdef LIBELF_SUPPORT
16 #include <libelf.h>
17 #include <gelf.h>
18 #include <elf.h>
19 #endif
20 
21 #ifdef HAVE_CPLUS_DEMANGLE
22 extern char *cplus_demangle(const char *, int);
23 
24 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
25 {
26 	return cplus_demangle(c, i);
27 }
28 #else
29 #ifdef NO_DEMANGLE
30 static inline char *bfd_demangle(void __maybe_unused *v,
31 				 const char __maybe_unused *c,
32 				 int __maybe_unused i)
33 {
34 	return NULL;
35 }
36 #else
37 #define PACKAGE 'perf'
38 #include <bfd.h>
39 #endif
40 #endif
41 
42 int hex2u64(const char *ptr, u64 *val);
43 char *strxfrchar(char *s, char from, char to);
44 
45 /*
46  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
47  * for newer versions we can use mmap to reduce memory usage:
48  */
49 #ifdef LIBELF_MMAP
50 # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
51 #else
52 # define PERF_ELF_C_READ_MMAP ELF_C_READ
53 #endif
54 
55 #ifndef DMGL_PARAMS
56 #define DMGL_PARAMS      (1 << 0)       /* Include function args */
57 #define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
58 #endif
59 
60 #define BUILD_ID_SIZE 20
61 
62 /** struct symbol - symtab entry
63  *
64  * @ignore - resolvable but tools ignore it (e.g. idle routines)
65  */
66 struct symbol {
67 	struct rb_node	rb_node;
68 	u64		start;
69 	u64		end;
70 	u16		namelen;
71 	u8		binding;
72 	bool		ignore;
73 	char		name[0];
74 };
75 
76 void symbol__delete(struct symbol *sym);
77 
78 static inline size_t symbol__size(const struct symbol *sym)
79 {
80 	return sym->end - sym->start + 1;
81 }
82 
83 struct strlist;
84 
85 struct symbol_conf {
86 	unsigned short	priv_size;
87 	unsigned short	nr_events;
88 	bool		try_vmlinux_path,
89 			show_kernel_path,
90 			use_modules,
91 			sort_by_name,
92 			show_nr_samples,
93 			show_total_period,
94 			use_callchain,
95 			exclude_other,
96 			show_cpu_utilization,
97 			initialized,
98 			kptr_restrict,
99 			annotate_asm_raw,
100 			annotate_src;
101 	const char	*vmlinux_name,
102 			*kallsyms_name,
103 			*source_prefix,
104 			*field_sep;
105 	const char	*default_guest_vmlinux_name,
106 			*default_guest_kallsyms,
107 			*default_guest_modules;
108 	const char	*guestmount;
109 	const char	*dso_list_str,
110 			*comm_list_str,
111 			*sym_list_str,
112 			*col_width_list_str;
113        struct strlist	*dso_list,
114 			*comm_list,
115 			*sym_list,
116 			*dso_from_list,
117 			*dso_to_list,
118 			*sym_from_list,
119 			*sym_to_list;
120 	const char	*symfs;
121 };
122 
123 extern struct symbol_conf symbol_conf;
124 
125 static inline void *symbol__priv(struct symbol *sym)
126 {
127 	return ((void *)sym) - symbol_conf.priv_size;
128 }
129 
130 struct ref_reloc_sym {
131 	const char	*name;
132 	u64		addr;
133 	u64		unrelocated_addr;
134 };
135 
136 struct map_symbol {
137 	struct map    *map;
138 	struct symbol *sym;
139 	bool	      unfolded;
140 	bool	      has_children;
141 };
142 
143 struct addr_map_symbol {
144 	struct map    *map;
145 	struct symbol *sym;
146 	u64	      addr;
147 	u64	      al_addr;
148 };
149 
150 struct branch_info {
151 	struct addr_map_symbol from;
152 	struct addr_map_symbol to;
153 	struct branch_flags flags;
154 };
155 
156 struct addr_location {
157 	struct thread *thread;
158 	struct map    *map;
159 	struct symbol *sym;
160 	u64	      addr;
161 	char	      level;
162 	bool	      filtered;
163 	u8	      cpumode;
164 	s32	      cpu;
165 };
166 
167 enum dso_binary_type {
168 	DSO_BINARY_TYPE__KALLSYMS = 0,
169 	DSO_BINARY_TYPE__GUEST_KALLSYMS,
170 	DSO_BINARY_TYPE__VMLINUX,
171 	DSO_BINARY_TYPE__GUEST_VMLINUX,
172 	DSO_BINARY_TYPE__JAVA_JIT,
173 	DSO_BINARY_TYPE__DEBUGLINK,
174 	DSO_BINARY_TYPE__BUILD_ID_CACHE,
175 	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
176 	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
177 	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
178 	DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
179 	DSO_BINARY_TYPE__GUEST_KMODULE,
180 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
181 	DSO_BINARY_TYPE__NOT_FOUND,
182 };
183 
184 enum dso_kernel_type {
185 	DSO_TYPE_USER = 0,
186 	DSO_TYPE_KERNEL,
187 	DSO_TYPE_GUEST_KERNEL
188 };
189 
190 enum dso_swap_type {
191 	DSO_SWAP__UNSET,
192 	DSO_SWAP__NO,
193 	DSO_SWAP__YES,
194 };
195 
196 #define DSO__DATA_CACHE_SIZE 4096
197 #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
198 
199 struct dso_cache {
200 	struct rb_node	rb_node;
201 	u64 offset;
202 	u64 size;
203 	char data[0];
204 };
205 
206 struct dso {
207 	struct list_head node;
208 	struct rb_root	 symbols[MAP__NR_TYPES];
209 	struct rb_root	 symbol_names[MAP__NR_TYPES];
210 	struct rb_root	 cache;
211 	enum dso_kernel_type	kernel;
212 	enum dso_swap_type	needs_swap;
213 	enum dso_binary_type	symtab_type;
214 	enum dso_binary_type	data_type;
215 	u8		 adjust_symbols:1;
216 	u8		 has_build_id:1;
217 	u8		 hit:1;
218 	u8		 annotate_warned:1;
219 	u8		 sname_alloc:1;
220 	u8		 lname_alloc:1;
221 	u8		 sorted_by_name;
222 	u8		 loaded;
223 	u8		 build_id[BUILD_ID_SIZE];
224 	const char	 *short_name;
225 	char	 	 *long_name;
226 	u16		 long_name_len;
227 	u16		 short_name_len;
228 	char		 name[0];
229 };
230 
231 struct symsrc {
232 	char *name;
233 	int fd;
234 	enum dso_binary_type type;
235 
236 #ifdef LIBELF_SUPPORT
237 	Elf *elf;
238 	GElf_Ehdr ehdr;
239 
240 	Elf_Scn *opdsec;
241 	size_t opdidx;
242 	GElf_Shdr opdshdr;
243 
244 	Elf_Scn *symtab;
245 	GElf_Shdr symshdr;
246 
247 	Elf_Scn *dynsym;
248 	size_t dynsym_idx;
249 	GElf_Shdr dynshdr;
250 
251 	bool adjust_symbols;
252 #endif
253 };
254 
255 void symsrc__destroy(struct symsrc *ss);
256 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
257 		 enum dso_binary_type type);
258 bool symsrc__has_symtab(struct symsrc *ss);
259 bool symsrc__possibly_runtime(struct symsrc *ss);
260 
261 #define DSO__SWAP(dso, type, val)			\
262 ({							\
263 	type ____r = val;				\
264 	BUG_ON(dso->needs_swap == DSO_SWAP__UNSET);	\
265 	if (dso->needs_swap == DSO_SWAP__YES) {		\
266 		switch (sizeof(____r)) {		\
267 		case 2:					\
268 			____r = bswap_16(val);		\
269 			break;				\
270 		case 4:					\
271 			____r = bswap_32(val);		\
272 			break;				\
273 		case 8:					\
274 			____r = bswap_64(val);		\
275 			break;				\
276 		default:				\
277 			BUG_ON(1);			\
278 		}					\
279 	}						\
280 	____r;						\
281 })
282 
283 struct dso *dso__new(const char *name);
284 void dso__delete(struct dso *dso);
285 
286 int dso__name_len(const struct dso *dso);
287 
288 bool dso__loaded(const struct dso *dso, enum map_type type);
289 bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
290 
291 static inline void dso__set_loaded(struct dso *dso, enum map_type type)
292 {
293 	dso->loaded |= (1 << type);
294 }
295 
296 void dso__sort_by_name(struct dso *dso, enum map_type type);
297 
298 void dsos__add(struct list_head *head, struct dso *dso);
299 struct dso *dsos__find(struct list_head *head, const char *name);
300 struct dso *__dsos__findnew(struct list_head *head, const char *name);
301 
302 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
303 int dso__load_vmlinux(struct dso *dso, struct map *map,
304 		      const char *vmlinux, symbol_filter_t filter);
305 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
306 			   symbol_filter_t filter);
307 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
308 		       symbol_filter_t filter);
309 int machine__load_kallsyms(struct machine *machine, const char *filename,
310 			   enum map_type type, symbol_filter_t filter);
311 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
312 			       symbol_filter_t filter);
313 
314 size_t __dsos__fprintf(struct list_head *head, FILE *fp);
315 
316 size_t machine__fprintf_dsos_buildid(struct machine *machine,
317 				     FILE *fp, bool with_hits);
318 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp);
319 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
320 				      FILE *fp, bool with_hits);
321 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
322 size_t dso__fprintf_symbols_by_name(struct dso *dso,
323 				    enum map_type type, FILE *fp);
324 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
325 
326 char dso__symtab_origin(const struct dso *dso);
327 void dso__set_long_name(struct dso *dso, char *name);
328 void dso__set_build_id(struct dso *dso, void *build_id);
329 bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
330 void dso__read_running_kernel_build_id(struct dso *dso,
331 				       struct machine *machine);
332 struct map *dso__new_map(const char *name);
333 struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
334 				u64 addr);
335 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
336 					const char *name);
337 
338 int filename__read_build_id(const char *filename, void *bf, size_t size);
339 int sysfs__read_build_id(const char *filename, void *bf, size_t size);
340 bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
341 int build_id__sprintf(const u8 *build_id, int len, char *bf);
342 int kallsyms__parse(const char *filename, void *arg,
343 		    int (*process_symbol)(void *arg, const char *name,
344 					  char type, u64 start));
345 int filename__read_debuglink(const char *filename, char *debuglink,
346 			     size_t size);
347 
348 void machine__destroy_kernel_maps(struct machine *machine);
349 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
350 int machine__create_kernel_maps(struct machine *machine);
351 
352 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid);
353 int machines__create_guest_kernel_maps(struct rb_root *machines);
354 void machines__destroy_guest_kernel_maps(struct rb_root *machines);
355 
356 int symbol__init(void);
357 void symbol__exit(void);
358 void symbol__elf_init(void);
359 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
360 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
361 				    const struct addr_location *al, FILE *fp);
362 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
363 bool symbol_type__is_a(char symbol_type, enum map_type map_type);
364 
365 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
366 
367 int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
368 			  char *root_dir, char *file, size_t size);
369 
370 int dso__data_fd(struct dso *dso, struct machine *machine);
371 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
372 			      u64 offset, u8 *data, ssize_t size);
373 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
374 			    struct machine *machine, u64 addr,
375 			    u8 *data, ssize_t size);
376 int dso__test_data(void);
377 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
378 		  struct symsrc *runtime_ss, symbol_filter_t filter,
379 		  int kmodule);
380 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
381 				struct map *map, symbol_filter_t filter);
382 
383 void symbols__insert(struct rb_root *symbols, struct symbol *sym);
384 void symbols__fixup_duplicate(struct rb_root *symbols);
385 void symbols__fixup_end(struct rb_root *symbols);
386 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
387 
388 #endif /* __PERF_SYMBOL */
389