Home | History | Annotate | Download | only in util
      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 
     16 #ifdef LIBELF_SUPPORT
     17 #include <libelf.h>
     18 #include <gelf.h>
     19 #endif
     20 #include <elf.h>
     21 
     22 #include "dso.h"
     23 
     24 #ifdef HAVE_CPLUS_DEMANGLE
     25 extern char *cplus_demangle(const char *, int);
     26 
     27 static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
     28 {
     29 	return cplus_demangle(c, i);
     30 }
     31 #else
     32 #ifdef NO_DEMANGLE
     33 static inline char *bfd_demangle(void __maybe_unused *v,
     34 				 const char __maybe_unused *c,
     35 				 int __maybe_unused i)
     36 {
     37 	return NULL;
     38 }
     39 #else
     40 #ifdef ANDROID
     41 /* in external/gcc-demangle */
     42 extern char *__cxa_demangle (const char *, char *, size_t *, int *);
     43 
     44 static inline char *bfd_demangle(void *v, const char *c, int i)
     45 {
     46     return __cxa_demangle(c, NULL, NULL, NULL);
     47 }
     48 #else
     49 #define PACKAGE 'perf'
     50 #include <bfd.h>
     51 #endif
     52 #endif
     53 #endif
     54 
     55 /*
     56  * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
     57  * for newer versions we can use mmap to reduce memory usage:
     58  */
     59 #ifdef LIBELF_MMAP
     60 # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP
     61 #else
     62 # define PERF_ELF_C_READ_MMAP ELF_C_READ
     63 #endif
     64 
     65 #ifndef DMGL_PARAMS
     66 #define DMGL_PARAMS      (1 << 0)       /* Include function args */
     67 #define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
     68 #endif
     69 
     70 /** struct symbol - symtab entry
     71  *
     72  * @ignore - resolvable but tools ignore it (e.g. idle routines)
     73  */
     74 struct symbol {
     75 	struct rb_node	rb_node;
     76 	u64		start;
     77 	u64		end;
     78 	u16		namelen;
     79 	u8		binding;
     80 	bool		ignore;
     81 	char		name[0];
     82 };
     83 
     84 void symbol__delete(struct symbol *sym);
     85 void symbols__delete(struct rb_root *symbols);
     86 
     87 static inline size_t symbol__size(const struct symbol *sym)
     88 {
     89 	return sym->end - sym->start + 1;
     90 }
     91 
     92 struct strlist;
     93 
     94 struct symbol_conf {
     95 	unsigned short	priv_size;
     96 	unsigned short	nr_events;
     97 	bool		try_vmlinux_path,
     98 			show_kernel_path,
     99 			use_modules,
    100 			sort_by_name,
    101 			show_nr_samples,
    102 			show_total_period,
    103 			use_callchain,
    104 			exclude_other,
    105 			show_cpu_utilization,
    106 			initialized,
    107 			kptr_restrict,
    108 			annotate_asm_raw,
    109 			annotate_src,
    110 			event_group,
    111 			demangle;
    112 	const char	*vmlinux_name,
    113 			*kallsyms_name,
    114 			*source_prefix,
    115 			*field_sep;
    116 	const char	*default_guest_vmlinux_name,
    117 			*default_guest_kallsyms,
    118 			*default_guest_modules;
    119 	const char	*guestmount;
    120 	const char	*dso_list_str,
    121 			*comm_list_str,
    122 			*sym_list_str,
    123 			*col_width_list_str;
    124        struct strlist	*dso_list,
    125 			*comm_list,
    126 			*sym_list,
    127 			*dso_from_list,
    128 			*dso_to_list,
    129 			*sym_from_list,
    130 			*sym_to_list;
    131 	const char	*symfs;
    132 };
    133 
    134 extern struct symbol_conf symbol_conf;
    135 extern int vmlinux_path__nr_entries;
    136 extern char **vmlinux_path;
    137 
    138 static inline void *symbol__priv(struct symbol *sym)
    139 {
    140 	return ((void *)sym) - symbol_conf.priv_size;
    141 }
    142 
    143 struct ref_reloc_sym {
    144 	const char	*name;
    145 	u64		addr;
    146 	u64		unrelocated_addr;
    147 };
    148 
    149 struct map_symbol {
    150 	struct map    *map;
    151 	struct symbol *sym;
    152 	bool	      unfolded;
    153 	bool	      has_children;
    154 };
    155 
    156 struct addr_map_symbol {
    157 	struct map    *map;
    158 	struct symbol *sym;
    159 	u64	      addr;
    160 	u64	      al_addr;
    161 };
    162 
    163 struct branch_info {
    164 	struct addr_map_symbol from;
    165 	struct addr_map_symbol to;
    166 	struct branch_flags flags;
    167 };
    168 
    169 struct mem_info {
    170 	struct addr_map_symbol iaddr;
    171 	struct addr_map_symbol daddr;
    172 	union perf_mem_data_src data_src;
    173 };
    174 
    175 struct addr_location {
    176 	struct thread *thread;
    177 	struct map    *map;
    178 	struct symbol *sym;
    179 	u64	      addr;
    180 	char	      level;
    181 	bool	      filtered;
    182 	u8	      cpumode;
    183 	s32	      cpu;
    184 };
    185 
    186 struct symsrc {
    187 	char *name;
    188 	int fd;
    189 	enum dso_binary_type type;
    190 
    191 #ifdef LIBELF_SUPPORT
    192 	Elf *elf;
    193 	GElf_Ehdr ehdr;
    194 
    195 	Elf_Scn *opdsec;
    196 	size_t opdidx;
    197 	GElf_Shdr opdshdr;
    198 
    199 	Elf_Scn *symtab;
    200 	GElf_Shdr symshdr;
    201 
    202 	Elf_Scn *dynsym;
    203 	size_t dynsym_idx;
    204 	GElf_Shdr dynshdr;
    205 
    206 	bool adjust_symbols;
    207 #endif
    208 };
    209 
    210 void symsrc__destroy(struct symsrc *ss);
    211 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
    212 		 enum dso_binary_type type);
    213 bool symsrc__has_symtab(struct symsrc *ss);
    214 bool symsrc__possibly_runtime(struct symsrc *ss);
    215 
    216 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
    217 int dso__load_vmlinux(struct dso *dso, struct map *map,
    218 		      const char *vmlinux, symbol_filter_t filter);
    219 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
    220 			   symbol_filter_t filter);
    221 int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map,
    222 		       symbol_filter_t filter);
    223 
    224 struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
    225 				u64 addr);
    226 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
    227 					const char *name);
    228 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type);
    229 
    230 int filename__read_build_id(const char *filename, void *bf, size_t size);
    231 int sysfs__read_build_id(const char *filename, void *bf, size_t size);
    232 int kallsyms__parse(const char *filename, void *arg,
    233 		    int (*process_symbol)(void *arg, const char *name,
    234 					  char type, u64 start));
    235 int filename__read_debuglink(const char *filename, char *debuglink,
    236 			     size_t size);
    237 
    238 int symbol__init(void);
    239 void symbol__exit(void);
    240 void symbol__elf_init(void);
    241 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
    242 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
    243 				    const struct addr_location *al, FILE *fp);
    244 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
    245 size_t symbol__fprintf(struct symbol *sym, FILE *fp);
    246 bool symbol_type__is_a(char symbol_type, enum map_type map_type);
    247 bool symbol__restricted_filename(const char *filename,
    248 				 const char *restricted_filename);
    249 
    250 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
    251 		  struct symsrc *runtime_ss, symbol_filter_t filter,
    252 		  int kmodule);
    253 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
    254 				struct map *map, symbol_filter_t filter);
    255 
    256 void symbols__insert(struct rb_root *symbols, struct symbol *sym);
    257 void symbols__fixup_duplicate(struct rb_root *symbols);
    258 void symbols__fixup_end(struct rb_root *symbols);
    259 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
    260 
    261 typedef int (*mapfn_t)(u64 start, u64 len, u64 pgoff, void *data);
    262 int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
    263 		    bool *is_64_bit);
    264 
    265 #endif /* __PERF_SYMBOL */
    266