blob: 07211c2f8456b409982532632726cb524c150ac1 [file] [log] [blame]
John Kacur8b40f522009-09-24 18:02:18 +02001#ifndef __PERF_SYMBOL
2#define __PERF_SYMBOL 1
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03003
4#include <linux/types.h>
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02005#include <stdbool.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03006#include <stdint.h>
7#include "map.h"
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01008#include "../perf.h"
Arnaldo Carvalho de Melo5da50252009-07-01 14:46:08 -03009#include <linux/list.h>
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030010#include <linux/rbtree.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030011#include <stdio.h>
Jiri Olsa8db48412012-05-30 14:23:42 +020012#include <byteswap.h>
Irina Tirdeab771a832012-09-08 03:43:17 +030013#include <libgen.h>
Jiri Olsa4383db82012-10-27 23:18:29 +020014#include "build-id.h"
Jiri Olsa0776eb52014-05-05 12:41:45 +020015#include "event.h"
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030016#include "util.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030017
Ingo Molnar89fe8082013-09-30 12:07:11 +020018#ifdef HAVE_LIBELF_SUPPORT
Cody P Schaferb68e2f92012-08-10 15:22:57 -070019#include <libelf.h>
20#include <gelf.h>
Cody P Schaferb68e2f92012-08-10 15:22:57 -070021#endif
Namhyung Kimeec185a2012-12-28 16:16:49 +090022#include <elf.h>
Cody P Schaferb68e2f92012-08-10 15:22:57 -070023
Jiri Olsacdd059d2012-10-27 23:18:32 +020024#include "dso.h"
25
Marti Raudsepp84087122009-10-24 19:10:36 +030026/*
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 */
Ingo Molnar89fe8082013-09-30 12:07:11 +020030#ifdef HAVE_LIBELF_MMAP_SUPPORT
Marti Raudsepp84087122009-10-24 19:10:36 +030031# define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
Namhyung Kim29a0fc92012-09-28 18:31:59 +090032#else
33# define PERF_ELF_C_READ_MMAP ELF_C_READ
Marti Raudsepp84087122009-10-24 19:10:36 +030034#endif
35
Masami Hiramatsu99ca4232014-01-16 09:39:49 +000036#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo3938bad2016-03-23 15:06:35 -030037Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
38 GElf_Shdr *shp, const char *name, size_t *idx);
Masami Hiramatsu99ca4232014-01-16 09:39:49 +000039#endif
40
Arnaldo Carvalho de Melo247648e2009-08-11 16:22:11 -030041#ifndef DMGL_PARAMS
Namhyung Kime71e7942014-07-31 14:47:42 +090042#define DMGL_NO_OPTS 0 /* For readability... */
Arnaldo Carvalho de Melo247648e2009-08-11 16:22:11 -030043#define DMGL_PARAMS (1 << 0) /* Include function args */
44#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
45#endif
46
Arnaldo Carvalho de Melo171b3be2011-03-11 13:36:01 -030047/** struct symbol - symtab entry
48 *
49 * @ignore - resolvable but tools ignore it (e.g. idle routines)
50 */
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030051struct symbol {
52 struct rb_node rb_node;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100053 u64 start;
54 u64 end;
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -030055 u16 namelen;
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -030056 u8 binding;
Arnaldo Carvalho de Melo171b3be2011-03-11 13:36:01 -030057 bool ignore;
Naveen N. Rao0b3c2262016-04-12 14:40:50 +053058 u8 arch_sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030059 char name[0];
60};
61
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030062void symbol__delete(struct symbol *sym);
Jiri Olsacdd059d2012-10-27 23:18:32 +020063void symbols__delete(struct rb_root *symbols);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030064
Masami Hiramatsueb948e52014-02-06 05:32:25 +000065/* 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
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -030076static inline size_t symbol__size(const struct symbol *sym)
77{
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -030078 return sym->end - sym->start;
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -030079}
80
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -020081struct strlist;
David Aherne03eaa42015-03-24 09:52:41 -060082struct intlist;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -020083
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020084struct symbol_conf {
85 unsigned short priv_size;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -020086 unsigned short nr_events;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020087 bool try_vmlinux_path,
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -030088 force,
Willy Tarreaufc2be692013-09-14 10:32:59 +020089 ignore_vmlinux,
Namhyung Kim00dc8652014-11-04 10:14:32 +090090 ignore_vmlinux_buildid,
Akihiro Nagai0bc8d202012-01-30 13:43:20 +090091 show_kernel_path,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020092 use_modules,
Namhyung Kim680d9262015-03-06 16:31:27 +090093 allow_aliases,
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020094 sort_by_name,
95 show_nr_samples,
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -030096 show_total_period,
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020097 use_callchain,
Namhyung Kimf8be1c82012-09-11 13:15:07 +090098 cumulate_callchain,
Arnaldo Carvalho de Melof7d87442009-12-27 21:37:04 -020099 exclude_other,
Jovi Zhang85e00b52010-09-09 13:30:59 -0300100 show_cpu_utilization,
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300101 initialized,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200102 kptr_restrict,
103 annotate_asm_raw,
Namhyung Kim6e1f6012013-01-22 18:09:32 +0900104 annotate_src,
Namhyung Kim328ccda2013-03-25 18:18:18 +0900105 event_group,
Namhyung Kimf2148332014-01-14 11:52:48 +0900106 demangle,
Avi Kivity763122a2014-09-13 07:15:05 +0300107 demangle_kernel,
Jiri Olsac8302362014-06-27 18:26:58 +0200108 filter_relative,
Andi Kleen8b7bad52014-11-12 18:05:20 -0800109 show_hist_headers,
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -0300110 branch_callstack,
Kan Liang9e207dd2015-08-11 06:30:49 -0400111 has_filter,
Namhyung Kimb49a8fe2015-11-26 16:08:20 +0900112 show_ref_callgraph,
Namhyung Kim053a3982015-12-23 02:07:05 +0900113 hide_unresolved,
Namhyung Kimaef810e2016-02-25 00:13:34 +0900114 raw_trace,
115 report_hierarchy;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200116 const char *vmlinux_name,
David Ahernb226a5a72010-12-07 19:39:46 -0700117 *kallsyms_name,
Chase Douglas9ed7e1b2010-06-14 15:26:30 -0400118 *source_prefix,
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200119 *field_sep;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800120 const char *default_guest_vmlinux_name,
121 *default_guest_kallsyms,
122 *default_guest_modules;
123 const char *guestmount;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300124 const char *dso_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200125 *comm_list_str,
David Aherne03eaa42015-03-24 09:52:41 -0600126 *pid_list_str,
127 *tid_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200128 *sym_list_str,
129 *col_width_list_str;
130 struct strlist *dso_list,
131 *comm_list,
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100132 *sym_list,
133 *dso_from_list,
134 *dso_to_list,
135 *sym_from_list,
136 *sym_to_list;
David Aherne03eaa42015-03-24 09:52:41 -0600137 struct intlist *pid_list,
138 *tid_list;
David Ahernec5761e2010-12-09 13:27:07 -0700139 const char *symfs;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200140};
141
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200142extern struct symbol_conf symbol_conf;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300143
Arnaldo Carvalho de Melobfbba182016-04-14 15:54:36 -0300144struct symbol_name_rb_node {
145 struct rb_node rb_node;
146 struct symbol sym;
147};
148
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300149static 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
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300156extern int vmlinux_path__nr_entries;
157extern char **vmlinux_path;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200158
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300159static inline void *symbol__priv(struct symbol *sym)
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200160{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300161 return ((void *)sym) - symbol_conf.priv_size;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200162}
163
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200164struct ref_reloc_sym {
165 const char *name;
166 u64 addr;
167 u64 unrelocated_addr;
168};
169
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300170struct map_symbol {
171 struct map *map;
172 struct symbol *sym;
173};
174
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100175struct addr_map_symbol {
176 struct map *map;
177 struct symbol *sym;
178 u64 addr;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100179 u64 al_addr;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100180};
181
182struct branch_info {
183 struct addr_map_symbol from;
184 struct addr_map_symbol to;
185 struct branch_flags flags;
186};
187
Stephane Eranian98a3b322013-01-24 16:10:35 +0100188struct mem_info {
189 struct addr_map_symbol iaddr;
190 struct addr_map_symbol daddr;
191 union perf_mem_data_src data_src;
192};
193
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200194struct addr_location {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300195 struct machine *machine;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200196 struct thread *thread;
197 struct map *map;
198 struct symbol *sym;
199 u64 addr;
200 char level;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300201 u8 filtered;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300202 u8 cpumode;
203 s32 cpu;
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400204 s32 socket;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800205};
206
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700207struct symsrc {
208 char *name;
209 int fd;
210 enum dso_binary_type type;
211
Ingo Molnar89fe8082013-09-30 12:07:11 +0200212#ifdef HAVE_LIBELF_SUPPORT
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700213 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;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300228 bool is_64_bit;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700229#endif
230};
231
232void symsrc__destroy(struct symsrc *ss);
233int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
234 enum dso_binary_type type);
Cody P Schaferd26cd122012-08-10 15:23:00 -0700235bool symsrc__has_symtab(struct symsrc *ss);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700236bool symsrc__possibly_runtime(struct symsrc *ss);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700237
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300238int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
239int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300240 const char *vmlinux, bool vmlinux_allocated,
241 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300242int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200243 symbol_filter_t filter);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300244int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
245 bool no_kcore, symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300246int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200247 symbol_filter_t filter);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300248
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300249struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
250 u64 addr);
251struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200252 const char *name);
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300253struct symbol *symbol__next_by_name(struct symbol *sym);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300254
Adrian Hunter9c00a812014-07-14 13:02:50 +0300255struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
256struct symbol *dso__next_symbol(struct symbol *sym);
257
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +0300258enum dso_type dso__type_fd(int fd);
259
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -0200260int filename__read_build_id(const char *filename, void *bf, size_t size);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200261int sysfs__read_build_id(const char *filename, void *bf, size_t size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300262int modules__parse(const char *filename, void *arg,
263 int (*process_module)(void *arg, const char *name,
264 u64 start));
Namhyung Kime5a18452012-08-06 13:41:20 +0900265int filename__read_debuglink(const char *filename, char *debuglink,
266 size_t size);
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -0200267
Kan Liangce80d3b2015-08-28 05:48:04 -0400268struct perf_env;
269int symbol__init(struct perf_env *env);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300270void symbol__exit(void);
Namhyung Kim166ccc92012-08-06 13:41:19 +0900271void symbol__elf_init(void);
Namhyung Kime5a18452012-08-06 13:41:20 +0900272struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
Arnaldo Carvalho de Melofd4be132016-04-11 22:03:56 -0300273size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
274 const struct addr_location *al,
275 bool unknown_as_addr, FILE *fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900276size_t symbol__fprintf_symname_offs(const struct symbol *sym,
277 const struct addr_location *al, FILE *fp);
Arnaldo Carvalho de Melofd4be132016-04-11 22:03:56 -0300278size_t __symbol__fprintf_symname(const struct symbol *sym,
279 const struct addr_location *al,
280 bool unknown_as_addr, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900281size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200282size_t symbol__fprintf(struct symbol *sym, FILE *fp);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200283bool symbol_type__is_a(char symbol_type, enum map_type map_type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300284bool symbol__restricted_filename(const char *filename,
285 const char *restricted_filename);
David Ahern82d1deb2013-11-18 13:32:45 -0700286bool symbol__is_idle(struct symbol *sym);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200287
Cody P Schafer261360b2012-08-10 15:23:01 -0700288int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
289 struct symsrc *runtime_ss, symbol_filter_t filter,
290 int kmodule);
Cody P Schafera44f6052012-08-10 15:22:59 -0700291int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
292 struct map *map, symbol_filter_t filter);
Namhyung Kime5a18452012-08-06 13:41:20 +0900293
294void symbols__insert(struct rb_root *symbols, struct symbol *sym);
295void symbols__fixup_duplicate(struct rb_root *symbols);
296void symbols__fixup_end(struct rb_root *symbols);
297void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
298
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300299typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
300int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
301 bool *is_64_bit);
302
Adrian Hunterafba19d2013-10-09 15:01:12 +0300303#define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
304
305struct kcore_extract {
306 char *kcore_filename;
307 u64 addr;
308 u64 offs;
309 u64 len;
310 char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
311 int fd;
312};
313
314int kcore_extract__create(struct kcore_extract *kce);
315void kcore_extract__delete(struct kcore_extract *kce);
316
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300317int kcore_copy(const char *from_dir, const char *to_dir);
318int compare_proc_modules(const char *from, const char *to);
319
David Ahern3bfe5f82013-11-18 13:32:48 -0700320int setup_list(struct strlist **list, const char *list_str,
321 const char *list_name);
David Aherne03eaa42015-03-24 09:52:41 -0600322int setup_intlist(struct intlist **list, const char *list_str,
323 const char *list_name);
David Ahern3bfe5f82013-11-18 13:32:48 -0700324
Naveen N. Raod2332092015-04-28 17:35:35 +0530325#ifdef HAVE_LIBELF_SUPPORT
326bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
Naveen N. Rao0b3c2262016-04-12 14:40:50 +0530327void arch__sym_update(struct symbol *s, GElf_Sym *sym);
Naveen N. Raod2332092015-04-28 17:35:35 +0530328#endif
329
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530330#define SYMBOL_A 0
331#define SYMBOL_B 1
332
333int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
334
John Kacur8b40f522009-09-24 18:02:18 +0200335#endif /* __PERF_SYMBOL */