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