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