blob: 2044324b755a51bc9b41536957dce8b35a11a6fd [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 Meloc410a332009-12-15 20:04:41 -02004#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02005#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02006#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02007#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -02008#include "thread_map.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02009
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020010static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020011 [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 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030026};
27
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020028const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc02010-12-07 12:48:42 +000029{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020030 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc02010-12-07 12:48:42 +000031 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020032 if (!perf_event__names[id])
Thomas Gleixner3835bc02010-12-07 12:48:42 +000033 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020034 return perf_event__names[id];
Thomas Gleixner3835bc02010-12-07 12:48:42 +000035}
36
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020037static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020038 .pid = -1,
39 .tid = -1,
40 .time = -1,
41 .stream_id = -1,
42 .cpu = -1,
43 .period = 1,
44};
45
David Ahernf5faf722011-12-22 11:30:02 -070046static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020047{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020048 char filename[PATH_MAX];
49 char bf[BUFSIZ];
50 FILE *fp;
51 size_t size = 0;
David Ahernf5faf722011-12-22 11:30:02 -070052 pid_t tgid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020053
54 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
55
56 fp = fopen(filename, "r");
57 if (fp == NULL) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020058 pr_debug("couldn't open %s\n", filename);
59 return 0;
60 }
61
David Ahernf5faf722011-12-22 11:30:02 -070062 while (!comm[0] || (tgid < 0)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020063 if (fgets(bf, sizeof(bf), fp) == NULL) {
David Ahernf5faf722011-12-22 11:30:02 -070064 pr_warning("couldn't get COMM and pgid, malformed %s\n",
65 filename);
66 break;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020067 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020068
69 if (memcmp(bf, "Name:", 5) == 0) {
70 char *name = bf + 5;
71 while (*name && isspace(*name))
72 ++name;
73 size = strlen(name) - 1;
David Ahernf5faf722011-12-22 11:30:02 -070074 if (size >= len)
75 size = len - 1;
76 memcpy(comm, name, size);
77
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020078 } else if (memcmp(bf, "Tgid:", 5) == 0) {
79 char *tgids = bf + 5;
80 while (*tgids && isspace(*tgids))
81 ++tgids;
David Ahernf5faf722011-12-22 11:30:02 -070082 tgid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020083 }
84 }
85
David Ahernf5faf722011-12-22 11:30:02 -070086 fclose(fp);
87
88 return tgid;
89}
90
91static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
92 union perf_event *event, pid_t pid,
93 int full,
94 perf_event__handler_t process,
95 struct machine *machine)
96{
97 char filename[PATH_MAX];
98 size_t size;
99 DIR *tasks;
100 struct dirent dirent, *next;
101 pid_t tgid;
102
103 memset(&event->comm, 0, sizeof(event->comm));
104
105 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
106 sizeof(event->comm.comm));
107 if (tgid < 0)
108 goto out;
109
110 event->comm.pid = tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200111 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700112
113 size = strlen(event->comm.comm) + 1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200114 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200115 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200116 event->comm.header.size = (sizeof(event->comm) -
117 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200118 machine->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200119 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200120 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200121
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200122 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200123 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200124 }
125
126 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
127
128 tasks = opendir(filename);
David Ahernf5faf722011-12-22 11:30:02 -0700129 if (tasks == NULL) {
130 pr_debug("couldn't open %s\n", filename);
131 return 0;
132 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200133
134 while (!readdir_r(tasks, &dirent, &next) && next) {
135 char *end;
136 pid = strtol(dirent.d_name, &end, 10);
137 if (*end)
138 continue;
139
David Ahernf5faf722011-12-22 11:30:02 -0700140 /* already have tgid; jut want to update the comm */
141 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
142 sizeof(event->comm.comm));
143
144 size = strlen(event->comm.comm) + 1;
145 size = ALIGN(size, sizeof(u64));
146 memset(event->comm.comm + size, 0, machine->id_hdr_size);
147 event->comm.header.size = (sizeof(event->comm) -
148 (sizeof(event->comm.comm) - size) +
149 machine->id_hdr_size);
150
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200151 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200152
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200153 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200154 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200155
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200156 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200157out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200158 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200159}
160
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200161static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200162 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200163 pid_t pid, pid_t tgid,
164 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200165 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200166{
167 char filename[PATH_MAX];
168 FILE *fp;
169
170 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
171
172 fp = fopen(filename, "r");
173 if (fp == NULL) {
174 /*
175 * We raced with a task exiting - just return:
176 */
177 pr_debug("couldn't open %s\n", filename);
178 return -1;
179 }
180
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200181 event->header.type = PERF_RECORD_MMAP;
182 /*
183 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
184 */
185 event->header.misc = PERF_RECORD_MISC_USER;
186
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200187 while (1) {
188 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200189 int n;
190 size_t size;
191 if (fgets(bf, sizeof(bf), fp) == NULL)
192 break;
193
194 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200195 n = hex2u64(pbf, &event->mmap.start);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200196 if (n < 0)
197 continue;
198 pbf += n + 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200199 n = hex2u64(pbf, &event->mmap.len);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200200 if (n < 0)
201 continue;
202 pbf += n + 3;
203 if (*pbf == 'x') { /* vm_exec */
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000204 char anonstr[] = "//anon\n";
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200205 char *execname = strchr(bf, '/');
206
207 /* Catch VDSO */
208 if (execname == NULL)
209 execname = strstr(bf, "[vdso]");
210
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000211 /* Catch anonymous mmaps */
212 if ((execname == NULL) && !strstr(bf, "["))
213 execname = anonstr;
214
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200215 if (execname == NULL)
216 continue;
217
Anton Blanchard4af8b352010-04-03 22:53:31 +1100218 pbf += 3;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200219 n = hex2u64(pbf, &event->mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100220
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200221 size = strlen(execname);
222 execname[size - 1] = '\0'; /* Remove \n */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200223 memcpy(event->mmap.filename, execname, size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200224 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200225 event->mmap.len -= event->mmap.start;
226 event->mmap.header.size = (sizeof(event->mmap) -
227 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200228 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
229 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200230 event->mmap.pid = tgid;
231 event->mmap.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200232
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200233 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200234 }
235 }
236
237 fclose(fp);
238 return 0;
239}
240
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200241int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200242 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200243 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200244{
245 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300246 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200247 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200248 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200249 if (event == NULL) {
250 pr_debug("Not enough memory synthesizing mmap event "
251 "for kernel modules\n");
252 return -1;
253 }
254
255 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200256
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800257 /*
258 * kernel uses 0 for user space maps, see kernel/perf_event.c
259 * __perf_event_mmap
260 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300261 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200262 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800263 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200264 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800265
266 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200267 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200268 size_t size;
269 struct map *pos = rb_entry(nd, struct map, rb_node);
270
271 if (pos->dso->kernel)
272 continue;
273
274 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200275 event->mmap.header.type = PERF_RECORD_MMAP;
276 event->mmap.header.size = (sizeof(event->mmap) -
277 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200278 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
279 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200280 event->mmap.start = pos->start;
281 event->mmap.len = pos->end - pos->start;
282 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200283
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200284 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200285 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200286 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200287 }
288
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200289 free(event);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200290 return 0;
291}
292
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200293static int __event__synthesize_thread(union perf_event *comm_event,
294 union perf_event *mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700295 pid_t pid, int full,
296 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200297 struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200298 struct machine *machine)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200299{
David Aherndefd8d32011-12-22 11:30:01 -0700300 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200301 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200302 if (tgid == -1)
303 return -1;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200304 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200305 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200306}
307
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200308int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200309 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200310 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200311 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200312{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200313 union perf_event *comm_event, *mmap_event;
David Aherndefd8d32011-12-22 11:30:01 -0700314 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200315
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200316 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200317 if (comm_event == NULL)
318 goto out;
319
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200320 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200321 if (mmap_event == NULL)
322 goto out_free_comm;
323
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200324 err = 0;
325 for (thread = 0; thread < threads->nr; ++thread) {
326 if (__event__synthesize_thread(comm_event, mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700327 threads->map[thread], 0,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200328 process, tool, machine)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200329 err = -1;
330 break;
331 }
David Aherndefd8d32011-12-22 11:30:01 -0700332
333 /*
334 * comm.pid is set to thread group id by
335 * perf_event__synthesize_comm
336 */
337 if ((int) comm_event->comm.pid != threads->map[thread]) {
338 bool need_leader = true;
339
340 /* is thread group leader in thread_map? */
341 for (j = 0; j < threads->nr; ++j) {
342 if ((int) comm_event->comm.pid == threads->map[j]) {
343 need_leader = false;
344 break;
345 }
346 }
347
348 /* if not, generate events for it */
349 if (need_leader &&
350 __event__synthesize_thread(comm_event,
351 mmap_event,
352 comm_event->comm.pid, 0,
353 process, tool, machine)) {
354 err = -1;
355 break;
356 }
357 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200358 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200359 free(mmap_event);
360out_free_comm:
361 free(comm_event);
362out:
363 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200364}
365
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200366int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200367 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200368 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200369{
370 DIR *proc;
371 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200372 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200373 int err = -1;
374
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200375 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200376 if (comm_event == NULL)
377 goto out;
378
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200379 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200380 if (mmap_event == NULL)
381 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200382
383 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200384 if (proc == NULL)
385 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200386
387 while (!readdir_r(proc, &dirent, &next) && next) {
388 char *end;
389 pid_t pid = strtol(dirent.d_name, &end, 10);
390
391 if (*end) /* only interested in proper numerical dirents */
392 continue;
393
David Aherndefd8d32011-12-22 11:30:01 -0700394 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200395 process, tool, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200396 }
397
398 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200399 err = 0;
400out_free_mmap:
401 free(mmap_event);
402out_free_comm:
403 free(comm_event);
404out:
405 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200406}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200407
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200408struct process_symbol_args {
409 const char *name;
410 u64 start;
411};
412
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200413static int find_symbol_cb(void *arg, const char *name, char type,
414 u64 start, u64 end __used)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200415{
416 struct process_symbol_args *args = arg;
417
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200418 /*
419 * Must be a function or at least an alias, as in PARISC64, where "_text" is
420 * an 'A' to the same address as "_stext".
421 */
422 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
423 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200424 return 0;
425
426 args->start = start;
427 return 1;
428}
429
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200430int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200431 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200432 struct machine *machine,
433 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200434{
435 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800436 const char *filename, *mmap_name;
437 char path[PATH_MAX];
438 char name_buff[PATH_MAX];
439 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200440 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200441 /*
442 * We should get this from /sys/kernel/sections/.text, but till that is
443 * available use this, and after it is use this as a fallback for older
444 * kernels.
445 */
446 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200447 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200448 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200449 if (event == NULL) {
450 pr_debug("Not enough memory synthesizing mmap event "
451 "for kernel modules\n");
452 return -1;
453 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200454
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300455 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300456 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800457 /*
458 * kernel uses PERF_RECORD_MISC_USER for user space maps,
459 * see kernel/perf_event.c __perf_event_mmap
460 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200461 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800462 filename = "/proc/kallsyms";
463 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200464 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300465 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800466 filename = (char *) symbol_conf.default_guest_kallsyms;
467 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300468 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800469 filename = path;
470 }
471 }
472
473 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200474 return -ENOENT;
475
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300476 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200477 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800478 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200479 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200480 event->mmap.header.type = PERF_RECORD_MMAP;
481 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200482 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200483 event->mmap.pgoff = args.start;
484 event->mmap.start = map->start;
485 event->mmap.len = map->end - event->mmap.start;
486 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200487
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200488 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200489 free(event);
490
491 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200492}
493
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200494size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
495{
496 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
497}
498
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200499int perf_event__process_comm(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200500 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200501 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200502 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200503{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200504 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200505
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200506 if (dump_trace)
507 perf_event__fprintf_comm(event, stdout);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200508
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -0300509 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200510 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
511 return -1;
512 }
513
514 return 0;
515}
516
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200517int perf_event__process_lost(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200518 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200519 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200520 struct machine *machine __used)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200521{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200522 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200523 event->lost.id, event->lost.lost);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200524 return 0;
525}
526
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200527static void perf_event__set_kernel_mmap_len(union perf_event *event,
528 struct map **maps)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200529{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200530 maps[MAP__FUNCTION]->start = event->mmap.start;
531 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800532 /*
533 * Be a bit paranoid here, some perf.data file came with
534 * a zero sized synthesized MMAP event for the kernel.
535 */
536 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100537 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800538}
539
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200540static int perf_event__process_kernel_mmap(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200541 union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200542 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800543{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200544 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800545 char kmmap_prefix[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800546 enum dso_kernel_type kernel_type;
547 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200548
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300549 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300550 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800551 kernel_type = DSO_TYPE_KERNEL;
552 else
553 kernel_type = DSO_TYPE_GUEST_KERNEL;
554
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200555 is_kernel_mmap = memcmp(event->mmap.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800556 kmmap_prefix,
Jiri Olsabf32c9e2012-02-05 18:11:05 +0100557 strlen(kmmap_prefix) - 1) == 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200558 if (event->mmap.filename[0] == '/' ||
559 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800560
561 char short_module_name[1024];
562 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200563
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200564 if (event->mmap.filename[0] == '/') {
565 name = strrchr(event->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200566 if (name == NULL)
567 goto out_problem;
568
569 ++name; /* skip / */
570 dot = strrchr(name, '.');
571 if (dot == NULL)
572 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200573 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800574 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200575 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800576 } else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200577 strcpy(short_module_name, event->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200578
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200579 map = machine__new_module(machine, event->mmap.start,
580 event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800581 if (map == NULL)
582 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200583
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800584 name = strdup(short_module_name);
585 if (name == NULL)
586 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200587
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800588 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300589 map->dso->sname_alloc = 1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200590 map->end = map->start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800591 } else if (is_kernel_mmap) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200592 const char *symbol_name = (event->mmap.filename +
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800593 strlen(kmmap_prefix));
594 /*
595 * Should be there already, from the build-id table in
596 * the header.
597 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300598 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
599 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800600 if (kernel == NULL)
601 goto out_problem;
602
603 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300604 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800605 goto out_problem;
606
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200607 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300608
609 /*
610 * Avoid using a zero address (kptr_restrict) for the ref reloc
611 * symbol. Effectively having zero here means that at record
612 * time /proc/sys/kernel/kptr_restrict was non zero.
613 */
614 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200615 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
616 symbol_name,
617 event->mmap.pgoff);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300618 }
619
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300620 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200621 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800622 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200623 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300624 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
625 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200626 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800627 }
628 return 0;
629out_problem:
630 return -1;
631}
632
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200633size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
634{
635 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
636 event->mmap.pid, event->mmap.tid, event->mmap.start,
637 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
638}
639
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200640int perf_event__process_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200641 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200642 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200643 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800644{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800645 struct thread *thread;
646 struct map *map;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200647 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800648 int ret = 0;
649
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200650 if (dump_trace)
651 perf_event__fprintf_mmap(event, stdout);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800652
653 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
654 cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200655 ret = perf_event__process_kernel_mmap(tool, event, machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656 if (ret < 0)
657 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200658 return 0;
659 }
660
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200661 thread = machine__findnew_thread(machine, event->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300662 if (thread == NULL)
663 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200664 map = map__new(&machine->user_dsos, event->mmap.start,
665 event->mmap.len, event->mmap.pgoff,
666 event->mmap.pid, event->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100667 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300668 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200669 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200670
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200671 thread__insert_map(thread, map);
672 return 0;
673
674out_problem:
675 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200676 return 0;
677}
678
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200679size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
680{
681 return fprintf(fp, "(%d:%d):(%d:%d)\n",
682 event->fork.pid, event->fork.tid,
683 event->fork.ppid, event->fork.ptid);
684}
685
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200686int perf_event__process_task(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200687 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200688 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200689 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200690{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200691 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
692 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200693
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200694 if (dump_trace)
695 perf_event__fprintf_task(event, stdout);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200696
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200697 if (event->header.type == PERF_RECORD_EXIT) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200698 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200699 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300700 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200701
702 if (thread == NULL || parent == NULL ||
703 thread__fork(thread, parent) < 0) {
704 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
705 return -1;
706 }
707
708 return 0;
709}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200710
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200711size_t perf_event__fprintf(union perf_event *event, FILE *fp)
712{
713 size_t ret = fprintf(fp, "PERF_RECORD_%s",
714 perf_event__name(event->header.type));
715
716 switch (event->header.type) {
717 case PERF_RECORD_COMM:
718 ret += perf_event__fprintf_comm(event, fp);
719 break;
720 case PERF_RECORD_FORK:
721 case PERF_RECORD_EXIT:
722 ret += perf_event__fprintf_task(event, fp);
723 break;
724 case PERF_RECORD_MMAP:
725 ret += perf_event__fprintf_mmap(event, fp);
726 break;
727 default:
728 ret += fprintf(fp, "\n");
729 }
730
731 return ret;
732}
733
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200734int perf_event__process(struct perf_tool *tool, union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200735 struct perf_sample *sample, struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530736{
737 switch (event->header.type) {
738 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200739 perf_event__process_comm(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530740 break;
741 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200742 perf_event__process_mmap(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530743 break;
744 case PERF_RECORD_FORK:
745 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200746 perf_event__process_task(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530747 break;
Arnaldo Carvalho de Meloef2bf6d2011-01-29 09:04:40 -0200748 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200749 perf_event__process_lost(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530750 default:
751 break;
752 }
753
754 return 0;
755}
756
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200757void thread__find_addr_map(struct thread *self,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200758 struct machine *machine, u8 cpumode,
759 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200760 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200761{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200762 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200763
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200764 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200765 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800766 al->cpumode = cpumode;
767 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200768
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200769 if (machine == NULL) {
770 al->map = NULL;
771 return;
772 }
773
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800774 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200775 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300776 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800777 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200778 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800779 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
780 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300781 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800782 } else {
783 /*
784 * 'u' means guest os user space.
785 * TODO: We don't support guest user space. Might support late.
786 */
787 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
788 al->level = 'u';
789 else
790 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200791 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800792
793 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
794 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
795 !perf_guest)
796 al->filtered = true;
797 if ((cpumode == PERF_RECORD_MISC_USER ||
798 cpumode == PERF_RECORD_MISC_KERNEL) &&
799 !perf_host)
800 al->filtered = true;
801
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200802 return;
803 }
804try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200805 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200806 if (al->map == NULL) {
807 /*
808 * If this is outside of all known maps, and is a negative
809 * address, try to look it up in the kernel dso, as it might be
810 * a vsyscall or vdso (which executes in user-mode).
811 *
812 * XXX This is nasty, we should have a symbol list in the
813 * "[vdso]" dso, but for now lets use the old trick of looking
814 * in the whole kernel symbol list.
815 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800816 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804f2011-03-24 00:36:56 -0400817 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300818 machine && mg != &machine->kmaps) {
819 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200820 goto try_again;
821 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200822 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200823 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200824}
825
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200826void thread__find_addr_location(struct thread *thread, struct machine *machine,
827 u8 cpumode, enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200828 struct addr_location *al,
829 symbol_filter_t filter)
830{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200831 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200832 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200833 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200834 else
835 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200836}
837
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200838int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200839 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200840 struct addr_location *al,
841 struct perf_sample *sample,
842 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200843{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200844 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200845 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200846
847 if (thread == NULL)
848 return -1;
849
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200850 if (symbol_conf.comm_list &&
851 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
852 goto out_filtered;
853
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200854 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300855 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200856 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300857 *
858 * This should have happened earlier, when we processed the kernel MMAP
859 * events, but for older perf.data files there was no such thing, so do
860 * it now.
861 */
862 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200863 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
864 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200865
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200866 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
867 event->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200868 dump_printf(" ...... dso: %s\n",
869 al->map ? al->map->dso->long_name :
870 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300871 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200872 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200873
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300874 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900875 struct dso *dso = al->map->dso;
876
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300877 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900878 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
879 dso->short_name) ||
880 (dso->short_name != dso->long_name &&
881 strlist__has_entry(symbol_conf.dso_list,
882 dso->long_name)))))
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300883 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300884
885 al->sym = map__find_symbol(al->map, al->addr, filter);
886 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200887
888 if (symbol_conf.sym_list && al->sym &&
889 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
890 goto out_filtered;
891
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200892 return 0;
893
894out_filtered:
895 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200896 return 0;
897}