projects/linux·blob master · refs

318 lines · 8.0 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __PERF_MAP_H
3#define __PERF_MAP_H
4
5#include <stdbool.h>
6#include <stdio.h>
7#include <string.h>
8
9#include <linux/refcount.h>
10#include <linux/types.h>
11
12#include <internal/rc_check.h>
13
14struct dso;
15struct maps;
16struct machine;
17
18enum mapping_type {
19 /* map__map_ip/map__unmap_ip are given as offsets in the DSO. */
20 MAPPING_TYPE__DSO,
21 /* map__map_ip/map__unmap_ip are just the given ip value. */
22 MAPPING_TYPE__IDENTITY,
23};
24
25DECLARE_RC_STRUCT(map) {
26 u64 start;
27 u64 end;
28 u64 pgoff;
29 u64 reloc;
30 struct dso *dso;
31 refcount_t refcnt;
32 u32 prot;
33 u32 flags;
34 enum mapping_type mapping_type:8;
35 bool erange_warned;
36 bool priv;
37 bool hit;
38};
39
40struct kmap;
41
42struct kmap *__map__kmap(struct map *map);
43struct kmap *map__kmap(struct map *map);
44struct maps *map__kmaps(struct map *map);
45
46static inline struct dso *map__dso(const struct map *map)
47{
48 return RC_CHK_ACCESS(map)->dso;
49}
50
51static inline u64 map__start(const struct map *map)
52{
53 return RC_CHK_ACCESS(map)->start;
54}
55
56static inline u64 map__end(const struct map *map)
57{
58 return RC_CHK_ACCESS(map)->end;
59}
60
61static inline u64 map__pgoff(const struct map *map)
62{
63 return RC_CHK_ACCESS(map)->pgoff;
64}
65
66static inline u64 map__reloc(const struct map *map)
67{
68 return RC_CHK_ACCESS(map)->reloc;
69}
70
71static inline u32 map__flags(const struct map *map)
72{
73 return RC_CHK_ACCESS(map)->flags;
74}
75
76static inline u32 map__prot(const struct map *map)
77{
78 return RC_CHK_ACCESS(map)->prot;
79}
80
81static inline bool map__priv(const struct map *map)
82{
83 return RC_CHK_ACCESS(map)->priv;
84}
85
86static inline bool map__hit(const struct map *map)
87{
88 return RC_CHK_ACCESS(map)->hit;
89}
90
91static inline refcount_t *map__refcnt(struct map *map)
92{
93 return &RC_CHK_ACCESS(map)->refcnt;
94}
95
96static inline bool map__erange_warned(struct map *map)
97{
98 return RC_CHK_ACCESS(map)->erange_warned;
99}
100
101static inline size_t map__size(const struct map *map)
102{
103 return map__end(map) - map__start(map);
104}
105
106/* ip -> dso rip */
107static inline u64 map__dso_map_ip(const struct map *map, u64 ip)
108{
109 return ip - map__start(map) + map__pgoff(map);
110}
111
112/* dso rip -> ip */
113static inline u64 map__dso_unmap_ip(const struct map *map, u64 rip)
114{
115 return rip + map__start(map) - map__pgoff(map);
116}
117
118static inline u64 map__map_ip(const struct map *map, u64 ip_or_rip)
119{
120 if ((RC_CHK_ACCESS(map)->mapping_type) == MAPPING_TYPE__DSO)
121 return map__dso_map_ip(map, ip_or_rip);
122 else
123 return ip_or_rip;
124}
125
126static inline u64 map__unmap_ip(const struct map *map, u64 ip_or_rip)
127{
128 if ((RC_CHK_ACCESS(map)->mapping_type) == MAPPING_TYPE__DSO)
129 return map__dso_unmap_ip(map, ip_or_rip);
130 else
131 return ip_or_rip;
132}
133
134/* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
135u64 map__rip_2objdump(const struct map *map, u64 rip);
136
137/* objdump address -> memory address */
138u64 map__objdump_2mem(const struct map *map, u64 ip);
139
140/* objdump address -> rip */
141u64 map__objdump_2rip(const struct map *map, u64 ip);
142
143struct symbol;
144struct thread;
145
146/* map__for_each_symbol - iterate over the symbols in the given map
147 *
148 * @map: the 'struct map *' in which symbols are iterated
149 * @pos: the 'struct symbol *' to use as a loop cursor
150 * @n: the 'struct rb_node *' to use as a temporary storage
151 * Note: caller must ensure map->dso is not NULL (map is loaded).
152 */
153#define map__for_each_symbol(map, pos, n) \
154 dso__for_each_symbol(map__dso(map), pos, n)
155
156/* map__for_each_symbol_with_name - iterate over the symbols in the given map
157 * that have the given name
158 *
159 * @map: the 'struct map *' in which symbols are iterated
160 * @sym_name: the symbol name
161 * @pos: the 'struct symbol *' to use as a loop cursor
162 * @idx: the cursor index in the symbol names array
163 */
164#define __map__for_each_symbol_by_name(map, sym_name, pos, idx) \
165 for (pos = map__find_symbol_by_name_idx(map, sym_name, &idx); \
166 pos && \
167 !symbol__match_symbol_name(pos->name, sym_name, \
168 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
169 pos = dso__next_symbol_by_name(map__dso(map), &idx))
170
171#define map__for_each_symbol_by_name(map, sym_name, pos, idx) \
172 __map__for_each_symbol_by_name(map, sym_name, (pos), idx)
173
174struct dso_id;
175
176struct map *map__new(struct machine *machine, u64 start, u64 len,
177 u64 pgoff, const struct dso_id *id, u32 prot, u32 flags,
178 char *filename, struct thread *thread);
179struct map *map__new2(u64 start, struct dso *dso);
180void map__delete(struct map *map);
181struct map *map__clone(struct map *map);
182
183static inline struct map *map__get(struct map *map)
184{
185 struct map *result;
186
187 if (RC_CHK_GET(result, map))
188 refcount_inc(map__refcnt(map));
189
190 return result;
191}
192
193void map__put(struct map *map);
194
195static inline void __map__zput(struct map **map)
196{
197 map__put(*map);
198 *map = NULL;
199}
200
201#define map__zput(map) __map__zput(&map)
202
203size_t map__fprintf(struct map *map, FILE *fp);
204size_t map__fprintf_dsoname(struct map *map, FILE *fp);
205size_t map__fprintf_dsoname_dsoff(struct map *map, bool print_off, u64 addr, FILE *fp);
206char *map__srcline(struct map *map, u64 addr, struct symbol *sym);
207int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
208 FILE *fp);
209
210int map__load(struct map *map);
211struct symbol *map__find_symbol(struct map *map, u64 addr);
212struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
213struct symbol *map__find_symbol_by_name_idx(struct map *map, const char *name, size_t *idx);
214void map__fixup_start(struct map *map);
215void map__fixup_end(struct map *map);
216
217int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name,
218 u64 addr);
219
220bool __map__is_kernel(const struct map *map);
221bool __map__is_extra_kernel_map(const struct map *map);
222bool __map__is_bpf_prog(const struct map *map);
223bool __map__is_bpf_image(const struct map *map);
224bool __map__is_ool(const struct map *map);
225
226static inline bool __map__is_kmodule(const struct map *map)
227{
228 return !__map__is_kernel(map) && !__map__is_extra_kernel_map(map) &&
229 !__map__is_bpf_prog(map) && !__map__is_ool(map) &&
230 !__map__is_bpf_image(map);
231}
232
233bool map__has_symbols(const struct map *map);
234
235bool map__contains_symbol(const struct map *map, const struct symbol *sym);
236
237#define ENTRY_TRAMPOLINE_NAME "__entry_SYSCALL_64_trampoline"
238
239static inline bool is_entry_trampoline(const char *name)
240{
241 return !strcmp(name, ENTRY_TRAMPOLINE_NAME);
242}
243
244static inline bool is_bpf_image(const char *name)
245{
246 return strncmp(name, "bpf_trampoline_", sizeof("bpf_trampoline_") - 1) == 0 ||
247 strncmp(name, "bpf_dispatcher_", sizeof("bpf_dispatcher_") - 1) == 0;
248}
249
250static inline int is_anon_memory(const char *filename)
251{
252 return !strcmp(filename, "//anon") ||
253 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
254 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
255}
256
257static inline int is_no_dso_memory(const char *filename)
258{
259 return !strncmp(filename, "[stack", 6) ||
260 !strncmp(filename, "/SYSV", 5) ||
261 !strcmp(filename, "[heap]");
262}
263
264static inline void map__set_start(struct map *map, u64 start)
265{
266 RC_CHK_ACCESS(map)->start = start;
267}
268
269static inline void map__set_end(struct map *map, u64 end)
270{
271 RC_CHK_ACCESS(map)->end = end;
272}
273
274static inline void map__set_pgoff(struct map *map, u64 pgoff)
275{
276 RC_CHK_ACCESS(map)->pgoff = pgoff;
277}
278
279static inline void map__add_pgoff(struct map *map, u64 inc)
280{
281 RC_CHK_ACCESS(map)->pgoff += inc;
282}
283
284static inline void map__set_reloc(struct map *map, u64 reloc)
285{
286 RC_CHK_ACCESS(map)->reloc = reloc;
287}
288
289static inline void map__set_priv(struct map *map)
290{
291 RC_CHK_ACCESS(map)->priv = true;
292}
293
294static inline void map__set_hit(struct map *map)
295{
296 RC_CHK_ACCESS(map)->hit = true;
297}
298
299static inline void map__set_erange_warned(struct map *map)
300{
301 RC_CHK_ACCESS(map)->erange_warned = true;
302}
303
304static inline void map__set_dso(struct map *map, struct dso *dso)
305{
306 RC_CHK_ACCESS(map)->dso = dso;
307}
308
309static inline void map__set_mapping_type(struct map *map, enum mapping_type type)
310{
311 RC_CHK_ACCESS(map)->mapping_type = type;
312}
313
314static inline enum mapping_type map__mapping_type(struct map *map)
315{
316 return RC_CHK_ACCESS(map)->mapping_type;
317}
318#endif /* __PERF_MAP_H */