map.h 6.6 KB

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