map.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "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. u64 pgoff;
  33. u64 reloc;
  34. u32 maj, min; /* only valid for MMAP2 record */
  35. u64 ino; /* only valid for MMAP2 record */
  36. u64 ino_generation;/* only valid for MMAP2 record */
  37. /* ip -> dso rip */
  38. u64 (*map_ip)(struct map *, u64);
  39. /* dso rip -> ip */
  40. u64 (*unmap_ip)(struct map *, u64);
  41. struct dso *dso;
  42. struct map_groups *groups;
  43. };
  44. struct kmap {
  45. struct ref_reloc_sym *ref_reloc_sym;
  46. struct map_groups *kmaps;
  47. };
  48. struct map_groups {
  49. struct rb_root maps[MAP__NR_TYPES];
  50. struct list_head removed_maps[MAP__NR_TYPES];
  51. struct machine *machine;
  52. };
  53. static inline struct kmap *map__kmap(struct map *map)
  54. {
  55. return (struct kmap *)(map + 1);
  56. }
  57. static inline u64 map__map_ip(struct map *map, u64 ip)
  58. {
  59. return ip - map->start + map->pgoff;
  60. }
  61. static inline u64 map__unmap_ip(struct map *map, u64 ip)
  62. {
  63. return ip + map->start - map->pgoff;
  64. }
  65. static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
  66. {
  67. return ip;
  68. }
  69. /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
  70. u64 map__rip_2objdump(struct map *map, u64 rip);
  71. /* objdump address -> memory address */
  72. u64 map__objdump_2mem(struct map *map, u64 ip);
  73. struct symbol;
  74. /* map__for_each_symbol - iterate over the symbols in the given map
  75. *
  76. * @map: the 'struct map *' in which symbols itereated
  77. * @pos: the 'struct symbol *' to use as a loop cursor
  78. * @n: the 'struct rb_node *' to use as a temporary storage
  79. * Note: caller must ensure map->dso is not NULL (map is loaded).
  80. */
  81. #define map__for_each_symbol(map, pos, n) \
  82. dso__for_each_symbol(map->dso, pos, n, map->type)
  83. typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
  84. void map__init(struct map *map, enum map_type type,
  85. u64 start, u64 end, u64 pgoff, struct dso *dso);
  86. struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
  87. u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
  88. u64 ino_gen,
  89. char *filename, enum map_type type);
  90. struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
  91. void map__delete(struct map *map);
  92. struct map *map__clone(struct map *map);
  93. int map__overlap(struct map *l, struct map *r);
  94. size_t map__fprintf(struct map *map, FILE *fp);
  95. size_t map__fprintf_dsoname(struct map *map, FILE *fp);
  96. int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
  97. FILE *fp);
  98. int map__load(struct map *map, symbol_filter_t filter);
  99. struct symbol *map__find_symbol(struct map *map,
  100. u64 addr, symbol_filter_t filter);
  101. struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
  102. symbol_filter_t filter);
  103. void map__fixup_start(struct map *map);
  104. void map__fixup_end(struct map *map);
  105. void map__reloc_vmlinux(struct map *map);
  106. size_t __map_groups__fprintf_maps(struct map_groups *mg,
  107. enum map_type type, int verbose, FILE *fp);
  108. void maps__insert(struct rb_root *maps, struct map *map);
  109. void maps__remove(struct rb_root *maps, struct map *map);
  110. struct map *maps__find(struct rb_root *maps, u64 addr);
  111. struct map *maps__first(struct rb_root *maps);
  112. struct map *maps__next(struct map *map);
  113. void map_groups__init(struct map_groups *mg);
  114. void map_groups__exit(struct map_groups *mg);
  115. int map_groups__clone(struct map_groups *mg,
  116. struct map_groups *parent, enum map_type type);
  117. size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp);
  118. size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp);
  119. int maps__set_kallsyms_ref_reloc_sym(struct map **maps, const char *symbol_name,
  120. u64 addr);
  121. static inline void map_groups__insert(struct map_groups *mg, struct map *map)
  122. {
  123. maps__insert(&mg->maps[map->type], map);
  124. map->groups = mg;
  125. }
  126. static inline void map_groups__remove(struct map_groups *mg, struct map *map)
  127. {
  128. maps__remove(&mg->maps[map->type], map);
  129. }
  130. static inline struct map *map_groups__find(struct map_groups *mg,
  131. enum map_type type, u64 addr)
  132. {
  133. return maps__find(&mg->maps[type], addr);
  134. }
  135. static inline struct map *map_groups__first(struct map_groups *mg,
  136. enum map_type type)
  137. {
  138. return maps__first(&mg->maps[type]);
  139. }
  140. static inline struct map *map_groups__next(struct map *map)
  141. {
  142. return maps__next(map);
  143. }
  144. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  145. enum map_type type, u64 addr,
  146. struct map **mapp,
  147. symbol_filter_t filter);
  148. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  149. enum map_type type,
  150. const char *name,
  151. struct map **mapp,
  152. symbol_filter_t filter);
  153. struct addr_map_symbol;
  154. int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter);
  155. static inline
  156. struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
  157. const char *name, struct map **mapp,
  158. symbol_filter_t filter)
  159. {
  160. return map_groups__find_symbol_by_name(mg, MAP__FUNCTION, name, mapp, filter);
  161. }
  162. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  163. int verbose, FILE *fp);
  164. struct map *map_groups__find_by_name(struct map_groups *mg,
  165. enum map_type type, const char *name);
  166. void map_groups__flush(struct map_groups *mg);
  167. #endif /* __PERF_MAP_H */