Home | History | Annotate | Download | only in util
      1 #ifndef __PERF_MAP_H
      2 #define __PERF_MAP_H
      3 
      4 /* ANDROID_CHANGE_BEGIN */
      5 #if 0
      6 #include <linux/compiler.h>
      7 #include <linux/list.h>
      8 #include <linux/rbtree.h>
      9 #else
     10 #include "include/linux/compiler.h"
     11 #include "include/linux/list.h"
     12 #include "include/linux/rbtree.h"
     13 #endif
     14 /* ANDROID_CHANGE_END */
     15 #include <stdio.h>
     16 #include <stdbool.h>
     17 #include "types.h"
     18 
     19 enum map_type {
     20 	MAP__FUNCTION = 0,
     21 	MAP__VARIABLE,
     22 };
     23 
     24 #define MAP__NR_TYPES (MAP__VARIABLE + 1)
     25 
     26 extern const char *map_type__name[MAP__NR_TYPES];
     27 
     28 struct dso;
     29 struct ref_reloc_sym;
     30 struct map_groups;
     31 struct machine;
     32 
     33 struct map {
     34 	union {
     35 		struct rb_node	rb_node;
     36 		struct list_head node;
     37 	};
     38 	u64			start;
     39 	u64			end;
     40 	u8 /* enum map_type */	type;
     41 	bool			referenced;
     42 	u32			priv;
     43 	u64			pgoff;
     44 
     45 	/* ip -> dso rip */
     46 	u64			(*map_ip)(struct map *, u64);
     47 	/* dso rip -> ip */
     48 	u64			(*unmap_ip)(struct map *, u64);
     49 
     50 	struct dso		*dso;
     51 	struct map_groups	*groups;
     52 };
     53 
     54 struct kmap {
     55 	struct ref_reloc_sym	*ref_reloc_sym;
     56 	struct map_groups	*kmaps;
     57 };
     58 
     59 struct map_groups {
     60 	struct rb_root	 maps[MAP__NR_TYPES];
     61 	struct list_head removed_maps[MAP__NR_TYPES];
     62 	struct machine	 *machine;
     63 };
     64 
     65 /* Native host kernel uses -1 as pid index in machine */
     66 #define	HOST_KERNEL_ID			(-1)
     67 #define	DEFAULT_GUEST_KERNEL_ID		(0)
     68 
     69 struct machine {
     70 	struct rb_node	  rb_node;
     71 	pid_t		  pid;
     72 	char		  *root_dir;
     73 	struct list_head  user_dsos;
     74 	struct list_head  kernel_dsos;
     75 	struct map_groups kmaps;
     76 	struct map	  *vmlinux_maps[MAP__NR_TYPES];
     77 };
     78 
     79 static inline
     80 struct map *machine__kernel_map(struct machine *self, enum map_type type)
     81 {
     82 	return self->vmlinux_maps[type];
     83 }
     84 
     85 static inline struct kmap *map__kmap(struct map *self)
     86 {
     87 	return (struct kmap *)(self + 1);
     88 }
     89 
     90 static inline u64 map__map_ip(struct map *map, u64 ip)
     91 {
     92 	return ip - map->start + map->pgoff;
     93 }
     94 
     95 static inline u64 map__unmap_ip(struct map *map, u64 ip)
     96 {
     97 	return ip + map->start - map->pgoff;
     98 }
     99 
    100 static inline u64 identity__map_ip(struct map *map __used, u64 ip)
    101 {
    102 	return ip;
    103 }
    104 
    105 
    106 /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
    107 u64 map__rip_2objdump(struct map *map, u64 rip);
    108 u64 map__objdump_2ip(struct map *map, u64 addr);
    109 
    110 struct symbol;
    111 
    112 typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
    113 
    114 void map__init(struct map *self, enum map_type type,
    115 	       u64 start, u64 end, u64 pgoff, struct dso *dso);
    116 struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
    117 		     u64 pgoff, u32 pid, char *filename,
    118 		     enum map_type type);
    119 void map__delete(struct map *self);
    120 struct map *map__clone(struct map *self);
    121 int map__overlap(struct map *l, struct map *r);
    122 size_t map__fprintf(struct map *self, FILE *fp);
    123 
    124 int map__load(struct map *self, symbol_filter_t filter);
    125 struct symbol *map__find_symbol(struct map *self,
    126 				u64 addr, symbol_filter_t filter);
    127 struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
    128 					symbol_filter_t filter);
    129 void map__fixup_start(struct map *self);
    130 void map__fixup_end(struct map *self);
    131 
    132 void map__reloc_vmlinux(struct map *self);
    133 
    134 size_t __map_groups__fprintf_maps(struct map_groups *self,
    135 				  enum map_type type, int verbose, FILE *fp);
    136 void maps__insert(struct rb_root *maps, struct map *map);
    137 void maps__remove(struct rb_root *self, struct map *map);
    138 struct map *maps__find(struct rb_root *maps, u64 addr);
    139 void map_groups__init(struct map_groups *self);
    140 void map_groups__exit(struct map_groups *self);
    141 int map_groups__clone(struct map_groups *self,
    142 		      struct map_groups *parent, enum map_type type);
    143 size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp);
    144 size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp);
    145 
    146 typedef void (*machine__process_t)(struct machine *self, void *data);
    147 
    148 void machines__process(struct rb_root *self, machine__process_t process, void *data);
    149 struct machine *machines__add(struct rb_root *self, pid_t pid,
    150 			      const char *root_dir);
    151 struct machine *machines__find_host(struct rb_root *self);
    152 struct machine *machines__find(struct rb_root *self, pid_t pid);
    153 struct machine *machines__findnew(struct rb_root *self, pid_t pid);
    154 char *machine__mmap_name(struct machine *self, char *bf, size_t size);
    155 int machine__init(struct machine *self, const char *root_dir, pid_t pid);
    156 void machine__exit(struct machine *self);
    157 void machine__delete(struct machine *self);
    158 
    159 /*
    160  * Default guest kernel is defined by parameter --guestkallsyms
    161  * and --guestmodules
    162  */
    163 static inline bool machine__is_default_guest(struct machine *self)
    164 {
    165 	return self ? self->pid == DEFAULT_GUEST_KERNEL_ID : false;
    166 }
    167 
    168 static inline bool machine__is_host(struct machine *self)
    169 {
    170 	return self ? self->pid == HOST_KERNEL_ID : false;
    171 }
    172 
    173 static inline void map_groups__insert(struct map_groups *self, struct map *map)
    174 {
    175 	maps__insert(&self->maps[map->type], map);
    176 	map->groups = self;
    177 }
    178 
    179 static inline void map_groups__remove(struct map_groups *self, struct map *map)
    180 {
    181 	maps__remove(&self->maps[map->type], map);
    182 }
    183 
    184 static inline struct map *map_groups__find(struct map_groups *self,
    185 					   enum map_type type, u64 addr)
    186 {
    187 	return maps__find(&self->maps[type], addr);
    188 }
    189 
    190 struct symbol *map_groups__find_symbol(struct map_groups *self,
    191 				       enum map_type type, u64 addr,
    192 				       struct map **mapp,
    193 				       symbol_filter_t filter);
    194 
    195 struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
    196 					       enum map_type type,
    197 					       const char *name,
    198 					       struct map **mapp,
    199 					       symbol_filter_t filter);
    200 
    201 static inline
    202 struct symbol *machine__find_kernel_symbol(struct machine *self,
    203 					   enum map_type type, u64 addr,
    204 					   struct map **mapp,
    205 					   symbol_filter_t filter)
    206 {
    207 	return map_groups__find_symbol(&self->kmaps, type, addr, mapp, filter);
    208 }
    209 
    210 static inline
    211 struct symbol *machine__find_kernel_function(struct machine *self, u64 addr,
    212 					     struct map **mapp,
    213 					     symbol_filter_t filter)
    214 {
    215 	return machine__find_kernel_symbol(self, MAP__FUNCTION, addr, mapp, filter);
    216 }
    217 
    218 static inline
    219 struct symbol *map_groups__find_function_by_name(struct map_groups *self,
    220 						 const char *name, struct map **mapp,
    221 						 symbol_filter_t filter)
    222 {
    223 	return map_groups__find_symbol_by_name(self, MAP__FUNCTION, name, mapp, filter);
    224 }
    225 
    226 static inline
    227 struct symbol *machine__find_kernel_function_by_name(struct machine *self,
    228 						     const char *name,
    229 						     struct map **mapp,
    230 						     symbol_filter_t filter)
    231 {
    232 	return map_groups__find_function_by_name(&self->kmaps, name, mapp,
    233 						 filter);
    234 }
    235 
    236 int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
    237 				   int verbose, FILE *fp);
    238 
    239 struct map *map_groups__find_by_name(struct map_groups *self,
    240 				     enum map_type type, const char *name);
    241 struct map *machine__new_module(struct machine *self, u64 start, const char *filename);
    242 
    243 void map_groups__flush(struct map_groups *self);
    244 
    245 #endif /* __PERF_MAP_H */
    246