blob: 0cdc811c48e281176b0543446b31df973dcb6cfc [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 Melod20deb62011-11-25 08:19:45 -020046static pid_t perf_event__synthesize_comm(struct perf_event_ops *ops,
47 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 Melo743eb862011-11-28 07:56:39 -0200102 process(ops, 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 Melo743eb862011-11-28 07:56:39 -0200120 process(ops, 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 Melod20deb62011-11-25 08:19:45 -0200130static int perf_event__synthesize_mmap_events(struct perf_event_ops *ops,
131 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 Melo743eb862011-11-28 07:56:39 -0200202 process(ops, 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 Melod20deb62011-11-25 08:19:45 -0200210int perf_event__synthesize_modules(struct perf_event_ops *ops,
211 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 Melo743eb862011-11-28 07:56:39 -0200255 process(ops, 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,
264 pid_t pid, perf_event__handler_t process,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200265 struct perf_event_ops *ops,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200266 struct machine *machine)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200267{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200268 pid_t tgid = perf_event__synthesize_comm(ops, comm_event, pid, 1,
269 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200270 if (tgid == -1)
271 return -1;
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200272 return perf_event__synthesize_mmap_events(ops, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200273 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200274}
275
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200276int perf_event__synthesize_thread_map(struct perf_event_ops *ops,
277 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200278 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200279 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200280{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200281 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200282 int err = -1, thread;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200283
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200284 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200285 if (comm_event == NULL)
286 goto out;
287
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200288 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200289 if (mmap_event == NULL)
290 goto out_free_comm;
291
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200292 err = 0;
293 for (thread = 0; thread < threads->nr; ++thread) {
294 if (__event__synthesize_thread(comm_event, mmap_event,
295 threads->map[thread],
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200296 process, ops, machine)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200297 err = -1;
298 break;
299 }
300 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200301 free(mmap_event);
302out_free_comm:
303 free(comm_event);
304out:
305 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200306}
307
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200308int perf_event__synthesize_threads(struct perf_event_ops *ops,
309 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200310 struct machine *machine)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200311{
312 DIR *proc;
313 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200314 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200315 int err = -1;
316
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200317 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200318 if (comm_event == NULL)
319 goto out;
320
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200321 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200322 if (mmap_event == NULL)
323 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200324
325 proc = opendir("/proc");
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200326 if (proc == NULL)
327 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200328
329 while (!readdir_r(proc, &dirent, &next) && next) {
330 char *end;
331 pid_t pid = strtol(dirent.d_name, &end, 10);
332
333 if (*end) /* only interested in proper numerical dirents */
334 continue;
335
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200336 __event__synthesize_thread(comm_event, mmap_event, pid,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200337 process, ops, machine);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200338 }
339
340 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200341 err = 0;
342out_free_mmap:
343 free(mmap_event);
344out_free_comm:
345 free(comm_event);
346out:
347 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200348}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200349
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200350struct process_symbol_args {
351 const char *name;
352 u64 start;
353};
354
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200355static int find_symbol_cb(void *arg, const char *name, char type,
356 u64 start, u64 end __used)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200357{
358 struct process_symbol_args *args = arg;
359
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200360 /*
361 * Must be a function or at least an alias, as in PARISC64, where "_text" is
362 * an 'A' to the same address as "_stext".
363 */
364 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
365 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200366 return 0;
367
368 args->start = start;
369 return 1;
370}
371
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200372int perf_event__synthesize_kernel_mmap(struct perf_event_ops *ops,
373 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200374 struct machine *machine,
375 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200376{
377 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800378 const char *filename, *mmap_name;
379 char path[PATH_MAX];
380 char name_buff[PATH_MAX];
381 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200382 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200383 /*
384 * We should get this from /sys/kernel/sections/.text, but till that is
385 * available use this, and after it is use this as a fallback for older
386 * kernels.
387 */
388 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200389 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200390 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200391 if (event == NULL) {
392 pr_debug("Not enough memory synthesizing mmap event "
393 "for kernel modules\n");
394 return -1;
395 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200396
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300397 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300398 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800399 /*
400 * kernel uses PERF_RECORD_MISC_USER for user space maps,
401 * see kernel/perf_event.c __perf_event_mmap
402 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200403 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800404 filename = "/proc/kallsyms";
405 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200406 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300407 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800408 filename = (char *) symbol_conf.default_guest_kallsyms;
409 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300410 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800411 filename = path;
412 }
413 }
414
415 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200416 return -ENOENT;
417
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300418 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200419 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800420 "%s%s", mmap_name, symbol_name) + 1;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200421 size = ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200422 event->mmap.header.type = PERF_RECORD_MMAP;
423 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200424 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200425 event->mmap.pgoff = args.start;
426 event->mmap.start = map->start;
427 event->mmap.len = map->end - event->mmap.start;
428 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200429
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200430 err = process(ops, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200431 free(event);
432
433 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200434}
435
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200436int perf_event__process_comm(struct perf_event_ops *ops __used,
437 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200438 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200439 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200440{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200441 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200442
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200443 dump_printf(": %s:%d\n", event->comm.comm, event->comm.tid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200444
Arnaldo Carvalho de Melod7603d52011-03-04 14:51:33 -0300445 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200446 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
447 return -1;
448 }
449
450 return 0;
451}
452
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200453int perf_event__process_lost(struct perf_event_ops *ops __used,
454 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200455 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200456 struct machine *machine __used)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200457{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200458 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200459 event->lost.id, event->lost.lost);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200460 return 0;
461}
462
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200463static void perf_event__set_kernel_mmap_len(union perf_event *event,
464 struct map **maps)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200465{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200466 maps[MAP__FUNCTION]->start = event->mmap.start;
467 maps[MAP__FUNCTION]->end = event->mmap.start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800468 /*
469 * Be a bit paranoid here, some perf.data file came with
470 * a zero sized synthesized MMAP event for the kernel.
471 */
472 if (maps[MAP__FUNCTION]->end == 0)
Ian Munsie9d1faba2010-11-25 15:12:53 +1100473 maps[MAP__FUNCTION]->end = ~0ULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800474}
475
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200476static int perf_event__process_kernel_mmap(struct perf_event_ops *ops __used,
477 union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200478 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800479{
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200480 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800481 char kmmap_prefix[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800482 enum dso_kernel_type kernel_type;
483 bool is_kernel_mmap;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200484
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300485 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300486 if (machine__is_host(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800487 kernel_type = DSO_TYPE_KERNEL;
488 else
489 kernel_type = DSO_TYPE_GUEST_KERNEL;
490
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200491 is_kernel_mmap = memcmp(event->mmap.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800492 kmmap_prefix,
493 strlen(kmmap_prefix)) == 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200494 if (event->mmap.filename[0] == '/' ||
495 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800496
497 char short_module_name[1024];
498 char *name, *dot;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200499
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200500 if (event->mmap.filename[0] == '/') {
501 name = strrchr(event->mmap.filename, '/');
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200502 if (name == NULL)
503 goto out_problem;
504
505 ++name; /* skip / */
506 dot = strrchr(name, '.');
507 if (dot == NULL)
508 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200509 snprintf(short_module_name, sizeof(short_module_name),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800510 "[%.*s]", (int)(dot - name), name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200511 strxfrchar(short_module_name, '-', '_');
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800512 } else
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200513 strcpy(short_module_name, event->mmap.filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200514
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200515 map = machine__new_module(machine, event->mmap.start,
516 event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800517 if (map == NULL)
518 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200519
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800520 name = strdup(short_module_name);
521 if (name == NULL)
522 goto out_problem;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200523
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800524 map->dso->short_name = name;
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300525 map->dso->sname_alloc = 1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200526 map->end = map->start + event->mmap.len;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800527 } else if (is_kernel_mmap) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200528 const char *symbol_name = (event->mmap.filename +
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800529 strlen(kmmap_prefix));
530 /*
531 * Should be there already, from the build-id table in
532 * the header.
533 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300534 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
535 kmmap_prefix);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800536 if (kernel == NULL)
537 goto out_problem;
538
539 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300540 if (__machine__create_kernel_maps(machine, kernel) < 0)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800541 goto out_problem;
542
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200543 perf_event__set_kernel_mmap_len(event, machine->vmlinux_maps);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300544
545 /*
546 * Avoid using a zero address (kptr_restrict) for the ref reloc
547 * symbol. Effectively having zero here means that at record
548 * time /proc/sys/kernel/kptr_restrict was non zero.
549 */
550 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200551 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
552 symbol_name,
553 event->mmap.pgoff);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300554 }
555
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300556 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200557 /*
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800558 * preload dso of guest kernel and modules
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200559 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300560 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
561 NULL);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200562 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800563 }
564 return 0;
565out_problem:
566 return -1;
567}
568
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200569int perf_event__process_mmap(struct perf_event_ops *ops,
570 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200571 struct perf_sample *sample __used,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200572 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800573{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800574 struct thread *thread;
575 struct map *map;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200576 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800577 int ret = 0;
578
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200579 dump_printf(" %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200580 event->mmap.pid, event->mmap.tid, event->mmap.start,
581 event->mmap.len, event->mmap.pgoff, event->mmap.filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800582
583 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
584 cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200585 ret = perf_event__process_kernel_mmap(ops, event, machine);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800586 if (ret < 0)
587 goto out_problem;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200588 return 0;
589 }
590
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200591 thread = machine__findnew_thread(machine, event->mmap.pid);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300592 if (thread == NULL)
593 goto out_problem;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200594 map = map__new(&machine->user_dsos, event->mmap.start,
595 event->mmap.len, event->mmap.pgoff,
596 event->mmap.pid, event->mmap.filename,
Dave Martin361d1342010-07-27 16:40:02 +0100597 MAP__FUNCTION);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -0300598 if (map == NULL)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200599 goto out_problem;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200600
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200601 thread__insert_map(thread, map);
602 return 0;
603
604out_problem:
605 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200606 return 0;
607}
608
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200609int perf_event__process_task(struct perf_event_ops *ops __used,
610 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)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200613{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200614 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
615 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200616
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200617 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
618 event->fork.ppid, event->fork.ptid);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200619
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200620 if (event->header.type == PERF_RECORD_EXIT) {
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200621 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200622 return 0;
Arnaldo Carvalho de Melo720a3ae2010-06-17 08:37:44 -0300623 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200624
625 if (thread == NULL || parent == NULL ||
626 thread__fork(thread, parent) < 0) {
627 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
628 return -1;
629 }
630
631 return 0;
632}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200633
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200634int perf_event__process(struct perf_event_ops *ops, union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200635 struct perf_sample *sample, struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530636{
637 switch (event->header.type) {
638 case PERF_RECORD_COMM:
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200639 perf_event__process_comm(ops, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530640 break;
641 case PERF_RECORD_MMAP:
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200642 perf_event__process_mmap(ops, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530643 break;
644 case PERF_RECORD_FORK:
645 case PERF_RECORD_EXIT:
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200646 perf_event__process_task(ops, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530647 break;
Arnaldo Carvalho de Meloef2bf6d2011-01-29 09:04:40 -0200648 case PERF_RECORD_LOST:
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200649 perf_event__process_lost(ops, event, sample, machine);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530650 default:
651 break;
652 }
653
654 return 0;
655}
656
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200657void thread__find_addr_map(struct thread *self,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200658 struct machine *machine, u8 cpumode,
659 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200660 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200661{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200662 struct map_groups *mg = &self->mg;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200663
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200664 al->thread = self;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200665 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800666 al->cpumode = cpumode;
667 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200668
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200669 if (machine == NULL) {
670 al->map = NULL;
671 return;
672 }
673
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800674 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200675 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300676 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800677 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200678 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800679 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
680 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300681 mg = &machine->kmaps;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800682 } else {
683 /*
684 * 'u' means guest os user space.
685 * TODO: We don't support guest user space. Might support late.
686 */
687 if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest)
688 al->level = 'u';
689 else
690 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200691 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800692
693 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
694 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
695 !perf_guest)
696 al->filtered = true;
697 if ((cpumode == PERF_RECORD_MISC_USER ||
698 cpumode == PERF_RECORD_MISC_KERNEL) &&
699 !perf_host)
700 al->filtered = true;
701
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200702 return;
703 }
704try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200705 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200706 if (al->map == NULL) {
707 /*
708 * If this is outside of all known maps, and is a negative
709 * address, try to look it up in the kernel dso, as it might be
710 * a vsyscall or vdso (which executes in user-mode).
711 *
712 * XXX This is nasty, we should have a symbol list in the
713 * "[vdso]" dso, but for now lets use the old trick of looking
714 * in the whole kernel symbol list.
715 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800716 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400717 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300718 machine && mg != &machine->kmaps) {
719 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200720 goto try_again;
721 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200722 } else
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200723 al->addr = al->map->map_ip(al->map, al->addr);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200724}
725
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200726void thread__find_addr_location(struct thread *thread, struct machine *machine,
727 u8 cpumode, enum map_type type, u64 addr,
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200728 struct addr_location *al,
729 symbol_filter_t filter)
730{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200731 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200732 if (al->map != NULL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200733 al->sym = map__find_symbol(al->map, al->addr, filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200734 else
735 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200736}
737
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200738int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200739 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200740 struct addr_location *al,
741 struct perf_sample *sample,
742 symbol_filter_t filter)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200743{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200744 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200745 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200746
747 if (thread == NULL)
748 return -1;
749
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200750 if (symbol_conf.comm_list &&
751 !strlist__has_entry(symbol_conf.comm_list, thread->comm))
752 goto out_filtered;
753
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200754 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300755 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200756 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300757 *
758 * This should have happened earlier, when we processed the kernel MMAP
759 * events, but for older perf.data files there was no such thing, so do
760 * it now.
761 */
762 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200763 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
764 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200765
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200766 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
767 event->ip.ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200768 dump_printf(" ...... dso: %s\n",
769 al->map ? al->map->dso->long_name :
770 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300771 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200772 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200773
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300774 if (al->map) {
775 if (symbol_conf.dso_list &&
776 (!al->map || !al->map->dso ||
777 !(strlist__has_entry(symbol_conf.dso_list,
778 al->map->dso->short_name) ||
779 (al->map->dso->short_name != al->map->dso->long_name &&
780 strlist__has_entry(symbol_conf.dso_list,
781 al->map->dso->long_name)))))
782 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300783
784 al->sym = map__find_symbol(al->map, al->addr, filter);
785 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200786
787 if (symbol_conf.sym_list && al->sym &&
788 !strlist__has_entry(symbol_conf.sym_list, al->sym->name))
789 goto out_filtered;
790
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200791 return 0;
792
793out_filtered:
794 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200795 return 0;
796}