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