map.h 6.3 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4
#ifndef __PERF_MAP_H
#define __PERF_MAP_H

5
#include <linux/refcount.h>
6 7 8
#include <linux/compiler.h>
#include <linux/list.h>
#include <linux/rbtree.h>
9
#include <pthread.h>
10
#include <stdio.h>
11
#include <stdbool.h>
B
Borislav Petkov 已提交
12
#include <linux/types.h>
13
#include "rwsem.h"
14 15

struct dso;
16
struct ip_callchain;
17 18
struct ref_reloc_sym;
struct map_groups;
19
struct machine;
20
struct perf_evsel;
21 22 23 24 25 26 27 28

struct map {
	union {
		struct rb_node	rb_node;
		struct list_head node;
	};
	u64			start;
	u64			end;
29
	bool			erange_warned;
30
	u32			priv;
31 32
	u32			prot;
	u32			flags;
33
	u64			pgoff;
34
	u64			reloc;
35 36 37
	u32			maj, min; /* only valid for MMAP2 record */
	u64			ino;      /* only valid for MMAP2 record */
	u64			ino_generation;/* only valid for MMAP2 record */
38 39

	/* ip -> dso rip */
40
	u64			(*map_ip)(struct map *, u64);
41
	/* dso rip -> ip */
42
	u64			(*unmap_ip)(struct map *, u64);
43

44
	struct dso		*dso;
45
	struct map_groups	*groups;
46
	refcount_t		refcnt;
47 48
};

49 50 51 52 53
struct kmap {
	struct ref_reloc_sym	*ref_reloc_sym;
	struct map_groups	*kmaps;
};

54 55
struct maps {
	struct rb_root	 entries;
56
	struct rw_semaphore lock;
57 58
};

59
struct map_groups {
60
	struct maps	 maps;
61
	struct machine	 *machine;
62
	refcount_t	 refcnt;
63 64
};

65
struct map_groups *map_groups__new(struct machine *machine);
66
void map_groups__delete(struct map_groups *mg);
67
bool map_groups__empty(struct map_groups *mg);
68

69 70
static inline struct map_groups *map_groups__get(struct map_groups *mg)
{
71
	if (mg)
72
		refcount_inc(&mg->refcnt);
73 74 75 76 77
	return mg;
}

void map_groups__put(struct map_groups *mg);

78 79
struct kmap *map__kmap(struct map *map);
struct map_groups *map__kmaps(struct map *map);
80

81 82 83 84 85 86 87 88 89 90
static inline u64 map__map_ip(struct map *map, u64 ip)
{
	return ip - map->start + map->pgoff;
}

static inline u64 map__unmap_ip(struct map *map, u64 ip)
{
	return ip + map->start - map->pgoff;
}

91
static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
92 93 94 95
{
	return ip;
}

96 97 98 99
static inline size_t map__size(const struct map *map)
{
	return map->end - map->start;
}
100

101
/* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
102 103
u64 map__rip_2objdump(struct map *map, u64 rip);

104 105 106
/* objdump address -> memory address */
u64 map__objdump_2mem(struct map *map, u64 ip);

107
struct symbol;
108
struct thread;
109

110 111 112 113 114 115 116 117
/* map__for_each_symbol - iterate over the symbols in the given map
 *
 * @map: the 'struct map *' in which symbols itereated
 * @pos: the 'struct symbol *' to use as a loop cursor
 * @n: the 'struct rb_node *' to use as a temporary storage
 * Note: caller must ensure map->dso is not NULL (map is loaded).
 */
#define map__for_each_symbol(map, pos, n)	\
118
	dso__for_each_symbol(map->dso, pos, n)
119

120 121 122 123 124 125 126
/* map__for_each_symbol_with_name - iterate over the symbols in the given map
 *                                  that have the given name
 *
 * @map: the 'struct map *' in which symbols itereated
 * @sym_name: the symbol name
 * @pos: the 'struct symbol *' to use as a loop cursor
 */
