]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - tools/perf/util/map.h
perf symbols: Unify symbol maps
[mirror_ubuntu-jammy-kernel.git] / tools / perf / util / map.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_MAP_H
3 #define __PERF_MAP_H
4
5 #include <linux/refcount.h>
6 #include <linux/compiler.h>
7 #include <linux/list.h>
8 #include <linux/rbtree.h>
9 #include <pthread.h>
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include <linux/types.h>
13 #include "rwsem.h"
14
15 struct dso;
16 struct ip_callchain;
17 struct ref_reloc_sym;
18 struct map_groups;
19 struct machine;
20 struct perf_evsel;
21
22 struct map {
23 union {
24 struct rb_node rb_node;
25 struct list_head node;
26 };
27 u64 start;
28 u64 end;
29 bool erange_warned;
30 u32 priv;
31 u32 prot;
32 u32 flags;
33 u64 pgoff;
34 u64 reloc;
35 u32 maj, min; /* only valid for MMAP2 record */
36 u64 ino; /* only valid for MMAP2 record */
37 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 struct kmap {
50 struct ref_reloc_sym *ref_reloc_sym;
51 struct map_groups *kmaps;
52 };
53
54 struct maps {
55 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 static inline struct map_groups *map_groups__get(struct map_groups *mg)
70 {
71 if (mg)
72 refcount_inc(&mg->refcnt);
73 return mg;
74 }
75
76 void map_groups__put(struct map_groups *mg);
77
78 struct kmap *map__kmap(struct map *map);
79 struct map_groups *map__kmaps(struct map *map);
80
81 static inline u64 map__map_ip(struct map *map, u64 ip)
82 {
83 return ip - map->start + map->pgoff;
84 }
85
86 static inline u64 map__unmap_ip(struct map *map, u64 ip)
87 {
88 return ip + map->start - map->pgoff;
89 }
90
91 static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
92 {
93 return ip;
94 }
95
96 static inline size_t map__size(const struct map *map)
97 {
98 return map->end - map->start;
99 }
100
101 /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
102 u64 map__rip_2objdump(struct map *map, u64 rip);
103
104 /* objdump address -> memory address */
105 u64 map__objdump_2mem(struct map *map, u64 ip);
106
107 struct symbol;
108 struct thread;
109
110 /* map__for_each_symbol - iterate over the symbols in the given map
111 *
112 * @map: the 'struct map *' in which symbols itereated
113 * @pos: the 'struct symbol *' to use as a loop cursor
114 * @n: the 'struct rb_node *' to use as a temporary storage
115 * Note: caller must ensure map->dso is not NULL (map is loaded).
116 */
117 #define map__for_each_symbol(map, pos, n) \
118 dso__for_each_symbol(map->dso, pos, n)
119
120 /* map__for_each_symbol_with_name - iterate over the symbols in the given map
121 * that have the given name
122 *
123 * @map: the 'struct map *' in which symbols itereated
124 * @sym_name: the symbol name
125 * @pos: the 'struct symbol *' to use as a loop cursor
126 */
127 #define __map__for_each_symbol_by_name(map, sym_name, pos) \
128 for (pos = map__find_symbol_by_name(map, sym_name); \
129 pos && \
130 !symbol__match_symbol_name(pos->name, sym_name, \
131 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
132 pos = symbol__next_by_name(pos))
133
134 #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 char *filename, struct thread *thread);
143 struct map *map__new2(u64 start, struct dso *dso);
144 void map__delete(struct map *map);
145 struct map *map__clone(struct map *map);
146
147 static inline struct map *map__get(struct map *map)
148 {
149 if (map)
150 refcount_inc(&map->refcnt);
151 return map;
152 }
153
154 void map__put(struct map *map);
155
156 static inline void __map__zput(struct map **map)
157 {
158 map__put(*map);
159 *map = NULL;
160 }
161
162 #define map__zput(map) __map__zput(&map)
163
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 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
168 FILE *fp);
169
170 int map__load(struct map *map);
171 struct symbol *map__find_symbol(struct map *map, u64 addr);
172 struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
173 void map__fixup_start(struct map *map);
174 void map__fixup_end(struct map *map);
175
176 void map__reloc_vmlinux(struct map *map);
177
178 void maps__insert(struct maps *maps, struct map *map);
179 void maps__remove(struct maps *maps, struct map *map);
180 struct map *maps__find(struct maps *maps, u64 addr);
181 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 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name,
192 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 static inline struct map *map_groups__find(struct map_groups *mg, u64 addr)
206 {
207 return maps__find(&mg->maps, addr);
208 }
209
210 struct map *map_groups__first(struct map_groups *mg);
211
212 static inline struct map *map_groups__next(struct map *map)
213 {
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 struct addr_map_symbol;
225
226 int map_groups__find_ams(struct addr_map_symbol *ams);
227
228 static inline
229 struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
230 const char *name, struct map **mapp)
231 {
232 return map_groups__find_symbol_by_name(mg, name, mapp);
233 }
234
235 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
236 FILE *fp);
237
238 struct map *map_groups__find_by_name(struct map_groups *mg, const char *name);
239
240 bool __map__is_kernel(const struct map *map);
241
242 static inline bool __map__is_kmodule(const struct map *map)
243 {
244 return !__map__is_kernel(map);
245 }
246
247 bool map__has_symbols(const struct map *map);
248
249 #endif /* __PERF_MAP_H */