map.h 7.3 KB

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