map.h 7.1 KB

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