^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __PERF_MAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __PERF_MAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct maps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct machine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct rb_node rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool erange_warned:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) bool priv:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u64 pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u64 reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* ip -> dso rip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u64 (*map_ip)(struct map *, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* dso rip -> ip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u64 (*unmap_ip)(struct map *, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct dso *dso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) refcount_t refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct kmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct kmap *__map__kmap(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct kmap *map__kmap(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct maps *map__kmaps(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static inline u64 map__map_ip(struct map *map, u64 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return ip - map->start + map->pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline u64 map__unmap_ip(struct map *map, u64 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return ip + map->start - map->pgoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static inline size_t map__size(const struct map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return map->end - map->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u64 map__rip_2objdump(struct map *map, u64 rip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* objdump address -> memory address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u64 map__objdump_2mem(struct map *map, u64 ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct symbol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* map__for_each_symbol - iterate over the symbols in the given map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @map: the 'struct map *' in which symbols itereated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @pos: the 'struct symbol *' to use as a loop cursor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @n: the 'struct rb_node *' to use as a temporary storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Note: caller must ensure map->dso is not NULL (map is loaded).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define map__for_each_symbol(map, pos, n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dso__for_each_symbol(map->dso, pos, n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* map__for_each_symbol_with_name - iterate over the symbols in the given map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * that have the given name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @map: the 'struct map *' in which symbols itereated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * @sym_name: the symbol name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * @pos: the 'struct symbol *' to use as a loop cursor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define __map__for_each_symbol_by_name(map, sym_name, pos) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for (pos = map__find_symbol_by_name(map, sym_name); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) pos && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) !symbol__match_symbol_name(pos->name, sym_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pos = symbol__next_by_name(pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define map__for_each_symbol_by_name(map, sym_name, pos) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __map__for_each_symbol_by_name(map, sym_name, (pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void map__init(struct map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u64 start, u64 end, u64 pgoff, struct dso *dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct dso_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct map *map__new(struct machine *machine, u64 start, u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u64 pgoff, struct dso_id *id, u32 prot, u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) char *filename, struct thread *thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct map *map__new2(u64 start, struct dso *dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) void map__delete(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct map *map__clone(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline struct map *map__get(struct map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) refcount_inc(&map->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void map__put(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static inline void __map__zput(struct map **map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) map__put(*map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define map__zput(map) __map__zput(&map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) size_t map__fprintf(struct map *map, FILE *fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) size_t map__fprintf_dsoname(struct map *map, FILE *fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) char *map__srcline(struct map *map, u64 addr, struct symbol *sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) FILE *fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int map__load(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct symbol *map__find_symbol(struct map *map, u64 addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void map__fixup_start(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) void map__fixup_end(struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u64 addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) bool __map__is_kernel(const struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) bool __map__is_extra_kernel_map(const struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) bool __map__is_bpf_prog(const struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) bool __map__is_bpf_image(const struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) bool __map__is_ool(const struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static inline bool __map__is_kmodule(const struct map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return !__map__is_kernel(map) && !__map__is_extra_kernel_map(map) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) !__map__is_bpf_prog(map) && !__map__is_ool(map) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) !__map__is_bpf_image(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) bool map__has_symbols(const struct map *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define ENTRY_TRAMPOLINE_NAME "__entry_SYSCALL_64_trampoline"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static inline bool is_entry_trampoline(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return !strcmp(name, ENTRY_TRAMPOLINE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static inline bool is_bpf_image(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return strncmp(name, "bpf_trampoline_", sizeof("bpf_trampoline_") - 1) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) strncmp(name, "bpf_dispatcher_", sizeof("bpf_dispatcher_") - 1) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static inline int is_anon_memory(const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return !strcmp(filename, "//anon") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static inline int is_no_dso_memory(const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return !strncmp(filename, "[stack", 6) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) !strncmp(filename, "/SYSV", 5) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) !strcmp(filename, "[heap]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #endif /* __PERF_MAP_H */