map.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <pthread.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include <linux/types.h>
  13. #include "rwsem.h"
  14. struct dso;
  15. struct ip_callchain;
  16. struct ref_reloc_sym;
  17. struct map_groups;
  18. struct machine;
  19. struct perf_evsel;
  20. struct map {
  21. union {
  22. struct rb_node rb_node;
  23. struct list_head node;
  24. };
  25. u64 start;
  26. u64 end;
  27. bool erange_warned;
  28. u32 priv;
  29. u32 prot;
  30. u32 flags;
  31. u64 pgoff;
  32. u64 reloc;
  33. u32 maj, min; /* only valid for MMAP2 record */
  34. u64 ino; /* only valid for MMAP2 record */
  35. u64 ino_generation;/* only valid for MMAP2 record */
  36. /* ip -> dso rip */
  37. u64 (*map_ip)(struct map *, u64);
  38. /* dso rip -> ip */
  39. u64 (*unmap_ip)(struct map *, u64);
  40. struct dso *dso;
  41. struct map_groups *groups;
  42. refcount_t refcnt;
  43. };
  44. #define KMAP_NAME_LEN 256
  45. struct kmap {
  46. struct ref_reloc_sym *ref_reloc_sym;
  47. struct map_groups *kmaps;
  48. char name[KMAP_NAME_LEN];
  49. };
  50. struct maps {
  51. struct rb_root entries;
  52. struct rw_semaphore lock;
  53. };
  54. struct map_groups {
  55. struct maps maps;
  56. struct machine *machine;
  57. refcount_t refcnt;
  58. };
  59. struct map_groups *map_groups__new(struct machine *machine);
  60. void map_groups__delete(struct map_groups *mg);
  61. bool map_groups__empty(struct map_groups *mg);
  62. static inline struct map_groups *map_groups__get(struct map_groups *mg)
  63. {
  64. if (mg)
  65. refcount_inc(&mg->refcnt);
  66. return mg;
  67. }
  68. void map_groups__put(struct map_groups *mg);
  69. struct kmap *__map__kmap(struct map *map);
  70. struct kmap *map__kmap(struct map *map);
  71. struct map_groups *map__kmaps(struct map *map);
  72. static inline u64 map__map_ip(struct map *map, u64 ip)
  73. {
  74. return ip - map->start + map->pgoff;
  75. }
  76. static inline u64 map__unmap_ip(struct map *map, u64 ip)
  77. {
  78. return ip + map->start - map->pgoff;
  79. }
  80. static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
  81. {
  82. return ip;
  83. }
  84. static inline size_t map__size(const struct map *map)
  85. {
  86. return map->end - map->start;
  87. }
  88. /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
  89. u64 map__rip_2objdump(struct map *map, u64 rip);
  90. /* objdump address -> memory address */
  91. u64 map__objdump_2mem(struct map *map, u64 ip);
  92. struct symbol;
  93. struct thread;
  94. /* map__for_each_symbol - iterate over the symbols in the given map
  95. *
  96. * @map: the 'struct map *' in which symbols itereated
  97. * @pos: the 'struct symbol *' to use as a loop cursor
  98. * @n: the 'struct rb_node *' to use as a temporary storage
  99. * Note: caller must ensure map->dso is not NULL (map is loaded).
  100. */
  101. #define map__for_each_symbol(map, pos, n) \
  102. dso__for_each_symbol(map->dso, pos, n)
  103. /* map__for_each_symbol_with_name - iterate over the symbols in the given map
  104. * that have the given name
  105. *
  106. * @map: the 'struct map *' in which symbols itereated
  107. * @sym_name: the symbol name
  108. * @pos: the 'struct symbol *' to use as a loop cursor
  109. */
  110. #define __map__for_each_symbol_by_name(map, sym_name, pos) \
  111. for (pos = map__find_symbol_by_name(map, sym_name); \
  112. pos && \
  113. !symbol__match_symbol_name(pos->name, sym_name, \
  114. SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
  115. pos = symbol__next_by_name(pos))
  116. #define map__for_each_symbol_by_name(map, sym_name, pos) \
  117. __map__for_each_symbol_by_name(map, sym_name, (pos))
  118. void map__init(struct map *map,
  119. u64 start, u64 end, u64 pgoff, struct dso *dso);
  120. struct map *map__new(struct machine *machine, u64 start, u64 len,
  121. u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
  122. u64 ino_gen, u32 prot, u32 flags,
  123. char *filename, struct thread *thread);
  124. struct map *map__new2(u64 start, struct dso *dso);
  125. void map__delete(struct map *map);
  126. struct map *map__clone(struct map *map);
  127. static inline struct map *map__get(struct map *map)
  128. {
  129. if (map)
  130. refcount_inc(&map->refcnt);
  131. return map;
  132. }
  133. void map__put(struct map *map);
  134. static inline void __map__zput(struct map **map)
  135. {
  136. map__put(*map);
  137. *map = NULL;
  138. }
  139. #define map__zput(map) __map__zput(&map)
  140. size_t map__fprintf(struct map *map, FILE *fp);
  141. size_t map__fprintf_dsoname(struct map *map, FILE *fp);
  142. char *map__srcline(struct map *map, u64 addr, struct symbol *sym);
  143. int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
  144. FILE *fp);
  145. int map__load(struct map *map);
  146. struct symbol *map__find_symbol(struct map *map, u64 addr);
  147. struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
  148. void map__fixup_start(struct map *map);
  149. void map__fixup_end(struct map *map);
  150. void map__reloc_vmlinux(struct map *map);
  151. void maps__insert(struct maps *maps, struct map *map);
  152. void maps__remove(struct maps *maps, struct map *map);
  153. struct map *maps__find(struct maps *maps, u64 addr);
  154. struct map *maps__first(struct maps *maps);
  155. struct map *map__next(struct map *map);
  156. struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
  157. struct map **mapp);
  158. void map_groups__init(struct map_groups *mg, struct machine *machine);
  159. void map_groups__exit(struct map_groups *mg);
  160. int map_groups__clone(struct thread *thread,
  161. struct map_groups *parent);
  162. size_t map_groups__fprintf(struct map_groups *mg, FILE *fp);
  163. int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name,
  164. u64 addr);
  165. static inline void map_groups__insert(struct map_groups *mg, struct map *map)
  166. {
  167. maps__insert(&mg->maps, map);
  168. map->groups = mg;
  169. }
  170. static inline void map_groups__remove(struct map_groups *mg, struct map *map)
  171. {
  172. maps__remove(&mg->maps, map);
  173. }
  174. static inline struct map *map_groups__find(struct map_groups *mg, u64 addr)
  175. {
  176. return maps__find(&mg->maps, addr);
  177. }
  178. struct map *map_groups__first(struct map_groups *mg);
  179. static inline struct map *map_groups__next(struct map *map)
  180. {
  181. return map__next(map);
  182. }
  183. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  184. u64 addr, struct map **mapp);
  185. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  186. const char *name,
  187. struct map **mapp);
  188. struct addr_map_symbol;
  189. int map_groups__find_ams(struct addr_map_symbol *ams);
  190. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  191. FILE *fp);
  192. struct map *map_groups__find_by_name(struct map_groups *mg, const char *name);
  193. bool __map__is_kernel(const struct map *map);
  194. bool __map__is_extra_kernel_map(const struct map *map);
  195. static inline bool __map__is_kmodule(const struct map *map)
  196. {
  197. return !__map__is_kernel(map) && !__map__is_extra_kernel_map(map);
  198. }
  199. bool map__has_symbols(const struct map *map);
  200. #define ENTRY_TRAMPOLINE_NAME "__entry_SYSCALL_64_trampoline"
  201. static inline bool is_entry_trampoline(const char *name)
  202. {
  203. return !strcmp(name, ENTRY_TRAMPOLINE_NAME);
  204. }
  205. #endif /* __PERF_MAP_H */