machine.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef __PERF_MACHINE_H
  2. #define __PERF_MACHINE_H
  3. #include <sys/types.h>
  4. #include <linux/rbtree.h>
  5. #include "map.h"
  6. #include "dso.h"
  7. #include "event.h"
  8. struct addr_location;
  9. struct branch_stack;
  10. struct perf_evsel;
  11. struct perf_sample;
  12. struct symbol;
  13. struct thread;
  14. union perf_event;
  15. /* Native host kernel uses -1 as pid index in machine */
  16. #define HOST_KERNEL_ID (-1)
  17. #define DEFAULT_GUEST_KERNEL_ID (0)
  18. extern const char *ref_reloc_sym_names[];
  19. struct vdso_info;
  20. struct machine {
  21. struct rb_node rb_node;
  22. pid_t pid;
  23. u16 id_hdr_size;
  24. bool comm_exec;
  25. bool kptr_restrict_warned;
  26. char *root_dir;
  27. struct rb_root threads;
  28. pthread_rwlock_t threads_lock;
  29. unsigned int nr_threads;
  30. struct list_head dead_threads;
  31. struct thread *last_match;
  32. struct vdso_info *vdso_info;
  33. struct perf_env *env;
  34. struct dsos dsos;
  35. struct map_groups kmaps;
  36. struct map *vmlinux_maps[MAP__NR_TYPES];
  37. u64 kernel_start;
  38. pid_t *current_tid;
  39. union { /* Tool specific area */
  40. void *priv;
  41. u64 db_id;
  42. };
  43. };
  44. static inline
  45. struct map *__machine__kernel_map(struct machine *machine, enum map_type type)
  46. {
  47. return machine->vmlinux_maps[type];
  48. }
  49. static inline
  50. struct map *machine__kernel_map(struct machine *machine)
  51. {
  52. return __machine__kernel_map(machine, MAP__FUNCTION);
  53. }
  54. int machine__get_kernel_start(struct machine *machine);
  55. static inline u64 machine__kernel_start(struct machine *machine)
  56. {
  57. if (!machine->kernel_start)
  58. machine__get_kernel_start(machine);
  59. return machine->kernel_start;
  60. }
  61. static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
  62. {
  63. u64 kernel_start = machine__kernel_start(machine);
  64. return ip >= kernel_start;
  65. }
  66. struct thread *machine__find_thread(struct machine *machine, pid_t pid,
  67. pid_t tid);
  68. struct comm *machine__thread_exec_comm(struct machine *machine,
  69. struct thread *thread);
  70. int machine__process_comm_event(struct machine *machine, union perf_event *event,
  71. struct perf_sample *sample);
  72. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  73. struct perf_sample *sample);
  74. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  75. struct perf_sample *sample);
  76. int machine__process_lost_event(struct machine *machine, union perf_event *event,
  77. struct perf_sample *sample);
  78. int machine__process_lost_samples_event(struct machine *machine, union perf_event *event,
  79. struct perf_sample *sample);
  80. int machine__process_aux_event(struct machine *machine,
  81. union perf_event *event);
  82. int machine__process_itrace_start_event(struct machine *machine,
  83. union perf_event *event);
  84. int machine__process_switch_event(struct machine *machine,
  85. union perf_event *event);
  86. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  87. struct perf_sample *sample);
  88. int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
  89. struct perf_sample *sample);
  90. int machine__process_event(struct machine *machine, union perf_event *event,
  91. struct perf_sample *sample);
  92. typedef void (*machine__process_t)(struct machine *machine, void *data);
  93. struct machines {
  94. struct machine host;
  95. struct rb_root guests;
  96. };
  97. void machines__init(struct machines *machines);
  98. void machines__exit(struct machines *machines);
  99. void machines__process_guests(struct machines *machines,
  100. machine__process_t process, void *data);
  101. struct machine *machines__add(struct machines *machines, pid_t pid,
  102. const char *root_dir);
  103. struct machine *machines__find_host(struct machines *machines);
  104. struct machine *machines__find(struct machines *machines, pid_t pid);
  105. struct machine *machines__findnew(struct machines *machines, pid_t pid);
  106. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
  107. char *machine__mmap_name(struct machine *machine, char *bf, size_t size);
  108. void machines__set_comm_exec(struct machines *machines, bool comm_exec);
  109. struct machine *machine__new_host(void);
  110. int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
  111. void machine__exit(struct machine *machine);
  112. void machine__delete_threads(struct machine *machine);
  113. void machine__delete(struct machine *machine);
  114. void machine__remove_thread(struct machine *machine, struct thread *th);
  115. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  116. struct addr_location *al);
  117. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  118. struct addr_location *al);
  119. struct callchain_cursor;
  120. int thread__resolve_callchain(struct thread *thread,
  121. struct callchain_cursor *cursor,
  122. struct perf_evsel *evsel,
  123. struct perf_sample *sample,
  124. struct symbol **parent,
  125. struct addr_location *root_al,
  126. int max_stack);
  127. /*
  128. * Default guest kernel is defined by parameter --guestkallsyms
  129. * and --guestmodules
  130. */
  131. static inline bool machine__is_default_guest(struct machine *machine)
  132. {
  133. return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
  134. }
  135. static inline bool machine__is_host(struct machine *machine)
  136. {
  137. return machine ? machine->pid == HOST_KERNEL_ID : false;
  138. }
  139. struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
  140. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
  141. struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
  142. size_t machine__fprintf(struct machine *machine, FILE *fp);
  143. static inline
  144. struct symbol *machine__find_kernel_symbol(struct machine *machine,
  145. enum map_type type, u64 addr,
  146. struct map **mapp)
  147. {
  148. return map_groups__find_symbol(&machine->kmaps, type, addr, mapp);
  149. }
  150. static inline
  151. struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine,
  152. enum map_type type, const char *name,
  153. struct map **mapp)
  154. {
  155. return map_groups__find_symbol_by_name(&machine->kmaps, type, name, mapp);
  156. }
  157. static inline
  158. struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
  159. struct map **mapp)
  160. {
  161. return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr,
  162. mapp);
  163. }
  164. static inline
  165. struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
  166. const char *name,
  167. struct map **mapp)
  168. {
  169. return map_groups__find_function_by_name(&machine->kmaps, name, mapp);
  170. }
  171. struct map *machine__findnew_module_map(struct machine *machine, u64 start,
  172. const char *filename);
  173. int arch__fix_module_text_start(u64 *start, const char *name);
  174. int __machine__load_kallsyms(struct machine *machine, const char *filename,
  175. enum map_type type, bool no_kcore);
  176. int machine__load_kallsyms(struct machine *machine, const char *filename,
  177. enum map_type type);
  178. int machine__load_vmlinux_path(struct machine *machine, enum map_type type);
  179. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  180. bool (skip)(struct dso *dso, int parm), int parm);
  181. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
  182. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  183. bool (skip)(struct dso *dso, int parm), int parm);
  184. void machine__destroy_kernel_maps(struct machine *machine);
  185. int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
  186. int machine__create_kernel_maps(struct machine *machine);
  187. int machines__create_kernel_maps(struct machines *machines, pid_t pid);
  188. int machines__create_guest_kernel_maps(struct machines *machines);
  189. void machines__destroy_kernel_maps(struct machines *machines);
  190. size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
  191. int machine__for_each_thread(struct machine *machine,
  192. int (*fn)(struct thread *thread, void *p),
  193. void *priv);
  194. int machines__for_each_thread(struct machines *machines,
  195. int (*fn)(struct thread *thread, void *p),
  196. void *priv);
  197. int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
  198. struct target *target, struct thread_map *threads,
  199. perf_event__handler_t process, bool data_mmap,
  200. unsigned int proc_map_timeout);
  201. static inline
  202. int machine__synthesize_threads(struct machine *machine, struct target *target,
  203. struct thread_map *threads, bool data_mmap,
  204. unsigned int proc_map_timeout)
  205. {
  206. return __machine__synthesize_threads(machine, NULL, target, threads,
  207. perf_event__process, data_mmap,
  208. proc_map_timeout);
  209. }
  210. pid_t machine__get_current_tid(struct machine *machine, int cpu);
  211. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  212. pid_t tid);
  213. /*
  214. * For use with libtraceevent's pevent_set_function_resolver()
  215. */
  216. char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp);
  217. #endif /* __PERF_MACHINE_H */