blob: bf491fda1f47cc4a6007be093f793e4a1f3f02b9 [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 Melod8f66242009-12-13 19:50:24 -020011 int (*process)(event_t *event,
12 struct perf_session *session),
13 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020014{
15 event_t ev;
16 char filename[PATH_MAX];
17 char bf[BUFSIZ];
18 FILE *fp;
19 size_t size = 0;
20 DIR *tasks;
21 struct dirent dirent, *next;
22 pid_t tgid = 0;
23
24 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
25
26 fp = fopen(filename, "r");
27 if (fp == NULL) {
28out_race:
29 /*
30 * We raced with a task exiting - just return:
31 */
32 pr_debug("couldn't open %s\n", filename);
33 return 0;
34 }
35
36 memset(&ev.comm, 0, sizeof(ev.comm));
37 while (!ev.comm.comm[0] || !ev.comm.pid) {
38 if (fgets(bf, sizeof(bf), fp) == NULL)
39 goto out_failure;
40
41 if (memcmp(bf, "Name:", 5) == 0) {
42 char *name = bf + 5;
43 while (*name && isspace(*name))
44 ++name;
45 size = strlen(name) - 1;
46 memcpy(ev.comm.comm, name, size++);
47 } else if (memcmp(bf, "Tgid:", 5) == 0) {
48 char *tgids = bf + 5;
49 while (*tgids && isspace(*tgids))
50 ++tgids;
51 tgid = ev.comm.pid = atoi(tgids);
52 }
53 }
54
55 ev.comm.header.type = PERF_RECORD_COMM;
56 size = ALIGN(size, sizeof(u64));
57 ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
58
59 if (!full) {
60 ev.comm.tid = pid;
61
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020062 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020063 goto out_fclose;
64 }
65
66 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
67
68 tasks = opendir(filename);
69 if (tasks == NULL)
70 goto out_race;
71
72 while (!readdir_r(tasks, &dirent, &next) && next) {
73 char *end;
74 pid = strtol(dirent.d_name, &end, 10);
75 if (*end)
76 continue;
77
78 ev.comm.tid = pid;
79
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020080 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020081 }
82 closedir(tasks);
83
84out_fclose:
85 fclose(fp);
86 return tgid;
87
88out_failure:
89 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
90 return -1;
91}
92
93static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020094 int (*process)(event_t *event,
95 struct perf_session *session),
96 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020097{
98 char filename[PATH_MAX];
99 FILE *fp;
100
101 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
102
103 fp = fopen(filename, "r");
104 if (fp == NULL) {
105 /*
106 * We raced with a task exiting - just return:
107 */
108 pr_debug("couldn't open %s\n", filename);
109 return -1;
110 }
111
112 while (1) {
113 char bf[BUFSIZ], *pbf = bf;
114 event_t ev = {
115 .header = { .type = PERF_RECORD_MMAP },
116 };
117 int n;
118 size_t size;
119 if (fgets(bf, sizeof(bf), fp) == NULL)
120 break;
121
122 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
123 n = hex2u64(pbf, &ev.mmap.start);
124 if (n < 0)
125 continue;
126 pbf += n + 1;
127 n = hex2u64(pbf, &ev.mmap.len);
128 if (n < 0)
129 continue;
130 pbf += n + 3;
131 if (*pbf == 'x') { /* vm_exec */
132 char *execname = strchr(bf, '/');
133
134 /* Catch VDSO */
135 if (execname == NULL)
136 execname = strstr(bf, "[vdso]");
137
138 if (execname == NULL)
139 continue;
140
141 size = strlen(execname);
142 execname[size - 1] = '\0'; /* Remove \n */
143 memcpy(ev.mmap.filename, execname, size);
144 size = ALIGN(size, sizeof(u64));
145 ev.mmap.len -= ev.mmap.start;
146 ev.mmap.header.size = (sizeof(ev.mmap) -
147 (sizeof(ev.mmap.filename) - size));
148 ev.mmap.pid = tgid;
149 ev.mmap.tid = pid;
150
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200151 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200152 }
153 }
154
155 fclose(fp);
156 return 0;
157}
158
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200159int event__synthesize_thread(pid_t pid,
160 int (*process)(event_t *event,
161 struct perf_session *session),
162 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200163{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200164 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200165 if (tgid == -1)
166 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200167 return event__synthesize_mmap_events(pid, tgid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200168}
169
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200170void event__synthesize_threads(int (*process)(event_t *event,
171 struct perf_session *session),
172 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200173{
174 DIR *proc;
175 struct dirent dirent, *next;
176
177 proc = opendir("/proc");
178
179 while (!readdir_r(proc, &dirent, &next) && next) {
180 char *end;
181 pid_t pid = strtol(dirent.d_name, &end, 10);
182
183 if (*end) /* only interested in proper numerical dirents */
184 continue;
185
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200186 event__synthesize_thread(pid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200187 }
188
189 closedir(proc);
190}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200191
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200192int event__process_comm(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200193{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200194 struct thread *thread = perf_session__findnew(session, self->comm.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200195
Li Zefanbab81b62009-12-01 14:04:49 +0800196 dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200197
198 if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
199 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
200 return -1;
201 }
202
203 return 0;
204}
205
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200206int event__process_lost(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200207{
208 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200209 session->events_stats.lost += self->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200210 return 0;
211}
212
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200213int event__process_mmap(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200214{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200215 struct thread *thread = perf_session__findnew(session, self->mmap.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200216 struct map *map = map__new(&self->mmap, MAP__FUNCTION,
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200217 session->cwd, session->cwdlen);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200218
219 dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
220 self->mmap.pid, self->mmap.tid,
221 (void *)(long)self->mmap.start,
222 (void *)(long)self->mmap.len,
223 (void *)(long)self->mmap.pgoff,
224 self->mmap.filename);
225
226 if (thread == NULL || map == NULL)
227 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
228 else
229 thread__insert_map(thread, map);
230
231 return 0;
232}
233
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200234int event__process_task(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200235{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200236 struct thread *thread = perf_session__findnew(session, self->fork.pid);
237 struct thread *parent = perf_session__findnew(session, self->fork.ppid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200238
239 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
240 self->fork.ppid, self->fork.ptid);
241 /*
242 * A thread clone will have the same PID for both parent and child.
243 */
244 if (thread == parent)
245 return 0;
246
247 if (self->header.type == PERF_RECORD_EXIT)
248 return 0;
249
250 if (thread == NULL || parent == NULL ||
251 thread__fork(thread, parent) < 0) {
252 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
253 return -1;
254 }
255
256 return 0;
257}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200258
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200259void thread__find_addr_location(struct thread *self,
260 struct perf_session *session, u8 cpumode,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200261 enum map_type type, u64 addr,
262 struct addr_location *al,
263 symbol_filter_t filter)
264{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200265 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200266
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200267 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200268 al->addr = addr;
269
270 if (cpumode & PERF_RECORD_MISC_KERNEL) {
271 al->level = 'k';
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200272 mg = &session->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200273 } else if (cpumode & PERF_RECORD_MISC_USER)
274 al->level = '.';
275 else {
276 al->level = 'H';
277 al->map = NULL;
278 al->sym = NULL;
279 return;
280 }
281try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200282 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200283 if (al->map == NULL) {
284 /*
285 * If this is outside of all known maps, and is a negative
286 * address, try to look it up in the kernel dso, as it might be
287 * a vsyscall or vdso (which executes in user-mode).
288 *
289 * XXX This is nasty, we should have a symbol list in the
290 * "[vdso]" dso, but for now lets use the old trick of looking
291 * in the whole kernel symbol list.
292 */
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200293 if ((long long)al->addr < 0 && mg != &session->kmaps) {
294 mg = &session->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200295 goto try_again;
296 }
297 al->sym = NULL;
298 } else {
299 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200300 al->sym = map__find_symbol(al->map, session, al->addr, filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200301 }
302}
303
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200304static void dso__calc_col_width(struct dso *self)
305{
306 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
307 (!symbol_conf.dso_list ||
308 strlist__has_entry(symbol_conf.dso_list, self->name))) {
309 unsigned int slen = strlen(self->name);
310 if (slen > dsos__col_width)
311 dsos__col_width = slen;
312 }
313
314 self->slen_calculated = 1;
315}
316
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200317int event__preprocess_sample(const event_t *self, struct perf_session *session,
318 struct addr_location *al, symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200319{
320 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200321 struct thread *thread = perf_session__findnew(session, self->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200322
323 if (thread == NULL)
324 return -1;
325
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200326 if (symbol_conf.comm_list &&
327 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
328 goto out_filtered;
329
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200330 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
331
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200332 thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200333 self->ip.ip, al, filter);
334 dump_printf(" ...... dso: %s\n",
335 al->map ? al->map->dso->long_name :
336 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200337 /*
338 * We have to do this here as we may have a dso with no symbol hit that
339 * has a name longer than the ones with symbols sampled.
340 */
341 if (al->map && !sort_dso.elide && !al->map->dso->slen_calculated)
342 dso__calc_col_width(al->map->dso);
343
344 if (symbol_conf.dso_list &&
345 (!al->map || !al->map->dso ||
346 !(strlist__has_entry(symbol_conf.dso_list, al->map->dso->short_name) ||
347 (al->map->dso->short_name != al->map->dso->long_name &&
348 strlist__has_entry(symbol_conf.dso_list, al->map->dso->long_name)))))
349 goto out_filtered;
350
351 if (symbol_conf.sym_list && al->sym &&
352 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
353 goto out_filtered;
354
355 al->filtered = false;
356 return 0;
357
358out_filtered:
359 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200360 return 0;
361}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900362
363int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
364{
365 u64 *array = event->sample.array;
366
367 if (type & PERF_SAMPLE_IP) {
368 data->ip = event->ip.ip;
369 array++;
370 }
371
372 if (type & PERF_SAMPLE_TID) {
373 u32 *p = (u32 *)array;
374 data->pid = p[0];
375 data->tid = p[1];
376 array++;
377 }
378
379 if (type & PERF_SAMPLE_TIME) {
380 data->time = *array;
381 array++;
382 }
383
384 if (type & PERF_SAMPLE_ADDR) {
385 data->addr = *array;
386 array++;
387 }
388
389 if (type & PERF_SAMPLE_ID) {
390 data->id = *array;
391 array++;
392 }
393
394 if (type & PERF_SAMPLE_STREAM_ID) {
395 data->stream_id = *array;
396 array++;
397 }
398
399 if (type & PERF_SAMPLE_CPU) {
400 u32 *p = (u32 *)array;
401 data->cpu = *p;
402 array++;
403 }
404
405 if (type & PERF_SAMPLE_PERIOD) {
406 data->period = *array;
407 array++;
408 }
409
410 if (type & PERF_SAMPLE_READ) {
411 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
412 return -1;
413 }
414
415 if (type & PERF_SAMPLE_CALLCHAIN) {
416 data->callchain = (struct ip_callchain *)array;
417 array += 1 + data->callchain->nr;
418 }
419
420 if (type & PERF_SAMPLE_RAW) {
421 u32 *p = (u32 *)array;
422 data->raw_size = *p;
423 p++;
424 data->raw_data = p;
425 }
426
427 return 0;
428}