machine.h 8.7 KB

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