map.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. u32 priv;
  31. u64 pgoff;
  32. /* ip -> dso rip */
  33. u64 (*map_ip)(struct map *, u64);
  34. /* dso rip -> ip */
  35. u64 (*unmap_ip)(struct map *, u64);
  36. struct dso *dso;
  37. struct map_groups *groups;
  38. };
  39. struct kmap {
  40. struct ref_reloc_sym *ref_reloc_sym;
  41. struct map_groups *kmaps;
  42. };
  43. struct map_groups {
  44. struct rb_root maps[MAP__NR_TYPES];
  45. struct list_head removed_maps[MAP__NR_TYPES];
  46. struct machine *machine;
  47. };
  48. /* Native host kernel uses -1 as pid index in machine */
  49. #define HOST_KERNEL_ID (-1)
  50. #define DEFAULT_GUEST_KERNEL_ID (0)
  51. struct machine {
  52. struct rb_node rb_node;
  53. pid_t pid;
  54. u16 id_hdr_size;
  55. char *root_dir;
  56. struct rb_root threads;
  57. struct list_head dead_threads;
  58. struct thread *last_match;
  59. struct list_head user_dsos;
  60. struct list_head kernel_dsos;
  61. struct map_groups kmaps;
  62. struct map *vmlinux_maps[MAP__NR_TYPES];
  63. };
  64. static inline
  65. struct map *machine__kernel_map(struct machine *self, enum map_type type)
  66. {
  67. return self->vmlinux_maps[type];
  68. }
  69. static inline struct kmap *map__kmap(struct map *self)
  70. {
  71. return (struct kmap *)(self + 1);
  72. }
  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 __used, 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. u64 map__objdump_2ip(struct map *map, u64 addr);
  88. struct symbol;
  89. typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
  90. void map__init(struct map *self, enum map_type type,
  91. u64 start, u64 end, u64 pgoff, struct dso *dso);
  92. struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
  93. u64 pgoff, u32 pid, char *filename,
  94. enum map_type type);
  95. void map__delete(struct map *self);
  96. struct map *map__clone(struct map *self);
  97. int map__overlap(struct map *l, struct map *r);
  98. size_t map__fprintf(struct map *self, FILE *fp);
  99. size_t map__fprintf_dsoname(struct map *map, FILE *fp);
  100. int map__load(struct map *self, symbol_filter_t filter);
  101. struct symbol *map__find_symbol(struct map *self,
  102. u64 addr, symbol_filter_t filter);
  103. struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
  104. symbol_filter_t filter);
  105. void map__fixup_start(struct map *self);
  106. void map__fixup_end(struct map *self);
  107. void map__reloc_vmlinux(struct map *self);
  108. size_t __map_groups__fprintf_maps(struct map_groups *mg,
  109. enum map_type type, int verbose, FILE *fp);
  110. void maps__insert(struct rb_root *maps, struct map *map);
  111. void maps__remove(struct rb_root *maps, struct map *map);
  112. struct map *maps__find(struct rb_root *maps, u64 addr);
  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. typedef void (*machine__process_t)(struct machine *self, void *data);
  120. void machines__process(struct rb_root *self, machine__process_t process, void *data);
  121. struct machine *machines__add(struct rb_root *self, pid_t pid,
  122. const char *root_dir);
  123. struct machine *machines__find_host(struct rb_root *self);
  124. struct machine *machines__find(struct rb_root *self, pid_t pid);
  125. struct machine *machines__findnew(struct rb_root *self, pid_t pid);
  126. char *machine__mmap_name(struct machine *self, char *bf, size_t size);
  127. int machine__init(struct machine *self, const char *root_dir, pid_t pid);
  128. void machine__exit(struct machine *self);
  129. void machine__delete(struct machine *self);
  130. int machine__resolve_callchain(struct machine *machine,
  131. struct perf_evsel *evsel, struct thread *thread,
  132. struct ip_callchain *chain,
  133. struct symbol **parent);
  134. int maps__set_kallsyms_ref_reloc_sym(struct map **maps, const char *symbol_name,
  135. u64 addr);
  136. /*
  137. * Default guest kernel is defined by parameter --guestkallsyms
  138. * and --guestmodules
  139. */
  140. static inline bool machine__is_default_guest(struct machine *self)
  141. {
  142. return self ? self->pid == DEFAULT_GUEST_KERNEL_ID : false;
  143. }
  144. static inline bool machine__is_host(struct machine *self)
  145. {
  146. return self ? self->pid == HOST_KERNEL_ID : false;
  147. }
  148. static inline void map_groups__insert(struct map_groups *mg, struct map *map)
  149. {
  150. maps__insert(&mg->maps[map->type], map);
  151. map->groups = mg;
  152. }
  153. static inline void map_groups__remove(struct map_groups *mg, struct map *map)
  154. {
  155. maps__remove(&mg->maps[map->type], map);
  156. }
  157. static inline struct map *map_groups__find(struct map_groups *mg,
  158. enum map_type type, u64 addr)
  159. {
  160. return maps__find(&mg->maps[type], addr);
  161. }
  162. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  163. enum map_type type, u64 addr,
  164. struct map **mapp,
  165. symbol_filter_t filter);
  166. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  167. enum map_type type,
  168. const char *name,
  169. struct map **mapp,
  170. symbol_filter_t filter);
  171. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid);
  172. void machine__remove_thread(struct machine *machine, struct thread *th);
  173. size_t machine__fprintf(struct machine *machine, FILE *fp);
  174. static inline
  175. struct symbol *machine__find_kernel_symbol(struct machine *self,
  176. enum map_type type, u64 addr,
  177. struct map **mapp,
  178. symbol_filter_t filter)
  179. {
  180. return map_groups__find_symbol(&self->kmaps, type, addr, mapp, filter);
  181. }
  182. static inline
  183. struct symbol *machine__find_kernel_function(struct machine *self, u64 addr,
  184. struct map **mapp,
  185. symbol_filter_t filter)
  186. {
  187. return machine__find_kernel_symbol(self, MAP__FUNCTION, addr, mapp, filter);
  188. }
  189. static inline
  190. struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
  191. const char *name, struct map **mapp,
  192. symbol_filter_t filter)
  193. {
  194. return map_groups__find_symbol_by_name(mg, MAP__FUNCTION, name, mapp, filter);
  195. }
  196. static inline
  197. struct symbol *machine__find_kernel_function_by_name(struct machine *self,
  198. const char *name,
  199. struct map **mapp,
  200. symbol_filter_t filter)
  201. {
  202. return map_groups__find_function_by_name(&self->kmaps, name, mapp,
  203. filter);
  204. }
  205. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  206. int verbose, FILE *fp);
  207. struct map *map_groups__find_by_name(struct map_groups *mg,
  208. enum map_type type, const char *name);
  209. struct map *machine__new_module(struct machine *self, u64 start, const char *filename);
  210. void map_groups__flush(struct map_groups *mg);
  211. #endif /* __PERF_MAP_H */