map.h 7.3 KB

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