perf symbols: Fill in pgoff in mmap synthesized events
[safe/jmp/linux-2.6] / 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 pid_t event__synthesize_comm(pid_t pid, int full,
11                                     event__handler_t process,
12                                     struct perf_session *session)
13 {
14         event_t ev;
15         char filename[PATH_MAX];
16         char bf[BUFSIZ];
17         FILE *fp;
18         size_t size = 0;
19         DIR *tasks;
20         struct dirent dirent, *next;
21         pid_t tgid = 0;
22
23         snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
24
25         fp = fopen(filename, "r");
26         if (fp == NULL) {
27 out_race:
28                 /*
29                  * We raced with a task exiting - just return:
30                  */
31                 pr_debug("couldn't open %s\n", filename);
32                 return 0;
33         }
34
35         memset(&ev.comm, 0, sizeof(ev.comm));
36         while (!ev.comm.comm[0] || !ev.comm.pid) {
37                 if (fgets(bf, sizeof(bf), fp) == NULL)
38                         goto out_failure;
39
40                 if (memcmp(bf, "Name:", 5) == 0) {
41                         char *name = bf + 5;
42                         while (*name && isspace(*name))
43                                 ++name;
44                         size = strlen(name) - 1;
45                         memcpy(ev.comm.comm, name, size++);
46                 } else if (memcmp(bf, "Tgid:", 5) == 0) {
47                         char *tgids = bf + 5;
48                         while (*tgids && isspace(*tgids))
49                                 ++tgids;
50                         tgid = ev.comm.pid = atoi(tgids);
51                 }
52         }
53
54         ev.comm.header.type = PERF_RECORD_COMM;
55         size = ALIGN(size, sizeof(u64));
56         ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
57
58         if (!full) {
59                 ev.comm.tid = pid;
60
61                 process(&ev, session);
62                 goto out_fclose;
63         }
64
65         snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
66
67         tasks = opendir(filename);
68         if (tasks == NULL)
69                 goto out_race;
70
71         while (!readdir_r(tasks, &dirent, &next) && next) {
72                 char *end;
73                 pid = strtol(dirent.d_name, &end, 10);
74                 if (*end)
75                         continue;
76
77                 ev.comm.tid = pid;
78
79                 process(&ev, session);
80         }
81         closedir(tasks);
82
83 out_fclose:
84         fclose(fp);
85         return tgid;
86
87 out_failure:
88         pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
89         return -1;
90 }
91
92 static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
93                                          event__handler_t process,
94                                          struct perf_session *session)
95 {
96         char filename[PATH_MAX];
97         FILE *fp;
98
99         snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
100
101         fp = fopen(filename, "r");
102         if (fp == NULL) {
103                 /*
104                  * We raced with a task exiting - just return:
105                  */
106                 pr_debug("couldn't open %s\n", filename);
107                 return -1;
108         }
109
110         while (1) {
111                 char bf[BUFSIZ], *pbf = bf;
112                 event_t ev = {
113                         .header = {
114                                 .type = PERF_RECORD_MMAP,
115                                 .misc = 0, /* Just like the kernel, see kernel/perf_event.c __perf_event_mmap */
116                          },
117                 };
118                 int n;
119                 size_t size;
120                 if (fgets(bf, sizeof(bf), fp) == NULL)
121                         break;
122
123                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
124                 n = hex2u64(pbf, &ev.mmap.start);
125                 if (n < 0)
126                         continue;
127                 pbf += n + 1;
128                 n = hex2u64(pbf, &ev.mmap.len);
129                 if (n < 0)
130                         continue;
131                 pbf += n + 3;
132                 if (*pbf == 'x') { /* vm_exec */
133                         u64 vm_pgoff;
134                         char *execname = strchr(bf, '/');
135
136                         /* Catch VDSO */
137                         if (execname == NULL)
138                                 execname = strstr(bf, "[vdso]");
139
140                         if (execname == NULL)
141                                 continue;
142
143                         pbf += 3;
144                         n = hex2u64(pbf, &vm_pgoff);
145                         /* pgoff is in bytes, not pages */
146                         if (n >= 0)
147                                 ev.mmap.pgoff = vm_pgoff << getpagesize();
148                         else
149                                 ev.mmap.pgoff = 0;
150
151                         size = strlen(execname);
152                         execname[size - 1] = '\0'; /* Remove \n */
153                         memcpy(ev.mmap.filename, execname, size);
154                         size = ALIGN(size, sizeof(u64));
155                         ev.mmap.len -= ev.mmap.start;
156                         ev.mmap.header.size = (sizeof(ev.mmap) -
157                                                (sizeof(ev.mmap.filename) - size));
158                         ev.mmap.pid = tgid;
159                         ev.mmap.tid = pid;
160
161                         process(&ev, session);
162                 }
163         }
164
165         fclose(fp);
166         return 0;
167 }
168
169 int event__synthesize_modules(event__handler_t process,
170                               struct perf_session *session)
171 {
172         struct rb_node *nd;
173
174         for (nd = rb_first(&session->kmaps.maps[MAP__FUNCTION]);
175              nd; nd = rb_next(nd)) {
176                 event_t ev;
177                 size_t size;
178                 struct map *pos = rb_entry(nd, struct map, rb_node);
179
180                 if (pos->dso->kernel)
181                         continue;
182
183                 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
184                 memset(&ev, 0, sizeof(ev));
185                 ev.mmap.header.misc = 1; /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */
186                 ev.mmap.header.type = PERF_RECORD_MMAP;
187                 ev.mmap.header.size = (sizeof(ev.mmap) -
188                                         (sizeof(ev.mmap.filename) - size));
189                 ev.mmap.start = pos->start;
190                 ev.mmap.len   = pos->end - pos->start;
191
192                 memcpy(ev.mmap.filename, pos->dso->long_name,
193                        pos->dso->long_name_len + 1);
194                 process(&ev, session);
195         }
196
197         return 0;
198 }
199
200 int event__synthesize_thread(pid_t pid, event__handler_t process,
201                              struct perf_session *session)
202 {
203         pid_t tgid = event__synthesize_comm(pid, 1, process, session);
204         if (tgid == -1)
205                 return -1;
206         return event__synthesize_mmap_events(pid, tgid, process, session);
207 }
208
209 void event__synthesize_threads(event__handler_t process,
210                                struct perf_session *session)
211 {
212         DIR *proc;
213         struct dirent dirent, *next;
214
215         proc = opendir("/proc");
216
217         while (!readdir_r(proc, &dirent, &next) && next) {
218                 char *end;
219                 pid_t pid = strtol(dirent.d_name, &end, 10);
220
221                 if (*end) /* only interested in proper numerical dirents */
222                         continue;
223
224                 event__synthesize_thread(pid, process, session);
225         }
226
227         closedir(proc);
228 }
229
230 struct process_symbol_args {
231         const char *name;
232         u64        start;
233 };
234
235 static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
236 {
237         struct process_symbol_args *args = arg;
238
239         /*
240          * Must be a function or at least an alias, as in PARISC64, where "_text" is
241          * an 'A' to the same address as "_stext".
242          */
243         if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
244               type == 'A') || strcmp(name, args->name))
245                 return 0;
246
247         args->start = start;
248         return 1;
249 }
250
251 int event__synthesize_kernel_mmap(event__handler_t process,
252                                   struct perf_session *session,
253                                   const char *symbol_name)
254 {
255         size_t size;
256         event_t ev = {
257                 .header = {
258                         .type = PERF_RECORD_MMAP,
259                         .misc = 1, /* kernel uses 0 for user space maps, see kernel/perf_event.c __perf_event_mmap */
260                 },
261         };
262         /*
263          * We should get this from /sys/kernel/sections/.text, but till that is
264          * available use this, and after it is use this as a fallback for older
265          * kernels.
266          */
267         struct process_symbol_args args = { .name = symbol_name, };
268
269         if (kallsyms__parse("/proc/kallsyms", &args, find_symbol_cb) <= 0)
270                 return -ENOENT;
271
272         size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
273                         "[kernel.kallsyms.%s]", symbol_name) + 1;
274         size = ALIGN(size, sizeof(u64));
275         ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size));
276         ev.mmap.pgoff = args.start;
277         ev.mmap.start = session->vmlinux_maps[MAP__FUNCTION]->start;
278         ev.mmap.len   = session->vmlinux_maps[MAP__FUNCTION]->end - ev.mmap.start ;
279
280         return process(&ev, session);
281 }
282
283 static void thread__comm_adjust(struct thread *self)
284 {
285         char *comm = self->comm;
286
287         if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
288             (!symbol_conf.comm_list ||
289              strlist__has_entry(symbol_conf.comm_list, comm))) {
290                 unsigned int slen = strlen(comm);
291
292                 if (slen > comms__col_width) {
293                         comms__col_width = slen;
294                         threads__col_width = slen + 6;
295                 }
296         }
297 }
298
299 static int thread__set_comm_adjust(struct thread *self, const char *comm)
300 {
301         int ret = thread__set_comm(self, comm);
302
303         if (ret)
304                 return ret;
305
306         thread__comm_adjust(self);
307
308         return 0;
309 }
310
311 int event__process_comm(event_t *self, struct perf_session *session)
312 {
313         struct thread *thread = perf_session__findnew(session, self->comm.pid);
314
315         dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
316
317         if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
318                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
319                 return -1;
320         }
321
322         return 0;
323 }
324
325 int event__process_lost(event_t *self, struct perf_session *session)
326 {
327         dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
328         session->events_stats.lost += self->lost.lost;
329         return 0;
330 }
331
332 int event__process_mmap(event_t *self, struct perf_session *session)
333 {
334         struct thread *thread;
335         struct map *map;
336
337         dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
338                     self->mmap.pid, self->mmap.tid, self->mmap.start,
339                     self->mmap.len, self->mmap.pgoff, self->mmap.filename);
340
341         if (self->mmap.pid == 0) {
342                 static const char kmmap_prefix[] = "[kernel.kallsyms.";
343
344                 if (self->mmap.filename[0] == '/') {
345                         char short_module_name[1024];
346                         char *name = strrchr(self->mmap.filename, '/'), *dot;
347
348                         if (name == NULL)
349                                 goto out_problem;
350
351                         ++name; /* skip / */
352                         dot = strrchr(name, '.');
353                         if (dot == NULL)
354                                 goto out_problem;
355
356                         snprintf(short_module_name, sizeof(short_module_name),
357                                  "[%.*s]", (int)(dot - name), name);
358                         strxfrchar(short_module_name, '-', '_');
359
360                         map = perf_session__new_module_map(session,
361                                                            self->mmap.start,
362                                                            self->mmap.filename);
363                         if (map == NULL)
364                                 goto out_problem;
365
366                         name = strdup(short_module_name);
367                         if (name == NULL)
368                                 goto out_problem;
369
370                         map->dso->short_name = name;
371                         map->end = map->start + self->mmap.len;
372                 } else if (memcmp(self->mmap.filename, kmmap_prefix,
373                                 sizeof(kmmap_prefix) - 1) == 0) {
374                         const char *symbol_name = (self->mmap.filename +
375                                                    sizeof(kmmap_prefix) - 1);
376                         /*
377                          * Should be there already, from the build-id table in
378                          * the header.
379                          */
380                         struct dso *kernel = __dsos__findnew(&dsos__kernel,
381                                                              "[kernel.kallsyms]");
382                         if (kernel == NULL)
383                                 goto out_problem;
384
385                         kernel->kernel = 1;
386                         if (__perf_session__create_kernel_maps(session, kernel) < 0)
387                                 goto out_problem;
388
389                         session->vmlinux_maps[MAP__FUNCTION]->start = self->mmap.start;
390                         session->vmlinux_maps[MAP__FUNCTION]->end   = self->mmap.start + self->mmap.len;
391                         /*
392                          * Be a bit paranoid here, some perf.data file came with
393                          * a zero sized synthesized MMAP event for the kernel.
394                          */
395                         if (session->vmlinux_maps[MAP__FUNCTION]->end == 0)
396                                 session->vmlinux_maps[MAP__FUNCTION]->end = ~0UL;
397
398                         perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name,
399                                                                  self->mmap.pgoff);
400                 }
401                 return 0;
402         }
403
404         thread = perf_session__findnew(session, self->mmap.pid);
405         map = map__new(self->mmap.start, self->mmap.len, self->mmap.pgoff,
406                        self->mmap.pid, self->mmap.filename, MAP__FUNCTION,
407                        session->cwd, session->cwdlen);
408
409         if (thread == NULL || map == NULL)
410                 goto out_problem;
411
412         thread__insert_map(thread, map);
413         return 0;
414
415 out_problem:
416         dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
417         return 0;
418 }
419
420 int event__process_task(event_t *self, struct perf_session *session)
421 {
422         struct thread *thread = perf_session__findnew(session, self->fork.pid);
423         struct thread *parent = perf_session__findnew(session, self->fork.ppid);
424
425         dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
426                     self->fork.ppid, self->fork.ptid);
427         /*
428          * A thread clone will have the same PID for both parent and child.
429          */
430         if (thread == parent)
431                 return 0;
432
433         if (self->header.type == PERF_RECORD_EXIT)
434                 return 0;
435
436         if (thread == NULL || parent == NULL ||
437             thread__fork(thread, parent) < 0) {
438                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
439                 return -1;
440         }
441
442         return 0;
443 }
444
445 void thread__find_addr_map(struct thread *self,
446                            struct perf_session *session, u8 cpumode,
447                            enum map_type type, u64 addr,
448                            struct addr_location *al)
449 {
450         struct map_groups *mg = &self->mg;
451
452         al->thread = self;
453         al->addr = addr;
454
455         if (cpumode == PERF_RECORD_MISC_KERNEL) {
456                 al->level = 'k';
457                 mg = &session->kmaps;
458         } else if (cpumode == PERF_RECORD_MISC_USER)
459                 al->level = '.';
460         else {
461                 al->level = 'H';
462                 al->map = NULL;
463                 return;
464         }
465 try_again:
466         al->map = map_groups__find(mg, type, al->addr);
467         if (al->map == NULL) {
468                 /*
469                  * If this is outside of all known maps, and is a negative
470                  * address, try to look it up in the kernel dso, as it might be
471                  * a vsyscall or vdso (which executes in user-mode).
472                  *
473                  * XXX This is nasty, we should have a symbol list in the
474                  * "[vdso]" dso, but for now lets use the old trick of looking
475                  * in the whole kernel symbol list.
476                  */
477                 if ((long long)al->addr < 0 && mg != &session->kmaps) {
478                         mg = &session->kmaps;
479                         goto try_again;
480                 }
481         } else
482                 al->addr = al->map->map_ip(al->map, al->addr);
483 }
484
485 void thread__find_addr_location(struct thread *self,
486                                 struct perf_session *session, u8 cpumode,
487                                 enum map_type type, u64 addr,
488                                 struct addr_location *al,
489                                 symbol_filter_t filter)
490 {
491         thread__find_addr_map(self, session, cpumode, type, addr, al);
492         if (al->map != NULL)
493                 al->sym = map__find_symbol(al->map, al->addr, filter);
494         else
495                 al->sym = NULL;
496 }
497
498 static void dso__calc_col_width(struct dso *self)
499 {
500         if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
501             (!symbol_conf.dso_list ||
502              strlist__has_entry(symbol_conf.dso_list, self->name))) {
503                 unsigned int slen = strlen(self->name);
504                 if (slen > dsos__col_width)
505                         dsos__col_width = slen;
506         }
507
508         self->slen_calculated = 1;
509 }
510
511 int event__preprocess_sample(const event_t *self, struct perf_session *session,
512                              struct addr_location *al, symbol_filter_t filter)
513 {
514         u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
515         struct thread *thread = perf_session__findnew(session, self->ip.pid);
516
517         if (thread == NULL)
518                 return -1;
519
520         if (symbol_conf.comm_list &&
521             !strlist__has_entry(symbol_conf.comm_list, thread->comm))
522                 goto out_filtered;
523
524         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
525
526         thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
527                               self->ip.ip, al);
528         dump_printf(" ...... dso: %s\n",
529                     al->map ? al->map->dso->long_name :
530                         al->level == 'H' ? "[hypervisor]" : "<not found>");
531         al->sym = NULL;
532
533         if (al->map) {
534                 if (symbol_conf.dso_list &&
535                     (!al->map || !al->map->dso ||
536                      !(strlist__has_entry(symbol_conf.dso_list,
537                                           al->map->dso->short_name) ||
538                        (al->map->dso->short_name != al->map->dso->long_name &&
539                         strlist__has_entry(symbol_conf.dso_list,
540                                            al->map->dso->long_name)))))
541                         goto out_filtered;
542                 /*
543                  * We have to do this here as we may have a dso with no symbol
544                  * hit that has a name longer than the ones with symbols
545                  * sampled.
546                  */
547                 if (!sort_dso.elide && !al->map->dso->slen_calculated)
548                         dso__calc_col_width(al->map->dso);
549
550                 al->sym = map__find_symbol(al->map, al->addr, filter);
551         }
552
553         if (symbol_conf.sym_list && al->sym &&
554             !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
555                 goto out_filtered;
556
557         al->filtered = false;
558         return 0;
559
560 out_filtered:
561         al->filtered = true;
562         return 0;
563 }
564
565 int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
566 {
567         u64 *array = event->sample.array;
568
569         if (type & PERF_SAMPLE_IP) {
570                 data->ip = event->ip.ip;
571                 array++;
572         }
573
574         if (type & PERF_SAMPLE_TID) {
575                 u32 *p = (u32 *)array;
576                 data->pid = p[0];
577                 data->tid = p[1];
578                 array++;
579         }
580
581         if (type & PERF_SAMPLE_TIME) {
582                 data->time = *array;
583                 array++;
584         }
585
586         if (type & PERF_SAMPLE_ADDR) {
587                 data->addr = *array;
588                 array++;
589         }
590
591         if (type & PERF_SAMPLE_ID) {
592                 data->id = *array;
593                 array++;
594         }
595
596         if (type & PERF_SAMPLE_STREAM_ID) {
597                 data->stream_id = *array;
598                 array++;
599         }
600
601         if (type & PERF_SAMPLE_CPU) {
602                 u32 *p = (u32 *)array;
603                 data->cpu = *p;
604                 array++;
605         }
606
607         if (type & PERF_SAMPLE_PERIOD) {
608                 data->period = *array;
609                 array++;
610         }
611
612         if (type & PERF_SAMPLE_READ) {
613                 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
614                 return -1;
615         }
616
617         if (type & PERF_SAMPLE_CALLCHAIN) {
618                 data->callchain = (struct ip_callchain *)array;
619                 array += 1 + data->callchain->nr;
620         }
621
622         if (type & PERF_SAMPLE_RAW) {
623                 u32 *p = (u32 *)array;
624                 data->raw_size = *p;
625                 p++;
626                 data->raw_data = p;
627         }
628
629         return 0;
630 }