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