blob: 34510f4419759681058429e7e1d2c63bf94d05f2 [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
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030010const char *event__name[] = {
11 [0] = "TOTAL",
12 [PERF_RECORD_MMAP] = "MMAP",
13 [PERF_RECORD_LOST] = "LOST",
14 [PERF_RECORD_COMM] = "COMM",
15 [PERF_RECORD_EXIT] = "EXIT",
16 [PERF_RECORD_THROTTLE] = "THROTTLE",
17 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
18 [PERF_RECORD_FORK] = "FORK",
19 [PERF_RECORD_READ] = "READ",
20 [PERF_RECORD_SAMPLE] = "SAMPLE",
21 [PERF_RECORD_HEADER_ATTR] = "ATTR",
22 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
23 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
24 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
25};
26
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020027static struct sample_data synth_sample = {
28 .pid = -1,
29 .tid = -1,
30 .time = -1,
31 .stream_id = -1,
32 .cpu = -1,
33 .period = 1,
34};
35
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020036static pid_t event__synthesize_comm(pid_t pid, int full,
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -020037 event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020038 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020039{
40 event_t ev;
41 char filename[PATH_MAX];
42 char bf[BUFSIZ];
43 FILE *fp;
44 size_t size = 0;
45 DIR *tasks;
46 struct dirent dirent, *next;
47 pid_t tgid = 0;
48
49 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
50
51 fp = fopen(filename, "r");
52 if (fp == NULL) {
53out_race:
54 /*
55 * We raced with a task exiting - just return:
56 */
57 pr_debug("couldn't open %s\n", filename);
58 return 0;
59 }
60
61 memset(&ev.comm, 0, sizeof(ev.comm));
62 while (!ev.comm.comm[0] || !ev.comm.pid) {
63 if (fgets(bf, sizeof(bf), fp) == NULL)
64 goto out_failure;
65
66 if (memcmp(bf, "Name:", 5) == 0) {
67 char *name = bf + 5;
68 while (*name && isspace(*name))
69 ++name;
70 size = strlen(name) - 1;
71 memcpy(ev.comm.comm, name, size++);
72 } else if (memcmp(bf, "Tgid:", 5) == 0) {
73 char *tgids = bf + 5;
74 while (*tgids && isspace(*tgids))
75 ++tgids;
76 tgid = ev.comm.pid = atoi(tgids);
77 }
78 }
79
80 ev.comm.header.type = PERF_RECORD_COMM;
81 size = ALIGN(size, sizeof(u64));
82 ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
83
84 if (!full) {
85 ev.comm.tid = pid;
86
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020087 process(&ev, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020088 goto out_fclose;
89 }
90
91 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
92
93 tasks = opendir(filename);
94 if (tasks == NULL)
95 goto out_race;
96
97 while (!readdir_r(tasks, &dirent, &next) && next) {
98 char *end;
99 pid = strtol(dirent.d_name, &end, 10);
100 if (*end)
101 continue;
102
103 ev.comm.tid = pid;
104
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200105 process(&ev, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200106 }
107 closedir(tasks);
108
109out_fclose:
110 fclose(fp);
111 return tgid;
112
113out_failure:
114 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
115 return -1;
116}
117
118static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200119 event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200120 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200121{
122 char filename[PATH_MAX];
123 FILE *fp;
124
125 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
126
127 fp = fopen(filename, "r");
128 if (fp == NULL) {
129 /*
130 * We raced with a task exiting - just return:
131 */
132 pr_debug("couldn't open %s\n", filename);
133 return -1;
134 }
135
136 while (1) {
137 char bf[BUFSIZ], *pbf = bf;
138 event_t ev = {
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200139 .header = {
140 .type = PERF_RECORD_MMAP,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800141 /*
142 * Just like the kernel, see __perf_event_mmap
143 * in kernel/perf_event.c
144 */
145 .misc = PERF_RECORD_MISC_USER,
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200146 },
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200147 };
148 int n;
149 size_t size;
150 if (fgets(bf, sizeof(bf), fp) == NULL)
151 break;
152
153 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
154 n = hex2u64(pbf, &ev.mmap.start);
155 if (n < 0)
156 continue;
157 pbf += n + 1;
158 n = hex2u64(pbf, &ev.mmap.len);
159 if (n < 0)
160 continue;
161 pbf += n + 3;
162 if (*pbf == 'x') { /* vm_exec */
163 char *execname = strchr(bf, '/');
164
165 /* Catch VDSO */
166 if (execname == NULL)
167 execname = strstr(bf, "[vdso]");
168
169 if (execname == NULL)
170 continue;
171
Anton Blanchard4af8b352010-04-03 22:53:31 +1100172 pbf += 3;
Dave Martinb5a63252010-08-03 12:48:35 +0100173 n = hex2u64(pbf, &ev.mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100174
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200175 size = strlen(execname);
176 execname[size - 1] = '\0'; /* Remove \n */
177 memcpy(ev.mmap.filename, execname, size);
178 size = ALIGN(size, sizeof(u64));
179 ev.mmap.len -= ev.mmap.start;
180 ev.mmap.header.size = (sizeof(ev.mmap) -
181 (sizeof(ev.mmap.filename) - size));
182 ev.mmap.pid = tgid;
183 ev.mmap.tid = pid;
184
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200185 process(&ev, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200186 }
187 }
188
189 fclose(fp);
190 return 0;
191}
192
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200193int event__synthesize_modules(event__handler_t process,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800194 struct perf_session *session,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300195 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200196{
197 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300198 struct map_groups *kmaps = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800199 u16 misc;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200200
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800201 /*
202 * kernel uses 0 for user space maps, see kernel/perf_event.c
203 * __perf_event_mmap
204 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300205 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800206 misc = PERF_RECORD_MISC_KERNEL;
207 else
208 misc = PERF_RECORD_MISC_GUEST_KERNEL;
209
210 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200211 nd; nd = rb_next(nd)) {
212 event_t ev;
213 size_t size;
214 struct map *pos = rb_entry(nd, struct map, rb_node);
215
216 if (pos->dso->kernel)
217 continue;
218
219 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
220 memset(&ev, 0, sizeof(ev));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800221 ev.mmap.header.misc = misc;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200222 ev.mmap.header.type = PERF_RECORD_MMAP;
223 ev.mmap.header.size = (sizeof(ev.mmap) -
224 (sizeof(ev.mmap.filename) - size));
225 ev.mmap.start = pos->start;
226 ev.mmap.len = pos->end - pos->start;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300227 ev.mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200228
229 memcpy(ev.mmap.filename, pos->dso->long_name,
230 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200231 process(&ev, &synth_sample, session);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200232 }
233
234 return 0;
235}
236
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200237int event__synthesize_thread(pid_t pid, event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200238 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200239{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200240 pid_t tgid = event__synthesize_comm(pid, 1, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200241 if (tgid == -1)
242 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200243 return event__synthesize_mmap_events(pid, tgid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200244}
245
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200246void event__synthesize_threads(event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200247 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200248{
249 DIR *proc;
250 struct dirent dirent, *next;
251
252 proc = opendir("/proc");
253
254 while (!readdir_r(proc, &dirent, &next) && next) {
255 char *end;
256 pid_t pid = strtol(dirent.d_name, &end, 10);
257
258 if (*end) /* only interested in proper numerical dirents */
259 continue;
260
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200261 event__synthesize_thread(pid, process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200262 }
263
264 closedir(proc);
265}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200266
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200267struct process_symbol_args {
268 const char *name;
269 u64 start;
270};
271
272static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
273{
274 struct process_symbol_args *args = arg;
275
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200276 /*
277 * Must be a function or at least an alias, as in PARISC64, where "_text" is
278 * an 'A' to the same address as "_stext".
279 */
280 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
281 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200282 return 0;
283
284 args->start = start;
285 return 1;
286}
287
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200288int event__synthesize_kernel_mmap(event__handler_t process,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200289 struct perf_session *session,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300290 struct machine *machine,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200291 const char *symbol_name)
292{
293 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800294 const char *filename, *mmap_name;
295 char path[PATH_MAX];
296 char name_buff[PATH_MAX];
297 struct map *map;
298
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200299 event_t ev = {
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200300 .header = {
301 .type = PERF_RECORD_MMAP,
Arnaldo Carvalho de Melo18c3daa2010-01-14 23:45:28 -0200302 },
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200303 };
304 /*
305 * We should get this from /sys/kernel/sections/.text, but till that is
306 * available use this, and after it is use this as a fallback for older
307 * kernels.
308 */
309 struct process_symbol_args args = { .name = symbol_name, };
310
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300311 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300312 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800313 /*
314 * kernel uses PERF_RECORD_MISC_USER for user space maps,
315 * see kernel/perf_event.c __perf_event_mmap
316 */
317 ev.header.misc = PERF_RECORD_MISC_KERNEL;
318 filename = "/proc/kallsyms";
319 } else {
320 ev.header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300321 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800322 filename = (char *) symbol_conf.default_guest_kallsyms;
323 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300324 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800325 filename = path;
326 }
327 }
328
329 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200330 return -ENOENT;
331
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300332 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200333 size = snprintf(ev.mmap.filename, sizeof(ev.mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800334 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200335 size = ALIGN(size, sizeof(u64));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800336 ev.mmap.header.size = (sizeof(ev.mmap) -
337 (sizeof(ev.mmap.filename) - size));
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200338 ev.mmap.pgoff = args.start;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800339 ev.mmap.start = map->start;
340 ev.mmap.len = map->end - ev.mmap.start;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300341 ev.mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200342
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200343 return process(&ev, &synth_sample, session);
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200344}
345
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300346static void thread__comm_adjust(struct thread *self, struct hists *hists)
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200347{
348 char *comm = self->comm;
349
350 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
351 (!symbol_conf.comm_list ||
352 strlist__has_entry(symbol_conf.comm_list, comm))) {
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300353 u16 slen = strlen(comm);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200354
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300355 if (hists__new_col_len(hists, HISTC_COMM, slen))
356 hists__set_col_len(hists, HISTC_THREAD, slen + 6);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200357 }
358}
359
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300360static int thread__set_comm_adjust(struct thread *self, const char *comm,
361 struct hists *hists)
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200362{
363 int ret = thread__set_comm(self, comm);
364
365 if (ret)
366 return ret;
367
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300368 thread__comm_adjust(self, hists);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200369
370 return 0;
371}
372
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200373int event__process_comm(event_t *self, struct sample_data *sample __used,
374 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200375{
Frederic Weisbecker13eb04fd2010-05-31 23:38:44 +0200376 struct thread *thread = perf_session__findnew(session, self->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200377
Frederic Weisbecker13eb04fd2010-05-31 23:38:44 +0200378 dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200379
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300380 if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm,
381 &session->hists)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200382 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
383 return -1;
384 }
385
386 return 0;
387}
388
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200389int event__process_lost(event_t *self, struct sample_data *sample __used,
390 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200391{
392 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300393 session->hists.stats.total_lost += self->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200394 return 0;
395}
396
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800397static void event_set_kernel_mmap_len(struct map **maps, event_t *self)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200398{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800399 maps[MAP__FUNCTION]->start = self->mmap.start;
400 maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
401 /*
402 * Be a bit paranoid here, some perf.data file came with
403 * a zero sized synthesized MMAP event for the kernel.
404 */
405 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100406 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800407}
408
409static int event__process_kernel_mmap(event_t *self,
410 struct perf_session *session)
411{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200412 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800413 char kmmap_prefix[PATH_MAX];
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300414 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800415 enum dso_kernel_type kernel_type;
416 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200417
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300418 machine = perf_session__findnew_machine(session, self->mmap.pid);
419 if (!machine) {
420 pr_err("Can't find id %d's machine\n", self->mmap.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800421 goto out_problem;
422 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200423
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300424 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300425 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800426 kernel_type = DSO_TYPE_KERNEL;
427 else
428 kernel_type = DSO_TYPE_GUEST_KERNEL;
429
430 is_kernel_mmap = memcmp(self->mmap.filename,
431 kmmap_prefix,
432 strlen(kmmap_prefix)) == 0;
433 if (self->mmap.filename[0] == '/' ||
434 (!is_kernel_mmap && self->mmap.filename[0] == '[')) {
435
436 char short_module_name[1024];
437 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200438
439 if (self->mmap.filename[0] == '/') {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800440 name = strrchr(self->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200441 if (name == NULL)
442 goto out_problem;
443
444 ++name; /* skip / */
445 dot = strrchr(name, '.');
446 if (dot == NULL)
447 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200448 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800449 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200450 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800451 } else
452 strcpy(short_module_name, self->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200453
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300454 map = machine__new_module(machine, self->mmap.start,
455 self->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800456 if (map == NULL)
457 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200458
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800459 name = strdup(short_module_name);
460 if (name == NULL)
461 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200462
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800463 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300464 map->dso->sname_alloc = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800465 map->end = map->start + self->mmap.len;
466 } else if (is_kernel_mmap) {
467 const char *symbol_name = (self->mmap.filename +
468 strlen(kmmap_prefix));
469 /*
470 * Should be there already, from the build-id table in
471 * the header.
472 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300473 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
474 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800475 if (kernel == NULL)
476 goto out_problem;
477
478 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300479 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800480 goto out_problem;
481
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300482 event_set_kernel_mmap_len(machine->vmlinux_maps, self);
483 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
484 symbol_name,
485 self->mmap.pgoff);
486 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200487 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800488 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200489 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300490 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
491 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200492 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800493 }
494 return 0;
495out_problem:
496 return -1;
497}
498
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200499int event__process_mmap(event_t *self, struct sample_data *sample __used,
500 struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800501{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300502 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800503 struct thread *thread;
504 struct map *map;
505 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
506 int ret = 0;
507
508 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
509 self->mmap.pid, self->mmap.tid, self->mmap.start,
510 self->mmap.len, self->mmap.pgoff, self->mmap.filename);
511
512 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
513 cpumode == PERF_RECORD_MISC_KERNEL) {
514 ret = event__process_kernel_mmap(self, session);
515 if (ret < 0)
516 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200517 return 0;
518 }
519
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300520 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300521 if (machine == NULL)
522 goto out_problem;
523 thread = perf_session__findnew(session, self->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300524 if (thread == NULL)
525 goto out_problem;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300526 map = map__new(&machine->user_dsos, self->mmap.start,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800527 self->mmap.len, self->mmap.pgoff,
528 self->mmap.pid, self->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100529 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300530 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200531 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200532
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200533 thread__insert_map(thread, map);
534 return 0;
535
536out_problem:
537 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200538 return 0;
539}
540
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200541int event__process_task(event_t *self, struct sample_data *sample __used,
542 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200543{
Frederic Weisbeckerdd833d72010-06-01 00:43:07 +0200544 struct thread *thread = perf_session__findnew(session, self->fork.tid);
545 struct thread *parent = perf_session__findnew(session, self->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200546
547 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
548 self->fork.ppid, self->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200549
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300550 if (self->header.type == PERF_RECORD_EXIT) {
551 perf_session__remove_thread(session, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200552 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300553 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200554
555 if (thread == NULL || parent == NULL ||
556 thread__fork(thread, parent) < 0) {
557 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
558 return -1;
559 }
560
561 return 0;
562}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200563
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200564int event__process(event_t *event, struct sample_data *sample,
565 struct perf_session *session)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530566{
567 switch (event->header.type) {
568 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200569 event__process_comm(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530570 break;
571 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200572 event__process_mmap(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530573 break;
574 case PERF_RECORD_FORK:
575 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200576 event__process_task(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530577 break;
578 default:
579 break;
580 }
581
582 return 0;
583}
584
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200585void thread__find_addr_map(struct thread *self,
586 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800587 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200588 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200589{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200590 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300591 struct machine *machine = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200592
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200593 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200594 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800595 al->cpumode = cpumode;
596 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200597
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800598 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200599 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300600 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300601 if (machine == NULL) {
602 al->map = NULL;
603 return;
604 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300605 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800606 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200607 al->level = '.';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300608 machine = perf_session__find_host_machine(session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800609 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
610 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300611 machine = perf_session__find_machine(session, pid);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300612 if (machine == NULL) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800613 al->map = NULL;
614 return;
615 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300616 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800617 } else {
618 /*
619 * 'u' means guest os user space.
620 * TODO: We don't support guest user space. Might support late.
621 */
622 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
623 al->level = 'u';
624 else
625 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200626 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800627
628 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
629 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
630 !perf_guest)
631 al->filtered = true;
632 if ((cpumode == PERF_RECORD_MISC_USER ||
633 cpumode == PERF_RECORD_MISC_KERNEL) &&
634 !perf_host)
635 al->filtered = true;
636
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200637 return;
638 }
639try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200640 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200641 if (al->map == NULL) {
642 /*
643 * If this is outside of all known maps, and is a negative
644 * address, try to look it up in the kernel dso, as it might be
645 * a vsyscall or vdso (which executes in user-mode).
646 *
647 * XXX This is nasty, we should have a symbol list in the
648 * "[vdso]" dso, but for now lets use the old trick of looking
649 * in the whole kernel symbol list.
650 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800651 if ((long long)al->addr < 0 &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300652 cpumode == PERF_RECORD_MISC_KERNEL &&
653 machine && mg != &machine->kmaps) {
654 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200655 goto try_again;
656 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200657 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200658 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200659}
660
661void thread__find_addr_location(struct thread *self,
662 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800663 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200664 struct addr_location *al,
665 symbol_filter_t filter)
666{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800667 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200668 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200669 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200670 else
671 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200672}
673
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300674static void dso__calc_col_width(struct dso *self, struct hists *hists)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200675{
676 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
677 (!symbol_conf.dso_list ||
678 strlist__has_entry(symbol_conf.dso_list, self->name))) {
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300679 u16 slen = dso__name_len(self);
680 hists__new_col_len(hists, HISTC_DSO, slen);
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200681 }
682
683 self->slen_calculated = 1;
684}
685
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200686int event__preprocess_sample(const event_t *self, struct perf_session *session,
Arnaldo Carvalho de Melo41a37e22010-06-04 08:02:07 -0300687 struct addr_location *al, struct sample_data *data,
688 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200689{
690 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200691 struct thread *thread = perf_session__findnew(session, self->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200692
693 if (thread == NULL)
694 return -1;
695
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200696 if (symbol_conf.comm_list &&
697 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
698 goto out_filtered;
699
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200700 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300701 /*
702 * Have we already created the kernel maps for the host machine?
703 *
704 * This should have happened earlier, when we processed the kernel MMAP
705 * events, but for older perf.data files there was no such thing, so do
706 * it now.
707 */
708 if (cpumode == PERF_RECORD_MISC_KERNEL &&
709 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
710 machine__create_kernel_maps(&session->host_machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200711
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300712 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800713 self->ip.pid, self->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200714 dump_printf(" ...... dso: %s\n",
715 al->map ? al->map->dso->long_name :
716 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300717 al->sym = NULL;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300718 al->cpu = data->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200719
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300720 if (al->map) {
721 if (symbol_conf.dso_list &&
722 (!al->map || !al->map->dso ||
723 !(strlist__has_entry(symbol_conf.dso_list,
724 al->map->dso->short_name) ||
725 (al->map->dso->short_name != al->map->dso->long_name &&
726 strlist__has_entry(symbol_conf.dso_list,
727 al->map->dso->long_name)))))
728 goto out_filtered;
729 /*
730 * We have to do this here as we may have a dso with no symbol
731 * hit that has a name longer than the ones with symbols
732 * sampled.
733 */
734 if (!sort_dso.elide && !al->map->dso->slen_calculated)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300735 dso__calc_col_width(al->map->dso, &session->hists);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300736
737 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo3ceb0d42010-05-09 16:07:01 -0300738 } else {
739 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
740
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300741 if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width &&
Arnaldo Carvalho de Melo3ceb0d42010-05-09 16:07:01 -0300742 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
743 !symbol_conf.dso_list)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300744 hists__set_col_len(&session->hists, HISTC_DSO,
745 unresolved_col_width);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300746 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200747
748 if (symbol_conf.sym_list && al->sym &&
749 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
750 goto out_filtered;
751
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200752 return 0;
753
754out_filtered:
755 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200756 return 0;
757}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900758
Arnaldo Carvalho de Melo41a37e22010-06-04 08:02:07 -0300759int event__parse_sample(const event_t *event, u64 type, struct sample_data *data)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900760{
Arnaldo Carvalho de Melo41a37e22010-06-04 08:02:07 -0300761 const u64 *array = event->sample.array;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900762
763 if (type & PERF_SAMPLE_IP) {
764 data->ip = event->ip.ip;
765 array++;
766 }
767
768 if (type & PERF_SAMPLE_TID) {
769 u32 *p = (u32 *)array;
770 data->pid = p[0];
771 data->tid = p[1];
772 array++;
773 }
774
775 if (type & PERF_SAMPLE_TIME) {
776 data->time = *array;
777 array++;
778 }
779
780 if (type & PERF_SAMPLE_ADDR) {
781 data->addr = *array;
782 array++;
783 }
784
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000785 data->id = -1ULL;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900786 if (type & PERF_SAMPLE_ID) {
787 data->id = *array;
788 array++;
789 }
790
791 if (type & PERF_SAMPLE_STREAM_ID) {
792 data->stream_id = *array;
793 array++;
794 }
795
796 if (type & PERF_SAMPLE_CPU) {
797 u32 *p = (u32 *)array;
798 data->cpu = *p;
799 array++;
Stephane Eranian761844b2010-05-28 12:08:01 +0200800 } else
801 data->cpu = -1;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900802
803 if (type & PERF_SAMPLE_PERIOD) {
804 data->period = *array;
805 array++;
806 }
807
808 if (type & PERF_SAMPLE_READ) {
809 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
810 return -1;
811 }
812
813 if (type & PERF_SAMPLE_CALLCHAIN) {
814 data->callchain = (struct ip_callchain *)array;
815 array += 1 + data->callchain->nr;
816 }
817
818 if (type & PERF_SAMPLE_RAW) {
819 u32 *p = (u32 *)array;
820 data->raw_size = *p;
821 p++;
822 data->raw_data = p;
823 }
824
825 return 0;
826}