GNU Linux-libre 4.14.290-gnu1
[releases.git] / tools / perf / util / machine.h
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 bool machine__is(struct machine *machine, const char *arch);
173 int machine__nr_cpus_avail(struct machine *machine);
174
175 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
176 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
177
178 struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
179
180 size_t machine__fprintf(struct machine *machine, FILE *fp);
181
182 static inline
183 struct symbol *machine__find_kernel_symbol(struct machine *machine,
184                                            enum map_type type, u64 addr,
185                                            struct map **mapp)
186 {
187         return map_groups__find_symbol(&machine->kmaps, type, addr, mapp);
188 }
189
190 static inline
191 struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine,
192                                                    enum map_type type, const char *name,
193                                                    struct map **mapp)
194 {
195         return map_groups__find_symbol_by_name(&machine->kmaps, type, name, mapp);
196 }
197
198 static inline
199 struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
200                                              struct map **mapp)
201 {
202         return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr,
203                                            mapp);
204 }
205
206 static inline
207 struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
208                                                      const char *name,
209                                                      struct map **mapp)
210 {
211         return map_groups__find_function_by_name(&machine->kmaps, name, mapp);
212 }
213
214 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
215                                         const char *filename);
216 int arch__fix_module_text_start(u64 *start, u64 *size, const char *name);
217
218 int __machine__load_kallsyms(struct machine *machine, const char *filename,
219                              enum map_type type, bool no_kcore);
220 int machine__load_kallsyms(struct machine *machine, const char *filename,
221                            enum map_type type);
222 int machine__load_vmlinux_path(struct machine *machine, enum map_type type);
223
224 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
225                                      bool (skip)(struct dso *dso, int parm), int parm);
226 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
227 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
228                                      bool (skip)(struct dso *dso, int parm), int parm);
229
230 void machine__destroy_kernel_maps(struct machine *machine);
231 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
232 int machine__create_kernel_maps(struct machine *machine);
233
234 int machines__create_kernel_maps(struct machines *machines, pid_t pid);
235 int machines__create_guest_kernel_maps(struct machines *machines);
236 void machines__destroy_kernel_maps(struct machines *machines);
237
238 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
239
240 int machine__for_each_thread(struct machine *machine,
241                              int (*fn)(struct thread *thread, void *p),
242                              void *priv);
243 int machines__for_each_thread(struct machines *machines,
244                               int (*fn)(struct thread *thread, void *p),
245                               void *priv);
246
247 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
248                                   struct target *target, struct thread_map *threads,
249                                   perf_event__handler_t process, bool data_mmap,
250                                   unsigned int proc_map_timeout);
251 static inline
252 int machine__synthesize_threads(struct machine *machine, struct target *target,
253                                 struct thread_map *threads, bool data_mmap,
254                                 unsigned int proc_map_timeout)
255 {
256         return __machine__synthesize_threads(machine, NULL, target, threads,
257                                              perf_event__process, data_mmap,
258                                              proc_map_timeout);
259 }
260
261 pid_t machine__get_current_tid(struct machine *machine, int cpu);
262 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
263                              pid_t tid);
264 /*
265  * For use with libtraceevent's pevent_set_function_resolver()
266  */
267 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp);
268
269 int machine__map_x86_64_entry_trampolines(struct machine *machine,
270                                           struct dso *kernel);
271
272 #endif /* __PERF_MACHINE_H */