blob: 23d5dfd4ed7398d11cf08396bd46a499699b7b11 [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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-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 Melo48ea8f52010-04-27 21:19:05 -0300291 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300292 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 Melob7cece72010-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 Melo48ea8f52010-04-27 21:19:05 -0300402 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300403 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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-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 Melob7cece72010-01-13 13:22:17 -0200431
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300432 map = machine__new_module(machine, self->mmap.start,
433 self->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800434 if (map == NULL)
435 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200436
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800437 name = strdup(short_module_name);
438 if (name == NULL)
439 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200440
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800441 map->dso->short_name = name;
442 map->end = map->start + self->mmap.len;
443 } else if (is_kernel_mmap) {
444 const char *symbol_name = (self->mmap.filename +
445 strlen(kmmap_prefix));
446 /*
447 * Should be there already, from the build-id table in
448 * the header.
449 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300450 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
451 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800452 if (kernel == NULL)
453 goto out_problem;
454
455 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300456 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800457 goto out_problem;
458
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300459 event_set_kernel_mmap_len(machine->vmlinux_maps, self);
460 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
461 symbol_name,
462 self->mmap.pgoff);
463 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200464 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800465 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200466 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300467 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
468 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200469 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800470 }
471 return 0;
472out_problem:
473 return -1;
474}
475
476int event__process_mmap(event_t *self, struct perf_session *session)
477{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300478 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800479 struct thread *thread;
480 struct map *map;
481 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
482 int ret = 0;
483
484 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
485 self->mmap.pid, self->mmap.tid, self->mmap.start,
486 self->mmap.len, self->mmap.pgoff, self->mmap.filename);
487
488 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
489 cpumode == PERF_RECORD_MISC_KERNEL) {
490 ret = event__process_kernel_mmap(self, session);
491 if (ret < 0)
492 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200493 return 0;
494 }
495
496 thread = perf_session__findnew(session, self->mmap.pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300497 machine = perf_session__find_host_machine(session);
498 map = map__new(&machine->user_dsos, self->mmap.start,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800499 self->mmap.len, self->mmap.pgoff,
500 self->mmap.pid, self->mmap.filename,
501 MAP__FUNCTION, session->cwd, session->cwdlen);
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200502
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200503 if (thread == NULL || map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200504 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200505
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200506 thread__insert_map(thread, map);
507 return 0;
508
509out_problem:
510 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200511 return 0;
512}
513
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200514int event__process_task(event_t *self, struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200515{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200516 struct thread *thread = perf_session__findnew(session, self->fork.pid);
517 struct thread *parent = perf_session__findnew(session, self->fork.ppid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200518
519 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
520 self->fork.ppid, self->fork.ptid);
521 /*
522 * A thread clone will have the same PID for both parent and child.
523 */
524 if (thread == parent)
525 return 0;
526
527 if (self->header.type == PERF_RECORD_EXIT)
528 return 0;
529
530 if (thread == NULL || parent == NULL ||
531 thread__fork(thread, parent) < 0) {
532 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
533 return -1;
534 }
535
536 return 0;
537}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200538
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200539void thread__find_addr_map(struct thread *self,
540 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800541 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200542 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200543{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200544 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300545 struct machine *machine = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200546
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200547 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200548 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800549 al->cpumode = cpumode;
550 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200551
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800552 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200553 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300554 machine = perf_session__find_host_machine(session);
555 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800556 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200557 al->level = '.';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300558 machine = perf_session__find_host_machine(session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800559 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
560 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300561 machine = perf_session__find_machine(session, pid);
562 if (!machine) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800563 al->map = NULL;
564 return;
565 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300566 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800567 } else {
568 /*
569 * 'u' means guest os user space.
570 * TODO: We don't support guest user space. Might support late.
571 */
572 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
573 al->level = 'u';
574 else
575 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200576 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800577
578 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
579 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
580 !perf_guest)
581 al->filtered = true;
582 if ((cpumode == PERF_RECORD_MISC_USER ||
583 cpumode == PERF_RECORD_MISC_KERNEL) &&
584 !perf_host)
585 al->filtered = true;
586
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200587 return;
588 }
589try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200590 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200591 if (al->map == NULL) {
592 /*
593 * If this is outside of all known maps, and is a negative
594 * address, try to look it up in the kernel dso, as it might be
595 * a vsyscall or vdso (which executes in user-mode).
596 *
597 * XXX This is nasty, we should have a symbol list in the
598 * "[vdso]" dso, but for now lets use the old trick of looking
599 * in the whole kernel symbol list.
600 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800601 if ((long long)al->addr < 0 &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300602 cpumode == PERF_RECORD_MISC_KERNEL &&
603 machine && mg != &machine->kmaps) {
604 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200605 goto try_again;
606 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200607 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200608 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200609}
610
611void thread__find_addr_location(struct thread *self,
612 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800613 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200614 struct addr_location *al,
615 symbol_filter_t filter)
616{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800617 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200618 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200619 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200620 else
621 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200622}
623
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200624static void dso__calc_col_width(struct dso *self)
625{
626 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
627 (!symbol_conf.dso_list ||
628 strlist__has_entry(symbol_conf.dso_list, self->name))) {
Arnaldo Carvalho de Melo98909482010-05-04 20:58:51 -0300629 u16 slen = self->short_name_len;
630 if (verbose)
631 slen = self->long_name_len;
632 if (dsos__col_width < slen)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200633 dsos__col_width = slen;
634 }
635
636 self->slen_calculated = 1;
637}
638
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200639int event__preprocess_sample(const event_t *self, struct perf_session *session,
640 struct addr_location *al, symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200641{
642 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200643 struct thread *thread = perf_session__findnew(session, self->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200644
645 if (thread == NULL)
646 return -1;
647
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200648 if (symbol_conf.comm_list &&
649 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
650 goto out_filtered;
651
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200652 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
653
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300654 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800655 self->ip.pid, self->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200656 dump_printf(" ...... dso: %s\n",
657 al->map ? al->map->dso->long_name :
658 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300659 al->sym = NULL;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200660
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300661 if (al->map) {
662 if (symbol_conf.dso_list &&
663 (!al->map || !al->map->dso ||
664 !(strlist__has_entry(symbol_conf.dso_list,
665 al->map->dso->short_name) ||
666 (al->map->dso->short_name != al->map->dso->long_name &&
667 strlist__has_entry(symbol_conf.dso_list,
668 al->map->dso->long_name)))))
669 goto out_filtered;
670 /*
671 * We have to do this here as we may have a dso with no symbol
672 * hit that has a name longer than the ones with symbols
673 * sampled.
674 */
675 if (!sort_dso.elide && !al->map->dso->slen_calculated)
676 dso__calc_col_width(al->map->dso);
677
678 al->sym = map__find_symbol(al->map, al->addr, filter);
679 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200680
681 if (symbol_conf.sym_list && al->sym &&
682 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
683 goto out_filtered;
684
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200685 return 0;
686
687out_filtered:
688 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200689 return 0;
690}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900691
692int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
693{
694 u64 *array = event->sample.array;
695
696 if (type & PERF_SAMPLE_IP) {
697 data->ip = event->ip.ip;
698 array++;
699 }
700
701 if (type & PERF_SAMPLE_TID) {
702 u32 *p = (u32 *)array;
703 data->pid = p[0];
704 data->tid = p[1];
705 array++;
706 }
707
708 if (type & PERF_SAMPLE_TIME) {
709 data->time = *array;
710 array++;
711 }
712
713 if (type & PERF_SAMPLE_ADDR) {
714 data->addr = *array;
715 array++;
716 }
717
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000718 data->id = -1ULL;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900719 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}