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