blob: 252b72a5e59ef17fc477dc7cc87eb7b8dbac7699 [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 Melo7c940c12011-02-11 11:45:54 -02009#include "thread_map.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020010
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020011static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020012 [0] = "TOTAL",
13 [PERF_RECORD_MMAP] = "MMAP",
14 [PERF_RECORD_LOST] = "LOST",
15 [PERF_RECORD_COMM] = "COMM",
16 [PERF_RECORD_EXIT] = "EXIT",
17 [PERF_RECORD_THROTTLE] = "THROTTLE",
18 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
19 [PERF_RECORD_FORK] = "FORK",
20 [PERF_RECORD_READ] = "READ",
21 [PERF_RECORD_SAMPLE] = "SAMPLE",
22 [PERF_RECORD_HEADER_ATTR] = "ATTR",
23 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
24 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
25 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
26 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030027};
28
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020029const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc02010-12-07 12:48:42 +000030{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020031 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc02010-12-07 12:48:42 +000032 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020033 if (!perf_event__names[id])
Thomas Gleixner3835bc02010-12-07 12:48:42 +000034 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020035 return perf_event__names[id];
Thomas Gleixner3835bc02010-12-07 12:48:42 +000036}
37
Frederic Weisbeckera2854122011-05-21 19:33:04 +020038int perf_sample_size(u64 sample_type)
39{
40 u64 mask = sample_type & PERF_SAMPLE_MASK;
41 int size = 0;
42 int i;
43
44 for (i = 0; i < 64; i++) {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020045 if (mask & (1UL << i))
Frederic Weisbeckera2854122011-05-21 19:33:04 +020046 size++;
47 }
48
49 size *= sizeof(u64);
50
51 return size;
52}
53
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020054static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020055 .pid = -1,
56 .tid = -1,
57 .time = -1,
58 .stream_id = -1,
59 .cpu = -1,
60 .period = 1,
61};
62
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020063static pid_t perf_event__synthesize_comm(union perf_event *event, pid_t pid,
64 int full, perf_event__handler_t process,
65 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020066{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020067 char filename[PATH_MAX];
68 char bf[BUFSIZ];
69 FILE *fp;
70 size_t size = 0;
71 DIR *tasks;
72 struct dirent dirent, *next;
73 pid_t tgid = 0;
74
75 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
76
77 fp = fopen(filename, "r");
78 if (fp == NULL) {
79out_race:
80 /*
81 * We raced with a task exiting - just return:
82 */
83 pr_debug("couldn't open %s\n", filename);
84 return 0;
85 }
86
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020087 memset(&event->comm, 0, sizeof(event->comm));
88
89 while (!event->comm.comm[0] || !event->comm.pid) {
90 if (fgets(bf, sizeof(bf), fp) == NULL) {
91 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
92 goto out;
93 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020094
95 if (memcmp(bf, "Name:", 5) == 0) {
96 char *name = bf + 5;
97 while (*name && isspace(*name))
98 ++name;
99 size = strlen(name) - 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200100 memcpy(event->comm.comm, name, size++);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200101 } else if (memcmp(bf, "Tgid:", 5) == 0) {
102 char *tgids = bf + 5;
103 while (*tgids && isspace(*tgids))
104 ++tgids;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200105 tgid = event->comm.pid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200106 }
107 }
108
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200109 event->comm.header.type = PERF_RECORD_COMM;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200110 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200111 memset(event->comm.comm + size, 0, session->id_hdr_size);
112 event->comm.header.size = (sizeof(event->comm) -
113 (sizeof(event->comm.comm) - size) +
114 session->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200115 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200116 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200117
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200118 process(event, &synth_sample, session);
119 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200120 }
121
122 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
123
124 tasks = opendir(filename);
125 if (tasks == NULL)
126 goto out_race;
127
128 while (!readdir_r(tasks, &dirent, &next) && next) {
129 char *end;
130 pid = strtol(dirent.d_name, &end, 10);
131 if (*end)
132 continue;
133
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200134 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200135
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200136 process(event, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200137 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200138
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200139 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200140out:
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200141 fclose(fp);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200142
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200143 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200144}
145
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200146static int perf_event__synthesize_mmap_events(union perf_event *event,
147 pid_t pid, pid_t tgid,
148 perf_event__handler_t process,
149 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200150{
151 char filename[PATH_MAX];
152 FILE *fp;
153
154 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
155
156 fp = fopen(filename, "r");
157 if (fp == NULL) {
158 /*
159 * We raced with a task exiting - just return:
160 */
161 pr_debug("couldn't open %s\n", filename);
162 return -1;
163 }
164
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200165 event->header.type = PERF_RECORD_MMAP;
166 /*
167 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
168 */
169 event->header.misc = PERF_RECORD_MISC_USER;
170
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200171 while (1) {
172 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200173 int n;
174 size_t size;
175 if (fgets(bf, sizeof(bf), fp) == NULL)
176 break;
177
178 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200179 n = hex2u64(pbf, &event->mmap.start);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200180 if (n < 0)
181 continue;
182 pbf += n + 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200183 n = hex2u64(pbf, &event->mmap.len);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200184 if (n < 0)
185 continue;
186 pbf += n + 3;
187 if (*pbf == 'x') { /* vm_exec */
188 char *execname = strchr(bf, '/');
189
190 /* Catch VDSO */
191 if (execname == NULL)
192 execname = strstr(bf, "[vdso]");
193
194 if (execname == NULL)
195 continue;
196
Anton Blanchard4af8b352010-04-03 22:53:31 +1100197 pbf += 3;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200198 n = hex2u64(pbf, &event->mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100199
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200200 size = strlen(execname);
201 execname[size - 1] = '\0'; /* Remove \n */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200202 memcpy(event->mmap.filename, execname, size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200203 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200204 event->mmap.len -= event->mmap.start;
205 event->mmap.header.size = (sizeof(event->mmap) -
206 (sizeof(event->mmap.filename) - size));
207 memset(event->mmap.filename + size, 0, session->id_hdr_size);
208 event->mmap.header.size += session->id_hdr_size;
209 event->mmap.pid = tgid;
210 event->mmap.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200211
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200212 process(event, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200213 }
214 }
215
216 fclose(fp);
217 return 0;
218}
219
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200220int perf_event__synthesize_modules(perf_event__handler_t process,
221 struct perf_session *session,
222 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200223{
224 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300225 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200226 union perf_event *event = zalloc((sizeof(event->mmap) +
227 session->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200228 if (event == NULL) {
229 pr_debug("Not enough memory synthesizing mmap event "
230 "for kernel modules\n");
231 return -1;
232 }
233
234 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200235
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800236 /*
237 * kernel uses 0 for user space maps, see kernel/perf_event.c
238 * __perf_event_mmap
239 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300240 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200241 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800242 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200243 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800244
245 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200246 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200247 size_t size;
248 struct map *pos = rb_entry(nd, struct map, rb_node);
249
250 if (pos->dso->kernel)
251 continue;
252
253 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200254 event->mmap.header.type = PERF_RECORD_MMAP;
255 event->mmap.header.size = (sizeof(event->mmap) -
256 (sizeof(event->mmap.filename) - size));
257 memset(event->mmap.filename + size, 0, session->id_hdr_size);
258 event->mmap.header.size += session->id_hdr_size;
259 event->mmap.start = pos->start;
260 event->mmap.len = pos->end - pos->start;
261 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200262
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200263 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200264 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200265 process(event, &synth_sample, session);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200266 }
267
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200268 free(event);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200269 return 0;
270}
271
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200272static int __event__synthesize_thread(union perf_event *comm_event,
273 union perf_event *mmap_event,
274 pid_t pid, perf_event__handler_t process,
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200275 struct perf_session *session)
276{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200277 pid_t tgid = perf_event__synthesize_comm(comm_event, pid, 1, process,
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200278 session);
279 if (tgid == -1)
280 return -1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200281 return perf_event__synthesize_mmap_events(mmap_event, pid, tgid,
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200282 process, session);
283}
284
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200285int perf_event__synthesize_thread_map(struct thread_map *threads,
286 perf_event__handler_t process,
287 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200288{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200289 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200290 int err = -1, thread;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200291
292 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
293 if (comm_event == NULL)
294 goto out;
295
296 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
297 if (mmap_event == NULL)
298 goto out_free_comm;
299
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200300 err = 0;
301 for (thread = 0; thread < threads->nr; ++thread) {
302 if (__event__synthesize_thread(comm_event, mmap_event,
303 threads->map[thread],
304 process, session)) {
305 err = -1;
306 break;
307 }
308 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200309 free(mmap_event);
310out_free_comm:
311 free(comm_event);
312out:
313 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200314}
315
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200316int perf_event__synthesize_threads(perf_event__handler_t process,
317 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200318{
319 DIR *proc;
320 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200321 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200322 int err = -1;
323
324 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
325 if (comm_event == NULL)
326 goto out;
327
328 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
329 if (mmap_event == NULL)
330 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200331
332 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200333 if (proc == NULL)
334 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200335
336 while (!readdir_r(proc, &dirent, &next) && next) {
337 char *end;
338 pid_t pid = strtol(dirent.d_name, &end, 10);
339
340 if (*end) /* only interested in proper numerical dirents */
341 continue;
342
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200343 __event__synthesize_thread(comm_event, mmap_event, pid,
344 process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200345 }
346
347 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200348 err = 0;
349out_free_mmap:
350 free(mmap_event);
351out_free_comm:
352 free(comm_event);
353out:
354 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200355}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200356
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200357struct process_symbol_args {
358 const char *name;
359 u64 start;
360};
361
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200362static int find_symbol_cb(void *arg, const char *name, char type,
363 u64 start, u64 end __used)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200364{
365 struct process_symbol_args *args = arg;
366
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200367 /*
368 * Must be a function or at least an alias, as in PARISC64, where "_text" is
369 * an 'A' to the same address as "_stext".
370 */
371 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
372 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200373 return 0;
374
375 args->start = start;
376 return 1;
377}
378
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200379int perf_event__synthesize_kernel_mmap(perf_event__handler_t process,
380 struct perf_session *session,
381 struct machine *machine,
382 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200383{
384 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800385 const char *filename, *mmap_name;
386 char path[PATH_MAX];
387 char name_buff[PATH_MAX];
388 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200389 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200390 /*
391 * We should get this from /sys/kernel/sections/.text, but till that is
392 * available use this, and after it is use this as a fallback for older
393 * kernels.
394 */
395 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200396 union perf_event *event = zalloc((sizeof(event->mmap) +
397 session->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200398 if (event == NULL) {
399 pr_debug("Not enough memory synthesizing mmap event "
400 "for kernel modules\n");
401 return -1;
402 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200403
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300404 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300405 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800406 /*
407 * kernel uses PERF_RECORD_MISC_USER for user space maps,
408 * see kernel/perf_event.c __perf_event_mmap
409 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200410 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800411 filename = "/proc/kallsyms";
412 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200413 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300414 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800415 filename = (char *) symbol_conf.default_guest_kallsyms;
416 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300417 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800418 filename = path;
419 }
420 }
421
422 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200423 return -ENOENT;
424
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300425 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200426 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800427 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200428 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200429 event->mmap.header.type = PERF_RECORD_MMAP;
430 event->mmap.header.size = (sizeof(event->mmap) -
431 (sizeof(event->mmap.filename) - size) + session->id_hdr_size);
432 event->mmap.pgoff = args.start;
433 event->mmap.start = map->start;
434 event->mmap.len = map->end - event->mmap.start;
435 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200436
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200437 err = process(event, &synth_sample, session);
438 free(event);
439
440 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200441}
442
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200443int perf_event__process_comm(union perf_event *event,
444 struct perf_sample *sample __used,
445 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200446{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200447 struct thread *thread = perf_session__findnew(session, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200448
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200449 dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200450
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -0300451 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200452 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
453 return -1;
454 }
455
456 return 0;
457}
458
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200459int perf_event__process_lost(union perf_event *event,
460 struct perf_sample *sample __used,
461 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200462{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200463 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200464 event->lost.id, event->lost.lost);
465 session->hists.stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200466 return 0;
467}
468
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200469static void perf_event__set_kernel_mmap_len(union perf_event *event,
470 struct map **maps)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200471{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200472 maps[MAP__FUNCTION]->start = event->mmap.start;
473 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800474 /*
475 * Be a bit paranoid here, some perf.data file came with
476 * a zero sized synthesized MMAP event for the kernel.
477 */
478 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100479 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800480}
481
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200482static int perf_event__process_kernel_mmap(union perf_event *event,
483 struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800484{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200485 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800486 char kmmap_prefix[PATH_MAX];
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300487 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800488 enum dso_kernel_type kernel_type;
489 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200490
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200491 machine = perf_session__findnew_machine(session, event->mmap.pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300492 if (!machine) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200493 pr_err("Can't find id %d's machine\n", event->mmap.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800494 goto out_problem;
495 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200496
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300497 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300498 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800499 kernel_type = DSO_TYPE_KERNEL;
500 else
501 kernel_type = DSO_TYPE_GUEST_KERNEL;
502
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200503 is_kernel_mmap = memcmp(event->mmap.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800504 kmmap_prefix,
505 strlen(kmmap_prefix)) == 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200506 if (event->mmap.filename[0] == '/' ||
507 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800508
509 char short_module_name[1024];
510 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200511
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200512 if (event->mmap.filename[0] == '/') {
513 name = strrchr(event->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200514 if (name == NULL)
515 goto out_problem;
516
517 ++name; /* skip / */
518 dot = strrchr(name, '.');
519 if (dot == NULL)
520 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200521 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800522 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200523 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800524 } else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200525 strcpy(short_module_name, event->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200526
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200527 map = machine__new_module(machine, event->mmap.start,
528 event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800529 if (map == NULL)
530 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200531
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800532 name = strdup(short_module_name);
533 if (name == NULL)
534 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200535
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800536 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300537 map->dso->sname_alloc = 1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200538 map->end = map->start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800539 } else if (is_kernel_mmap) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200540 const char *symbol_name = (event->mmap.filename +
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800541 strlen(kmmap_prefix));
542 /*
543 * Should be there already, from the build-id table in
544 * the header.
545 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300546 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
547 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800548 if (kernel == NULL)
549 goto out_problem;
550
551 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300552 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800553 goto out_problem;
554
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200555 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300556 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
557 symbol_name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200558 event->mmap.pgoff);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300559 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200560 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800561 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200562 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300563 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
564 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200565 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800566 }
567 return 0;
568out_problem:
569 return -1;
570}
571
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200572int perf_event__process_mmap(union perf_event *event,
573 struct perf_sample *sample __used,
574 struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800575{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300576 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800577 struct thread *thread;
578 struct map *map;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200579 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800580 int ret = 0;
581
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200582 dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200583 event->mmap.pid, event->mmap.tid, event->mmap.start,
584 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800585
586 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
587 cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200588 ret = perf_event__process_kernel_mmap(event, session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800589 if (ret < 0)
590 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200591 return 0;
592 }
593
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300594 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300595 if (machine == NULL)
596 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200597 thread = perf_session__findnew(session, event->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300598 if (thread == NULL)
599 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200600 map = map__new(&machine->user_dsos, event->mmap.start,
601 event->mmap.len, event->mmap.pgoff,
602 event->mmap.pid, event->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100603 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300604 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200605 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200606
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200607 thread__insert_map(thread, map);
608 return 0;
609
610out_problem:
611 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200612 return 0;
613}
614
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200615int perf_event__process_task(union perf_event *event,
616 struct perf_sample *sample __used,
617 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200618{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200619 struct thread *thread = perf_session__findnew(session, event->fork.tid);
620 struct thread *parent = perf_session__findnew(session, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200621
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200622 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
623 event->fork.ppid, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200624
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200625 if (event->header.type == PERF_RECORD_EXIT) {
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300626 perf_session__remove_thread(session, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200627 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300628 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200629
630 if (thread == NULL || parent == NULL ||
631 thread__fork(thread, parent) < 0) {
632 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
633 return -1;
634 }
635
636 return 0;
637}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200638
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200639int perf_event__process(union perf_event *event, struct perf_sample *sample,
640 struct perf_session *session)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530641{
642 switch (event->header.type) {
643 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200644 perf_event__process_comm(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530645 break;
646 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200647 perf_event__process_mmap(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530648 break;
649 case PERF_RECORD_FORK:
650 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200651 perf_event__process_task(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530652 break;
Arnaldo Carvalho de Meloef2bf6d2011-01-29 09:04:40 -0200653 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200654 perf_event__process_lost(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530655 default:
656 break;
657 }
658
659 return 0;
660}
661
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200662void thread__find_addr_map(struct thread *self,
663 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200665 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200666{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200667 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300668 struct machine *machine = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200669
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200670 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200671 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800672 al->cpumode = cpumode;
673 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200674
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800675 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200676 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300677 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300678 if (machine == NULL) {
679 al->map = NULL;
680 return;
681 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300682 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800683 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200684 al->level = '.';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300685 machine = perf_session__find_host_machine(session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800686 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
687 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300688 machine = perf_session__find_machine(session, pid);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300689 if (machine == NULL) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800690 al->map = NULL;
691 return;
692 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300693 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800694 } else {
695 /*
696 * 'u' means guest os user space.
697 * TODO: We don't support guest user space. Might support late.
698 */
699 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
700 al->level = 'u';
701 else
702 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200703 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800704
705 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
706 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
707 !perf_guest)
708 al->filtered = true;
709 if ((cpumode == PERF_RECORD_MISC_USER ||
710 cpumode == PERF_RECORD_MISC_KERNEL) &&
711 !perf_host)
712 al->filtered = true;
713
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200714 return;
715 }
716try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200717 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200718 if (al->map == NULL) {
719 /*
720 * If this is outside of all known maps, and is a negative
721 * address, try to look it up in the kernel dso, as it might be
722 * a vsyscall or vdso (which executes in user-mode).
723 *
724 * XXX This is nasty, we should have a symbol list in the
725 * "[vdso]" dso, but for now lets use the old trick of looking
726 * in the whole kernel symbol list.
727 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800728 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400729 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300730 machine && mg != &machine->kmaps) {
731 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200732 goto try_again;
733 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200734 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200735 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200736}
737
738void thread__find_addr_location(struct thread *self,
739 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800740 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200741 struct addr_location *al,
742 symbol_filter_t filter)
743{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800744 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200745 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200746 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200747 else
748 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200749}
750
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200751int perf_event__preprocess_sample(const union perf_event *event,
752 struct perf_session *session,
753 struct addr_location *al,
754 struct perf_sample *sample,
755 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200756{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200757 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
758 struct thread *thread = perf_session__findnew(session, event->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200759
760 if (thread == NULL)
761 return -1;
762
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200763 if (symbol_conf.comm_list &&
764 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
765 goto out_filtered;
766
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200767 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300768 /*
769 * Have we already created the kernel maps for the host machine?
770 *
771 * This should have happened earlier, when we processed the kernel MMAP
772 * events, but for older perf.data files there was no such thing, so do
773 * it now.
774 */
775 if (cpumode == PERF_RECORD_MISC_KERNEL &&
776 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
777 machine__create_kernel_maps(&session->host_machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200778
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300779 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200780 event->ip.pid, event->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200781 dump_printf(" ...... dso: %s\n",
782 al->map ? al->map->dso->long_name :
783 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300784 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200785 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200786
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300787 if (al->map) {
788 if (symbol_conf.dso_list &&
789 (!al->map || !al->map->dso ||
790 !(strlist__has_entry(symbol_conf.dso_list,
791 al->map->dso->short_name) ||
792 (al->map->dso->short_name != al->map->dso->long_name &&
793 strlist__has_entry(symbol_conf.dso_list,
794 al->map->dso->long_name)))))
795 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300796
797 al->sym = map__find_symbol(al->map, al->addr, filter);
798 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200799
800 if (symbol_conf.sym_list && al->sym &&
801 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
802 goto out_filtered;
803
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200804 return 0;
805
806out_filtered:
807 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200808 return 0;
809}