blob: bef47ead1d9bd1efc5e9620f04b13714286ff616 [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,
108 branch_callstack;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200109 const char *vmlinux_name,
David Ahernb226a5a72010-12-07 19:39:46 -0700110 *kallsyms_name,
Chase Douglas9ed7e1b2010-06-14 15:26:30 -0400111 *source_prefix,
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200112 *field_sep;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800113 const char *default_guest_vmlinux_name,
114 *default_guest_kallsyms,
115 *default_guest_modules;
116 const char *guestmount;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300117 const char *dso_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200118 *comm_list_str,
David Aherne03eaa42015-03-24 09:52:41 -0600119 *pid_list_str,
120 *tid_list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200121 *sym_list_str,
122 *col_width_list_str;
123 struct strlist *dso_list,
124 *comm_list,
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100125 *sym_list,
126 *dso_from_list,
127 *dso_to_list,
128 *sym_from_list,
129 *sym_to_list;
David Aherne03eaa42015-03-24 09:52:41 -0600130 struct intlist *pid_list,
131 *tid_list;
David Ahernec5761e2010-12-09 13:27:07 -0700132 const char *symfs;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200133};
134
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200135extern struct symbol_conf symbol_conf;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300136
137static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
138{
139 return path__join(bf, size, symbol_conf.symfs, path);
140}
141
142#define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
143
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300144extern int vmlinux_path__nr_entries;
145extern char **vmlinux_path;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200146
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300147static inline void *symbol__priv(struct symbol *sym)
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200148{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300149 return ((void *)sym) - symbol_conf.priv_size;
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200150}
151
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200152struct ref_reloc_sym {
153 const char *name;
154 u64 addr;
155 u64 unrelocated_addr;
156};
157
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300158struct map_symbol {
159 struct map *map;
160 struct symbol *sym;
161};
162
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100163struct addr_map_symbol {
164 struct map *map;
165 struct symbol *sym;
166 u64 addr;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100167 u64 al_addr;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100168};
169
170struct branch_info {
171 struct addr_map_symbol from;
172 struct addr_map_symbol to;
173 struct branch_flags flags;
174};
175
Stephane Eranian98a3b322013-01-24 16:10:35 +0100176struct mem_info {
177 struct addr_map_symbol iaddr;
178 struct addr_map_symbol daddr;
179 union perf_mem_data_src data_src;
180};
181
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200182struct addr_location {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300183 struct machine *machine;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200184 struct thread *thread;
185 struct map *map;
186 struct symbol *sym;
187 u64 addr;
188 char level;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300189 u8 filtered;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300190 u8 cpumode;
191 s32 cpu;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800192};
193
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700194struct symsrc {
195 char *name;
196 int fd;
197 enum dso_binary_type type;
198
Ingo Molnar89fe8082013-09-30 12:07:11 +0200199#ifdef HAVE_LIBELF_SUPPORT
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700200 Elf *elf;
201 GElf_Ehdr ehdr;
202
203 Elf_Scn *opdsec;
204 size_t opdidx;
205 GElf_Shdr opdshdr;
206
207 Elf_Scn *symtab;
208 GElf_Shdr symshdr;
209
210 Elf_Scn *dynsym;
211 size_t dynsym_idx;
212 GElf_Shdr dynshdr;
213
214 bool adjust_symbols;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300215 bool is_64_bit;
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700216#endif
217};
218
219void symsrc__destroy(struct symsrc *ss);
220int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
221 enum dso_binary_type type);
Cody P Schaferd26cd122012-08-10 15:23:00 -0700222bool symsrc__has_symtab(struct symsrc *ss);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -0700223bool symsrc__possibly_runtime(struct symsrc *ss);
Cody P Schaferb68e2f92012-08-10 15:22:57 -0700224
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300225int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
226int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300227 const char *vmlinux, bool vmlinux_allocated,
228 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300229int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200230 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300231int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200232 symbol_filter_t filter);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -0300233
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300234struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
235 u64 addr);
236struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200237 const char *name);
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300238struct symbol *symbol__next_by_name(struct symbol *sym);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300239
Adrian Hunter9c00a812014-07-14 13:02:50 +0300240struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
241struct symbol *dso__next_symbol(struct symbol *sym);
242
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +0300243enum dso_type dso__type_fd(int fd);
244
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -0200245int filename__read_build_id(const char *filename, void *bf, size_t size);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200246int sysfs__read_build_id(const char *filename, void *bf, size_t size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300247int modules__parse(const char *filename, void *arg,
248 int (*process_module)(void *arg, const char *name,
249 u64 start));
Namhyung Kime5a18452012-08-06 13:41:20 +0900250int filename__read_debuglink(const char *filename, char *debuglink,
251 size_t size);
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -0200252
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900253struct perf_session_env;
254int symbol__init(struct perf_session_env *env);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300255void symbol__exit(void);
Namhyung Kim166ccc92012-08-06 13:41:19 +0900256void symbol__elf_init(void);
Namhyung Kime5a18452012-08-06 13:41:20 +0900257struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900258size_t symbol__fprintf_symname_offs(const struct symbol *sym,
259 const struct addr_location *al, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900260size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200261size_t symbol__fprintf(struct symbol *sym, FILE *fp);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200262bool symbol_type__is_a(char symbol_type, enum map_type map_type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300263bool symbol__restricted_filename(const char *filename,
264 const char *restricted_filename);
David Ahern82d1deb2013-11-18 13:32:45 -0700265bool symbol__is_idle(struct symbol *sym);
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -0200266
Cody P Schafer261360b2012-08-10 15:23:01 -0700267int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
268 struct symsrc *runtime_ss, symbol_filter_t filter,
269 int kmodule);
Cody P Schafera44f6052012-08-10 15:22:59 -0700270int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
271 struct map *map, symbol_filter_t filter);
Namhyung Kime5a18452012-08-06 13:41:20 +0900272
273void symbols__insert(struct rb_root *symbols, struct symbol *sym);
274void symbols__fixup_duplicate(struct rb_root *symbols);
275void symbols__fixup_end(struct rb_root *symbols);
276void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
277
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300278typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
279int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
280 bool *is_64_bit);
281
Adrian Hunterafba19d2013-10-09 15:01:12 +0300282#define PERF_KCORE_EXTRACT "/tmp/perf-kcore-XXXXXX"
283
284struct kcore_extract {
285 char *kcore_filename;
286 u64 addr;
287 u64 offs;
288 u64 len;
289 char extract_filename[sizeof(PERF_KCORE_EXTRACT)];
290 int fd;
291};
292
293int kcore_extract__create(struct kcore_extract *kce);
294void kcore_extract__delete(struct kcore_extract *kce);
295
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300296int kcore_copy(const char *from_dir, const char *to_dir);
297int compare_proc_modules(const char *from, const char *to);
298
David Ahern3bfe5f82013-11-18 13:32:48 -0700299int setup_list(struct strlist **list, const char *list_str,
300 const char *list_name);
David Aherne03eaa42015-03-24 09:52:41 -0600301int setup_intlist(struct intlist **list, const char *list_str,
302 const char *list_name);
David Ahern3bfe5f82013-11-18 13:32:48 -0700303
Naveen N. Raod2332092015-04-28 17:35:35 +0530304#ifdef HAVE_LIBELF_SUPPORT
305bool elf__needs_adjust_symbols(GElf_Ehdr ehdr);
Ananth N Mavinakayanahallic50fc0a2015-04-28 17:35:38 +0530306void arch__elf_sym_adjust(GElf_Sym *sym);
Naveen N. Raod2332092015-04-28 17:35:35 +0530307#endif
308
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530309#define SYMBOL_A 0
310#define SYMBOL_B 1
311
312int arch__choose_best_symbol(struct symbol *syma, struct symbol *symb);
313
John Kacur8b40f522009-09-24 18:02:18 +0200314#endif /* __PERF_SYMBOL */