machine.h 9.0 KB

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