xref: /openbmc/linux/tools/perf/util/symbol.h (revision 12eb4683)
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 #include "build-id.h"
15 
16 #ifdef HAVE_LIBELF_SUPPORT
17 #include <libelf.h>
18 #include <gelf.h>
19 #endif
20 #include <elf.h>
21 
22 #include "dso.h"
23 
24 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
25 extern char *cplus_demangle(const char *, int);
26 
27 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
28 {
29 	return cplus_demangle(c, i);
30 }
31 #else
32 #ifdef NO_DEMANGLE
33 static inline char *bfd_demangle(void __maybe_unused *v,
34 				 const char __maybe_unused *c,
35 				 int __maybe_unused i)
36 {
37 	return NULL;
38 }
39 #else
40 #define PACKAGE 'perf'
41 #include <bfd.h>
42 #endif
43 #endif
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 HAVE_LIBELF_MMAP_SUPPORT
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 /** struct symbol - symtab entry
61  *
62  * @ignore - resolvable but tools ignore it (e.g. idle routines)
63  */
64 struct symbol {
65 	struct rb_node	rb_node;
66 	u64		start;
67 	u64		end;
68 	u16		namelen;
69 	u8		binding;
70 	bool		ignore;
71 	char		name[0];
72 };
73 
74 void symbol__delete(struct symbol *sym);
75 void symbols__delete(struct rb_root *symbols);
76 
77 static inline size_t symbol__size(const struct symbol *sym)
78 {
79 	return sym->end - sym->start + 1;
80 }
81 
82 struct strlist;
83 
84 struct symbol_conf {
85 	unsigned short	priv_size;
86 	unsigned short	nr_events;
87 	bool		try_vmlinux_path,
88 			ignore_vmlinux,
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 			event_group,
102 			demangle;
103 	const char	*vmlinux_name,
104 			*kallsyms_name,
105 			*source_prefix,
106 			*field_sep;
107 	const char	*default_guest_vmlinux_name,
108 			*default_guest_kallsyms,
109 			*default_guest_modules;
110 	const char	*guestmount;
111 	const char	*dso_list_str,
112 			*comm_list_str,
113 			*sym_list_str,
114 			*col_width_list_str;
115        struct strlist	*dso_list,
116 			*comm_list,
117 			*sym_list,
118 			*dso_from_list,
119 			*dso_to_list,
120 			*sym_from_list,
121 			*sym_to_list;
122 	const char	*symfs;
123 };
124 
125 extern struct symbol_conf symbol_conf;
126 extern int vmlinux_path__nr_entries;
127 extern char **vmlinux_path;
128 
129 static inline void *symbol__priv(struct symbol *sym)
130 {
131 	return ((void *)sym) - symbol_conf.priv_size;
132 }
133 
134 struct ref_reloc_sym {
135 	const char	*name;
136 	u64		addr;
137 	u64		unrelocated_addr;
138 };
139 
140 struct map_symbol {
141 	struct map    *map;
142 	struct symbol *sym;
143 	bool	      unfolded;
144 	bool	      has_children;
145 };
146 
147 struct addr_map_symbol {
148 	struct map    *map;
149 	struct symbol *sym;
150 	u64	      addr;
151 	u64	      al_addr;
152 };
153 
154 struct branch_info {
155 	struct addr_map_symbol from;
156 	struct addr_map_symbol to;
157 	struct branch_flags flags;
158 };
159 
160 struct mem_info {
161 	struct addr_map_symbol iaddr;
162 	struct addr_map_symbol daddr;
163 	union perf_mem_data_src data_src;
164 };
165 
166 struct addr_location {
167 	struct thread *thread;
168 	struct map    *map;
169 	struct symbol *sym;
170 	u64	      addr;
171 	char	      level;
172 	bool	      filtered;
173 	u8	      cpumode;
174 	s32	      cpu;
175 };
176 
177 struct symsrc {
178 	char *name;
179 	int fd;
180 	enum dso_binary_type type;
181 
182 #ifdef HAVE_LIBELF_SUPPORT
183 	Elf *elf;
184 	GElf_Ehdr ehdr;
185 
186 	Elf_Scn *opdsec;
187 	size_t opdidx;
188 	GElf_Shdr opdshdr;
189 
190 	Elf_Scn *symtab;
191 	GElf_Shdr symshdr;
192 
193 	Elf_Scn *dynsym;
194 	size_t dynsym_idx;
195 	GElf_Shdr dynshdr;
196 
197 	bool adjust_symbols;
198 #endif
199 };
200 
201 void symsrc__destroy(struct symsrc *ss);
202 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
203 		 enum dso_binary_type type);
204 bool symsrc__has_symtab(struct symsrc *ss);
205 bool symsrc__possibly_runtime(struct symsrc *ss);
206 
207 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
208 int dso__load_vmlinux(struct dso *dso, struct map *map,
209 		      const char *vmlinux, symbol_filter_t filter);
210 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
211 			   symbol_filter_t filter);
212 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
213 		       symbol_filter_t filter);
214 
215 struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
216 				u64 addr);
217 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
218 					const char *name);
219 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
220 
221 int filename__read_build_id(const char *filename, void *bf, size_t size);
222 int sysfs__read_build_id(const char *filename, void *bf, size_t size);
223 int kallsyms__parse(const char *filename, void *arg,
224 		    int (*process_symbol)(void *arg, const char *name,
225 					  char type, u64 start));
226 int modules__parse(const char *filename, void *arg,
227 		   int (*process_module)(void *arg, const char *name,
228 					 u64 start));
229 int filename__read_debuglink(const char *filename, char *debuglink,
230 			     size_t size);
231 
232 int symbol__init(void);
233 void symbol__exit(void);
234 void symbol__elf_init(void);
235 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
236 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
237 				    const struct addr_location *al, FILE *fp);
238 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
239 size_t symbol__fprintf(struct symbol *sym, FILE *fp);
240 bool symbol_type__is_a(char symbol_type, enum map_type map_type);
241 bool symbol__restricted_filename(const char *filename,
242 				 const char *restricted_filename);
243 
244 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
245 		  struct symsrc *runtime_ss, symbol_filter_t filter,
246 		  int kmodule);
247 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
248 				struct map *map, symbol_filter_t filter);
249 
250 void symbols__insert(struct rb_root *symbols, struct symbol *sym);
251 void symbols__fixup_duplicate(struct rb_root *symbols);
252 void symbols__fixup_end(struct rb_root *symbols);
253 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
254 
255 typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
256 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
257 		    bool *is_64_bit);
258 
259 #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
260 
261 struct kcore_extract {
262 	char *kcore_filename;
263 	u64 addr;
264 	u64 offs;
265 	u64 len;
266 	char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
267 	int fd;
268 };
269 
270 int kcore_extract__create(struct kcore_extract *kce);
271 void kcore_extract__delete(struct kcore_extract *kce);
272 
273 int kcore_copy(const char *from_dir, const char *to_dir);
274 int compare_proc_modules(const char *from, const char *to);
275 
276 #endif /* __PERF_SYMBOL */
277