Merge branch 'kvm-updates/3.3' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[cascardo/linux.git] / tools / perf / util / event.c
1 #include <linux/types.h>
2 #include "event.h"
3 #include "debug.h"
4 #include "sort.h"
5 #include "string.h"
6 #include "strlist.h"
7 #include "thread.h"
8 #include "thread_map.h"
9
10 static const char *perf_event__names[] = {
11         [0]                                     = "TOTAL",
12         [PERF_RECORD_MMAP]                      = "MMAP",
13         [PERF_RECORD_LOST]                      = "LOST",
14         [PERF_RECORD_COMM]                      = "COMM",
15         [PERF_RECORD_EXIT]                      = "EXIT",
16         [PERF_RECORD_THROTTLE]                  = "THROTTLE",
17         [PERF_RECORD_UNTHROTTLE]                = "UNTHROTTLE",
18         [PERF_RECORD_FORK]                      = "FORK",
19         [PERF_RECORD_READ]                      = "READ",
20         [PERF_RECORD_SAMPLE]                    = "SAMPLE",
21         [PERF_RECORD_HEADER_ATTR]               = "ATTR",
22         [PERF_RECORD_HEADER_EVENT_TYPE]         = "EVENT_TYPE",
23         [PERF_RECORD_HEADER_TRACING_DATA]       = "TRACING_DATA",
24         [PERF_RECORD_HEADER_BUILD_ID]           = "BUILD_ID",
25         [PERF_RECORD_FINISHED_ROUND]            = "FINISHED_ROUND",
26 };
27
28 const char *perf_event__name(unsigned int id)
29 {
30         if (id >= ARRAY_SIZE(perf_event__names))
31                 return "INVALID";
32         if (!perf_event__names[id])
33                 return "UNKNOWN";
34         return perf_event__names[id];
35 }
36
37 static struct perf_sample synth_sample = {
38         .pid       = -1,
39         .tid       = -1,
40         .time      = -1,
41         .stream_id = -1,
42         .cpu       = -1,
43         .period    = 1,
44 };
45
46 static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
47 {
48         char filename[PATH_MAX];
49         char bf[BUFSIZ];
50         FILE *fp;
51         size_t size = 0;
52         pid_t tgid = -1;
53
54         snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
55
56         fp = fopen(filename, "r");
57         if (fp == NULL) {
58                 pr_debug("couldn't open %s\n", filename);
59                 return 0;
60         }
61
62         while (!comm[0] || (tgid < 0)) {
63                 if (fgets(bf, sizeof(bf), fp) == NULL) {
64                         pr_warning("couldn't get COMM and pgid, malformed %s\n",
65                                    filename);
66                         break;
67                 }
68
69                 if (memcmp(bf, "Name:", 5) == 0) {
70                         char *name = bf + 5;
71                         while (*name && isspace(*name))
72                                 ++name;
73                         size = strlen(name) - 1;
74                         if (size >= len)
75                                 size = len - 1;
76                         memcpy(comm, name, size);
77
78                 } else if (memcmp(bf, "Tgid:", 5) == 0) {
79                         char *tgids = bf + 5;
80                         while (*tgids && isspace(*tgids))
81                                 ++tgids;
82                         tgid = atoi(tgids);
83                 }
84         }
85
86         fclose(fp);
87
88         return tgid;
89 }
90
91 static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
92                                          union perf_event *event, pid_t pid,
93                                          int full,
94                                          perf_event__handler_t process,
95                                          struct machine *machine)
96 {
97         char filename[PATH_MAX];
98         size_t size;
99         DIR *tasks;
100         struct dirent dirent, *next;
101         pid_t tgid;
102
103         memset(&event->comm, 0, sizeof(event->comm));
104
105         tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
106                                          sizeof(event->comm.comm));
107         if (tgid < 0)
108                 goto out;
109
110         event->comm.pid = tgid;
111         event->comm.header.type = PERF_RECORD_COMM;
112
113         size = strlen(event->comm.comm) + 1;
114         size = ALIGN(size, sizeof(u64));
115         memset(event->comm.comm + size, 0, machine->id_hdr_size);
116         event->comm.header.size = (sizeof(event->comm) -
117                                 (sizeof(event->comm.comm) - size) +
118                                 machine->id_hdr_size);
119         if (!full) {
120                 event->comm.tid = pid;
121
122                 process(tool, event, &synth_sample, machine);
123                 goto out;
124         }
125
126         snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
127
128         tasks = opendir(filename);
129         if (tasks == NULL) {
130                 pr_debug("couldn't open %s\n", filename);
131                 return 0;
132         }
133
134         while (!readdir_r(tasks, &dirent, &next) && next) {
135                 char *end;
136                 pid = strtol(dirent.d_name, &end, 10);
137                 if (*end)
138                         continue;
139
140                 /* already have tgid; jut want to update the comm */
141                 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
142                                          sizeof(event->comm.comm));
143
144                 size = strlen(event->comm.comm) + 1;
145                 size = ALIGN(size, sizeof(u64));
146                 memset(event->comm.comm + size, 0, machine->id_hdr_size);
147                 event->comm.header.size = (sizeof(event->comm) -
148                                           (sizeof(event->comm.comm) - size) +
149                                           machine->id_hdr_size);
150
151                 event->comm.tid = pid;
152
153                 process(tool, event, &synth_sample, machine);
154         }
155
156         closedir(tasks);
157 out:
158         return tgid;
159 }
160
161 static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
162                                               union perf_event *event,
163                                               pid_t pid, pid_t tgid,
164                                               perf_event__handler_t process,
165                                               struct machine *machine)
166 {
167         char filename[PATH_MAX];
168         FILE *fp;
169
170         snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
171
172         fp = fopen(filename, "r");
173         if (fp == NULL) {
174                 /*
175                  * We raced with a task exiting - just return:
176                  */
177                 pr_debug("couldn't open %s\n", filename);
178                 return -1;
179         }
180
181         event->header.type = PERF_RECORD_MMAP;
182         /*
183          * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
184          */
185         event->header.misc = PERF_RECORD_MISC_USER;
186
187         while (1) {
188                 char bf[BUFSIZ], *pbf = bf;
189                 int n;
190                 size_t size;
191                 if (fgets(bf, sizeof(bf), fp) == NULL)
192                         break;
193
194                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
195                 n = hex2u64(pbf, &event->mmap.start);
196                 if (n < 0)
197                         continue;
198                 pbf += n + 1;
199                 n = hex2u64(pbf, &event->mmap.len);
200                 if (n < 0)
201                         continue;
202                 pbf += n + 3;
203                 if (*pbf == 'x') { /* vm_exec */
204                         char anonstr[] = "//anon\n";
205                         char *execname = strchr(bf, '/');
206
207                         /* Catch VDSO */
208                         if (execname == NULL)
209                                 execname = strstr(bf, "[vdso]");
210
211                         /* Catch anonymous mmaps */
212                         if ((execname == NULL) && !strstr(bf, "["))
213                                 execname = anonstr;
214
215                         if (execname == NULL)
216                                 continue;
217
218                         pbf += 3;
219                         n = hex2u64(pbf, &event->mmap.pgoff);
220
221                         size = strlen(execname);
222                         execname[size - 1] = '\0'; /* Remove \n */
223                         memcpy(event->mmap.filename, execname, size);
224                         size = ALIGN(size, sizeof(u64));
225                         event->mmap.len -= event->mmap.start;
226                         event->mmap.header.size = (sizeof(event->mmap) -
227                                                 (sizeof(event->mmap.filename) - size));
228                         memset(event->mmap.filename + size, 0, machine->id_hdr_size);
229                         event->mmap.header.size += machine->id_hdr_size;
230                         event->mmap.pid = tgid;
231                         event->mmap.tid = pid;
232
233                         process(tool, event, &synth_sample, machine);
234                 }
235         }
236
237         fclose(fp);
238         return 0;
239 }
240
241 int perf_event__synthesize_modules(struct perf_tool *tool,
242                                    perf_event__handler_t process,
243                                    struct machine *machine)
244 {
245         struct rb_node *nd;
246         struct map_groups *kmaps = &machine->kmaps;
247         union perf_event *event = zalloc((sizeof(event->mmap) +
248                                           machine->id_hdr_size));
249         if (event == NULL) {
250                 pr_debug("Not enough memory synthesizing mmap event "
251                          "for kernel modules\n");
252                 return -1;
253         }
254
255         event->header.type = PERF_RECORD_MMAP;
256
257         /*
258          * kernel uses 0 for user space maps, see kernel/perf_event.c
259          * __perf_event_mmap
260          */
261         if (machine__is_host(machine))
262                 event->header.misc = PERF_RECORD_MISC_KERNEL;
263         else
264                 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
265
266         for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
267              nd; nd = rb_next(nd)) {
268                 size_t size;
269                 struct map *pos = rb_entry(nd, struct map, rb_node);
270
271                 if (pos->dso->kernel)
272                         continue;
273
274                 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
275                 event->mmap.header.type = PERF_RECORD_MMAP;
276                 event->mmap.header.size = (sizeof(event->mmap) -
277                                         (sizeof(event->mmap.filename) - size));
278                 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
279                 event->mmap.header.size += machine->id_hdr_size;
280                 event->mmap.start = pos->start;
281                 event->mmap.len   = pos->end - pos->start;
282                 event->mmap.pid   = machine->pid;
283
284                 memcpy(event->mmap.filename, pos->dso->long_name,
285                        pos->dso->long_name_len + 1);
286                 process(tool, event, &synth_sample, machine);
287         }
288
289         free(event);
290         return 0;
291 }
292
293 static int __event__synthesize_thread(union perf_event *comm_event,
294                                       union perf_event *mmap_event,
295                                       pid_t pid, int full,
296                                           perf_event__handler_t process,
297                                       struct perf_tool *tool,
298                                       struct machine *machine)
299 {
300         pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
301                                                  process, machine);
302         if (tgid == -1)
303                 return -1;
304         return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
305                                                   process, machine);
306 }
307
308 int perf_event__synthesize_thread_map(struct perf_tool *tool,
309                                       struct thread_map *threads,
310                                       perf_event__handler_t process,
311                                       struct machine *machine)
312 {
313         union perf_event *comm_event, *mmap_event;
314         int err = -1, thread, j;
315
316         comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
317         if (comm_event == NULL)
318                 goto out;
319
320         mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
321         if (mmap_event == NULL)
322                 goto out_free_comm;
323
324         err = 0;
325         for (thread = 0; thread < threads->nr; ++thread) {
326                 if (__event__synthesize_thread(comm_event, mmap_event,
327                                                threads->map[thread], 0,
328                                                process, tool, machine)) {
329                         err = -1;
330                         break;
331                 }
332
333                 /*
334                  * comm.pid is set to thread group id by
335                  * perf_event__synthesize_comm
336                  */
337                 if ((int) comm_event->comm.pid != threads->map[thread]) {
338                         bool need_leader = true;
339
340                         /* is thread group leader in thread_map? */
341                         for (j = 0; j < threads->nr; ++j) {
342                                 if ((int) comm_event->comm.pid == threads->map[j]) {
343                                         need_leader = false;
344                                         break;
345                                 }
346                         }
347
348                         /* if not, generate events for it */
349                         if (need_leader &&
350                             __event__synthesize_thread(comm_event,
351                                                       mmap_event,
352                                                       comm_event->comm.pid, 0,
353                                                       process, tool, machine)) {
354                                 err = -1;
355                                 break;
356                         }
357                 }
358         }
359         free(mmap_event);
360 out_free_comm:
361         free(comm_event);
362 out:
363         return err;
364 }
365
366 int perf_event__synthesize_threads(struct perf_tool *tool,
367                                    perf_event__handler_t process,
368                                    struct machine *machine)
369 {
370         DIR *proc;
371         struct dirent dirent, *next;
372         union perf_event *comm_event, *mmap_event;
373         int err = -1;
374
375         comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
376         if (comm_event == NULL)
377                 goto out;
378
379         mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
380         if (mmap_event == NULL)
381                 goto out_free_comm;
382
383         proc = opendir("/proc");
384         if (proc == NULL)
385                 goto out_free_mmap;
386
387         while (!readdir_r(proc, &dirent, &next) && next) {
388                 char *end;
389                 pid_t pid = strtol(dirent.d_name, &end, 10);
390
391                 if (*end) /* only interested in proper numerical dirents */
392                         continue;
393
394                 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
395                                            process, tool, machine);
396         }
397
398         closedir(proc);
399         err = 0;
400 out_free_mmap:
401         free(mmap_event);
402 out_free_comm:
403         free(comm_event);
404 out:
405         return err;
406 }
407
408 struct process_symbol_args {
409         const char *name;
410         u64        start;
411 };
412
413 static int find_symbol_cb(void *arg, const char *name, char type,
414                           u64 start, u64 end __used)
415 {
416         struct process_symbol_args *args = arg;
417
418         /*
419          * Must be a function or at least an alias, as in PARISC64, where "_text" is
420          * an 'A' to the same address as "_stext".
421          */
422         if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
423               type == 'A') || strcmp(name, args->name))
424                 return 0;
425
426         args->start = start;
427         return 1;
428 }
429
430 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
431                                        perf_event__handler_t process,
432                                        struct machine *machine,
433                                        const char *symbol_name)
434 {
435         size_t size;
436         const char *filename, *mmap_name;
437         char path[PATH_MAX];
438         char name_buff[PATH_MAX];
439         struct map *map;
440         int err;
441         /*
442          * We should get this from /sys/kernel/sections/.text, but till that is
443          * available use this, and after it is use this as a fallback for older
444          * kernels.
445          */
446         struct process_symbol_args args = { .name = symbol_name, };
447         union perf_event *event = zalloc((sizeof(event->mmap) +
448                                           machine->id_hdr_size));
449         if (event == NULL) {
450                 pr_debug("Not enough memory synthesizing mmap event "
451                          "for kernel modules\n");
452                 return -1;
453         }
454
455         mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
456         if (machine__is_host(machine)) {
457                 /*
458                  * kernel uses PERF_RECORD_MISC_USER for user space maps,
459                  * see kernel/perf_event.c __perf_event_mmap
460                  */
461                 event->header.misc = PERF_RECORD_MISC_KERNEL;
462                 filename = "/proc/kallsyms";
463         } else {
464                 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
465                 if (machine__is_default_guest(machine))
466                         filename = (char *) symbol_conf.default_guest_kallsyms;
467                 else {
468                         sprintf(path, "%s/proc/kallsyms", machine->root_dir);
469                         filename = path;
470                 }
471         }
472
473         if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
474                 return -ENOENT;
475
476         map = machine->vmlinux_maps[MAP__FUNCTION];
477         size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
478                         "%s%s", mmap_name, symbol_name) + 1;
479         size = ALIGN(size, sizeof(u64));
480         event->mmap.header.type = PERF_RECORD_MMAP;
481         event->mmap.header.size = (sizeof(event->mmap) -
482                         (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
483         event->mmap.pgoff = args.start;
484         event->mmap.start = map->start;
485         event->mmap.len   = map->end - event->mmap.start;
486         event->mmap.pid   = machine->pid;
487
488         err = process(tool, event, &synth_sample, machine);
489         free(event);
490
491         return err;
492 }
493
494 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
495 {
496         return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
497 }
498
499 int perf_event__process_comm(struct perf_tool *tool __used,
500                              union perf_event *event,
501                              struct perf_sample *sample __used,
502                              struct machine *machine)
503 {
504         struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
505
506         if (dump_trace)
507                 perf_event__fprintf_comm(event, stdout);
508
509         if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
510                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
511                 return -1;
512         }
513
514         return 0;
515 }
516
517 int perf_event__process_lost(struct perf_tool *tool __used,
518                              union perf_event *event,
519                              struct perf_sample *sample __used,
520                              struct machine *machine __used)
521 {
522         dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
523                     event->lost.id, event->lost.lost);
524         return 0;
525 }
526
527 static void perf_event__set_kernel_mmap_len(union perf_event *event,
528                                             struct map **maps)
529 {
530         maps[MAP__FUNCTION]->start = event->mmap.start;
531         maps[MAP__FUNCTION]->end   = event->mmap.start + event->mmap.len;
532         /*
533          * Be a bit paranoid here, some perf.data file came with
534          * a zero sized synthesized MMAP event for the kernel.
535          */
536         if (maps[MAP__FUNCTION]->end == 0)
537                 maps[MAP__FUNCTION]->end = ~0ULL;
538 }
539
540 static int perf_event__process_kernel_mmap(struct perf_tool *tool __used,
541                                            union perf_event *event,
542                                            struct machine *machine)
543 {
544         struct map *map;
545         char kmmap_prefix[PATH_MAX];
546         enum dso_kernel_type kernel_type;
547         bool is_kernel_mmap;
548
549         machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
550         if (machine__is_host(machine))
551                 kernel_type = DSO_TYPE_KERNEL;
552         else
553                 kernel_type = DSO_TYPE_GUEST_KERNEL;
554
555         is_kernel_mmap = memcmp(event->mmap.filename,
556                                 kmmap_prefix,
557                                 strlen(kmmap_prefix)) == 0;
558         if (event->mmap.filename[0] == '/' ||
559             (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
560
561                 char short_module_name[1024];
562                 char *name, *dot;
563
564                 if (event->mmap.filename[0] == '/') {
565                         name = strrchr(event->mmap.filename, '/');
566                         if (name == NULL)
567                                 goto out_problem;
568
569                         ++name; /* skip / */
570                         dot = strrchr(name, '.');
571                         if (dot == NULL)
572                                 goto out_problem;
573                         snprintf(short_module_name, sizeof(short_module_name),
574                                         "[%.*s]", (int)(dot - name), name);
575                         strxfrchar(short_module_name, '-', '_');
576                 } else
577                         strcpy(short_module_name, event->mmap.filename);
578
579                 map = machine__new_module(machine, event->mmap.start,
580                                           event->mmap.filename);
581                 if (map == NULL)
582                         goto out_problem;
583
584                 name = strdup(short_module_name);
585                 if (name == NULL)
586                         goto out_problem;
587
588                 map->dso->short_name = name;
589                 map->dso->sname_alloc = 1;
590                 map->end = map->start + event->mmap.len;
591         } else if (is_kernel_mmap) {
592                 const char *symbol_name = (event->mmap.filename +
593                                 strlen(kmmap_prefix));
594                 /*
595                  * Should be there already, from the build-id table in
596                  * the header.
597                  */
598                 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
599                                                      kmmap_prefix);
600                 if (kernel == NULL)
601                         goto out_problem;
602
603                 kernel->kernel = kernel_type;
604                 if (__machine__create_kernel_maps(machine, kernel) < 0)
605                         goto out_problem;
606
607                 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
608
609                 /*
610                  * Avoid using a zero address (kptr_restrict) for the ref reloc
611                  * symbol. Effectively having zero here means that at record
612                  * time /proc/sys/kernel/kptr_restrict was non zero.
613                  */
614                 if (event->mmap.pgoff != 0) {
615                         maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
616                                                          symbol_name,
617                                                          event->mmap.pgoff);
618                 }
619
620                 if (machine__is_default_guest(machine)) {
621                         /*
622                          * preload dso of guest kernel and modules
623                          */
624                         dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
625                                   NULL);
626                 }
627         }
628         return 0;
629 out_problem:
630         return -1;
631 }
632
633 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
634 {
635         return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
636                        event->mmap.pid, event->mmap.tid, event->mmap.start,
637                        event->mmap.len, event->mmap.pgoff, event->mmap.filename);
638 }
639
640 int perf_event__process_mmap(struct perf_tool *tool,
641                              union perf_event *event,
642                              struct perf_sample *sample __used,
643                              struct machine *machine)
644 {
645         struct thread *thread;
646         struct map *map;
647         u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
648         int ret = 0;
649
650         if (dump_trace)
651                 perf_event__fprintf_mmap(event, stdout);
652
653         if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
654             cpumode == PERF_RECORD_MISC_KERNEL) {
655                 ret = perf_event__process_kernel_mmap(tool, event, machine);
656                 if (ret < 0)
657                         goto out_problem;
658                 return 0;
659         }
660
661         thread = machine__findnew_thread(machine, event->mmap.pid);
662         if (thread == NULL)
663                 goto out_problem;
664         map = map__new(&machine->user_dsos, event->mmap.start,
665                         event->mmap.len, event->mmap.pgoff,
666                         event->mmap.pid, event->mmap.filename,
667                         MAP__FUNCTION);
668         if (map == NULL)
669                 goto out_problem;
670
671         thread__insert_map(thread, map);
672         return 0;
673
674 out_problem:
675         dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
676         return 0;
677 }
678
679 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
680 {
681         return fprintf(fp, "(%d:%d):(%d:%d)\n",
682                        event->fork.pid, event->fork.tid,
683                        event->fork.ppid, event->fork.ptid);
684 }
685
686 int perf_event__process_task(struct perf_tool *tool __used,
687                              union perf_event *event,
688                              struct perf_sample *sample __used,
689                               struct machine *machine)
690 {
691         struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
692         struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
693
694         if (dump_trace)
695                 perf_event__fprintf_task(event, stdout);
696
697         if (event->header.type == PERF_RECORD_EXIT) {
698                 machine__remove_thread(machine, thread);
699                 return 0;
700         }
701
702         if (thread == NULL || parent == NULL ||
703             thread__fork(thread, parent) < 0) {
704                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
705                 return -1;
706         }
707
708         return 0;
709 }
710
711 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
712 {
713         size_t ret = fprintf(fp, "PERF_RECORD_%s",
714                              perf_event__name(event->header.type));
715
716         switch (event->header.type) {
717         case PERF_RECORD_COMM:
718                 ret += perf_event__fprintf_comm(event, fp);
719                 break;
720         case PERF_RECORD_FORK:
721         case PERF_RECORD_EXIT:
722                 ret += perf_event__fprintf_task(event, fp);
723                 break;
724         case PERF_RECORD_MMAP:
725                 ret += perf_event__fprintf_mmap(event, fp);
726                 break;
727         default:
728                 ret += fprintf(fp, "\n");
729         }
730
731         return ret;
732 }
733
734 int perf_event__process(struct perf_tool *tool, union perf_event *event,
735                         struct perf_sample *sample, struct machine *machine)
736 {
737         switch (event->header.type) {
738         case PERF_RECORD_COMM:
739                 perf_event__process_comm(tool, event, sample, machine);
740                 break;
741         case PERF_RECORD_MMAP:
742                 perf_event__process_mmap(tool, event, sample, machine);
743                 break;
744         case PERF_RECORD_FORK:
745         case PERF_RECORD_EXIT:
746                 perf_event__process_task(tool, event, sample, machine);
747                 break;
748         case PERF_RECORD_LOST:
749                 perf_event__process_lost(tool, event, sample, machine);
750         default:
751                 break;
752         }
753
754         return 0;
755 }
756
757 void thread__find_addr_map(struct thread *self,
758                            struct machine *machine, u8 cpumode,
759                            enum map_type type, u64 addr,
760                            struct addr_location *al)
761 {
762         struct map_groups *mg = &self->mg;
763
764         al->thread = self;
765         al->addr = addr;
766         al->cpumode = cpumode;
767         al->filtered = false;
768
769         if (machine == NULL) {
770                 al->map = NULL;
771                 return;
772         }
773
774         if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
775                 al->level = 'k';
776                 mg = &machine->kmaps;
777         } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
778                 al->level = '.';
779         } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
780                 al->level = 'g';
781                 mg = &machine->kmaps;
782         } else {
783                 /*
784                  * 'u' means guest os user space.
785                  * TODO: We don't support guest user space. Might support late.
786                  */
787                 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
788                         al->level = 'u';
789                 else
790                         al->level = 'H';
791                 al->map = NULL;
792
793                 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
794                         cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
795                         !perf_guest)
796                         al->filtered = true;
797                 if ((cpumode == PERF_RECORD_MISC_USER ||
798                         cpumode == PERF_RECORD_MISC_KERNEL) &&
799                         !perf_host)
800                         al->filtered = true;
801
802                 return;
803         }
804 try_again:
805         al->map = map_groups__find(mg, type, al->addr);
806         if (al->map == NULL) {
807                 /*
808                  * If this is outside of all known maps, and is a negative
809                  * address, try to look it up in the kernel dso, as it might be
810                  * a vsyscall or vdso (which executes in user-mode).
811                  *
812                  * XXX This is nasty, we should have a symbol list in the
813                  * "[vdso]" dso, but for now lets use the old trick of looking
814                  * in the whole kernel symbol list.
815                  */
816                 if ((long long)al->addr < 0 &&
817                     cpumode == PERF_RECORD_MISC_USER &&
818                     machine && mg != &machine->kmaps) {
819                         mg = &machine->kmaps;
820                         goto try_again;
821                 }
822         } else
823                 al->addr = al->map->map_ip(al->map, al->addr);
824 }
825
826 void thread__find_addr_location(struct thread *thread, struct machine *machine,
827                                 u8 cpumode, enum map_type type, u64 addr,
828                                 struct addr_location *al,
829                                 symbol_filter_t filter)
830 {
831         thread__find_addr_map(thread, machine, cpumode, type, addr, al);
832         if (al->map != NULL)
833                 al->sym = map__find_symbol(al->map, al->addr, filter);
834         else
835                 al->sym = NULL;
836 }
837
838 int perf_event__preprocess_sample(const union perf_event *event,
839                                   struct machine *machine,
840                                   struct addr_location *al,
841                                   struct perf_sample *sample,
842                                   symbol_filter_t filter)
843 {
844         u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
845         struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
846
847         if (thread == NULL)
848                 return -1;
849
850         if (symbol_conf.comm_list &&
851             !strlist__has_entry(symbol_conf.comm_list, thread->comm))
852                 goto out_filtered;
853
854         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
855         /*
856          * Have we already created the kernel maps for this machine?
857          *
858          * This should have happened earlier, when we processed the kernel MMAP
859          * events, but for older perf.data files there was no such thing, so do
860          * it now.
861          */
862         if (cpumode == PERF_RECORD_MISC_KERNEL &&
863             machine->vmlinux_maps[MAP__FUNCTION] == NULL)
864                 machine__create_kernel_maps(machine);
865
866         thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
867                               event->ip.ip, al);
868         dump_printf(" ...... dso: %s\n",
869                     al->map ? al->map->dso->long_name :
870                         al->level == 'H' ? "[hypervisor]" : "<not found>");
871         al->sym = NULL;
872         al->cpu = sample->cpu;
873
874         if (al->map) {
875                 struct dso *dso = al->map->dso;
876
877                 if (symbol_conf.dso_list &&
878                     (!dso || !(strlist__has_entry(symbol_conf.dso_list,
879                                                   dso->short_name) ||
880                                (dso->short_name != dso->long_name &&
881                                 strlist__has_entry(symbol_conf.dso_list,
882                                                    dso->long_name)))))
883                         goto out_filtered;
884
885                 al->sym = map__find_symbol(al->map, al->addr, filter);
886         }
887
888         if (symbol_conf.sym_list && al->sym &&
889             !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
890                 goto out_filtered;
891
892         return 0;
893
894 out_filtered:
895         al->filtered = true;
896         return 0;
897 }