machine.h 9.4 KB

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