map.h 6.7 KB

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