blob: 2f33ca9899bdd05884c18bb26b27e6f85698fb8d [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 = {
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200113 .header = {
114 .type = PERF_RECORD_MMAP,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800115 /*
116 * Just like the kernel, see __perf_event_mmap
117 * in kernel/perf_event.c
118 */
119 .misc = PERF_RECORD_MISC_USER,
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200120 },
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200121 };
122 int n;
123 size_t size;
124 if (fgets(bf, sizeof(bf), fp) == NULL)
125 break;
126
127 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
128 n = hex2u64(pbf, &ev.mmap.start);
129 if (n < 0)
130 continue;
131 pbf += n + 1;
132 n = hex2u64(pbf, &ev.mmap.len);
133 if (n < 0)
134 continue;
135 pbf += n + 3;
136 if (*pbf == 'x') { /* vm_exec */
Anton Blanchard4af8b352010-04-03 22:53:31 +1100137 u64 vm_pgoff;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200138 char *execname = strchr(bf, '/');
139
140 /* Catch VDSO */
141 if (execname == NULL)
142 execname = strstr(bf, "[vdso]");
143
144 if (execname == NULL)
145 continue;
146
Anton Blanchard4af8b352010-04-03 22:53:31 +1100147 pbf += 3;
148 n = hex2u64(pbf, &vm_pgoff);
149 /* pgoff is in bytes, not pages */
150 if (n >= 0)
151 ev.mmap.pgoff = vm_pgoff << getpagesize();
152 else
153 ev.mmap.pgoff = 0;
154
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200155 size = strlen(execname);
156 execname[size - 1] = '\0'; /* Remove \n */
157 memcpy(ev.mmap.filename, execname, size);
158 size = ALIGN(size, sizeof(u64));
159 ev.mmap.len -= ev.mmap.start;
160 ev.mmap.header.size = (sizeof(ev.mmap) -
161 (sizeof(ev.mmap.filename) - size));
162 ev.mmap.pid = tgid;
163 ev.mmap.tid = pid;
164
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200165 process(&ev, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200166 }
167 }
168
169 fclose(fp);
170 return 0;
171}
172
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200173int event__synthesize_modules(event__handler_t process,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800174 struct perf_session *session,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300175 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200176{
177 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300178 struct map_groups *kmaps = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800179 u16 misc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200180
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800181 /*
182 * kernel uses 0 for user space maps, see kernel/perf_event.c
183 * __perf_event_mmap
184 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300185 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800186 misc = PERF_RECORD_MISC_KERNEL;
187 else
188 misc = PERF_RECORD_MISC_GUEST_KERNEL;
189
190 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200191 nd; nd = rb_next(nd)) {
192 event_t ev;
193 size_t size;
194 struct map *pos = rb_entry(nd, struct map, rb_node);
195
196 if (pos->dso->kernel)
197 continue;
198
199 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
200 memset(&ev, 0, sizeof(ev));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800201 ev.mmap.header.misc = misc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200202 ev.mmap.header.type = PERF_RECORD_MMAP;
203 ev.mmap.header.size = (sizeof(ev.mmap) -
204 (sizeof(ev.mmap.filename) - size));
205 ev.mmap.start = pos->start;
206 ev.mmap.len = pos->end - pos->start;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300207 ev.mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200208
209 memcpy(ev.mmap.filename, pos->dso->long_name,
210 pos->dso->long_name_len + 1);
211 process(&ev, session);
212 }
213
214 return 0;
215}
216
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200217int event__synthesize_thread(pid_t pid, event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200218 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200219{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200220 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200221 if (tgid == -1)
222 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200223 return event__synthesize_mmap_events(pid, tgid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200224}
225
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200226void event__synthesize_threads(event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200227 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200228{
229 DIR *proc;
230 struct dirent dirent, *next;
231
232 proc = opendir("/proc");
233
234 while (!readdir_r(proc, &dirent, &next) && next) {
235 char *end;
236 pid_t pid = strtol(dirent.d_name, &end, 10);
237
238 if (*end) /* only interested in proper numerical dirents */
239 continue;
240
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200241 event__synthesize_thread(pid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200242 }
243
244 closedir(proc);
245}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200246
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200247struct process_symbol_args {
248 const char *name;
249 u64 start;
250};
251
252static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
253{
254 struct process_symbol_args *args = arg;
255
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200256 /*
257 * Must be a function or at least an alias, as in PARISC64, where "_text" is
258 * an 'A' to the same address as "_stext".
259 */
260 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
261 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200262 return 0;
263
264 args->start = start;
265 return 1;
266}
267
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200268int event__synthesize_kernel_mmap(event__handler_t process,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200269 struct perf_session *session,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300270 struct machine *machine,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200271 const char *symbol_name)
272{
273 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800274 const char *filename, *mmap_name;
275 char path[PATH_MAX];
276 char name_buff[PATH_MAX];
277 struct map *map;
278
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200279 event_t ev = {
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200280 .header = {
281 .type = PERF_RECORD_MMAP,
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200282 },
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200283 };
284 /*
285 * We should get this from /sys/kernel/sections/.text, but till that is
286 * available use this, and after it is use this as a fallback for older
287 * kernels.
288 */
289 struct process_symbol_args args = { .name = symbol_name, };
290
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300291 mmap_name = machine__mmap_name(machine, name_buff);
292 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800293 /*
294 * kernel uses PERF_RECORD_MISC_USER for user space maps,
295 * see kernel/perf_event.c __perf_event_mmap
296 */
297 ev.header.misc = PERF_RECORD_MISC_KERNEL;
298 filename = "/proc/kallsyms";
299 } else {
300 ev.header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300301 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800302 filename = (char *) symbol_conf.default_guest_kallsyms;
303 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300304 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800305 filename = path;
306 }
307 }
308
309 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200310 return -ENOENT;
311
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300312 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200313 size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800314 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200315 size = ALIGN(size, sizeof(u64));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800316 ev.mmap.header.size = (sizeof(ev.mmap) -
317 (sizeof(ev.mmap.filename) - size));
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200318 ev.mmap.pgoff = args.start;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800319 ev.mmap.start = map->start;
320 ev.mmap.len = map->end - ev.mmap.start;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300321 ev.mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200322
323 return process(&ev, session);
324}
325
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200326static void thread__comm_adjust(struct thread *self)
327{
328 char *comm = self->comm;
329
330 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
331 (!symbol_conf.comm_list ||
332 strlist__has_entry(symbol_conf.comm_list, comm))) {
333 unsigned int slen = strlen(comm);
334
335 if (slen > comms__col_width) {
336 comms__col_width = slen;
337 threads__col_width = slen + 6;
338 }
339 }
340}
341
342static int thread__set_comm_adjust(struct thread *self, const char *comm)
343{
344 int ret = thread__set_comm(self, comm);
345
346 if (ret)
347 return ret;
348
349 thread__comm_adjust(self);
350
351 return 0;
352}
353
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200354int event__process_comm(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200355{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200356 struct thread *thread = perf_session__findnew(session, self->comm.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200357
Li Zefanbab81b62009-12-01 14:04:49 +0800358 dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200359
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200360 if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200361 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
362 return -1;
363 }
364
365 return 0;
366}
367
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200368int event__process_lost(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200369{
370 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200371 session->events_stats.lost += self->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200372 return 0;
373}
374
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800375static void event_set_kernel_mmap_len(struct map **maps, event_t *self)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200376{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800377 maps[MAP__FUNCTION]->start = self->mmap.start;
378 maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
379 /*
380 * Be a bit paranoid here, some perf.data file came with
381 * a zero sized synthesized MMAP event for the kernel.
382 */
383 if (maps[MAP__FUNCTION]->end == 0)
384 maps[MAP__FUNCTION]->end = ~0UL;
385}
386
387static int event__process_kernel_mmap(event_t *self,
388 struct perf_session *session)
389{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200390 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800391 char kmmap_prefix[PATH_MAX];
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300392 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800393 enum dso_kernel_type kernel_type;
394 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200395
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300396 machine = perf_session__findnew_machine(session, self->mmap.pid);
397 if (!machine) {
398 pr_err("Can't find id %d's machine\n", self->mmap.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800399 goto out_problem;
400 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200401
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300402 machine__mmap_name(machine, kmmap_prefix);
403 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800404 kernel_type = DSO_TYPE_KERNEL;
405 else
406 kernel_type = DSO_TYPE_GUEST_KERNEL;
407
408 is_kernel_mmap = memcmp(self->mmap.filename,
409 kmmap_prefix,
410 strlen(kmmap_prefix)) == 0;
411 if (self->mmap.filename[0] == '/' ||
412 (!is_kernel_mmap && self->mmap.filename[0] == '[')) {
413
414 char short_module_name[1024];
415 char *name, *dot;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200416
417 if (self->mmap.filename[0] == '/') {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800418 name = strrchr(self->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200419 if (name == NULL)
420 goto out_problem;
421
422 ++name; /* skip / */
423 dot = strrchr(name, '.');
424 if (dot == NULL)
425 goto out_problem;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200426 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800427 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200428 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800429 } else
430 strcpy(short_module_name, self->mmap.filename);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200431
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300432 map = map_groups__new_module(&machine->kmaps,
433 self->mmap.start,
434 self->mmap.filename, machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800435 if (map == NULL)
436 goto out_problem;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200437
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800438 name = strdup(short_module_name);
439 if (name == NULL)
440 goto out_problem;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200441
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800442 map->dso->short_name = name;
443 map->end = map->start + self->mmap.len;
444 } else if (is_kernel_mmap) {
445 const char *symbol_name = (self->mmap.filename +
446 strlen(kmmap_prefix));
447 /*
448 * Should be there already, from the build-id table in
449 * the header.
450 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300451 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
452 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800453 if (kernel == NULL)
454 goto out_problem;
455
456 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300457 if (__map_groups__create_kernel_maps(&machine->kmaps,
458 machine->vmlinux_maps,
459 kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800460 goto out_problem;
461
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300462 event_set_kernel_mmap_len(machine->vmlinux_maps, self);
463 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
464 symbol_name,
465 self->mmap.pgoff);
466 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200467 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800468 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200469 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300470 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
471 NULL);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200472 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800473 }
474 return 0;
475out_problem:
476 return -1;
477}
478
479int event__process_mmap(event_t *self, struct perf_session *session)
480{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300481 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800482 struct thread *thread;
483 struct map *map;
484 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
485 int ret = 0;
486
487 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
488 self->mmap.pid, self->mmap.tid, self->mmap.start,
489 self->mmap.len, self->mmap.pgoff, self->mmap.filename);
490
491 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
492 cpumode == PERF_RECORD_MISC_KERNEL) {
493 ret = event__process_kernel_mmap(self, session);
494 if (ret < 0)
495 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200496 return 0;
497 }
498
499 thread = perf_session__findnew(session, self->mmap.pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300500 machine = perf_session__find_host_machine(session);
501 map = map__new(&machine->user_dsos, self->mmap.start,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800502 self->mmap.len, self->mmap.pgoff,
503 self->mmap.pid, self->mmap.filename,
504 MAP__FUNCTION, session->cwd, session->cwdlen);
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200505
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200506 if (thread == NULL || map == NULL)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200507 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200508
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200509 thread__insert_map(thread, map);
510 return 0;
511
512out_problem:
513 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200514 return 0;
515}
516
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200517int event__process_task(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200518{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200519 struct thread *thread = perf_session__findnew(session, self->fork.pid);
520 struct thread *parent = perf_session__findnew(session, self->fork.ppid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200521
522 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
523 self->fork.ppid, self->fork.ptid);
524 /*
525 * A thread clone will have the same PID for both parent and child.
526 */
527 if (thread == parent)
528 return 0;
529
530 if (self->header.type == PERF_RECORD_EXIT)
531 return 0;
532
533 if (thread == NULL || parent == NULL ||
534 thread__fork(thread, parent) < 0) {
535 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
536 return -1;
537 }
538
539 return 0;
540}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200541
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200542void thread__find_addr_map(struct thread *self,
543 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800544 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200545 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200546{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200547 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300548 struct machine *machine = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200549
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200550 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200551 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800552 al->cpumode = cpumode;
553 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200554
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800555 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200556 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300557 machine = perf_session__find_host_machine(session);
558 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800559 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200560 al->level = '.';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300561 machine = perf_session__find_host_machine(session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800562 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
563 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300564 machine = perf_session__find_machine(session, pid);
565 if (!machine) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800566 al->map = NULL;
567 return;
568 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300569 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800570 } else {
571 /*
572 * 'u' means guest os user space.
573 * TODO: We don't support guest user space. Might support late.
574 */
575 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
576 al->level = 'u';
577 else
578 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200579 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800580
581 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
582 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
583 !perf_guest)
584 al->filtered = true;
585 if ((cpumode == PERF_RECORD_MISC_USER ||
586 cpumode == PERF_RECORD_MISC_KERNEL) &&
587 !perf_host)
588 al->filtered = true;
589
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200590 return;
591 }
592try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200593 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200594 if (al->map == NULL) {
595 /*
596 * If this is outside of all known maps, and is a negative
597 * address, try to look it up in the kernel dso, as it might be
598 * a vsyscall or vdso (which executes in user-mode).
599 *
600 * XXX This is nasty, we should have a symbol list in the
601 * "[vdso]" dso, but for now lets use the old trick of looking
602 * in the whole kernel symbol list.
603 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800604 if ((long long)al->addr < 0 &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300605 cpumode == PERF_RECORD_MISC_KERNEL &&
606 machine && mg != &machine->kmaps) {
607 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200608 goto try_again;
609 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200610 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200611 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200612}
613
614void thread__find_addr_location(struct thread *self,
615 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800616 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200617 struct addr_location *al,
618 symbol_filter_t filter)
619{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800620 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200621 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200622 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200623 else
624 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200625}
626
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200627static void dso__calc_col_width(struct dso *self)
628{
629 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
630 (!symbol_conf.dso_list ||
631 strlist__has_entry(symbol_conf.dso_list, self->name))) {
632 unsigned int slen = strlen(self->name);
633 if (slen > dsos__col_width)
634 dsos__col_width = slen;
635 }
636
637 self->slen_calculated = 1;
638}
639
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200640int event__preprocess_sample(const event_t *self, struct perf_session *session,
641 struct addr_location *al, symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200642{
643 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200644 struct thread *thread = perf_session__findnew(session, self->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200645
646 if (thread == NULL)
647 return -1;
648
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200649 if (symbol_conf.comm_list &&
650 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
651 goto out_filtered;
652
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200653 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
654
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300655 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656 self->ip.pid, self->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200657 dump_printf(" ...... dso: %s\n",
658 al->map ? al->map->dso->long_name :
659 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300660 al->sym = NULL;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200661
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300662 if (al->map) {
663 if (symbol_conf.dso_list &&
664 (!al->map || !al->map->dso ||
665 !(strlist__has_entry(symbol_conf.dso_list,
666 al->map->dso->short_name) ||
667 (al->map->dso->short_name != al->map->dso->long_name &&
668 strlist__has_entry(symbol_conf.dso_list,
669 al->map->dso->long_name)))))
670 goto out_filtered;
671 /*
672 * We have to do this here as we may have a dso with no symbol
673 * hit that has a name longer than the ones with symbols
674 * sampled.
675 */
676 if (!sort_dso.elide && !al->map->dso->slen_calculated)
677 dso__calc_col_width(al->map->dso);
678
679 al->sym = map__find_symbol(al->map, al->addr, filter);
680 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200681
682 if (symbol_conf.sym_list && al->sym &&
683 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
684 goto out_filtered;
685
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200686 return 0;
687
688out_filtered:
689 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200690 return 0;
691}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900692
693int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
694{
695 u64 *array = event->sample.array;
696
697 if (type & PERF_SAMPLE_IP) {
698 data->ip = event->ip.ip;
699 array++;
700 }
701
702 if (type & PERF_SAMPLE_TID) {
703 u32 *p = (u32 *)array;
704 data->pid = p[0];
705 data->tid = p[1];
706 array++;
707 }
708
709 if (type & PERF_SAMPLE_TIME) {
710 data->time = *array;
711 array++;
712 }
713
714 if (type & PERF_SAMPLE_ADDR) {
715 data->addr = *array;
716 array++;
717 }
718
719 if (type & PERF_SAMPLE_ID) {
720 data->id = *array;
721 array++;
722 }
723
724 if (type & PERF_SAMPLE_STREAM_ID) {
725 data->stream_id = *array;
726 array++;
727 }
728
729 if (type & PERF_SAMPLE_CPU) {
730 u32 *p = (u32 *)array;
731 data->cpu = *p;
732 array++;
733 }
734
735 if (type & PERF_SAMPLE_PERIOD) {
736 data->period = *array;
737 array++;
738 }
739
740 if (type & PERF_SAMPLE_READ) {
741 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
742 return -1;
743 }
744
745 if (type & PERF_SAMPLE_CALLCHAIN) {
746 data->callchain = (struct ip_callchain *)array;
747 array += 1 + data->callchain->nr;
748 }
749
750 if (type & PERF_SAMPLE_RAW) {
751 u32 *p = (u32 *)array;
752 data->raw_size = *p;
753 p++;
754 data->raw_data = p;
755 }
756
757 return 0;
758}