blob: bfb3d872b9f5d3187e2dd4c2d0229565fc380f22 [file] [log] [blame]
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02001#include <linux/types.h>
2#include "event.h"
3#include "debug.h"
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -02004#include "session.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02005#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02006#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02007#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02008#include "thread.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02009
10static pid_t event__synthesize_comm(pid_t pid, int full,
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -020011 event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020012 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020013{
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) {
27out_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
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020061 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020062 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
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020079 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020080 }
81 closedir(tasks);
82
83out_fclose:
84 fclose(fp);
85 return tgid;
86
87out_failure:
88 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
89 return -1;
90}
91
92static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -020093 event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020094 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020095{
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
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200149 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200150 }
151 }
152
153 fclose(fp);
154 return 0;
155}
156
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200157int event__synthesize_thread(pid_t pid, event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200158 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200159{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200160 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200161 if (tgid == -1)
162 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200163 return event__synthesize_mmap_events(pid, tgid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200164}
165
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200166void event__synthesize_threads(event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200167 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200168{
169 DIR *proc;
170 struct dirent dirent, *next;
171
172 proc = opendir("/proc");
173
174 while (!readdir_r(proc, &dirent, &next) && next) {
175 char *end;
176 pid_t pid = strtol(dirent.d_name, &end, 10);
177
178 if (*end) /* only interested in proper numerical dirents */
179 continue;
180
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200181 event__synthesize_thread(pid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200182 }
183
184 closedir(proc);
185}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200186
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200187struct process_symbol_args {
188 const char *name;
189 u64 start;
190};
191
192static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
193{
194 struct process_symbol_args *args = arg;
195
196 if (!symbol_type__is_a(type, MAP__FUNCTION) || strcmp(name, args->name))
197 return 0;
198
199 args->start = start;
200 return 1;
201}
202
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200203int event__synthesize_kernel_mmap(event__handler_t process,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200204 struct perf_session *session,
205 const char *symbol_name)
206{
207 size_t size;
208 event_t ev = {
209 .header = { .type = PERF_RECORD_MMAP },
210 };
211 /*
212 * We should get this from /sys/kernel/sections/.text, but till that is
213 * available use this, and after it is use this as a fallback for older
214 * kernels.
215 */
216 struct process_symbol_args args = { .name = symbol_name, };
217
218 if (kallsyms__parse(&args, find_symbol_cb) <= 0)
219 return -ENOENT;
220
221 size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
222 "[kernel.kallsyms.%s]", symbol_name) + 1;
223 size = ALIGN(size, sizeof(u64));
224 ev.mmap.header.size = (sizeof(ev.mmap) - (sizeof(ev.mmap.filename) - size));
225 ev.mmap.start = args.start;
226
227 return process(&ev, session);
228}
229
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200230static void thread__comm_adjust(struct thread *self)
231{
232 char *comm = self->comm;
233
234 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
235 (!symbol_conf.comm_list ||
236 strlist__has_entry(symbol_conf.comm_list, comm))) {
237 unsigned int slen = strlen(comm);
238
239 if (slen > comms__col_width) {
240 comms__col_width = slen;
241 threads__col_width = slen + 6;
242 }
243 }
244}
245
246static int thread__set_comm_adjust(struct thread *self, const char *comm)
247{
248 int ret = thread__set_comm(self, comm);
249
250 if (ret)
251 return ret;
252
253 thread__comm_adjust(self);
254
255 return 0;
256}
257
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200258int event__process_comm(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200259{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200260 struct thread *thread = perf_session__findnew(session, self->comm.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200261
Li Zefanbab81b62009-12-01 14:04:49 +0800262 dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200263
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200264 if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200265 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
266 return -1;
267 }
268
269 return 0;
270}
271
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200272int event__process_lost(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200273{
274 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200275 session->events_stats.lost += self->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200276 return 0;
277}
278
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200279int event__process_mmap(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200280{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200281 struct thread *thread;
282 struct map *map;
283 static const char kmmap_prefix[] = "[kernel.kallsyms.";
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200284
285 dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
286 self->mmap.pid, self->mmap.tid,
287 (void *)(long)self->mmap.start,
288 (void *)(long)self->mmap.len,
289 (void *)(long)self->mmap.pgoff,
290 self->mmap.filename);
291
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200292 if (self->mmap.pid == 0 &&
293 memcmp(self->mmap.filename, kmmap_prefix,
294 sizeof(kmmap_prefix) - 1) == 0) {
295 const char *symbol_name = (self->mmap.filename +
296 sizeof(kmmap_prefix) - 1);
297 perf_session__set_kallsyms_ref_reloc_sym(session, symbol_name,
298 self->mmap.start);
299 return 0;
300 }
301
302 thread = perf_session__findnew(session, self->mmap.pid);
303 map = map__new(&self->mmap, MAP__FUNCTION,
304 session->cwd, session->cwdlen);
305
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200306 if (thread == NULL || map == NULL)
307 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
308 else
309 thread__insert_map(thread, map);
310
311 return 0;
312}
313
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200314int event__process_task(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200315{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200316 struct thread *thread = perf_session__findnew(session, self->fork.pid);
317 struct thread *parent = perf_session__findnew(session, self->fork.ppid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200318
319 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
320 self->fork.ppid, self->fork.ptid);
321 /*
322 * A thread clone will have the same PID for both parent and child.
323 */
324 if (thread == parent)
325 return 0;
326
327 if (self->header.type == PERF_RECORD_EXIT)
328 return 0;
329
330 if (thread == NULL || parent == NULL ||
331 thread__fork(thread, parent) < 0) {
332 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
333 return -1;
334 }
335
336 return 0;
337}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200338
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200339void thread__find_addr_location(struct thread *self,
340 struct perf_session *session, u8 cpumode,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200341 enum map_type type, u64 addr,
342 struct addr_location *al,
343 symbol_filter_t filter)
344{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200345 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200346
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200347 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200348 al->addr = addr;
349
350 if (cpumode & PERF_RECORD_MISC_KERNEL) {
351 al->level = 'k';
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200352 mg = &session->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200353 } else if (cpumode & PERF_RECORD_MISC_USER)
354 al->level = '.';
355 else {
356 al->level = 'H';
357 al->map = NULL;
358 al->sym = NULL;
359 return;
360 }
361try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200362 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200363 if (al->map == NULL) {
364 /*
365 * If this is outside of all known maps, and is a negative
366 * address, try to look it up in the kernel dso, as it might be
367 * a vsyscall or vdso (which executes in user-mode).
368 *
369 * XXX This is nasty, we should have a symbol list in the
370 * "[vdso]" dso, but for now lets use the old trick of looking
371 * in the whole kernel symbol list.
372 */
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200373 if ((long long)al->addr < 0 && mg != &session->kmaps) {
374 mg = &session->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200375 goto try_again;
376 }
377 al->sym = NULL;
378 } else {
379 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200380 al->sym = map__find_symbol(al->map, session, al->addr, filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200381 }
382}
383
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200384static void dso__calc_col_width(struct dso *self)
385{
386 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
387 (!symbol_conf.dso_list ||
388 strlist__has_entry(symbol_conf.dso_list, self->name))) {
389 unsigned int slen = strlen(self->name);
390 if (slen > dsos__col_width)
391 dsos__col_width = slen;
392 }
393
394 self->slen_calculated = 1;
395}
396
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200397int event__preprocess_sample(const event_t *self, struct perf_session *session,
398 struct addr_location *al, symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200399{
400 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200401 struct thread *thread = perf_session__findnew(session, self->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200402
403 if (thread == NULL)
404 return -1;
405
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200406 if (symbol_conf.comm_list &&
407 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
408 goto out_filtered;
409
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200410 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
411
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200412 thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200413 self->ip.ip, al, filter);
414 dump_printf(" ...... dso: %s\n",
415 al->map ? al->map->dso->long_name :
416 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200417 /*
418 * We have to do this here as we may have a dso with no symbol hit that
419 * has a name longer than the ones with symbols sampled.
420 */
421 if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
422 dso__calc_col_width(al->map->dso);
423
424 if (symbol_conf.dso_list &&
425 (!al->map || !al->map->dso ||
426 !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
427 (al->map->dso->short_name != al->map->dso->long_name &&
428 strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
429 goto out_filtered;
430
431 if (symbol_conf.sym_list && al->sym &&
432 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
433 goto out_filtered;
434
435 al->filtered = false;
436 return 0;
437
438out_filtered:
439 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200440 return 0;
441}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900442
443int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
444{
445 u64 *array = event->sample.array;
446
447 if (type & PERF_SAMPLE_IP) {
448 data->ip = event->ip.ip;
449 array++;
450 }
451
452 if (type & PERF_SAMPLE_TID) {
453 u32 *p = (u32 *)array;
454 data->pid = p[0];
455 data->tid = p[1];
456 array++;
457 }
458
459 if (type & PERF_SAMPLE_TIME) {
460 data->time = *array;
461 array++;
462 }
463
464 if (type & PERF_SAMPLE_ADDR) {
465 data->addr = *array;
466 array++;
467 }
468
469 if (type & PERF_SAMPLE_ID) {
470 data->id = *array;
471 array++;
472 }
473
474 if (type & PERF_SAMPLE_STREAM_ID) {
475 data->stream_id = *array;
476 array++;
477 }
478
479 if (type & PERF_SAMPLE_CPU) {
480 u32 *p = (u32 *)array;
481 data->cpu = *p;
482 array++;
483 }
484
485 if (type & PERF_SAMPLE_PERIOD) {
486 data->period = *array;
487 array++;
488 }
489
490 if (type & PERF_SAMPLE_READ) {
491 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
492 return -1;
493 }
494
495 if (type & PERF_SAMPLE_CALLCHAIN) {
496 data->callchain = (struct ip_callchain *)array;
497 array += 1 + data->callchain->nr;
498 }
499
500 if (type & PERF_SAMPLE_RAW) {
501 u32 *p = (u32 *)array;
502 data->raw_size = *p;
503 p++;
504 data->raw_data = p;
505 }
506
507 return 0;
508}