blob: 40073c60b83d6b7e91fe2e3208f50210cf61f6ce [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
37extern Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
38 GElf_Shdr *shp, const char *name, size_t *idx);
39#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,
Willy Tarreaufc2be692013-09-14 10:32:59 +020087 ignore_vmlinux,
Namhyung Kim00dc8652014-11-04 10:14:32 +090088 ignore_vmlinux_buildid,
Akihiro Nagai0bc8d202012-01-30 13:43:20 +090089 show_kernel_path,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020090 use_modules,
Namhyung Kim680d9262015-03-06 16:31:27 +090091 allow_aliases,
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020092 sort_by_name,
93 show_nr_samples,
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -030094 show_total_period,
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020095 use_callchain,
Namhyung Kimf8be1c82012-09-11 13:15:07 +090096 cumulate_callchain,
Arnaldo Carvalho de Melof7d87442009-12-27 21:37:04 -020097 exclude_other,
Jovi Zhang85e00b52010-09-09 13:30:59 -030098 show_cpu_utilization,
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -030099 initialized,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200100 kptr_restrict,
101 annotate_asm_raw,
Namhyung Kim6e1f6012013-01-22 18:09:32 +0900102 annotate_src,
Namhyung Kim328ccda2013-03-25 18:18:18 +0900103 event_group,
Namhyung Kimf2148332014-01-14 11:52:48 +0900104 demangle,
Avi Kivity763122a2014-09-13 07:15:05 +0300105 demangle_kernel,
Jiri Olsac8302362014-06-27 18:26:58 +0200106 filter_relative,
Andi Kleen8b7bad52014-11-12 18:05:20 -0800107 show_hist_headers,
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -0300108 branch_callstack,
Kan Liang9e207dd2015-08-11 06:30:49 -0400109 has_filter,
110 show_ref_callgraph;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200111 const char *vmlinux_name,
David Ahernb226a5a72010-12-07 19:39:46 -0700112 *kallsyms_name,
Chase Douglas9ed7e1b2010-06-14 15:26:30 -0400113 *source_prefix,
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200114 *field_sep;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800115 const char *default_guest_vmlinux_name,
116 *default_guest_kallsyms,
117 *default_guest_modules;
118 const char *guestmount;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300119 const char *dso_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200120 *comm_list_str,
David Aherne03eaa42015-03-24 09:52:41 -0600121 *pid_list_str,
122 *tid_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200123 *sym_list_str,
124 *col_width_list_str;
125 struct strlist *dso_list,
126 *comm_list,
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100127 *sym_list,
128 *dso_from_list,
129 *dso_to_list,
130 *sym_from_list,
131 *sym_to_list;
David Aherne03eaa42015-03-24 09:52:41 -0600132 struct intlist *pid_list,
133 *tid_list;
David Ahernec5761e2010-12-09 13:27:07 -0700134 const char *symfs;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200135};
136
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200137extern struct symbol_conf symbol_conf;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300138
139static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
140{
141 return path__join(bf, size, symbol_conf.symfs, path);
142}
143
144#define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
145
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300146extern int vmlinux_path__nr_entries;
147extern char **vmlinux_path;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200148
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300149static inline void *symbol__priv(struct symbol *sym)
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200150{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300151 return ((void *)sym) - symbol_conf.priv_size;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200152}
153
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200154struct ref_reloc_sym {
155 const char *name;
156 u64 addr;
157 u64 unrelocated_addr;
158};
159
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300160struct map_symbol {
161 struct map *map;
162 struct symbol *sym;
163};
164
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100165struct addr_map_symbol {
166 struct map *map;
167 struct symbol *sym;
168 u64 addr;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100169 u64 al_addr;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100170};
171
172struct branch_info {
173 struct addr_map_symbol from;
174 struct addr_map_symbol to;
175 struct branch_flags flags;
176};
177
Stephane Eranian98a3b322013-01-24 16:10:35 +0100178struct mem_info {
179 struct addr_map_symbol iaddr;
180 struct addr_map_symbol daddr;
181 union perf_mem_data_src data_src;
182};
183
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200184struct addr_location {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300185 struct machine *machine;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200186 struct thread *thread;
187 struct map *map;
188 struct symbol *sym;
189 u64 addr;
190 char level;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300191 u8 filtered;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300192 u8 cpumode;
193 s32 cpu;
Kan Liang0c4c4de2015-09-04 10:45:42 -0400194 s32 socket;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800195};
196
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700197struct symsrc {
198 char *name;
199 int fd;
200 enum dso_binary_type type;
201
Ingo Molnar89fe8082013-09-30 12:07:11 +0200202#ifdef HAVE_LIBELF_SUPPORT
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700203 Elf *elf;
204 GElf_Ehdr ehdr;
205
206 Elf_Scn *opdsec;
207 size_t opdidx;
208 GElf_Shdr opdshdr;
209
210 Elf_Scn *symtab;
211 GElf_Shdr symshdr;
212
213 Elf_Scn *dynsym;
214 size_t dynsym_idx;
215 GElf_Shdr dynshdr;
216
217 bool adjust_symbols;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300218 bool is_64_bit;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700219#endif
220};
221
222void symsrc__destroy(struct symsrc *ss);
223int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
224 enum dso_binary_type type);
Cody P Schaferd26cd122012-08-10 15:23:00 -0700225bool symsrc__has_symtab(struct symsrc *ss);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700226bool symsrc__possibly_runtime(struct symsrc *ss);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700227
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300228int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
229int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300230 const char *vmlinux, bool vmlinux_allocated,
231 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300232int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200233 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300234int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200235 symbol_filter_t filter);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300236
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300237struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
238 u64 addr);
239struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200240 const char *name);
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300241struct symbol *symbol__next_by_name(struct symbol *sym);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300242
Adrian Hunter9c00a812014-07-14 13:02:50 +0300243struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
244struct symbol *dso__next_symbol(struct symbol *sym);
245
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +0300246enum dso_type dso__type_fd(int fd);
247
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200248int filename__read_build_id(const char *filename, void *bf, size_t size);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200249int sysfs__read_build_id(const char *filename, void *bf, size_t size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300250int modules__parse(const char *filename, void *arg,
251 int (*process_module)(void *arg, const char *name,
252 u64 start));
Namhyung Kime5a18452012-08-06 13:41:20 +0900253int filename__read_debuglink(const char *filename, char *debuglink,
254 size_t size);
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200255
Kan Liangce80d3b2015-08-28 05:48:04 -0400256struct perf_env;
257int symbol__init(struct perf_env *env);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300258void symbol__exit(void);
Namhyung Kim166ccc92012-08-06 13:41:19 +0900259void symbol__elf_init(void);
Namhyung Kime5a18452012-08-06 13:41:20 +0900260struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900261size_t symbol__fprintf_symname_offs(const struct symbol *sym,
262 const struct addr_location *al, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900263size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200264size_t symbol__fprintf(struct symbol *sym, FILE *fp);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200265bool symbol_type__is_a(char symbol_type, enum map_type map_type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300266bool symbol__restricted_filename(const char *filename,
267 const char *restricted_filename);
David Ahern82d1deb2013-11-18 13:32:45 -0700268bool symbol__is_idle(struct symbol *sym);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200269
Cody P Schafer261360b2012-08-10 15:23:01 -0700270int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
271 struct symsrc *runtime_ss, symbol_filter_t filter,
272 int kmodule);
Cody P Schafera44f6052012-08-10 15:22:59 -0700273int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
274 struct map *map, symbol_filter_t filter);
Namhyung Kime5a18452012-08-06 13:41:20 +0900275
276void symbols__insert(struct rb_root *symbols, struct symbol *sym);
277void symbols__fixup_duplicate(struct rb_root *symbols);
278void symbols__fixup_end(struct rb_root *symbols);
279void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
280
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300281typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
282int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
283 bool *is_64_bit);
284
Adrian Hunterafba19d2013-10-09 15:01:12 +0300285#define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
286
287struct kcore_extract {
288 char *kcore_filename;
289 u64 addr;
290 u64 offs;
291 u64 len;
292 char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
293 int fd;
294};
295
296int kcore_extract__create(struct kcore_extract *kce);
297void kcore_extract__delete(struct kcore_extract *kce);
298
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300299int kcore_copy(const char *from_dir, const char *to_dir);
300int compare_proc_modules(const char *from, const char *to);
301
David Ahern3bfe5f82013-11-18 13:32:48 -0700302int setup_list(struct strlist **list, const char *list_str,
303 const char *list_name);
David Aherne03eaa42015-03-24 09:52:41 -0600304int setup_intlist(struct intlist **list, const char *list_str,
305 const char *list_name);
David Ahern3bfe5f82013-11-18 13:32:48 -0700306
Naveen N. Raod2332092015-04-28 17:35:35 +0530307#ifdef HAVE_LIBELF_SUPPORT
308bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +0530309void arch__elf_sym_adjust(GElf_Sym *sym);
Naveen N. Raod2332092015-04-28 17:35:35 +0530310#endif
311
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530312#define SYMBOL_A 0
313#define SYMBOL_B 1
314
315int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
316
John Kacur8b40f522009-09-24 18:02:18 +0200317#endif /* __PERF_SYMBOL */