127 128
#define __map__for_each_symbol_by_name(map, sym_name, pos)	\
	for (pos = map__find_symbol_by_name(map, sym_name);	\
129 130 131
	     pos &&						\
	     !symbol__match_symbol_name(pos->name, sym_name,	\
					SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
132 133 134
	     pos = symbol__next_by_name(pos))

#define map__for_each_symbol_by_name(map, sym_name, pos)		\
135
	__map__for_each_symbol_by_name(map, sym_name, (pos))
136

137
void map__init(struct map *map,
138
	       u64 start, u64 end, u64 pgoff, struct dso *dso);
139
struct map *map__new(struct machine *machine, u64 start, u64 len,
140
		     u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
141
		     u64 ino_gen, u32 prot, u32 flags,
142 143
		     char *filename, struct thread *thread);
struct map *map__new2(u64 start, struct dso *dso);
144 145
void map__delete(struct map *map);
struct map *map__clone(struct map *map);
146 147 148 149

static inline struct map *map__get(struct map *map)
{
	if (map)
150
		refcount_inc(&map->refcnt);
151 152 153 154 155
	return map;
}

void map__put(struct map *map);

156 157 158 159 160 161 162 163
static inline void __map__zput(struct map **map)
{
	map__put(*map);
	*map = NULL;
}

#define map__zput(map) __map__zput(&map)

164
int map__overlap(struct map *l, struct map *r);
165
size_t map__fprintf(struct map *map, FILE *fp);
166
size_t map__fprintf_dsoname(struct map *map, FILE *fp);
167 168
int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
			 FILE *fp);
169

170 171 172
int map__load(struct map *map);
struct symbol *map__find_symbol(struct map *map, u64 addr);
struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
173 174
void map__fixup_start(struct map *map);
void map__fixup_end(struct map *map);
175

176
void map__reloc_vmlinux(struct map *map);
177

178 179 180 181
void maps__insert(struct maps *maps, struct map *map);
void maps__remove(struct maps *maps, struct map *map);
struct map *maps__find(struct maps *maps, u64 addr);
struct map *maps__first(struct maps *maps);
182
struct map *map__next(struct map *map);
183
struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
184
                                         struct map **mapp);
185
void map_groups__init(struct map_groups *mg, struct machine *machine);
186
void map_groups__exit(struct map_groups *mg);
187
int map_groups__clone(struct thread *thread,
188
		      struct map_groups *parent);
189
size_t map_groups__fprintf(struct map_groups *mg, FILE *fp);
190

191 192
int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name,
				    u64 addr);
193

194
static inline void map_groups__insert(struct map_groups *mg, struct map *map)
195
{
196
	maps__insert(&mg->maps, map);
197
	map->groups = mg;
198 199
}

200
static inline void map_groups__remove(struct map_groups *mg, struct map *map)
201
{
202
	maps__remove(&mg->maps, map);
203 204
}

205 206
static inline struct map *map_groups__find(struct map_groups *mg, u64 addr)
{
207
	return maps__find(&mg->maps, addr);
208 209
}

210
struct map *map_groups__first(struct map_groups *mg);
211 212 213

static inline struct map *map_groups__next(struct map *map)
{
214
	return map__next(map);
215 216
}

217
struct symbol *map_groups__find_symbol(struct map_groups *mg,
218
				       u64 addr, struct map **mapp);
219

220
struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
221
					       const char *name,
222
					       struct map **mapp);
223

224 225
struct addr_map_symbol;

226
int map_groups__find_ams(struct addr_map_symbol *ams);
227

228
int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
229
				   FILE *fp);
230

231
struct map *map_groups__find_by_name(struct map_groups *mg, const char *name);
232

233 234 235 236 237 238 239
bool __map__is_kernel(const struct map *map);

static inline bool __map__is_kmodule(const struct map *map)
{
	return !__map__is_kernel(map);
}

240 241
bool map__has_symbols(const struct map *map);

242
#endif /* __PERF_MAP_H */