map.h 6.0 KB

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