machine.h 9.3 KB

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