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
19enum map_type {
20	MAP__FUNCTION = 0,
21	MAP__VARIABLE,
22};
23
24#define MAP__NR_TYPES (MAP__VARIABLE + 1)
25
26extern const char *map_type__name[MAP__NR_TYPES];
27
28struct dso;
29struct ref_reloc_sym;
30struct map_groups;
31struct machine;
32
33struct 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
54struct kmap {
55	struct ref_reloc_sym	*ref_reloc_sym;
56	struct map_groups	*kmaps;
57};
58
59struct 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
69struct 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
79static inline
80struct map *machine__kernel_map(struct machine *self, enum map_type type)
81{
82	return self->vmlinux_maps[type];
83}
84
85static inline struct kmap *map__kmap(struct map *self)
86{
87	return (struct kmap *)(self + 1);
88}
89
90static inline u64 map__map_ip(struct map *map, u64 ip)
91{
92	return ip - map->start + map->pgoff;
93}
94
95static inline u64 map__unmap_ip(struct map *map, u64 ip)
96{
97	return ip + map->start - map->pgoff;
98}
99
100static 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=` */
107u64 map__rip_2objdump(struct map *map, u64 rip);
108u64 map__objdump_2ip(struct map *map, u64 addr);
109
110struct symbol;
111
112typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
113
114void map__init(struct map *self, enum map_type type,
115	       u64 start, u64 end, u64 pgoff, struct dso *dso);
116struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
117		     u64 pgoff, u32 pid, char *filename,
118		     enum map_type type);
119void map__delete(struct map *self);
120struct map *map__clone(struct map *self);
121int map__overlap(struct map *l, struct map *r);
122size_t map__fprintf(struct map *self, FILE *fp);
123
124int map__load(struct map *self, symbol_filter_t filter);
125struct symbol *map__find_symbol(struct map *self,
126				u64 addr, symbol_filter_t filter);
127struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
128					symbol_filter_t filter);
129void map__fixup_start(struct map *self);
130void map__fixup_end(struct map *self);
131
132void map__reloc_vmlinux(struct map *self);
133
134size_t __map_groups__fprintf_maps(struct map_groups *self,
135				  enum map_type type, int verbose, FILE *fp);
136void maps__insert(struct rb_root *maps, struct map *map);
137void maps__remove(struct rb_root *self, struct map *map);
138struct map *maps__find(struct rb_root *maps, u64 addr);
139void map_groups__init(struct map_groups *self);
140void map_groups__exit(struct map_groups *self);
141int map_groups__clone(struct map_groups *self,
142		      struct map_groups *parent, enum map_type type);
143size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp);
144size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp);
145
146typedef void (*machine__process_t)(struct machine *self, void *data);
147
148void machines__process(struct rb_root *self, machine__process_t process, void *data);
149struct machine *machines__add(struct rb_root *self, pid_t pid,
150			      const char *root_dir);
151struct machine *machines__find_host(struct rb_root *self);
152struct machine *machines__find(struct rb_root *self, pid_t pid);
153struct machine *machines__findnew(struct rb_root *self, pid_t pid);
154char *machine__mmap_name(struct machine *self, char *bf, size_t size);
155int machine__init(struct machine *self, const char *root_dir, pid_t pid);
156void machine__exit(struct machine *self);
157void machine__delete(struct machine *self);
158
159/*
160 * Default guest kernel is defined by parameter --guestkallsyms
161 * and --guestmodules
162 */
163static inline bool machine__is_default_guest(struct machine *self)
164{
165	return self ? self->pid == DEFAULT_GUEST_KERNEL_ID : false;
166}
167
168static inline bool machine__is_host(struct machine *self)
169{
170	return self ? self->pid == HOST_KERNEL_ID : false;
171}
172
173static 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
179static inline void map_groups__remove(struct map_groups *self, struct map *map)
180{
181	maps__remove(&self->maps[map->type], map);
182}
183
184static 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
190struct 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
195struct 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
201static inline
202struct 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
210static inline
211struct 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
218static inline
219struct 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
226static inline
227struct 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
236int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
237				   int verbose, FILE *fp);
238
239struct map *map_groups__find_by_name(struct map_groups *self,
240				     enum map_type type, const char *name);
241struct map *machine__new_module(struct machine *self, u64 start, const char *filename);
242
243void map_groups__flush(struct map_groups *self);
244
245#endif /* __PERF_MAP_H */
246