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