blob: 7ff61274ed57f5219416c69767fac9acca21e091 [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",
Adrian Hunter4a96f7a2015-04-30 17:37:29 +030026 [PERF_RECORD_AUX] = "AUX",
Adrian Hunter0ad21f62015-04-30 17:37:30 +030027 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
Kan Liangc4937a92015-05-10 15:13:15 -040028 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
Adrian Hunter02860392015-07-21 12:44:03 +030029 [PERF_RECORD_SWITCH] = "SWITCH",
30 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020031 [PERF_RECORD_HEADER_ATTR] = "ATTR",
32 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
33 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
34 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
35 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020036 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Adrian Huntera16ac022015-04-09 18:53:43 +030037 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
38 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
Adrian Huntere9bf54d2015-04-09 18:53:47 +030039 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030040};
41
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020042const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc02010-12-07 12:48:42 +000043{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020044 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc02010-12-07 12:48:42 +000045 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020046 if (!perf_event__names[id])
Thomas Gleixner3835bc02010-12-07 12:48:42 +000047 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020048 return perf_event__names[id];
Thomas Gleixner3835bc02010-12-07 12:48:42 +000049}
50
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020051static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020052 .pid = -1,
53 .tid = -1,
54 .time = -1,
55 .stream_id = -1,
56 .cpu = -1,
57 .period = 1,
58};
59
David Ahern5aa0b032015-03-30 14:35:57 -060060/*
61 * Assumes that the first 4095 bytes of /proc/pid/stat contains
David Ahernca6c41c2015-03-30 14:35:58 -060062 * the comm, tgid and ppid.
David Ahern5aa0b032015-03-30 14:35:57 -060063 */
David Ahernca6c41c2015-03-30 14:35:58 -060064static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
65 pid_t *tgid, pid_t *ppid)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020066{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020067 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -060068 char bf[4096];
69 int fd;
70 size_t size = 0, n;
David Ahernca6c41c2015-03-30 14:35:58 -060071 char *nl, *name, *tgids, *ppids;
72
73 *tgid = -1;
74 *ppid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020075
76 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
77
David Ahern5aa0b032015-03-30 14:35:57 -060078 fd = open(filename, O_RDONLY);
79 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020080 pr_debug("couldn't open %s\n", filename);
David Ahernca6c41c2015-03-30 14:35:58 -060081 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020082 }
83
David Ahern5aa0b032015-03-30 14:35:57 -060084 n = read(fd, bf, sizeof(bf) - 1);
85 close(fd);
86 if (n <= 0) {
David Ahernca6c41c2015-03-30 14:35:58 -060087 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
David Ahern5aa0b032015-03-30 14:35:57 -060088 pid);
89 return -1;
90 }
91 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020092
David Ahern5aa0b032015-03-30 14:35:57 -060093 name = strstr(bf, "Name:");
94 tgids = strstr(bf, "Tgid:");
David Ahernca6c41c2015-03-30 14:35:58 -060095 ppids = strstr(bf, "PPid:");
David Ahernf5faf722011-12-22 11:30:02 -070096
David Ahern5aa0b032015-03-30 14:35:57 -060097 if (name) {
98 name += 5; /* strlen("Name:") */
99
100 while (*name && isspace(*name))
101 ++name;
102
103 nl = strchr(name, '\n');
104 if (nl)
105 *nl = '\0';
106
107 size = strlen(name);
108 if (size >= len)
109 size = len - 1;
110 memcpy(comm, name, size);
111 comm[size] = '\0';
112 } else {
113 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200114 }
115
David Ahern5aa0b032015-03-30 14:35:57 -0600116 if (tgids) {
117 tgids += 5; /* strlen("Tgid:") */
David Ahernca6c41c2015-03-30 14:35:58 -0600118 *tgid = atoi(tgids);
David Ahern5aa0b032015-03-30 14:35:57 -0600119 } else {
120 pr_debug("Tgid: string not found for pid %d\n", pid);
121 }
David Ahernf5faf722011-12-22 11:30:02 -0700122
David Ahernca6c41c2015-03-30 14:35:58 -0600123 if (ppids) {
124 ppids += 5; /* strlen("PPid:") */
125 *ppid = atoi(ppids);
126 } else {
127 pr_debug("PPid: string not found for pid %d\n", pid);
128 }
129
130 return 0;
David Ahernf5faf722011-12-22 11:30:02 -0700131}
132
David Ahernca6c41c2015-03-30 14:35:58 -0600133static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
134 struct machine *machine,
135 pid_t *tgid, pid_t *ppid)
David Ahernf5faf722011-12-22 11:30:02 -0700136{
David Ahernf5faf722011-12-22 11:30:02 -0700137 size_t size;
David Ahernca6c41c2015-03-30 14:35:58 -0600138
139 *ppid = -1;
David Ahernf5faf722011-12-22 11:30:02 -0700140
141 memset(&event->comm, 0, sizeof(event->comm));
142
David Ahernca6c41c2015-03-30 14:35:58 -0600143 if (machine__is_host(machine)) {
144 if (perf_event__get_comm_ids(pid, event->comm.comm,
145 sizeof(event->comm.comm),
146 tgid, ppid) != 0) {
147 return -1;
148 }
149 } else {
150 *tgid = machine->pid;
151 }
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500152
David Ahernca6c41c2015-03-30 14:35:58 -0600153 if (*tgid < 0)
154 return -1;
David Ahernf5faf722011-12-22 11:30:02 -0700155
David Ahernca6c41c2015-03-30 14:35:58 -0600156 event->comm.pid = *tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200157 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700158
159 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300160 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200161 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200162 event->comm.header.size = (sizeof(event->comm) -
163 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200164 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500165 event->comm.tid = pid;
David Ahernca6c41c2015-03-30 14:35:58 -0600166
167 return 0;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300168}
169
170static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
171 union perf_event *event, pid_t pid,
172 perf_event__handler_t process,
173 struct machine *machine)
174{
David Ahernca6c41c2015-03-30 14:35:58 -0600175 pid_t tgid, ppid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300176
David Ahernca6c41c2015-03-30 14:35:58 -0600177 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
178 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200179
Don Zickusbfd66cc2014-02-26 10:45:26 -0500180 if (process(tool, event, &synth_sample, machine) != 0)
181 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600182
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200183 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200184}
185
Don Zickus363b7852014-03-14 10:43:44 -0400186static int perf_event__synthesize_fork(struct perf_tool *tool,
David Ahernca6c41c2015-03-30 14:35:58 -0600187 union perf_event *event,
188 pid_t pid, pid_t tgid, pid_t ppid,
189 perf_event__handler_t process,
Don Zickus363b7852014-03-14 10:43:44 -0400190 struct machine *machine)
191{
192 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
193
David Ahern7764a382015-04-09 12:48:27 -0400194 /*
195 * for main thread set parent to ppid from status file. For other
196 * threads set parent pid to main thread. ie., assume main thread
197 * spawns all threads in a process
198 */
199 if (tgid == pid) {
200 event->fork.ppid = ppid;
201 event->fork.ptid = ppid;
202 } else {
203 event->fork.ppid = tgid;
204 event->fork.ptid = tgid;
205 }
Don Zickus363b7852014-03-14 10:43:44 -0400206 event->fork.pid = tgid;
207 event->fork.tid = pid;
208 event->fork.header.type = PERF_RECORD_FORK;
209
210 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
211
212 if (process(tool, event, &synth_sample, machine) != 0)
213 return -1;
214
215 return 0;
216}
217
Jiri Olsaa18382b2014-01-07 13:47:20 +0100218int perf_event__synthesize_mmap_events(struct perf_tool *tool,
219 union perf_event *event,
220 pid_t pid, pid_t tgid,
221 perf_event__handler_t process,
222 struct machine *machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400223 bool mmap_data,
224 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200225{
226 char filename[PATH_MAX];
227 FILE *fp;
Kan Liang930e6fc2015-06-17 09:51:10 -0400228 unsigned long long t;
229 bool truncation = false;
Kan Liang9d9cad72015-06-17 09:51:11 -0400230 unsigned long long timeout = proc_map_timeout * 1000000ULL;
David Ahern1e6d5322012-08-26 12:24:42 -0600231 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200232
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500233 if (machine__is_default_guest(machine))
234 return 0;
235
Dongsheng Yang99563462013-12-20 15:52:57 -0500236 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
237 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200238
239 fp = fopen(filename, "r");
240 if (fp == NULL) {
241 /*
242 * We raced with a task exiting - just return:
243 */
244 pr_debug("couldn't open %s\n", filename);
245 return -1;
246 }
247
Don Zickusa5a5ba72014-05-30 10:49:42 -0400248 event->header.type = PERF_RECORD_MMAP2;
Kan Liang930e6fc2015-06-17 09:51:10 -0400249 t = rdclock();
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200250
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200251 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900252 char bf[BUFSIZ];
253 char prot[5];
254 char execname[PATH_MAX];
255 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400256 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200257 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200258 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900259
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200260 if (fgets(bf, sizeof(bf), fp) == NULL)
261 break;
262
Kan Liang9d9cad72015-06-17 09:51:11 -0400263 if ((rdclock() - t) > timeout) {
264 pr_warning("Reading %s time out. "
265 "You may want to increase "
266 "the time limit by --proc-map-timeout\n",
267 filename);
Kan Liang930e6fc2015-06-17 09:51:10 -0400268 truncation = true;
269 goto out;
270 }
271
Namhyung Kim60648032012-11-11 23:20:50 +0900272 /* ensure null termination since stack will be reused. */
273 strcpy(execname, "");
274
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200275 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400276 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
277 &event->mmap2.start, &event->mmap2.len, prot,
278 &event->mmap2.pgoff, &event->mmap2.maj,
279 &event->mmap2.min,
280 &ino, execname);
281
Don Zickus9d4ecc82013-11-13 15:32:06 -0300282 /*
283 * Anon maps don't have the execname.
284 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400285 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200286 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400287
288 event->mmap2.ino = (u64)ino;
289
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300290 /*
291 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
292 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500293 if (machine__is_host(machine))
294 event->header.misc = PERF_RECORD_MISC_USER;
295 else
296 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900297
Don Zickus7ef80702014-05-19 15:13:49 -0400298 /* map protection and flags bits */
299 event->mmap2.prot = 0;
300 event->mmap2.flags = 0;
301 if (prot[0] == 'r')
302 event->mmap2.prot |= PROT_READ;
303 if (prot[1] == 'w')
304 event->mmap2.prot |= PROT_WRITE;
305 if (prot[2] == 'x')
306 event->mmap2.prot |= PROT_EXEC;
307
308 if (prot[3] == 's')
309 event->mmap2.flags |= MAP_SHARED;
310 else
311 event->mmap2.flags |= MAP_PRIVATE;
312
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300313 if (prot[2] != 'x') {
314 if (!mmap_data || prot[0] != 'r')
315 continue;
316
317 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
318 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200319
Kan Liang930e6fc2015-06-17 09:51:10 -0400320out:
321 if (truncation)
322 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
323
Namhyung Kim60648032012-11-11 23:20:50 +0900324 if (!strcmp(execname, ""))
325 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200326
Namhyung Kim60648032012-11-11 23:20:50 +0900327 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400328 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900329 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400330 event->mmap2.len -= event->mmap.start;
331 event->mmap2.header.size = (sizeof(event->mmap2) -
332 (sizeof(event->mmap2.filename) - size));
333 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
334 event->mmap2.header.size += machine->id_hdr_size;
335 event->mmap2.pid = tgid;
336 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000337
Namhyung Kim60648032012-11-11 23:20:50 +0900338 if (process(tool, event, &synth_sample, machine) != 0) {
339 rc = -1;
340 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200341 }
Kan Liang930e6fc2015-06-17 09:51:10 -0400342
343 if (truncation)
344 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200345 }
346
347 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600348 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200349}
350
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200351int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200352 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200353 struct machine *machine)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200354{
David Ahern1e6d5322012-08-26 12:24:42 -0600355 int rc = 0;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300356 struct map *pos;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300357 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300358 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200359 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200360 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200361 if (event == NULL) {
362 pr_debug("Not enough memory synthesizing mmap event "
363 "for kernel modules\n");
364 return -1;
365 }
366
367 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200368
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800369 /*
370 * kernel uses 0 for user space maps, see kernel/perf_event.c
371 * __perf_event_mmap
372 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300373 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200374 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800375 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200376 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800377
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300378 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200379 size_t size;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200380
381 if (pos->dso->kernel)
382 continue;
383
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300384 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200385 event->mmap.header.type = PERF_RECORD_MMAP;
386 event->mmap.header.size = (sizeof(event->mmap) -
387 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200388 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
389 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200390 event->mmap.start = pos->start;
391 event->mmap.len = pos->end - pos->start;
392 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200393
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200394 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200395 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600396 if (process(tool, event, &synth_sample, machine) != 0) {
397 rc = -1;
398 break;
399 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200400 }
401
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200402 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600403 return rc;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200404}
405
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200406static int __event__synthesize_thread(union perf_event *comm_event,
407 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400408 union perf_event *fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700409 pid_t pid, int full,
410 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200411 struct perf_tool *tool,
Kan Liang9d9cad72015-06-17 09:51:11 -0400412 struct machine *machine,
413 bool mmap_data,
414 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200415{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500416 char filename[PATH_MAX];
417 DIR *tasks;
418 struct dirent dirent, *next;
David Ahernca6c41c2015-03-30 14:35:58 -0600419 pid_t tgid, ppid;
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300420 int rc = 0;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500421
422 /* special case: only send one comm event using passed in pid */
423 if (!full) {
424 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
425 process, machine);
426
427 if (tgid == -1)
428 return -1;
429
430 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400431 process, machine, mmap_data,
432 proc_map_timeout);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500433 }
434
435 if (machine__is_default_guest(machine))
436 return 0;
437
438 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
439 machine->root_dir, pid);
440
441 tasks = opendir(filename);
442 if (tasks == NULL) {
443 pr_debug("couldn't open %s\n", filename);
444 return 0;
445 }
446
447 while (!readdir_r(tasks, &dirent, &next) && next) {
448 char *end;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500449 pid_t _pid;
450
451 _pid = strtol(dirent.d_name, &end, 10);
452 if (*end)
453 continue;
454
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300455 rc = -1;
David Ahernca6c41c2015-03-30 14:35:58 -0600456 if (perf_event__prepare_comm(comm_event, _pid, machine,
457 &tgid, &ppid) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300458 break;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500459
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300460 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
David Ahernca6c41c2015-03-30 14:35:58 -0600461 ppid, process, machine) < 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300462 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300463 /*
464 * Send the prepared comm event
465 */
466 if (process(tool, comm_event, &synth_sample, machine) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300467 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300468
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300469 rc = 0;
Don Zickus363b7852014-03-14 10:43:44 -0400470 if (_pid == pid) {
471 /* process the parent's maps too */
472 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400473 process, machine, mmap_data, proc_map_timeout);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300474 if (rc)
475 break;
Don Zickus363b7852014-03-14 10:43:44 -0400476 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500477 }
478
479 closedir(tasks);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300480 return rc;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200481}
482
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200483int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200484 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200485 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300486 struct machine *machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400487 bool mmap_data,
488 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200489{
Don Zickus363b7852014-03-14 10:43:44 -0400490 union perf_event *comm_event, *mmap_event, *fork_event;
David Aherndefd8d32011-12-22 11:30:01 -0700491 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200492
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200493 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200494 if (comm_event == NULL)
495 goto out;
496
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200497 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200498 if (mmap_event == NULL)
499 goto out_free_comm;
500
Don Zickus363b7852014-03-14 10:43:44 -0400501 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
502 if (fork_event == NULL)
503 goto out_free_mmap;
504
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200505 err = 0;
506 for (thread = 0; thread < threads->nr; ++thread) {
507 if (__event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400508 fork_event,
Jiri Olsae13798c2015-06-23 00:36:02 +0200509 thread_map__pid(threads, thread), 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300510 process, tool, machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400511 mmap_data, proc_map_timeout)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200512 err = -1;
513 break;
514 }
David Aherndefd8d32011-12-22 11:30:01 -0700515
516 /*
517 * comm.pid is set to thread group id by
518 * perf_event__synthesize_comm
519 */
Jiri Olsae13798c2015-06-23 00:36:02 +0200520 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
David Aherndefd8d32011-12-22 11:30:01 -0700521 bool need_leader = true;
522
523 /* is thread group leader in thread_map? */
524 for (j = 0; j < threads->nr; ++j) {
Jiri Olsae13798c2015-06-23 00:36:02 +0200525 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
David Aherndefd8d32011-12-22 11:30:01 -0700526 need_leader = false;
527 break;
528 }
529 }
530
531 /* if not, generate events for it */
532 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300533 __event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400534 fork_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300535 comm_event->comm.pid, 0,
536 process, tool, machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400537 mmap_data, proc_map_timeout)) {
David Aherndefd8d32011-12-22 11:30:01 -0700538 err = -1;
539 break;
540 }
541 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200542 }
Don Zickus363b7852014-03-14 10:43:44 -0400543 free(fork_event);
544out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200545 free(mmap_event);
546out_free_comm:
547 free(comm_event);
548out:
549 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200550}
551
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200552int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200553 perf_event__handler_t process,
Kan Liang9d9cad72015-06-17 09:51:11 -0400554 struct machine *machine,
555 bool mmap_data,
556 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200557{
558 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500559 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200560 struct dirent dirent, *next;
Don Zickus363b7852014-03-14 10:43:44 -0400561 union perf_event *comm_event, *mmap_event, *fork_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200562 int err = -1;
563
Namhyung Kim574799b2014-03-17 10:45:49 +0900564 if (machine__is_default_guest(machine))
565 return 0;
566
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200567 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200568 if (comm_event == NULL)
569 goto out;
570
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200571 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200572 if (mmap_event == NULL)
573 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200574
Don Zickus363b7852014-03-14 10:43:44 -0400575 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
576 if (fork_event == NULL)
577 goto out_free_mmap;
578
Dongsheng Yang99563462013-12-20 15:52:57 -0500579 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
580 proc = opendir(proc_path);
581
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200582 if (proc == NULL)
Don Zickus363b7852014-03-14 10:43:44 -0400583 goto out_free_fork;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200584
585 while (!readdir_r(proc, &dirent, &next) && next) {
586 char *end;
587 pid_t pid = strtol(dirent.d_name, &end, 10);
588
589 if (*end) /* only interested in proper numerical dirents */
590 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300591 /*
592 * We may race with exiting thread, so don't stop just because
593 * one thread couldn't be synthesized.
594 */
Don Zickus363b7852014-03-14 10:43:44 -0400595 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400596 1, process, tool, machine, mmap_data,
597 proc_map_timeout);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200598 }
599
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200600 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600601 closedir(proc);
Don Zickus363b7852014-03-14 10:43:44 -0400602out_free_fork:
603 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200604out_free_mmap:
605 free(mmap_event);
606out_free_comm:
607 free(comm_event);
608out:
609 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200610}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200611
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200612struct process_symbol_args {
613 const char *name;
614 u64 start;
615};
616
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200617static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700618 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200619{
620 struct process_symbol_args *args = arg;
621
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200622 /*
623 * Must be a function or at least an alias, as in PARISC64, where "_text" is
624 * an 'A' to the same address as "_stext".
625 */
626 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
627 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200628 return 0;
629
630 args->start = start;
631 return 1;
632}
633
Adrian Hunter29b596b2014-01-29 16:14:37 +0200634u64 kallsyms__get_function_start(const char *kallsyms_filename,
635 const char *symbol_name)
636{
637 struct process_symbol_args args = { .name = symbol_name, };
638
639 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
640 return 0;
641
642 return args.start;
643}
644
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200645int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200646 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200647 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200648{
649 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200650 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800651 char name_buff[PATH_MAX];
652 struct map *map;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200653 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200654 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700655 union perf_event *event;
656
657 if (machine->vmlinux_maps[0] == NULL)
658 return -1;
659
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200660 /*
661 * We should get this from /sys/kernel/sections/.text, but till that is
662 * available use this, and after it is use this as a fallback for older
663 * kernels.
664 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700665 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200666 if (event == NULL) {
667 pr_debug("Not enough memory synthesizing mmap event "
668 "for kernel modules\n");
669 return -1;
670 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200671
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300672 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300673 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800674 /*
675 * kernel uses PERF_RECORD_MISC_USER for user space maps,
676 * see kernel/perf_event.c __perf_event_mmap
677 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200678 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800679 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200680 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100681 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200682
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300683 map = machine->vmlinux_maps[MAP__FUNCTION];
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200684 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200685 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200686 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300687 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200688 event->mmap.header.type = PERF_RECORD_MMAP;
689 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200690 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200691 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200692 event->mmap.start = map->start;
693 event->mmap.len = map->end - event->mmap.start;
694 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200695
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200696 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200697 free(event);
698
699 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200700}
701
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200702size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
703{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300704 const char *s;
705
706 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
707 s = " exec";
708 else
709 s = "";
710
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300711 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 -0200712}
713
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300714int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200715 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200716 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200717 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200718{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200719 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200720}
721
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300722int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200723 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200724 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300725 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200726{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200727 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800728}
729
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300730int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
731 union perf_event *event,
732 struct perf_sample *sample __maybe_unused,
733 struct machine *machine)
734{
735 return machine__process_aux_event(machine, event);
736}
737
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300738int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
739 union perf_event *event,
740 struct perf_sample *sample __maybe_unused,
741 struct machine *machine)
742{
743 return machine__process_itrace_start_event(machine, event);
744}
745
Kan Liangc4937a92015-05-10 15:13:15 -0400746int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
747 union perf_event *event,
748 struct perf_sample *sample,
749 struct machine *machine)
750{
751 return machine__process_lost_samples_event(machine, event, sample);
752}
753
Adrian Hunter02860392015-07-21 12:44:03 +0300754int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
755 union perf_event *event,
756 struct perf_sample *sample __maybe_unused,
757 struct machine *machine)
758{
759 return machine__process_switch_event(machine, event);
760}
761
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200762size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
763{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300764 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200765 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300766 event->mmap.len, event->mmap.pgoff,
767 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
768 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200769}
770
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200771size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
772{
773 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -0400774 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200775 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
776 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
777 event->mmap2.min, event->mmap2.ino,
778 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400779 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
780 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
781 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
782 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200783 event->mmap2.filename);
784}
785
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300786int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200787 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200788 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200789 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800790{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200791 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200792}
793
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200794int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
795 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200796 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200797 struct machine *machine)
798{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200799 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200800}
801
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200802size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
803{
804 return fprintf(fp, "(%d:%d):(%d:%d)\n",
805 event->fork.pid, event->fork.tid,
806 event->fork.ppid, event->fork.ptid);
807}
808
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300809int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200810 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200811 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300812 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200813{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200814 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200815}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200816
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300817int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
818 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200819 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300820 struct machine *machine)
821{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200822 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300823}
824
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300825size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
826{
827 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
828 event->aux.aux_offset, event->aux.aux_size,
829 event->aux.flags,
830 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
831 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
832}
833
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300834size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
835{
836 return fprintf(fp, " pid: %u tid: %u\n",
837 event->itrace_start.pid, event->itrace_start.tid);
838}
839
Adrian Hunter02860392015-07-21 12:44:03 +0300840size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
841{
842 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
843 const char *in_out = out ? "OUT" : "IN ";
844
845 if (event->header.type == PERF_RECORD_SWITCH)
846 return fprintf(fp, " %s\n", in_out);
847
848 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
849 in_out, out ? "next" : "prev",
850 event->context_switch.next_prev_pid,
851 event->context_switch.next_prev_tid);
852}
853
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200854size_t perf_event__fprintf(union perf_event *event, FILE *fp)
855{
856 size_t ret = fprintf(fp, "PERF_RECORD_%s",
857 perf_event__name(event->header.type));
858
859 switch (event->header.type) {
860 case PERF_RECORD_COMM:
861 ret += perf_event__fprintf_comm(event, fp);
862 break;
863 case PERF_RECORD_FORK:
864 case PERF_RECORD_EXIT:
865 ret += perf_event__fprintf_task(event, fp);
866 break;
867 case PERF_RECORD_MMAP:
868 ret += perf_event__fprintf_mmap(event, fp);
869 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200870 case PERF_RECORD_MMAP2:
871 ret += perf_event__fprintf_mmap2(event, fp);
872 break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300873 case PERF_RECORD_AUX:
874 ret += perf_event__fprintf_aux(event, fp);
875 break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300876 case PERF_RECORD_ITRACE_START:
877 ret += perf_event__fprintf_itrace_start(event, fp);
878 break;
Adrian Hunter02860392015-07-21 12:44:03 +0300879 case PERF_RECORD_SWITCH:
880 case PERF_RECORD_SWITCH_CPU_WIDE:
881 ret += perf_event__fprintf_switch(event, fp);
882 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200883 default:
884 ret += fprintf(fp, "\n");
885 }
886
887 return ret;
888}
889
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300890int perf_event__process(struct perf_tool *tool __maybe_unused,
891 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200892 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300893 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530894{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200895 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530896}
897
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300898void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200899 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300900 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200901{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300902 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300903 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300904 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200905
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300906 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300907 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200908 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800909 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300910 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200911
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200912 if (machine == NULL) {
913 al->map = NULL;
914 return;
915 }
916
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800917 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200918 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300919 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300920 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800921 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200922 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800923 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
924 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300925 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300926 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500927 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
928 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800929 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500930 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200931 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800932
933 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
934 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
935 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300936 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800937 if ((cpumode == PERF_RECORD_MISC_USER ||
938 cpumode == PERF_RECORD_MISC_KERNEL) &&
939 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300940 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800941
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200942 return;
943 }
944try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200945 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200946 if (al->map == NULL) {
947 /*
948 * If this is outside of all known maps, and is a negative
949 * address, try to look it up in the kernel dso, as it might be
950 * a vsyscall or vdso (which executes in user-mode).
951 *
952 * XXX This is nasty, we should have a symbol list in the
953 * "[vdso]" dso, but for now lets use the old trick of looking
954 * in the whole kernel symbol list.
955 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300956 if (cpumode == PERF_RECORD_MISC_USER && machine &&
957 mg != &machine->kmaps &&
958 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300959 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +0300960 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200961 goto try_again;
962 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300963 } else {
964 /*
965 * Kernel maps might be changed when loading symbols so loading
966 * must be done prior to using kernel maps.
967 */
968 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300969 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200970 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300971 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200972}
973
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300974void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200975 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300976 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200977{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300978 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200979 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300980 al->sym = map__find_symbol(al->map, al->addr,
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300981 thread->mg->machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200982 else
983 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200984}
985
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300986/*
987 * Callers need to drop the reference to al->thread, obtained in
988 * machine__findnew_thread()
989 */
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200990int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200991 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200992 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300993 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200994{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200995 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300996 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900997 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200998
999 if (thread == NULL)
1000 return -1;
1001
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001002 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001003 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001004 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001005 *
1006 * This should have happened earlier, when we processed the kernel MMAP
1007 * events, but for older perf.data files there was no such thing, so do
1008 * it now.
1009 */
1010 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001011 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
1012 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001013
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001014 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001015 dump_printf(" ...... dso: %s\n",
1016 al->map ? al->map->dso->long_name :
1017 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -03001018
1019 if (thread__is_filtered(thread))
1020 al->filtered |= (1 << HIST_FILTER__THREAD);
1021
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001022 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -02001023 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001024
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001025 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001026 struct dso *dso = al->map->dso;
1027
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001028 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001029 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1030 dso->short_name) ||
1031 (dso->short_name != dso->long_name &&
1032 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001033 dso->long_name))))) {
1034 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001035 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001036
Adrian Huntere44baa32013-08-08 14:32:25 +03001037 al->sym = map__find_symbol(al->map, al->addr,
1038 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001039 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001040
Feng Tang1500b932012-09-07 16:42:23 +08001041 if (symbol_conf.sym_list &&
1042 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001043 al->sym->name))) {
1044 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001045 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001046
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001047 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001048}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001049
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001050/*
1051 * The preprocess_sample method will return with reference counts for the
1052 * in it, when done using (and perhaps getting ref counts if needing to
1053 * keep a pointer to one of those entries) it must be paired with
1054 * addr_location__put(), so that the refcounts can be decremented.
1055 */
1056void addr_location__put(struct addr_location *al)
1057{
1058 thread__zput(al->thread);
1059}
1060
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001061bool is_bts_event(struct perf_event_attr *attr)
1062{
1063 return attr->type == PERF_TYPE_HARDWARE &&
1064 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1065 attr->sample_period == 1;
1066}
1067
1068bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1069{
1070 if (attr->type == PERF_TYPE_SOFTWARE &&
1071 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1072 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1073 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1074 return true;
1075
1076 if (is_bts_event(attr))
1077 return true;
1078
1079 return false;
1080}
1081
1082void perf_event__preprocess_sample_addr(union perf_event *event,
1083 struct perf_sample *sample,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001084 struct thread *thread,
1085 struct addr_location *al)
1086{
1087 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1088
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001089 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001090 if (!al->map)
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001091 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001092 sample->addr, al);
1093
1094 al->cpu = sample->cpu;
1095 al->sym = NULL;
1096
1097 if (al->map)
1098 al->sym = map__find_symbol(al->map, al->addr, NULL);
1099}