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