blob: 9e806d855b04351a34e541913bcff88807e2b53f [file] [log] [blame]
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02001#include <linux/types.h>
Don Zickus7ef80702014-05-19 15:13:49 -04002#include <sys/mman.h>
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02003#include "event.h"
4#include "debug.h"
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03005#include "hist.h"
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03006#include "machine.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02007#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02008#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02009#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020010#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -020011#include "thread_map.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030012#include "symbol/kallsyms.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020013
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020014static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020015 [0] = "TOTAL",
16 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020017 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020018 [PERF_RECORD_LOST] = "LOST",
19 [PERF_RECORD_COMM] = "COMM",
20 [PERF_RECORD_EXIT] = "EXIT",
21 [PERF_RECORD_THROTTLE] = "THROTTLE",
22 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
23 [PERF_RECORD_FORK] = "FORK",
24 [PERF_RECORD_READ] = "READ",
25 [PERF_RECORD_SAMPLE] = "SAMPLE",
26 [PERF_RECORD_HEADER_ATTR] = "ATTR",
27 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
28 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
29 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
30 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020031 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030032};
33
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020034const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000035{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020036 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000037 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020038 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000039 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020040 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000041}
42
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020043static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020044 .pid = -1,
45 .tid = -1,
46 .time = -1,
47 .stream_id = -1,
48 .cpu = -1,
49 .period = 1,
50};
51
David Ahernf5faf722011-12-22 11:30:02 -070052static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020053{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020054 char filename[PATH_MAX];
55 char bf[BUFSIZ];
56 FILE *fp;
57 size_t size = 0;
David Ahernf5faf722011-12-22 11:30:02 -070058 pid_t tgid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020059
60 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
61
62 fp = fopen(filename, "r");
63 if (fp == NULL) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020064 pr_debug("couldn't open %s\n", filename);
65 return 0;
66 }
67
David Ahernf5faf722011-12-22 11:30:02 -070068 while (!comm[0] || (tgid < 0)) {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020069 if (fgets(bf, sizeof(bf), fp) == NULL) {
David Ahernf5faf722011-12-22 11:30:02 -070070 pr_warning("couldn't get COMM and pgid, malformed %s\n",
71 filename);
72 break;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -020073 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020074
75 if (memcmp(bf, "Name:", 5) == 0) {
76 char *name = bf + 5;
77 while (*name && isspace(*name))
78 ++name;
79 size = strlen(name) - 1;
David Ahernf5faf722011-12-22 11:30:02 -070080 if (size >= len)
81 size = len - 1;
82 memcpy(comm, name, size);
David Aherncfbd70c2012-02-24 12:31:38 -070083 comm[size] = '\0';
David Ahernf5faf722011-12-22 11:30:02 -070084
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;
David Ahernf5faf722011-12-22 11:30:02 -070089 tgid = atoi(tgids);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020090 }
91 }
92
David Ahernf5faf722011-12-22 11:30:02 -070093 fclose(fp);
94
95 return tgid;
96}
97
98static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
99 union perf_event *event, pid_t pid,
David Ahernf5faf722011-12-22 11:30:02 -0700100 perf_event__handler_t process,
101 struct machine *machine)
102{
David Ahernf5faf722011-12-22 11:30:02 -0700103 size_t size;
David Ahernf5faf722011-12-22 11:30:02 -0700104 pid_t tgid;
105
106 memset(&event->comm, 0, sizeof(event->comm));
107
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500108 if (machine__is_host(machine))
109 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
110 sizeof(event->comm.comm));
111 else
112 tgid = machine->pid;
113
David Ahernf5faf722011-12-22 11:30:02 -0700114 if (tgid < 0)
115 goto out;
116
117 event->comm.pid = tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200118 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700119
120 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300121 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200122 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200123 event->comm.header.size = (sizeof(event->comm) -
124 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200125 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500126 event->comm.tid = pid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200127
Don Zickusbfd66cc2014-02-26 10:45:26 -0500128 if (process(tool, event, &synth_sample, machine) != 0)
129 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600130
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200131out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200132 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200133}
134
Don Zickus363b7852014-03-14 10:43:44 -0400135static int perf_event__synthesize_fork(struct perf_tool *tool,
136 union perf_event *event, pid_t pid,
137 pid_t tgid, perf_event__handler_t process,
138 struct machine *machine)
139{
140 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
141
142 /* this is really a clone event but we use fork to synthesize it */
143 event->fork.ppid = tgid;
144 event->fork.ptid = tgid;
145 event->fork.pid = tgid;
146 event->fork.tid = pid;
147 event->fork.header.type = PERF_RECORD_FORK;
148
149 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
150
151 if (process(tool, event, &synth_sample, machine) != 0)
152 return -1;
153
154 return 0;
155}
156
Jiri Olsaa18382b2014-01-07 13:47:20 +0100157int perf_event__synthesize_mmap_events(struct perf_tool *tool,
158 union perf_event *event,
159 pid_t pid, pid_t tgid,
160 perf_event__handler_t process,
161 struct machine *machine,
162 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200163{
164 char filename[PATH_MAX];
165 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600166 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200167
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500168 if (machine__is_default_guest(machine))
169 return 0;
170
Dongsheng Yang99563462013-12-20 15:52:57 -0500171 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
172 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200173
174 fp = fopen(filename, "r");
175 if (fp == NULL) {
176 /*
177 * We raced with a task exiting - just return:
178 */
179 pr_debug("couldn't open %s\n", filename);
180 return -1;
181 }
182
Don Zickusa5a5ba72014-05-30 10:49:42 -0400183 event->header.type = PERF_RECORD_MMAP2;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200184
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200185 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900186 char bf[BUFSIZ];
187 char prot[5];
188 char execname[PATH_MAX];
189 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400190 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200191 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200192 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900193
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200194 if (fgets(bf, sizeof(bf), fp) == NULL)
195 break;
196
Namhyung Kim60648032012-11-11 23:20:50 +0900197 /* ensure null termination since stack will be reused. */
198 strcpy(execname, "");
199
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200200 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400201 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
202 &event->mmap2.start, &event->mmap2.len, prot,
203 &event->mmap2.pgoff, &event->mmap2.maj,
204 &event->mmap2.min,
205 &ino, execname);
206
Don Zickus9d4ecc82013-11-13 15:32:06 -0300207 /*
208 * Anon maps don't have the execname.
209 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400210 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200211 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400212
213 event->mmap2.ino = (u64)ino;
214
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300215 /*
216 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
217 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500218 if (machine__is_host(machine))
219 event->header.misc = PERF_RECORD_MISC_USER;
220 else
221 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900222
Don Zickus7ef80702014-05-19 15:13:49 -0400223 /* map protection and flags bits */
224 event->mmap2.prot = 0;
225 event->mmap2.flags = 0;
226 if (prot[0] == 'r')
227 event->mmap2.prot |= PROT_READ;
228 if (prot[1] == 'w')
229 event->mmap2.prot |= PROT_WRITE;
230 if (prot[2] == 'x')
231 event->mmap2.prot |= PROT_EXEC;
232
233 if (prot[3] == 's')
234 event->mmap2.flags |= MAP_SHARED;
235 else
236 event->mmap2.flags |= MAP_PRIVATE;
237
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300238 if (prot[2] != 'x') {
239 if (!mmap_data || prot[0] != 'r')
240 continue;
241
242 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
243 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200244
Namhyung Kim60648032012-11-11 23:20:50 +0900245 if (!strcmp(execname, ""))
246 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200247
Namhyung Kim60648032012-11-11 23:20:50 +0900248 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400249 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900250 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400251 event->mmap2.len -= event->mmap.start;
252 event->mmap2.header.size = (sizeof(event->mmap2) -
253 (sizeof(event->mmap2.filename) - size));
254 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
255 event->mmap2.header.size += machine->id_hdr_size;
256 event->mmap2.pid = tgid;
257 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000258
Namhyung Kim60648032012-11-11 23:20:50 +0900259 if (process(tool, event, &synth_sample, machine) != 0) {
260 rc = -1;
261 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200262 }
263 }
264
265 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600266 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200267}
268
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200269int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200270 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200271 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200272{
David Ahern1e6d5322012-08-26 12:24:42 -0600273 int rc = 0;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200274 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300275 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200276 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200277 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200278 if (event == NULL) {
279 pr_debug("Not enough memory synthesizing mmap event "
280 "for kernel modules\n");
281 return -1;
282 }
283
284 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200285
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800286 /*
287 * kernel uses 0 for user space maps, see kernel/perf_event.c
288 * __perf_event_mmap
289 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300290 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200291 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800292 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200293 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800294
295 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200296 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200297 size_t size;
298 struct map *pos = rb_entry(nd, struct map, rb_node);
299
300 if (pos->dso->kernel)
301 continue;
302
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300303 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200304 event->mmap.header.type = PERF_RECORD_MMAP;
305 event->mmap.header.size = (sizeof(event->mmap) -
306 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200307 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
308 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200309 event->mmap.start = pos->start;
310 event->mmap.len = pos->end - pos->start;
311 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200312
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200313 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200314 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600315 if (process(tool, event, &synth_sample, machine) != 0) {
316 rc = -1;
317 break;
318 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200319 }
320
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200321 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600322 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200323}
324
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200325static int __event__synthesize_thread(union perf_event *comm_event,
326 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400327 union perf_event *fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700328 pid_t pid, int full,
329 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200330 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300331 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200332{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500333 char filename[PATH_MAX];
334 DIR *tasks;
335 struct dirent dirent, *next;
336 pid_t tgid;
337
338 /* special case: only send one comm event using passed in pid */
339 if (!full) {
340 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
341 process, machine);
342
343 if (tgid == -1)
344 return -1;
345
346 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
347 process, machine, mmap_data);
348 }
349
350 if (machine__is_default_guest(machine))
351 return 0;
352
353 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
354 machine->root_dir, pid);
355
356 tasks = opendir(filename);
357 if (tasks == NULL) {
358 pr_debug("couldn't open %s\n", filename);
359 return 0;
360 }
361
362 while (!readdir_r(tasks, &dirent, &next) && next) {
363 char *end;
364 int rc = 0;
365 pid_t _pid;
366
367 _pid = strtol(dirent.d_name, &end, 10);
368 if (*end)
369 continue;
370
371 tgid = perf_event__synthesize_comm(tool, comm_event, _pid,
372 process, machine);
373 if (tgid == -1)
374 return -1;
375
Don Zickus363b7852014-03-14 10:43:44 -0400376 if (_pid == pid) {
377 /* process the parent's maps too */
378 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
379 process, machine, mmap_data);
380 } else {
381 /* only fork the tid's map, to save time */
382 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
383 process, machine);
384 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500385
386 if (rc)
387 return rc;
388 }
389
390 closedir(tasks);
391 return 0;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200392}
393
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200394int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200395 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200396 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300397 struct machine *machine,
398 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200399{
Don Zickus363b7852014-03-14 10:43:44 -0400400 union perf_event *comm_event, *mmap_event, *fork_event;
David Aherndefd8d32011-12-22 11:30:01 -0700401 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200402
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200403 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200404 if (comm_event == NULL)
405 goto out;
406
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200407 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200408 if (mmap_event == NULL)
409 goto out_free_comm;
410
Don Zickus363b7852014-03-14 10:43:44 -0400411 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
412 if (fork_event == NULL)
413 goto out_free_mmap;
414
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200415 err = 0;
416 for (thread = 0; thread < threads->nr; ++thread) {
417 if (__event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400418 fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700419 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300420 process, tool, machine,
421 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200422 err = -1;
423 break;
424 }
David Aherndefd8d32011-12-22 11:30:01 -0700425
426 /*
427 * comm.pid is set to thread group id by
428 * perf_event__synthesize_comm
429 */
430 if ((int) comm_event->comm.pid != threads->map[thread]) {
431 bool need_leader = true;
432
433 /* is thread group leader in thread_map? */
434 for (j = 0; j < threads->nr; ++j) {
435 if ((int) comm_event->comm.pid == threads->map[j]) {
436 need_leader = false;
437 break;
438 }
439 }
440
441 /* if not, generate events for it */
442 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300443 __event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400444 fork_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300445 comm_event->comm.pid, 0,
446 process, tool, machine,
447 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700448 err = -1;
449 break;
450 }
451 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200452 }
Don Zickus363b7852014-03-14 10:43:44 -0400453 free(fork_event);
454out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200455 free(mmap_event);
456out_free_comm:
457 free(comm_event);
458out:
459 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200460}
461
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200462int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200463 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300464 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200465{
466 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500467 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200468 struct dirent dirent, *next;
Don Zickus363b7852014-03-14 10:43:44 -0400469 union perf_event *comm_event, *mmap_event, *fork_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200470 int err = -1;
471
Namhyung Kim574799b2014-03-17 10:45:49 +0900472 if (machine__is_default_guest(machine))
473 return 0;
474
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200475 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200476 if (comm_event == NULL)
477 goto out;
478
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200479 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200480 if (mmap_event == NULL)
481 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200482
Don Zickus363b7852014-03-14 10:43:44 -0400483 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
484 if (fork_event == NULL)
485 goto out_free_mmap;
486
Dongsheng Yang99563462013-12-20 15:52:57 -0500487 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
488 proc = opendir(proc_path);
489
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200490 if (proc == NULL)
Don Zickus363b7852014-03-14 10:43:44 -0400491 goto out_free_fork;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200492
493 while (!readdir_r(proc, &dirent, &next) && next) {
494 char *end;
495 pid_t pid = strtol(dirent.d_name, &end, 10);
496
497 if (*end) /* only interested in proper numerical dirents */
498 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300499 /*
500 * We may race with exiting thread, so don't stop just because
501 * one thread couldn't be synthesized.
502 */
Don Zickus363b7852014-03-14 10:43:44 -0400503 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
504 1, process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200505 }
506
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200507 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600508 closedir(proc);
Don Zickus363b7852014-03-14 10:43:44 -0400509out_free_fork:
510 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200511out_free_mmap:
512 free(mmap_event);
513out_free_comm:
514 free(comm_event);
515out:
516 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200517}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200518
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200519struct process_symbol_args {
520 const char *name;
521 u64 start;
522};
523
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200524static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700525 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200526{
527 struct process_symbol_args *args = arg;
528
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200529 /*
530 * Must be a function or at least an alias, as in PARISC64, where "_text" is
531 * an 'A' to the same address as "_stext".
532 */
533 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
534 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200535 return 0;
536
537 args->start = start;
538 return 1;
539}
540
Adrian Hunter29b596b2014-01-29 16:14:37 +0200541u64 kallsyms__get_function_start(const char *kallsyms_filename,
542 const char *symbol_name)
543{
544 struct process_symbol_args args = { .name = symbol_name, };
545
546 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
547 return 0;
548
549 return args.start;
550}
551
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200552int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200553 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200554 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200555{
556 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200557 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800558 char name_buff[PATH_MAX];
559 struct map *map;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200560 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200561 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700562 union perf_event *event;
563
564 if (machine->vmlinux_maps[0] == NULL)
565 return -1;
566
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200567 /*
568 * We should get this from /sys/kernel/sections/.text, but till that is
569 * available use this, and after it is use this as a fallback for older
570 * kernels.
571 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700572 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200573 if (event == NULL) {
574 pr_debug("Not enough memory synthesizing mmap event "
575 "for kernel modules\n");
576 return -1;
577 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200578
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300579 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300580 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800581 /*
582 * kernel uses PERF_RECORD_MISC_USER for user space maps,
583 * see kernel/perf_event.c __perf_event_mmap
584 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200585 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800586 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200587 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100588 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200589
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300590 map = machine->vmlinux_maps[MAP__FUNCTION];
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200591 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200592 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200593 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300594 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200595 event->mmap.header.type = PERF_RECORD_MMAP;
596 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200597 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200598 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200599 event->mmap.start = map->start;
600 event->mmap.len = map->end - event->mmap.start;
601 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200602
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200603 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200604 free(event);
605
606 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200607}
608
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200609size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
610{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300611 const char *s;
612
613 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
614 s = " exec";
615 else
616 s = "";
617
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300618 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200619}
620
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300621int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200622 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200623 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200624 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200625{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200626 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200627}
628
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300629int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200630 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200631 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300632 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200633{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200634 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800635}
636
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200637size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
638{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300639 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200640 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300641 event->mmap.len, event->mmap.pgoff,
642 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
643 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200644}
645
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200646size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
647{
648 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -0400649 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200650 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
651 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
652 event->mmap2.min, event->mmap2.ino,
653 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400654 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
655 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
656 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
657 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200658 event->mmap2.filename);
659}
660
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300661int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200662 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200663 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200664 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800665{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200666 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200667}
668
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200669int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
670 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200671 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200672 struct machine *machine)
673{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200674 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200675}
676
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200677size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
678{
679 return fprintf(fp, "(%d:%d):(%d:%d)\n",
680 event->fork.pid, event->fork.tid,
681 event->fork.ppid, event->fork.ptid);
682}
683
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300684int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200685 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200686 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300687 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200688{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200689 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200690}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200691
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300692int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
693 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200694 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300695 struct machine *machine)
696{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200697 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300698}
699
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200700size_t perf_event__fprintf(union perf_event *event, FILE *fp)
701{
702 size_t ret = fprintf(fp, "PERF_RECORD_%s",
703 perf_event__name(event->header.type));
704
705 switch (event->header.type) {
706 case PERF_RECORD_COMM:
707 ret += perf_event__fprintf_comm(event, fp);
708 break;
709 case PERF_RECORD_FORK:
710 case PERF_RECORD_EXIT:
711 ret += perf_event__fprintf_task(event, fp);
712 break;
713 case PERF_RECORD_MMAP:
714 ret += perf_event__fprintf_mmap(event, fp);
715 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200716 case PERF_RECORD_MMAP2:
717 ret += perf_event__fprintf_mmap2(event, fp);
718 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200719 default:
720 ret += fprintf(fp, "\n");
721 }
722
723 return ret;
724}
725
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300726int perf_event__process(struct perf_tool *tool __maybe_unused,
727 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200728 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300729 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530730{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200731 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530732}
733
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300734void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200735 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300736 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200737{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300738 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300739 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300740 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200741
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300742 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300743 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200744 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800745 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300746 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200747
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200748 if (machine == NULL) {
749 al->map = NULL;
750 return;
751 }
752
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800753 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200754 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300755 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300756 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800757 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200758 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800759 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
760 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300761 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300762 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500763 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
764 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800765 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500766 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200767 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800768
769 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
770 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
771 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300772 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800773 if ((cpumode == PERF_RECORD_MISC_USER ||
774 cpumode == PERF_RECORD_MISC_KERNEL) &&
775 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300776 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800777
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200778 return;
779 }
780try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200781 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200782 if (al->map == NULL) {
783 /*
784 * If this is outside of all known maps, and is a negative
785 * address, try to look it up in the kernel dso, as it might be
786 * a vsyscall or vdso (which executes in user-mode).
787 *
788 * XXX This is nasty, we should have a symbol list in the
789 * "[vdso]" dso, but for now lets use the old trick of looking
790 * in the whole kernel symbol list.
791 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300792 if (cpumode == PERF_RECORD_MISC_USER && machine &&
793 mg != &machine->kmaps &&
794 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300795 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +0300796 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200797 goto try_again;
798 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300799 } else {
800 /*
801 * Kernel maps might be changed when loading symbols so loading
802 * must be done prior to using kernel maps.
803 */
804 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300805 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200806 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300807 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200808}
809
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300810void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200811 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300812 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200813{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300814 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200815 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300816 al->sym = map__find_symbol(al->map, al->addr,
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300817 thread->mg->machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200818 else
819 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200820}
821
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200822int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200823 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200824 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300825 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200826{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200827 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300828 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900829 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200830
831 if (thread == NULL)
832 return -1;
833
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200834 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300835 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200836 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300837 *
838 * This should have happened earlier, when we processed the kernel MMAP
839 * events, but for older perf.data files there was no such thing, so do
840 * it now.
841 */
842 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200843 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
844 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200845
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300846 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200847 dump_printf(" ...... dso: %s\n",
848 al->map ? al->map->dso->long_name :
849 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -0300850
851 if (thread__is_filtered(thread))
852 al->filtered |= (1 << HIST_FILTER__THREAD);
853
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300854 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200855 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200856
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300857 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900858 struct dso *dso = al->map->dso;
859
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300860 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900861 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
862 dso->short_name) ||
863 (dso->short_name != dso->long_name &&
864 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300865 dso->long_name))))) {
866 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300867 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300868
Adrian Huntere44baa32013-08-08 14:32:25 +0300869 al->sym = map__find_symbol(al->map, al->addr,
870 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300871 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200872
Feng Tang1500b932012-09-07 16:42:23 +0800873 if (symbol_conf.sym_list &&
874 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300875 al->sym->name))) {
876 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300877 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200878
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200879 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200880}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300881
882bool is_bts_event(struct perf_event_attr *attr)
883{
884 return attr->type == PERF_TYPE_HARDWARE &&
885 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
886 attr->sample_period == 1;
887}
888
889bool sample_addr_correlates_sym(struct perf_event_attr *attr)
890{
891 if (attr->type == PERF_TYPE_SOFTWARE &&
892 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
893 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
894 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
895 return true;
896
897 if (is_bts_event(attr))
898 return true;
899
900 return false;
901}
902
903void perf_event__preprocess_sample_addr(union perf_event *event,
904 struct perf_sample *sample,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300905 struct thread *thread,
906 struct addr_location *al)
907{
908 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
909
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300910 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300911 if (!al->map)
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300912 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300913 sample->addr, al);
914
915 al->cpu = sample->cpu;
916 al->sym = NULL;
917
918 if (al->map)
919 al->sym = map__find_symbol(al->map, al->addr, NULL);
920}