blob: 67a977e5d0abee03a97eb74f914f9329163a6ca7 [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",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020029 [PERF_RECORD_HEADER_ATTR] = "ATTR",
30 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
31 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
32 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
33 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020034 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Adrian Huntera16ac022015-04-09 18:53:43 +030035 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
36 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
Adrian Huntere9bf54d2015-04-09 18:53:47 +030037 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030038};
39
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020040const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000041{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020042 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000043 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020044 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000045 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020046 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000047}
48
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020049static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020050 .pid = -1,
51 .tid = -1,
52 .time = -1,
53 .stream_id = -1,
54 .cpu = -1,
55 .period = 1,
56};
57
David Ahern5aa0b032015-03-30 14:35:57 -060058/*
59 * Assumes that the first 4095 bytes of /proc/pid/stat contains
David Ahernca6c41c2015-03-30 14:35:58 -060060 * the comm, tgid and ppid.
David Ahern5aa0b032015-03-30 14:35:57 -060061 */
David Ahernca6c41c2015-03-30 14:35:58 -060062static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
63 pid_t *tgid, pid_t *ppid)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020064{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020065 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -060066 char bf[4096];
67 int fd;
68 size_t size = 0, n;
David Ahernca6c41c2015-03-30 14:35:58 -060069 char *nl, *name, *tgids, *ppids;
70
71 *tgid = -1;
72 *ppid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020073
74 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
75
David Ahern5aa0b032015-03-30 14:35:57 -060076 fd = open(filename, O_RDONLY);
77 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020078 pr_debug("couldn't open %s\n", filename);
David Ahernca6c41c2015-03-30 14:35:58 -060079 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020080 }
81
David Ahern5aa0b032015-03-30 14:35:57 -060082 n = read(fd, bf, sizeof(bf) - 1);
83 close(fd);
84 if (n <= 0) {
David Ahernca6c41c2015-03-30 14:35:58 -060085 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
David Ahern5aa0b032015-03-30 14:35:57 -060086 pid);
87 return -1;
88 }
89 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020090
David Ahern5aa0b032015-03-30 14:35:57 -060091 name = strstr(bf, "Name:");
92 tgids = strstr(bf, "Tgid:");
David Ahernca6c41c2015-03-30 14:35:58 -060093 ppids = strstr(bf, "PPid:");
David Ahernf5faf722011-12-22 11:30:02 -070094
David Ahern5aa0b032015-03-30 14:35:57 -060095 if (name) {
96 name += 5; /* strlen("Name:") */
97
98 while (*name && isspace(*name))
99 ++name;
100
101 nl = strchr(name, '\n');
102 if (nl)
103 *nl = '\0';
104
105 size = strlen(name);
106 if (size >= len)
107 size = len - 1;
108 memcpy(comm, name, size);
109 comm[size] = '\0';
110 } else {
111 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200112 }
113
David Ahern5aa0b032015-03-30 14:35:57 -0600114 if (tgids) {
115 tgids += 5; /* strlen("Tgid:") */
David Ahernca6c41c2015-03-30 14:35:58 -0600116 *tgid = atoi(tgids);
David Ahern5aa0b032015-03-30 14:35:57 -0600117 } else {
118 pr_debug("Tgid: string not found for pid %d\n", pid);
119 }
David Ahernf5faf722011-12-22 11:30:02 -0700120
David Ahernca6c41c2015-03-30 14:35:58 -0600121 if (ppids) {
122 ppids += 5; /* strlen("PPid:") */
123 *ppid = atoi(ppids);
124 } else {
125 pr_debug("PPid: string not found for pid %d\n", pid);
126 }
127
128 return 0;
David Ahernf5faf722011-12-22 11:30:02 -0700129}
130
David Ahernca6c41c2015-03-30 14:35:58 -0600131static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
132 struct machine *machine,
133 pid_t *tgid, pid_t *ppid)
David Ahernf5faf722011-12-22 11:30:02 -0700134{
David Ahernf5faf722011-12-22 11:30:02 -0700135 size_t size;
David Ahernca6c41c2015-03-30 14:35:58 -0600136
137 *ppid = -1;
David Ahernf5faf722011-12-22 11:30:02 -0700138
139 memset(&event->comm, 0, sizeof(event->comm));
140
David Ahernca6c41c2015-03-30 14:35:58 -0600141 if (machine__is_host(machine)) {
142 if (perf_event__get_comm_ids(pid, event->comm.comm,
143 sizeof(event->comm.comm),
144 tgid, ppid) != 0) {
145 return -1;
146 }
147 } else {
148 *tgid = machine->pid;
149 }
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500150
David Ahernca6c41c2015-03-30 14:35:58 -0600151 if (*tgid < 0)
152 return -1;
David Ahernf5faf722011-12-22 11:30:02 -0700153
David Ahernca6c41c2015-03-30 14:35:58 -0600154 event->comm.pid = *tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200155 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700156
157 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300158 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200159 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200160 event->comm.header.size = (sizeof(event->comm) -
161 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200162 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500163 event->comm.tid = pid;
David Ahernca6c41c2015-03-30 14:35:58 -0600164
165 return 0;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300166}
167
168static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
169 union perf_event *event, pid_t pid,
170 perf_event__handler_t process,
171 struct machine *machine)
172{
David Ahernca6c41c2015-03-30 14:35:58 -0600173 pid_t tgid, ppid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300174
David Ahernca6c41c2015-03-30 14:35:58 -0600175 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
176 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200177
Don Zickusbfd66cc2014-02-26 10:45:26 -0500178 if (process(tool, event, &synth_sample, machine) != 0)
179 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600180
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200181 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200182}
183
Don Zickus363b7852014-03-14 10:43:44 -0400184static int perf_event__synthesize_fork(struct perf_tool *tool,
David Ahernca6c41c2015-03-30 14:35:58 -0600185 union perf_event *event,
186 pid_t pid, pid_t tgid, pid_t ppid,
187 perf_event__handler_t process,
Don Zickus363b7852014-03-14 10:43:44 -0400188 struct machine *machine)
189{
190 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
191
David Ahern7764a382015-04-09 12:48:27 -0400192 /*
193 * for main thread set parent to ppid from status file. For other
194 * threads set parent pid to main thread. ie., assume main thread
195 * spawns all threads in a process
196 */
197 if (tgid == pid) {
198 event->fork.ppid = ppid;
199 event->fork.ptid = ppid;
200 } else {
201 event->fork.ppid = tgid;
202 event->fork.ptid = tgid;
203 }
Don Zickus363b7852014-03-14 10:43:44 -0400204 event->fork.pid = tgid;
205 event->fork.tid = pid;
206 event->fork.header.type = PERF_RECORD_FORK;
207
208 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
209
210 if (process(tool, event, &synth_sample, machine) != 0)
211 return -1;
212
213 return 0;
214}
215
Jiri Olsaa18382b2014-01-07 13:47:20 +0100216int perf_event__synthesize_mmap_events(struct perf_tool *tool,
217 union perf_event *event,
218 pid_t pid, pid_t tgid,
219 perf_event__handler_t process,
220 struct machine *machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400221 bool mmap_data,
222 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200223{
224 char filename[PATH_MAX];
225 FILE *fp;
Kan Liang930e6fc2015-06-17 09:51:10 -0400226 unsigned long long t;
227 bool truncation = false;
Kan Liang9d9cad72015-06-17 09:51:11 -0400228 unsigned long long timeout = proc_map_timeout * 1000000ULL;
David Ahern1e6d5322012-08-26 12:24:42 -0600229 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200230
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500231 if (machine__is_default_guest(machine))
232 return 0;
233
Dongsheng Yang99563462013-12-20 15:52:57 -0500234 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
235 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200236
237 fp = fopen(filename, "r");
238 if (fp == NULL) {
239 /*
240 * We raced with a task exiting - just return:
241 */
242 pr_debug("couldn't open %s\n", filename);
243 return -1;
244 }
245
Don Zickusa5a5ba72014-05-30 10:49:42 -0400246 event->header.type = PERF_RECORD_MMAP2;
Kan Liang930e6fc2015-06-17 09:51:10 -0400247 t = rdclock();
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200248
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200249 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900250 char bf[BUFSIZ];
251 char prot[5];
252 char execname[PATH_MAX];
253 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400254 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200255 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200256 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900257
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200258 if (fgets(bf, sizeof(bf), fp) == NULL)
259 break;
260
Kan Liang9d9cad72015-06-17 09:51:11 -0400261 if ((rdclock() - t) > timeout) {
262 pr_warning("Reading %s time out. "
263 "You may want to increase "
264 "the time limit by --proc-map-timeout\n",
265 filename);
Kan Liang930e6fc2015-06-17 09:51:10 -0400266 truncation = true;
267 goto out;
268 }
269
Namhyung Kim60648032012-11-11 23:20:50 +0900270 /* ensure null termination since stack will be reused. */
271 strcpy(execname, "");
272
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200273 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400274 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
275 &event->mmap2.start, &event->mmap2.len, prot,
276 &event->mmap2.pgoff, &event->mmap2.maj,
277 &event->mmap2.min,
278 &ino, execname);
279
Don Zickus9d4ecc82013-11-13 15:32:06 -0300280 /*
281 * Anon maps don't have the execname.
282 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400283 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200284 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400285
286 event->mmap2.ino = (u64)ino;
287
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300288 /*
289 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
290 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500291 if (machine__is_host(machine))
292 event->header.misc = PERF_RECORD_MISC_USER;
293 else
294 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900295
Don Zickus7ef80702014-05-19 15:13:49 -0400296 /* map protection and flags bits */
297 event->mmap2.prot = 0;
298 event->mmap2.flags = 0;
299 if (prot[0] == 'r')
300 event->mmap2.prot |= PROT_READ;
301 if (prot[1] == 'w')
302 event->mmap2.prot |= PROT_WRITE;
303 if (prot[2] == 'x')
304 event->mmap2.prot |= PROT_EXEC;
305
306 if (prot[3] == 's')
307 event->mmap2.flags |= MAP_SHARED;
308 else
309 event->mmap2.flags |= MAP_PRIVATE;
310
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300311 if (prot[2] != 'x') {
312 if (!mmap_data || prot[0] != 'r')
313 continue;
314
315 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
316 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200317
Kan Liang930e6fc2015-06-17 09:51:10 -0400318out:
319 if (truncation)
320 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
321
Namhyung Kim60648032012-11-11 23:20:50 +0900322 if (!strcmp(execname, ""))
323 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200324
Namhyung Kim60648032012-11-11 23:20:50 +0900325 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400326 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900327 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400328 event->mmap2.len -= event->mmap.start;
329 event->mmap2.header.size = (sizeof(event->mmap2) -
330 (sizeof(event->mmap2.filename) - size));
331 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
332 event->mmap2.header.size += machine->id_hdr_size;
333 event->mmap2.pid = tgid;
334 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000335
Namhyung Kim60648032012-11-11 23:20:50 +0900336 if (process(tool, event, &synth_sample, machine) != 0) {
337 rc = -1;
338 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200339 }
Kan Liang930e6fc2015-06-17 09:51:10 -0400340
341 if (truncation)
342 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200343 }
344
345 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600346 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200347}
348
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200349int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200350 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200351 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200352{
David Ahern1e6d5322012-08-26 12:24:42 -0600353 int rc = 0;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300354 struct map *pos;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300355 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300356 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200357 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200358 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200359 if (event == NULL) {
360 pr_debug("Not enough memory synthesizing mmap event "
361 "for kernel modules\n");
362 return -1;
363 }
364
365 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200366
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800367 /*
368 * kernel uses 0 for user space maps, see kernel/perf_event.c
369 * __perf_event_mmap
370 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300371 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200372 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800373 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200374 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800375
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300376 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200377 size_t size;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200378
379 if (pos->dso->kernel)
380 continue;
381
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300382 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200383 event->mmap.header.type = PERF_RECORD_MMAP;
384 event->mmap.header.size = (sizeof(event->mmap) -
385 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200386 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
387 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200388 event->mmap.start = pos->start;
389 event->mmap.len = pos->end - pos->start;
390 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200391
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200392 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200393 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600394 if (process(tool, event, &synth_sample, machine) != 0) {
395 rc = -1;
396 break;
397 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200398 }
399
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200400 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600401 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200402}
403
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200404static int __event__synthesize_thread(union perf_event *comm_event,
405 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400406 union perf_event *fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700407 pid_t pid, int full,
408 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200409 struct perf_tool *tool,
Kan Liang9d9cad72015-06-17 09:51:11 -0400410 struct machine *machine,
411 bool mmap_data,
412 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200413{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500414 char filename[PATH_MAX];
415 DIR *tasks;
416 struct dirent dirent, *next;
David Ahernca6c41c2015-03-30 14:35:58 -0600417 pid_t tgid, ppid;
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300418 int rc = 0;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500419
420 /* special case: only send one comm event using passed in pid */
421 if (!full) {
422 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
423 process, machine);
424
425 if (tgid == -1)
426 return -1;
427
428 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400429 process, machine, mmap_data,
430 proc_map_timeout);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500431 }
432
433 if (machine__is_default_guest(machine))
434 return 0;
435
436 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
437 machine->root_dir, pid);
438
439 tasks = opendir(filename);
440 if (tasks == NULL) {
441 pr_debug("couldn't open %s\n", filename);
442 return 0;
443 }
444
445 while (!readdir_r(tasks, &dirent, &next) && next) {
446 char *end;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500447 pid_t _pid;
448
449 _pid = strtol(dirent.d_name, &end, 10);
450 if (*end)
451 continue;
452
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300453 rc = -1;
David Ahernca6c41c2015-03-30 14:35:58 -0600454 if (perf_event__prepare_comm(comm_event, _pid, machine,
455 &tgid, &ppid) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300456 break;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500457
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300458 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
David Ahernca6c41c2015-03-30 14:35:58 -0600459 ppid, process, machine) < 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300460 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300461 /*
462 * Send the prepared comm event
463 */
464 if (process(tool, comm_event, &synth_sample, machine) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300465 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300466
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300467 rc = 0;
Don Zickus363b7852014-03-14 10:43:44 -0400468 if (_pid == pid) {
469 /* process the parent's maps too */
470 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400471 process, machine, mmap_data, proc_map_timeout);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300472 if (rc)
473 break;
Don Zickus363b7852014-03-14 10:43:44 -0400474 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500475 }
476
477 closedir(tasks);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300478 return rc;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200479}
480
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200481int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200482 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200483 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300484 struct machine *machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400485 bool mmap_data,
486 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200487{
Don Zickus363b7852014-03-14 10:43:44 -0400488 union perf_event *comm_event, *mmap_event, *fork_event;
David Aherndefd8d32011-12-22 11:30:01 -0700489 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200490
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200491 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200492 if (comm_event == NULL)
493 goto out;
494
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200495 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200496 if (mmap_event == NULL)
497 goto out_free_comm;
498
Don Zickus363b7852014-03-14 10:43:44 -0400499 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
500 if (fork_event == NULL)
501 goto out_free_mmap;
502
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200503 err = 0;
504 for (thread = 0; thread < threads->nr; ++thread) {
505 if (__event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400506 fork_event,
Jiri Olsae13798c2015-06-23 00:36:02 +0200507 thread_map__pid(threads, thread), 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300508 process, tool, machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400509 mmap_data, proc_map_timeout)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200510 err = -1;
511 break;
512 }
David Aherndefd8d32011-12-22 11:30:01 -0700513
514 /*
515 * comm.pid is set to thread group id by
516 * perf_event__synthesize_comm
517 */
Jiri Olsae13798c2015-06-23 00:36:02 +0200518 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
David Aherndefd8d32011-12-22 11:30:01 -0700519 bool need_leader = true;
520
521 /* is thread group leader in thread_map? */
522 for (j = 0; j < threads->nr; ++j) {
Jiri Olsae13798c2015-06-23 00:36:02 +0200523 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
David Aherndefd8d32011-12-22 11:30:01 -0700524 need_leader = false;
525 break;
526 }
527 }
528
529 /* if not, generate events for it */
530 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300531 __event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400532 fork_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300533 comm_event->comm.pid, 0,
534 process, tool, machine,
Kan Liang9d9cad72015-06-17 09:51:11 -0400535 mmap_data, proc_map_timeout)) {
David Aherndefd8d32011-12-22 11:30:01 -0700536 err = -1;
537 break;
538 }
539 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200540 }
Don Zickus363b7852014-03-14 10:43:44 -0400541 free(fork_event);
542out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200543 free(mmap_event);
544out_free_comm:
545 free(comm_event);
546out:
547 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200548}
549
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200550int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200551 perf_event__handler_t process,
Kan Liang9d9cad72015-06-17 09:51:11 -0400552 struct machine *machine,
553 bool mmap_data,
554 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200555{
556 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500557 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200558 struct dirent dirent, *next;
Don Zickus363b7852014-03-14 10:43:44 -0400559 union perf_event *comm_event, *mmap_event, *fork_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200560 int err = -1;
561
Namhyung Kim574799b2014-03-17 10:45:49 +0900562 if (machine__is_default_guest(machine))
563 return 0;
564
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200565 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200566 if (comm_event == NULL)
567 goto out;
568
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200569 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200570 if (mmap_event == NULL)
571 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200572
Don Zickus363b7852014-03-14 10:43:44 -0400573 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
574 if (fork_event == NULL)
575 goto out_free_mmap;
576
Dongsheng Yang99563462013-12-20 15:52:57 -0500577 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
578 proc = opendir(proc_path);
579
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200580 if (proc == NULL)
Don Zickus363b7852014-03-14 10:43:44 -0400581 goto out_free_fork;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200582
583 while (!readdir_r(proc, &dirent, &next) && next) {
584 char *end;
585 pid_t pid = strtol(dirent.d_name, &end, 10);
586
587 if (*end) /* only interested in proper numerical dirents */
588 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300589 /*
590 * We may race with exiting thread, so don't stop just because
591 * one thread couldn't be synthesized.
592 */
Don Zickus363b7852014-03-14 10:43:44 -0400593 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
Kan Liang9d9cad72015-06-17 09:51:11 -0400594 1, process, tool, machine, mmap_data,
595 proc_map_timeout);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200596 }
597
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200598 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600599 closedir(proc);
Don Zickus363b7852014-03-14 10:43:44 -0400600out_free_fork:
601 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200602out_free_mmap:
603 free(mmap_event);
604out_free_comm:
605 free(comm_event);
606out:
607 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200608}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200609
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200610struct process_symbol_args {
611 const char *name;
612 u64 start;
613};
614
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200615static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700616 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200617{
618 struct process_symbol_args *args = arg;
619
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200620 /*
621 * Must be a function or at least an alias, as in PARISC64, where "_text" is
622 * an 'A' to the same address as "_stext".
623 */
624 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
625 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200626 return 0;
627
628 args->start = start;
629 return 1;
630}
631
Adrian Hunter29b596b2014-01-29 16:14:37 +0200632u64 kallsyms__get_function_start(const char *kallsyms_filename,
633 const char *symbol_name)
634{
635 struct process_symbol_args args = { .name = symbol_name, };
636
637 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
638 return 0;
639
640 return args.start;
641}
642
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200643int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200644 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200645 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200646{
647 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200648 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800649 char name_buff[PATH_MAX];
650 struct map *map;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200651 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200652 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700653 union perf_event *event;
654
655 if (machine->vmlinux_maps[0] == NULL)
656 return -1;
657
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200658 /*
659 * We should get this from /sys/kernel/sections/.text, but till that is
660 * available use this, and after it is use this as a fallback for older
661 * kernels.
662 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700663 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200664 if (event == NULL) {
665 pr_debug("Not enough memory synthesizing mmap event "
666 "for kernel modules\n");
667 return -1;
668 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200669
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300670 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300671 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800672 /*
673 * kernel uses PERF_RECORD_MISC_USER for user space maps,
674 * see kernel/perf_event.c __perf_event_mmap
675 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200676 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800677 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200678 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100679 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200680
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300681 map = machine->vmlinux_maps[MAP__FUNCTION];
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200682 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200683 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200684 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300685 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200686 event->mmap.header.type = PERF_RECORD_MMAP;
687 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200688 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200689 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200690 event->mmap.start = map->start;
691 event->mmap.len = map->end - event->mmap.start;
692 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200693
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200694 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200695 free(event);
696
697 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200698}
699
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200700size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
701{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300702 const char *s;
703
704 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
705 s = " exec";
706 else
707 s = "";
708
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300709 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 -0200710}
711
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300712int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200713 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200714 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200715 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200716{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200717 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200718}
719
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300720int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200721 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200722 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300723 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200724{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200725 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800726}
727
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300728int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
729 union perf_event *event,
730 struct perf_sample *sample __maybe_unused,
731 struct machine *machine)
732{
733 return machine__process_aux_event(machine, event);
734}
735
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300736int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
737 union perf_event *event,
738 struct perf_sample *sample __maybe_unused,
739 struct machine *machine)
740{
741 return machine__process_itrace_start_event(machine, event);
742}
743
Kan Liangc4937a92015-05-10 15:13:15 -0400744int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
745 union perf_event *event,
746 struct perf_sample *sample,
747 struct machine *machine)
748{
749 return machine__process_lost_samples_event(machine, event, sample);
750}
751
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200752size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
753{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300754 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200755 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300756 event->mmap.len, event->mmap.pgoff,
757 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
758 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200759}
760
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200761size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
762{
763 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -0400764 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200765 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
766 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
767 event->mmap2.min, event->mmap2.ino,
768 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400769 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
770 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
771 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
772 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200773 event->mmap2.filename);
774}
775
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300776int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200777 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200778 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200779 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800780{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200781 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200782}
783
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200784int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
785 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200786 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200787 struct machine *machine)
788{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200789 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200790}
791
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200792size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
793{
794 return fprintf(fp, "(%d:%d):(%d:%d)\n",
795 event->fork.pid, event->fork.tid,
796 event->fork.ppid, event->fork.ptid);
797}
798
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300799int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200800 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200801 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300802 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200803{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200804 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200805}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200806
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300807int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
808 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200809 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300810 struct machine *machine)
811{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200812 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300813}
814
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300815size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
816{
817 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
818 event->aux.aux_offset, event->aux.aux_size,
819 event->aux.flags,
820 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
821 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
822}
823
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300824size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
825{
826 return fprintf(fp, " pid: %u tid: %u\n",
827 event->itrace_start.pid, event->itrace_start.tid);
828}
829
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200830size_t perf_event__fprintf(union perf_event *event, FILE *fp)
831{
832 size_t ret = fprintf(fp, "PERF_RECORD_%s",
833 perf_event__name(event->header.type));
834
835 switch (event->header.type) {
836 case PERF_RECORD_COMM:
837 ret += perf_event__fprintf_comm(event, fp);
838 break;
839 case PERF_RECORD_FORK:
840 case PERF_RECORD_EXIT:
841 ret += perf_event__fprintf_task(event, fp);
842 break;
843 case PERF_RECORD_MMAP:
844 ret += perf_event__fprintf_mmap(event, fp);
845 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200846 case PERF_RECORD_MMAP2:
847 ret += perf_event__fprintf_mmap2(event, fp);
848 break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300849 case PERF_RECORD_AUX:
850 ret += perf_event__fprintf_aux(event, fp);
851 break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300852 case PERF_RECORD_ITRACE_START:
853 ret += perf_event__fprintf_itrace_start(event, fp);
854 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200855 default:
856 ret += fprintf(fp, "\n");
857 }
858
859 return ret;
860}
861
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300862int perf_event__process(struct perf_tool *tool __maybe_unused,
863 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200864 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300865 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530866{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200867 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530868}
869
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300870void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200871 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300872 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200873{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300874 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300875 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300876 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200877
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300878 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300879 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200880 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800881 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300882 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200883
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200884 if (machine == NULL) {
885 al->map = NULL;
886 return;
887 }
888
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800889 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200890 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300891 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300892 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800893 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200894 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800895 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
896 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300897 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300898 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500899 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
900 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800901 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500902 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200903 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800904
905 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
906 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
907 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300908 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800909 if ((cpumode == PERF_RECORD_MISC_USER ||
910 cpumode == PERF_RECORD_MISC_KERNEL) &&
911 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300912 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800913
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200914 return;
915 }
916try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200917 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200918 if (al->map == NULL) {
919 /*
920 * If this is outside of all known maps, and is a negative
921 * address, try to look it up in the kernel dso, as it might be
922 * a vsyscall or vdso (which executes in user-mode).
923 *
924 * XXX This is nasty, we should have a symbol list in the
925 * "[vdso]" dso, but for now lets use the old trick of looking
926 * in the whole kernel symbol list.
927 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300928 if (cpumode == PERF_RECORD_MISC_USER && machine &&
929 mg != &machine->kmaps &&
930 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300931 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +0300932 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200933 goto try_again;
934 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300935 } else {
936 /*
937 * Kernel maps might be changed when loading symbols so loading
938 * must be done prior to using kernel maps.
939 */
940 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300941 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200942 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300943 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200944}
945
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300946void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200947 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300948 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200949{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300950 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200951 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300952 al->sym = map__find_symbol(al->map, al->addr,
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300953 thread->mg->machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200954 else
955 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200956}
957
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300958/*
959 * Callers need to drop the reference to al->thread, obtained in
960 * machine__findnew_thread()
961 */
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200962int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200963 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200964 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300965 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200966{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200967 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300968 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900969 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200970
971 if (thread == NULL)
972 return -1;
973
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200974 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300975 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200976 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300977 *
978 * This should have happened earlier, when we processed the kernel MMAP
979 * events, but for older perf.data files there was no such thing, so do
980 * it now.
981 */
982 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200983 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
984 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200985
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300986 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200987 dump_printf(" ...... dso: %s\n",
988 al->map ? al->map->dso->long_name :
989 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -0300990
991 if (thread__is_filtered(thread))
992 al->filtered |= (1 << HIST_FILTER__THREAD);
993
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300994 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200995 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200996
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300997 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900998 struct dso *dso = al->map->dso;
999
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001000 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +09001001 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1002 dso->short_name) ||
1003 (dso->short_name != dso->long_name &&
1004 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001005 dso->long_name))))) {
1006 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001007 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001008
Adrian Huntere44baa32013-08-08 14:32:25 +03001009 al->sym = map__find_symbol(al->map, al->addr,
1010 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -03001011 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001012
Feng Tang1500b932012-09-07 16:42:23 +08001013 if (symbol_conf.sym_list &&
1014 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001015 al->sym->name))) {
1016 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001017 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001018
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001019 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -02001020}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001021
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001022/*
1023 * The preprocess_sample method will return with reference counts for the
1024 * in it, when done using (and perhaps getting ref counts if needing to
1025 * keep a pointer to one of those entries) it must be paired with
1026 * addr_location__put(), so that the refcounts can be decremented.
1027 */
1028void addr_location__put(struct addr_location *al)
1029{
1030 thread__zput(al->thread);
1031}
1032
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001033bool is_bts_event(struct perf_event_attr *attr)
1034{
1035 return attr->type == PERF_TYPE_HARDWARE &&
1036 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1037 attr->sample_period == 1;
1038}
1039
1040bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1041{
1042 if (attr->type == PERF_TYPE_SOFTWARE &&
1043 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1044 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1045 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1046 return true;
1047
1048 if (is_bts_event(attr))
1049 return true;
1050
1051 return false;
1052}
1053
1054void perf_event__preprocess_sample_addr(union perf_event *event,
1055 struct perf_sample *sample,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001056 struct thread *thread,
1057 struct addr_location *al)
1058{
1059 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1060
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001061 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001062 if (!al->map)
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001063 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001064 sample->addr, al);
1065
1066 al->cpu = sample->cpu;
1067 al->sym = NULL;
1068
1069 if (al->map)
1070 al->sym = map__find_symbol(al->map, al->addr, NULL);
1071}