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