]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/perf/util/machine.h
perf symbols: Unify symbol maps
[linux.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 #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         char              *mmap_name;
47         struct threads    threads[THREADS__TABLE_SIZE];
48         struct vdso_info  *vdso_info;
49         struct perf_env   *env;
50         struct dsos       dsos;
51         struct map_groups kmaps;
52         struct map        *vmlinux_map;
53         u64               kernel_start;
54         pid_t             *current_tid;
55         union { /* Tool specific area */
56                 void      *priv;
57                 u64       db_id;
58         };
59 };
60
61 static inline struct threads *machine__threads(struct machine *machine, pid_t tid)
62 {
63         /* Cast it to handle tid == -1 */
64         return &machine->threads[(unsigned int)tid % THREADS__TABLE_SIZE];
65 }
66
67 /*
68  * The main kernel (vmlinux) map
69  */
70 static inline
71 struct map *machine__kernel_map(struct machine *machine)
72 {
73         return machine->vmlinux_map;
74 }
75
76 /*
77  * kernel (the one returned by machine__kernel_map()) plus kernel modules maps
78  */
79 static inline
80 struct maps *machine__kernel_maps(struct machine *machine)
81 {
82         return &machine->kmaps.maps;
83 }
84
85 int machine__get_kernel_start(struct machine *machine);
86
87 static inline u64 machine__kernel_start(struct machine *machine)
88 {
89         if (!machine->kernel_start)
90                 machine__get_kernel_start(machine);
91         return machine->kernel_start;
92 }
93
94 static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
95 {
96         u64 kernel_start = machine__kernel_start(machine);
97
98         return ip >= kernel_start;
99 }
100
101 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
102                                     pid_t tid);
103 struct comm *machine__thread_exec_comm(struct machine *machine,
104                                        struct thread *thread);
105
106 int machine__process_comm_event(struct machine *machine, union perf_event *event,
107                                 struct perf_sample *sample);
108 int machine__process_exit_event(struct machine *machine, union perf_event *event,
109                                 struct perf_sample *sample);
110 int machine__process_fork_event(struct machine *machine, union perf_event *event,
111                                 struct perf_sample *sample);
112 int machine__process_lost_event(struct machine *machine, union perf_event *event,
113                                 struct perf_sample *sample);
114 int machine__process_lost_samples_event(struct machine *machine, union perf_event *event,
115                                         struct perf_sample *sample);
116 int machine__process_aux_event(struct machine *machine,
117                                union perf_event *event);
118 int machine__process_itrace_start_event(struct machine *machine,
119                                         union perf_event *event);
120 int machine__process_switch_event(struct machine *machine,
121                                   union perf_event *event);
122 int machine__process_namespaces_event(struct machine *machine,
123                                       union perf_event *event,
124                                       struct perf_sample *sample);
125 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
126                                 struct perf_sample *sample);
127 int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
128                                  struct perf_sample *sample);
129 int machine__process_event(struct machine *machine, union perf_event *event,
130                                 struct perf_sample *sample);
131
132 typedef void (*machine__process_t)(struct machine *machine, void *data);
133
134 struct machines {
135         struct machine host;
136         struct rb_root guests;
137 };
138
139 void machines__init(struct machines *machines);
140 void machines__exit(struct machines *machines);
141
142 void machines__process_guests(struct machines *machines,
143                               machine__process_t process, void *data);
144
145 struct machine *machines__add(struct machines *machines, pid_t pid,
146                               const char *root_dir);
147 struct machine *machines__find_host(struct machines *machines);
148 struct machine *machines__find(struct machines *machines, pid_t pid);
149 struct machine *machines__findnew(struct machines *machines, pid_t pid);
150
151 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
152 void machines__set_comm_exec(struct machines *machines, bool comm_exec);
153
154 struct machine *machine__new_host(void);
155 struct machine *machine__new_kallsyms(void);
156 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
157 void machine__exit(struct machine *machine);
158 void machine__delete_threads(struct machine *machine);
159 void machine__delete(struct machine *machine);
160 void machine__remove_thread(struct machine *machine, struct thread *th);
161
162 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
163                                            struct addr_location *al);
164 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
165                                      struct addr_location *al);
166
167 struct callchain_cursor;
168
169 int thread__resolve_callchain(struct thread *thread,
170                               struct callchain_cursor *cursor,
171                               struct perf_evsel *evsel,
172                               struct perf_sample *sample,
173                               struct symbol **parent,
174                               struct addr_location *root_al,
175                               int max_stack);
176
177 /*
178  * Default guest kernel is defined by parameter --guestkallsyms
179  * and --guestmodules
180  */
181 static inline bool machine__is_default_guest(struct machine *machine)
182 {
183         return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
184 }
185
186 static inline bool machine__is_host(struct machine *machine)
187 {
188         return machine ? machine->pid == HOST_KERNEL_ID : false;
189 }
190
191 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
192 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
193
194 struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
195
196 size_t machine__fprintf(struct machine *machine, FILE *fp);
197
198 static inline
199 struct symbol *machine__find_kernel_symbol(struct machine *machine, u64 addr,
200                                            struct map **mapp)
201 {
202         return map_groups__find_symbol(&machine->kmaps, addr, mapp);
203 }
204
205 static inline
206 struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine,
207                                                    const char *name,
208                                                    struct map **mapp)
209 {
210         return map_groups__find_symbol_by_name(&machine->kmaps, name, mapp);
211 }
212
213 static inline
214 struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
215                                              struct map **mapp)
216 {
217         return machine__find_kernel_symbol(machine,  addr, mapp);
218 }
219
220 static inline
221 struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
222                                                      const char *name,
223                                                      struct map **mapp)
224 {
225         return map_groups__find_function_by_name(&machine->kmaps, name, mapp);
226 }
227
228 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
229                                         const char *filename);
230 int arch__fix_module_text_start(u64 *start, const char *name);
231
232 int machine__load_kallsyms(struct machine *machine, const char *filename);
233
234 int machine__load_vmlinux_path(struct machine *machine);
235
236 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
237                                      bool (skip)(struct dso *dso, int parm), int parm);
238 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
239 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
240                                      bool (skip)(struct dso *dso, int parm), int parm);
241
242 void machine__destroy_kernel_maps(struct machine *machine);
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 */