blob: ea3e09f6a9c62562d0146cae135e5fe9c8a9a865 [file] [log] [blame]
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002#include "debug.h"
3#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03004#include "evsel.h"
5#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -03006#include "machine.h"
7#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03008#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03009#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030011#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030012#include <stdbool.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030013#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "unwind.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030015
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030016int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
17{
18 map_groups__init(&machine->kmaps);
19 RB_CLEAR_NODE(&machine->rb_node);
20 INIT_LIST_HEAD(&machine->user_dsos);
21 INIT_LIST_HEAD(&machine->kernel_dsos);
22
23 machine->threads = RB_ROOT;
24 INIT_LIST_HEAD(&machine->dead_threads);
25 machine->last_match = NULL;
26
Adrian Hunterd027b642014-07-23 14:23:00 +030027 machine->vdso_info = NULL;
28
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030029 machine->kmaps.machine = machine;
30 machine->pid = pid;
31
Adrian Hunter611a5ce2013-08-08 14:32:20 +030032 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010033 machine->id_hdr_size = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030034
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030035 machine->root_dir = strdup(root_dir);
36 if (machine->root_dir == NULL)
37 return -ENOMEM;
38
39 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030040 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030041 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030042 char comm[64];
43
44 if (thread == NULL)
45 return -ENOMEM;
46
47 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020048 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030049 }
50
Adrian Hunterb9d266b2014-07-22 16:17:25 +030051 machine->current_tid = NULL;
52
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030053 return 0;
54}
55
David Ahern8fb598e2013-09-28 13:13:00 -060056struct machine *machine__new_host(void)
57{
58 struct machine *machine = malloc(sizeof(*machine));
59
60 if (machine != NULL) {
61 machine__init(machine, "", HOST_KERNEL_ID);
62
63 if (machine__create_kernel_maps(machine) < 0)
64 goto out_delete;
65 }
66
67 return machine;
68out_delete:
69 free(machine);
70 return NULL;
71}
72
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030073static void dsos__delete(struct list_head *dsos)
74{
75 struct dso *pos, *n;
76
77 list_for_each_entry_safe(pos, n, dsos, node) {
78 list_del(&pos->node);
79 dso__delete(pos);
80 }
81}
82
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030083void machine__delete_dead_threads(struct machine *machine)
84{
85 struct thread *n, *t;
86
87 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
88 list_del(&t->node);
89 thread__delete(t);
90 }
91}
92
93void machine__delete_threads(struct machine *machine)
94{
95 struct rb_node *nd = rb_first(&machine->threads);
96
97 while (nd) {
98 struct thread *t = rb_entry(nd, struct thread, rb_node);
99
100 rb_erase(&t->rb_node, &machine->threads);
101 nd = rb_next(nd);
102 thread__delete(t);
103 }
104}
105
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300106void machine__exit(struct machine *machine)
107{
108 map_groups__exit(&machine->kmaps);
109 dsos__delete(&machine->user_dsos);
110 dsos__delete(&machine->kernel_dsos);
Adrian Hunterd027b642014-07-23 14:23:00 +0300111 vdso__exit(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300112 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300113 zfree(&machine->current_tid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300114}
115
116void machine__delete(struct machine *machine)
117{
118 machine__exit(machine);
119 free(machine);
120}
121
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300122void machines__init(struct machines *machines)
123{
124 machine__init(&machines->host, "", HOST_KERNEL_ID);
125 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300126 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300127}
128
129void machines__exit(struct machines *machines)
130{
131 machine__exit(&machines->host);
132 /* XXX exit guest */
133}
134
135struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300136 const char *root_dir)
137{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300138 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300139 struct rb_node *parent = NULL;
140 struct machine *pos, *machine = malloc(sizeof(*machine));
141
142 if (machine == NULL)
143 return NULL;
144
145 if (machine__init(machine, root_dir, pid) != 0) {
146 free(machine);
147 return NULL;
148 }
149
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300150 machine->symbol_filter = machines->symbol_filter;
151
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300152 while (*p != NULL) {
153 parent = *p;
154 pos = rb_entry(parent, struct machine, rb_node);
155 if (pid < pos->pid)
156 p = &(*p)->rb_left;
157 else
158 p = &(*p)->rb_right;
159 }
160
161 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300162 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300163
164 return machine;
165}
166
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300167void machines__set_symbol_filter(struct machines *machines,
168 symbol_filter_t symbol_filter)
169{
170 struct rb_node *nd;
171
172 machines->symbol_filter = symbol_filter;
173 machines->host.symbol_filter = symbol_filter;
174
175 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
176 struct machine *machine = rb_entry(nd, struct machine, rb_node);
177
178 machine->symbol_filter = symbol_filter;
179 }
180}
181
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300182struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300183{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300184 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185 struct rb_node *parent = NULL;
186 struct machine *machine;
187 struct machine *default_machine = NULL;
188
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300189 if (pid == HOST_KERNEL_ID)
190 return &machines->host;
191
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300192 while (*p != NULL) {
193 parent = *p;
194 machine = rb_entry(parent, struct machine, rb_node);
195 if (pid < machine->pid)
196 p = &(*p)->rb_left;
197 else if (pid > machine->pid)
198 p = &(*p)->rb_right;
199 else
200 return machine;
201 if (!machine->pid)
202 default_machine = machine;
203 }
204
205 return default_machine;
206}
207
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300208struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300209{
210 char path[PATH_MAX];
211 const char *root_dir = "";
212 struct machine *machine = machines__find(machines, pid);
213
214 if (machine && (machine->pid == pid))
215 goto out;
216
217 if ((pid != HOST_KERNEL_ID) &&
218 (pid != DEFAULT_GUEST_KERNEL_ID) &&
219 (symbol_conf.guestmount)) {
220 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
221 if (access(path, R_OK)) {
222 static struct strlist *seen;
223
224 if (!seen)
225 seen = strlist__new(true, NULL);
226
227 if (!strlist__has_entry(seen, path)) {
228 pr_err("Can't access file %s\n", path);
229 strlist__add(seen, path);
230 }
231 machine = NULL;
232 goto out;
233 }
234 root_dir = path;
235 }
236
237 machine = machines__add(machines, pid, root_dir);
238out:
239 return machine;
240}
241
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300242void machines__process_guests(struct machines *machines,
243 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300244{
245 struct rb_node *nd;
246
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300247 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300248 struct machine *pos = rb_entry(nd, struct machine, rb_node);
249 process(pos, data);
250 }
251}
252
253char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
254{
255 if (machine__is_host(machine))
256 snprintf(bf, size, "[%s]", "kernel.kallsyms");
257 else if (machine__is_default_guest(machine))
258 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
259 else {
260 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
261 machine->pid);
262 }
263
264 return bf;
265}
266
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300267void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300268{
269 struct rb_node *node;
270 struct machine *machine;
271
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300272 machines->host.id_hdr_size = id_hdr_size;
273
274 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300275 machine = rb_entry(node, struct machine, rb_node);
276 machine->id_hdr_size = id_hdr_size;
277 }
278
279 return;
280}
281
Adrian Hunter29ce3612014-07-16 11:07:13 +0300282static void machine__update_thread_pid(struct machine *machine,
283 struct thread *th, pid_t pid)
284{
285 struct thread *leader;
286
287 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
288 return;
289
290 th->pid_ = pid;
291
292 if (th->pid_ == th->tid)
293 return;
294
295 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
296 if (!leader)
297 goto out_err;
298
299 if (!leader->mg)
300 leader->mg = map_groups__new();
301
302 if (!leader->mg)
303 goto out_err;
304
305 if (th->mg == leader->mg)
306 return;
307
308 if (th->mg) {
309 /*
310 * Maps are created from MMAP events which provide the pid and
311 * tid. Consequently there never should be any maps on a thread
312 * with an unknown pid. Just print an error if there are.
313 */
314 if (!map_groups__empty(th->mg))
315 pr_err("Discarding thread maps for %d:%d\n",
316 th->pid_, th->tid);
317 map_groups__delete(th->mg);
318 }
319
320 th->mg = map_groups__get(leader->mg);
321
322 return;
323
324out_err:
325 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
326}
327
Adrian Hunter99d725f2013-08-26 16:00:19 +0300328static struct thread *__machine__findnew_thread(struct machine *machine,
329 pid_t pid, pid_t tid,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300330 bool create)
331{
332 struct rb_node **p = &machine->threads.rb_node;
333 struct rb_node *parent = NULL;
334 struct thread *th;
335
336 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300337 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300338 * so most of the time we dont have to look up
339 * the full rbtree:
340 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300341 th = machine->last_match;
342 if (th && th->tid == tid) {
343 machine__update_thread_pid(machine, th, pid);
344 return th;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300345 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300346
347 while (*p != NULL) {
348 parent = *p;
349 th = rb_entry(parent, struct thread, rb_node);
350
Adrian Hunter38051232013-07-04 16:20:31 +0300351 if (th->tid == tid) {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300352 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300353 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300354 return th;
355 }
356
Adrian Hunter38051232013-07-04 16:20:31 +0300357 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300358 p = &(*p)->rb_left;
359 else
360 p = &(*p)->rb_right;
361 }
362
363 if (!create)
364 return NULL;
365
Adrian Hunter99d725f2013-08-26 16:00:19 +0300366 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300367 if (th != NULL) {
368 rb_link_node(&th->rb_node, parent, p);
369 rb_insert_color(&th->rb_node, &machine->threads);
370 machine->last_match = th;
Jiri Olsacddcef62014-04-09 20:54:29 +0200371
372 /*
373 * We have to initialize map_groups separately
374 * after rb tree is updated.
375 *
376 * The reason is that we call machine__findnew_thread
377 * within thread__init_map_groups to find the thread
378 * leader and that would screwed the rb tree.
379 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300380 if (thread__init_map_groups(th, machine)) {
381 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200382 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300383 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300384 }
385
386 return th;
387}
388
Adrian Hunter314add62013-08-27 11:23:03 +0300389struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
390 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300391{
Adrian Hunter314add62013-08-27 11:23:03 +0300392 return __machine__findnew_thread(machine, pid, tid, true);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300393}
394
Jiri Olsad75e6092014-03-14 15:00:03 +0100395struct thread *machine__find_thread(struct machine *machine, pid_t pid,
396 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300397{
Jiri Olsad75e6092014-03-14 15:00:03 +0100398 return __machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300399}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300400
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200401int machine__process_comm_event(struct machine *machine, union perf_event *event,
402 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300403{
Adrian Hunter314add62013-08-27 11:23:03 +0300404 struct thread *thread = machine__findnew_thread(machine,
405 event->comm.pid,
406 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300407 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300408
409 if (dump_trace)
410 perf_event__fprintf_comm(event, stdout);
411
Adrian Hunter65de51f2014-07-31 09:00:44 +0300412 if (thread == NULL ||
413 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300414 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
415 return -1;
416 }
417
418 return 0;
419}
420
421int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200422 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300423{
424 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
425 event->lost.id, event->lost.lost);
426 return 0;
427}
428
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300429struct map *machine__new_module(struct machine *machine, u64 start,
430 const char *filename)
431{
432 struct map *map;
433 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
434
435 if (dso == NULL)
436 return NULL;
437
438 map = map__new2(start, dso, MAP__FUNCTION);
439 if (map == NULL)
440 return NULL;
441
442 if (machine__is_host(machine))
443 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
444 else
445 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
446 map_groups__insert(&machine->kmaps, map);
447 return map;
448}
449
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300450size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300451{
452 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300453 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
454 __dsos__fprintf(&machines->host.user_dsos, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300455
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300456 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300457 struct machine *pos = rb_entry(nd, struct machine, rb_node);
458 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
459 ret += __dsos__fprintf(&pos->user_dsos, fp);
460 }
461
462 return ret;
463}
464
465size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
466 bool (skip)(struct dso *dso, int parm), int parm)
467{
468 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
469 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
470}
471
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300472size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300473 bool (skip)(struct dso *dso, int parm), int parm)
474{
475 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300476 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300477
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300478 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300479 struct machine *pos = rb_entry(nd, struct machine, rb_node);
480 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
481 }
482 return ret;
483}
484
485size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
486{
487 int i;
488 size_t printed = 0;
489 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
490
491 if (kdso->has_build_id) {
492 char filename[PATH_MAX];
493 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
494 printed += fprintf(fp, "[0] %s\n", filename);
495 }
496
497 for (i = 0; i < vmlinux_path__nr_entries; ++i)
498 printed += fprintf(fp, "[%d] %s\n",
499 i + kdso->has_build_id, vmlinux_path[i]);
500
501 return printed;
502}
503
504size_t machine__fprintf(struct machine *machine, FILE *fp)
505{
506 size_t ret = 0;
507 struct rb_node *nd;
508
509 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
510 struct thread *pos = rb_entry(nd, struct thread, rb_node);
511
512 ret += thread__fprintf(pos, fp);
513 }
514
515 return ret;
516}
517
518static struct dso *machine__get_kernel(struct machine *machine)
519{
520 const char *vmlinux_name = NULL;
521 struct dso *kernel;
522
523 if (machine__is_host(machine)) {
524 vmlinux_name = symbol_conf.vmlinux_name;
525 if (!vmlinux_name)
526 vmlinux_name = "[kernel.kallsyms]";
527
528 kernel = dso__kernel_findnew(machine, vmlinux_name,
529 "[kernel]",
530 DSO_TYPE_KERNEL);
531 } else {
532 char bf[PATH_MAX];
533
534 if (machine__is_default_guest(machine))
535 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
536 if (!vmlinux_name)
537 vmlinux_name = machine__mmap_name(machine, bf,
538 sizeof(bf));
539
540 kernel = dso__kernel_findnew(machine, vmlinux_name,
541 "[guest.kernel]",
542 DSO_TYPE_GUEST_KERNEL);
543 }
544
545 if (kernel != NULL && (!kernel->has_build_id))
546 dso__read_running_kernel_build_id(kernel, machine);
547
548 return kernel;
549}
550
551struct process_args {
552 u64 start;
553};
554
Adrian Hunter15a0a872014-01-29 16:14:38 +0200555static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
556 size_t bufsz)
557{
558 if (machine__is_default_guest(machine))
559 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
560 else
561 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
562}
563
Simon Quea93f0e52014-06-16 11:32:09 -0700564const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
565
566/* Figure out the start address of kernel map from /proc/kallsyms.
567 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
568 * symbol_name if it's not that important.
569 */
570static u64 machine__get_kernel_start_addr(struct machine *machine,
571 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300572{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200573 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700574 int i;
575 const char *name;
576 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300577
Adrian Hunter15a0a872014-01-29 16:14:38 +0200578 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300579
580 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
581 return 0;
582
Simon Quea93f0e52014-06-16 11:32:09 -0700583 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
584 addr = kallsyms__get_function_start(filename, name);
585 if (addr)
586 break;
587 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300588
Simon Quea93f0e52014-06-16 11:32:09 -0700589 if (symbol_name)
590 *symbol_name = name;
591
592 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300593}
594
595int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
596{
597 enum map_type type;
Simon Quea93f0e52014-06-16 11:32:09 -0700598 u64 start = machine__get_kernel_start_addr(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300599
600 for (type = 0; type < MAP__NR_TYPES; ++type) {
601 struct kmap *kmap;
602
603 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
604 if (machine->vmlinux_maps[type] == NULL)
605 return -1;
606
607 machine->vmlinux_maps[type]->map_ip =
608 machine->vmlinux_maps[type]->unmap_ip =
609 identity__map_ip;
610 kmap = map__kmap(machine->vmlinux_maps[type]);
611 kmap->kmaps = &machine->kmaps;
612 map_groups__insert(&machine->kmaps,
613 machine->vmlinux_maps[type]);
614 }
615
616 return 0;
617}
618
619void machine__destroy_kernel_maps(struct machine *machine)
620{
621 enum map_type type;
622
623 for (type = 0; type < MAP__NR_TYPES; ++type) {
624 struct kmap *kmap;
625
626 if (machine->vmlinux_maps[type] == NULL)
627 continue;
628
629 kmap = map__kmap(machine->vmlinux_maps[type]);
630 map_groups__remove(&machine->kmaps,
631 machine->vmlinux_maps[type]);
632 if (kmap->ref_reloc_sym) {
633 /*
634 * ref_reloc_sym is shared among all maps, so free just
635 * on one of them.
636 */
637 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300638 zfree((char **)&kmap->ref_reloc_sym->name);
639 zfree(&kmap->ref_reloc_sym);
640 } else
641 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300642 }
643
644 map__delete(machine->vmlinux_maps[type]);
645 machine->vmlinux_maps[type] = NULL;
646 }
647}
648
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300649int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300650{
651 int ret = 0;
652 struct dirent **namelist = NULL;
653 int i, items = 0;
654 char path[PATH_MAX];
655 pid_t pid;
656 char *endp;
657
658 if (symbol_conf.default_guest_vmlinux_name ||
659 symbol_conf.default_guest_modules ||
660 symbol_conf.default_guest_kallsyms) {
661 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
662 }
663
664 if (symbol_conf.guestmount) {
665 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
666 if (items <= 0)
667 return -ENOENT;
668 for (i = 0; i < items; i++) {
669 if (!isdigit(namelist[i]->d_name[0])) {
670 /* Filter out . and .. */
671 continue;
672 }
673 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
674 if ((*endp != '\0') ||
675 (endp == namelist[i]->d_name) ||
676 (errno == ERANGE)) {
677 pr_debug("invalid directory (%s). Skipping.\n",
678 namelist[i]->d_name);
679 continue;
680 }
681 sprintf(path, "%s/%s/proc/kallsyms",
682 symbol_conf.guestmount,
683 namelist[i]->d_name);
684 ret = access(path, R_OK);
685 if (ret) {
686 pr_debug("Can't access file %s\n", path);
687 goto failure;
688 }
689 machines__create_kernel_maps(machines, pid);
690 }
691failure:
692 free(namelist);
693 }
694
695 return ret;
696}
697
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300698void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300699{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300700 struct rb_node *next = rb_first(&machines->guests);
701
702 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300703
704 while (next) {
705 struct machine *pos = rb_entry(next, struct machine, rb_node);
706
707 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300708 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300709 machine__delete(pos);
710 }
711}
712
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300713int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300714{
715 struct machine *machine = machines__findnew(machines, pid);
716
717 if (machine == NULL)
718 return -1;
719
720 return machine__create_kernel_maps(machine);
721}
722
723int machine__load_kallsyms(struct machine *machine, const char *filename,
724 enum map_type type, symbol_filter_t filter)
725{
726 struct map *map = machine->vmlinux_maps[type];
727 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
728
729 if (ret > 0) {
730 dso__set_loaded(map->dso, type);
731 /*
732 * Since /proc/kallsyms will have multiple sessions for the
733 * kernel, with modules between them, fixup the end of all
734 * sections.
735 */
736 __map_groups__fixup_end(&machine->kmaps, type);
737 }
738
739 return ret;
740}
741
742int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
743 symbol_filter_t filter)
744{
745 struct map *map = machine->vmlinux_maps[type];
746 int ret = dso__load_vmlinux_path(map->dso, map, filter);
747
Adrian Hunter39b12f782013-08-07 14:38:47 +0300748 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300749 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300750
751 return ret;
752}
753
754static void map_groups__fixup_end(struct map_groups *mg)
755{
756 int i;
757 for (i = 0; i < MAP__NR_TYPES; ++i)
758 __map_groups__fixup_end(mg, i);
759}
760
761static char *get_kernel_version(const char *root_dir)
762{
763 char version[PATH_MAX];
764 FILE *file;
765 char *name, *tmp;
766 const char *prefix = "Linux version ";
767
768 sprintf(version, "%s/proc/version", root_dir);
769 file = fopen(version, "r");
770 if (!file)
771 return NULL;
772
773 version[0] = '\0';
774 tmp = fgets(version, sizeof(version), file);
775 fclose(file);
776
777 name = strstr(version, prefix);
778 if (!name)
779 return NULL;
780 name += strlen(prefix);
781 tmp = strchr(name, ' ');
782 if (tmp)
783 *tmp = '\0';
784
785 return strdup(name);
786}
787
788static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400789 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300790{
791 struct dirent *dent;
792 DIR *dir = opendir(dir_name);
793 int ret = 0;
794
795 if (!dir) {
796 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
797 return -1;
798 }
799
800 while ((dent = readdir(dir)) != NULL) {
801 char path[PATH_MAX];
802 struct stat st;
803
804 /*sshfs might return bad dent->d_type, so we have to stat*/
805 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
806 if (stat(path, &st))
807 continue;
808
809 if (S_ISDIR(st.st_mode)) {
810 if (!strcmp(dent->d_name, ".") ||
811 !strcmp(dent->d_name, ".."))
812 continue;
813
Richard Yao61d42902014-04-26 13:17:55 -0400814 /* Do not follow top-level source and build symlinks */
815 if (depth == 0) {
816 if (!strcmp(dent->d_name, "source") ||
817 !strcmp(dent->d_name, "build"))
818 continue;
819 }
820
821 ret = map_groups__set_modules_path_dir(mg, path,
822 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300823 if (ret < 0)
824 goto out;
825 } else {
826 char *dot = strrchr(dent->d_name, '.'),
827 dso_name[PATH_MAX];
828 struct map *map;
829 char *long_name;
830
831 if (dot == NULL || strcmp(dot, ".ko"))
832 continue;
833 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
834 (int)(dot - dent->d_name), dent->d_name);
835
836 strxfrchar(dso_name, '-', '_');
837 map = map_groups__find_by_name(mg, MAP__FUNCTION,
838 dso_name);
839 if (map == NULL)
840 continue;
841
842 long_name = strdup(path);
843 if (long_name == NULL) {
844 ret = -1;
845 goto out;
846 }
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300847 dso__set_long_name(map->dso, long_name, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300848 dso__kernel_module_get_build_id(map->dso, "");
849 }
850 }
851
852out:
853 closedir(dir);
854 return ret;
855}
856
857static int machine__set_modules_path(struct machine *machine)
858{
859 char *version;
860 char modules_path[PATH_MAX];
861
862 version = get_kernel_version(machine->root_dir);
863 if (!version)
864 return -1;
865
Richard Yao61d42902014-04-26 13:17:55 -0400866 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300867 machine->root_dir, version);
868 free(version);
869
Richard Yao61d42902014-04-26 13:17:55 -0400870 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300871}
872
Adrian Hunter316d70d2013-10-08 11:45:48 +0300873static int machine__create_module(void *arg, const char *name, u64 start)
874{
875 struct machine *machine = arg;
876 struct map *map;
877
878 map = machine__new_module(machine, start, name);
879 if (map == NULL)
880 return -1;
881
882 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
883
884 return 0;
885}
886
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300887static int machine__create_modules(struct machine *machine)
888{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300889 const char *modules;
890 char path[PATH_MAX];
891
Adrian Hunterf4be9042013-09-22 13:22:09 +0300892 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300893 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +0300894 } else {
895 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300896 modules = path;
897 }
898
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +0300899 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300900 return -1;
901
Adrian Hunter316d70d2013-10-08 11:45:48 +0300902 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300903 return -1;
904
Adrian Hunter316d70d2013-10-08 11:45:48 +0300905 if (!machine__set_modules_path(machine))
906 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300907
Adrian Hunter316d70d2013-10-08 11:45:48 +0300908 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300909
Jason Wessel8f76fcd2013-07-15 15:27:53 -0500910 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300911}
912
913int machine__create_kernel_maps(struct machine *machine)
914{
915 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200916 const char *name;
Simon Quea93f0e52014-06-16 11:32:09 -0700917 u64 addr = machine__get_kernel_start_addr(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200918 if (!addr)
919 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300920
921 if (kernel == NULL ||
922 __machine__create_kernel_maps(machine, kernel) < 0)
923 return -1;
924
925 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
926 if (machine__is_host(machine))
927 pr_debug("Problems creating module maps, "
928 "continuing anyway...\n");
929 else
930 pr_debug("Problems creating module maps for guest %d, "
931 "continuing anyway...\n", machine->pid);
932 }
933
934 /*
935 * Now that we have all the maps created, just set the ->end of them:
936 */
937 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200938
939 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
940 addr)) {
941 machine__destroy_kernel_maps(machine);
942 return -1;
943 }
944
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300945 return 0;
946}
947
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300948static void machine__set_kernel_mmap_len(struct machine *machine,
949 union perf_event *event)
950{
Namhyung Kim4552cf02012-11-07 16:27:10 +0900951 int i;
952
953 for (i = 0; i < MAP__NR_TYPES; i++) {
954 machine->vmlinux_maps[i]->start = event->mmap.start;
955 machine->vmlinux_maps[i]->end = (event->mmap.start +
956 event->mmap.len);
957 /*
958 * Be a bit paranoid here, some perf.data file came with
959 * a zero sized synthesized MMAP event for the kernel.
960 */
961 if (machine->vmlinux_maps[i]->end == 0)
962 machine->vmlinux_maps[i]->end = ~0ULL;
963 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300964}
965
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300966static bool machine__uses_kcore(struct machine *machine)
967{
968 struct dso *dso;
969
970 list_for_each_entry(dso, &machine->kernel_dsos, node) {
971 if (dso__is_kcore(dso))
972 return true;
973 }
974
975 return false;
976}
977
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300978static int machine__process_kernel_mmap_event(struct machine *machine,
979 union perf_event *event)
980{
981 struct map *map;
982 char kmmap_prefix[PATH_MAX];
983 enum dso_kernel_type kernel_type;
984 bool is_kernel_mmap;
985
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300986 /* If we have maps from kcore then we do not need or want any others */
987 if (machine__uses_kcore(machine))
988 return 0;
989
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300990 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
991 if (machine__is_host(machine))
992 kernel_type = DSO_TYPE_KERNEL;
993 else
994 kernel_type = DSO_TYPE_GUEST_KERNEL;
995
996 is_kernel_mmap = memcmp(event->mmap.filename,
997 kmmap_prefix,
998 strlen(kmmap_prefix) - 1) == 0;
999 if (event->mmap.filename[0] == '/' ||
1000 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1001
1002 char short_module_name[1024];
1003 char *name, *dot;
1004
1005 if (event->mmap.filename[0] == '/') {
1006 name = strrchr(event->mmap.filename, '/');
1007 if (name == NULL)
1008 goto out_problem;
1009
1010 ++name; /* skip / */
1011 dot = strrchr(name, '.');
1012 if (dot == NULL)
1013 goto out_problem;
1014 snprintf(short_module_name, sizeof(short_module_name),
1015 "[%.*s]", (int)(dot - name), name);
1016 strxfrchar(short_module_name, '-', '_');
1017 } else
1018 strcpy(short_module_name, event->mmap.filename);
1019
1020 map = machine__new_module(machine, event->mmap.start,
1021 event->mmap.filename);
1022 if (map == NULL)
1023 goto out_problem;
1024
1025 name = strdup(short_module_name);
1026 if (name == NULL)
1027 goto out_problem;
1028
Adrian Hunter58a98c92013-12-10 11:11:46 -03001029 dso__set_short_name(map->dso, name, true);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001030 map->end = map->start + event->mmap.len;
1031 } else if (is_kernel_mmap) {
1032 const char *symbol_name = (event->mmap.filename +
1033 strlen(kmmap_prefix));
1034 /*
1035 * Should be there already, from the build-id table in
1036 * the header.
1037 */
1038 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1039 kmmap_prefix);
1040 if (kernel == NULL)
1041 goto out_problem;
1042
1043 kernel->kernel = kernel_type;
1044 if (__machine__create_kernel_maps(machine, kernel) < 0)
1045 goto out_problem;
1046
1047 machine__set_kernel_mmap_len(machine, event);
1048
1049 /*
1050 * Avoid using a zero address (kptr_restrict) for the ref reloc
1051 * symbol. Effectively having zero here means that at record
1052 * time /proc/sys/kernel/kptr_restrict was non zero.
1053 */
1054 if (event->mmap.pgoff != 0) {
1055 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1056 symbol_name,
1057 event->mmap.pgoff);
1058 }
1059
1060 if (machine__is_default_guest(machine)) {
1061 /*
1062 * preload dso of guest kernel and modules
1063 */
1064 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1065 NULL);
1066 }
1067 }
1068 return 0;
1069out_problem:
1070 return -1;
1071}
1072
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001073int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001074 union perf_event *event,
1075 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001076{
1077 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1078 struct thread *thread;
1079 struct map *map;
1080 enum map_type type;
1081 int ret = 0;
1082
1083 if (dump_trace)
1084 perf_event__fprintf_mmap2(event, stdout);
1085
1086 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1087 cpumode == PERF_RECORD_MISC_KERNEL) {
1088 ret = machine__process_kernel_mmap_event(machine, event);
1089 if (ret < 0)
1090 goto out_problem;
1091 return 0;
1092 }
1093
1094 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001095 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001096 if (thread == NULL)
1097 goto out_problem;
1098
1099 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1100 type = MAP__VARIABLE;
1101 else
1102 type = MAP__FUNCTION;
1103
Adrian Hunter2a030682014-07-22 16:17:53 +03001104 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001105 event->mmap2.len, event->mmap2.pgoff,
1106 event->mmap2.pid, event->mmap2.maj,
1107 event->mmap2.min, event->mmap2.ino,
1108 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001109 event->mmap2.prot,
1110 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001111 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001112
1113 if (map == NULL)
1114 goto out_problem;
1115
1116 thread__insert_map(thread, map);
1117 return 0;
1118
1119out_problem:
1120 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1121 return 0;
1122}
1123
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001124int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1125 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001126{
1127 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1128 struct thread *thread;
1129 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001130 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001131 int ret = 0;
1132
1133 if (dump_trace)
1134 perf_event__fprintf_mmap(event, stdout);
1135
1136 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1137 cpumode == PERF_RECORD_MISC_KERNEL) {
1138 ret = machine__process_kernel_mmap_event(machine, event);
1139 if (ret < 0)
1140 goto out_problem;
1141 return 0;
1142 }
1143
Adrian Hunter314add62013-08-27 11:23:03 +03001144 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001145 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001146 if (thread == NULL)
1147 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001148
1149 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1150 type = MAP__VARIABLE;
1151 else
1152 type = MAP__FUNCTION;
1153
Adrian Hunter2a030682014-07-22 16:17:53 +03001154 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001155 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001156 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001157 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001158 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001159
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001160 if (map == NULL)
1161 goto out_problem;
1162
1163 thread__insert_map(thread, map);
1164 return 0;
1165
1166out_problem:
1167 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1168 return 0;
1169}
1170
David Ahern236a3bb2013-08-14 08:49:27 -06001171static void machine__remove_thread(struct machine *machine, struct thread *th)
1172{
1173 machine->last_match = NULL;
1174 rb_erase(&th->rb_node, &machine->threads);
1175 /*
1176 * We may have references to this thread, for instance in some hist_entry
1177 * instances, so just move them to a separate list.
1178 */
1179 list_add_tail(&th->node, &machine->dead_threads);
1180}
1181
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001182int machine__process_fork_event(struct machine *machine, union perf_event *event,
1183 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001184{
Jiri Olsad75e6092014-03-14 15:00:03 +01001185 struct thread *thread = machine__find_thread(machine,
1186 event->fork.pid,
1187 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001188 struct thread *parent = machine__findnew_thread(machine,
1189 event->fork.ppid,
1190 event->fork.ptid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001191
David Ahern236a3bb2013-08-14 08:49:27 -06001192 /* if a thread currently exists for the thread id remove it */
1193 if (thread != NULL)
1194 machine__remove_thread(machine, thread);
1195
Adrian Hunter314add62013-08-27 11:23:03 +03001196 thread = machine__findnew_thread(machine, event->fork.pid,
1197 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001198 if (dump_trace)
1199 perf_event__fprintf_task(event, stdout);
1200
1201 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001202 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001203 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1204 return -1;
1205 }
1206
1207 return 0;
1208}
1209
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001210int machine__process_exit_event(struct machine *machine, union perf_event *event,
1211 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001212{
Jiri Olsad75e6092014-03-14 15:00:03 +01001213 struct thread *thread = machine__find_thread(machine,
1214 event->fork.pid,
1215 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001216
1217 if (dump_trace)
1218 perf_event__fprintf_task(event, stdout);
1219
1220 if (thread != NULL)
David Ahern236a3bb2013-08-14 08:49:27 -06001221 thread__exited(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001222
1223 return 0;
1224}
1225
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001226int machine__process_event(struct machine *machine, union perf_event *event,
1227 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001228{
1229 int ret;
1230
1231 switch (event->header.type) {
1232 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001233 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001234 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001235 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001236 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001237 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001238 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001239 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001240 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001241 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001242 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001243 ret = machine__process_lost_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001244 default:
1245 ret = -1;
1246 break;
1247 }
1248
1249 return ret;
1250}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001251
Greg Priceb21484f2012-12-06 21:48:05 -08001252static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001253{
Greg Priceb21484f2012-12-06 21:48:05 -08001254 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001255 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001256 return 0;
1257}
1258
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001259static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1260 struct addr_map_symbol *ams,
1261 u64 ip)
1262{
1263 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001264
1265 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001266 /*
1267 * We cannot use the header.misc hint to determine whether a
1268 * branch stack address is user, kernel, guest, hypervisor.
1269 * Branches may straddle the kernel/user/hypervisor boundaries.
1270 * Thus, we have to try consecutively until we find a match
1271 * or else, the symbol is unknown
1272 */
1273 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001274
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001275 ams->addr = ip;
1276 ams->al_addr = al.addr;
1277 ams->sym = al.sym;
1278 ams->map = al.map;
1279}
1280
Stephane Eranian98a3b322013-01-24 16:10:35 +01001281static void ip__resolve_data(struct machine *machine, struct thread *thread,
1282 u8 m, struct addr_map_symbol *ams, u64 addr)
1283{
1284 struct addr_location al;
1285
1286 memset(&al, 0, sizeof(al));
1287
Adrian Hunter61710bd2013-08-08 14:32:26 +03001288 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1289 &al);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001290 ams->addr = addr;
1291 ams->al_addr = al.addr;
1292 ams->sym = al.sym;
1293 ams->map = al.map;
1294}
1295
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001296struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1297 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001298{
1299 struct mem_info *mi = zalloc(sizeof(*mi));
1300
1301 if (!mi)
1302 return NULL;
1303
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001304 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1305 ip__resolve_data(al->machine, al->thread, al->cpumode,
1306 &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001307 mi->data_src.val = sample->data_src;
1308
1309 return mi;
1310}
1311
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001312struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1313 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001314{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001315 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001316 const struct branch_stack *bs = sample->branch_stack;
1317 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001318
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001319 if (!bi)
1320 return NULL;
1321
1322 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001323 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1324 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001325 bi[i].flags = bs->entries[i].flags;
1326 }
1327 return bi;
1328}
1329
1330static int machine__resolve_callchain_sample(struct machine *machine,
1331 struct thread *thread,
1332 struct ip_callchain *chain,
Greg Priceb21484f2012-12-06 21:48:05 -08001333 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001334 struct addr_location *root_al,
1335 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001336{
1337 u8 cpumode = PERF_RECORD_MISC_USER;
Waiman Long91e95612013-10-18 10:38:48 -04001338 int chain_nr = min(max_stack, (int)chain->nr);
1339 int i;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001340 int j;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001341 int err;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001342 int skip_idx __maybe_unused;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001343
1344 callchain_cursor_reset(&callchain_cursor);
1345
1346 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1347 pr_warning("corrupted callchain. skipping...\n");
1348 return 0;
1349 }
1350
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001351 /*
1352 * Based on DWARF debug information, some architectures skip
1353 * a callchain entry saved by the kernel.
1354 */
1355 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1356
Waiman Long91e95612013-10-18 10:38:48 -04001357 for (i = 0; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001358 u64 ip;
1359 struct addr_location al;
1360
1361 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001362 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001363 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001364 j = chain->nr - i - 1;
1365
1366#ifdef HAVE_SKIP_CALLCHAIN_IDX
1367 if (j == skip_idx)
1368 continue;
1369#endif
1370 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001371
1372 if (ip >= PERF_CONTEXT_MAX) {
1373 switch (ip) {
1374 case PERF_CONTEXT_HV:
1375 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1376 break;
1377 case PERF_CONTEXT_KERNEL:
1378 cpumode = PERF_RECORD_MISC_KERNEL;
1379 break;
1380 case PERF_CONTEXT_USER:
1381 cpumode = PERF_RECORD_MISC_USER;
1382 break;
1383 default:
1384 pr_debug("invalid callchain context: "
1385 "%"PRId64"\n", (s64) ip);
1386 /*
1387 * It seems the callchain is corrupted.
1388 * Discard all.
1389 */
1390 callchain_cursor_reset(&callchain_cursor);
1391 return 0;
1392 }
1393 continue;
1394 }
1395
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001396 al.filtered = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001397 thread__find_addr_location(thread, machine, cpumode,
Adrian Hunter61710bd2013-08-08 14:32:26 +03001398 MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001399 if (al.sym != NULL) {
1400 if (sort__has_parent && !*parent &&
Greg Priceb21484f2012-12-06 21:48:05 -08001401 symbol__match_regex(al.sym, &parent_regex))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001402 *parent = al.sym;
Greg Priceb21484f2012-12-06 21:48:05 -08001403 else if (have_ignore_callees && root_al &&
1404 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1405 /* Treat this symbol as the root,
1406 forgetting its callees. */
1407 *root_al = al;
1408 callchain_cursor_reset(&callchain_cursor);
1409 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001410 }
1411
1412 err = callchain_cursor_append(&callchain_cursor,
1413 ip, al.map, al.sym);
1414 if (err)
1415 return err;
1416 }
1417
1418 return 0;
1419}
1420
1421static int unwind_entry(struct unwind_entry *entry, void *arg)
1422{
1423 struct callchain_cursor *cursor = arg;
1424 return callchain_cursor_append(cursor, entry->ip,
1425 entry->map, entry->sym);
1426}
1427
1428int machine__resolve_callchain(struct machine *machine,
1429 struct perf_evsel *evsel,
1430 struct thread *thread,
1431 struct perf_sample *sample,
Greg Priceb21484f2012-12-06 21:48:05 -08001432 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001433 struct addr_location *root_al,
1434 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001435{
1436 int ret;
1437
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001438 ret = machine__resolve_callchain_sample(machine, thread,
Waiman Long91e95612013-10-18 10:38:48 -04001439 sample->callchain, parent,
1440 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001441 if (ret)
1442 return ret;
1443
1444 /* Can we do dwarf post unwind? */
1445 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1446 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1447 return 0;
1448
1449 /* Bail out if nothing was captured. */
1450 if ((!sample->user_regs.regs) ||
1451 (!sample->user_stack.size))
1452 return 0;
1453
1454 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
Jiri Olsa352ea452014-01-07 13:47:25 +01001455 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001456
1457}
David Ahern35feee12013-09-28 13:12:58 -06001458
1459int machine__for_each_thread(struct machine *machine,
1460 int (*fn)(struct thread *thread, void *p),
1461 void *priv)
1462{
1463 struct rb_node *nd;
1464 struct thread *thread;
1465 int rc = 0;
1466
1467 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1468 thread = rb_entry(nd, struct thread, rb_node);
1469 rc = fn(thread, priv);
1470 if (rc != 0)
1471 return rc;
1472 }
1473
1474 list_for_each_entry(thread, &machine->dead_threads, node) {
1475 rc = fn(thread, priv);
1476 if (rc != 0)
1477 return rc;
1478 }
1479 return rc;
1480}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001481
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001482int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001483 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001484 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001485{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001486 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001487 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001488 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001489 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1490 /* command specified */
1491 return 0;
1492}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001493
1494pid_t machine__get_current_tid(struct machine *machine, int cpu)
1495{
1496 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1497 return -1;
1498
1499 return machine->current_tid[cpu];
1500}
1501
1502int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1503 pid_t tid)
1504{
1505 struct thread *thread;
1506
1507 if (cpu < 0)
1508 return -EINVAL;
1509
1510 if (!machine->current_tid) {
1511 int i;
1512
1513 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1514 if (!machine->current_tid)
1515 return -ENOMEM;
1516 for (i = 0; i < MAX_NR_CPUS; i++)
1517 machine->current_tid[i] = -1;
1518 }
1519
1520 if (cpu >= MAX_NR_CPUS) {
1521 pr_err("Requested CPU %d too large. ", cpu);
1522 pr_err("Consider raising MAX_NR_CPUS\n");
1523 return -EINVAL;
1524 }
1525
1526 machine->current_tid[cpu] = tid;
1527
1528 thread = machine__findnew_thread(machine, pid, tid);
1529 if (!thread)
1530 return -ENOMEM;
1531
1532 thread->cpu = cpu;
1533
1534 return 0;
1535}