map.h 8.0 KB

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