blob: 183aedd4db8383041a84b21ad981ec39cdfe5c4d [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
Thomas Gleixner3835bc02010-12-07 12:48:42 +000010static const char *event__name[] = {
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030011 [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",
Thomas Gleixner3835bc02010-12-07 12:48:42 +000025 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030026};
27
Thomas Gleixner3835bc02010-12-07 12:48:42 +000028const char *event__get_event_name(unsigned int id)
29{
30 if (id >= ARRAY_SIZE(event__name))
31 return "INVALID";
32 if (!event__name[id])
33 return "UNKNOWN";
34 return event__name[id];
35}
36
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020037static struct sample_data synth_sample = {
38 .pid = -1,
39 .tid = -1,
40 .time = -1,
41 .stream_id = -1,
42 .cpu = -1,
43 .period = 1,
44};
45
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020046static pid_t event__synthesize_comm(event_t *event, pid_t pid, int full,
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -020047 event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020048 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020049{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020050 char filename[PATH_MAX];
51 char bf[BUFSIZ];
52 FILE *fp;
53 size_t size = 0;
54 DIR *tasks;
55 struct dirent dirent, *next;
56 pid_t tgid = 0;
57
58 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
59
60 fp = fopen(filename, "r");
61 if (fp == NULL) {
62out_race:
63 /*
64 * We raced with a task exiting - just return:
65 */
66 pr_debug("couldn't open %s\n", filename);
67 return 0;
68 }
69
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070 memset(&event->comm, 0, sizeof(event->comm));
71
72 while (!event->comm.comm[0] || !event->comm.pid) {
73 if (fgets(bf, sizeof(bf), fp) == NULL) {
74 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
75 goto out;
76 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020077
78 if (memcmp(bf, "Name:", 5) == 0) {
79 char *name = bf + 5;
80 while (*name && isspace(*name))
81 ++name;
82 size = strlen(name) - 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020083 memcpy(event->comm.comm, name, size++);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020084 } else if (memcmp(bf, "Tgid:", 5) == 0) {
85 char *tgids = bf + 5;
86 while (*tgids && isspace(*tgids))
87 ++tgids;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020088 tgid = event->comm.pid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020089 }
90 }
91
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020092 event->comm.header.type = PERF_RECORD_COMM;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020093 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020094 memset(event->comm.comm + size, 0, session->id_hdr_size);
95 event->comm.header.size = (sizeof(event->comm) -
96 (sizeof(event->comm.comm) - size) +
97 session->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020098 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020099 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200100
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200101 process(event, &synth_sample, session);
102 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200103 }
104
105 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
106
107 tasks = opendir(filename);
108 if (tasks == NULL)
109 goto out_race;
110
111 while (!readdir_r(tasks, &dirent, &next) && next) {
112 char *end;
113 pid = strtol(dirent.d_name, &end, 10);
114 if (*end)
115 continue;
116
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200117 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200118
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200119 process(event, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200120 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200121
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200122 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200123out:
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200124 fclose(fp);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200125
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200126 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200127}
128
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200129static int event__synthesize_mmap_events(event_t *event, pid_t pid, pid_t tgid,
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200130 event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200131 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200132{
133 char filename[PATH_MAX];
134 FILE *fp;
135
136 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
137
138 fp = fopen(filename, "r");
139 if (fp == NULL) {
140 /*
141 * We raced with a task exiting - just return:
142 */
143 pr_debug("couldn't open %s\n", filename);
144 return -1;
145 }
146
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200147 event->header.type = PERF_RECORD_MMAP;
148 /*
149 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
150 */
151 event->header.misc = PERF_RECORD_MISC_USER;
152
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200153 while (1) {
154 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200155 int n;
156 size_t size;
157 if (fgets(bf, sizeof(bf), fp) == NULL)
158 break;
159
160 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200161 n = hex2u64(pbf, &event->mmap.start);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200162 if (n < 0)
163 continue;
164 pbf += n + 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200165 n = hex2u64(pbf, &event->mmap.len);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200166 if (n < 0)
167 continue;
168 pbf += n + 3;
169 if (*pbf == 'x') { /* vm_exec */
170 char *execname = strchr(bf, '/');
171
172 /* Catch VDSO */
173 if (execname == NULL)
174 execname = strstr(bf, "[vdso]");
175
176 if (execname == NULL)
177 continue;
178
Anton Blanchard4af8b352010-04-03 22:53:31 +1100179 pbf += 3;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200180 n = hex2u64(pbf, &event->mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100181
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200182 size = strlen(execname);
183 execname[size - 1] = '\0'; /* Remove \n */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200184 memcpy(event->mmap.filename, execname, size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200185 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200186 event->mmap.len -= event->mmap.start;
187 event->mmap.header.size = (sizeof(event->mmap) -
188 (sizeof(event->mmap.filename) - size));
189 memset(event->mmap.filename + size, 0, session->id_hdr_size);
190 event->mmap.header.size += session->id_hdr_size;
191 event->mmap.pid = tgid;
192 event->mmap.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200193
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200194 process(event, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200195 }
196 }
197
198 fclose(fp);
199 return 0;
200}
201
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200202int event__synthesize_modules(event__handler_t process,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800203 struct perf_session *session,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300204 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200205{
206 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300207 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200208 event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size);
209
210 if (event == NULL) {
211 pr_debug("Not enough memory synthesizing mmap event "
212 "for kernel modules\n");
213 return -1;
214 }
215
216 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200217
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800218 /*
219 * kernel uses 0 for user space maps, see kernel/perf_event.c
220 * __perf_event_mmap
221 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300222 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200223 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800224 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200225 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800226
227 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200228 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200229 size_t size;
230 struct map *pos = rb_entry(nd, struct map, rb_node);
231
232 if (pos->dso->kernel)
233 continue;
234
235 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200236 event->mmap.header.type = PERF_RECORD_MMAP;
237 event->mmap.header.size = (sizeof(event->mmap) -
238 (sizeof(event->mmap.filename) - size));
239 memset(event->mmap.filename + size, 0, session->id_hdr_size);
240 event->mmap.header.size += session->id_hdr_size;
241 event->mmap.start = pos->start;
242 event->mmap.len = pos->end - pos->start;
243 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200244
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200245 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200246 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200247 process(event, &synth_sample, session);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200248 }
249
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200250 free(event);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200251 return 0;
252}
253
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200254static int __event__synthesize_thread(event_t *comm_event, event_t *mmap_event,
255 pid_t pid, event__handler_t process,
256 struct perf_session *session)
257{
258 pid_t tgid = event__synthesize_comm(comm_event, pid, 1, process,
259 session);
260 if (tgid == -1)
261 return -1;
262 return event__synthesize_mmap_events(mmap_event, pid, tgid,
263 process, session);
264}
265
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200266int event__synthesize_thread(pid_t pid, event__handler_t process,
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200267 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200268{
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200269 event_t *comm_event, *mmap_event;
270 int err = -1;
271
272 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
273 if (comm_event == NULL)
274 goto out;
275
276 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
277 if (mmap_event == NULL)
278 goto out_free_comm;
279
280 err = __event__synthesize_thread(comm_event, mmap_event, pid,
281 process, session);
282 free(mmap_event);
283out_free_comm:
284 free(comm_event);
285out:
286 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200287}
288
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200289int event__synthesize_threads(event__handler_t process,
290 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200291{
292 DIR *proc;
293 struct dirent dirent, *next;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200294 event_t *comm_event, *mmap_event;
295 int err = -1;
296
297 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
298 if (comm_event == NULL)
299 goto out;
300
301 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
302 if (mmap_event == NULL)
303 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200304
305 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200306 if (proc == NULL)
307 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200308
309 while (!readdir_r(proc, &dirent, &next) && next) {
310 char *end;
311 pid_t pid = strtol(dirent.d_name, &end, 10);
312
313 if (*end) /* only interested in proper numerical dirents */
314 continue;
315
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200316 __event__synthesize_thread(comm_event, mmap_event, pid,
317 process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200318 }
319
320 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200321 err = 0;
322out_free_mmap:
323 free(mmap_event);
324out_free_comm:
325 free(comm_event);
326out:
327 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200328}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200329
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200330struct process_symbol_args {
331 const char *name;
332 u64 start;
333};
334
335static int find_symbol_cb(void *arg, const char *name, char type, u64 start)
336{
337 struct process_symbol_args *args = arg;
338
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200339 /*
340 * Must be a function or at least an alias, as in PARISC64, where "_text" is
341 * an 'A' to the same address as "_stext".
342 */
343 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
344 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200345 return 0;
346
347 args->start = start;
348 return 1;
349}
350
Arnaldo Carvalho de Melocf553112010-01-07 19:59:40 -0200351int event__synthesize_kernel_mmap(event__handler_t process,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200352 struct perf_session *session,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300353 struct machine *machine,
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200354 const char *symbol_name)
355{
356 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800357 const char *filename, *mmap_name;
358 char path[PATH_MAX];
359 char name_buff[PATH_MAX];
360 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200361 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200362 /*
363 * We should get this from /sys/kernel/sections/.text, but till that is
364 * available use this, and after it is use this as a fallback for older
365 * kernels.
366 */
367 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200368 event_t *event = zalloc(sizeof(event->mmap) + session->id_hdr_size);
369
370 if (event == NULL) {
371 pr_debug("Not enough memory synthesizing mmap event "
372 "for kernel modules\n");
373 return -1;
374 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200375
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300376 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300377 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800378 /*
379 * kernel uses PERF_RECORD_MISC_USER for user space maps,
380 * see kernel/perf_event.c __perf_event_mmap
381 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200382 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800383 filename = "/proc/kallsyms";
384 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200385 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300386 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800387 filename = (char *) symbol_conf.default_guest_kallsyms;
388 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300389 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800390 filename = path;
391 }
392 }
393
394 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200395 return -ENOENT;
396
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300397 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200398 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800399 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200400 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200401 event->mmap.header.type = PERF_RECORD_MMAP;
402 event->mmap.header.size = (sizeof(event->mmap) -
403 (sizeof(event->mmap.filename) - size) + session->id_hdr_size);
404 event->mmap.pgoff = args.start;
405 event->mmap.start = map->start;
406 event->mmap.len = map->end - event->mmap.start;
407 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200408
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200409 err = process(event, &synth_sample, session);
410 free(event);
411
412 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200413}
414
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300415static void thread__comm_adjust(struct thread *self, struct hists *hists)
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200416{
417 char *comm = self->comm;
418
419 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
420 (!symbol_conf.comm_list ||
421 strlist__has_entry(symbol_conf.comm_list, comm))) {
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300422 u16 slen = strlen(comm);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200423
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300424 if (hists__new_col_len(hists, HISTC_COMM, slen))
425 hists__set_col_len(hists, HISTC_THREAD, slen + 6);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200426 }
427}
428
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300429static int thread__set_comm_adjust(struct thread *self, const char *comm,
430 struct hists *hists)
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200431{
432 int ret = thread__set_comm(self, comm);
433
434 if (ret)
435 return ret;
436
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300437 thread__comm_adjust(self, hists);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200438
439 return 0;
440}
441
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200442int event__process_comm(event_t *self, struct sample_data *sample __used,
443 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200444{
Frederic Weisbecker13eb04fd2010-05-31 23:38:44 +0200445 struct thread *thread = perf_session__findnew(session, self->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200446
Frederic Weisbecker13eb04fd2010-05-31 23:38:44 +0200447 dump_printf(": %s:%d\n", self->comm.comm, self->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200448
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300449 if (thread == NULL || thread__set_comm_adjust(thread, self->comm.comm,
450 &session->hists)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200451 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
452 return -1;
453 }
454
455 return 0;
456}
457
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200458int event__process_lost(event_t *self, struct sample_data *sample __used,
459 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200460{
461 dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300462 session->hists.stats.total_lost += self->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200463 return 0;
464}
465
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800466static void event_set_kernel_mmap_len(struct map **maps, event_t *self)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200467{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800468 maps[MAP__FUNCTION]->start = self->mmap.start;
469 maps[MAP__FUNCTION]->end = self->mmap.start + self->mmap.len;
470 /*
471 * Be a bit paranoid here, some perf.data file came with
472 * a zero sized synthesized MMAP event for the kernel.
473 */
474 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100475 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800476}
477
478static int event__process_kernel_mmap(event_t *self,
479 struct perf_session *session)
480{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200481 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800482 char kmmap_prefix[PATH_MAX];
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300483 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800484 enum dso_kernel_type kernel_type;
485 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200486
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300487 machine = perf_session__findnew_machine(session, self->mmap.pid);
488 if (!machine) {
489 pr_err("Can't find id %d's machine\n", self->mmap.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800490 goto out_problem;
491 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200492
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300493 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300494 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800495 kernel_type = DSO_TYPE_KERNEL;
496 else
497 kernel_type = DSO_TYPE_GUEST_KERNEL;
498
499 is_kernel_mmap = memcmp(self->mmap.filename,
500 kmmap_prefix,
501 strlen(kmmap_prefix)) == 0;
502 if (self->mmap.filename[0] == '/' ||
503 (!is_kernel_mmap && self->mmap.filename[0] == '[')) {
504
505 char short_module_name[1024];
506 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200507
508 if (self->mmap.filename[0] == '/') {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800509 name = strrchr(self->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200510 if (name == NULL)
511 goto out_problem;
512
513 ++name; /* skip / */
514 dot = strrchr(name, '.');
515 if (dot == NULL)
516 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200517 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800518 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200519 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800520 } else
521 strcpy(short_module_name, self->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200522
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300523 map = machine__new_module(machine, self->mmap.start,
524 self->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800525 if (map == NULL)
526 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200527
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800528 name = strdup(short_module_name);
529 if (name == NULL)
530 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200531
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800532 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300533 map->dso->sname_alloc = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800534 map->end = map->start + self->mmap.len;
535 } else if (is_kernel_mmap) {
536 const char *symbol_name = (self->mmap.filename +
537 strlen(kmmap_prefix));
538 /*
539 * Should be there already, from the build-id table in
540 * the header.
541 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300542 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
543 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800544 if (kernel == NULL)
545 goto out_problem;
546
547 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300548 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800549 goto out_problem;
550
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300551 event_set_kernel_mmap_len(machine->vmlinux_maps, self);
552 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
553 symbol_name,
554 self->mmap.pgoff);
555 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200556 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800557 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200558 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300559 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
560 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200561 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800562 }
563 return 0;
564out_problem:
565 return -1;
566}
567
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200568int event__process_mmap(event_t *self, struct sample_data *sample __used,
569 struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800570{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300571 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800572 struct thread *thread;
573 struct map *map;
574 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
575 int ret = 0;
576
577 dump_printf(" %d/%d: [%#Lx(%#Lx) @ %#Lx]: %s\n",
578 self->mmap.pid, self->mmap.tid, self->mmap.start,
579 self->mmap.len, self->mmap.pgoff, self->mmap.filename);
580
581 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
582 cpumode == PERF_RECORD_MISC_KERNEL) {
583 ret = event__process_kernel_mmap(self, session);
584 if (ret < 0)
585 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200586 return 0;
587 }
588
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300589 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300590 if (machine == NULL)
591 goto out_problem;
592 thread = perf_session__findnew(session, self->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300593 if (thread == NULL)
594 goto out_problem;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300595 map = map__new(&machine->user_dsos, self->mmap.start,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800596 self->mmap.len, self->mmap.pgoff,
597 self->mmap.pid, self->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100598 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300599 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200600 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200601
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200602 thread__insert_map(thread, map);
603 return 0;
604
605out_problem:
606 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200607 return 0;
608}
609
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200610int event__process_task(event_t *self, struct sample_data *sample __used,
611 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200612{
Frederic Weisbeckerdd833d72010-06-01 00:43:07 +0200613 struct thread *thread = perf_session__findnew(session, self->fork.tid);
614 struct thread *parent = perf_session__findnew(session, self->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200615
616 dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
617 self->fork.ppid, self->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200618
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300619 if (self->header.type == PERF_RECORD_EXIT) {
620 perf_session__remove_thread(session, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200621 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300622 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200623
624 if (thread == NULL || parent == NULL ||
625 thread__fork(thread, parent) < 0) {
626 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
627 return -1;
628 }
629
630 return 0;
631}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200632
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200633int event__process(event_t *event, struct sample_data *sample,
634 struct perf_session *session)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530635{
636 switch (event->header.type) {
637 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200638 event__process_comm(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530639 break;
640 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200641 event__process_mmap(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530642 break;
643 case PERF_RECORD_FORK:
644 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200645 event__process_task(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530646 break;
647 default:
648 break;
649 }
650
651 return 0;
652}
653
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200654void thread__find_addr_map(struct thread *self,
655 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200657 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200658{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200659 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300660 struct machine *machine = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200661
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200662 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200663 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664 al->cpumode = cpumode;
665 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200666
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800667 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200668 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300669 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300670 if (machine == NULL) {
671 al->map = NULL;
672 return;
673 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300674 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800675 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200676 al->level = '.';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300677 machine = perf_session__find_host_machine(session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800678 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
679 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300680 machine = perf_session__find_machine(session, pid);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300681 if (machine == NULL) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800682 al->map = NULL;
683 return;
684 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300685 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800686 } else {
687 /*
688 * 'u' means guest os user space.
689 * TODO: We don't support guest user space. Might support late.
690 */
691 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
692 al->level = 'u';
693 else
694 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200695 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800696
697 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
698 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
699 !perf_guest)
700 al->filtered = true;
701 if ((cpumode == PERF_RECORD_MISC_USER ||
702 cpumode == PERF_RECORD_MISC_KERNEL) &&
703 !perf_host)
704 al->filtered = true;
705
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200706 return;
707 }
708try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200709 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200710 if (al->map == NULL) {
711 /*
712 * If this is outside of all known maps, and is a negative
713 * address, try to look it up in the kernel dso, as it might be
714 * a vsyscall or vdso (which executes in user-mode).
715 *
716 * XXX This is nasty, we should have a symbol list in the
717 * "[vdso]" dso, but for now lets use the old trick of looking
718 * in the whole kernel symbol list.
719 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800720 if ((long long)al->addr < 0 &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300721 cpumode == PERF_RECORD_MISC_KERNEL &&
722 machine && mg != &machine->kmaps) {
723 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200724 goto try_again;
725 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200726 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200727 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200728}
729
730void thread__find_addr_location(struct thread *self,
731 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800732 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200733 struct addr_location *al,
734 symbol_filter_t filter)
735{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800736 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200737 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200738 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200739 else
740 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200741}
742
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300743static void dso__calc_col_width(struct dso *self, struct hists *hists)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200744{
745 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
746 (!symbol_conf.dso_list ||
747 strlist__has_entry(symbol_conf.dso_list, self->name))) {
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300748 u16 slen = dso__name_len(self);
749 hists__new_col_len(hists, HISTC_DSO, slen);
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200750 }
751
752 self->slen_calculated = 1;
753}
754
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200755int event__preprocess_sample(const event_t *self, struct perf_session *session,
Arnaldo Carvalho de Melo41a37e22010-06-04 08:02:07 -0300756 struct addr_location *al, struct sample_data *data,
757 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200758{
759 u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200760 struct thread *thread = perf_session__findnew(session, self->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200761
762 if (thread == NULL)
763 return -1;
764
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200765 if (symbol_conf.comm_list &&
766 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
767 goto out_filtered;
768
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200769 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300770 /*
771 * Have we already created the kernel maps for the host machine?
772 *
773 * This should have happened earlier, when we processed the kernel MMAP
774 * events, but for older perf.data files there was no such thing, so do
775 * it now.
776 */
777 if (cpumode == PERF_RECORD_MISC_KERNEL &&
778 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
779 machine__create_kernel_maps(&session->host_machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200780
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300781 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800782 self->ip.pid, self->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200783 dump_printf(" ...... dso: %s\n",
784 al->map ? al->map->dso->long_name :
785 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300786 al->sym = NULL;
Arun Sharmaf60f3592010-06-04 11:27:10 -0300787 al->cpu = data->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200788
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300789 if (al->map) {
790 if (symbol_conf.dso_list &&
791 (!al->map || !al->map->dso ||
792 !(strlist__has_entry(symbol_conf.dso_list,
793 al->map->dso->short_name) ||
794 (al->map->dso->short_name != al->map->dso->long_name &&
795 strlist__has_entry(symbol_conf.dso_list,
796 al->map->dso->long_name)))))
797 goto out_filtered;
798 /*
799 * We have to do this here as we may have a dso with no symbol
800 * hit that has a name longer than the ones with symbols
801 * sampled.
802 */
803 if (!sort_dso.elide && !al->map->dso->slen_calculated)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300804 dso__calc_col_width(al->map->dso, &session->hists);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300805
806 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo3ceb0d42010-05-09 16:07:01 -0300807 } else {
808 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
809
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300810 if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width &&
Arnaldo Carvalho de Melo3ceb0d42010-05-09 16:07:01 -0300811 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
812 !symbol_conf.dso_list)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300813 hists__set_col_len(&session->hists, HISTC_DSO,
814 unresolved_col_width);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300815 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200816
817 if (symbol_conf.sym_list && al->sym &&
818 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
819 goto out_filtered;
820
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200821 return 0;
822
823out_filtered:
824 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200825 return 0;
826}
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900827
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200828static int event__parse_id_sample(const event_t *event,
829 struct perf_session *session,
830 struct sample_data *sample)
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900831{
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200832 const u64 *array;
833 u64 type;
834
835 sample->cpu = sample->pid = sample->tid = -1;
836 sample->stream_id = sample->id = sample->time = -1ULL;
837
838 if (!session->sample_id_all)
839 return 0;
840
841 array = event->sample.array;
842 array += ((event->header.size -
843 sizeof(event->header)) / sizeof(u64)) - 1;
844 type = session->sample_type;
845
846 if (type & PERF_SAMPLE_CPU) {
847 u32 *p = (u32 *)array;
848 sample->cpu = *p;
849 array--;
850 }
851
852 if (type & PERF_SAMPLE_STREAM_ID) {
853 sample->stream_id = *array;
854 array--;
855 }
856
857 if (type & PERF_SAMPLE_ID) {
858 sample->id = *array;
859 array--;
860 }
861
862 if (type & PERF_SAMPLE_TIME) {
863 sample->time = *array;
864 array--;
865 }
866
867 if (type & PERF_SAMPLE_TID) {
868 u32 *p = (u32 *)array;
869 sample->pid = p[0];
870 sample->tid = p[1];
871 }
872
873 return 0;
874}
875
876int event__parse_sample(const event_t *event, struct perf_session *session,
877 struct sample_data *data)
878{
879 const u64 *array;
880 u64 type;
881
882 if (event->header.type != PERF_RECORD_SAMPLE)
883 return event__parse_id_sample(event, session, data);
884
885 array = event->sample.array;
886 type = session->sample_type;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900887
888 if (type & PERF_SAMPLE_IP) {
889 data->ip = event->ip.ip;
890 array++;
891 }
892
893 if (type & PERF_SAMPLE_TID) {
894 u32 *p = (u32 *)array;
895 data->pid = p[0];
896 data->tid = p[1];
897 array++;
898 }
899
900 if (type & PERF_SAMPLE_TIME) {
901 data->time = *array;
902 array++;
903 }
904
905 if (type & PERF_SAMPLE_ADDR) {
906 data->addr = *array;
907 array++;
908 }
909
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000910 data->id = -1ULL;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900911 if (type & PERF_SAMPLE_ID) {
912 data->id = *array;
913 array++;
914 }
915
916 if (type & PERF_SAMPLE_STREAM_ID) {
917 data->stream_id = *array;
918 array++;
919 }
920
921 if (type & PERF_SAMPLE_CPU) {
922 u32 *p = (u32 *)array;
923 data->cpu = *p;
924 array++;
Stephane Eranian761844b2010-05-28 12:08:01 +0200925 } else
926 data->cpu = -1;
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900927
928 if (type & PERF_SAMPLE_PERIOD) {
929 data->period = *array;
930 array++;
931 }
932
933 if (type & PERF_SAMPLE_READ) {
934 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
935 return -1;
936 }
937
938 if (type & PERF_SAMPLE_CALLCHAIN) {
939 data->callchain = (struct ip_callchain *)array;
940 array += 1 + data->callchain->nr;
941 }
942
943 if (type & PERF_SAMPLE_RAW) {
944 u32 *p = (u32 *)array;
945 data->raw_size = *p;
946 p++;
947 data->raw_data = p;
948 }
949
950 return 0;
951}