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 "map.h" 8 #include "dso.h" 9 #include "event.h" 10 11 struct addr_location; 12 struct branch_stack; 13 struct perf_evsel; 14 struct perf_sample; 15 struct symbol; 16 struct thread; 17 union perf_event; 18 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 23 extern const char *ref_reloc_sym_names[]; 24 25 struct vdso_info; 26 27 struct machine { 28 struct rb_node rb_node; 29 pid_t pid; 30 u16 id_hdr_size; 31 bool comm_exec; 32 bool kptr_restrict_warned; 33 char *root_dir; 34 struct rb_root threads; 35 pthread_rwlock_t threads_lock; 36 unsigned int nr_threads; 37 struct list_head dead_threads; 38 struct thread *last_match; 39 struct vdso_info *vdso_info; 40 struct perf_env *env; 41 struct dsos dsos; 42 struct map_groups kmaps; 43 struct map *vmlinux_maps[MAP__NR_TYPES]; 44 u64 kernel_start; 45 pid_t *current_tid; 46 union { /* Tool specific area */ 47 void *priv; 48 u64 db_id; 49 }; 50 }; 51 52 static inline 53 struct map *__machine__kernel_map(struct machine *machine, enum map_type type) 54 { 55 return machine->vmlinux_maps[type]; 56 } 57 58 static inline 59 struct map *machine__kernel_map(struct machine *machine) 60 { 61 return __machine__kernel_map(machine, MAP__FUNCTION); 62 } 63 64 int machine__get_kernel_start(struct machine *machine); 65 66 static inline u64 machine__kernel_start(struct machine *machine) 67 { 68 if (!machine->kernel_start) 69 machine__get_kernel_start(machine); 70 return machine->kernel_start; 71 } 72 73 static inline bool machine__kernel_ip(struct machine *machine, u64 ip) 74 { 75 u64 kernel_start = machine__kernel_start(machine); 76 77 return ip >= kernel_start; 78 } 79 80 struct thread *machine__find_thread(struct machine *machine, pid_t pid, 81 pid_t tid); 82 struct comm *machine__thread_exec_comm(struct machine *machine, 83 struct thread *thread); 84 85 int machine__process_comm_event(struct machine *machine, union perf_event *event, 86 struct perf_sample *sample); 87 int machine__process_exit_event(struct machine *machine, union perf_event *event, 88 struct perf_sample *sample); 89 int machine__process_fork_event(struct machine *machine, union perf_event *event, 90 struct perf_sample *sample); 91 int machine__process_lost_event(struct machine *machine, union perf_event *event, 92 struct perf_sample *sample); 93 int machine__process_lost_samples_event(struct machine *machine, union perf_event *event, 94 struct perf_sample *sample); 95 int machine__process_aux_event(struct machine *machine, 96 union perf_event *event); 97 int machine__process_itrace_start_event(struct machine *machine, 98 union perf_event *event); 99 int machine__process_switch_event(struct machine *machine, 100 union perf_event *event); 101 int machine__process_namespaces_event(struct machine *machine, 102 union perf_event *event, 103 struct perf_sample *sample); 104 int machine__process_mmap_event(struct machine *machine, union perf_event *event, 105 struct perf_sample *sample); 106 int machine__process_mmap2_event(struct machine *machine, union perf_event *event, 107 struct perf_sample *sample); 108 int machine__process_event(struct machine *machine, union perf_event *event, 109 struct perf_sample *sample); 110 111 typedef void (*machine__process_t)(struct machine *machine, void *data); 112 113 struct machines { 114 struct machine host; 115 struct rb_root guests; 116 }; 117 118 void machines__init(struct machines *machines); 119 void machines__exit(struct machines *machines); 120 121 void machines__process_guests(struct machines *machines, 122 machine__process_t process, void *data); 123 124 struct machine *machines__add(struct machines *machines, pid_t pid, 125 const char *root_dir); 126 struct machine *machines__find_host(struct machines *machines); 127 struct machine *machines__find(struct machines *machines, pid_t pid); 128 struct machine *machines__findnew(struct machines *machines, pid_t pid); 129 130 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size); 131 char *machine__mmap_name(struct machine *machine, char *bf, size_t size); 132 133 void machines__set_comm_exec(struct machines *machines, bool comm_exec); 134 135 struct machine *machine__new_host(void); 136 struct machine *machine__new_kallsyms(void); 137 int machine__init(struct machine *machine, const char *root_dir, pid_t pid); 138 void machine__exit(struct machine *machine); 139 void machine__delete_threads(struct machine *machine); 140 void machine__delete(struct machine *machine); 141 void machine__remove_thread(struct machine *machine, struct thread *th); 142 143 struct branch_info *sample__resolve_bstack(struct perf_sample *sample, 144 struct addr_location *al); 145 struct mem_info *sample__resolve_mem(struct perf_sample *sample, 146 struct addr_location *al); 147 148 struct callchain_cursor; 149 150 int thread__resolve_callchain(struct thread *thread, 151 struct callchain_cursor *cursor, 152 struct perf_evsel *evsel, 153 struct perf_sample *sample, 154 struct symbol **parent, 155 struct addr_location *root_al, 156 int max_stack); 157 158 /* 159 * Default guest kernel is defined by parameter --guestkallsyms 160 * and --guestmodules 161 */ 162 static inline bool machine__is_default_guest(struct machine *machine) 163 { 164 return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false; 165 } 166 167 static inline bool machine__is_host(struct machine *machine) 168 { 169 return machine ? machine->pid == HOST_KERNEL_ID : false; 170 } 171 172 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid); 173 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid); 174 175 struct dso *machine__findnew_dso(struct machine *machine, const char *filename); 176 177 size_t machine__fprintf(struct machine *machine, FILE *fp); 178 179 static inline 180 struct symbol *machine__find_kernel_symbol(struct machine *machine, 181 enum map_type type, u64 addr, 182 struct map **mapp) 183 { 184 return map_groups__find_symbol(&machine->kmaps, type, addr, mapp); 185 } 186 187 static inline 188 struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine, 189 enum map_type type, const char *name, 190 struct map **mapp) 191 { 192 return map_groups__find_symbol_by_name(&machine->kmaps, type, name, mapp); 193 } 194 195 static inline 196 struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr, 197 struct map **mapp) 198 { 199 return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr, 200 mapp); 201 } 202 203 static inline 204 struct symbol *machine__find_kernel_function_by_name(struct machine *machine, 205 const char *name, 206 struct map **mapp) 207 { 208 return map_groups__find_function_by_name(&machine->kmaps, name, mapp); 209 } 210 211 struct map *machine__findnew_module_map(struct machine *machine, u64 start, 212 const char *filename); 213 int arch__fix_module_text_start(u64 *start, const char *name); 214 215 int __machine__load_kallsyms(struct machine *machine, const char *filename, 216 enum map_type type, bool no_kcore); 217 int machine__load_kallsyms(struct machine *machine, const char *filename, 218 enum map_type type); 219 int machine__load_vmlinux_path(struct machine *machine, enum map_type type); 220 221 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp, 222 bool (skip)(struct dso *dso, int parm), int parm); 223 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp); 224 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, 225 bool (skip)(struct dso *dso, int parm), int parm); 226 227 void machine__destroy_kernel_maps(struct machine *machine); 228 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel); 229 int machine__create_kernel_maps(struct machine *machine); 230 231 int machines__create_kernel_maps(struct machines *machines, pid_t pid); 232 int machines__create_guest_kernel_maps(struct machines *machines); 233 void machines__destroy_kernel_maps(struct machines *machines); 234 235 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp); 236 237 int machine__for_each_thread(struct machine *machine, 238 int (*fn)(struct thread *thread, void *p), 239 void *priv); 240 int machines__for_each_thread(struct machines *machines, 241 int (*fn)(struct thread *thread, void *p), 242 void *priv); 243 244 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, 245 struct target *target, struct thread_map *threads, 246 perf_event__handler_t process, bool data_mmap, 247 unsigned int proc_map_timeout); 248 static inline 249 int machine__synthesize_threads(struct machine *machine, struct target *target, 250 struct thread_map *threads, bool data_mmap, 251 unsigned int proc_map_timeout) 252 { 253 return __machine__synthesize_threads(machine, NULL, target, threads, 254 perf_event__process, data_mmap, 255 proc_map_timeout); 256 } 257 258 pid_t machine__get_current_tid(struct machine *machine, int cpu); 259 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, 260 pid_t tid); 261 /* 262 * For use with libtraceevent's pevent_set_function_resolver() 263 */ 264 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp); 265 266 #endif /* __PERF_MACHINE_H */ 267