Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[cascardo/linux.git] / tools / perf / util / symbol.h
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 #include "event.h"
16 #include "util.h"
17
18 #ifdef HAVE_LIBELF_SUPPORT
19 #include <libelf.h>
20 #include <gelf.h>
21 #endif
22 #include <elf.h>
23
24 #include "dso.h"
25
26 /*
27  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
28  * for newer versions we can use mmap to reduce memory usage:
29  */
30 #ifdef HAVE_LIBELF_MMAP_SUPPORT
31 # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
32 #else
33 # define PERF_ELF_C_READ_MMAP ELF_C_READ
34 #endif
35
36 #ifdef HAVE_LIBELF_SUPPORT
37 Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
38                              GElf_Shdr *shp, const char *name, size_t *idx);
39 #endif
40
41 #ifndef DMGL_PARAMS
42 #define DMGL_NO_OPTS     0              /* For readability... */
43 #define DMGL_PARAMS      (1 << 0)       /* Include function args */
44 #define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
45 #endif
46
47 /** struct symbol - symtab entry
48  *
49  * @ignore - resolvable but tools ignore it (e.g. idle routines)
50  */
51 struct symbol {
52         struct rb_node  rb_node;
53         u64             start;
54         u64             end;
55         u16             namelen;
56         u8              binding;
57         bool            ignore;
58         u8              arch_sym;
59         char            name[0];
60 };
61
62 void symbol__delete(struct symbol *sym);
63 void symbols__delete(struct rb_root *symbols);
64
65 /* symbols__for_each_entry - iterate over symbols (rb_root)
66  *
67  * @symbols: the rb_root of symbols
68  * @pos: the 'struct symbol *' to use as a loop cursor
69  * @nd: the 'struct rb_node *' to use as a temporary storage
70  */
71 #define symbols__for_each_entry(symbols, pos, nd)                       \
72         for (nd = rb_first(symbols);                                    \
73              nd && (pos = rb_entry(nd, struct symbol, rb_node));        \
74              nd = rb_next(nd))
75
76 static inline size_t symbol__size(const struct symbol *sym)
77 {
78         return sym->end - sym->start;
79 }
80
81 struct strlist;
82 struct intlist;
83
84 struct symbol_conf {
85         unsigned short  priv_size;
86         unsigned short  nr_events;
87         bool            try_vmlinux_path,
88                         force,
89                         ignore_vmlinux,
90                         ignore_vmlinux_buildid,
91                         show_kernel_path,
92                         use_modules,
93                         allow_aliases,
94                         sort_by_name,
95                         show_nr_samples,
96                         show_total_period,
97                         use_callchain,
98                         cumulate_callchain,
99                         exclude_other,
100                         show_cpu_utilization,
101                         initialized,
102                         kptr_restrict,
103                         annotate_asm_raw,
104                         annotate_src,
105                         event_group,
106                         demangle,
107                         demangle_kernel,
108                         filter_relative,
109                         show_hist_headers,
110                         branch_callstack,
111                         has_filter,
112                         show_ref_callgraph,
113                         hide_unresolved,
114                         raw_trace,
115                         report_hierarchy;
116         const char      *vmlinux_name,
117                         *kallsyms_name,
118                         *source_prefix,
119                         *field_sep;
120         const char      *default_guest_vmlinux_name,
121                         *default_guest_kallsyms,
122                         *default_guest_modules;
123         const char      *guestmount;
124         const char      *dso_list_str,
125                         *comm_list_str,
126                         *pid_list_str,
127                         *tid_list_str,
128                         *sym_list_str,
129                         *col_width_list_str;
130        struct strlist   *dso_list,
131                         *comm_list,
132                         *sym_list,
133                         *dso_from_list,
134                         *dso_to_list,
135                         *sym_from_list,
136                         *sym_to_list;
137         struct intlist  *pid_list,
138                         *tid_list;
139         const char      *symfs;
140 };
141
142 extern struct symbol_conf symbol_conf;
143
144 struct symbol_name_rb_node {
145         struct rb_node  rb_node;
146         struct symbol   sym;
147 };
148
149 static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
150 {
151         return path__join(bf, size, symbol_conf.symfs, path);
152 }
153
154 #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
155
156 extern int vmlinux_path__nr_entries;
157 extern char **vmlinux_path;
158
159 static inline void *symbol__priv(struct symbol *sym)
160 {
161         return ((void *)sym) - symbol_conf.priv_size;
162 }
163
164 struct ref_reloc_sym {
165         const char      *name;
166         u64             addr;
167         u64             unrelocated_addr;
168 };
169
170 struct map_symbol {
171         struct map    *map;
172         struct symbol *sym;
173 };
174
175 struct addr_map_symbol {
176         struct map    *map;
177         struct symbol *sym;
178         u64           addr;
179         u64           al_addr;
180 };
181
182 struct branch_info {
183         struct addr_map_symbol from;
184         struct addr_map_symbol to;
185         struct branch_flags flags;
186 };
187
188 struct mem_info {
189         struct addr_map_symbol iaddr;
190         struct addr_map_symbol daddr;
191         union perf_mem_data_src data_src;
192 };
193
194 struct addr_location {
195         struct machine *machine;
196         struct thread *thread;
197         struct map    *map;
198         struct symbol *sym;
199         u64           addr;
200         char          level;
201         u8            filtered;
202         u8            cpumode;
203         s32           cpu;
204         s32           socket;
205 };
206
207 struct symsrc {
208         char *name;
209         int fd;
210         enum dso_binary_type type;
211
212 #ifdef HAVE_LIBELF_SUPPORT
213         Elf *elf;
214         GElf_Ehdr ehdr;
215
216         Elf_Scn *opdsec;
217         size_t opdidx;
218         GElf_Shdr opdshdr;
219
220         Elf_Scn *symtab;
221         GElf_Shdr symshdr;
222
223         Elf_Scn *dynsym;
224         size_t dynsym_idx;
225         GElf_Shdr dynshdr;
226
227         bool adjust_symbols;
228         bool is_64_bit;
229 #endif
230 };
231
232 void symsrc__destroy(struct symsrc *ss);
233 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
234                  enum dso_binary_type type);
235 bool symsrc__has_symtab(struct symsrc *ss);
236 bool symsrc__possibly_runtime(struct symsrc *ss);
237
238 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
239 int dso__load_vmlinux(struct dso *dso, struct map *map,
240                       const char *vmlinux, bool vmlinux_allocated,
241                       symbol_filter_t filter);
242 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
243                            symbol_filter_t filter);
244 int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
245                          bool no_kcore, symbol_filter_t filter);
246 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
247                        symbol_filter_t filter);
248
249 void dso__insert_symbol(struct dso *dso, enum map_type type,
250                         struct symbol *sym);
251
252 struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
253                                 u64 addr);
254 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
255                                         const char *name);
256 struct symbol *symbol__next_by_name(struct symbol *sym);
257
258 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
259 struct symbol *dso__next_symbol(struct symbol *sym);
260
261 enum dso_type dso__type_fd(int fd);
262
263 int filename__read_build_id(const char *filename, void *bf, size_t size);
264 int sysfs__read_build_id(const char *filename, void *bf, size_t size);
265 int modules__parse(const char *filename, void *arg,
266                    int (*process_module)(void *arg, const char *name,
267                                          u64 start));
268 int filename__read_debuglink(const char *filename, char *debuglink,
269                              size_t size);
270
271 struct perf_env;
272 int symbol__init(struct perf_env *env);
273 void symbol__exit(void);
274 void symbol__elf_init(void);
275 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
276 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
277                                       const struct addr_location *al,
278                                       bool unknown_as_addr, FILE *fp);
279 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
280                                     const struct addr_location *al, FILE *fp);
281 size_t __symbol__fprintf_symname(const struct symbol *sym,
282                                  const struct addr_location *al,
283                                  bool unknown_as_addr, FILE *fp);
284 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
285 size_t symbol__fprintf(struct symbol *sym, FILE *fp);
286 bool symbol_type__is_a(char symbol_type, enum map_type map_type);
287 bool symbol__restricted_filename(const char *filename,
288                                  const char *restricted_filename);
289 bool symbol__is_idle(struct symbol *sym);
290
291 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
292                   struct symsrc *runtime_ss, symbol_filter_t filter,
293                   int kmodule);
294 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
295                                 struct map *map, symbol_filter_t filter);
296
297 void symbols__insert(struct rb_root *symbols, struct symbol *sym);
298 void symbols__fixup_duplicate(struct rb_root *symbols);
299 void symbols__fixup_end(struct rb_root *symbols);
300 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
301
302 typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
303 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
304                     bool *is_64_bit);
305
306 #define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
307
308 struct kcore_extract {
309         char *kcore_filename;
310         u64 addr;
311         u64 offs;
312         u64 len;
313         char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
314         int fd;
315 };
316
317 int kcore_extract__create(struct kcore_extract *kce);
318 void kcore_extract__delete(struct kcore_extract *kce);
319
320 int kcore_copy(const char *from_dir, const char *to_dir);
321 int compare_proc_modules(const char *from, const char *to);
322
323 int setup_list(struct strlist **list, const char *list_str,
324                const char *list_name);
325 int setup_intlist(struct intlist **list, const char *list_str,
326                   const char *list_name);
327
328 #ifdef HAVE_LIBELF_SUPPORT
329 bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
330 void arch__sym_update(struct symbol *s, GElf_Sym *sym);
331 #endif
332
333 #define SYMBOL_A 0
334 #define SYMBOL_B 1
335
336 int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
337
338 #endif /* __PERF_SYMBOL */