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. void machines__set_id_hdr_size(struct rb_root *self, u16 id_hdr_size);
  128. char *machine__mmap_name(struct machine *self, char *bf, size_t size);
  129. int machine__init(struct machine *self, const char *root_dir, pid_t pid);
  130. void machine__exit(struct machine *self);
  131. void machine__delete(struct machine *self);
  132. int machine__resolve_callchain(struct machine *machine,
  133. struct thread *thread,
  134. struct ip_callchain *chain,
  135. struct symbol **parent);
  136. int maps__set_kallsyms_ref_reloc_sym(struct map **maps, const char *symbol_name,
  137. u64 addr);
  138. /*
  139. * Default guest kernel is defined by parameter --guestkallsyms
  140. * and --guestmodules
  141. */
  142. static inline bool machine__is_default_guest(struct machine *self)
  143. {
  144. return self ? self->pid == DEFAULT_GUEST_KERNEL_ID : false;
  145. }
  146. static inline bool machine__is_host(struct machine *self)
  147. {
  148. return self ? self->pid == HOST_KERNEL_ID : false;
  149. }
  150. static inline void map_groups__insert(struct map_groups *mg, struct map *map)
  151. {
  152. maps__insert(&mg->maps[map->type], map);
  153. map->groups = mg;
  154. }
  155. static inline void map_groups__remove(struct map_groups *mg, struct map *map)
  156. {
  157. maps__remove(&mg->maps[map->type], map);
  158. }
  159. static inline struct map *map_groups__find(struct map_groups *mg,
  160. enum map_type type, u64 addr)
  161. {
  162. return maps__find(&mg->maps[type], addr);
  163. }
  164. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  165. enum map_type type, u64 addr,
  166. struct map **mapp,
  167. symbol_filter_t filter);
  168. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  169. enum map_type type,
  170. const char *name,
  171. struct map **mapp,
  172. symbol_filter_t filter);
  173. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid);
  174. void machine__remove_thread(struct machine *machine, struct thread *th);
  175. size_t machine__fprintf(struct machine *machine, FILE *fp);
  176. static inline
  177. struct symbol *machine__find_kernel_symbol(struct machine *self,
  178. enum map_type type, u64 addr,
  179. struct map **mapp,
  180. symbol_filter_t filter)
  181. {
  182. return map_groups__find_symbol(&self->kmaps, type, addr, mapp, filter);
  183. }
  184. static inline
  185. struct symbol *machine__find_kernel_function(struct machine *self, u64 addr,
  186. struct map **mapp,
  187. symbol_filter_t filter)
  188. {
  189. return machine__find_kernel_symbol(self, MAP__FUNCTION, addr, mapp, filter);
  190. }
  191. static inline
  192. struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
  193. const char *name, struct map **mapp,
  194. symbol_filter_t filter)
  195. {
  196. return map_groups__find_symbol_by_name(mg, MAP__FUNCTION, name, mapp, filter);
  197. }
  198. static inline
  199. struct symbol *machine__find_kernel_function_by_name(struct machine *self,
  200. const char *name,
  201. struct map **mapp,
  202. symbol_filter_t filter)
  203. {
  204. return map_groups__find_function_by_name(&self->kmaps, name, mapp,
  205. filter);
  206. }
  207. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  208. int verbose, FILE *fp);
  209. struct map *map_groups__find_by_name(struct map_groups *mg,
  210. enum map_type type, const char *name);
  211. struct map *machine__new_module(struct machine *self, u64 start, const char *filename);
  212. void map_groups__flush(struct map_groups *mg);
  213. #endif /* __PERF_MAP_H */