map.h 5.6 KB

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