blob: 83b2fbbeeb900e713e733e054392a1eb45efac61 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03002#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03003#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03004#include <inttypes.h>
Arnaldo Carvalho de Melo1eae20c2017-04-18 12:33:30 -03005#include <regex.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03006#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03007#include "debug.h"
8#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03009#include "evsel.h"
10#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030011#include "machine.h"
12#include "map.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010013#include "symbol.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030015#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030016#include "thread.h"
Adrian Hunterd027b6402014-07-23 14:23:00 +030017#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030018#include <stdbool.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030019#include <sys/types.h>
20#include <sys/stat.h>
21#include <unistd.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030022#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080023#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053024#include "asm/bug.h"
Song Liu45178a92019-01-17 08:15:18 -080025#include "bpf-event.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030026
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030027#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030028#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -030029#include <linux/mman.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030030#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030031
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030032static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
33
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030034static void dsos__init(struct dsos *dsos)
35{
36 INIT_LIST_HEAD(&dsos->head);
37 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030038 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030039}
40
Kan Liang91e467b2017-09-10 19:23:14 -070041static void machine__threads_init(struct machine *machine)
42{
43 int i;
44
45 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
46 struct threads *threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -080047 threads->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030048 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070049 threads->nr = 0;
50 INIT_LIST_HEAD(&threads->dead);
51 threads->last_match = NULL;
52 }
53}
54
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010055static int machine__set_mmap_name(struct machine *machine)
56{
Jiri Olsac1925242018-03-12 16:24:06 +010057 if (machine__is_host(machine))
58 machine->mmap_name = strdup("[kernel.kallsyms]");
59 else if (machine__is_default_guest(machine))
60 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
61 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
62 machine->pid) < 0)
63 machine->mmap_name = NULL;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010064
65 return machine->mmap_name ? 0 : -ENOMEM;
66}
67
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030068int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
69{
Jiri Olsa81f981d2018-02-15 13:26:29 +010070 int err = -ENOMEM;
71
Wang Nan93b0ba32015-12-08 02:25:44 +000072 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030073 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030074 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030075 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030076
Kan Liang91e467b2017-09-10 19:23:14 -070077 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030078
Adrian Hunterd027b6402014-07-23 14:23:00 +030079 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030080 machine->env = NULL;
Adrian Hunterd027b6402014-07-23 14:23:00 +030081
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030082 machine->pid = pid;
83
Jiri Olsa14bd6d22014-01-07 13:47:19 +010084 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030085 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030086 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030087 machine->kernel_start = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030088 machine->vmlinux_map = NULL;
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090089
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030090 machine->root_dir = strdup(root_dir);
91 if (machine->root_dir == NULL)
92 return -ENOMEM;
93
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010094 if (machine__set_mmap_name(machine))
95 goto out;
96
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030097 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030098 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030099 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300100 char comm[64];
101
102 if (thread == NULL)
Jiri Olsa81f981d2018-02-15 13:26:29 +0100103 goto out;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300104
105 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200106 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300107 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300108 }
109
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300110 machine->current_tid = NULL;
Jiri Olsa81f981d2018-02-15 13:26:29 +0100111 err = 0;
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300112
Jiri Olsa81f981d2018-02-15 13:26:29 +0100113out:
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100114 if (err) {
Jiri Olsa81f981d2018-02-15 13:26:29 +0100115 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100116 zfree(&machine->mmap_name);
117 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300118 return 0;
119}
120
David Ahern8fb598e2013-09-28 13:13:00 -0600121struct machine *machine__new_host(void)
122{
123 struct machine *machine = malloc(sizeof(*machine));
124
125 if (machine != NULL) {
126 machine__init(machine, "", HOST_KERNEL_ID);
127
128 if (machine__create_kernel_maps(machine) < 0)
129 goto out_delete;
130 }
131
132 return machine;
133out_delete:
134 free(machine);
135 return NULL;
136}
137
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300138struct machine *machine__new_kallsyms(void)
139{
140 struct machine *machine = machine__new_host();
141 /*
142 * FIXME:
Ingo Molnaradba16342018-12-03 11:22:00 +0100143 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300144 * ask for not using the kcore parsing code, once this one is fixed
145 * to create a map per module.
146 */
Arnaldo Carvalho de Melo329f0ad2018-04-25 11:40:32 -0300147 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300148 machine__delete(machine);
149 machine = NULL;
150 }
151
152 return machine;
153}
154
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300155static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300156{
157 struct dso *pos, *n;
158
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300159 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300160
Waiman Long8fa7d872014-09-29 16:07:28 -0400161 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400162 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200163 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300164 list_del_init(&pos->node);
165 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300166 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300167
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300168 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300169}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300170
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300171static void dsos__exit(struct dsos *dsos)
172{
173 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300174 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300175}
176
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300177void machine__delete_threads(struct machine *machine)
178{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300179 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700180 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300181
Kan Liang91e467b2017-09-10 19:23:14 -0700182 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
183 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300184 down_write(&threads->lock);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800185 nd = rb_first_cached(&threads->entries);
Kan Liang91e467b2017-09-10 19:23:14 -0700186 while (nd) {
187 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300188
Kan Liang91e467b2017-09-10 19:23:14 -0700189 nd = rb_next(nd);
190 __machine__remove_thread(machine, t, false);
191 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300192 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300193 }
194}
195
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300196void machine__exit(struct machine *machine)
197{
Kan Liang91e467b2017-09-10 19:23:14 -0700198 int i;
199
Arnaldo Carvalho de Melo19993b82017-11-13 16:06:29 -0300200 if (machine == NULL)
201 return;
202
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900203 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300204 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300205 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300206 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300207 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100208 zfree(&machine->mmap_name);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300209 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700210
211 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
212 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -0300213 struct thread *thread, *n;
214 /*
215 * Forget about the dead, at this point whatever threads were
216 * left in the dead lists better have a reference count taken
217 * by who is using them, and then, when they drop those references
218 * and it finally hits zero, thread__put() will check and see that
219 * its not in the dead threads list and will not try to remove it
220 * from there, just calling thread__delete() straight away.
221 */
222 list_for_each_entry_safe(thread, n, &threads->dead, node)
223 list_del_init(&thread->node);
224
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300225 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700226 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300227}
228
229void machine__delete(struct machine *machine)
230{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300231 if (machine) {
232 machine__exit(machine);
233 free(machine);
234 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300235}
236
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300237void machines__init(struct machines *machines)
238{
239 machine__init(&machines->host, "", HOST_KERNEL_ID);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800240 machines->guests = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300241}
242
243void machines__exit(struct machines *machines)
244{
245 machine__exit(&machines->host);
246 /* XXX exit guest */
247}
248
249struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300250 const char *root_dir)
251{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800252 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300253 struct rb_node *parent = NULL;
254 struct machine *pos, *machine = malloc(sizeof(*machine));
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800255 bool leftmost = true;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300256
257 if (machine == NULL)
258 return NULL;
259
260 if (machine__init(machine, root_dir, pid) != 0) {
261 free(machine);
262 return NULL;
263 }
264
265 while (*p != NULL) {
266 parent = *p;
267 pos = rb_entry(parent, struct machine, rb_node);
268 if (pid < pos->pid)
269 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800270 else {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300271 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800272 leftmost = false;
273 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300274 }
275
276 rb_link_node(&machine->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800277 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300278
279 return machine;
280}
281
Adrian Huntercfe1c412014-07-31 09:00:45 +0300282void machines__set_comm_exec(struct machines *machines, bool comm_exec)
283{
284 struct rb_node *nd;
285
286 machines->host.comm_exec = comm_exec;
287
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800288 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntercfe1c412014-07-31 09:00:45 +0300289 struct machine *machine = rb_entry(nd, struct machine, rb_node);
290
291 machine->comm_exec = comm_exec;
292 }
293}
294
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300295struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300296{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800297 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300298 struct rb_node *parent = NULL;
299 struct machine *machine;
300 struct machine *default_machine = NULL;
301
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300302 if (pid == HOST_KERNEL_ID)
303 return &machines->host;
304
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300305 while (*p != NULL) {
306 parent = *p;
307 machine = rb_entry(parent, struct machine, rb_node);
308 if (pid < machine->pid)
309 p = &(*p)->rb_left;
310 else if (pid > machine->pid)
311 p = &(*p)->rb_right;
312 else
313 return machine;
314 if (!machine->pid)
315 default_machine = machine;
316 }
317
318 return default_machine;
319}
320
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300321struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300322{
323 char path[PATH_MAX];
324 const char *root_dir = "";
325 struct machine *machine = machines__find(machines, pid);
326
327 if (machine && (machine->pid == pid))
328 goto out;
329
330 if ((pid != HOST_KERNEL_ID) &&
331 (pid != DEFAULT_GUEST_KERNEL_ID) &&
332 (symbol_conf.guestmount)) {
333 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
334 if (access(path, R_OK)) {
335 static struct strlist *seen;
336
337 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300338 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300339
340 if (!strlist__has_entry(seen, path)) {
341 pr_err("Can't access file %s\n", path);
342 strlist__add(seen, path);
343 }
344 machine = NULL;
345 goto out;
346 }
347 root_dir = path;
348 }
349
350 machine = machines__add(machines, pid, root_dir);
351out:
352 return machine;
353}
354
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300355void machines__process_guests(struct machines *machines,
356 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300357{
358 struct rb_node *nd;
359
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800360 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300361 struct machine *pos = rb_entry(nd, struct machine, rb_node);
362 process(pos, data);
363 }
364}
365
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300366void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300367{
368 struct rb_node *node;
369 struct machine *machine;
370
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300371 machines->host.id_hdr_size = id_hdr_size;
372
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800373 for (node = rb_first_cached(&machines->guests); node;
374 node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300375 machine = rb_entry(node, struct machine, rb_node);
376 machine->id_hdr_size = id_hdr_size;
377 }
378
379 return;
380}
381
Adrian Hunter29ce3612014-07-16 11:07:13 +0300382static void machine__update_thread_pid(struct machine *machine,
383 struct thread *th, pid_t pid)
384{
385 struct thread *leader;
386
387 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
388 return;
389
390 th->pid_ = pid;
391
392 if (th->pid_ == th->tid)
393 return;
394
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300395 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300396 if (!leader)
397 goto out_err;
398
399 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300400 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300401
402 if (!leader->mg)
403 goto out_err;
404
405 if (th->mg == leader->mg)
406 return;
407
408 if (th->mg) {
409 /*
410 * Maps are created from MMAP events which provide the pid and
411 * tid. Consequently there never should be any maps on a thread
412 * with an unknown pid. Just print an error if there are.
413 */
414 if (!map_groups__empty(th->mg))
415 pr_err("Discarding thread maps for %d:%d\n",
416 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300417 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300418 }
419
420 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300421out_put:
422 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300423 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300424out_err:
425 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300426 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300427}
428
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300429/*
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200430 * Front-end cache - TID lookups come in blocks,
431 * so most of the time we dont have to look up
432 * the full rbtree:
433 */
434static struct thread*
Jiri Olsab57334b2018-07-19 16:33:44 +0200435__threads__get_last_match(struct threads *threads, struct machine *machine,
436 int pid, int tid)
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200437{
438 struct thread *th;
439
440 th = threads->last_match;
441 if (th != NULL) {
442 if (th->tid == tid) {
443 machine__update_thread_pid(machine, th, pid);
444 return thread__get(th);
445 }
446
447 threads->last_match = NULL;
448 }
449
450 return NULL;
451}
452
Jiri Olsab57334b2018-07-19 16:33:44 +0200453static struct thread*
454threads__get_last_match(struct threads *threads, struct machine *machine,
455 int pid, int tid)
456{
457 struct thread *th = NULL;
458
459 if (perf_singlethreaded)
460 th = __threads__get_last_match(threads, machine, pid, tid);
461
462 return th;
463}
464
465static void
466__threads__set_last_match(struct threads *threads, struct thread *th)
467{
468 threads->last_match = th;
469}
470
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200471static void
472threads__set_last_match(struct threads *threads, struct thread *th)
473{
Jiri Olsab57334b2018-07-19 16:33:44 +0200474 if (perf_singlethreaded)
475 __threads__set_last_match(threads, th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200476}
477
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200478/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800479 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300480 * lookup/new thread inserted.
481 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300482static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300483 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300484 pid_t pid, pid_t tid,
485 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300486{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800487 struct rb_node **p = &threads->entries.rb_root.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300488 struct rb_node *parent = NULL;
489 struct thread *th;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800490 bool leftmost = true;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300491
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200492 th = threads__get_last_match(threads, machine, pid, tid);
493 if (th)
494 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300495
496 while (*p != NULL) {
497 parent = *p;
498 th = rb_entry(parent, struct thread, rb_node);
499
Adrian Hunter38051232013-07-04 16:20:31 +0300500 if (th->tid == tid) {
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200501 threads__set_last_match(threads, th);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300502 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300503 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300504 }
505
Adrian Hunter38051232013-07-04 16:20:31 +0300506 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300507 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800508 else {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300509 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800510 leftmost = false;
511 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300512 }
513
514 if (!create)
515 return NULL;
516
Adrian Hunter99d725f2013-08-26 16:00:19 +0300517 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300518 if (th != NULL) {
519 rb_link_node(&th->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800520 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
Jiri Olsacddcef62014-04-09 20:54:29 +0200521
522 /*
523 * We have to initialize map_groups separately
524 * after rb tree is updated.
525 *
526 * The reason is that we call machine__findnew_thread
527 * within thread__init_map_groups to find the thread
528 * leader and that would screwed the rb tree.
529 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300530 if (thread__init_map_groups(th, machine)) {
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800531 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300532 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300533 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200534 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300535 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300536 /*
537 * It is now in the rbtree, get a ref
538 */
539 thread__get(th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200540 threads__set_last_match(threads, th);
Kan Liang91e467b2017-09-10 19:23:14 -0700541 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300542 }
543
544 return th;
545}
546
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300547struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
548{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300549 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300550}
551
Adrian Hunter314add62013-08-27 11:23:03 +0300552struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
553 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300554{
Kan Liang91e467b2017-09-10 19:23:14 -0700555 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300556 struct thread *th;
557
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300558 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300559 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300560 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300561 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300562}
563
Jiri Olsad75e6092014-03-14 15:00:03 +0100564struct thread *machine__find_thread(struct machine *machine, pid_t pid,
565 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300566{
Kan Liang91e467b2017-09-10 19:23:14 -0700567 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300568 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700569
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300570 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300571 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300572 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300573 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300574}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300575
Adrian Huntercfe1c412014-07-31 09:00:45 +0300576struct comm *machine__thread_exec_comm(struct machine *machine,
577 struct thread *thread)
578{
579 if (machine->comm_exec)
580 return thread__exec_comm(thread);
581 else
582 return thread__comm(thread);
583}
584
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200585int machine__process_comm_event(struct machine *machine, union perf_event *event,
586 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300587{
Adrian Hunter314add62013-08-27 11:23:03 +0300588 struct thread *thread = machine__findnew_thread(machine,
589 event->comm.pid,
590 event->comm.tid);
Adrian Hunter65de51f92014-07-31 09:00:44 +0300591 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300592 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300593
Adrian Huntercfe1c412014-07-31 09:00:45 +0300594 if (exec)
595 machine->comm_exec = true;
596
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300597 if (dump_trace)
598 perf_event__fprintf_comm(event, stdout);
599
Adrian Hunter65de51f92014-07-31 09:00:44 +0300600 if (thread == NULL ||
601 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300602 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300603 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300604 }
605
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300606 thread__put(thread);
607
608 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300609}
610
Hari Bathinif3b36142017-03-08 02:11:43 +0530611int machine__process_namespaces_event(struct machine *machine __maybe_unused,
612 union perf_event *event,
613 struct perf_sample *sample __maybe_unused)
614{
615 struct thread *thread = machine__findnew_thread(machine,
616 event->namespaces.pid,
617 event->namespaces.tid);
618 int err = 0;
619
620 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
621 "\nWARNING: kernel seems to support more namespaces than perf"
622 " tool.\nTry updating the perf tool..\n\n");
623
624 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
625 "\nWARNING: perf tool seems to support more namespaces than"
626 " the kernel.\nTry updating the kernel..\n\n");
627
628 if (dump_trace)
629 perf_event__fprintf_namespaces(event, stdout);
630
631 if (thread == NULL ||
632 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
633 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
634 err = -1;
635 }
636
637 thread__put(thread);
638
639 return err;
640}
641
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300642int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200643 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300644{
645 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
646 event->lost.id, event->lost.lost);
647 return 0;
648}
649
Kan Liangc4937a92015-05-10 15:13:15 -0400650int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
651 union perf_event *event, struct perf_sample *sample)
652{
653 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
654 sample->id, event->lost_samples.lost);
655 return 0;
656}
657
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300658static struct dso *machine__findnew_module_dso(struct machine *machine,
659 struct kmod_path *m,
660 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100661{
662 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100663
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300664 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300665
666 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100667 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300668 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100669 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300670 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100671
Namhyung Kim6b335e82017-05-31 21:01:04 +0900672 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100673 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100674 }
675
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300676 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300677out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300678 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100679 return dso;
680}
681
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300682int machine__process_aux_event(struct machine *machine __maybe_unused,
683 union perf_event *event)
684{
685 if (dump_trace)
686 perf_event__fprintf_aux(event, stdout);
687 return 0;
688}
689
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300690int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
691 union perf_event *event)
692{
693 if (dump_trace)
694 perf_event__fprintf_itrace_start(event, stdout);
695 return 0;
696}
697
Adrian Hunter02860392015-07-21 12:44:03 +0300698int machine__process_switch_event(struct machine *machine __maybe_unused,
699 union perf_event *event)
700{
701 if (dump_trace)
702 perf_event__fprintf_switch(event, stdout);
703 return 0;
704}
705
Song Liu9aa0bfa2019-01-17 08:15:17 -0800706static int machine__process_ksymbol_register(struct machine *machine,
707 union perf_event *event,
708 struct perf_sample *sample __maybe_unused)
709{
710 struct symbol *sym;
711 struct map *map;
712
713 map = map_groups__find(&machine->kmaps, event->ksymbol_event.addr);
714 if (!map) {
715 map = dso__new_map(event->ksymbol_event.name);
716 if (!map)
717 return -ENOMEM;
718
719 map->start = event->ksymbol_event.addr;
Song Liu9aa0bfa2019-01-17 08:15:17 -0800720 map->end = map->start + event->ksymbol_event.len;
721 map_groups__insert(&machine->kmaps, map);
722 }
723
Jiri Olsa8529f2e2019-05-08 15:20:04 +0200724 sym = symbol__new(map->map_ip(map, map->start),
725 event->ksymbol_event.len,
Song Liu9aa0bfa2019-01-17 08:15:17 -0800726 0, 0, event->ksymbol_event.name);
727 if (!sym)
728 return -ENOMEM;
729 dso__insert_symbol(map->dso, sym);
730 return 0;
731}
732
733static int machine__process_ksymbol_unregister(struct machine *machine,
734 union perf_event *event,
735 struct perf_sample *sample __maybe_unused)
736{
737 struct map *map;
738
739 map = map_groups__find(&machine->kmaps, event->ksymbol_event.addr);
740 if (map)
741 map_groups__remove(&machine->kmaps, map);
742
743 return 0;
744}
745
746int machine__process_ksymbol(struct machine *machine __maybe_unused,
747 union perf_event *event,
748 struct perf_sample *sample)
749{
750 if (dump_trace)
751 perf_event__fprintf_ksymbol(event, stdout);
752
753 if (event->ksymbol_event.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
754 return machine__process_ksymbol_unregister(machine, event,
755 sample);
756 return machine__process_ksymbol_register(machine, event, sample);
757}
758
Wang Nanc03d5182015-11-26 03:59:57 +0000759static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
760{
761 const char *dup_filename;
762
763 if (!filename || !dso || !dso->long_name)
764 return;
765 if (dso->long_name[0] != '[')
766 return;
767 if (!strchr(filename, '/'))
768 return;
769
770 dup_filename = strdup(filename);
771 if (!dup_filename)
772 return;
773
Wang Nan5dcf16d2015-12-07 02:36:25 +0000774 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000775}
776
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300777struct map *machine__findnew_module_map(struct machine *machine, u64 start,
778 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300779{
Jiri Olsaca333802015-02-17 17:29:57 +0100780 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900781 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100782 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300783
Jiri Olsaca333802015-02-17 17:29:57 +0100784 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300785 return NULL;
786
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -0300787 map = map_groups__find_by_name(&machine->kmaps, m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000788 if (map) {
789 /*
790 * If the map's dso is an offline module, give dso__load()
791 * a chance to find the file path of that module by fixing
792 * long_name.
793 */
794 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f4642015-02-17 17:31:18 +0100795 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000796 }
Jiri Olsabc84f4642015-02-17 17:31:18 +0100797
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300798 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100799 if (dso == NULL)
800 goto out;
801
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300802 map = map__new2(start, dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300803 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100804 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300805
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300806 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100807
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900808 /* Put the map here because map_groups__insert alread got it */
809 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100810out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900811 /* put the dso here, corresponding to machine__findnew_module_dso */
812 dso__put(dso);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300813 zfree(&m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300814 return map;
815}
816
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300817size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300818{
819 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300820 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300821
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800822 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300823 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300824 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300825 }
826
827 return ret;
828}
829
Waiman Long8fa7d872014-09-29 16:07:28 -0400830size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300831 bool (skip)(struct dso *dso, int parm), int parm)
832{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300833 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300834}
835
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300836size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300837 bool (skip)(struct dso *dso, int parm), int parm)
838{
839 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300840 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300841
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800842 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300843 struct machine *pos = rb_entry(nd, struct machine, rb_node);
844 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
845 }
846 return ret;
847}
848
849size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
850{
851 int i;
852 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300853 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300854
855 if (kdso->has_build_id) {
856 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700857 if (dso__build_id_filename(kdso, filename, sizeof(filename),
858 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300859 printed += fprintf(fp, "[0] %s\n", filename);
860 }
861
862 for (i = 0; i < vmlinux_path__nr_entries; ++i)
863 printed += fprintf(fp, "[%d] %s\n",
864 i + kdso->has_build_id, vmlinux_path[i]);
865
866 return printed;
867}
868
869size_t machine__fprintf(struct machine *machine, FILE *fp)
870{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300871 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700872 size_t ret;
873 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300874
Kan Liang91e467b2017-09-10 19:23:14 -0700875 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
876 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300877
878 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300879
Kan Liang91e467b2017-09-10 19:23:14 -0700880 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300881
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800882 for (nd = rb_first_cached(&threads->entries); nd;
883 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700884 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300885
Kan Liang91e467b2017-09-10 19:23:14 -0700886 ret += thread__fprintf(pos, fp);
887 }
888
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300889 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300890 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300891 return ret;
892}
893
894static struct dso *machine__get_kernel(struct machine *machine)
895{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100896 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300897 struct dso *kernel;
898
899 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +0100900 if (symbol_conf.vmlinux_name)
901 vmlinux_name = symbol_conf.vmlinux_name;
902
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300903 kernel = machine__findnew_kernel(machine, vmlinux_name,
904 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300905 } else {
Jiri Olsac1925242018-03-12 16:24:06 +0100906 if (symbol_conf.default_guest_vmlinux_name)
907 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
908
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300909 kernel = machine__findnew_kernel(machine, vmlinux_name,
910 "[guest.kernel]",
911 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300912 }
913
914 if (kernel != NULL && (!kernel->has_build_id))
915 dso__read_running_kernel_build_id(kernel, machine);
916
917 return kernel;
918}
919
920struct process_args {
921 u64 start;
922};
923
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300924void machine__get_kallsyms_filename(struct machine *machine, char *buf,
925 size_t bufsz)
Adrian Hunter15a0a872014-01-29 16:14:38 +0200926{
927 if (machine__is_default_guest(machine))
928 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
929 else
930 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
931}
932
Simon Quea93f0e52014-06-16 11:32:09 -0700933const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
934
935/* Figure out the start address of kernel map from /proc/kallsyms.
936 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
937 * symbol_name if it's not that important.
938 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300939static int machine__get_running_kernel_start(struct machine *machine,
Jiri Olsaed9adb22019-05-08 15:20:03 +0200940 const char **symbol_name,
941 u64 *start, u64 *end)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300942{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200943 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300944 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700945 const char *name;
946 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300947
Adrian Hunter15a0a872014-01-29 16:14:38 +0200948 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300949
950 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
951 return 0;
952
Simon Quea93f0e52014-06-16 11:32:09 -0700953 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300954 err = kallsyms__get_function_start(filename, name, &addr);
955 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700956 break;
957 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300958
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300959 if (err)
960 return -1;
961
Simon Quea93f0e52014-06-16 11:32:09 -0700962 if (symbol_name)
963 *symbol_name = name;
964
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300965 *start = addr;
Jiri Olsaed9adb22019-05-08 15:20:03 +0200966
967 err = kallsyms__get_function_start(filename, "_etext", &addr);
968 if (!err)
969 *end = addr;
970
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300971 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300972}
973
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300974int machine__create_extra_kernel_map(struct machine *machine,
975 struct dso *kernel,
976 struct extra_kernel_map *xm)
Adrian Hunter4d99e4132018-05-22 13:54:33 +0300977{
978 struct kmap *kmap;
979 struct map *map;
980
981 map = map__new2(xm->start, kernel);
982 if (!map)
983 return -1;
984
985 map->end = xm->end;
986 map->pgoff = xm->pgoff;
987
988 kmap = map__kmap(map);
989
990 kmap->kmaps = &machine->kmaps;
Adrian Hunter5759a682018-05-22 13:54:35 +0300991 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
Adrian Hunter4d99e4132018-05-22 13:54:33 +0300992
993 map_groups__insert(&machine->kmaps, map);
994
Adrian Hunter5759a682018-05-22 13:54:35 +0300995 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
996 kmap->name, map->start, map->end);
Adrian Hunter4d99e4132018-05-22 13:54:33 +0300997
998 map__put(map);
999
1000 return 0;
1001}
1002
1003static u64 find_entry_trampoline(struct dso *dso)
1004{
1005 /* Duplicates are removed so lookup all aliases */
1006 const char *syms[] = {
1007 "_entry_trampoline",
1008 "__entry_trampoline_start",
1009 "entry_SYSCALL_64_trampoline",
1010 };
1011 struct symbol *sym = dso__first_symbol(dso);
1012 unsigned int i;
1013
1014 for (; sym; sym = dso__next_symbol(sym)) {
1015 if (sym->binding != STB_GLOBAL)
1016 continue;
1017 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1018 if (!strcmp(sym->name, syms[i]))
1019 return sym->start;
1020 }
1021 }
1022
1023 return 0;
1024}
1025
1026/*
1027 * These values can be used for kernels that do not have symbols for the entry
1028 * trampolines in kallsyms.
1029 */
1030#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1031#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1032#define X86_64_ENTRY_TRAMPOLINE 0x6000
1033
1034/* Map x86_64 PTI entry trampolines */
1035int machine__map_x86_64_entry_trampolines(struct machine *machine,
1036 struct dso *kernel)
1037{
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001038 struct map_groups *kmaps = &machine->kmaps;
1039 struct maps *maps = &kmaps->maps;
Adrian Hunter4d99e4132018-05-22 13:54:33 +03001040 int nr_cpus_avail, cpu;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001041 bool found = false;
1042 struct map *map;
1043 u64 pgoff;
Adrian Hunter4d99e4132018-05-22 13:54:33 +03001044
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001045 /*
1046 * In the vmlinux case, pgoff is a virtual address which must now be
1047 * mapped to a vmlinux offset.
1048 */
1049 for (map = maps__first(maps); map; map = map__next(map)) {
1050 struct kmap *kmap = __map__kmap(map);
1051 struct map *dest_map;
1052
1053 if (!kmap || !is_entry_trampoline(kmap->name))
1054 continue;
1055
1056 dest_map = map_groups__find(kmaps, map->pgoff);
1057 if (dest_map != map)
1058 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1059 found = true;
1060 }
1061 if (found || machine->trampolines_mapped)
1062 return 0;
1063
1064 pgoff = find_entry_trampoline(kernel);
Adrian Hunter4d99e4132018-05-22 13:54:33 +03001065 if (!pgoff)
1066 return 0;
1067
1068 nr_cpus_avail = machine__nr_cpus_avail(machine);
1069
1070 /* Add a 1 page map for each CPU's entry trampoline */
1071 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1072 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1073 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1074 X86_64_ENTRY_TRAMPOLINE;
1075 struct extra_kernel_map xm = {
1076 .start = va,
1077 .end = va + page_size,
1078 .pgoff = pgoff,
1079 };
1080
Adrian Hunter5759a682018-05-22 13:54:35 +03001081 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1082
Adrian Hunter4d99e4132018-05-22 13:54:33 +03001083 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1084 return -1;
1085 }
1086
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001087 machine->trampolines_mapped = nr_cpus_avail;
1088
1089 return 0;
1090}
1091
1092int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1093 struct dso *kernel __maybe_unused)
1094{
Adrian Hunter4d99e4132018-05-22 13:54:33 +03001095 return 0;
1096}
1097
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001098static int
1099__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001100{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001101 struct kmap *kmap;
1102 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001103
Masami Hiramatsucc1121a2015-12-09 11:11:33 +09001104 /* In case of renewal the kernel map, destroy previous one */
1105 machine__destroy_kernel_maps(machine);
1106
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001107 machine->vmlinux_map = map__new2(0, kernel);
1108 if (machine->vmlinux_map == NULL)
1109 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001110
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001111 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1112 map = machine__kernel_map(machine);
1113 kmap = map__kmap(map);
1114 if (!kmap)
1115 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001116
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001117 kmap->kmaps = &machine->kmaps;
1118 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001119
1120 return 0;
1121}
1122
1123void machine__destroy_kernel_maps(struct machine *machine)
1124{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001125 struct kmap *kmap;
1126 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001127
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001128 if (map == NULL)
1129 return;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001130
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001131 kmap = map__kmap(map);
1132 map_groups__remove(&machine->kmaps, map);
1133 if (kmap && kmap->ref_reloc_sym) {
1134 zfree((char **)&kmap->ref_reloc_sym->name);
1135 zfree(&kmap->ref_reloc_sym);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001136 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001137
1138 map__zput(machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001139}
1140
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001141int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001142{
1143 int ret = 0;
1144 struct dirent **namelist = NULL;
1145 int i, items = 0;
1146 char path[PATH_MAX];
1147 pid_t pid;
1148 char *endp;
1149
1150 if (symbol_conf.default_guest_vmlinux_name ||
1151 symbol_conf.default_guest_modules ||
1152 symbol_conf.default_guest_kallsyms) {
1153 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1154 }
1155
1156 if (symbol_conf.guestmount) {
1157 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1158 if (items <= 0)
1159 return -ENOENT;
1160 for (i = 0; i < items; i++) {
1161 if (!isdigit(namelist[i]->d_name[0])) {
1162 /* Filter out . and .. */
1163 continue;
1164 }
1165 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1166 if ((*endp != '\0') ||
1167 (endp == namelist[i]->d_name) ||
1168 (errno == ERANGE)) {
1169 pr_debug("invalid directory (%s). Skipping.\n",
1170 namelist[i]->d_name);
1171 continue;
1172 }
1173 sprintf(path, "%s/%s/proc/kallsyms",
1174 symbol_conf.guestmount,
1175 namelist[i]->d_name);
1176 ret = access(path, R_OK);
1177 if (ret) {
1178 pr_debug("Can't access file %s\n", path);
1179 goto failure;
1180 }
1181 machines__create_kernel_maps(machines, pid);
1182 }
1183failure:
1184 free(namelist);
1185 }
1186
1187 return ret;
1188}
1189
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001190void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001191{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001192 struct rb_node *next = rb_first_cached(&machines->guests);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001193
1194 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001195
1196 while (next) {
1197 struct machine *pos = rb_entry(next, struct machine, rb_node);
1198
1199 next = rb_next(&pos->rb_node);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001200 rb_erase_cached(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001201 machine__delete(pos);
1202 }
1203}
1204
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001205int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001206{
1207 struct machine *machine = machines__findnew(machines, pid);
1208
1209 if (machine == NULL)
1210 return -1;
1211
1212 return machine__create_kernel_maps(machine);
1213}
1214
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001215int machine__load_kallsyms(struct machine *machine, const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001216{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001217 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +01001218 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001219
1220 if (ret > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001221 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001222 /*
1223 * Since /proc/kallsyms will have multiple sessions for the
1224 * kernel, with modules between them, fixup the end of all
1225 * sections.
1226 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001227 map_groups__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001228 }
1229
1230 return ret;
1231}
1232
Arnaldo Carvalho de Melo1d1a2652018-04-25 12:18:11 -03001233int machine__load_vmlinux_path(struct machine *machine)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001234{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001235 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001236 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001237
Adrian Hunter39b12f782013-08-07 14:38:47 +03001238 if (ret > 0)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001239 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001240
1241 return ret;
1242}
1243
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001244static char *get_kernel_version(const char *root_dir)
1245{
1246 char version[PATH_MAX];
1247 FILE *file;
1248 char *name, *tmp;
1249 const char *prefix = "Linux version ";
1250
1251 sprintf(version, "%s/proc/version", root_dir);
1252 file = fopen(version, "r");
1253 if (!file)
1254 return NULL;
1255
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001256 tmp = fgets(version, sizeof(version), file);
1257 fclose(file);
Donald Yandt34b65af2019-05-28 09:41:28 -04001258 if (!tmp)
1259 return NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001260
1261 name = strstr(version, prefix);
1262 if (!name)
1263 return NULL;
1264 name += strlen(prefix);
1265 tmp = strchr(name, ' ');
1266 if (tmp)
1267 *tmp = '\0';
1268
1269 return strdup(name);
1270}
1271
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001272static bool is_kmod_dso(struct dso *dso)
1273{
1274 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1275 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1276}
1277
1278static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1279 struct kmod_path *m)
1280{
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001281 char *long_name;
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -03001282 struct map *map = map_groups__find_by_name(mg, m->name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001283
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001284 if (map == NULL)
1285 return 0;
1286
1287 long_name = strdup(path);
1288 if (long_name == NULL)
1289 return -ENOMEM;
1290
1291 dso__set_long_name(map->dso, long_name, true);
1292 dso__kernel_module_get_build_id(map->dso, "");
1293
1294 /*
1295 * Full name could reveal us kmod compression, so
1296 * we need to update the symtab_type if needed.
1297 */
Jiri Olsa2af524752018-08-17 11:48:07 +02001298 if (m->comp && is_kmod_dso(map->dso)) {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001299 map->dso->symtab_type++;
Jiri Olsa2af524752018-08-17 11:48:07 +02001300 map->dso->comp = m->comp;
1301 }
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001302
1303 return 0;
1304}
1305
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001306static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001307 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001308{
1309 struct dirent *dent;
1310 DIR *dir = opendir(dir_name);
1311 int ret = 0;
1312
1313 if (!dir) {
1314 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1315 return -1;
1316 }
1317
1318 while ((dent = readdir(dir)) != NULL) {
1319 char path[PATH_MAX];
1320 struct stat st;
1321
1322 /*sshfs might return bad dent->d_type, so we have to stat*/
1323 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1324 if (stat(path, &st))
1325 continue;
1326
1327 if (S_ISDIR(st.st_mode)) {
1328 if (!strcmp(dent->d_name, ".") ||
1329 !strcmp(dent->d_name, ".."))
1330 continue;
1331
Richard Yao61d42902014-04-26 13:17:55 -04001332 /* Do not follow top-level source and build symlinks */
1333 if (depth == 0) {
1334 if (!strcmp(dent->d_name, "source") ||
1335 !strcmp(dent->d_name, "build"))
1336 continue;
1337 }
1338
1339 ret = map_groups__set_modules_path_dir(mg, path,
1340 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001341 if (ret < 0)
1342 goto out;
1343 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001344 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001345
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001346 ret = kmod_path__parse_name(&m, dent->d_name);
1347 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001348 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001349
1350 if (m.kmod)
1351 ret = map_groups__set_module_path(mg, path, &m);
1352
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001353 zfree(&m.name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001354
1355 if (ret)
1356 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001357 }
1358 }
1359
1360out:
1361 closedir(dir);
1362 return ret;
1363}
1364
1365static int machine__set_modules_path(struct machine *machine)
1366{
1367 char *version;
1368 char modules_path[PATH_MAX];
1369
1370 version = get_kernel_version(machine->root_dir);
1371 if (!version)
1372 return -1;
1373
Richard Yao61d42902014-04-26 13:17:55 -04001374 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001375 machine->root_dir, version);
1376 free(version);
1377
Richard Yao61d42902014-04-26 13:17:55 -04001378 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001379}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001380int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
Thomas Richter12a6d292019-07-24 14:27:02 +02001381 u64 *size __maybe_unused,
Song Shan Gong203d8a42016-07-21 11:10:51 +08001382 const char *name __maybe_unused)
1383{
1384 return 0;
1385}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001386
Thomas Richter9ad46522017-08-03 15:49:02 +02001387static int machine__create_module(void *arg, const char *name, u64 start,
1388 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001389{
1390 struct machine *machine = arg;
1391 struct map *map;
1392
Thomas Richter12a6d292019-07-24 14:27:02 +02001393 if (arch__fix_module_text_start(&start, &size, name) < 0)
Song Shan Gong203d8a42016-07-21 11:10:51 +08001394 return -1;
1395
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001396 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001397 if (map == NULL)
1398 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001399 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001400
1401 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1402
1403 return 0;
1404}
1405
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001406static int machine__create_modules(struct machine *machine)
1407{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001408 const char *modules;
1409 char path[PATH_MAX];
1410
Adrian Hunterf4be9042013-09-22 13:22:09 +03001411 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001412 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001413 } else {
1414 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001415 modules = path;
1416 }
1417
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001418 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001419 return -1;
1420
Adrian Hunter316d70d2013-10-08 11:45:48 +03001421 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001422 return -1;
1423
Adrian Hunter316d70d2013-10-08 11:45:48 +03001424 if (!machine__set_modules_path(machine))
1425 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001426
Adrian Hunter316d70d2013-10-08 11:45:48 +03001427 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001428
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001429 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001430}
1431
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001432static void machine__set_kernel_mmap(struct machine *machine,
1433 u64 start, u64 end)
1434{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001435 machine->vmlinux_map->start = start;
1436 machine->vmlinux_map->end = end;
1437 /*
1438 * Be a bit paranoid here, some perf.data file came with
1439 * a zero sized synthesized MMAP event for the kernel.
1440 */
1441 if (start == 0 && end == 0)
1442 machine->vmlinux_map->end = ~0ULL;
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001443}
1444
Wei Li977c7a62019-02-28 17:20:03 +08001445static void machine__update_kernel_mmap(struct machine *machine,
1446 u64 start, u64 end)
1447{
1448 struct map *map = machine__kernel_map(machine);
1449
1450 map__get(map);
1451 map_groups__remove(&machine->kmaps, map);
1452
1453 machine__set_kernel_mmap(machine, start, end);
1454
1455 map_groups__insert(&machine->kmaps, map);
1456 map__put(map);
1457}
1458
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001459int machine__create_kernel_maps(struct machine *machine)
1460{
1461 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001462 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001463 struct map *map;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001464 u64 start = 0, end = ~0ULL;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001465 int ret;
1466
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001467 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001468 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001469
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001470 ret = __machine__create_kernel_maps(machine, kernel);
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001471 if (ret < 0)
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001472 goto out_put;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001473
1474 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1475 if (machine__is_host(machine))
1476 pr_debug("Problems creating module maps, "
1477 "continuing anyway...\n");
1478 else
1479 pr_debug("Problems creating module maps for guest %d, "
1480 "continuing anyway...\n", machine->pid);
1481 }
1482
Jiri Olsaed9adb22019-05-08 15:20:03 +02001483 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001484 if (name &&
Jiri Olsaed9adb22019-05-08 15:20:03 +02001485 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001486 machine__destroy_kernel_maps(machine);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001487 ret = -1;
1488 goto out_put;
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001489 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001490
Wei Li977c7a62019-02-28 17:20:03 +08001491 /*
1492 * we have a real start address now, so re-order the kmaps
1493 * assume it's the last in the kmaps
1494 */
Jiri Olsaed9adb22019-05-08 15:20:03 +02001495 machine__update_kernel_mmap(machine, start, end);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001496 }
1497
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001498 if (machine__create_extra_kernel_maps(machine, kernel))
1499 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1500
Jiri Olsaed9adb22019-05-08 15:20:03 +02001501 if (end == ~0ULL) {
1502 /* update end address of the kernel map using adjacent module address */
1503 map = map__next(machine__kernel_map(machine));
1504 if (map)
1505 machine__set_kernel_mmap(machine, start, map->start);
1506 }
1507
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001508out_put:
1509 dso__put(kernel);
1510 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001511}
1512
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001513static bool machine__uses_kcore(struct machine *machine)
1514{
1515 struct dso *dso;
1516
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001517 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001518 if (dso__is_kcore(dso))
1519 return true;
1520 }
1521
1522 return false;
1523}
1524
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001525static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1526 union perf_event *event)
1527{
1528 return machine__is(machine, "x86_64") &&
1529 is_entry_trampoline(event->mmap.filename);
1530}
1531
1532static int machine__process_extra_kernel_map(struct machine *machine,
1533 union perf_event *event)
1534{
1535 struct map *kernel_map = machine__kernel_map(machine);
1536 struct dso *kernel = kernel_map ? kernel_map->dso : NULL;
1537 struct extra_kernel_map xm = {
1538 .start = event->mmap.start,
1539 .end = event->mmap.start + event->mmap.len,
1540 .pgoff = event->mmap.pgoff,
1541 };
1542
1543 if (kernel == NULL)
1544 return -1;
1545
1546 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1547
1548 return machine__create_extra_kernel_map(machine, kernel, &xm);
1549}
1550
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001551static int machine__process_kernel_mmap_event(struct machine *machine,
1552 union perf_event *event)
1553{
1554 struct map *map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001555 enum dso_kernel_type kernel_type;
1556 bool is_kernel_mmap;
1557
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001558 /* If we have maps from kcore then we do not need or want any others */
1559 if (machine__uses_kcore(machine))
1560 return 0;
1561
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001562 if (machine__is_host(machine))
1563 kernel_type = DSO_TYPE_KERNEL;
1564 else
1565 kernel_type = DSO_TYPE_GUEST_KERNEL;
1566
1567 is_kernel_mmap = memcmp(event->mmap.filename,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001568 machine->mmap_name,
1569 strlen(machine->mmap_name) - 1) == 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001570 if (event->mmap.filename[0] == '/' ||
1571 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001572 map = machine__findnew_module_map(machine, event->mmap.start,
1573 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001574 if (map == NULL)
1575 goto out_problem;
1576
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001577 map->end = map->start + event->mmap.len;
1578 } else if (is_kernel_mmap) {
1579 const char *symbol_name = (event->mmap.filename +
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001580 strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001581 /*
1582 * Should be there already, from the build-id table in
1583 * the header.
1584 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001585 struct dso *kernel = NULL;
1586 struct dso *dso;
1587
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001588 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001589
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001590 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001591
1592 /*
1593 * The cpumode passed to is_kernel_module is not the
1594 * cpumode of *this* event. If we insist on passing
1595 * correct cpumode to is_kernel_module, we should
1596 * record the cpumode when we adding this dso to the
1597 * linked list.
1598 *
1599 * However we don't really need passing correct
1600 * cpumode. We know the correct cpumode must be kernel
1601 * mode (if not, we should not link it onto kernel_dsos
1602 * list).
1603 *
1604 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1605 * is_kernel_module() treats it as a kernel cpumode.
1606 */
1607
1608 if (!dso->kernel ||
1609 is_kernel_module(dso->long_name,
1610 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001611 continue;
1612
Wang Nan1f121b02015-06-03 08:52:21 +00001613
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001614 kernel = dso;
1615 break;
1616 }
1617
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001618 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001619
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001620 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001621 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001622 if (kernel == NULL)
1623 goto out_problem;
1624
1625 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001626 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1627 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001628 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001629 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001630
Namhyung Kim330dfa22014-11-18 13:30:28 +09001631 if (strstr(kernel->long_name, "vmlinux"))
1632 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001633
Wei Li977c7a62019-02-28 17:20:03 +08001634 machine__update_kernel_mmap(machine, event->mmap.start,
Jiri Olsa05db6ff2018-02-15 13:26:31 +01001635 event->mmap.start + event->mmap.len);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001636
1637 /*
1638 * Avoid using a zero address (kptr_restrict) for the ref reloc
1639 * symbol. Effectively having zero here means that at record
1640 * time /proc/sys/kernel/kptr_restrict was non zero.
1641 */
1642 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001643 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1644 symbol_name,
1645 event->mmap.pgoff);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001646 }
1647
1648 if (machine__is_default_guest(machine)) {
1649 /*
1650 * preload dso of guest kernel and modules
1651 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001652 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001653 }
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001654 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1655 return machine__process_extra_kernel_map(machine, event);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001656 }
1657 return 0;
1658out_problem:
1659 return -1;
1660}
1661
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001662int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001663 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001664 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001665{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001666 struct thread *thread;
1667 struct map *map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001668 int ret = 0;
1669
1670 if (dump_trace)
1671 perf_event__fprintf_mmap2(event, stdout);
1672
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001673 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1674 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001675 ret = machine__process_kernel_mmap_event(machine, event);
1676 if (ret < 0)
1677 goto out_problem;
1678 return 0;
1679 }
1680
1681 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001682 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001683 if (thread == NULL)
1684 goto out_problem;
1685
Adrian Hunter2a030682014-07-22 16:17:53 +03001686 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001687 event->mmap2.len, event->mmap2.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001688 event->mmap2.maj,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001689 event->mmap2.min, event->mmap2.ino,
1690 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001691 event->mmap2.prot,
1692 event->mmap2.flags,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001693 event->mmap2.filename, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001694
1695 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001696 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001697
He Kuang8132a2a2016-06-03 03:33:13 +00001698 ret = thread__insert_map(thread, map);
1699 if (ret)
1700 goto out_problem_insert;
1701
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001702 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001703 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001704 return 0;
1705
He Kuang8132a2a2016-06-03 03:33:13 +00001706out_problem_insert:
1707 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001708out_problem_map:
1709 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001710out_problem:
1711 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1712 return 0;
1713}
1714
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001715int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001716 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001717{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001718 struct thread *thread;
1719 struct map *map;
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001720 u32 prot = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001721 int ret = 0;
1722
1723 if (dump_trace)
1724 perf_event__fprintf_mmap(event, stdout);
1725
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001726 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1727 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001728 ret = machine__process_kernel_mmap_event(machine, event);
1729 if (ret < 0)
1730 goto out_problem;
1731 return 0;
1732 }
1733
Adrian Hunter314add62013-08-27 11:23:03 +03001734 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001735 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001736 if (thread == NULL)
1737 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001738
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001739 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001740 prot = PROT_EXEC;
Stephane Eranianbad40912013-01-24 16:10:40 +01001741
Adrian Hunter2a030682014-07-22 16:17:53 +03001742 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001743 event->mmap.len, event->mmap.pgoff,
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001744 0, 0, 0, 0, prot, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001745 event->mmap.filename,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001746 thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001747
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001748 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001749 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001750
He Kuang8132a2a2016-06-03 03:33:13 +00001751 ret = thread__insert_map(thread, map);
1752 if (ret)
1753 goto out_problem_insert;
1754
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001755 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001756 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001757 return 0;
1758
He Kuang8132a2a2016-06-03 03:33:13 +00001759out_problem_insert:
1760 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001761out_problem_map:
1762 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001763out_problem:
1764 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1765 return 0;
1766}
1767
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001768static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001769{
Kan Liang91e467b2017-09-10 19:23:14 -07001770 struct threads *threads = machine__threads(machine, th->tid);
1771
1772 if (threads->last_match == th)
Jiri Olsa67fda0f2018-07-19 16:33:43 +02001773 threads__set_last_match(threads, NULL);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001774
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001775 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001776 down_write(&threads->lock);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001777
1778 BUG_ON(refcount_read(&th->refcnt) == 0);
1779
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001780 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001781 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001782 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001783 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001784 * Move it first to the dead_threads list, then drop the reference,
1785 * if this is the last reference, then the thread__delete destructor
1786 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001787 */
Kan Liang91e467b2017-09-10 19:23:14 -07001788 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001789
1790 /*
1791 * We need to do the put here because if this is the last refcount,
1792 * then we will be touching the threads->dead head when removing the
1793 * thread.
1794 */
1795 thread__put(th);
1796
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001797 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001798 up_write(&threads->lock);
David Ahern236a3bb2013-08-14 08:49:27 -06001799}
1800
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001801void machine__remove_thread(struct machine *machine, struct thread *th)
1802{
1803 return __machine__remove_thread(machine, th, true);
1804}
1805
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001806int machine__process_fork_event(struct machine *machine, union perf_event *event,
1807 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001808{
Jiri Olsad75e6092014-03-14 15:00:03 +01001809 struct thread *thread = machine__find_thread(machine,
1810 event->fork.pid,
1811 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001812 struct thread *parent = machine__findnew_thread(machine,
1813 event->fork.ppid,
1814 event->fork.ptid);
David Miller4f8f3822018-10-30 22:24:04 -07001815 bool do_maps_clone = true;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001816 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001817
Adrian Hunter5cb73342015-08-19 17:29:20 +03001818 if (dump_trace)
1819 perf_event__fprintf_task(event, stdout);
1820
1821 /*
1822 * There may be an existing thread that is not actually the parent,
1823 * either because we are processing events out of order, or because the
1824 * (fork) event that would have removed the thread was lost. Assume the
1825 * latter case and continue on as best we can.
1826 */
1827 if (parent->pid_ != (pid_t)event->fork.ppid) {
1828 dump_printf("removing erroneous parent thread %d/%d\n",
1829 parent->pid_, parent->tid);
1830 machine__remove_thread(machine, parent);
1831 thread__put(parent);
1832 parent = machine__findnew_thread(machine, event->fork.ppid,
1833 event->fork.ptid);
1834 }
1835
David Ahern236a3bb2013-08-14 08:49:27 -06001836 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001837 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001838 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001839 thread__put(thread);
1840 }
David Ahern236a3bb2013-08-14 08:49:27 -06001841
Adrian Hunter314add62013-08-27 11:23:03 +03001842 thread = machine__findnew_thread(machine, event->fork.pid,
1843 event->fork.tid);
David Miller4f8f3822018-10-30 22:24:04 -07001844 /*
1845 * When synthesizing FORK events, we are trying to create thread
1846 * objects for the already running tasks on the machine.
1847 *
1848 * Normally, for a kernel FORK event, we want to clone the parent's
1849 * maps because that is what the kernel just did.
1850 *
1851 * But when synthesizing, this should not be done. If we do, we end up
1852 * with overlapping maps as we process the sythesized MMAP2 events that
1853 * get delivered shortly thereafter.
1854 *
1855 * Use the FORK event misc flags in an internal way to signal this
1856 * situation, so we can elide the map clone when appropriate.
1857 */
1858 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1859 do_maps_clone = false;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001860
1861 if (thread == NULL || parent == NULL ||
David Miller4f8f3822018-10-30 22:24:04 -07001862 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001863 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001864 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001865 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001866 thread__put(thread);
1867 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001868
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001869 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001870}
1871
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001872int machine__process_exit_event(struct machine *machine, union perf_event *event,
1873 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001874{
Jiri Olsad75e6092014-03-14 15:00:03 +01001875 struct thread *thread = machine__find_thread(machine,
1876 event->fork.pid,
1877 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001878
1879 if (dump_trace)
1880 perf_event__fprintf_task(event, stdout);
1881
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001882 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001883 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001884 thread__put(thread);
1885 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001886
1887 return 0;
1888}
1889
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001890int machine__process_event(struct machine *machine, union perf_event *event,
1891 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001892{
1893 int ret;
1894
1895 switch (event->header.type) {
1896 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001897 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001898 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001899 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301900 case PERF_RECORD_NAMESPACES:
1901 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001902 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001903 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001904 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001905 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001906 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001907 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001908 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001909 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001910 case PERF_RECORD_AUX:
1911 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001912 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001913 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001914 case PERF_RECORD_LOST_SAMPLES:
1915 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001916 case PERF_RECORD_SWITCH:
1917 case PERF_RECORD_SWITCH_CPU_WIDE:
1918 ret = machine__process_switch_event(machine, event); break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08001919 case PERF_RECORD_KSYMBOL:
1920 ret = machine__process_ksymbol(machine, event, sample); break;
Song Liu45178a92019-01-17 08:15:18 -08001921 case PERF_RECORD_BPF_EVENT:
1922 ret = machine__process_bpf_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001923 default:
1924 ret = -1;
1925 break;
1926 }
1927
1928 return ret;
1929}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001930
Greg Priceb21484f2012-12-06 21:48:05 -08001931static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001932{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001933 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001934 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001935 return 0;
1936}
1937
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001938static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001939 struct addr_map_symbol *ams,
1940 u64 ip)
1941{
1942 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001943
1944 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001945 /*
1946 * We cannot use the header.misc hint to determine whether a
1947 * branch stack address is user, kernel, guest, hypervisor.
1948 * Branches may straddle the kernel/user/hypervisor boundaries.
1949 * Thus, we have to try consecutively until we find a match
1950 * or else, the symbol is unknown
1951 */
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03001952 thread__find_cpumode_addr_location(thread, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001953
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001954 ams->addr = ip;
1955 ams->al_addr = al.addr;
1956 ams->sym = al.sym;
1957 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001958 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001959}
1960
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001961static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001962 u8 m, struct addr_map_symbol *ams,
1963 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001964{
1965 struct addr_location al;
1966
1967 memset(&al, 0, sizeof(al));
1968
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03001969 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001970
Stephane Eranian98a3b322013-01-24 16:10:35 +01001971 ams->addr = addr;
1972 ams->al_addr = al.addr;
1973 ams->sym = al.sym;
1974 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001975 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001976}
1977
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001978struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1979 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001980{
Jiri Olsa9f874982018-03-07 16:50:06 +01001981 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01001982
1983 if (!mi)
1984 return NULL;
1985
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001986 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04001987 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1988 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001989 mi->data_src.val = sample->data_src;
1990
1991 return mi;
1992}
1993
Milian Wolff40a342c2017-10-09 22:32:56 +02001994static char *callchain_srcline(struct map *map, struct symbol *sym, u64 ip)
1995{
Milian Wolff21ac9d52017-10-19 13:38:34 +02001996 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02001997
Milian Wolff21ac9d52017-10-19 13:38:34 +02001998 if (!map || callchain_param.key == CCKEY_FUNCTION)
1999 return srcline;
2000
2001 srcline = srcline__tree_find(&map->dso->srclines, ip);
2002 if (!srcline) {
2003 bool show_sym = false;
2004 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2005
2006 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Jin Yao935f5a92017-12-30 00:26:52 +08002007 sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02002008 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2009 }
2010
2011 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02002012}
2013
Jin Yaoc4ee0622017-08-07 21:05:15 +08002014struct iterations {
2015 int nr_loop_iter;
2016 u64 cycles;
2017};
2018
Andi Kleen37592b82014-11-12 18:05:19 -08002019static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002020 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08002021 struct symbol **parent,
2022 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002023 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08002024 u64 ip,
2025 bool branch,
2026 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002027 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08002028 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08002029{
2030 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002031 int nr_loop_iter = 0;
2032 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002033 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08002034
2035 al.filtered = 0;
2036 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002037 if (!cpumode) {
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002038 thread__find_cpumode_addr_location(thread, ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002039 } else {
Kan Liang2e777842014-12-02 10:06:53 -05002040 if (ip >= PERF_CONTEXT_MAX) {
2041 switch (ip) {
2042 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002043 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05002044 break;
2045 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002046 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05002047 break;
2048 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002049 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05002050 break;
2051 default:
2052 pr_debug("invalid callchain context: "
2053 "%"PRId64"\n", (s64) ip);
2054 /*
2055 * It seems the callchain is corrupted.
2056 * Discard all.
2057 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002058 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05002059 return 1;
2060 }
2061 return 0;
2062 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002063 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05002064 }
2065
Andi Kleen37592b82014-11-12 18:05:19 -08002066 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02002067 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08002068 symbol__match_regex(al.sym, &parent_regex))
2069 *parent = al.sym;
2070 else if (have_ignore_callees && root_al &&
2071 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2072 /* Treat this symbol as the root,
2073 forgetting its callees. */
2074 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002075 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08002076 }
2077 }
2078
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002079 if (symbol_conf.hide_unresolved && al.sym == NULL)
2080 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002081
2082 if (iter) {
2083 nr_loop_iter = iter->nr_loop_iter;
2084 iter_cycles = iter->cycles;
2085 }
2086
Milian Wolff40a342c2017-10-09 22:32:56 +02002087 srcline = callchain_srcline(al.map, al.sym, al.addr);
Sandipan Das19610182018-05-17 12:03:25 +05302088 return callchain_cursor_append(cursor, ip, al.map, al.sym,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002089 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02002090 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08002091}
2092
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002093struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2094 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002095{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002096 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002097 const struct branch_stack *bs = sample->branch_stack;
2098 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002099
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002100 if (!bi)
2101 return NULL;
2102
2103 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002104 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2105 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002106 bi[i].flags = bs->entries[i].flags;
2107 }
2108 return bi;
2109}
2110
Jin Yaoc4ee0622017-08-07 21:05:15 +08002111static void save_iterations(struct iterations *iter,
2112 struct branch_entry *be, int nr)
2113{
2114 int i;
2115
Jin Yaoa3366db02019-01-04 14:10:30 +08002116 iter->nr_loop_iter++;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002117 iter->cycles = 0;
2118
2119 for (i = 0; i < nr; i++)
2120 iter->cycles += be[i].flags.cycles;
2121}
2122
Andi Kleen8b7bad52014-11-12 18:05:20 -08002123#define CHASHSZ 127
2124#define CHASHBITS 7
2125#define NO_ENTRY 0xff
2126
2127#define PERF_MAX_BRANCH_DEPTH 127
2128
2129/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08002130static int remove_loops(struct branch_entry *l, int nr,
2131 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08002132{
2133 int i, j, off;
2134 unsigned char chash[CHASHSZ];
2135
2136 memset(chash, NO_ENTRY, sizeof(chash));
2137
2138 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2139
2140 for (i = 0; i < nr; i++) {
2141 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2142
2143 /* no collision handling for now */
2144 if (chash[h] == NO_ENTRY) {
2145 chash[h] = i;
2146 } else if (l[chash[h]].from == l[i].from) {
2147 bool is_loop = true;
2148 /* check if it is a real loop */
2149 off = 0;
2150 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2151 if (l[j].from != l[i + off].from) {
2152 is_loop = false;
2153 break;
2154 }
2155 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002156 j = nr - (i + off);
2157 if (j > 0) {
2158 save_iterations(iter + i + off,
2159 l + i, off);
2160
2161 memmove(iter + i, iter + i + off,
2162 j * sizeof(*iter));
2163
2164 memmove(l + i, l + i + off,
2165 j * sizeof(*l));
2166 }
2167
Andi Kleen8b7bad52014-11-12 18:05:20 -08002168 nr -= off;
2169 }
2170 }
2171 }
2172 return nr;
2173}
2174
Kan Liang384b6052015-01-05 13:23:05 -05002175/*
2176 * Recolve LBR callstack chain sample
2177 * Return:
2178 * 1 on success get LBR callchain information
2179 * 0 no available LBR callchain information, should try fp
2180 * negative error code on other errors.
2181 */
2182static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002183 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002184 struct perf_sample *sample,
2185 struct symbol **parent,
2186 struct addr_location *root_al,
2187 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002188{
Kan Liang384b6052015-01-05 13:23:05 -05002189 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002190 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002191 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08002192 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05002193
2194 for (i = 0; i < chain_nr; i++) {
2195 if (chain->ips[i] == PERF_CONTEXT_USER)
2196 break;
2197 }
2198
2199 /* LBR only affects the user callchain */
2200 if (i != chain_nr) {
2201 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08002202 int lbr_nr = lbr_stack->nr, j, k;
2203 bool branch;
2204 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05002205 /*
2206 * LBR callstack can only get user call chain.
2207 * The mix_chain_nr is kernel call chain
2208 * number plus LBR user call chain number.
2209 * i is kernel call chain number,
2210 * 1 is PERF_CONTEXT_USER,
2211 * lbr_nr + 1 is the user call chain number.
2212 * For details, please refer to the comments
2213 * in callchain__printf
2214 */
2215 int mix_chain_nr = i + 1 + lbr_nr + 1;
2216
Kan Liang384b6052015-01-05 13:23:05 -05002217 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002218 int err;
Jin Yao410024d2016-10-31 09:19:49 +08002219 branch = false;
2220 flags = NULL;
2221
Kan Liang384b6052015-01-05 13:23:05 -05002222 if (callchain_param.order == ORDER_CALLEE) {
2223 if (j < i + 1)
2224 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08002225 else if (j > i + 1) {
2226 k = j - i - 2;
2227 ip = lbr_stack->entries[k].from;
2228 branch = true;
2229 flags = &lbr_stack->entries[k].flags;
2230 } else {
Kan Liang384b6052015-01-05 13:23:05 -05002231 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002232 branch = true;
2233 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002234 branch_from =
2235 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002236 }
Kan Liang384b6052015-01-05 13:23:05 -05002237 } else {
Jin Yao410024d2016-10-31 09:19:49 +08002238 if (j < lbr_nr) {
2239 k = lbr_nr - j - 1;
2240 ip = lbr_stack->entries[k].from;
2241 branch = true;
2242 flags = &lbr_stack->entries[k].flags;
2243 }
Kan Liang384b6052015-01-05 13:23:05 -05002244 else if (j > lbr_nr)
2245 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08002246 else {
Kan Liang384b6052015-01-05 13:23:05 -05002247 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002248 branch = true;
2249 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002250 branch_from =
2251 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002252 }
Kan Liang384b6052015-01-05 13:23:05 -05002253 }
2254
Jin Yao410024d2016-10-31 09:19:49 +08002255 err = add_callchain_ip(thread, cursor, parent,
2256 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002257 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08002258 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05002259 if (err)
2260 return (err < 0) ? err : 0;
2261 }
2262 return 1;
2263 }
2264
2265 return 0;
2266}
2267
David S. Millere9024d52018-10-30 12:12:26 -03002268static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2269 struct callchain_cursor *cursor,
2270 struct symbol **parent,
2271 struct addr_location *root_al,
2272 u8 *cpumode, int ent)
2273{
2274 int err = 0;
2275
2276 while (--ent >= 0) {
2277 u64 ip = chain->ips[ent];
2278
2279 if (ip >= PERF_CONTEXT_MAX) {
2280 err = add_callchain_ip(thread, cursor, parent,
2281 root_al, cpumode, ip,
2282 false, NULL, NULL, 0);
2283 break;
2284 }
2285 }
2286 return err;
2287}
2288
Kan Liang384b6052015-01-05 13:23:05 -05002289static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002290 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002291 struct perf_evsel *evsel,
2292 struct perf_sample *sample,
2293 struct symbol **parent,
2294 struct addr_location *root_al,
2295 int max_stack)
2296{
2297 struct branch_stack *branch = sample->branch_stack;
2298 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002299 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002300 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002301 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002302 int skip_idx = -1;
2303 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002304
Jin Yaob49a8212017-05-08 18:43:02 +08002305 if (chain)
2306 chain_nr = chain->nr;
2307
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03002308 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002309 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05002310 root_al, max_stack);
2311 if (err)
2312 return (err < 0) ? err : 0;
2313 }
2314
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002315 /*
2316 * Based on DWARF debug information, some architectures skip
2317 * a callchain entry saved by the kernel.
2318 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002319 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002320
Andi Kleen8b7bad52014-11-12 18:05:20 -08002321 /*
2322 * Add branches to call stack for easier browsing. This gives
2323 * more context for a sample than just the callers.
2324 *
2325 * This uses individual histograms of paths compared to the
2326 * aggregated histograms the normal LBR mode uses.
2327 *
2328 * Limitations for now:
2329 * - No extra filters
2330 * - No annotations (should annotate somehow)
2331 */
2332
2333 if (branch && callchain_param.branch_callstack) {
2334 int nr = min(max_stack, (int)branch->nr);
2335 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002336 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002337
2338 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2339 pr_warning("corrupted branch chain. skipping...\n");
2340 goto check_calls;
2341 }
2342
2343 for (i = 0; i < nr; i++) {
2344 if (callchain_param.order == ORDER_CALLEE) {
2345 be[i] = branch->entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002346
2347 if (chain == NULL)
2348 continue;
2349
Andi Kleen8b7bad52014-11-12 18:05:20 -08002350 /*
2351 * Check for overlap into the callchain.
2352 * The return address is one off compared to
2353 * the branch entry. To adjust for this
2354 * assume the calling instruction is not longer
2355 * than 8 bytes.
2356 */
2357 if (i == skip_idx ||
2358 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2359 first_call++;
2360 else if (be[i].from < chain->ips[first_call] &&
2361 be[i].from >= chain->ips[first_call] - 8)
2362 first_call++;
2363 } else
2364 be[i] = branch->entries[branch->nr - i - 1];
2365 }
2366
Jin Yaoc4ee0622017-08-07 21:05:15 +08002367 memset(iter, 0, sizeof(struct iterations) * nr);
2368 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002369
Andi Kleen8b7bad52014-11-12 18:05:20 -08002370 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002371 err = add_callchain_ip(thread, cursor, parent,
2372 root_al,
2373 NULL, be[i].to,
2374 true, &be[i].flags,
2375 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002376
Andi Kleen8b7bad52014-11-12 18:05:20 -08002377 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002378 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002379 NULL, be[i].from,
2380 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002381 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002382 if (err == -EINVAL)
2383 break;
2384 if (err)
2385 return err;
2386 }
Jin Yaob49a8212017-05-08 18:43:02 +08002387
2388 if (chain_nr == 0)
2389 return 0;
2390
Andi Kleen8b7bad52014-11-12 18:05:20 -08002391 chain_nr -= nr;
2392 }
2393
2394check_calls:
David S. Millere9024d52018-10-30 12:12:26 -03002395 if (callchain_param.order != ORDER_CALLEE) {
2396 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2397 &cpumode, chain->nr - first_call);
2398 if (err)
2399 return (err < 0) ? err : 0;
2400 }
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002401 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002402 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002403 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002404
2405 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002406 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002407 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002408 j = chain->nr - i - 1;
2409
2410#ifdef HAVE_SKIP_CALLCHAIN_IDX
2411 if (j == skip_idx)
2412 continue;
2413#endif
2414 ip = chain->ips[j];
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002415 if (ip < PERF_CONTEXT_MAX)
2416 ++nr_entries;
David S. Millere9024d52018-10-30 12:12:26 -03002417 else if (callchain_param.order != ORDER_CALLEE) {
2418 err = find_prev_cpumode(chain, thread, cursor, parent,
2419 root_al, &cpumode, j);
2420 if (err)
2421 return (err < 0) ? err : 0;
2422 continue;
2423 }
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002424
Jin Yao410024d2016-10-31 09:19:49 +08002425 err = add_callchain_ip(thread, cursor, parent,
2426 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002427 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002428
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002429 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002430 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002431 }
2432
2433 return 0;
2434}
2435
Milian Wolff11ea2512017-10-09 22:32:59 +02002436static int append_inlines(struct callchain_cursor *cursor,
2437 struct map *map, struct symbol *sym, u64 ip)
2438{
2439 struct inline_node *inline_node;
2440 struct inline_list *ilist;
2441 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002442 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002443
2444 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002445 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002446
Milian Wolff7a8a8fc2018-09-26 15:52:06 +02002447 addr = map__map_ip(map, ip);
2448 addr = map__rip_2objdump(map, addr);
Milian Wolff11ea2512017-10-09 22:32:59 +02002449
2450 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2451 if (!inline_node) {
2452 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2453 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002454 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002455 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2456 }
2457
2458 list_for_each_entry(ilist, &inline_node->val, list) {
Milian Wolffb38775c2017-10-19 13:38:33 +02002459 ret = callchain_cursor_append(cursor, ip, map,
2460 ilist->symbol, false,
2461 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002462
2463 if (ret != 0)
2464 return ret;
2465 }
2466
Milian Wolffb38775c2017-10-19 13:38:33 +02002467 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002468}
2469
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002470static int unwind_entry(struct unwind_entry *entry, void *arg)
2471{
2472 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002473 const char *srcline = NULL;
Milian Wolffff4ce282018-09-26 15:52:05 +02002474 u64 addr = entry->ip;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002475
2476 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2477 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002478
Milian Wolff11ea2512017-10-09 22:32:59 +02002479 if (append_inlines(cursor, entry->map, entry->sym, entry->ip) == 0)
2480 return 0;
2481
Sandipan Das2a9d5052018-07-03 17:35:55 +05302482 /*
2483 * Convert entry->ip from a virtual address to an offset in
2484 * its corresponding binary.
2485 */
Milian Wolffff4ce282018-09-26 15:52:05 +02002486 if (entry->map)
2487 addr = map__map_ip(entry->map, entry->ip);
Sandipan Das2a9d5052018-07-03 17:35:55 +05302488
2489 srcline = callchain_srcline(entry->map, entry->sym, addr);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002490 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002491 entry->map, entry->sym,
Milian Wolff40a342c2017-10-09 22:32:56 +02002492 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002493}
2494
Chris Phlipot9919a652016-04-28 01:19:06 -07002495static int thread__resolve_callchain_unwind(struct thread *thread,
2496 struct callchain_cursor *cursor,
2497 struct perf_evsel *evsel,
2498 struct perf_sample *sample,
2499 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002500{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002501 /* Can we do dwarf post unwind? */
2502 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2503 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2504 return 0;
2505
2506 /* Bail out if nothing was captured. */
2507 if ((!sample->user_regs.regs) ||
2508 (!sample->user_stack.size))
2509 return 0;
2510
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002511 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002512 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002513}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002514
Chris Phlipot9919a652016-04-28 01:19:06 -07002515int thread__resolve_callchain(struct thread *thread,
2516 struct callchain_cursor *cursor,
2517 struct perf_evsel *evsel,
2518 struct perf_sample *sample,
2519 struct symbol **parent,
2520 struct addr_location *root_al,
2521 int max_stack)
2522{
2523 int ret = 0;
2524
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002525 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07002526
2527 if (callchain_param.order == ORDER_CALLEE) {
2528 ret = thread__resolve_callchain_sample(thread, cursor,
2529 evsel, sample,
2530 parent, root_al,
2531 max_stack);
2532 if (ret)
2533 return ret;
2534 ret = thread__resolve_callchain_unwind(thread, cursor,
2535 evsel, sample,
2536 max_stack);
2537 } else {
2538 ret = thread__resolve_callchain_unwind(thread, cursor,
2539 evsel, sample,
2540 max_stack);
2541 if (ret)
2542 return ret;
2543 ret = thread__resolve_callchain_sample(thread, cursor,
2544 evsel, sample,
2545 parent, root_al,
2546 max_stack);
2547 }
2548
2549 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002550}
David Ahern35feee12013-09-28 13:12:58 -06002551
2552int machine__for_each_thread(struct machine *machine,
2553 int (*fn)(struct thread *thread, void *p),
2554 void *priv)
2555{
Kan Liang91e467b2017-09-10 19:23:14 -07002556 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002557 struct rb_node *nd;
2558 struct thread *thread;
2559 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002560 int i;
David Ahern35feee12013-09-28 13:12:58 -06002561
Kan Liang91e467b2017-09-10 19:23:14 -07002562 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2563 threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002564 for (nd = rb_first_cached(&threads->entries); nd;
2565 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -07002566 thread = rb_entry(nd, struct thread, rb_node);
2567 rc = fn(thread, priv);
2568 if (rc != 0)
2569 return rc;
2570 }
David Ahern35feee12013-09-28 13:12:58 -06002571
Kan Liang91e467b2017-09-10 19:23:14 -07002572 list_for_each_entry(thread, &threads->dead, node) {
2573 rc = fn(thread, priv);
2574 if (rc != 0)
2575 return rc;
2576 }
David Ahern35feee12013-09-28 13:12:58 -06002577 }
2578 return rc;
2579}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002580
Adrian Huntera5499b32015-05-29 16:33:30 +03002581int machines__for_each_thread(struct machines *machines,
2582 int (*fn)(struct thread *thread, void *p),
2583 void *priv)
2584{
2585 struct rb_node *nd;
2586 int rc = 0;
2587
2588 rc = machine__for_each_thread(&machines->host, fn, priv);
2589 if (rc != 0)
2590 return rc;
2591
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002592 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntera5499b32015-05-29 16:33:30 +03002593 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2594
2595 rc = machine__for_each_thread(machine, fn, priv);
2596 if (rc != 0)
2597 return rc;
2598 }
2599 return rc;
2600}
2601
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002602int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002603 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002604 perf_event__handler_t process, bool data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002605 unsigned int nr_threads_synthesize)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002606{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002607 if (target__has_task(target))
Mark Drayton3fcb10e2018-12-04 12:34:20 -08002608 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002609 else if (target__has_cpu(target))
Kan Liang340b47f2017-09-29 07:47:54 -07002610 return perf_event__synthesize_threads(tool, process,
2611 machine, data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002612 nr_threads_synthesize);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002613 /* command specified */
2614 return 0;
2615}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002616
2617pid_t machine__get_current_tid(struct machine *machine, int cpu)
2618{
2619 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2620 return -1;
2621
2622 return machine->current_tid[cpu];
2623}
2624
2625int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2626 pid_t tid)
2627{
2628 struct thread *thread;
2629
2630 if (cpu < 0)
2631 return -EINVAL;
2632
2633 if (!machine->current_tid) {
2634 int i;
2635
2636 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2637 if (!machine->current_tid)
2638 return -ENOMEM;
2639 for (i = 0; i < MAX_NR_CPUS; i++)
2640 machine->current_tid[i] = -1;
2641 }
2642
2643 if (cpu >= MAX_NR_CPUS) {
2644 pr_err("Requested CPU %d too large. ", cpu);
2645 pr_err("Consider raising MAX_NR_CPUS\n");
2646 return -EINVAL;
2647 }
2648
2649 machine->current_tid[cpu] = tid;
2650
2651 thread = machine__findnew_thread(machine, pid, tid);
2652 if (!thread)
2653 return -ENOMEM;
2654
2655 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002656 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002657
2658 return 0;
2659}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002660
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03002661/*
2662 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2663 * normalized arch is needed.
2664 */
2665bool machine__is(struct machine *machine, const char *arch)
2666{
2667 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2668}
2669
Adrian Hunter9cecca32018-05-22 13:54:32 +03002670int machine__nr_cpus_avail(struct machine *machine)
2671{
2672 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2673}
2674
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002675int machine__get_kernel_start(struct machine *machine)
2676{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002677 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002678 int err = 0;
2679
2680 /*
2681 * The only addresses above 2^63 are kernel addresses of a 64-bit
2682 * kernel. Note that addresses are unsigned so that on a 32-bit system
2683 * all addresses including kernel addresses are less than 2^32. In
2684 * that case (32-bit system), if the kernel mapping is unknown, all
2685 * addresses will be assumed to be in user space - see
2686 * machine__kernel_ip().
2687 */
2688 machine->kernel_start = 1ULL << 63;
2689 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002690 err = map__load(map);
Adrian Hunter19422a92018-05-17 12:21:54 +03002691 /*
2692 * On x86_64, PTI entry trampolines are less than the
2693 * start of kernel text, but still above 2^63. So leave
2694 * kernel_start = 1ULL << 63 for x86_64.
2695 */
2696 if (!err && !machine__is(machine, "x86_64"))
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002697 machine->kernel_start = map->start;
2698 }
2699 return err;
2700}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002701
Adrian Hunter8e80ad92018-11-06 23:07:10 +02002702u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2703{
2704 u8 addr_cpumode = cpumode;
2705 bool kernel_ip;
2706
2707 if (!machine->single_address_space)
2708 goto out;
2709
2710 kernel_ip = machine__kernel_ip(machine, addr);
2711 switch (cpumode) {
2712 case PERF_RECORD_MISC_KERNEL:
2713 case PERF_RECORD_MISC_USER:
2714 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2715 PERF_RECORD_MISC_USER;
2716 break;
2717 case PERF_RECORD_MISC_GUEST_KERNEL:
2718 case PERF_RECORD_MISC_GUEST_USER:
2719 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2720 PERF_RECORD_MISC_GUEST_USER;
2721 break;
2722 default:
2723 break;
2724 }
2725out:
2726 return addr_cpumode;
2727}
2728
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002729struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2730{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002731 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002732}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002733
2734char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2735{
2736 struct machine *machine = vmachine;
2737 struct map *map;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002738 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002739
2740 if (sym == NULL)
2741 return NULL;
2742
2743 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2744 *addrp = map->unmap_ip(map, sym->start);
2745 return sym->name;
2746}