blob: cfc691000f13a24bf93704f63dbdc5fc219682df [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"
11#include <stdbool.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030012#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030013#include "unwind.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030014
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030015int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
16{
17 map_groups__init(&machine->kmaps);
18 RB_CLEAR_NODE(&machine->rb_node);
19 INIT_LIST_HEAD(&machine->user_dsos);
20 INIT_LIST_HEAD(&machine->kernel_dsos);
21
22 machine->threads = RB_ROOT;
23 INIT_LIST_HEAD(&machine->dead_threads);
24 machine->last_match = NULL;
25
26 machine->kmaps.machine = machine;
27 machine->pid = pid;
28
Adrian Hunter611a5ce2013-08-08 14:32:20 +030029 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010030 machine->id_hdr_size = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030031
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030032 machine->root_dir = strdup(root_dir);
33 if (machine->root_dir == NULL)
34 return -ENOMEM;
35
36 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030037 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030038 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030039 char comm[64];
40
41 if (thread == NULL)
42 return -ENOMEM;
43
44 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020045 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030046 }
47
Adrian Hunterb9d266b2014-07-22 16:17:25 +030048 machine->current_tid = NULL;
49
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030050 return 0;
51}
52
David Ahern8fb598e2013-09-28 13:13:00 -060053struct machine *machine__new_host(void)
54{
55 struct machine *machine = malloc(sizeof(*machine));
56
57 if (machine != NULL) {
58 machine__init(machine, "", HOST_KERNEL_ID);
59
60 if (machine__create_kernel_maps(machine) < 0)
61 goto out_delete;
62 }
63
64 return machine;
65out_delete:
66 free(machine);
67 return NULL;
68}
69
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030070static void dsos__delete(struct list_head *dsos)
71{
72 struct dso *pos, *n;
73
74 list_for_each_entry_safe(pos, n, dsos, node) {
75 list_del(&pos->node);
76 dso__delete(pos);
77 }
78}
79
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030080void machine__delete_dead_threads(struct machine *machine)
81{
82 struct thread *n, *t;
83
84 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
85 list_del(&t->node);
86 thread__delete(t);
87 }
88}
89
90void machine__delete_threads(struct machine *machine)
91{
92 struct rb_node *nd = rb_first(&machine->threads);
93
94 while (nd) {
95 struct thread *t = rb_entry(nd, struct thread, rb_node);
96
97 rb_erase(&t->rb_node, &machine->threads);
98 nd = rb_next(nd);
99 thread__delete(t);
100 }
101}
102
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300103void machine__exit(struct machine *machine)
104{
105 map_groups__exit(&machine->kmaps);
106 dsos__delete(&machine->user_dsos);
107 dsos__delete(&machine->kernel_dsos);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300108 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300109 zfree(&machine->current_tid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300110}
111
112void machine__delete(struct machine *machine)
113{
114 machine__exit(machine);
115 free(machine);
116}
117
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300118void machines__init(struct machines *machines)
119{
120 machine__init(&machines->host, "", HOST_KERNEL_ID);
121 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300122 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300123}
124
125void machines__exit(struct machines *machines)
126{
127 machine__exit(&machines->host);
128 /* XXX exit guest */
129}
130
131struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300132 const char *root_dir)
133{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300134 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300135 struct rb_node *parent = NULL;
136 struct machine *pos, *machine = malloc(sizeof(*machine));
137
138 if (machine == NULL)
139 return NULL;
140
141 if (machine__init(machine, root_dir, pid) != 0) {
142 free(machine);
143 return NULL;
144 }
145
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300146 machine->symbol_filter = machines->symbol_filter;
147
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300148 while (*p != NULL) {
149 parent = *p;
150 pos = rb_entry(parent, struct machine, rb_node);
151 if (pid < pos->pid)
152 p = &(*p)->rb_left;
153 else
154 p = &(*p)->rb_right;
155 }
156
157 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300158 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300159
160 return machine;
161}
162
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300163void machines__set_symbol_filter(struct machines *machines,
164 symbol_filter_t symbol_filter)
165{
166 struct rb_node *nd;
167
168 machines->symbol_filter = symbol_filter;
169 machines->host.symbol_filter = symbol_filter;
170
171 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
172 struct machine *machine = rb_entry(nd, struct machine, rb_node);
173
174 machine->symbol_filter = symbol_filter;
175 }
176}
177
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300178struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300179{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300180 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300181 struct rb_node *parent = NULL;
182 struct machine *machine;
183 struct machine *default_machine = NULL;
184
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300185 if (pid == HOST_KERNEL_ID)
186 return &machines->host;
187
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300188 while (*p != NULL) {
189 parent = *p;
190 machine = rb_entry(parent, struct machine, rb_node);
191 if (pid < machine->pid)
192 p = &(*p)->rb_left;
193 else if (pid > machine->pid)
194 p = &(*p)->rb_right;
195 else
196 return machine;
197 if (!machine->pid)
198 default_machine = machine;
199 }
200
201 return default_machine;
202}
203
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300204struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300205{
206 char path[PATH_MAX];
207 const char *root_dir = "";
208 struct machine *machine = machines__find(machines, pid);
209
210 if (machine && (machine->pid == pid))
211 goto out;
212
213 if ((pid != HOST_KERNEL_ID) &&
214 (pid != DEFAULT_GUEST_KERNEL_ID) &&
215 (symbol_conf.guestmount)) {
216 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
217 if (access(path, R_OK)) {
218 static struct strlist *seen;
219
220 if (!seen)
221 seen = strlist__new(true, NULL);
222
223 if (!strlist__has_entry(seen, path)) {
224 pr_err("Can't access file %s\n", path);
225 strlist__add(seen, path);
226 }
227 machine = NULL;
228 goto out;
229 }
230 root_dir = path;
231 }
232
233 machine = machines__add(machines, pid, root_dir);
234out:
235 return machine;
236}
237
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300238void machines__process_guests(struct machines *machines,
239 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300240{
241 struct rb_node *nd;
242
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300243 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300244 struct machine *pos = rb_entry(nd, struct machine, rb_node);
245 process(pos, data);
246 }
247}
248
249char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
250{
251 if (machine__is_host(machine))
252 snprintf(bf, size, "[%s]", "kernel.kallsyms");
253 else if (machine__is_default_guest(machine))
254 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
255 else {
256 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
257 machine->pid);
258 }
259
260 return bf;
261}
262
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300263void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300264{
265 struct rb_node *node;
266 struct machine *machine;
267
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300268 machines->host.id_hdr_size = id_hdr_size;
269
270 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300271 machine = rb_entry(node, struct machine, rb_node);
272 machine->id_hdr_size = id_hdr_size;
273 }
274
275 return;
276}
277
Adrian Hunter29ce3612014-07-16 11:07:13 +0300278static void machine__update_thread_pid(struct machine *machine,
279 struct thread *th, pid_t pid)
280{
281 struct thread *leader;
282
283 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
284 return;
285
286 th->pid_ = pid;
287
288 if (th->pid_ == th->tid)
289 return;
290
291 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
292 if (!leader)
293 goto out_err;
294
295 if (!leader->mg)
296 leader->mg = map_groups__new();
297
298 if (!leader->mg)
299 goto out_err;
300
301 if (th->mg == leader->mg)
302 return;
303
304 if (th->mg) {
305 /*
306 * Maps are created from MMAP events which provide the pid and
307 * tid. Consequently there never should be any maps on a thread
308 * with an unknown pid. Just print an error if there are.
309 */
310 if (!map_groups__empty(th->mg))
311 pr_err("Discarding thread maps for %d:%d\n",
312 th->pid_, th->tid);
313 map_groups__delete(th->mg);
314 }
315
316 th->mg = map_groups__get(leader->mg);
317
318 return;
319
320out_err:
321 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
322}
323
Adrian Hunter99d725f2013-08-26 16:00:19 +0300324static struct thread *__machine__findnew_thread(struct machine *machine,
325 pid_t pid, pid_t tid,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300326 bool create)
327{
328 struct rb_node **p = &machine->threads.rb_node;
329 struct rb_node *parent = NULL;
330 struct thread *th;
331
332 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300333 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300334 * so most of the time we dont have to look up
335 * the full rbtree:
336 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300337 th = machine->last_match;
338 if (th && th->tid == tid) {
339 machine__update_thread_pid(machine, th, pid);
340 return th;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300341 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300342
343 while (*p != NULL) {
344 parent = *p;
345 th = rb_entry(parent, struct thread, rb_node);
346
Adrian Hunter38051232013-07-04 16:20:31 +0300347 if (th->tid == tid) {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300348 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300349 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300350 return th;
351 }
352
Adrian Hunter38051232013-07-04 16:20:31 +0300353 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300354 p = &(*p)->rb_left;
355 else
356 p = &(*p)->rb_right;
357 }
358
359 if (!create)
360 return NULL;
361
Adrian Hunter99d725f2013-08-26 16:00:19 +0300362 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300363 if (th != NULL) {
364 rb_link_node(&th->rb_node, parent, p);
365 rb_insert_color(&th->rb_node, &machine->threads);
366 machine->last_match = th;
Jiri Olsacddcef62014-04-09 20:54:29 +0200367
368 /*
369 * We have to initialize map_groups separately
370 * after rb tree is updated.
371 *
372 * The reason is that we call machine__findnew_thread
373 * within thread__init_map_groups to find the thread
374 * leader and that would screwed the rb tree.
375 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300376 if (thread__init_map_groups(th, machine)) {
377 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200378 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300379 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300380 }
381
382 return th;
383}
384
Adrian Hunter314add62013-08-27 11:23:03 +0300385struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
386 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300387{
Adrian Hunter314add62013-08-27 11:23:03 +0300388 return __machine__findnew_thread(machine, pid, tid, true);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300389}
390
Jiri Olsad75e6092014-03-14 15:00:03 +0100391struct thread *machine__find_thread(struct machine *machine, pid_t pid,
392 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300393{
Jiri Olsad75e6092014-03-14 15:00:03 +0100394 return __machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300395}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300396
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200397int machine__process_comm_event(struct machine *machine, union perf_event *event,
398 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300399{
Adrian Hunter314add62013-08-27 11:23:03 +0300400 struct thread *thread = machine__findnew_thread(machine,
401 event->comm.pid,
402 event->comm.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300403
404 if (dump_trace)
405 perf_event__fprintf_comm(event, stdout);
406
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200407 if (thread == NULL || thread__set_comm(thread, event->comm.comm, sample->time)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300408 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
409 return -1;
410 }
411
412 return 0;
413}
414
415int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200416 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300417{
418 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
419 event->lost.id, event->lost.lost);
420 return 0;
421}
422
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300423struct map *machine__new_module(struct machine *machine, u64 start,
424 const char *filename)
425{
426 struct map *map;
427 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
428
429 if (dso == NULL)
430 return NULL;
431
432 map = map__new2(start, dso, MAP__FUNCTION);
433 if (map == NULL)
434 return NULL;
435
436 if (machine__is_host(machine))
437 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
438 else
439 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
440 map_groups__insert(&machine->kmaps, map);
441 return map;
442}
443
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300444size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300445{
446 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300447 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
448 __dsos__fprintf(&machines->host.user_dsos, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300449
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300450 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300451 struct machine *pos = rb_entry(nd, struct machine, rb_node);
452 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
453 ret += __dsos__fprintf(&pos->user_dsos, fp);
454 }
455
456 return ret;
457}
458
459size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
460 bool (skip)(struct dso *dso, int parm), int parm)
461{
462 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
463 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
464}
465
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300466size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300467 bool (skip)(struct dso *dso, int parm), int parm)
468{
469 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300470 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300471
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300472 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300473 struct machine *pos = rb_entry(nd, struct machine, rb_node);
474 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
475 }
476 return ret;
477}
478
479size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
480{
481 int i;
482 size_t printed = 0;
483 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
484
485 if (kdso->has_build_id) {
486 char filename[PATH_MAX];
487 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
488 printed += fprintf(fp, "[0] %s\n", filename);
489 }
490
491 for (i = 0; i < vmlinux_path__nr_entries; ++i)
492 printed += fprintf(fp, "[%d] %s\n",
493 i + kdso->has_build_id, vmlinux_path[i]);
494
495 return printed;
496}
497
498size_t machine__fprintf(struct machine *machine, FILE *fp)
499{
500 size_t ret = 0;
501 struct rb_node *nd;
502
503 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
504 struct thread *pos = rb_entry(nd, struct thread, rb_node);
505
506 ret += thread__fprintf(pos, fp);
507 }
508
509 return ret;
510}
511
512static struct dso *machine__get_kernel(struct machine *machine)
513{
514 const char *vmlinux_name = NULL;
515 struct dso *kernel;
516
517 if (machine__is_host(machine)) {
518 vmlinux_name = symbol_conf.vmlinux_name;
519 if (!vmlinux_name)
520 vmlinux_name = "[kernel.kallsyms]";
521
522 kernel = dso__kernel_findnew(machine, vmlinux_name,
523 "[kernel]",
524 DSO_TYPE_KERNEL);
525 } else {
526 char bf[PATH_MAX];
527
528 if (machine__is_default_guest(machine))
529 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
530 if (!vmlinux_name)
531 vmlinux_name = machine__mmap_name(machine, bf,
532 sizeof(bf));
533
534 kernel = dso__kernel_findnew(machine, vmlinux_name,
535 "[guest.kernel]",
536 DSO_TYPE_GUEST_KERNEL);
537 }
538
539 if (kernel != NULL && (!kernel->has_build_id))
540 dso__read_running_kernel_build_id(kernel, machine);
541
542 return kernel;
543}
544
545struct process_args {
546 u64 start;
547};
548
Adrian Hunter15a0a872014-01-29 16:14:38 +0200549static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
550 size_t bufsz)
551{
552 if (machine__is_default_guest(machine))
553 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
554 else
555 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
556}
557
Simon Quea93f0e52014-06-16 11:32:09 -0700558const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
559
560/* Figure out the start address of kernel map from /proc/kallsyms.
561 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
562 * symbol_name if it's not that important.
563 */
564static u64 machine__get_kernel_start_addr(struct machine *machine,
565 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300566{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200567 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700568 int i;
569 const char *name;
570 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300571
Adrian Hunter15a0a872014-01-29 16:14:38 +0200572 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300573
574 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
575 return 0;
576
Simon Quea93f0e52014-06-16 11:32:09 -0700577 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
578 addr = kallsyms__get_function_start(filename, name);
579 if (addr)
580 break;
581 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300582
Simon Quea93f0e52014-06-16 11:32:09 -0700583 if (symbol_name)
584 *symbol_name = name;
585
586 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300587}
588
589int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
590{
591 enum map_type type;
Simon Quea93f0e52014-06-16 11:32:09 -0700592 u64 start = machine__get_kernel_start_addr(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300593
594 for (type = 0; type < MAP__NR_TYPES; ++type) {
595 struct kmap *kmap;
596
597 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
598 if (machine->vmlinux_maps[type] == NULL)
599 return -1;
600
601 machine->vmlinux_maps[type]->map_ip =
602 machine->vmlinux_maps[type]->unmap_ip =
603 identity__map_ip;
604 kmap = map__kmap(machine->vmlinux_maps[type]);
605 kmap->kmaps = &machine->kmaps;
606 map_groups__insert(&machine->kmaps,
607 machine->vmlinux_maps[type]);
608 }
609
610 return 0;
611}
612
613void machine__destroy_kernel_maps(struct machine *machine)
614{
615 enum map_type type;
616
617 for (type = 0; type < MAP__NR_TYPES; ++type) {
618 struct kmap *kmap;
619
620 if (machine->vmlinux_maps[type] == NULL)
621 continue;
622
623 kmap = map__kmap(machine->vmlinux_maps[type]);
624 map_groups__remove(&machine->kmaps,
625 machine->vmlinux_maps[type]);
626 if (kmap->ref_reloc_sym) {
627 /*
628 * ref_reloc_sym is shared among all maps, so free just
629 * on one of them.
630 */
631 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300632 zfree((char **)&kmap->ref_reloc_sym->name);
633 zfree(&kmap->ref_reloc_sym);
634 } else
635 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300636 }
637
638 map__delete(machine->vmlinux_maps[type]);
639 machine->vmlinux_maps[type] = NULL;
640 }
641}
642
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300643int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300644{
645 int ret = 0;
646 struct dirent **namelist = NULL;
647 int i, items = 0;
648 char path[PATH_MAX];
649 pid_t pid;
650 char *endp;
651
652 if (symbol_conf.default_guest_vmlinux_name ||
653 symbol_conf.default_guest_modules ||
654 symbol_conf.default_guest_kallsyms) {
655 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
656 }
657
658 if (symbol_conf.guestmount) {
659 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
660 if (items <= 0)
661 return -ENOENT;
662 for (i = 0; i < items; i++) {
663 if (!isdigit(namelist[i]->d_name[0])) {
664 /* Filter out . and .. */
665 continue;
666 }
667 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
668 if ((*endp != '\0') ||
669 (endp == namelist[i]->d_name) ||
670 (errno == ERANGE)) {
671 pr_debug("invalid directory (%s). Skipping.\n",
672 namelist[i]->d_name);
673 continue;
674 }
675 sprintf(path, "%s/%s/proc/kallsyms",
676 symbol_conf.guestmount,
677 namelist[i]->d_name);
678 ret = access(path, R_OK);
679 if (ret) {
680 pr_debug("Can't access file %s\n", path);
681 goto failure;
682 }
683 machines__create_kernel_maps(machines, pid);
684 }
685failure:
686 free(namelist);
687 }
688
689 return ret;
690}
691
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300692void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300693{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300694 struct rb_node *next = rb_first(&machines->guests);
695
696 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300697
698 while (next) {
699 struct machine *pos = rb_entry(next, struct machine, rb_node);
700
701 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300702 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300703 machine__delete(pos);
704 }
705}
706
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300707int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300708{
709 struct machine *machine = machines__findnew(machines, pid);
710
711 if (machine == NULL)
712 return -1;
713
714 return machine__create_kernel_maps(machine);
715}
716
717int machine__load_kallsyms(struct machine *machine, const char *filename,
718 enum map_type type, symbol_filter_t filter)
719{
720 struct map *map = machine->vmlinux_maps[type];
721 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
722
723 if (ret > 0) {
724 dso__set_loaded(map->dso, type);
725 /*
726 * Since /proc/kallsyms will have multiple sessions for the
727 * kernel, with modules between them, fixup the end of all
728 * sections.
729 */
730 __map_groups__fixup_end(&machine->kmaps, type);
731 }
732
733 return ret;
734}
735
736int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
737 symbol_filter_t filter)
738{
739 struct map *map = machine->vmlinux_maps[type];
740 int ret = dso__load_vmlinux_path(map->dso, map, filter);
741
Adrian Hunter39b12f782013-08-07 14:38:47 +0300742 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300743 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300744
745 return ret;
746}
747
748static void map_groups__fixup_end(struct map_groups *mg)
749{
750 int i;
751 for (i = 0; i < MAP__NR_TYPES; ++i)
752 __map_groups__fixup_end(mg, i);
753}
754
755static char *get_kernel_version(const char *root_dir)
756{
757 char version[PATH_MAX];
758 FILE *file;
759 char *name, *tmp;
760 const char *prefix = "Linux version ";
761
762 sprintf(version, "%s/proc/version", root_dir);
763 file = fopen(version, "r");
764 if (!file)
765 return NULL;
766
767 version[0] = '\0';
768 tmp = fgets(version, sizeof(version), file);
769 fclose(file);
770
771 name = strstr(version, prefix);
772 if (!name)
773 return NULL;
774 name += strlen(prefix);
775 tmp = strchr(name, ' ');
776 if (tmp)
777 *tmp = '\0';
778
779 return strdup(name);
780}
781
782static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400783 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300784{
785 struct dirent *dent;
786 DIR *dir = opendir(dir_name);
787 int ret = 0;
788
789 if (!dir) {
790 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
791 return -1;
792 }
793
794 while ((dent = readdir(dir)) != NULL) {
795 char path[PATH_MAX];
796 struct stat st;
797
798 /*sshfs might return bad dent->d_type, so we have to stat*/
799 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
800 if (stat(path, &st))
801 continue;
802
803 if (S_ISDIR(st.st_mode)) {
804 if (!strcmp(dent->d_name, ".") ||
805 !strcmp(dent->d_name, ".."))
806 continue;
807
Richard Yao61d42902014-04-26 13:17:55 -0400808 /* Do not follow top-level source and build symlinks */
809 if (depth == 0) {
810 if (!strcmp(dent->d_name, "source") ||
811 !strcmp(dent->d_name, "build"))
812 continue;
813 }
814
815 ret = map_groups__set_modules_path_dir(mg, path,
816 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300817 if (ret < 0)
818 goto out;
819 } else {
820 char *dot = strrchr(dent->d_name, '.'),
821 dso_name[PATH_MAX];
822 struct map *map;
823 char *long_name;
824
825 if (dot == NULL || strcmp(dot, ".ko"))
826 continue;
827 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
828 (int)(dot - dent->d_name), dent->d_name);
829
830 strxfrchar(dso_name, '-', '_');
831 map = map_groups__find_by_name(mg, MAP__FUNCTION,
832 dso_name);
833 if (map == NULL)
834 continue;
835
836 long_name = strdup(path);
837 if (long_name == NULL) {
838 ret = -1;
839 goto out;
840 }
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300841 dso__set_long_name(map->dso, long_name, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300842 dso__kernel_module_get_build_id(map->dso, "");
843 }
844 }
845
846out:
847 closedir(dir);
848 return ret;
849}
850
851static int machine__set_modules_path(struct machine *machine)
852{
853 char *version;
854 char modules_path[PATH_MAX];
855
856 version = get_kernel_version(machine->root_dir);
857 if (!version)
858 return -1;
859
Richard Yao61d42902014-04-26 13:17:55 -0400860 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300861 machine->root_dir, version);
862 free(version);
863
Richard Yao61d42902014-04-26 13:17:55 -0400864 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300865}
866
Adrian Hunter316d70d2013-10-08 11:45:48 +0300867static int machine__create_module(void *arg, const char *name, u64 start)
868{
869 struct machine *machine = arg;
870 struct map *map;
871
872 map = machine__new_module(machine, start, name);
873 if (map == NULL)
874 return -1;
875
876 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
877
878 return 0;
879}
880
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300881static int machine__create_modules(struct machine *machine)
882{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300883 const char *modules;
884 char path[PATH_MAX];
885
Adrian Hunterf4be9042013-09-22 13:22:09 +0300886 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300887 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +0300888 } else {
889 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300890 modules = path;
891 }
892
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +0300893 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300894 return -1;
895
Adrian Hunter316d70d2013-10-08 11:45:48 +0300896 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300897 return -1;
898
Adrian Hunter316d70d2013-10-08 11:45:48 +0300899 if (!machine__set_modules_path(machine))
900 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300901
Adrian Hunter316d70d2013-10-08 11:45:48 +0300902 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300903
Jason Wessel8f76fcd2013-07-15 15:27:53 -0500904 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300905}
906
907int machine__create_kernel_maps(struct machine *machine)
908{
909 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200910 const char *name;
Simon Quea93f0e52014-06-16 11:32:09 -0700911 u64 addr = machine__get_kernel_start_addr(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200912 if (!addr)
913 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300914
915 if (kernel == NULL ||
916 __machine__create_kernel_maps(machine, kernel) < 0)
917 return -1;
918
919 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
920 if (machine__is_host(machine))
921 pr_debug("Problems creating module maps, "
922 "continuing anyway...\n");
923 else
924 pr_debug("Problems creating module maps for guest %d, "
925 "continuing anyway...\n", machine->pid);
926 }
927
928 /*
929 * Now that we have all the maps created, just set the ->end of them:
930 */
931 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200932
933 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
934 addr)) {
935 machine__destroy_kernel_maps(machine);
936 return -1;
937 }
938
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300939 return 0;
940}
941
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300942static void machine__set_kernel_mmap_len(struct machine *machine,
943 union perf_event *event)
944{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +0900945 int i;
946
947 for (i = 0; i < MAP__NR_TYPES; i++) {
948 machine->vmlinux_maps[i]->start = event->mmap.start;
949 machine->vmlinux_maps[i]->end = (event->mmap.start +
950 event->mmap.len);
951 /*
952 * Be a bit paranoid here, some perf.data file came with
953 * a zero sized synthesized MMAP event for the kernel.
954 */
955 if (machine->vmlinux_maps[i]->end == 0)
956 machine->vmlinux_maps[i]->end = ~0ULL;
957 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300958}
959
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300960static bool machine__uses_kcore(struct machine *machine)
961{
962 struct dso *dso;
963
964 list_for_each_entry(dso, &machine->kernel_dsos, node) {
965 if (dso__is_kcore(dso))
966 return true;
967 }
968
969 return false;
970}
971
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300972static int machine__process_kernel_mmap_event(struct machine *machine,
973 union perf_event *event)
974{
975 struct map *map;
976 char kmmap_prefix[PATH_MAX];
977 enum dso_kernel_type kernel_type;
978 bool is_kernel_mmap;
979
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300980 /* If we have maps from kcore then we do not need or want any others */
981 if (machine__uses_kcore(machine))
982 return 0;
983
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300984 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
985 if (machine__is_host(machine))
986 kernel_type = DSO_TYPE_KERNEL;
987 else
988 kernel_type = DSO_TYPE_GUEST_KERNEL;
989
990 is_kernel_mmap = memcmp(event->mmap.filename,
991 kmmap_prefix,
992 strlen(kmmap_prefix) - 1) == 0;
993 if (event->mmap.filename[0] == '/' ||
994 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
995
996 char short_module_name[1024];
997 char *name, *dot;
998
999 if (event->mmap.filename[0] == '/') {
1000 name = strrchr(event->mmap.filename, '/');
1001 if (name == NULL)
1002 goto out_problem;
1003
1004 ++name; /* skip / */
1005 dot = strrchr(name, '.');
1006 if (dot == NULL)
1007 goto out_problem;
1008 snprintf(short_module_name, sizeof(short_module_name),
1009 "[%.*s]", (int)(dot - name), name);
1010 strxfrchar(short_module_name, '-', '_');
1011 } else
1012 strcpy(short_module_name, event->mmap.filename);
1013
1014 map = machine__new_module(machine, event->mmap.start,
1015 event->mmap.filename);
1016 if (map == NULL)
1017 goto out_problem;
1018
1019 name = strdup(short_module_name);
1020 if (name == NULL)
1021 goto out_problem;
1022
Adrian Hunter58a98c92013-12-10 11:11:46 -03001023 dso__set_short_name(map->dso, name, true);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001024 map->end = map->start + event->mmap.len;
1025 } else if (is_kernel_mmap) {
1026 const char *symbol_name = (event->mmap.filename +
1027 strlen(kmmap_prefix));
1028 /*
1029 * Should be there already, from the build-id table in
1030 * the header.
1031 */
1032 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1033 kmmap_prefix);
1034 if (kernel == NULL)
1035 goto out_problem;
1036
1037 kernel->kernel = kernel_type;
1038 if (__machine__create_kernel_maps(machine, kernel) < 0)
1039 goto out_problem;
1040
1041 machine__set_kernel_mmap_len(machine, event);
1042
1043 /*
1044 * Avoid using a zero address (kptr_restrict) for the ref reloc
1045 * symbol. Effectively having zero here means that at record
1046 * time /proc/sys/kernel/kptr_restrict was non zero.
1047 */
1048 if (event->mmap.pgoff != 0) {
1049 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1050 symbol_name,
1051 event->mmap.pgoff);
1052 }
1053
1054 if (machine__is_default_guest(machine)) {
1055 /*
1056 * preload dso of guest kernel and modules
1057 */
1058 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1059 NULL);
1060 }
1061 }
1062 return 0;
1063out_problem:
1064 return -1;
1065}
1066
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001067int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001068 union perf_event *event,
1069 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001070{
1071 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1072 struct thread *thread;
1073 struct map *map;
1074 enum map_type type;
1075 int ret = 0;
1076
1077 if (dump_trace)
1078 perf_event__fprintf_mmap2(event, stdout);
1079
1080 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1081 cpumode == PERF_RECORD_MISC_KERNEL) {
1082 ret = machine__process_kernel_mmap_event(machine, event);
1083 if (ret < 0)
1084 goto out_problem;
1085 return 0;
1086 }
1087
1088 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001089 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001090 if (thread == NULL)
1091 goto out_problem;
1092
1093 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1094 type = MAP__VARIABLE;
1095 else
1096 type = MAP__FUNCTION;
1097
1098 map = map__new(&machine->user_dsos, event->mmap2.start,
1099 event->mmap2.len, event->mmap2.pgoff,
1100 event->mmap2.pid, event->mmap2.maj,
1101 event->mmap2.min, event->mmap2.ino,
1102 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001103 event->mmap2.prot,
1104 event->mmap2.flags,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001105 event->mmap2.filename, type);
1106
1107 if (map == NULL)
1108 goto out_problem;
1109
1110 thread__insert_map(thread, map);
1111 return 0;
1112
1113out_problem:
1114 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1115 return 0;
1116}
1117
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001118int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1119 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001120{
1121 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1122 struct thread *thread;
1123 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001124 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001125 int ret = 0;
1126
1127 if (dump_trace)
1128 perf_event__fprintf_mmap(event, stdout);
1129
1130 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1131 cpumode == PERF_RECORD_MISC_KERNEL) {
1132 ret = machine__process_kernel_mmap_event(machine, event);
1133 if (ret < 0)
1134 goto out_problem;
1135 return 0;
1136 }
1137
Adrian Hunter314add62013-08-27 11:23:03 +03001138 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001139 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001140 if (thread == NULL)
1141 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001142
1143 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1144 type = MAP__VARIABLE;
1145 else
1146 type = MAP__FUNCTION;
1147
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001148 map = map__new(&machine->user_dsos, event->mmap.start,
1149 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001150 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001151 event->mmap.filename,
Stephane Eranianbad40912013-01-24 16:10:40 +01001152 type);
1153
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001154 if (map == NULL)
1155 goto out_problem;
1156
1157 thread__insert_map(thread, map);
1158 return 0;
1159
1160out_problem:
1161 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1162 return 0;
1163}
1164
David Ahern236a3bb2013-08-14 08:49:27 -06001165static void machine__remove_thread(struct machine *machine, struct thread *th)
1166{
1167 machine->last_match = NULL;
1168 rb_erase(&th->rb_node, &machine->threads);
1169 /*
1170 * We may have references to this thread, for instance in some hist_entry
1171 * instances, so just move them to a separate list.
1172 */
1173 list_add_tail(&th->node, &machine->dead_threads);
1174}
1175
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001176int machine__process_fork_event(struct machine *machine, union perf_event *event,
1177 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001178{
Jiri Olsad75e6092014-03-14 15:00:03 +01001179 struct thread *thread = machine__find_thread(machine,
1180 event->fork.pid,
1181 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001182 struct thread *parent = machine__findnew_thread(machine,
1183 event->fork.ppid,
1184 event->fork.ptid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001185
David Ahern236a3bb2013-08-14 08:49:27 -06001186 /* if a thread currently exists for the thread id remove it */
1187 if (thread != NULL)
1188 machine__remove_thread(machine, thread);
1189
Adrian Hunter314add62013-08-27 11:23:03 +03001190 thread = machine__findnew_thread(machine, event->fork.pid,
1191 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001192 if (dump_trace)
1193 perf_event__fprintf_task(event, stdout);
1194
1195 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001196 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001197 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1198 return -1;
1199 }
1200
1201 return 0;
1202}
1203
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001204int machine__process_exit_event(struct machine *machine, union perf_event *event,
1205 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001206{
Jiri Olsad75e6092014-03-14 15:00:03 +01001207 struct thread *thread = machine__find_thread(machine,
1208 event->fork.pid,
1209 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001210
1211 if (dump_trace)
1212 perf_event__fprintf_task(event, stdout);
1213
1214 if (thread != NULL)
David Ahern236a3bb2013-08-14 08:49:27 -06001215 thread__exited(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001216
1217 return 0;
1218}
1219
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001220int machine__process_event(struct machine *machine, union perf_event *event,
1221 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001222{
1223 int ret;
1224
1225 switch (event->header.type) {
1226 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001227 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001228 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001229 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001230 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001231 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001232 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001233 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001234 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001235 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001236 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001237 ret = machine__process_lost_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001238 default:
1239 ret = -1;
1240 break;
1241 }
1242
1243 return ret;
1244}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001245
Greg Priceb21484f2012-12-06 21:48:05 -08001246static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001247{
Greg Priceb21484f2012-12-06 21:48:05 -08001248 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001249 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001250 return 0;
1251}
1252
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001253static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1254 struct addr_map_symbol *ams,
1255 u64 ip)
1256{
1257 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001258
1259 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001260 /*
1261 * We cannot use the header.misc hint to determine whether a
1262 * branch stack address is user, kernel, guest, hypervisor.
1263 * Branches may straddle the kernel/user/hypervisor boundaries.
1264 * Thus, we have to try consecutively until we find a match
1265 * or else, the symbol is unknown
1266 */
1267 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001268
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001269 ams->addr = ip;
1270 ams->al_addr = al.addr;
1271 ams->sym = al.sym;
1272 ams->map = al.map;
1273}
1274
Stephane Eranian98a3b322013-01-24 16:10:35 +01001275static void ip__resolve_data(struct machine *machine, struct thread *thread,
1276 u8 m, struct addr_map_symbol *ams, u64 addr)
1277{
1278 struct addr_location al;
1279
1280 memset(&al, 0, sizeof(al));
1281
Adrian Hunter61710bd2013-08-08 14:32:26 +03001282 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1283 &al);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001284 ams->addr = addr;
1285 ams->al_addr = al.addr;
1286 ams->sym = al.sym;
1287 ams->map = al.map;
1288}
1289
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001290struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1291 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001292{
1293 struct mem_info *mi = zalloc(sizeof(*mi));
1294
1295 if (!mi)
1296 return NULL;
1297
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001298 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1299 ip__resolve_data(al->machine, al->thread, al->cpumode,
1300 &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001301 mi->data_src.val = sample->data_src;
1302
1303 return mi;
1304}
1305
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001306struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1307 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001308{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001309 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001310 const struct branch_stack *bs = sample->branch_stack;
1311 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001312
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001313 if (!bi)
1314 return NULL;
1315
1316 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001317 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1318 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001319 bi[i].flags = bs->entries[i].flags;
1320 }
1321 return bi;
1322}
1323
1324static int machine__resolve_callchain_sample(struct machine *machine,
1325 struct thread *thread,
1326 struct ip_callchain *chain,
Greg Priceb21484f2012-12-06 21:48:05 -08001327 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001328 struct addr_location *root_al,
1329 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001330{
1331 u8 cpumode = PERF_RECORD_MISC_USER;
Waiman Long91e95612013-10-18 10:38:48 -04001332 int chain_nr = min(max_stack, (int)chain->nr);
1333 int i;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001334 int j;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001335 int err;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001336 int skip_idx __maybe_unused;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001337
1338 callchain_cursor_reset(&callchain_cursor);
1339
1340 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1341 pr_warning("corrupted callchain. skipping...\n");
1342 return 0;
1343 }
1344
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001345 /*
1346 * Based on DWARF debug information, some architectures skip
1347 * a callchain entry saved by the kernel.
1348 */
1349 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1350
Waiman Long91e95612013-10-18 10:38:48 -04001351 for (i = 0; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001352 u64 ip;
1353 struct addr_location al;
1354
1355 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001356 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001357 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001358 j = chain->nr - i - 1;
1359
1360#ifdef HAVE_SKIP_CALLCHAIN_IDX
1361 if (j == skip_idx)
1362 continue;
1363#endif
1364 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001365
1366 if (ip >= PERF_CONTEXT_MAX) {
1367 switch (ip) {
1368 case PERF_CONTEXT_HV:
1369 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1370 break;
1371 case PERF_CONTEXT_KERNEL:
1372 cpumode = PERF_RECORD_MISC_KERNEL;
1373 break;
1374 case PERF_CONTEXT_USER:
1375 cpumode = PERF_RECORD_MISC_USER;
1376 break;
1377 default:
1378 pr_debug("invalid callchain context: "
1379 "%"PRId64"\n", (s64) ip);
1380 /*
1381 * It seems the callchain is corrupted.
1382 * Discard all.
1383 */
1384 callchain_cursor_reset(&callchain_cursor);
1385 return 0;
1386 }
1387 continue;
1388 }
1389
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001390 al.filtered = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001391 thread__find_addr_location(thread, machine, cpumode,
Adrian Hunter61710bd2013-08-08 14:32:26 +03001392 MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001393 if (al.sym != NULL) {
1394 if (sort__has_parent && !*parent &&
Greg Priceb21484f2012-12-06 21:48:05 -08001395 symbol__match_regex(al.sym, &parent_regex))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001396 *parent = al.sym;
Greg Priceb21484f2012-12-06 21:48:05 -08001397 else if (have_ignore_callees && root_al &&
1398 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1399 /* Treat this symbol as the root,
1400 forgetting its callees. */
1401 *root_al = al;
1402 callchain_cursor_reset(&callchain_cursor);
1403 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001404 }
1405
1406 err = callchain_cursor_append(&callchain_cursor,
1407 ip, al.map, al.sym);
1408 if (err)
1409 return err;
1410 }
1411
1412 return 0;
1413}
1414
1415static int unwind_entry(struct unwind_entry *entry, void *arg)
1416{
1417 struct callchain_cursor *cursor = arg;
1418 return callchain_cursor_append(cursor, entry->ip,
1419 entry->map, entry->sym);
1420}
1421
1422int machine__resolve_callchain(struct machine *machine,
1423 struct perf_evsel *evsel,
1424 struct thread *thread,
1425 struct perf_sample *sample,
Greg Priceb21484f2012-12-06 21:48:05 -08001426 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001427 struct addr_location *root_al,
1428 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001429{
1430 int ret;
1431
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001432 ret = machine__resolve_callchain_sample(machine, thread,
Waiman Long91e95612013-10-18 10:38:48 -04001433 sample->callchain, parent,
1434 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001435 if (ret)
1436 return ret;
1437
1438 /* Can we do dwarf post unwind? */
1439 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1440 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1441 return 0;
1442
1443 /* Bail out if nothing was captured. */
1444 if ((!sample->user_regs.regs) ||
1445 (!sample->user_stack.size))
1446 return 0;
1447
1448 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
Jiri Olsa352ea452014-01-07 13:47:25 +01001449 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001450
1451}
David Ahern35feee12013-09-28 13:12:58 -06001452
1453int machine__for_each_thread(struct machine *machine,
1454 int (*fn)(struct thread *thread, void *p),
1455 void *priv)
1456{
1457 struct rb_node *nd;
1458 struct thread *thread;
1459 int rc = 0;
1460
1461 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1462 thread = rb_entry(nd, struct thread, rb_node);
1463 rc = fn(thread, priv);
1464 if (rc != 0)
1465 return rc;
1466 }
1467
1468 list_for_each_entry(thread, &machine->dead_threads, node) {
1469 rc = fn(thread, priv);
1470 if (rc != 0)
1471 return rc;
1472 }
1473 return rc;
1474}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001475
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001476int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001477 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001478 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001479{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001480 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001481 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001482 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001483 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1484 /* command specified */
1485 return 0;
1486}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001487
1488pid_t machine__get_current_tid(struct machine *machine, int cpu)
1489{
1490 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1491 return -1;
1492
1493 return machine->current_tid[cpu];
1494}
1495
1496int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1497 pid_t tid)
1498{
1499 struct thread *thread;
1500
1501 if (cpu < 0)
1502 return -EINVAL;
1503
1504 if (!machine->current_tid) {
1505 int i;
1506
1507 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1508 if (!machine->current_tid)
1509 return -ENOMEM;
1510 for (i = 0; i < MAX_NR_CPUS; i++)
1511 machine->current_tid[i] = -1;
1512 }
1513
1514 if (cpu >= MAX_NR_CPUS) {
1515 pr_err("Requested CPU %d too large. ", cpu);
1516 pr_err("Consider raising MAX_NR_CPUS\n");
1517 return -EINVAL;
1518 }
1519
1520 machine->current_tid[cpu] = tid;
1521
1522 thread = machine__findnew_thread(machine, pid, tid);
1523 if (!thread)
1524 return -ENOMEM;
1525
1526 thread->cpu = cpu;
1527
1528 return 0;
1529}