blob: a5787260181a4bbcfd98084979e06b1ad1c81d8d [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
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020046static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020047 union perf_event *event, pid_t pid,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020048 int full, perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020049 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020050{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020051 char filename[PATH_MAX];
52 char bf[BUFSIZ];
53 FILE *fp;
54 size_t size = 0;
55 DIR *tasks;
56 struct dirent dirent, *next;
57 pid_t tgid = 0;
58
59 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
60
61 fp = fopen(filename, "r");
62 if (fp == NULL) {
63out_race:
64 /*
65 * We raced with a task exiting - just return:
66 */
67 pr_debug("couldn't open %s\n", filename);
68 return 0;
69 }
70
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020071 memset(&event->comm, 0, sizeof(event->comm));
72
73 while (!event->comm.comm[0] || !event->comm.pid) {
74 if (fgets(bf, sizeof(bf), fp) == NULL) {
75 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
76 goto out;
77 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020078
79 if (memcmp(bf, "Name:", 5) == 0) {
80 char *name = bf + 5;
81 while (*name && isspace(*name))
82 ++name;
83 size = strlen(name) - 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020084 memcpy(event->comm.comm, name, size++);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020085 } else if (memcmp(bf, "Tgid:", 5) == 0) {
86 char *tgids = bf + 5;
87 while (*tgids && isspace(*tgids))
88 ++tgids;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020089 tgid = event->comm.pid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020090 }
91 }
92
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020093 event->comm.header.type = PERF_RECORD_COMM;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020094 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020095 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020096 event->comm.header.size = (sizeof(event->comm) -
97 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020098 machine->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020099 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200100 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200101
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200102 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200103 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200104 }
105
106 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
107
108 tasks = opendir(filename);
109 if (tasks == NULL)
110 goto out_race;
111
112 while (!readdir_r(tasks, &dirent, &next) && next) {
113 char *end;
114 pid = strtol(dirent.d_name, &end, 10);
115 if (*end)
116 continue;
117
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200118 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200119
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200120 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200121 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200122
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200123 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200124out:
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200125 fclose(fp);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200126
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200127 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200128}
129
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200130static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200131 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200132 pid_t pid, pid_t tgid,
133 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200134 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200135{
136 char filename[PATH_MAX];
137 FILE *fp;
138
139 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
140
141 fp = fopen(filename, "r");
142 if (fp == NULL) {
143 /*
144 * We raced with a task exiting - just return:
145 */
146 pr_debug("couldn't open %s\n", filename);
147 return -1;
148 }
149
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200150 event->header.type = PERF_RECORD_MMAP;
151 /*
152 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
153 */
154 event->header.misc = PERF_RECORD_MISC_USER;
155
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200156 while (1) {
157 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200158 int n;
159 size_t size;
160 if (fgets(bf, sizeof(bf), fp) == NULL)
161 break;
162
163 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200164 n = hex2u64(pbf, &event->mmap.start);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200165 if (n < 0)
166 continue;
167 pbf += n + 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200168 n = hex2u64(pbf, &event->mmap.len);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200169 if (n < 0)
170 continue;
171 pbf += n + 3;
172 if (*pbf == 'x') { /* vm_exec */
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000173 char anonstr[] = "//anon\n";
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200174 char *execname = strchr(bf, '/');
175
176 /* Catch VDSO */
177 if (execname == NULL)
178 execname = strstr(bf, "[vdso]");
179
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000180 /* Catch anonymous mmaps */
181 if ((execname == NULL) && !strstr(bf, "["))
182 execname = anonstr;
183
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200184 if (execname == NULL)
185 continue;
186
Anton Blanchard4af8b352010-04-03 22:53:31 +1100187 pbf += 3;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200188 n = hex2u64(pbf, &event->mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100189
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200190 size = strlen(execname);
191 execname[size - 1] = '\0'; /* Remove \n */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200192 memcpy(event->mmap.filename, execname, size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200193 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200194 event->mmap.len -= event->mmap.start;
195 event->mmap.header.size = (sizeof(event->mmap) -
196 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200197 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
198 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200199 event->mmap.pid = tgid;
200 event->mmap.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200201
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200202 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200203 }
204 }
205
206 fclose(fp);
207 return 0;
208}
209
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200210int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200211 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200212 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200213{
214 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300215 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200216 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200217 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200218 if (event == NULL) {
219 pr_debug("Not enough memory synthesizing mmap event "
220 "for kernel modules\n");
221 return -1;
222 }
223
224 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200225
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800226 /*
227 * kernel uses 0 for user space maps, see kernel/perf_event.c
228 * __perf_event_mmap
229 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300230 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200231 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800232 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200233 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800234
235 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200236 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200237 size_t size;
238 struct map *pos = rb_entry(nd, struct map, rb_node);
239
240 if (pos->dso->kernel)
241 continue;
242
243 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200244 event->mmap.header.type = PERF_RECORD_MMAP;
245 event->mmap.header.size = (sizeof(event->mmap) -
246 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200247 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
248 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200249 event->mmap.start = pos->start;
250 event->mmap.len = pos->end - pos->start;
251 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200252
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200253 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200254 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200255 process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200256 }
257
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200258 free(event);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200259 return 0;
260}
261
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200262static int __event__synthesize_thread(union perf_event *comm_event,
263 union perf_event *mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700264 pid_t pid, int full,
265 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200266 struct perf_tool *tool,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200267 struct machine *machine)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200268{
David Aherndefd8d32011-12-22 11:30:01 -0700269 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200270 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200271 if (tgid == -1)
272 return -1;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200273 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200274 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200275}
276
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200277int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200278 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200279 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200280 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200281{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200282 union perf_event *comm_event, *mmap_event;
David Aherndefd8d32011-12-22 11:30:01 -0700283 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200284
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200285 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200286 if (comm_event == NULL)
287 goto out;
288
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200289 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200290 if (mmap_event == NULL)
291 goto out_free_comm;
292
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200293 err = 0;
294 for (thread = 0; thread < threads->nr; ++thread) {
295 if (__event__synthesize_thread(comm_event, mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700296 threads->map[thread], 0,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200297 process, tool, machine)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200298 err = -1;
299 break;
300 }
David Aherndefd8d32011-12-22 11:30:01 -0700301
302 /*
303 * comm.pid is set to thread group id by
304 * perf_event__synthesize_comm
305 */
306 if ((int) comm_event->comm.pid != threads->map[thread]) {
307 bool need_leader = true;
308
309 /* is thread group leader in thread_map? */
310 for (j = 0; j < threads->nr; ++j) {
311 if ((int) comm_event->comm.pid == threads->map[j]) {
312 need_leader = false;
313 break;
314 }
315 }
316
317 /* if not, generate events for it */
318 if (need_leader &&
319 __event__synthesize_thread(comm_event,
320 mmap_event,
321 comm_event->comm.pid, 0,
322 process, tool, machine)) {
323 err = -1;
324 break;
325 }
326 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200327 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200328 free(mmap_event);
329out_free_comm:
330 free(comm_event);
331out:
332 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200333}
334
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200335int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200336 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200337 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200338{
339 DIR *proc;
340 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200341 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200342 int err = -1;
343
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200344 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200345 if (comm_event == NULL)
346 goto out;
347
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200348 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200349 if (mmap_event == NULL)
350 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200351
352 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200353 if (proc == NULL)
354 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200355
356 while (!readdir_r(proc, &dirent, &next) && next) {
357 char *end;
358 pid_t pid = strtol(dirent.d_name, &end, 10);
359
360 if (*end) /* only interested in proper numerical dirents */
361 continue;
362
David Aherndefd8d32011-12-22 11:30:01 -0700363 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200364 process, tool, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200365 }
366
367 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200368 err = 0;
369out_free_mmap:
370 free(mmap_event);
371out_free_comm:
372 free(comm_event);
373out:
374 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200375}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200376
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200377struct process_symbol_args {
378 const char *name;
379 u64 start;
380};
381
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200382static int find_symbol_cb(void *arg, const char *name, char type,
383 u64 start, u64 end __used)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200384{
385 struct process_symbol_args *args = arg;
386
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200387 /*
388 * Must be a function or at least an alias, as in PARISC64, where "_text" is
389 * an 'A' to the same address as "_stext".
390 */
391 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
392 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200393 return 0;
394
395 args->start = start;
396 return 1;
397}
398
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200399int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200400 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200401 struct machine *machine,
402 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200403{
404 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800405 const char *filename, *mmap_name;
406 char path[PATH_MAX];
407 char name_buff[PATH_MAX];
408 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200409 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200410 /*
411 * We should get this from /sys/kernel/sections/.text, but till that is
412 * available use this, and after it is use this as a fallback for older
413 * kernels.
414 */
415 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200416 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200417 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200418 if (event == NULL) {
419 pr_debug("Not enough memory synthesizing mmap event "
420 "for kernel modules\n");
421 return -1;
422 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200423
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300424 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300425 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800426 /*
427 * kernel uses PERF_RECORD_MISC_USER for user space maps,
428 * see kernel/perf_event.c __perf_event_mmap
429 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200430 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800431 filename = "/proc/kallsyms";
432 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200433 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300434 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800435 filename = (char *) symbol_conf.default_guest_kallsyms;
436 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300437 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800438 filename = path;
439 }
440 }
441
442 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200443 return -ENOENT;
444
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300445 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200446 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800447 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200448 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200449 event->mmap.header.type = PERF_RECORD_MMAP;
450 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200451 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200452 event->mmap.pgoff = args.start;
453 event->mmap.start = map->start;
454 event->mmap.len = map->end - event->mmap.start;
455 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200456
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200457 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200458 free(event);
459
460 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200461}
462
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200463size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
464{
465 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
466}
467
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200468int perf_event__process_comm(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200469 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200470 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200471 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200472{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200473 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200474
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200475 if (dump_trace)
476 perf_event__fprintf_comm(event, stdout);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200477
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -0300478 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200479 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
480 return -1;
481 }
482
483 return 0;
484}
485
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200486int perf_event__process_lost(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200487 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200488 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200489 struct machine *machine __used)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200490{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200491 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200492 event->lost.id, event->lost.lost);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200493 return 0;
494}
495
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200496static void perf_event__set_kernel_mmap_len(union perf_event *event,
497 struct map **maps)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200498{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200499 maps[MAP__FUNCTION]->start = event->mmap.start;
500 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800501 /*
502 * Be a bit paranoid here, some perf.data file came with
503 * a zero sized synthesized MMAP event for the kernel.
504 */
505 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100506 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800507}
508
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200509static int perf_event__process_kernel_mmap(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200510 union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200511 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800512{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200513 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800514 char kmmap_prefix[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800515 enum dso_kernel_type kernel_type;
516 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200517
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300518 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300519 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800520 kernel_type = DSO_TYPE_KERNEL;
521 else
522 kernel_type = DSO_TYPE_GUEST_KERNEL;
523
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200524 is_kernel_mmap = memcmp(event->mmap.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800525 kmmap_prefix,
526 strlen(kmmap_prefix)) == 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200527 if (event->mmap.filename[0] == '/' ||
528 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800529
530 char short_module_name[1024];
531 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200532
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200533 if (event->mmap.filename[0] == '/') {
534 name = strrchr(event->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200535 if (name == NULL)
536 goto out_problem;
537
538 ++name; /* skip / */
539 dot = strrchr(name, '.');
540 if (dot == NULL)
541 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200542 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800543 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200544 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800545 } else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200546 strcpy(short_module_name, event->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200547
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200548 map = machine__new_module(machine, event->mmap.start,
549 event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800550 if (map == NULL)
551 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200552
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800553 name = strdup(short_module_name);
554 if (name == NULL)
555 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200556
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800557 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300558 map->dso->sname_alloc = 1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200559 map->end = map->start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800560 } else if (is_kernel_mmap) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200561 const char *symbol_name = (event->mmap.filename +
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800562 strlen(kmmap_prefix));
563 /*
564 * Should be there already, from the build-id table in
565 * the header.
566 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300567 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
568 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800569 if (kernel == NULL)
570 goto out_problem;
571
572 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300573 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800574 goto out_problem;
575
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200576 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300577
578 /*
579 * Avoid using a zero address (kptr_restrict) for the ref reloc
580 * symbol. Effectively having zero here means that at record
581 * time /proc/sys/kernel/kptr_restrict was non zero.
582 */
583 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200584 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
585 symbol_name,
586 event->mmap.pgoff);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300587 }
588
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300589 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200590 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800591 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200592 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300593 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
594 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200595 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800596 }
597 return 0;
598out_problem:
599 return -1;
600}
601
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200602size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
603{
604 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
605 event->mmap.pid, event->mmap.tid, event->mmap.start,
606 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
607}
608
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200609int perf_event__process_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200610 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200611 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200612 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800613{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800614 struct thread *thread;
615 struct map *map;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200616 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800617 int ret = 0;
618
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200619 if (dump_trace)
620 perf_event__fprintf_mmap(event, stdout);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800621
622 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
623 cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200624 ret = perf_event__process_kernel_mmap(tool, event, machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800625 if (ret < 0)
626 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200627 return 0;
628 }
629
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200630 thread = machine__findnew_thread(machine, event->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300631 if (thread == NULL)
632 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200633 map = map__new(&machine->user_dsos, event->mmap.start,
634 event->mmap.len, event->mmap.pgoff,
635 event->mmap.pid, event->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100636 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300637 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200638 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200639
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200640 thread__insert_map(thread, map);
641 return 0;
642
643out_problem:
644 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200645 return 0;
646}
647
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200648size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
649{
650 return fprintf(fp, "(%d:%d):(%d:%d)\n",
651 event->fork.pid, event->fork.tid,
652 event->fork.ppid, event->fork.ptid);
653}
654
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200655int perf_event__process_task(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200656 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200657 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200658 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200659{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200660 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
661 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200662
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200663 if (dump_trace)
664 perf_event__fprintf_task(event, stdout);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200665
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200666 if (event->header.type == PERF_RECORD_EXIT) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200667 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200668 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300669 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200670
671 if (thread == NULL || parent == NULL ||
672 thread__fork(thread, parent) < 0) {
673 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
674 return -1;
675 }
676
677 return 0;
678}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200679
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200680size_t perf_event__fprintf(union perf_event *event, FILE *fp)
681{
682 size_t ret = fprintf(fp, "PERF_RECORD_%s",
683 perf_event__name(event->header.type));
684
685 switch (event->header.type) {
686 case PERF_RECORD_COMM:
687 ret += perf_event__fprintf_comm(event, fp);
688 break;
689 case PERF_RECORD_FORK:
690 case PERF_RECORD_EXIT:
691 ret += perf_event__fprintf_task(event, fp);
692 break;
693 case PERF_RECORD_MMAP:
694 ret += perf_event__fprintf_mmap(event, fp);
695 break;
696 default:
697 ret += fprintf(fp, "\n");
698 }
699
700 return ret;
701}
702
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200703int perf_event__process(struct perf_tool *tool, union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200704 struct perf_sample *sample, struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530705{
706 switch (event->header.type) {
707 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200708 perf_event__process_comm(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530709 break;
710 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200711 perf_event__process_mmap(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530712 break;
713 case PERF_RECORD_FORK:
714 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200715 perf_event__process_task(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530716 break;
Arnaldo Carvalho de Meloef2bf6d2011-01-29 09:04:40 -0200717 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200718 perf_event__process_lost(tool, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530719 default:
720 break;
721 }
722
723 return 0;
724}
725
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200726void thread__find_addr_map(struct thread *self,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200727 struct machine *machine, u8 cpumode,
728 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200729 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200730{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200731 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200732
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200733 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200734 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800735 al->cpumode = cpumode;
736 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200737
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200738 if (machine == NULL) {
739 al->map = NULL;
740 return;
741 }
742
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800743 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200744 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300745 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800746 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200747 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800748 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
749 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300750 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800751 } else {
752 /*
753 * 'u' means guest os user space.
754 * TODO: We don't support guest user space. Might support late.
755 */
756 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
757 al->level = 'u';
758 else
759 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200760 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800761
762 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
763 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
764 !perf_guest)
765 al->filtered = true;
766 if ((cpumode == PERF_RECORD_MISC_USER ||
767 cpumode == PERF_RECORD_MISC_KERNEL) &&
768 !perf_host)
769 al->filtered = true;
770
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200771 return;
772 }
773try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200774 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200775 if (al->map == NULL) {
776 /*
777 * If this is outside of all known maps, and is a negative
778 * address, try to look it up in the kernel dso, as it might be
779 * a vsyscall or vdso (which executes in user-mode).
780 *
781 * XXX This is nasty, we should have a symbol list in the
782 * "[vdso]" dso, but for now lets use the old trick of looking
783 * in the whole kernel symbol list.
784 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800785 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400786 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300787 machine && mg != &machine->kmaps) {
788 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200789 goto try_again;
790 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200791 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200792 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200793}
794
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200795void thread__find_addr_location(struct thread *thread, struct machine *machine,
796 u8 cpumode, enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200797 struct addr_location *al,
798 symbol_filter_t filter)
799{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200800 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200801 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200802 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200803 else
804 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200805}
806
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200807int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200808 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200809 struct addr_location *al,
810 struct perf_sample *sample,
811 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200812{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200813 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200814 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200815
816 if (thread == NULL)
817 return -1;
818
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200819 if (symbol_conf.comm_list &&
820 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
821 goto out_filtered;
822
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200823 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300824 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200825 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300826 *
827 * This should have happened earlier, when we processed the kernel MMAP
828 * events, but for older perf.data files there was no such thing, so do
829 * it now.
830 */
831 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200832 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
833 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200834
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200835 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
836 event->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200837 dump_printf(" ...... dso: %s\n",
838 al->map ? al->map->dso->long_name :
839 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300840 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200841 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200842
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300843 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900844 struct dso *dso = al->map->dso;
845
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300846 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900847 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
848 dso->short_name) ||
849 (dso->short_name != dso->long_name &&
850 strlist__has_entry(symbol_conf.dso_list,
851 dso->long_name)))))
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300852 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300853
854 al->sym = map__find_symbol(al->map, al->addr, filter);
855 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200856
857 if (symbol_conf.sym_list && al->sym &&
858 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
859 goto out_filtered;
860
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200861 return 0;
862
863out_filtered:
864 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200865 return 0;
866}