blob: 731265f4ad19bd7558ee1d36975e6a52702ba5a3 [file] [log] [blame]
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02001#include <linux/types.h>
2#include "event.h"
3#include "debug.h"
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -02004#include "session.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02005#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02006#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02007#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -02008#include "thread.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02009
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020010static const char *perf_event__names[] = {
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030011 [0] = "TOTAL",
12 [PERF_RECORD_MMAP] = "MMAP",
13 [PERF_RECORD_LOST] = "LOST",
14 [PERF_RECORD_COMM] = "COMM",
15 [PERF_RECORD_EXIT] = "EXIT",
16 [PERF_RECORD_THROTTLE] = "THROTTLE",
17 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
18 [PERF_RECORD_FORK] = "FORK",
19 [PERF_RECORD_READ] = "READ",
20 [PERF_RECORD_SAMPLE] = "SAMPLE",
21 [PERF_RECORD_HEADER_ATTR] = "ATTR",
22 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
23 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
24 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
Thomas Gleixner3835bc02010-12-07 12:48:42 +000025 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030026};
27
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 Melo8115d602011-01-29 14:01:45 -020046static pid_t perf_event__synthesize_comm(union perf_event *event, pid_t pid,
47 int full, perf_event__handler_t process,
48 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020049{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020050 char filename[PATH_MAX];
51 char bf[BUFSIZ];
52 FILE *fp;
53 size_t size = 0;
54 DIR *tasks;
55 struct dirent dirent, *next;
56 pid_t tgid = 0;
57
58 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
59
60 fp = fopen(filename, "r");
61 if (fp == NULL) {
62out_race:
63 /*
64 * We raced with a task exiting - just return:
65 */
66 pr_debug("couldn't open %s\n", filename);
67 return 0;
68 }
69
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070 memset(&event->comm, 0, sizeof(event->comm));
71
72 while (!event->comm.comm[0] || !event->comm.pid) {
73 if (fgets(bf, sizeof(bf), fp) == NULL) {
74 pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
75 goto out;
76 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020077
78 if (memcmp(bf, "Name:", 5) == 0) {
79 char *name = bf + 5;
80 while (*name && isspace(*name))
81 ++name;
82 size = strlen(name) - 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020083 memcpy(event->comm.comm, name, size++);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020084 } else if (memcmp(bf, "Tgid:", 5) == 0) {
85 char *tgids = bf + 5;
86 while (*tgids && isspace(*tgids))
87 ++tgids;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020088 tgid = event->comm.pid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020089 }
90 }
91
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020092 event->comm.header.type = PERF_RECORD_COMM;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020093 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020094 memset(event->comm.comm + size, 0, session->id_hdr_size);
95 event->comm.header.size = (sizeof(event->comm) -
96 (sizeof(event->comm.comm) - size) +
97 session->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020098 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020099 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200100
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200101 process(event, &synth_sample, session);
102 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200103 }
104
105 snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
106
107 tasks = opendir(filename);
108 if (tasks == NULL)
109 goto out_race;
110
111 while (!readdir_r(tasks, &dirent, &next) && next) {
112 char *end;
113 pid = strtol(dirent.d_name, &end, 10);
114 if (*end)
115 continue;
116
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200117 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200118
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200119 process(event, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200120 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200121
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200122 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200123out:
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200124 fclose(fp);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200125
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200126 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200127}
128
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200129static int perf_event__synthesize_mmap_events(union perf_event *event,
130 pid_t pid, pid_t tgid,
131 perf_event__handler_t process,
132 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200133{
134 char filename[PATH_MAX];
135 FILE *fp;
136
137 snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
138
139 fp = fopen(filename, "r");
140 if (fp == NULL) {
141 /*
142 * We raced with a task exiting - just return:
143 */
144 pr_debug("couldn't open %s\n", filename);
145 return -1;
146 }
147
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200148 event->header.type = PERF_RECORD_MMAP;
149 /*
150 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
151 */
152 event->header.misc = PERF_RECORD_MISC_USER;
153
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200154 while (1) {
155 char bf[BUFSIZ], *pbf = bf;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200156 int n;
157 size_t size;
158 if (fgets(bf, sizeof(bf), fp) == NULL)
159 break;
160
161 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200162 n = hex2u64(pbf, &event->mmap.start);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200163 if (n < 0)
164 continue;
165 pbf += n + 1;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200166 n = hex2u64(pbf, &event->mmap.len);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200167 if (n < 0)
168 continue;
169 pbf += n + 3;
170 if (*pbf == 'x') { /* vm_exec */
171 char *execname = strchr(bf, '/');
172
173 /* Catch VDSO */
174 if (execname == NULL)
175 execname = strstr(bf, "[vdso]");
176
177 if (execname == NULL)
178 continue;
179
Anton Blanchard4af8b352010-04-03 22:53:31 +1100180 pbf += 3;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200181 n = hex2u64(pbf, &event->mmap.pgoff);
Anton Blanchard4af8b352010-04-03 22:53:31 +1100182
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200183 size = strlen(execname);
184 execname[size - 1] = '\0'; /* Remove \n */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200185 memcpy(event->mmap.filename, execname, size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200186 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200187 event->mmap.len -= event->mmap.start;
188 event->mmap.header.size = (sizeof(event->mmap) -
189 (sizeof(event->mmap.filename) - size));
190 memset(event->mmap.filename + size, 0, session->id_hdr_size);
191 event->mmap.header.size += session->id_hdr_size;
192 event->mmap.pid = tgid;
193 event->mmap.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200194
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200195 process(event, &synth_sample, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200196 }
197 }
198
199 fclose(fp);
200 return 0;
201}
202
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200203int perf_event__synthesize_modules(perf_event__handler_t process,
204 struct perf_session *session,
205 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200206{
207 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300208 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200209 union perf_event *event = zalloc((sizeof(event->mmap) +
210 session->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200211 if (event == NULL) {
212 pr_debug("Not enough memory synthesizing mmap event "
213 "for kernel modules\n");
214 return -1;
215 }
216
217 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200218
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800219 /*
220 * kernel uses 0 for user space maps, see kernel/perf_event.c
221 * __perf_event_mmap
222 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300223 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200224 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800225 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200226 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800227
228 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200229 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200230 size_t size;
231 struct map *pos = rb_entry(nd, struct map, rb_node);
232
233 if (pos->dso->kernel)
234 continue;
235
236 size = ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200237 event->mmap.header.type = PERF_RECORD_MMAP;
238 event->mmap.header.size = (sizeof(event->mmap) -
239 (sizeof(event->mmap.filename) - size));
240 memset(event->mmap.filename + size, 0, session->id_hdr_size);
241 event->mmap.header.size += session->id_hdr_size;
242 event->mmap.start = pos->start;
243 event->mmap.len = pos->end - pos->start;
244 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200245
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200246 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200247 pos->dso->long_name_len + 1);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200248 process(event, &synth_sample, session);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200249 }
250
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200251 free(event);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200252 return 0;
253}
254
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200255static int __event__synthesize_thread(union perf_event *comm_event,
256 union perf_event *mmap_event,
257 pid_t pid, perf_event__handler_t process,
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200258 struct perf_session *session)
259{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200260 pid_t tgid = perf_event__synthesize_comm(comm_event, pid, 1, process,
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200261 session);
262 if (tgid == -1)
263 return -1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200264 return perf_event__synthesize_mmap_events(mmap_event, pid, tgid,
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200265 process, session);
266}
267
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200268int perf_event__synthesize_thread(pid_t pid, perf_event__handler_t process,
269 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200270{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200271 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200272 int err = -1;
273
274 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
275 if (comm_event == NULL)
276 goto out;
277
278 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
279 if (mmap_event == NULL)
280 goto out_free_comm;
281
282 err = __event__synthesize_thread(comm_event, mmap_event, pid,
283 process, session);
284 free(mmap_event);
285out_free_comm:
286 free(comm_event);
287out:
288 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200289}
290
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200291int perf_event__synthesize_threads(perf_event__handler_t process,
292 struct perf_session *session)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200293{
294 DIR *proc;
295 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200296 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200297 int err = -1;
298
299 comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
300 if (comm_event == NULL)
301 goto out;
302
303 mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
304 if (mmap_event == NULL)
305 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200306
307 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200308 if (proc == NULL)
309 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200310
311 while (!readdir_r(proc, &dirent, &next) && next) {
312 char *end;
313 pid_t pid = strtol(dirent.d_name, &end, 10);
314
315 if (*end) /* only interested in proper numerical dirents */
316 continue;
317
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200318 __event__synthesize_thread(comm_event, mmap_event, pid,
319 process, session);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200320 }
321
322 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200323 err = 0;
324out_free_mmap:
325 free(mmap_event);
326out_free_comm:
327 free(comm_event);
328out:
329 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200330}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200331
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200332struct process_symbol_args {
333 const char *name;
334 u64 start;
335};
336
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200337static int find_symbol_cb(void *arg, const char *name, char type,
338 u64 start, u64 end __used)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200339{
340 struct process_symbol_args *args = arg;
341
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200342 /*
343 * Must be a function or at least an alias, as in PARISC64, where "_text" is
344 * an 'A' to the same address as "_stext".
345 */
346 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
347 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200348 return 0;
349
350 args->start = start;
351 return 1;
352}
353
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200354int perf_event__synthesize_kernel_mmap(perf_event__handler_t process,
355 struct perf_session *session,
356 struct machine *machine,
357 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200358{
359 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800360 const char *filename, *mmap_name;
361 char path[PATH_MAX];
362 char name_buff[PATH_MAX];
363 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200364 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200365 /*
366 * We should get this from /sys/kernel/sections/.text, but till that is
367 * available use this, and after it is use this as a fallback for older
368 * kernels.
369 */
370 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200371 union perf_event *event = zalloc((sizeof(event->mmap) +
372 session->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200373 if (event == NULL) {
374 pr_debug("Not enough memory synthesizing mmap event "
375 "for kernel modules\n");
376 return -1;
377 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200378
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300379 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300380 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800381 /*
382 * kernel uses PERF_RECORD_MISC_USER for user space maps,
383 * see kernel/perf_event.c __perf_event_mmap
384 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200385 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800386 filename = "/proc/kallsyms";
387 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200388 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300389 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800390 filename = (char *) symbol_conf.default_guest_kallsyms;
391 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300392 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800393 filename = path;
394 }
395 }
396
397 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200398 return -ENOENT;
399
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300400 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200401 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800402 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200403 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200404 event->mmap.header.type = PERF_RECORD_MMAP;
405 event->mmap.header.size = (sizeof(event->mmap) -
406 (sizeof(event->mmap.filename) - size) + session->id_hdr_size);
407 event->mmap.pgoff = args.start;
408 event->mmap.start = map->start;
409 event->mmap.len = map->end - event->mmap.start;
410 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200411
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200412 err = process(event, &synth_sample, session);
413 free(event);
414
415 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200416}
417
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300418static void thread__comm_adjust(struct thread *self, struct hists *hists)
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200419{
420 char *comm = self->comm;
421
422 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
423 (!symbol_conf.comm_list ||
424 strlist__has_entry(symbol_conf.comm_list, comm))) {
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300425 u16 slen = strlen(comm);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200426
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300427 if (hists__new_col_len(hists, HISTC_COMM, slen))
428 hists__set_col_len(hists, HISTC_THREAD, slen + 6);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200429 }
430}
431
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300432static int thread__set_comm_adjust(struct thread *self, const char *comm,
433 struct hists *hists)
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200434{
435 int ret = thread__set_comm(self, comm);
436
437 if (ret)
438 return ret;
439
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300440 thread__comm_adjust(self, hists);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200441
442 return 0;
443}
444
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200445int perf_event__process_comm(union perf_event *event,
446 struct perf_sample *sample __used,
447 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200448{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200449 struct thread *thread = perf_session__findnew(session, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200450
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200451 dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200452
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200453 if (thread == NULL || thread__set_comm_adjust(thread, event->comm.comm,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300454 &session->hists)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200455 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
456 return -1;
457 }
458
459 return 0;
460}
461
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200462int perf_event__process_lost(union perf_event *event,
463 struct perf_sample *sample __used,
464 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200465{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200466 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200467 event->lost.id, event->lost.lost);
468 session->hists.stats.total_lost += event->lost.lost;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200469 return 0;
470}
471
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200472static void perf_event__set_kernel_mmap_len(union perf_event *event,
473 struct map **maps)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200474{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200475 maps[MAP__FUNCTION]->start = event->mmap.start;
476 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800477 /*
478 * Be a bit paranoid here, some perf.data file came with
479 * a zero sized synthesized MMAP event for the kernel.
480 */
481 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100482 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800483}
484
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200485static int perf_event__process_kernel_mmap(union perf_event *event,
486 struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800487{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200488 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800489 char kmmap_prefix[PATH_MAX];
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300490 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800491 enum dso_kernel_type kernel_type;
492 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200493
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200494 machine = perf_session__findnew_machine(session, event->mmap.pid);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300495 if (!machine) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200496 pr_err("Can't find id %d's machine\n", event->mmap.pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800497 goto out_problem;
498 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200499
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300500 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300501 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800502 kernel_type = DSO_TYPE_KERNEL;
503 else
504 kernel_type = DSO_TYPE_GUEST_KERNEL;
505
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200506 is_kernel_mmap = memcmp(event->mmap.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800507 kmmap_prefix,
508 strlen(kmmap_prefix)) == 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200509 if (event->mmap.filename[0] == '/' ||
510 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800511
512 char short_module_name[1024];
513 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200514
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200515 if (event->mmap.filename[0] == '/') {
516 name = strrchr(event->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200517 if (name == NULL)
518 goto out_problem;
519
520 ++name; /* skip / */
521 dot = strrchr(name, '.');
522 if (dot == NULL)
523 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200524 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800525 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200526 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800527 } else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200528 strcpy(short_module_name, event->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200529
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200530 map = machine__new_module(machine, event->mmap.start,
531 event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800532 if (map == NULL)
533 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200534
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800535 name = strdup(short_module_name);
536 if (name == NULL)
537 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200538
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800539 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300540 map->dso->sname_alloc = 1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200541 map->end = map->start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800542 } else if (is_kernel_mmap) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200543 const char *symbol_name = (event->mmap.filename +
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800544 strlen(kmmap_prefix));
545 /*
546 * Should be there already, from the build-id table in
547 * the header.
548 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300549 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
550 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800551 if (kernel == NULL)
552 goto out_problem;
553
554 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300555 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800556 goto out_problem;
557
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200558 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300559 perf_session__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
560 symbol_name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200561 event->mmap.pgoff);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300562 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200563 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800564 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200565 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300566 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
567 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200568 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800569 }
570 return 0;
571out_problem:
572 return -1;
573}
574
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200575int perf_event__process_mmap(union perf_event *event,
576 struct perf_sample *sample __used,
577 struct perf_session *session)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800578{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300579 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800580 struct thread *thread;
581 struct map *map;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200582 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800583 int ret = 0;
584
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200585 dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200586 event->mmap.pid, event->mmap.tid, event->mmap.start,
587 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800588
589 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
590 cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200591 ret = perf_event__process_kernel_mmap(event, session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800592 if (ret < 0)
593 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200594 return 0;
595 }
596
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300597 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300598 if (machine == NULL)
599 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200600 thread = perf_session__findnew(session, event->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300601 if (thread == NULL)
602 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200603 map = map__new(&machine->user_dsos, event->mmap.start,
604 event->mmap.len, event->mmap.pgoff,
605 event->mmap.pid, event->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100606 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300607 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200608 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200609
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200610 thread__insert_map(thread, map);
611 return 0;
612
613out_problem:
614 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200615 return 0;
616}
617
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200618int perf_event__process_task(union perf_event *event,
619 struct perf_sample *sample __used,
620 struct perf_session *session)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200621{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200622 struct thread *thread = perf_session__findnew(session, event->fork.tid);
623 struct thread *parent = perf_session__findnew(session, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200624
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200625 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
626 event->fork.ppid, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200627
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200628 if (event->header.type == PERF_RECORD_EXIT) {
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300629 perf_session__remove_thread(session, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200630 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300631 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200632
633 if (thread == NULL || parent == NULL ||
634 thread__fork(thread, parent) < 0) {
635 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
636 return -1;
637 }
638
639 return 0;
640}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200641
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200642int perf_event__process(union perf_event *event, struct perf_sample *sample,
643 struct perf_session *session)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530644{
645 switch (event->header.type) {
646 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200647 perf_event__process_comm(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530648 break;
649 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200650 perf_event__process_mmap(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530651 break;
652 case PERF_RECORD_FORK:
653 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200654 perf_event__process_task(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530655 break;
Arnaldo Carvalho de Meloef2bf6d2011-01-29 09:04:40 -0200656 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200657 perf_event__process_lost(event, sample, session);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530658 default:
659 break;
660 }
661
662 return 0;
663}
664
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200665void thread__find_addr_map(struct thread *self,
666 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800667 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200668 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200669{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200670 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300671 struct machine *machine = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200672
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200673 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200674 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800675 al->cpumode = cpumode;
676 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200677
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800678 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200679 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300680 machine = perf_session__find_host_machine(session);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300681 if (machine == NULL) {
682 al->map = NULL;
683 return;
684 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300685 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800686 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200687 al->level = '.';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300688 machine = perf_session__find_host_machine(session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800689 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
690 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300691 machine = perf_session__find_machine(session, pid);
Arnaldo Carvalho de Melo4cc49452010-05-09 21:14:07 -0300692 if (machine == NULL) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800693 al->map = NULL;
694 return;
695 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300696 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800697 } else {
698 /*
699 * 'u' means guest os user space.
700 * TODO: We don't support guest user space. Might support late.
701 */
702 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
703 al->level = 'u';
704 else
705 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200706 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800707
708 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
709 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
710 !perf_guest)
711 al->filtered = true;
712 if ((cpumode == PERF_RECORD_MISC_USER ||
713 cpumode == PERF_RECORD_MISC_KERNEL) &&
714 !perf_host)
715 al->filtered = true;
716
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200717 return;
718 }
719try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200720 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200721 if (al->map == NULL) {
722 /*
723 * If this is outside of all known maps, and is a negative
724 * address, try to look it up in the kernel dso, as it might be
725 * a vsyscall or vdso (which executes in user-mode).
726 *
727 * XXX This is nasty, we should have a symbol list in the
728 * "[vdso]" dso, but for now lets use the old trick of looking
729 * in the whole kernel symbol list.
730 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800731 if ((long long)al->addr < 0 &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300732 cpumode == PERF_RECORD_MISC_KERNEL &&
733 machine && mg != &machine->kmaps) {
734 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200735 goto try_again;
736 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200737 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200738 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200739}
740
741void thread__find_addr_location(struct thread *self,
742 struct perf_session *session, u8 cpumode,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800743 enum map_type type, pid_t pid, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200744 struct addr_location *al,
745 symbol_filter_t filter)
746{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800747 thread__find_addr_map(self, session, cpumode, type, pid, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200748 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200749 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200750 else
751 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200752}
753
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300754static void dso__calc_col_width(struct dso *self, struct hists *hists)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200755{
756 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
757 (!symbol_conf.dso_list ||
758 strlist__has_entry(symbol_conf.dso_list, self->name))) {
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300759 u16 slen = dso__name_len(self);
760 hists__new_col_len(hists, HISTC_DSO, slen);
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200761 }
762
763 self->slen_calculated = 1;
764}
765
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200766int perf_event__preprocess_sample(const union perf_event *event,
767 struct perf_session *session,
768 struct addr_location *al,
769 struct perf_sample *sample,
770 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200771{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200772 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
773 struct thread *thread = perf_session__findnew(session, event->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200774
775 if (thread == NULL)
776 return -1;
777
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200778 if (symbol_conf.comm_list &&
779 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
780 goto out_filtered;
781
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200782 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300783 /*
784 * Have we already created the kernel maps for the host machine?
785 *
786 * This should have happened earlier, when we processed the kernel MMAP
787 * events, but for older perf.data files there was no such thing, so do
788 * it now.
789 */
790 if (cpumode == PERF_RECORD_MISC_KERNEL &&
791 session->host_machine.vmlinux_maps[MAP__FUNCTION] == NULL)
792 machine__create_kernel_maps(&session->host_machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200793
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300794 thread__find_addr_map(thread, session, cpumode, MAP__FUNCTION,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200795 event->ip.pid, event->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200796 dump_printf(" ...... dso: %s\n",
797 al->map ? al->map->dso->long_name :
798 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300799 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200800 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200801
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300802 if (al->map) {
803 if (symbol_conf.dso_list &&
804 (!al->map || !al->map->dso ||
805 !(strlist__has_entry(symbol_conf.dso_list,
806 al->map->dso->short_name) ||
807 (al->map->dso->short_name != al->map->dso->long_name &&
808 strlist__has_entry(symbol_conf.dso_list,
809 al->map->dso->long_name)))))
810 goto out_filtered;
811 /*
812 * We have to do this here as we may have a dso with no symbol
813 * hit that has a name longer than the ones with symbols
814 * sampled.
815 */
816 if (!sort_dso.elide && !al->map->dso->slen_calculated)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300817 dso__calc_col_width(al->map->dso, &session->hists);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300818
819 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo3ceb0d42010-05-09 16:07:01 -0300820 } else {
821 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
822
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300823 if (hists__col_len(&session->hists, HISTC_DSO) < unresolved_col_width &&
Arnaldo Carvalho de Melo3ceb0d42010-05-09 16:07:01 -0300824 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
825 !symbol_conf.dso_list)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300826 hists__set_col_len(&session->hists, HISTC_DSO,
827 unresolved_col_width);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300828 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200829
830 if (symbol_conf.sym_list && al->sym &&
831 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
832 goto out_filtered;
833
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200834 return 0;
835
836out_filtered:
837 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200838 return 0;
839}