perf tools: Don't cast RIP to pointers
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / event.c
1 #include <linux/types.h>
2 #include "event.h"
3 #include "debug.h"
4 #include "session.h"
5 #include "sort.h"
6 #include "string.h"
7 #include "strlist.h"
8 #include "thread.h"
9
10 static 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 = { .type = PERF_RECORD_MMAP },
114                 };
115                 int n;
116                 size_t size;
117                 if (fgets(bf, sizeof(bf), fp) == NULL)
118                         break;
119
120                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
121                 n = hex2u64(pbf, &ev.mmap.start);
122                 if (n < 0)
123                         continue;
124                 pbf += n + 1;
125                 n = hex2u64(pbf, &ev.mmap.len);
126                 if (n < 0)
127                         continue;
128                 pbf += n + 3;
129                 if (*pbf == 'x') { /* vm_exec */
130                         char *execname = strchr(bf, '/');
131
132                         /* Catch VDSO */
133                         if (execname == NULL)
134                                 execname = strstr(bf, "[vdso]");
135
136                         if (execname == NULL)
137                                 continue;
138
139                         size = strlen(execname);
140                         execname[size - 1] = '\0'; /* Remove \n */
141                         memcpy(ev.mmap.filename, execname, size);
142                         size = ALIGN(size, sizeof(u64));
143                         ev.mmap.len -= ev.mmap.start;
144                         ev.mmap.header.size = (sizeof(ev.mmap) -
145                                                (sizeof(ev.mmap.filename) - size));
146                         ev.mmap.pid = tgid;
147                         ev.mmap.tid = pid;
148
149                         process(&ev, session);
150                 }
151         }
152
153         fclose(fp);
154         return 0;
155 }
156
157 int event__synthesize_modules(event__handler_t process,
158                               struct perf_session *session)
159 {
160         struct rb_node *nd;
161
162         for (nd = rb_first(&session->kmaps.maps[MAP__FUNCTION]);
163              nd; nd = rb_next(nd)) {
164                 event_t ev;
165                 size_t size;
166                 struct map *pos = rb_entry(nd, struct map, rb_node);
167
168                 if (pos->dso->kernel)
169                         continue;
170
171                 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
172                 memset(&ev, 0, sizeof(ev));
173                 ev.mmap.header.type = PERF_RECORD_MMAP;
174                 ev.mmap.header.size = (sizeof(ev.mmap) -
175                                         (sizeof(ev.mmap.filename) - size));
176                 ev.mmap.start = pos->start;
177                 ev.mmap.len   = pos->end - pos->start;
178
179                 memcpy(ev.mmap.filename, pos->dso->long_name,
180                        pos->dso->long_name_len + 1);
181                 process(&ev, session);
182         }
183
184         return 0;
185 }
186
187 int event__synthesize_thread(pid_t pid, event__handler_t process,
188                              struct perf_session *session)
189 {
190         pid_t tgid = event__synthesize_comm(pid, 1, process, session);
191         if (tgid == -1)
192                 return -1;
193         return event__synthesize_mmap_events(pid, tgid, process, session);
194 }
195
196 void event__synthesize_threads(event__handler_t process,
197                                struct perf_session *session)
198 {
199         DIR *proc;
200         struct dirent dirent, *next;
201
202         proc = opendir("/proc");
203
204         while (!readdir_r(proc, &dirent, &next) && next) {
205                 char *end;
206                 pid_t pid = strtol(dirent.d_name, &end, 10);
207
208                 if (*end) /* only interested in proper numerical dirents */
209                         continue;
210
211                 event__synthesize_thread(pid, process, session);
212         }
213
214         closedir(proc);
215 }
216
217 struct process_symbol_args {
218         const char *name;
219         u64        start;
220 };
221
222 static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
223 {
224         struct process_symbol_args *args = arg;
225
226         if (!symbol_type__is_a(type, MAP__FUNCTION) || strcmp(name, args->name))
227                 return 0;
228
229         args->start = start;
230         return 1;
231 }
232
233 int event__synthesize_kernel_mmap(event__handler_t process,
234                                   struct perf_session *session,
235                                   const char *symbol_name)
236 {
237         size_t size;
238         event_t ev = {
239                 .header = { .type = PERF_RECORD_MMAP },
240         };
241         /*
242          * We should get this from /sys/kernel/sections/.text, but till that is
243          * available use this, and after it is use this as a fallback for older
244          * kernels.
245          */
246         struct process_symbol_args args = { .name = symbol_name, };
247
248         if (kallsyms__parse(&args, find_symbol_cb) <= 0)
249                 return -ENOENT;
250
251         size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
252                         "[kernel.kallsyms.%s]", symbol_name) + 1;
253         size = ALIGN(size, sizeof(u64));
254         ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size));
255         ev.mmap.pgoff = args.start;
256         ev.mmap.start = session->vmlinux_maps[MAP__FUNCTION]->start;
257         ev.mmap.len   = session->vmlinux_maps[MAP__FUNCTION]->end - ev.mmap.start ;
258
259         return process(&ev, session);
260 }
261
262 static void thread__comm_adjust(struct thread *self)
263 {
264         char *comm = self->comm;
265
266         if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
267             (!symbol_conf.comm_list ||
268              strlist__has_entry(symbol_conf.comm_list, comm))) {
269                 unsigned int slen = strlen(comm);
270
271                 if (slen > comms__col_width) {
272                         comms__col_width = slen;
273                         threads__col_width = slen + 6;
274                 }
275         }
276 }
277
278 static int thread__set_comm_adjust(struct thread *self, const char *comm)
279 {
280         int ret = thread__set_comm(self, comm);
281
282         if (ret)
283                 return ret;
284
285         thread__comm_adjust(self);
286
287         return 0;
288 }
289
290 int event__process_comm(event_t *self, struct perf_session *session)
291 {
292         struct thread *thread = perf_session__findnew(session, self->comm.pid);
293
294         dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
295
296         if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
297                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
298                 return -1;
299         }
300
301         return 0;
302 }
303
304 int event__process_lost(event_t *self, struct perf_session *session)
305 {
306         dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
307         session->events_stats.lost += self->lost.lost;
308         return 0;
309 }
310
311 int event__process_mmap(event_t *self, struct perf_session *session)
312 {
313         struct thread *thread;
314         struct map *map;
315
316         dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
317                     self->mmap.pid, self->mmap.tid, self->mmap.start,
318                     self->mmap.len, self->mmap.pgoff, self->mmap.filename);
319
320         if (self->mmap.pid == 0) {
321                 static const char kmmap_prefix[] = "[kernel.kallsyms.";
322
323                 if (self->mmap.filename[0] == '/') {
324                         char short_module_name[1024];
325                         char *name = strrchr(self->mmap.filename, '/'), *dot;
326
327                         if (name == NULL)
328                                 goto out_problem;
329
330                         ++name; /* skip / */
331                         dot = strrchr(name, '.');
332                         if (dot == NULL)
333                                 goto out_problem;
334
335                         snprintf(short_module_name, sizeof(short_module_name),
336                                  "[%.*s]", (int)(dot - name), name);
337                         strxfrchar(short_module_name, '-', '_');
338
339                         map = perf_session__new_module_map(session,
340                                                            self->mmap.start,
341                                                            short_module_name);
342                         if (map == NULL)
343                                 goto out_problem;
344
345                         name = strdup(self->mmap.filename);
346                         if (name == NULL)
347                                 goto out_problem;
348
349                         dso__set_long_name(map->dso, name);
350                         map->end = map->start + self->mmap.len;
351                 } else if (memcmp(self->mmap.filename, kmmap_prefix,
352                                 sizeof(kmmap_prefix) - 1) == 0) {
353                         const char *symbol_name = (self->mmap.filename +
354                                                    sizeof(kmmap_prefix) - 1);
355                         /*
356                          * Should be there already, from the build-id table in
357                          * the header.
358                          */
359                         struct dso *kernel = __dsos__findnew(&dsos__kernel,
360                                                              "[kernel.kallsyms]");
361                         if (kernel == NULL)
362                                 goto out_problem;
363
364                         if (__map_groups__create_kernel_maps(&session->kmaps,
365                                                              session->vmlinux_maps,
366                                                              kernel) < 0)
367                                 goto out_problem;
368
369                         session->vmlinux_maps[MAP__FUNCTION]->start = self->mmap.start;
370                         session->vmlinux_maps[MAP__FUNCTION]->end   = self->mmap.start + self->mmap.len;
371
372                         perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name,
373                                                                  self->mmap.pgoff);
374                 }
375                 return 0;
376         }
377
378         thread = perf_session__findnew(session, self->mmap.pid);
379         map = map__new(&self->mmap, MAP__FUNCTION,
380                        session->cwd, session->cwdlen);
381
382         if (thread == NULL || map == NULL)
383                 goto out_problem;
384
385         thread__insert_map(thread, map);
386         return 0;
387
388 out_problem:
389         dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
390         return 0;
391 }
392
393 int event__process_task(event_t *self, struct perf_session *session)
394 {
395         struct thread *thread = perf_session__findnew(session, self->fork.pid);
396         struct thread *parent = perf_session__findnew(session, self->fork.ppid);
397
398         dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
399                     self->fork.ppid, self->fork.ptid);
400         /*
401          * A thread clone will have the same PID for both parent and child.
402          */
403         if (thread == parent)
404                 return 0;
405
406         if (self->header.type == PERF_RECORD_EXIT)
407                 return 0;
408
409         if (thread == NULL || parent == NULL ||
410             thread__fork(thread, parent) < 0) {
411                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
412                 return -1;
413         }
414
415         return 0;
416 }
417
418 void thread__find_addr_location(struct thread *self,
419                                 struct perf_session *session, u8 cpumode,
420                                 enum map_type type, u64 addr,
421                                 struct addr_location *al,
422                                 symbol_filter_t filter)
423 {
424         struct map_groups *mg = &self->mg;
425
426         al->thread = self;
427         al->addr = addr;
428
429         if (cpumode & PERF_RECORD_MISC_KERNEL) {
430                 al->level = 'k';
431                 mg = &session->kmaps;
432         } else if (cpumode & PERF_RECORD_MISC_USER)
433                 al->level = '.';
434         else {
435                 al->level = 'H';
436                 al->map = NULL;
437                 al->sym = NULL;
438                 return;
439         }
440 try_again:
441         al->map = map_groups__find(mg, type, al->addr);
442         if (al->map == NULL) {
443                 /*
444                  * If this is outside of all known maps, and is a negative
445                  * address, try to look it up in the kernel dso, as it might be
446                  * a vsyscall or vdso (which executes in user-mode).
447                  *
448                  * XXX This is nasty, we should have a symbol list in the
449                  * "[vdso]" dso, but for now lets use the old trick of looking
450                  * in the whole kernel symbol list.
451                  */
452                 if ((long long)al->addr < 0 && mg != &session->kmaps) {
453                         mg = &session->kmaps;
454                         goto try_again;
455                 }
456                 al->sym = NULL;
457         } else {
458                 al->addr = al->map->map_ip(al->map, al->addr);
459                 al->sym = map__find_symbol(al->map, session, al->addr, filter);
460         }
461 }
462
463 static void dso__calc_col_width(struct dso *self)
464 {
465         if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
466             (!symbol_conf.dso_list ||
467              strlist__has_entry(symbol_conf.dso_list, self->name))) {
468                 unsigned int slen = strlen(self->name);
469                 if (slen > dsos__col_width)
470                         dsos__col_width = slen;
471         }
472
473         self->slen_calculated = 1;
474 }
475
476 int event__preprocess_sample(const event_t *self, struct perf_session *session,
477                              struct addr_location *al, symbol_filter_t filter)
478 {
479         u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
480         struct thread *thread = perf_session__findnew(session, self->ip.pid);
481
482         if (thread == NULL)
483                 return -1;
484
485         if (symbol_conf.comm_list &&
486             !strlist__has_entry(symbol_conf.comm_list, thread->comm))
487                 goto out_filtered;
488
489         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
490
491         thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
492                                    self->ip.ip, al, filter);
493         dump_printf(" ...... dso: %s\n",
494                     al->map ? al->map->dso->long_name :
495                         al->level == 'H' ? "[hypervisor]" : "<not found>");
496         /*
497          * We have to do this here as we may have a dso with no symbol hit that
498          * has a name longer than the ones with symbols sampled.
499          */
500         if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
501                 dso__calc_col_width(al->map->dso);
502
503         if (symbol_conf.dso_list &&
504             (!al->map || !al->map->dso ||
505              !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
506                (al->map->dso->short_name != al->map->dso->long_name &&
507                 strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
508                 goto out_filtered;
509
510         if (symbol_conf.sym_list && al->sym &&
511             !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
512                 goto out_filtered;
513
514         al->filtered = false;
515         return 0;
516
517 out_filtered:
518         al->filtered = true;
519         return 0;
520 }
521
522 int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
523 {
524         u64 *array = event->sample.array;
525
526         if (type & PERF_SAMPLE_IP) {
527                 data->ip = event->ip.ip;
528                 array++;
529         }
530
531         if (type & PERF_SAMPLE_TID) {
532                 u32 *p = (u32 *)array;
533                 data->pid = p[0];
534                 data->tid = p[1];
535                 array++;
536         }
537
538         if (type & PERF_SAMPLE_TIME) {
539                 data->time = *array;
540                 array++;
541         }
542
543         if (type & PERF_SAMPLE_ADDR) {
544                 data->addr = *array;
545                 array++;
546         }
547
548         if (type & PERF_SAMPLE_ID) {
549                 data->id = *array;
550                 array++;
551         }
552
553         if (type & PERF_SAMPLE_STREAM_ID) {
554                 data->stream_id = *array;
555                 array++;
556         }
557
558         if (type & PERF_SAMPLE_CPU) {
559                 u32 *p = (u32 *)array;
560                 data->cpu = *p;
561                 array++;
562         }
563
564         if (type & PERF_SAMPLE_PERIOD) {
565                 data->period = *array;
566                 array++;
567         }
568
569         if (type & PERF_SAMPLE_READ) {
570                 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
571                 return -1;
572         }
573
574         if (type & PERF_SAMPLE_CALLCHAIN) {
575                 data->callchain = (struct ip_callchain *)array;
576                 array += 1 + data->callchain->nr;
577         }
578
579         if (type & PERF_SAMPLE_RAW) {
580                 u32 *p = (u32 *)array;
581                 data->raw_size = *p;
582                 p++;
583                 data->raw_data = p;
584         }
585
586         return 0;
587 }