blob: 2905771a1f494ac7c65aa605c6fd2b62a60025ca [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 Melof62d3f02012-10-06 15:44:59 -03004#include "machine.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 Meloc506c962013-12-11 09:15:00 -030010#include "symbol/kallsyms.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020011
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020012static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020013 [0] = "TOTAL",
14 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020015 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020016 [PERF_RECORD_LOST] = "LOST",
17 [PERF_RECORD_COMM] = "COMM",
18 [PERF_RECORD_EXIT] = "EXIT",
19 [PERF_RECORD_THROTTLE] = "THROTTLE",
20 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
21 [PERF_RECORD_FORK] = "FORK",
22 [PERF_RECORD_READ] = "READ",
23 [PERF_RECORD_SAMPLE] = "SAMPLE",
24 [PERF_RECORD_HEADER_ATTR] = "ATTR",
25 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
26 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
27 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
28 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030029};
30
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020031const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc02010-12-07 12:48:42 +000032{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020033 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc02010-12-07 12:48:42 +000034 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020035 if (!perf_event__names[id])
Thomas Gleixner3835bc02010-12-07 12:48:42 +000036 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020037 return perf_event__names[id];
Thomas Gleixner3835bc02010-12-07 12:48:42 +000038}
39
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020040static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020041 .pid = -1,
42 .tid = -1,
43 .time = -1,
44 .stream_id = -1,
45 .cpu = -1,
46 .period = 1,
47};
48
David Ahernf5faf722011-12-22 11:30:02 -070049static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
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;
David Ahernf5faf722011-12-22 11:30:02 -070055 pid_t tgid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020056
57 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
58
59 fp = fopen(filename, "r");
60 if (fp == NULL) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020061 pr_debug("couldn't open %s\n", filename);
62 return 0;
63 }
64
David Ahernf5faf722011-12-22 11:30:02 -070065 while (!comm[0] || (tgid < 0)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020066 if (fgets(bf, sizeof(bf), fp) == NULL) {
David Ahernf5faf722011-12-22 11:30:02 -070067 pr_warning("couldn't get COMM and pgid, malformed %s\n",
68 filename);
69 break;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020070 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020071
72 if (memcmp(bf, "Name:", 5) == 0) {
73 char *name = bf + 5;
74 while (*name && isspace(*name))
75 ++name;
76 size = strlen(name) - 1;
David Ahernf5faf722011-12-22 11:30:02 -070077 if (size >= len)
78 size = len - 1;
79 memcpy(comm, name, size);
David Aherncfbd70c2012-02-24 12:31:38 -070080 comm[size] = '\0';
David Ahernf5faf722011-12-22 11:30:02 -070081
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020082 } else if (memcmp(bf, "Tgid:", 5) == 0) {
83 char *tgids = bf + 5;
84 while (*tgids && isspace(*tgids))
85 ++tgids;
David Ahernf5faf722011-12-22 11:30:02 -070086 tgid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020087 }
88 }
89
David Ahernf5faf722011-12-22 11:30:02 -070090 fclose(fp);
91
92 return tgid;
93}
94
95static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
96 union perf_event *event, pid_t pid,
97 int full,
98 perf_event__handler_t process,
99 struct machine *machine)
100{
101 char filename[PATH_MAX];
102 size_t size;
103 DIR *tasks;
104 struct dirent dirent, *next;
105 pid_t tgid;
106
107 memset(&event->comm, 0, sizeof(event->comm));
108
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500109 if (machine__is_host(machine))
110 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
111 sizeof(event->comm.comm));
112 else
113 tgid = machine->pid;
114
David Ahernf5faf722011-12-22 11:30:02 -0700115 if (tgid < 0)
116 goto out;
117
118 event->comm.pid = tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200119 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700120
121 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300122 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200123 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200124 event->comm.header.size = (sizeof(event->comm) -
125 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200126 machine->id_hdr_size);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200127 if (!full) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200128 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200129
David Ahern1e6d5322012-08-26 12:24:42 -0600130 if (process(tool, event, &synth_sample, machine) != 0)
131 return -1;
132
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200133 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200134 }
135
Dongsheng Yang99563462013-12-20 15:52:57 -0500136 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
137 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200138
139 tasks = opendir(filename);
David Ahernf5faf722011-12-22 11:30:02 -0700140 if (tasks == NULL) {
141 pr_debug("couldn't open %s\n", filename);
142 return 0;
143 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200144
145 while (!readdir_r(tasks, &dirent, &next) && next) {
146 char *end;
147 pid = strtol(dirent.d_name, &end, 10);
148 if (*end)
149 continue;
150
David Ahernf5faf722011-12-22 11:30:02 -0700151 /* already have tgid; jut want to update the comm */
152 (void) perf_event__get_comm_tgid(pid, event->comm.comm,
153 sizeof(event->comm.comm));
154
155 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300156 size = PERF_ALIGN(size, sizeof(u64));
David Ahernf5faf722011-12-22 11:30:02 -0700157 memset(event->comm.comm + size, 0, machine->id_hdr_size);
158 event->comm.header.size = (sizeof(event->comm) -
159 (sizeof(event->comm.comm) - size) +
160 machine->id_hdr_size);
161
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200162 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200163
David Ahern1e6d5322012-08-26 12:24:42 -0600164 if (process(tool, event, &synth_sample, machine) != 0) {
165 tgid = -1;
166 break;
167 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200168 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200169
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200170 closedir(tasks);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200171out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200172 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200173}
174
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200175static int perf_event__synthesize_mmap_events(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200176 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200177 pid_t pid, pid_t tgid,
178 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300179 struct machine *machine,
180 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200181{
182 char filename[PATH_MAX];
183 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600184 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200185
Dongsheng Yang99563462013-12-20 15:52:57 -0500186 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
187 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200188
189 fp = fopen(filename, "r");
190 if (fp == NULL) {
191 /*
192 * We raced with a task exiting - just return:
193 */
194 pr_debug("couldn't open %s\n", filename);
195 return -1;
196 }
197
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200198 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200199
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200200 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900201 char bf[BUFSIZ];
202 char prot[5];
203 char execname[PATH_MAX];
204 char anonstr[] = "//anon";
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200205 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200206 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900207
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200208 if (fgets(bf, sizeof(bf), fp) == NULL)
209 break;
210
Namhyung Kim60648032012-11-11 23:20:50 +0900211 /* ensure null termination since stack will be reused. */
212 strcpy(execname, "");
213
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200214 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200215 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %*x:%*x %*u %s\n",
216 &event->mmap.start, &event->mmap.len, prot,
217 &event->mmap.pgoff,
218 execname);
Don Zickus9d4ecc82013-11-13 15:32:06 -0300219 /*
220 * Anon maps don't have the execname.
221 */
222 if (n < 4)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200223 continue;
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300224 /*
225 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
226 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500227 if (machine__is_host(machine))
228 event->header.misc = PERF_RECORD_MISC_USER;
229 else
230 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900231
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300232 if (prot[2] != 'x') {
233 if (!mmap_data || prot[0] != 'r')
234 continue;
235
236 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
237 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200238
Namhyung Kim60648032012-11-11 23:20:50 +0900239 if (!strcmp(execname, ""))
240 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200241
Namhyung Kim60648032012-11-11 23:20:50 +0900242 size = strlen(execname) + 1;
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200243 memcpy(event->mmap.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900244 size = PERF_ALIGN(size, sizeof(u64));
Stephane Eranian3090ffb2013-10-17 19:32:15 +0200245 event->mmap.len -= event->mmap.start;
246 event->mmap.header.size = (sizeof(event->mmap) -
247 (sizeof(event->mmap.filename) - size));
248 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
249 event->mmap.header.size += machine->id_hdr_size;
250 event->mmap.pid = tgid;
251 event->mmap.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000252
Namhyung Kim60648032012-11-11 23:20:50 +0900253 if (process(tool, event, &synth_sample, machine) != 0) {
254 rc = -1;
255 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200256 }
257 }
258
259 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600260 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200261}
262
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200263int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200264 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200265 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200266{
David Ahern1e6d5322012-08-26 12:24:42 -0600267 int rc = 0;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200268 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300269 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200270 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200271 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200272 if (event == NULL) {
273 pr_debug("Not enough memory synthesizing mmap event "
274 "for kernel modules\n");
275 return -1;
276 }
277
278 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200279
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800280 /*
281 * kernel uses 0 for user space maps, see kernel/perf_event.c
282 * __perf_event_mmap
283 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300284 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200285 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800286 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200287 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800288
289 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200290 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200291 size_t size;
292 struct map *pos = rb_entry(nd, struct map, rb_node);
293
294 if (pos->dso->kernel)
295 continue;
296
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300297 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200298 event->mmap.header.type = PERF_RECORD_MMAP;
299 event->mmap.header.size = (sizeof(event->mmap) -
300 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200301 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
302 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200303 event->mmap.start = pos->start;
304 event->mmap.len = pos->end - pos->start;
305 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200306
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200307 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200308 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600309 if (process(tool, event, &synth_sample, machine) != 0) {
310 rc = -1;
311 break;
312 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200313 }
314
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200315 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600316 return rc;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200317}
318
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200319static int __event__synthesize_thread(union perf_event *comm_event,
320 union perf_event *mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700321 pid_t pid, int full,
322 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200323 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300324 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200325{
David Aherndefd8d32011-12-22 11:30:01 -0700326 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200327 process, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200328 if (tgid == -1)
329 return -1;
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200330 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300331 process, machine, mmap_data);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200332}
333
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200334int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200335 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200336 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300337 struct machine *machine,
338 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200339{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200340 union perf_event *comm_event, *mmap_event;
David Aherndefd8d32011-12-22 11:30:01 -0700341 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200342
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200343 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200344 if (comm_event == NULL)
345 goto out;
346
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200347 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200348 if (mmap_event == NULL)
349 goto out_free_comm;
350
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200351 err = 0;
352 for (thread = 0; thread < threads->nr; ++thread) {
353 if (__event__synthesize_thread(comm_event, mmap_event,
David Aherndefd8d32011-12-22 11:30:01 -0700354 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300355 process, tool, machine,
356 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200357 err = -1;
358 break;
359 }
David Aherndefd8d32011-12-22 11:30:01 -0700360
361 /*
362 * comm.pid is set to thread group id by
363 * perf_event__synthesize_comm
364 */
365 if ((int) comm_event->comm.pid != threads->map[thread]) {
366 bool need_leader = true;
367
368 /* is thread group leader in thread_map? */
369 for (j = 0; j < threads->nr; ++j) {
370 if ((int) comm_event->comm.pid == threads->map[j]) {
371 need_leader = false;
372 break;
373 }
374 }
375
376 /* if not, generate events for it */
377 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300378 __event__synthesize_thread(comm_event, mmap_event,
379 comm_event->comm.pid, 0,
380 process, tool, machine,
381 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700382 err = -1;
383 break;
384 }
385 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200386 }
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200387 free(mmap_event);
388out_free_comm:
389 free(comm_event);
390out:
391 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200392}
393
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200394int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200395 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300396 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200397{
398 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500399 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200400 struct dirent dirent, *next;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200401 union perf_event *comm_event, *mmap_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200402 int err = -1;
403
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200404 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200405 if (comm_event == NULL)
406 goto out;
407
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200408 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200409 if (mmap_event == NULL)
410 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200411
Dongsheng Yang99563462013-12-20 15:52:57 -0500412 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
413 proc = opendir(proc_path);
414
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200415 if (proc == NULL)
416 goto out_free_mmap;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200417
418 while (!readdir_r(proc, &dirent, &next) && next) {
419 char *end;
420 pid_t pid = strtol(dirent.d_name, &end, 10);
421
422 if (*end) /* only interested in proper numerical dirents */
423 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300424 /*
425 * We may race with exiting thread, so don't stop just because
426 * one thread couldn't be synthesized.
427 */
428 __event__synthesize_thread(comm_event, mmap_event, pid, 1,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300429 process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200430 }
431
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200432 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600433 closedir(proc);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200434out_free_mmap:
435 free(mmap_event);
436out_free_comm:
437 free(comm_event);
438out:
439 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200440}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200441
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200442struct process_symbol_args {
443 const char *name;
444 u64 start;
445};
446
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200447static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700448 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200449{
450 struct process_symbol_args *args = arg;
451
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200452 /*
453 * Must be a function or at least an alias, as in PARISC64, where "_text" is
454 * an 'A' to the same address as "_stext".
455 */
456 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
457 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200458 return 0;
459
460 args->start = start;
461 return 1;
462}
463
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200464int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200465 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200466 struct machine *machine,
467 const char *symbol_name)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200468{
469 size_t size;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800470 const char *filename, *mmap_name;
471 char path[PATH_MAX];
472 char name_buff[PATH_MAX];
473 struct map *map;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200474 int err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200475 /*
476 * We should get this from /sys/kernel/sections/.text, but till that is
477 * available use this, and after it is use this as a fallback for older
478 * kernels.
479 */
480 struct process_symbol_args args = { .name = symbol_name, };
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200481 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200482 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200483 if (event == NULL) {
484 pr_debug("Not enough memory synthesizing mmap event "
485 "for kernel modules\n");
486 return -1;
487 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200488
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300489 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300490 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800491 /*
492 * kernel uses PERF_RECORD_MISC_USER for user space maps,
493 * see kernel/perf_event.c __perf_event_mmap
494 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200495 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800496 filename = "/proc/kallsyms";
497 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200498 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300499 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800500 filename = (char *) symbol_conf.default_guest_kallsyms;
501 else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300502 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800503 filename = path;
504 }
505 }
506
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100507 if (kallsyms__parse(filename, &args, find_symbol_cb) <= 0) {
508 free(event);
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200509 return -ENOENT;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100510 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200511
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300512 map = machine->vmlinux_maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200513 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800514 "%s%s", mmap_name, symbol_name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300515 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200516 event->mmap.header.type = PERF_RECORD_MMAP;
517 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200518 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200519 event->mmap.pgoff = args.start;
520 event->mmap.start = map->start;
521 event->mmap.len = map->end - event->mmap.start;
522 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200523
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200524 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200525 free(event);
526
527 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200528}
529
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200530size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
531{
532 return fprintf(fp, ": %s:%d\n", event->comm.comm, event->comm.tid);
533}
534
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300535int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200536 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200537 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200538 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200539{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200540 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200541}
542
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300543int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200544 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200545 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300546 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200547{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200548 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800549}
550
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200551size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
552{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300553 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200554 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300555 event->mmap.len, event->mmap.pgoff,
556 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
557 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200558}
559
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200560size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
561{
562 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300563 " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200564 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
565 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
566 event->mmap2.min, event->mmap2.ino,
567 event->mmap2.ino_generation,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300568 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200569 event->mmap2.filename);
570}
571
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300572int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200573 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200574 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200575 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800576{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200577 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200578}
579
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200580int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
581 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200582 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200583 struct machine *machine)
584{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200585 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200586}
587
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200588size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
589{
590 return fprintf(fp, "(%d:%d):(%d:%d)\n",
591 event->fork.pid, event->fork.tid,
592 event->fork.ppid, event->fork.ptid);
593}
594
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300595int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200596 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200597 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300598 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200599{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200600 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200601}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200602
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300603int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
604 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200605 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300606 struct machine *machine)
607{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200608 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300609}
610
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200611size_t perf_event__fprintf(union perf_event *event, FILE *fp)
612{
613 size_t ret = fprintf(fp, "PERF_RECORD_%s",
614 perf_event__name(event->header.type));
615
616 switch (event->header.type) {
617 case PERF_RECORD_COMM:
618 ret += perf_event__fprintf_comm(event, fp);
619 break;
620 case PERF_RECORD_FORK:
621 case PERF_RECORD_EXIT:
622 ret += perf_event__fprintf_task(event, fp);
623 break;
624 case PERF_RECORD_MMAP:
625 ret += perf_event__fprintf_mmap(event, fp);
626 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200627 case PERF_RECORD_MMAP2:
628 ret += perf_event__fprintf_mmap2(event, fp);
629 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200630 default:
631 ret += fprintf(fp, "\n");
632 }
633
634 return ret;
635}
636
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300637int perf_event__process(struct perf_tool *tool __maybe_unused,
638 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200639 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300640 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530641{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200642 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530643}
644
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300645void thread__find_addr_map(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200646 struct machine *machine, u8 cpumode,
647 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300648 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200649{
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300650 struct map_groups *mg = &thread->mg;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300651 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200652
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300653 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300654 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200655 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800656 al->cpumode = cpumode;
657 al->filtered = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200658
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200659 if (machine == NULL) {
660 al->map = NULL;
661 return;
662 }
663
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800664 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200665 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300666 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300667 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800668 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200669 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800670 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
671 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300672 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300673 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500674 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
675 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800676 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500677 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200678 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800679
680 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
681 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
682 !perf_guest)
683 al->filtered = true;
684 if ((cpumode == PERF_RECORD_MISC_USER ||
685 cpumode == PERF_RECORD_MISC_KERNEL) &&
686 !perf_host)
687 al->filtered = true;
688
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200689 return;
690 }
691try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200692 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200693 if (al->map == NULL) {
694 /*
695 * If this is outside of all known maps, and is a negative
696 * address, try to look it up in the kernel dso, as it might be
697 * a vsyscall or vdso (which executes in user-mode).
698 *
699 * XXX This is nasty, we should have a symbol list in the
700 * "[vdso]" dso, but for now lets use the old trick of looking
701 * in the whole kernel symbol list.
702 */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800703 if ((long long)al->addr < 0 &&
Andrew Lutomirski6c6804fb2011-03-24 00:36:56 -0400704 cpumode == PERF_RECORD_MISC_USER &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300705 machine && mg != &machine->kmaps) {
706 mg = &machine->kmaps;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200707 goto try_again;
708 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300709 } else {
710 /*
711 * Kernel maps might be changed when loading symbols so loading
712 * must be done prior to using kernel maps.
713 */
714 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300715 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200716 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300717 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200718}
719
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200720void thread__find_addr_location(struct thread *thread, struct machine *machine,
721 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300722 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200723{
Adrian Hunter326f59b2013-08-08 14:32:27 +0300724 thread__find_addr_map(thread, machine, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200725 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300726 al->sym = map__find_symbol(al->map, al->addr,
727 machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200728 else
729 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200730}
731
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200732int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200733 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200734 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300735 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200736{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200737 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300738 struct thread *thread = machine__findnew_thread(machine, sample->pid,
739 sample->pid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200740
741 if (thread == NULL)
742 return -1;
743
David Ahern1f3878c2013-11-18 13:32:47 -0700744 if (thread__is_filtered(thread))
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200745 goto out_filtered;
746
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200747 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300748 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200749 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300750 *
751 * This should have happened earlier, when we processed the kernel MMAP
752 * events, but for older perf.data files there was no such thing, so do
753 * it now.
754 */
755 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200756 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
757 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200758
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200759 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
Adrian Hunteref893252013-08-27 11:23:06 +0300760 sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200761 dump_printf(" ...... dso: %s\n",
762 al->map ? al->map->dso->long_name :
763 al->level == 'H' ? "[hypervisor]" : "<not found>");
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300764 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200765 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200766
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300767 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900768 struct dso *dso = al->map->dso;
769
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300770 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900771 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
772 dso->short_name) ||
773 (dso->short_name != dso->long_name &&
774 strlist__has_entry(symbol_conf.dso_list,
775 dso->long_name)))))
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300776 goto out_filtered;
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300777
Adrian Huntere44baa32013-08-08 14:32:25 +0300778 al->sym = map__find_symbol(al->map, al->addr,
779 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300780 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200781
Feng Tang1500b932012-09-07 16:42:23 +0800782 if (symbol_conf.sym_list &&
783 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
784 al->sym->name)))
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200785 goto out_filtered;
786
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200787 return 0;
788
789out_filtered:
790 al->filtered = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200791 return 0;
792}