map.h 6.5 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 <string.h>
12
#include <stdbool.h>
B
Borislav Petkov 已提交
13
#include <linux/types.h>
14
#include "rwsem.h"
15 16

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

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

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

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

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

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

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

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

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

void map_groups__put(struct map_groups *mg);

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

82 83 84 85 86 87 88 89 90 91
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;
}

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

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

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

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

108
struct symbol;
109
struct thread;
110

111 112 113 114 115 116 117 118
/* 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)	\
119
	dso__for_each_symbol(map->dso, pos, n)
120

121 122 123 124 125 126 127
/* 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
 */
128 129
#define __map__for_each_symbol_by_name(map, sym_name, pos)	\
	for (pos = map__find_symbol_by_name(map, sym_name);	\
130 131 132
	     pos &&						\
	     !symbol__match_symbol_name(pos->name, sym_name,	\
					SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
133 134 135
	     pos = symbol__next_by_name(pos))

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

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

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

void map__put(struct map *map);

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

#define map__zput(map) __map__zput(&map)

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

171 172 173
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);
174 175
void map__fixup_start(struct map *map);
void map__fixup_end(struct map *map);
176

177
void map__reloc_vmlinux(struct map *map);
178

179 180 181 182
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);
183
struct map *map__next(struct map *map);
184
struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
185
                                         struct map **mapp);
186
void map_groups__init(struct map_groups *mg, struct machine *machine);
187
void map_groups__exit(struct map_groups *mg);
188
int map_groups__clone(struct thread *thread,
189
		      struct map_groups *parent);
190
size_t map_groups__fprintf(struct map_groups *mg, FILE *fp);
191

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

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

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

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

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

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

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

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

225 226
struct addr_map_symbol;

227
int map_groups__find_ams(struct addr_map_symbol *ams);
228

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

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

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

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

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

243 244 245 246 247 248 249
#define ENTRY_TRAMPOLINE_NAME "__entry_SYSCALL_64_trampoline"

static inline bool is_entry_trampoline(const char *name)
{
	return !strcmp(name, ENTRY_TRAMPOLINE_NAME);
}

250
#endif /* __PERF_MAP_H */