blob: c8b7544d92675a0833c87404f8f236f5446df2c2 [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;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030058 char name[0];
59};
60
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030061void symbol__delete(struct symbol *sym);
Jiri Olsacdd059d2012-10-27 23:18:32 +020062void symbols__delete(struct rb_root *symbols);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -030063
Masami Hiramatsueb948e52014-02-06 05:32:25 +000064/* symbols__for_each_entry - iterate over symbols (rb_root)
65 *
66 * @symbols: the rb_root of symbols
67 * @pos: the 'struct symbol *' to use as a loop cursor
68 * @nd: the 'struct rb_node *' to use as a temporary storage
69 */
70#define symbols__for_each_entry(symbols, pos, nd) \
71 for (nd = rb_first(symbols); \
72 nd && (pos = rb_entry(nd, struct symbol, rb_node)); \
73 nd = rb_next(nd))
74
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -030075static inline size_t symbol__size(const struct symbol *sym)
76{
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -030077 return sym->end - sym->start;
Arnaldo Carvalho de Melo1b2e2df2012-04-19 10:57:06 -030078}
79
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -020080struct strlist;
David Aherne03eaa42015-03-24 09:52:41 -060081struct intlist;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -020082
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020083struct symbol_conf {
84 unsigned short priv_size;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -020085 unsigned short nr_events;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020086 bool try_vmlinux_path,
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -030087 force,
Willy Tarreaufc2be692013-09-14 10:32:59 +020088 ignore_vmlinux,
Namhyung Kim00dc8652014-11-04 10:14:32 +090089 ignore_vmlinux_buildid,
Akihiro Nagai0bc8d202012-01-30 13:43:20 +090090 show_kernel_path,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020091 use_modules,
Namhyung Kim680d9262015-03-06 16:31:27 +090092 allow_aliases,
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020093 sort_by_name,
94 show_nr_samples,
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -030095 show_total_period,
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020096 use_callchain,
Namhyung Kimf8be1c82012-09-11 13:15:07 +090097 cumulate_callchain,
Arnaldo Carvalho de Melof7d87442009-12-27 21:37:04 -020098 exclude_other,
Jovi Zhang85e00b52010-09-09 13:30:59 -030099 show_cpu_utilization,
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300100 initialized,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200101 kptr_restrict,
102 annotate_asm_raw,
Namhyung Kim6e1f6012013-01-22 18:09:32 +0900103 annotate_src,
Namhyung Kim328ccda2013-03-25 18:18:18 +0900104 event_group,
Namhyung Kimf2148332014-01-14 11:52:48 +0900105 demangle,
Avi Kivity763122a2014-09-13 07:15:05 +0300106 demangle_kernel,
Jiri Olsac8302362014-06-27 18:26:58 +0200107 filter_relative,
Andi Kleen8b7bad52014-11-12 18:05:20 -0800108 show_hist_headers,
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -0300109 branch_callstack,
Kan Liang9e207dd2015-08-11 06:30:49 -0400110 has_filter,
Namhyung Kimb49a8fe2015-11-26 16:08:20 +0900111 show_ref_callgraph,
Namhyung Kim053a3982015-12-23 02:07:05 +0900112 hide_unresolved,
Namhyung Kimaef810e2016-02-25 00:13:34 +0900113 raw_trace,
114 report_hierarchy;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200115 const char *vmlinux_name,
David Ahernb226a5a72010-12-07 19:39:46 -0700116 *kallsyms_name,
Chase Douglas9ed7e1b2010-06-14 15:26:30 -0400117 *source_prefix,
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200118 *field_sep;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800119 const char *default_guest_vmlinux_name,
120 *default_guest_kallsyms,
121 *default_guest_modules;
122 const char *guestmount;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300123 const char *dso_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200124 *comm_list_str,
David Aherne03eaa42015-03-24 09:52:41 -0600125 *pid_list_str,
126 *tid_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200127 *sym_list_str,
128 *col_width_list_str;
129 struct strlist *dso_list,
130 *comm_list,
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100131 *sym_list,
132 *dso_from_list,
133 *dso_to_list,
134 *sym_from_list,
135 *sym_to_list;
David Aherne03eaa42015-03-24 09:52:41 -0600136 struct intlist *pid_list,
137 *tid_list;
David Ahernec5761e2010-12-09 13:27:07 -0700138 const char *symfs;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200139};
140
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200141extern struct symbol_conf symbol_conf;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300142
143static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
144{
145 return path__join(bf, size, symbol_conf.symfs, path);
146}
147
148#define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
149
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300150extern int vmlinux_path__nr_entries;
151extern char **vmlinux_path;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200152
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300153static inline void *symbol__priv(struct symbol *sym)
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200154{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300155 return ((void *)sym) - symbol_conf.priv_size;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200156}
157
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200158struct ref_reloc_sym {
159 const char *name;
160 u64 addr;
161 u64 unrelocated_addr;
162};
163
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300164struct map_symbol {
165 struct map *map;
166 struct symbol *sym;
167};
168
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100169struct addr_map_symbol {
170 struct map *map;
171 struct symbol *sym;
172 u64 addr;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100173 u64 al_addr;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100174};
175
176struct branch_info {
177 struct addr_map_symbol from;
178 struct addr_map_symbol to;
179 struct branch_flags flags;
180};
181
Stephane Eranian98a3b322013-01-24 16:10:35 +0100182struct mem_info {
183 struct addr_map_symbol iaddr;
184 struct addr_map_symbol daddr;
185 union perf_mem_data_src data_src;
186};
187
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200188struct addr_location {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300189 struct machine *machine;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200190 struct thread *thread;
191 struct map *map;
192 struct symbol *sym;
193 u64 addr;
194 char level;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300195 u8 filtered;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300196 u8 cpumode;
197 s32 cpu;
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400198 s32 socket;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800199};
200
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700201struct symsrc {
202 char *name;
203 int fd;
204 enum dso_binary_type type;
205
Ingo Molnar89fe8082013-09-30 12:07:11 +0200206#ifdef HAVE_LIBELF_SUPPORT
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700207 Elf *elf;
208 GElf_Ehdr ehdr;
209
210 Elf_Scn *opdsec;
211 size_t opdidx;
212 GElf_Shdr opdshdr;
213
214 Elf_Scn *symtab;
215 GElf_Shdr symshdr;
216
217 Elf_Scn *dynsym;
218 size_t dynsym_idx;
219 GElf_Shdr dynshdr;
220
221 bool adjust_symbols;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300222 bool is_64_bit;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700223#endif
224};
225
226void symsrc__destroy(struct symsrc *ss);
227int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
228 enum dso_binary_type type);
Cody P Schaferd26cd122012-08-10 15:23:00 -0700229bool symsrc__has_symtab(struct symsrc *ss);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700230bool symsrc__possibly_runtime(struct symsrc *ss);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700231
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300232int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
233int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300234 const char *vmlinux, bool vmlinux_allocated,
235 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300236int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200237 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300238int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200239 symbol_filter_t filter);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300240
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300241struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
242 u64 addr);
243struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200244 const char *name);
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300245struct symbol *symbol__next_by_name(struct symbol *sym);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300246
Adrian Hunter9c00a812014-07-14 13:02:50 +0300247struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
248struct symbol *dso__next_symbol(struct symbol *sym);
249
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +0300250enum dso_type dso__type_fd(int fd);
251
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -0200252int filename__read_build_id(const char *filename, void *bf, size_t size);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200253int sysfs__read_build_id(const char *filename, void *bf, size_t size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300254int modules__parse(const char *filename, void *arg,
255 int (*process_module)(void *arg, const char *name,
256 u64 start));
Namhyung Kime5a18452012-08-06 13:41:20 +0900257int filename__read_debuglink(const char *filename, char *debuglink,
258 size_t size);
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -0200259
Kan Liangce80d3b2015-08-28 05:48:04 -0400260struct perf_env;
261int symbol__init(struct perf_env *env);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300262void symbol__exit(void);
Namhyung Kim166ccc92012-08-06 13:41:19 +0900263void symbol__elf_init(void);
Namhyung Kime5a18452012-08-06 13:41:20 +0900264struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900265size_t symbol__fprintf_symname_offs(const struct symbol *sym,
266 const struct addr_location *al, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900267size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200268size_t symbol__fprintf(struct symbol *sym, FILE *fp);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200269bool symbol_type__is_a(char symbol_type, enum map_type map_type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300270bool symbol__restricted_filename(const char *filename,
271 const char *restricted_filename);
David Ahern82d1deb2013-11-18 13:32:45 -0700272bool symbol__is_idle(struct symbol *sym);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200273
Cody P Schafer261360b2012-08-10 15:23:01 -0700274int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
275 struct symsrc *runtime_ss, symbol_filter_t filter,
276 int kmodule);
Cody P Schafera44f6052012-08-10 15:22:59 -0700277int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
278 struct map *map, symbol_filter_t filter);
Namhyung Kime5a18452012-08-06 13:41:20 +0900279
280void symbols__insert(struct rb_root *symbols, struct symbol *sym);
281void symbols__fixup_duplicate(struct rb_root *symbols);
282void symbols__fixup_end(struct rb_root *symbols);
283void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
284
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300285typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
286int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
287 bool *is_64_bit);
288
Adrian Hunterafba19d2013-10-09 15:01:12 +0300289#define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
290
291struct kcore_extract {
292 char *kcore_filename;
293 u64 addr;
294 u64 offs;
295 u64 len;
296 char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
297 int fd;
298};
299
300int kcore_extract__create(struct kcore_extract *kce);
301void kcore_extract__delete(struct kcore_extract *kce);
302
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300303int kcore_copy(const char *from_dir, const char *to_dir);
304int compare_proc_modules(const char *from, const char *to);
305
David Ahern3bfe5f82013-11-18 13:32:48 -0700306int setup_list(struct strlist **list, const char *list_str,
307 const char *list_name);
David Aherne03eaa42015-03-24 09:52:41 -0600308int setup_intlist(struct intlist **list, const char *list_str,
309 const char *list_name);
David Ahern3bfe5f82013-11-18 13:32:48 -0700310
Naveen N. Raod2332092015-04-28 17:35:35 +0530311#ifdef HAVE_LIBELF_SUPPORT
312bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +0530313void arch__elf_sym_adjust(GElf_Sym *sym);
Naveen N. Raod2332092015-04-28 17:35:35 +0530314#endif
315
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530316#define SYMBOL_A 0
317#define SYMBOL_B 1
318
319int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
320
John Kacur8b40f522009-09-24 18:02:18 +0200321#endif /* __PERF_SYMBOL */