machine.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "maps.h"
  7. #include "dsos.h"
  8. #include "rwsem.h"
  9. #include "threads.h"
  10. struct addr_location;
  11. struct branch_stack;
  12. struct dso;
  13. struct dso_id;
  14. struct evsel;
  15. struct perf_sample;
  16. struct symbol;
  17. struct target;
  18. struct thread;
  19. union perf_event;
  20. struct machines;
  21. /* Native host kernel uses -1 as pid index in machine */
  22. #define HOST_KERNEL_ID (-1)
  23. #define DEFAULT_GUEST_KERNEL_ID (0)
  24. extern const char *ref_reloc_sym_names[];
  25. struct vdso_info;
  26. struct machine {
  27. struct rb_node rb_node;
  28. pid_t pid;
  29. u16 id_hdr_size;
  30. bool comm_exec;
  31. bool kptr_restrict_warned;
  32. bool single_address_space;
  33. char *root_dir;
  34. char *mmap_name;
  35. char *kallsyms_filename;
  36. struct threads threads;
  37. struct vdso_info *vdso_info;
  38. struct perf_env *env;
  39. struct dsos dsos;
  40. struct maps *kmaps;
  41. struct map *vmlinux_map;
  42. u64 kernel_start;
  43. struct {
  44. u64 text_start;
  45. u64 text_end;
  46. } sched, lock, traceiter, trace;
  47. /*
  48. * The current parallelism level (number of threads that run on CPUs).
  49. * This value can be less than 1, or larger than the total number
  50. * of CPUs, if events are poorly ordered.
  51. */
  52. int parallelism;
  53. pid_t *current_tid;
  54. size_t current_tid_sz;
  55. union { /* Tool specific area */
  56. void *priv;
  57. u64 db_id;
  58. };
  59. struct machines *machines;
  60. bool trampolines_mapped;
  61. };
  62. /*
  63. * The main kernel (vmlinux) map
  64. */
  65. static inline
  66. struct map *machine__kernel_map(struct machine *machine)
  67. {
  68. return machine->vmlinux_map;
  69. }
  70. /*
  71. * kernel (the one returned by machine__kernel_map()) plus kernel modules maps
  72. */
  73. static inline
  74. struct maps *machine__kernel_maps(struct machine *machine)
  75. {
  76. return machine->kmaps;
  77. }
  78. int machine__get_kernel_start(struct machine *machine);
  79. static inline u64 machine__kernel_start(struct machine *machine)
  80. {
  81. if (!machine->kernel_start)
  82. machine__get_kernel_start(machine);
  83. return machine->kernel_start;
  84. }
  85. static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
  86. {
  87. u64 kernel_start = machine__kernel_start(machine);
  88. return ip >= kernel_start;
  89. }
  90. u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr);
  91. struct thread *machine__find_thread(struct machine *machine, pid_t pid,
  92. pid_t tid);
  93. struct thread *machine__idle_thread(struct machine *machine);
  94. struct comm *machine__thread_exec_comm(struct machine *machine,
  95. struct thread *thread);
  96. int machine__process_comm_event(struct machine *machine, union perf_event *event,
  97. struct perf_sample *sample);
  98. int machine__process_exit_event(struct machine *machine, union perf_event *event,
  99. struct perf_sample *sample);
  100. int machine__process_fork_event(struct machine *machine, union perf_event *event,
  101. struct perf_sample *sample);
  102. int machine__process_lost_event(struct machine *machine, union perf_event *event,
  103. struct perf_sample *sample);
  104. int machine__process_lost_samples_event(struct machine *machine, union perf_event *event,
  105. struct perf_sample *sample);
  106. int machine__process_aux_event(struct machine *machine,
  107. union perf_event *event);
  108. int machine__process_itrace_start_event(struct machine *machine,
  109. union perf_event *event);
  110. int machine__process_aux_output_hw_id_event(struct machine *machine,
  111. union perf_event *event);
  112. int machine__process_switch_event(struct machine *machine,
  113. union perf_event *event);
  114. int machine__process_namespaces_event(struct machine *machine,
  115. union perf_event *event,
  116. struct perf_sample *sample);
  117. int machine__process_cgroup_event(struct machine *machine,
  118. union perf_event *event,
  119. struct perf_sample *sample);
  120. int machine__process_mmap_event(struct machine *machine, union perf_event *event,
  121. struct perf_sample *sample);
  122. int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
  123. struct perf_sample *sample);
  124. int machine__process_ksymbol(struct machine *machine,
  125. union perf_event *event,
  126. struct perf_sample *sample);
  127. int machine__process_text_poke(struct machine *machine,
  128. union perf_event *event,
  129. struct perf_sample *sample);
  130. int machine__process_event(struct machine *machine, union perf_event *event,
  131. struct perf_sample *sample);
  132. typedef void (*machine__process_t)(struct machine *machine, void *data);
  133. struct machines {
  134. struct machine host;
  135. struct rb_root_cached guests;
  136. };
  137. void machines__init(struct machines *machines);
  138. void machines__exit(struct machines *machines);
  139. void machines__process_guests(struct machines *machines,
  140. machine__process_t process, void *data);
  141. struct machine *machines__add(struct machines *machines, pid_t pid,
  142. const char *root_dir);
  143. struct machine *machines__find(struct machines *machines, pid_t pid);
  144. struct machine *machines__findnew(struct machines *machines, pid_t pid);
  145. struct machine *machines__find_guest(struct machines *machines, pid_t pid);
  146. struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid);
  147. struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid);
  148. void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
  149. void machines__set_comm_exec(struct machines *machines, bool comm_exec);
  150. struct machine *machine__new_host(struct perf_env *host_env);
  151. struct machine *machine__new_kallsyms(struct perf_env *host_env);
  152. struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid);
  153. int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
  154. void machine__exit(struct machine *machine);
  155. void machine__delete_threads(struct machine *machine);
  156. void machine__delete(struct machine *machine);
  157. void machine__remove_thread(struct machine *machine, struct thread *th);
  158. struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
  159. struct addr_location *al);
  160. struct mem_info *sample__resolve_mem(struct perf_sample *sample,
  161. struct addr_location *al);
  162. struct callchain_cursor;
  163. int __thread__resolve_callchain(struct thread *thread,
  164. struct callchain_cursor *cursor,
  165. struct evsel *evsel,
  166. struct perf_sample *sample,
  167. struct symbol **parent,
  168. struct addr_location *root_al,
  169. int max_stack,
  170. bool symbols);
  171. static inline int thread__resolve_callchain(struct thread *thread,
  172. struct callchain_cursor *cursor,
  173. struct evsel *evsel,
  174. struct perf_sample *sample,
  175. struct symbol **parent,
  176. struct addr_location *root_al,
  177. int max_stack)
  178. {
  179. return __thread__resolve_callchain(thread,
  180. cursor,
  181. evsel,
  182. sample,
  183. parent,
  184. root_al,
  185. max_stack,
  186. /*symbols=*/true);
  187. }
  188. /*
  189. * Default guest kernel is defined by parameter --guestkallsyms
  190. * and --guestmodules
  191. */
  192. static inline bool machine__is_default_guest(struct machine *machine)
  193. {
  194. return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
  195. }
  196. static inline bool machine__is_host(struct machine *machine)
  197. {
  198. return machine ? machine->pid == HOST_KERNEL_ID : false;
  199. }
  200. bool machine__is_lock_function(struct machine *machine, u64 addr);
  201. bool machine__is(struct machine *machine, const char *arch);
  202. bool machine__normalized_is(struct machine *machine, const char *arch);
  203. int machine__nr_cpus_avail(struct machine *machine);
  204. struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
  205. struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename,
  206. const struct dso_id *id);
  207. struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
  208. size_t machine__fprintf(struct machine *machine, FILE *fp);
  209. static inline
  210. struct symbol *machine__find_kernel_symbol(struct machine *machine, u64 addr,
  211. struct map **mapp)
  212. {
  213. return maps__find_symbol(machine->kmaps, addr, mapp);
  214. }
  215. static inline
  216. struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine,
  217. const char *name,
  218. struct map **mapp)
  219. {
  220. return maps__find_symbol_by_name(machine->kmaps, name, mapp);
  221. }
  222. int arch__fix_module_text_start(u64 *start, u64 *size, const char *name);
  223. int machine__load_kallsyms(struct machine *machine, const char *filename);
  224. int machine__load_vmlinux_path(struct machine *machine);
  225. size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
  226. bool (skip)(struct dso *dso, int parm), int parm);
  227. size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
  228. size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
  229. bool (skip)(struct dso *dso, int parm), int parm);
  230. void machine__destroy_kernel_maps(struct machine *machine);
  231. int machine__create_kernel_maps(struct machine *machine);
  232. int machines__create_kernel_maps(struct machines *machines, pid_t pid);
  233. int machines__create_guest_kernel_maps(struct machines *machines);
  234. void machines__destroy_kernel_maps(struct machines *machines);
  235. typedef int (*machine__dso_t)(struct dso *dso, struct machine *machine, void *priv);
  236. int machine__for_each_dso(struct machine *machine, machine__dso_t fn,
  237. void *priv);
  238. typedef int (*machine__map_t)(struct map *map, void *priv);
  239. int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn,
  240. void *priv);
  241. int machine__for_each_thread(struct machine *machine,
  242. int (*fn)(struct thread *thread, void *p),
  243. void *priv);
  244. int machines__for_each_thread(struct machines *machines,
  245. int (*fn)(struct thread *thread, void *p),
  246. void *priv);
  247. struct thread_list {
  248. struct list_head list;
  249. struct thread *thread;
  250. };
  251. /* Make a list of struct thread_list based on threads in the machine. */
  252. int machine__thread_list(struct machine *machine, struct list_head *list);
  253. /* Free up the nodes within the thread_list list. */
  254. void thread_list__delete(struct list_head *list);
  255. pid_t machine__get_current_tid(struct machine *machine, int cpu);
  256. int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
  257. pid_t tid);
  258. /*
  259. * For use with libtraceevent's tep_set_function_resolver()
  260. */
  261. char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp);
  262. void machine__get_kallsyms_filename(struct machine *machine, char *buf,
  263. size_t bufsz);
  264. int machine__create_extra_kernel_maps(struct machine *machine,
  265. struct dso *kernel);
  266. /* Kernel-space maps for symbols that are outside the main kernel map and module maps */
  267. struct extra_kernel_map {
  268. u64 start;
  269. u64 end;
  270. u64 pgoff;
  271. char name[KMAP_NAME_LEN];
  272. };
  273. int machine__create_extra_kernel_map(struct machine *machine,
  274. struct dso *kernel,
  275. struct extra_kernel_map *xm);
  276. int machine__map_x86_64_entry_trampolines(struct machine *machine,
  277. struct dso *kernel);
  278. int machine__resolve(struct machine *machine, struct addr_location *al,
  279. struct perf_sample *sample);
  280. int machine__hit_all_dsos(struct machine *machine);
  281. #endif /* __PERF_MACHINE_H */