xref: /linux/tools/perf/util/symbol.h (revision 5148fa52a12fa1b97c730b2fe321f2aad7ea041c)
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 
13 #ifdef HAVE_CPLUS_DEMANGLE
14 extern char *cplus_demangle(const char *, int);
15 
16 static inline char *bfd_demangle(void __used *v, const char *c, int i)
17 {
18 	return cplus_demangle(c, i);
19 }
20 #else
21 #ifdef NO_DEMANGLE
22 static inline char *bfd_demangle(void __used *v, const char __used *c,
23 				 int __used i)
24 {
25 	return NULL;
26 }
27 #else
28 #include <bfd.h>
29 #endif
30 #endif
31 
32 int hex2u64(const char *ptr, u64 *val);
33 char *strxfrchar(char *s, char from, char to);
34 
35 /*
36  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
37  * for newer versions we can use mmap to reduce memory usage:
38  */
39 #ifdef LIBELF_NO_MMAP
40 # define PERF_ELF_C_READ_MMAP ELF_C_READ
41 #else
42 # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
43 #endif
44 
45 #ifndef DMGL_PARAMS
46 #define DMGL_PARAMS      (1 << 0)       /* Include function args */
47 #define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
48 #endif
49 
50 #define BUILD_ID_SIZE 20
51 
52 /** struct symbol - symtab entry
53  *
54  * @ignore - resolvable but tools ignore it (e.g. idle routines)
55  */
56 struct symbol {
57 	struct rb_node	rb_node;
58 	u64		start;
59 	u64		end;
60 	u16		namelen;
61 	u8		binding;
62 	bool		ignore;
63 	char		name[0];
64 };
65 
66 void symbol__delete(struct symbol *sym);
67 
68 static inline size_t symbol__size(const struct symbol *sym)
69 {
70 	return sym->end - sym->start + 1;
71 }
72 
73 struct strlist;
74 
75 struct symbol_conf {
76 	unsigned short	priv_size;
77 	unsigned short	nr_events;
78 	bool		try_vmlinux_path,
79 			show_kernel_path,
80 			use_modules,
81 			sort_by_name,
82 			show_nr_samples,
83 			show_total_period,
84 			use_callchain,
85 			exclude_other,
86 			show_cpu_utilization,
87 			initialized,
88 			kptr_restrict,
89 			annotate_asm_raw,
90 			annotate_src;
91 	const char	*vmlinux_name,
92 			*kallsyms_name,
93 			*source_prefix,
94 			*field_sep;
95 	const char	*default_guest_vmlinux_name,
96 			*default_guest_kallsyms,
97 			*default_guest_modules;
98 	const char	*guestmount;
99 	const char	*dso_list_str,
100 			*comm_list_str,
101 			*sym_list_str,
102 			*col_width_list_str;
103        struct strlist	*dso_list,
104 			*comm_list,
105 			*sym_list,
106 			*dso_from_list,
107 			*dso_to_list,
108 			*sym_from_list,
109 			*sym_to_list;
110 	const char	*symfs;
111 };
112 
113 extern struct symbol_conf symbol_conf;
114 
115 static inline void *symbol__priv(struct symbol *sym)
116 {
117 	return ((void *)sym) - symbol_conf.priv_size;
118 }
119 
120 struct ref_reloc_sym {
121 	const char	*name;
122 	u64		addr;
123 	u64		unrelocated_addr;
124 };
125 
126 struct map_symbol {
127 	struct map    *map;
128 	struct symbol *sym;
129 	bool	      unfolded;
130 	bool	      has_children;
131 };
132 
133 struct addr_map_symbol {
134 	struct map    *map;
135 	struct symbol *sym;
136 	u64	      addr;
137 	u64	      al_addr;
138 };
139 
140 struct branch_info {
141 	struct addr_map_symbol from;
142 	struct addr_map_symbol to;
143 	struct branch_flags flags;
144 };
145 
146 struct addr_location {
147 	struct thread *thread;
148 	struct map    *map;
149 	struct symbol *sym;
150 	u64	      addr;
151 	char	      level;
152 	bool	      filtered;
153 	u8	      cpumode;
154 	s32	      cpu;
155 };
156 
157 enum dso_kernel_type {
158 	DSO_TYPE_USER = 0,
159 	DSO_TYPE_KERNEL,
160 	DSO_TYPE_GUEST_KERNEL
161 };
162 
163 struct dso {
164 	struct list_head node;
165 	struct rb_root	 symbols[MAP__NR_TYPES];
166 	struct rb_root	 symbol_names[MAP__NR_TYPES];
167 	enum dso_kernel_type	kernel;
168 	u8		 adjust_symbols:1;
169 	u8		 has_build_id:1;
170 	u8		 hit:1;
171 	u8		 annotate_warned:1;
172 	u8		 sname_alloc:1;
173 	u8		 lname_alloc:1;
174 	unsigned char	 symtab_type;
175 	u8		 sorted_by_name;
176 	u8		 loaded;
177 	u8		 build_id[BUILD_ID_SIZE];
178 	const char	 *short_name;
179 	char	 	 *long_name;
180 	u16		 long_name_len;
181 	u16		 short_name_len;
182 	char		 name[0];
183 };
184 
185 struct dso *dso__new(const char *name);
186 void dso__delete(struct dso *dso);
187 
188 int dso__name_len(const struct dso *dso);
189 
190 bool dso__loaded(const struct dso *dso, enum map_type type);
191 bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
192 
193 static inline void dso__set_loaded(struct dso *dso, enum map_type type)
194 {
195 	dso->loaded |= (1 << type);
196 }
197 
198 void dso__sort_by_name(struct dso *dso, enum map_type type);
199 
200 struct dso *__dsos__findnew(struct list_head *head, const char *name);
201 
202 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
203 int dso__load_vmlinux(struct dso *dso, struct map *map,
204 		      const char *vmlinux, symbol_filter_t filter);
205 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
206 			   symbol_filter_t filter);
207 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
208 		       symbol_filter_t filter);
209 int machine__load_kallsyms(struct machine *machine, const char *filename,
210 			   enum map_type type, symbol_filter_t filter);
211 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
212 			       symbol_filter_t filter);
213 
214 size_t __dsos__fprintf(struct list_head *head, FILE *fp);
215 
216 size_t machine__fprintf_dsos_buildid(struct machine *machine,
217 				     FILE *fp, bool with_hits);
218 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp);
219 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
220 				      FILE *fp, bool with_hits);
221 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
222 size_t dso__fprintf_symbols_by_name(struct dso *dso,
223 				    enum map_type type, FILE *fp);
224 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
225 
226 enum symtab_type {
227 	SYMTAB__KALLSYMS = 0,
228 	SYMTAB__GUEST_KALLSYMS,
229 	SYMTAB__JAVA_JIT,
230 	SYMTAB__BUILD_ID_CACHE,
231 	SYMTAB__FEDORA_DEBUGINFO,
232 	SYMTAB__UBUNTU_DEBUGINFO,
233 	SYMTAB__BUILDID_DEBUGINFO,
234 	SYMTAB__SYSTEM_PATH_DSO,
235 	SYMTAB__GUEST_KMODULE,
236 	SYMTAB__SYSTEM_PATH_KMODULE,
237 	SYMTAB__NOT_FOUND,
238 };
239 
240 char dso__symtab_origin(const struct dso *dso);
241 void dso__set_long_name(struct dso *dso, char *name);
242 void dso__set_build_id(struct dso *dso, void *build_id);
243 void dso__read_running_kernel_build_id(struct dso *dso,
244 				       struct machine *machine);
245 struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
246 				u64 addr);
247 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
248 					const char *name);
249 
250 int filename__read_build_id(const char *filename, void *bf, size_t size);
251 int sysfs__read_build_id(const char *filename, void *bf, size_t size);
252 bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
253 int build_id__sprintf(const u8 *build_id, int len, char *bf);
254 int kallsyms__parse(const char *filename, void *arg,
255 		    int (*process_symbol)(void *arg, const char *name,
256 					  char type, u64 start, u64 end));
257 
258 void machine__destroy_kernel_maps(struct machine *machine);
259 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
260 int machine__create_kernel_maps(struct machine *machine);
261 
262 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid);
263 int machines__create_guest_kernel_maps(struct rb_root *machines);
264 void machines__destroy_guest_kernel_maps(struct rb_root *machines);
265 
266 int symbol__init(void);
267 void symbol__exit(void);
268 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
269 				    const struct addr_location *al, FILE *fp);
270 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
271 bool symbol_type__is_a(char symbol_type, enum map_type map_type);
272 
273 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
274 
275 #endif /* __PERF_SYMBOL */
276