blob: 40d8d842a21f94a5a7cf2ba9af02972a98fc7d25 [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 Melo234fbbf2009-10-26 19:23:18 -02005#include "string.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02006#include "thread.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02007
8static pid_t event__synthesize_comm(pid_t pid, int full,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02009 int (*process)(event_t *event,
10 struct perf_session *session),
11 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020012{
13 event_t ev;
14 char filename[PATH_MAX];
15 char bf[BUFSIZ];
16 FILE *fp;
17 size_t size = 0;
18 DIR *tasks;
19 struct dirent dirent, *next;
20 pid_t tgid = 0;
21
22 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
23
24 fp = fopen(filename, "r");
25 if (fp == NULL) {
26out_race:
27 /*
28 * We raced with a task exiting - just return:
29 */
30 pr_debug("couldn't open %s\n", filename);
31 return 0;
32 }
33
34 memset(&ev.comm, 0, sizeof(ev.comm));
35 while (!ev.comm.comm[0] || !ev.comm.pid) {
36 if (fgets(bf, sizeof(bf), fp) == NULL)
37 goto out_failure;
38
39 if (memcmp(bf, "Name:", 5) == 0) {
40 char *name = bf + 5;
41 while (*name && isspace(*name))
42 ++name;
43 size = strlen(name) - 1;
44 memcpy(ev.comm.comm, name, size++);
45 } else if (memcmp(bf, "Tgid:", 5) == 0) {
46 char *tgids = bf + 5;
47 while (*tgids && isspace(*tgids))
48 ++tgids;
49 tgid = ev.comm.pid = atoi(tgids);
50 }
51 }
52
53 ev.comm.header.type = PERF_RECORD_COMM;
54 size = ALIGN(size, sizeof(u64));
55 ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
56
57 if (!full) {
58 ev.comm.tid = pid;
59
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020060 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020061 goto out_fclose;
62 }
63
64 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
65
66 tasks = opendir(filename);
67 if (tasks == NULL)
68 goto out_race;
69
70 while (!readdir_r(tasks, &dirent, &next) && next) {
71 char *end;
72 pid = strtol(dirent.d_name, &end, 10);
73 if (*end)
74 continue;
75
76 ev.comm.tid = pid;
77
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020078 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020079 }
80 closedir(tasks);
81
82out_fclose:
83 fclose(fp);
84 return tgid;
85
86out_failure:
87 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
88 return -1;
89}
90
91static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020092 int (*process)(event_t *event,
93 struct perf_session *session),
94 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 Melod8f66242009-12-13 19:50:24 -0200157int event__synthesize_thread(pid_t pid,
158 int (*process)(event_t *event,
159 struct perf_session *session),
160 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200161{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200162 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200163 if (tgid == -1)
164 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200165 return event__synthesize_mmap_events(pid, tgid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200166}
167
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200168void event__synthesize_threads(int (*process)(event_t *event,
169 struct perf_session *session),
170 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200171{
172 DIR *proc;
173 struct dirent dirent, *next;
174
175 proc = opendir("/proc");
176
177 while (!readdir_r(proc, &dirent, &next) && next) {
178 char *end;
179 pid_t pid = strtol(dirent.d_name, &end, 10);
180
181 if (*end) /* only interested in proper numerical dirents */
182 continue;
183
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200184 event__synthesize_thread(pid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200185 }
186
187 closedir(proc);
188}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200189
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200190struct events_stats event__stats;
191
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200192int event__process_comm(event_t *self, struct perf_session *session __used)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200193{
194 struct thread *thread = threads__findnew(self->comm.pid);
195
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 Melod8f66242009-12-13 19:50:24 -0200206int event__process_lost(event_t *self, struct perf_session *session __used)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200207{
208 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
209 event__stats.lost += self->lost.lost;
210 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{
215 struct thread *thread = threads__findnew(self->mmap.pid);
216 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 Melod8f66242009-12-13 19:50:24 -0200234int event__process_task(event_t *self, struct perf_session *session __used)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200235{
236 struct thread *thread = threads__findnew(self->fork.pid);
237 struct thread *parent = threads__findnew(self->fork.ppid);
238
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
259void thread__find_addr_location(struct thread *self, u8 cpumode,
260 enum map_type type, u64 addr,
261 struct addr_location *al,
262 symbol_filter_t filter)
263{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200264 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200265
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200266 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200267 al->addr = addr;
268
269 if (cpumode & PERF_RECORD_MISC_KERNEL) {
270 al->level = 'k';
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200271 mg = kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200272 } else if (cpumode & PERF_RECORD_MISC_USER)
273 al->level = '.';
274 else {
275 al->level = 'H';
276 al->map = NULL;
277 al->sym = NULL;
278 return;
279 }
280try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200281 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200282 if (al->map == NULL) {
283 /*
284 * If this is outside of all known maps, and is a negative
285 * address, try to look it up in the kernel dso, as it might be
286 * a vsyscall or vdso (which executes in user-mode).
287 *
288 * XXX This is nasty, we should have a symbol list in the
289 * "[vdso]" dso, but for now lets use the old trick of looking
290 * in the whole kernel symbol list.
291 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200292 if ((long long)al->addr < 0 && mg != kmaps) {
293 mg = kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200294 goto try_again;
295 }
296 al->sym = NULL;
297 } else {
298 al->addr = al->map->map_ip(al->map, al->addr);
299 al->sym = map__find_symbol(al->map, al->addr, filter);
300 }
301}
302
303int event__preprocess_sample(const event_t *self, struct addr_location *al,
304 symbol_filter_t filter)
305{
306 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
307 struct thread *thread = threads__findnew(self->ip.pid);
308
309 if (thread == NULL)
310 return -1;
311
312 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
313
314 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
315 self->ip.ip, al, filter);
316 dump_printf(" ...... dso: %s\n",
317 al->map ? al->map->dso->long_name :
318 al->level == 'H' ? "[hypervisor]" : "<not found>");
319 return 0;
320}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900321
322int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
323{
324 u64 *array = event->sample.array;
325
326 if (type & PERF_SAMPLE_IP) {
327 data->ip = event->ip.ip;
328 array++;
329 }
330
331 if (type & PERF_SAMPLE_TID) {
332 u32 *p = (u32 *)array;
333 data->pid = p[0];
334 data->tid = p[1];
335 array++;
336 }
337
338 if (type & PERF_SAMPLE_TIME) {
339 data->time = *array;
340 array++;
341 }
342
343 if (type & PERF_SAMPLE_ADDR) {
344 data->addr = *array;
345 array++;
346 }
347
348 if (type & PERF_SAMPLE_ID) {
349 data->id = *array;
350 array++;
351 }
352
353 if (type & PERF_SAMPLE_STREAM_ID) {
354 data->stream_id = *array;
355 array++;
356 }
357
358 if (type & PERF_SAMPLE_CPU) {
359 u32 *p = (u32 *)array;
360 data->cpu = *p;
361 array++;
362 }
363
364 if (type & PERF_SAMPLE_PERIOD) {
365 data->period = *array;
366 array++;
367 }
368
369 if (type & PERF_SAMPLE_READ) {
370 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
371 return -1;
372 }
373
374 if (type & PERF_SAMPLE_CALLCHAIN) {
375 data->callchain = (struct ip_callchain *)array;
376 array += 1 + data->callchain->nr;
377 }
378
379 if (type & PERF_SAMPLE_RAW) {
380 u32 *p = (u32 *)array;
381 data->raw_size = *p;
382 p++;
383 data->raw_data = p;
384 }
385
386 return 0;
387}