blob: 93c8b6fbc799c12c10290a8dbb10d7b429687213 [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
48 return 0;
49}
50
David Ahern8fb598e2013-09-28 13:13:00 -060051struct machine *machine__new_host(void)
52{
53 struct machine *machine = malloc(sizeof(*machine));
54
55 if (machine != NULL) {
56 machine__init(machine, "", HOST_KERNEL_ID);
57
58 if (machine__create_kernel_maps(machine) < 0)
59 goto out_delete;
60 }
61
62 return machine;
63out_delete:
64 free(machine);
65 return NULL;
66}
67
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030068static void dsos__delete(struct list_head *dsos)
69{
70 struct dso *pos, *n;
71
72 list_for_each_entry_safe(pos, n, dsos, node) {
73 list_del(&pos->node);
74 dso__delete(pos);
75 }
76}
77
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030078void machine__delete_dead_threads(struct machine *machine)
79{
80 struct thread *n, *t;
81
82 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
83 list_del(&t->node);
84 thread__delete(t);
85 }
86}
87
88void machine__delete_threads(struct machine *machine)
89{
90 struct rb_node *nd = rb_first(&machine->threads);
91
92 while (nd) {
93 struct thread *t = rb_entry(nd, struct thread, rb_node);
94
95 rb_erase(&t->rb_node, &machine->threads);
96 nd = rb_next(nd);
97 thread__delete(t);
98 }
99}
100
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300101void machine__exit(struct machine *machine)
102{
103 map_groups__exit(&machine->kmaps);
104 dsos__delete(&machine->user_dsos);
105 dsos__delete(&machine->kernel_dsos);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300106 zfree(&machine->root_dir);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300107}
108
109void machine__delete(struct machine *machine)
110{
111 machine__exit(machine);
112 free(machine);
113}
114
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300115void machines__init(struct machines *machines)
116{
117 machine__init(&machines->host, "", HOST_KERNEL_ID);
118 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300119 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300120}
121
122void machines__exit(struct machines *machines)
123{
124 machine__exit(&machines->host);
125 /* XXX exit guest */
126}
127
128struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300129 const char *root_dir)
130{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300131 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300132 struct rb_node *parent = NULL;
133 struct machine *pos, *machine = malloc(sizeof(*machine));
134
135 if (machine == NULL)
136 return NULL;
137
138 if (machine__init(machine, root_dir, pid) != 0) {
139 free(machine);
140 return NULL;
141 }
142
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300143 machine->symbol_filter = machines->symbol_filter;
144
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300145 while (*p != NULL) {
146 parent = *p;
147 pos = rb_entry(parent, struct machine, rb_node);
148 if (pid < pos->pid)
149 p = &(*p)->rb_left;
150 else
151 p = &(*p)->rb_right;
152 }
153
154 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300155 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300156
157 return machine;
158}
159
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300160void machines__set_symbol_filter(struct machines *machines,
161 symbol_filter_t symbol_filter)
162{
163 struct rb_node *nd;
164
165 machines->symbol_filter = symbol_filter;
166 machines->host.symbol_filter = symbol_filter;
167
168 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
169 struct machine *machine = rb_entry(nd, struct machine, rb_node);
170
171 machine->symbol_filter = symbol_filter;
172 }
173}
174
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300175struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300176{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300177 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300178 struct rb_node *parent = NULL;
179 struct machine *machine;
180 struct machine *default_machine = NULL;
181
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300182 if (pid == HOST_KERNEL_ID)
183 return &machines->host;
184
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185 while (*p != NULL) {
186 parent = *p;
187 machine = rb_entry(parent, struct machine, rb_node);
188 if (pid < machine->pid)
189 p = &(*p)->rb_left;
190 else if (pid > machine->pid)
191 p = &(*p)->rb_right;
192 else
193 return machine;
194 if (!machine->pid)
195 default_machine = machine;
196 }
197
198 return default_machine;
199}
200
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300201struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300202{
203 char path[PATH_MAX];
204 const char *root_dir = "";
205 struct machine *machine = machines__find(machines, pid);
206
207 if (machine && (machine->pid == pid))
208 goto out;
209
210 if ((pid != HOST_KERNEL_ID) &&
211 (pid != DEFAULT_GUEST_KERNEL_ID) &&
212 (symbol_conf.guestmount)) {
213 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
214 if (access(path, R_OK)) {
215 static struct strlist *seen;
216
217 if (!seen)
218 seen = strlist__new(true, NULL);
219
220 if (!strlist__has_entry(seen, path)) {
221 pr_err("Can't access file %s\n", path);
222 strlist__add(seen, path);
223 }
224 machine = NULL;
225 goto out;
226 }
227 root_dir = path;
228 }
229
230 machine = machines__add(machines, pid, root_dir);
231out:
232 return machine;
233}
234
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300235void machines__process_guests(struct machines *machines,
236 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300237{
238 struct rb_node *nd;
239
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300240 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300241 struct machine *pos = rb_entry(nd, struct machine, rb_node);
242 process(pos, data);
243 }
244}
245
246char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
247{
248 if (machine__is_host(machine))
249 snprintf(bf, size, "[%s]", "kernel.kallsyms");
250 else if (machine__is_default_guest(machine))
251 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
252 else {
253 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
254 machine->pid);
255 }
256
257 return bf;
258}
259
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300260void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300261{
262 struct rb_node *node;
263 struct machine *machine;
264
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300265 machines->host.id_hdr_size = id_hdr_size;
266
267 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300268 machine = rb_entry(node, struct machine, rb_node);
269 machine->id_hdr_size = id_hdr_size;
270 }
271
272 return;
273}
274
Adrian Hunter29ce3612014-07-16 11:07:13 +0300275static void machine__update_thread_pid(struct machine *machine,
276 struct thread *th, pid_t pid)
277{
278 struct thread *leader;
279
280 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
281 return;
282
283 th->pid_ = pid;
284
285 if (th->pid_ == th->tid)
286 return;
287
288 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
289 if (!leader)
290 goto out_err;
291
292 if (!leader->mg)
293 leader->mg = map_groups__new();
294
295 if (!leader->mg)
296 goto out_err;
297
298 if (th->mg == leader->mg)
299 return;
300
301 if (th->mg) {
302 /*
303 * Maps are created from MMAP events which provide the pid and
304 * tid. Consequently there never should be any maps on a thread
305 * with an unknown pid. Just print an error if there are.
306 */
307 if (!map_groups__empty(th->mg))
308 pr_err("Discarding thread maps for %d:%d\n",
309 th->pid_, th->tid);
310 map_groups__delete(th->mg);
311 }
312
313 th->mg = map_groups__get(leader->mg);
314
315 return;
316
317out_err:
318 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
319}
320
Adrian Hunter99d725f2013-08-26 16:00:19 +0300321static struct thread *__machine__findnew_thread(struct machine *machine,
322 pid_t pid, pid_t tid,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300323 bool create)
324{
325 struct rb_node **p = &machine->threads.rb_node;
326 struct rb_node *parent = NULL;
327 struct thread *th;
328
329 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300330 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300331 * so most of the time we dont have to look up
332 * the full rbtree:
333 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300334 th = machine->last_match;
335 if (th && th->tid == tid) {
336 machine__update_thread_pid(machine, th, pid);
337 return th;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300338 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300339
340 while (*p != NULL) {
341 parent = *p;
342 th = rb_entry(parent, struct thread, rb_node);
343
Adrian Hunter38051232013-07-04 16:20:31 +0300344 if (th->tid == tid) {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300345 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300346 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300347 return th;
348 }
349
Adrian Hunter38051232013-07-04 16:20:31 +0300350 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300351 p = &(*p)->rb_left;
352 else
353 p = &(*p)->rb_right;
354 }
355
356 if (!create)
357 return NULL;
358
Adrian Hunter99d725f2013-08-26 16:00:19 +0300359 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300360 if (th != NULL) {
361 rb_link_node(&th->rb_node, parent, p);
362 rb_insert_color(&th->rb_node, &machine->threads);
363 machine->last_match = th;
Jiri Olsacddcef62014-04-09 20:54:29 +0200364
365 /*
366 * We have to initialize map_groups separately
367 * after rb tree is updated.
368 *
369 * The reason is that we call machine__findnew_thread
370 * within thread__init_map_groups to find the thread
371 * leader and that would screwed the rb tree.
372 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300373 if (thread__init_map_groups(th, machine)) {
374 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200375 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300376 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300377 }
378
379 return th;
380}
381
Adrian Hunter314add62013-08-27 11:23:03 +0300382struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
383 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300384{
Adrian Hunter314add62013-08-27 11:23:03 +0300385 return __machine__findnew_thread(machine, pid, tid, true);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300386}
387
Jiri Olsad75e6092014-03-14 15:00:03 +0100388struct thread *machine__find_thread(struct machine *machine, pid_t pid,
389 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300390{
Jiri Olsad75e6092014-03-14 15:00:03 +0100391 return __machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300392}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300393
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200394int machine__process_comm_event(struct machine *machine, union perf_event *event,
395 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300396{
Adrian Hunter314add62013-08-27 11:23:03 +0300397 struct thread *thread = machine__findnew_thread(machine,
398 event->comm.pid,
399 event->comm.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300400
401 if (dump_trace)
402 perf_event__fprintf_comm(event, stdout);
403
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200404 if (thread == NULL || thread__set_comm(thread, event->comm.comm, sample->time)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300405 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
406 return -1;
407 }
408
409 return 0;
410}
411
412int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200413 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300414{
415 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
416 event->lost.id, event->lost.lost);
417 return 0;
418}
419
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300420struct map *machine__new_module(struct machine *machine, u64 start,
421 const char *filename)
422{
423 struct map *map;
424 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
425
426 if (dso == NULL)
427 return NULL;
428
429 map = map__new2(start, dso, MAP__FUNCTION);
430 if (map == NULL)
431 return NULL;
432
433 if (machine__is_host(machine))
434 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
435 else
436 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
437 map_groups__insert(&machine->kmaps, map);
438 return map;
439}
440
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300441size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300442{
443 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300444 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
445 __dsos__fprintf(&machines->host.user_dsos, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300446
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300447 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300448 struct machine *pos = rb_entry(nd, struct machine, rb_node);
449 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
450 ret += __dsos__fprintf(&pos->user_dsos, fp);
451 }
452
453 return ret;
454}
455
456size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
457 bool (skip)(struct dso *dso, int parm), int parm)
458{
459 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
460 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
461}
462
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300463size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300464 bool (skip)(struct dso *dso, int parm), int parm)
465{
466 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300467 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300468
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300469 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300470 struct machine *pos = rb_entry(nd, struct machine, rb_node);
471 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
472 }
473 return ret;
474}
475
476size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
477{
478 int i;
479 size_t printed = 0;
480 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
481
482 if (kdso->has_build_id) {
483 char filename[PATH_MAX];
484 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
485 printed += fprintf(fp, "[0] %s\n", filename);
486 }
487
488 for (i = 0; i < vmlinux_path__nr_entries; ++i)
489 printed += fprintf(fp, "[%d] %s\n",
490 i + kdso->has_build_id, vmlinux_path[i]);
491
492 return printed;
493}
494
495size_t machine__fprintf(struct machine *machine, FILE *fp)
496{
497 size_t ret = 0;
498 struct rb_node *nd;
499
500 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
501 struct thread *pos = rb_entry(nd, struct thread, rb_node);
502
503 ret += thread__fprintf(pos, fp);
504 }
505
506 return ret;
507}
508
509static struct dso *machine__get_kernel(struct machine *machine)
510{
511 const char *vmlinux_name = NULL;
512 struct dso *kernel;
513
514 if (machine__is_host(machine)) {
515 vmlinux_name = symbol_conf.vmlinux_name;
516 if (!vmlinux_name)
517 vmlinux_name = "[kernel.kallsyms]";
518
519 kernel = dso__kernel_findnew(machine, vmlinux_name,
520 "[kernel]",
521 DSO_TYPE_KERNEL);
522 } else {
523 char bf[PATH_MAX];
524
525 if (machine__is_default_guest(machine))
526 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
527 if (!vmlinux_name)
528 vmlinux_name = machine__mmap_name(machine, bf,
529 sizeof(bf));
530
531 kernel = dso__kernel_findnew(machine, vmlinux_name,
532 "[guest.kernel]",
533 DSO_TYPE_GUEST_KERNEL);
534 }
535
536 if (kernel != NULL && (!kernel->has_build_id))
537 dso__read_running_kernel_build_id(kernel, machine);
538
539 return kernel;
540}
541
542struct process_args {
543 u64 start;
544};
545
Adrian Hunter15a0a872014-01-29 16:14:38 +0200546static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
547 size_t bufsz)
548{
549 if (machine__is_default_guest(machine))
550 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
551 else
552 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
553}
554
Simon Quea93f0e52014-06-16 11:32:09 -0700555const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
556
557/* Figure out the start address of kernel map from /proc/kallsyms.
558 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
559 * symbol_name if it's not that important.
560 */
561static u64 machine__get_kernel_start_addr(struct machine *machine,
562 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300563{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200564 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700565 int i;
566 const char *name;
567 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300568
Adrian Hunter15a0a872014-01-29 16:14:38 +0200569 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300570
571 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
572 return 0;
573
Simon Quea93f0e52014-06-16 11:32:09 -0700574 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
575 addr = kallsyms__get_function_start(filename, name);
576 if (addr)
577 break;
578 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300579
Simon Quea93f0e52014-06-16 11:32:09 -0700580 if (symbol_name)
581 *symbol_name = name;
582
583 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300584}
585
586int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
587{
588 enum map_type type;
Simon Quea93f0e52014-06-16 11:32:09 -0700589 u64 start = machine__get_kernel_start_addr(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300590
591 for (type = 0; type < MAP__NR_TYPES; ++type) {
592 struct kmap *kmap;
593
594 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
595 if (machine->vmlinux_maps[type] == NULL)
596 return -1;
597
598 machine->vmlinux_maps[type]->map_ip =
599 machine->vmlinux_maps[type]->unmap_ip =
600 identity__map_ip;
601 kmap = map__kmap(machine->vmlinux_maps[type]);
602 kmap->kmaps = &machine->kmaps;
603 map_groups__insert(&machine->kmaps,
604 machine->vmlinux_maps[type]);
605 }
606
607 return 0;
608}
609
610void machine__destroy_kernel_maps(struct machine *machine)
611{
612 enum map_type type;
613
614 for (type = 0; type < MAP__NR_TYPES; ++type) {
615 struct kmap *kmap;
616
617 if (machine->vmlinux_maps[type] == NULL)
618 continue;
619
620 kmap = map__kmap(machine->vmlinux_maps[type]);
621 map_groups__remove(&machine->kmaps,
622 machine->vmlinux_maps[type]);
623 if (kmap->ref_reloc_sym) {
624 /*
625 * ref_reloc_sym is shared among all maps, so free just
626 * on one of them.
627 */
628 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300629 zfree((char **)&kmap->ref_reloc_sym->name);
630 zfree(&kmap->ref_reloc_sym);
631 } else
632 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300633 }
634
635 map__delete(machine->vmlinux_maps[type]);
636 machine->vmlinux_maps[type] = NULL;
637 }
638}
639
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300640int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300641{
642 int ret = 0;
643 struct dirent **namelist = NULL;
644 int i, items = 0;
645 char path[PATH_MAX];
646 pid_t pid;
647 char *endp;
648
649 if (symbol_conf.default_guest_vmlinux_name ||
650 symbol_conf.default_guest_modules ||
651 symbol_conf.default_guest_kallsyms) {
652 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
653 }
654
655 if (symbol_conf.guestmount) {
656 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
657 if (items <= 0)
658 return -ENOENT;
659 for (i = 0; i < items; i++) {
660 if (!isdigit(namelist[i]->d_name[0])) {
661 /* Filter out . and .. */
662 continue;
663 }
664 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
665 if ((*endp != '\0') ||
666 (endp == namelist[i]->d_name) ||
667 (errno == ERANGE)) {
668 pr_debug("invalid directory (%s). Skipping.\n",
669 namelist[i]->d_name);
670 continue;
671 }
672 sprintf(path, "%s/%s/proc/kallsyms",
673 symbol_conf.guestmount,
674 namelist[i]->d_name);
675 ret = access(path, R_OK);
676 if (ret) {
677 pr_debug("Can't access file %s\n", path);
678 goto failure;
679 }
680 machines__create_kernel_maps(machines, pid);
681 }
682failure:
683 free(namelist);
684 }
685
686 return ret;
687}
688
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300689void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300690{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300691 struct rb_node *next = rb_first(&machines->guests);
692
693 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300694
695 while (next) {
696 struct machine *pos = rb_entry(next, struct machine, rb_node);
697
698 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300699 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300700 machine__delete(pos);
701 }
702}
703
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300704int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300705{
706 struct machine *machine = machines__findnew(machines, pid);
707
708 if (machine == NULL)
709 return -1;
710
711 return machine__create_kernel_maps(machine);
712}
713
714int machine__load_kallsyms(struct machine *machine, const char *filename,
715 enum map_type type, symbol_filter_t filter)
716{
717 struct map *map = machine->vmlinux_maps[type];
718 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
719
720 if (ret > 0) {
721 dso__set_loaded(map->dso, type);
722 /*
723 * Since /proc/kallsyms will have multiple sessions for the
724 * kernel, with modules between them, fixup the end of all
725 * sections.
726 */
727 __map_groups__fixup_end(&machine->kmaps, type);
728 }
729
730 return ret;
731}
732
733int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
734 symbol_filter_t filter)
735{
736 struct map *map = machine->vmlinux_maps[type];
737 int ret = dso__load_vmlinux_path(map->dso, map, filter);
738
Adrian Hunter39b12f782013-08-07 14:38:47 +0300739 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300740 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300741
742 return ret;
743}
744
745static void map_groups__fixup_end(struct map_groups *mg)
746{
747 int i;
748 for (i = 0; i < MAP__NR_TYPES; ++i)
749 __map_groups__fixup_end(mg, i);
750}
751
752static char *get_kernel_version(const char *root_dir)
753{
754 char version[PATH_MAX];
755 FILE *file;
756 char *name, *tmp;
757 const char *prefix = "Linux version ";
758
759 sprintf(version, "%s/proc/version", root_dir);
760 file = fopen(version, "r");
761 if (!file)
762 return NULL;
763
764 version[0] = '\0';
765 tmp = fgets(version, sizeof(version), file);
766 fclose(file);
767
768 name = strstr(version, prefix);
769 if (!name)
770 return NULL;
771 name += strlen(prefix);
772 tmp = strchr(name, ' ');
773 if (tmp)
774 *tmp = '\0';
775
776 return strdup(name);
777}
778
779static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400780 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300781{
782 struct dirent *dent;
783 DIR *dir = opendir(dir_name);
784 int ret = 0;
785
786 if (!dir) {
787 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
788 return -1;
789 }
790
791 while ((dent = readdir(dir)) != NULL) {
792 char path[PATH_MAX];
793 struct stat st;
794
795 /*sshfs might return bad dent->d_type, so we have to stat*/
796 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
797 if (stat(path, &st))
798 continue;
799
800 if (S_ISDIR(st.st_mode)) {
801 if (!strcmp(dent->d_name, ".") ||
802 !strcmp(dent->d_name, ".."))
803 continue;
804
Richard Yao61d42902014-04-26 13:17:55 -0400805 /* Do not follow top-level source and build symlinks */
806 if (depth == 0) {
807 if (!strcmp(dent->d_name, "source") ||
808 !strcmp(dent->d_name, "build"))
809 continue;
810 }
811
812 ret = map_groups__set_modules_path_dir(mg, path,
813 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300814 if (ret < 0)
815 goto out;
816 } else {
817 char *dot = strrchr(dent->d_name, '.'),
818 dso_name[PATH_MAX];
819 struct map *map;
820 char *long_name;
821
822 if (dot == NULL || strcmp(dot, ".ko"))
823 continue;
824 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
825 (int)(dot - dent->d_name), dent->d_name);
826
827 strxfrchar(dso_name, '-', '_');
828 map = map_groups__find_by_name(mg, MAP__FUNCTION,
829 dso_name);
830 if (map == NULL)
831 continue;
832
833 long_name = strdup(path);
834 if (long_name == NULL) {
835 ret = -1;
836 goto out;
837 }
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300838 dso__set_long_name(map->dso, long_name, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300839 dso__kernel_module_get_build_id(map->dso, "");
840 }
841 }
842
843out:
844 closedir(dir);
845 return ret;
846}
847
848static int machine__set_modules_path(struct machine *machine)
849{
850 char *version;
851 char modules_path[PATH_MAX];
852
853 version = get_kernel_version(machine->root_dir);
854 if (!version)
855 return -1;
856
Richard Yao61d42902014-04-26 13:17:55 -0400857 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300858 machine->root_dir, version);
859 free(version);
860
Richard Yao61d42902014-04-26 13:17:55 -0400861 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300862}
863
Adrian Hunter316d70d2013-10-08 11:45:48 +0300864static int machine__create_module(void *arg, const char *name, u64 start)
865{
866 struct machine *machine = arg;
867 struct map *map;
868
869 map = machine__new_module(machine, start, name);
870 if (map == NULL)
871 return -1;
872
873 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
874
875 return 0;
876}
877
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300878static int machine__create_modules(struct machine *machine)
879{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300880 const char *modules;
881 char path[PATH_MAX];
882
Adrian Hunterf4be9042013-09-22 13:22:09 +0300883 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300884 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +0300885 } else {
886 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300887 modules = path;
888 }
889
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +0300890 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300891 return -1;
892
Adrian Hunter316d70d2013-10-08 11:45:48 +0300893 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300894 return -1;
895
Adrian Hunter316d70d2013-10-08 11:45:48 +0300896 if (!machine__set_modules_path(machine))
897 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300898
Adrian Hunter316d70d2013-10-08 11:45:48 +0300899 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300900
Jason Wessel8f76fcd2013-07-15 15:27:53 -0500901 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300902}
903
904int machine__create_kernel_maps(struct machine *machine)
905{
906 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200907 const char *name;
Simon Quea93f0e52014-06-16 11:32:09 -0700908 u64 addr = machine__get_kernel_start_addr(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200909 if (!addr)
910 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300911
912 if (kernel == NULL ||
913 __machine__create_kernel_maps(machine, kernel) < 0)
914 return -1;
915
916 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
917 if (machine__is_host(machine))
918 pr_debug("Problems creating module maps, "
919 "continuing anyway...\n");
920 else
921 pr_debug("Problems creating module maps for guest %d, "
922 "continuing anyway...\n", machine->pid);
923 }
924
925 /*
926 * Now that we have all the maps created, just set the ->end of them:
927 */
928 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200929
930 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
931 addr)) {
932 machine__destroy_kernel_maps(machine);
933 return -1;
934 }
935
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300936 return 0;
937}
938
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300939static void machine__set_kernel_mmap_len(struct machine *machine,
940 union perf_event *event)
941{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +0900942 int i;
943
944 for (i = 0; i < MAP__NR_TYPES; i++) {
945 machine->vmlinux_maps[i]->start = event->mmap.start;
946 machine->vmlinux_maps[i]->end = (event->mmap.start +
947 event->mmap.len);
948 /*
949 * Be a bit paranoid here, some perf.data file came with
950 * a zero sized synthesized MMAP event for the kernel.
951 */
952 if (machine->vmlinux_maps[i]->end == 0)
953 machine->vmlinux_maps[i]->end = ~0ULL;
954 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300955}
956
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300957static bool machine__uses_kcore(struct machine *machine)
958{
959 struct dso *dso;
960
961 list_for_each_entry(dso, &machine->kernel_dsos, node) {
962 if (dso__is_kcore(dso))
963 return true;
964 }
965
966 return false;
967}
968
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300969static int machine__process_kernel_mmap_event(struct machine *machine,
970 union perf_event *event)
971{
972 struct map *map;
973 char kmmap_prefix[PATH_MAX];
974 enum dso_kernel_type kernel_type;
975 bool is_kernel_mmap;
976
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300977 /* If we have maps from kcore then we do not need or want any others */
978 if (machine__uses_kcore(machine))
979 return 0;
980
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300981 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
982 if (machine__is_host(machine))
983 kernel_type = DSO_TYPE_KERNEL;
984 else
985 kernel_type = DSO_TYPE_GUEST_KERNEL;
986
987 is_kernel_mmap = memcmp(event->mmap.filename,
988 kmmap_prefix,
989 strlen(kmmap_prefix) - 1) == 0;
990 if (event->mmap.filename[0] == '/' ||
991 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
992
993 char short_module_name[1024];
994 char *name, *dot;
995
996 if (event->mmap.filename[0] == '/') {
997 name = strrchr(event->mmap.filename, '/');
998 if (name == NULL)
999 goto out_problem;
1000
1001 ++name; /* skip / */
1002 dot = strrchr(name, '.');
1003 if (dot == NULL)
1004 goto out_problem;
1005 snprintf(short_module_name, sizeof(short_module_name),
1006 "[%.*s]", (int)(dot - name), name);
1007 strxfrchar(short_module_name, '-', '_');
1008 } else
1009 strcpy(short_module_name, event->mmap.filename);
1010
1011 map = machine__new_module(machine, event->mmap.start,
1012 event->mmap.filename);
1013 if (map == NULL)
1014 goto out_problem;
1015
1016 name = strdup(short_module_name);
1017 if (name == NULL)
1018 goto out_problem;
1019
Adrian Hunter58a98c92013-12-10 11:11:46 -03001020 dso__set_short_name(map->dso, name, true);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001021 map->end = map->start + event->mmap.len;
1022 } else if (is_kernel_mmap) {
1023 const char *symbol_name = (event->mmap.filename +
1024 strlen(kmmap_prefix));
1025 /*
1026 * Should be there already, from the build-id table in
1027 * the header.
1028 */
1029 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1030 kmmap_prefix);
1031 if (kernel == NULL)
1032 goto out_problem;
1033
1034 kernel->kernel = kernel_type;
1035 if (__machine__create_kernel_maps(machine, kernel) < 0)
1036 goto out_problem;
1037
1038 machine__set_kernel_mmap_len(machine, event);
1039
1040 /*
1041 * Avoid using a zero address (kptr_restrict) for the ref reloc
1042 * symbol. Effectively having zero here means that at record
1043 * time /proc/sys/kernel/kptr_restrict was non zero.
1044 */
1045 if (event->mmap.pgoff != 0) {
1046 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1047 symbol_name,
1048 event->mmap.pgoff);
1049 }
1050
1051 if (machine__is_default_guest(machine)) {
1052 /*
1053 * preload dso of guest kernel and modules
1054 */
1055 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1056 NULL);
1057 }
1058 }
1059 return 0;
1060out_problem:
1061 return -1;
1062}
1063
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001064int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001065 union perf_event *event,
1066 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001067{
1068 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1069 struct thread *thread;
1070 struct map *map;
1071 enum map_type type;
1072 int ret = 0;
1073
1074 if (dump_trace)
1075 perf_event__fprintf_mmap2(event, stdout);
1076
1077 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1078 cpumode == PERF_RECORD_MISC_KERNEL) {
1079 ret = machine__process_kernel_mmap_event(machine, event);
1080 if (ret < 0)
1081 goto out_problem;
1082 return 0;
1083 }
1084
1085 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001086 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001087 if (thread == NULL)
1088 goto out_problem;
1089
1090 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1091 type = MAP__VARIABLE;
1092 else
1093 type = MAP__FUNCTION;
1094
1095 map = map__new(&machine->user_dsos, event->mmap2.start,
1096 event->mmap2.len, event->mmap2.pgoff,
1097 event->mmap2.pid, event->mmap2.maj,
1098 event->mmap2.min, event->mmap2.ino,
1099 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001100 event->mmap2.prot,
1101 event->mmap2.flags,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001102 event->mmap2.filename, type);
1103
1104 if (map == NULL)
1105 goto out_problem;
1106
1107 thread__insert_map(thread, map);
1108 return 0;
1109
1110out_problem:
1111 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1112 return 0;
1113}
1114
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001115int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1116 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001117{
1118 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1119 struct thread *thread;
1120 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001121 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001122 int ret = 0;
1123
1124 if (dump_trace)
1125 perf_event__fprintf_mmap(event, stdout);
1126
1127 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1128 cpumode == PERF_RECORD_MISC_KERNEL) {
1129 ret = machine__process_kernel_mmap_event(machine, event);
1130 if (ret < 0)
1131 goto out_problem;
1132 return 0;
1133 }
1134
Adrian Hunter314add62013-08-27 11:23:03 +03001135 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001136 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001137 if (thread == NULL)
1138 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001139
1140 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1141 type = MAP__VARIABLE;
1142 else
1143 type = MAP__FUNCTION;
1144
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001145 map = map__new(&machine->user_dsos, event->mmap.start,
1146 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001147 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001148 event->mmap.filename,
Stephane Eranianbad40912013-01-24 16:10:40 +01001149 type);
1150
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001151 if (map == NULL)
1152 goto out_problem;
1153
1154 thread__insert_map(thread, map);
1155 return 0;
1156
1157out_problem:
1158 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1159 return 0;
1160}
1161
David Ahern236a3bb2013-08-14 08:49:27 -06001162static void machine__remove_thread(struct machine *machine, struct thread *th)
1163{
1164 machine->last_match = NULL;
1165 rb_erase(&th->rb_node, &machine->threads);
1166 /*
1167 * We may have references to this thread, for instance in some hist_entry
1168 * instances, so just move them to a separate list.
1169 */
1170 list_add_tail(&th->node, &machine->dead_threads);
1171}
1172
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001173int machine__process_fork_event(struct machine *machine, union perf_event *event,
1174 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001175{
Jiri Olsad75e6092014-03-14 15:00:03 +01001176 struct thread *thread = machine__find_thread(machine,
1177 event->fork.pid,
1178 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001179 struct thread *parent = machine__findnew_thread(machine,
1180 event->fork.ppid,
1181 event->fork.ptid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001182
David Ahern236a3bb2013-08-14 08:49:27 -06001183 /* if a thread currently exists for the thread id remove it */
1184 if (thread != NULL)
1185 machine__remove_thread(machine, thread);
1186
Adrian Hunter314add62013-08-27 11:23:03 +03001187 thread = machine__findnew_thread(machine, event->fork.pid,
1188 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001189 if (dump_trace)
1190 perf_event__fprintf_task(event, stdout);
1191
1192 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001193 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001194 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1195 return -1;
1196 }
1197
1198 return 0;
1199}
1200
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001201int machine__process_exit_event(struct machine *machine, union perf_event *event,
1202 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001203{
Jiri Olsad75e6092014-03-14 15:00:03 +01001204 struct thread *thread = machine__find_thread(machine,
1205 event->fork.pid,
1206 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001207
1208 if (dump_trace)
1209 perf_event__fprintf_task(event, stdout);
1210
1211 if (thread != NULL)
David Ahern236a3bb2013-08-14 08:49:27 -06001212 thread__exited(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001213
1214 return 0;
1215}
1216
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001217int machine__process_event(struct machine *machine, union perf_event *event,
1218 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001219{
1220 int ret;
1221
1222 switch (event->header.type) {
1223 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001224 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001225 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001226 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001227 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001228 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001229 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001230 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001231 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001232 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001233 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001234 ret = machine__process_lost_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001235 default:
1236 ret = -1;
1237 break;
1238 }
1239
1240 return ret;
1241}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001242
Greg Priceb21484f2012-12-06 21:48:05 -08001243static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001244{
Greg Priceb21484f2012-12-06 21:48:05 -08001245 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001246 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001247 return 0;
1248}
1249
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001250static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1251 struct addr_map_symbol *ams,
1252 u64 ip)
1253{
1254 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001255
1256 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001257 /*
1258 * We cannot use the header.misc hint to determine whether a
1259 * branch stack address is user, kernel, guest, hypervisor.
1260 * Branches may straddle the kernel/user/hypervisor boundaries.
1261 * Thus, we have to try consecutively until we find a match
1262 * or else, the symbol is unknown
1263 */
1264 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001265
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001266 ams->addr = ip;
1267 ams->al_addr = al.addr;
1268 ams->sym = al.sym;
1269 ams->map = al.map;
1270}
1271
Stephane Eranian98a3b322013-01-24 16:10:35 +01001272static void ip__resolve_data(struct machine *machine, struct thread *thread,
1273 u8 m, struct addr_map_symbol *ams, u64 addr)
1274{
1275 struct addr_location al;
1276
1277 memset(&al, 0, sizeof(al));
1278
Adrian Hunter61710bd2013-08-08 14:32:26 +03001279 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1280 &al);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001281 ams->addr = addr;
1282 ams->al_addr = al.addr;
1283 ams->sym = al.sym;
1284 ams->map = al.map;
1285}
1286
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001287struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1288 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001289{
1290 struct mem_info *mi = zalloc(sizeof(*mi));
1291
1292 if (!mi)
1293 return NULL;
1294
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001295 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1296 ip__resolve_data(al->machine, al->thread, al->cpumode,
1297 &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001298 mi->data_src.val = sample->data_src;
1299
1300 return mi;
1301}
1302
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001303struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1304 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001305{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001306 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001307 const struct branch_stack *bs = sample->branch_stack;
1308 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001309
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001310 if (!bi)
1311 return NULL;
1312
1313 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001314 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1315 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001316 bi[i].flags = bs->entries[i].flags;
1317 }
1318 return bi;
1319}
1320
1321static int machine__resolve_callchain_sample(struct machine *machine,
1322 struct thread *thread,
1323 struct ip_callchain *chain,
Greg Priceb21484f2012-12-06 21:48:05 -08001324 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001325 struct addr_location *root_al,
1326 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001327{
1328 u8 cpumode = PERF_RECORD_MISC_USER;
Waiman Long91e95612013-10-18 10:38:48 -04001329 int chain_nr = min(max_stack, (int)chain->nr);
1330 int i;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001331 int j;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001332 int err;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001333 int skip_idx __maybe_unused;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001334
1335 callchain_cursor_reset(&callchain_cursor);
1336
1337 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1338 pr_warning("corrupted callchain. skipping...\n");
1339 return 0;
1340 }
1341
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001342 /*
1343 * Based on DWARF debug information, some architectures skip
1344 * a callchain entry saved by the kernel.
1345 */
1346 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1347
Waiman Long91e95612013-10-18 10:38:48 -04001348 for (i = 0; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001349 u64 ip;
1350 struct addr_location al;
1351
1352 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001353 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001354 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001355 j = chain->nr - i - 1;
1356
1357#ifdef HAVE_SKIP_CALLCHAIN_IDX
1358 if (j == skip_idx)
1359 continue;
1360#endif
1361 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001362
1363 if (ip >= PERF_CONTEXT_MAX) {
1364 switch (ip) {
1365 case PERF_CONTEXT_HV:
1366 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1367 break;
1368 case PERF_CONTEXT_KERNEL:
1369 cpumode = PERF_RECORD_MISC_KERNEL;
1370 break;
1371 case PERF_CONTEXT_USER:
1372 cpumode = PERF_RECORD_MISC_USER;
1373 break;
1374 default:
1375 pr_debug("invalid callchain context: "
1376 "%"PRId64"\n", (s64) ip);
1377 /*
1378 * It seems the callchain is corrupted.
1379 * Discard all.
1380 */
1381 callchain_cursor_reset(&callchain_cursor);
1382 return 0;
1383 }
1384 continue;
1385 }
1386
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001387 al.filtered = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001388 thread__find_addr_location(thread, machine, cpumode,
Adrian Hunter61710bd2013-08-08 14:32:26 +03001389 MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001390 if (al.sym != NULL) {
1391 if (sort__has_parent && !*parent &&
Greg Priceb21484f2012-12-06 21:48:05 -08001392 symbol__match_regex(al.sym, &parent_regex))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001393 *parent = al.sym;
Greg Priceb21484f2012-12-06 21:48:05 -08001394 else if (have_ignore_callees && root_al &&
1395 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1396 /* Treat this symbol as the root,
1397 forgetting its callees. */
1398 *root_al = al;
1399 callchain_cursor_reset(&callchain_cursor);
1400 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001401 }
1402
1403 err = callchain_cursor_append(&callchain_cursor,
1404 ip, al.map, al.sym);
1405 if (err)
1406 return err;
1407 }
1408
1409 return 0;
1410}
1411
1412static int unwind_entry(struct unwind_entry *entry, void *arg)
1413{
1414 struct callchain_cursor *cursor = arg;
1415 return callchain_cursor_append(cursor, entry->ip,
1416 entry->map, entry->sym);
1417}
1418
1419int machine__resolve_callchain(struct machine *machine,
1420 struct perf_evsel *evsel,
1421 struct thread *thread,
1422 struct perf_sample *sample,
Greg Priceb21484f2012-12-06 21:48:05 -08001423 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001424 struct addr_location *root_al,
1425 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001426{
1427 int ret;
1428
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001429 ret = machine__resolve_callchain_sample(machine, thread,
Waiman Long91e95612013-10-18 10:38:48 -04001430 sample->callchain, parent,
1431 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001432 if (ret)
1433 return ret;
1434
1435 /* Can we do dwarf post unwind? */
1436 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1437 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1438 return 0;
1439
1440 /* Bail out if nothing was captured. */
1441 if ((!sample->user_regs.regs) ||
1442 (!sample->user_stack.size))
1443 return 0;
1444
1445 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
Jiri Olsa352ea452014-01-07 13:47:25 +01001446 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001447
1448}
David Ahern35feee12013-09-28 13:12:58 -06001449
1450int machine__for_each_thread(struct machine *machine,
1451 int (*fn)(struct thread *thread, void *p),
1452 void *priv)
1453{
1454 struct rb_node *nd;
1455 struct thread *thread;
1456 int rc = 0;
1457
1458 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1459 thread = rb_entry(nd, struct thread, rb_node);
1460 rc = fn(thread, priv);
1461 if (rc != 0)
1462 return rc;
1463 }
1464
1465 list_for_each_entry(thread, &machine->dead_threads, node) {
1466 rc = fn(thread, priv);
1467 if (rc != 0)
1468 return rc;
1469 }
1470 return rc;
1471}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001472
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001473int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001474 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001475 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001476{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001477 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001478 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001479 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001480 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1481 /* command specified */
1482 return 0;
1483}