blob: 9d09851312522e78d3ffebc733933fa222083100 [file] [log] [blame]
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02001#include <linux/types.h>
Don Zickus7ef80702014-05-19 15:13:49 -04002#include <sys/mman.h>
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02003#include "event.h"
4#include "debug.h"
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03005#include "hist.h"
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -03006#include "machine.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02007#include "sort.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -02008#include "string.h"
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02009#include "strlist.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020010#include "thread.h"
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -020011#include "thread_map.h"
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030012#include "symbol/kallsyms.h"
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020013
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020014static const char *perf_event__names[] = {
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020015 [0] = "TOTAL",
16 [PERF_RECORD_MMAP] = "MMAP",
Stephane Eranian5c5e8542013-08-21 12:10:25 +020017 [PERF_RECORD_MMAP2] = "MMAP2",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020018 [PERF_RECORD_LOST] = "LOST",
19 [PERF_RECORD_COMM] = "COMM",
20 [PERF_RECORD_EXIT] = "EXIT",
21 [PERF_RECORD_THROTTLE] = "THROTTLE",
22 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
23 [PERF_RECORD_FORK] = "FORK",
24 [PERF_RECORD_READ] = "READ",
25 [PERF_RECORD_SAMPLE] = "SAMPLE",
26 [PERF_RECORD_HEADER_ATTR] = "ATTR",
27 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
28 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
29 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
30 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020031 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030032};
33
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020034const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000035{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020036 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000037 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020038 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000039 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020040 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000041}
42
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020043static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020044 .pid = -1,
45 .tid = -1,
46 .time = -1,
47 .stream_id = -1,
48 .cpu = -1,
49 .period = 1,
50};
51
David Ahern5aa0b032015-03-30 14:35:57 -060052/*
53 * Assumes that the first 4095 bytes of /proc/pid/stat contains
David Ahernca6c41c2015-03-30 14:35:58 -060054 * the comm, tgid and ppid.
David Ahern5aa0b032015-03-30 14:35:57 -060055 */
David Ahernca6c41c2015-03-30 14:35:58 -060056static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
57 pid_t *tgid, pid_t *ppid)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020058{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020059 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -060060 char bf[4096];
61 int fd;
62 size_t size = 0, n;
David Ahernca6c41c2015-03-30 14:35:58 -060063 char *nl, *name, *tgids, *ppids;
64
65 *tgid = -1;
66 *ppid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020067
68 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
69
David Ahern5aa0b032015-03-30 14:35:57 -060070 fd = open(filename, O_RDONLY);
71 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020072 pr_debug("couldn't open %s\n", filename);
David Ahernca6c41c2015-03-30 14:35:58 -060073 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020074 }
75
David Ahern5aa0b032015-03-30 14:35:57 -060076 n = read(fd, bf, sizeof(bf) - 1);
77 close(fd);
78 if (n <= 0) {
David Ahernca6c41c2015-03-30 14:35:58 -060079 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
David Ahern5aa0b032015-03-30 14:35:57 -060080 pid);
81 return -1;
82 }
83 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020084
David Ahern5aa0b032015-03-30 14:35:57 -060085 name = strstr(bf, "Name:");
86 tgids = strstr(bf, "Tgid:");
David Ahernca6c41c2015-03-30 14:35:58 -060087 ppids = strstr(bf, "PPid:");
David Ahernf5faf722011-12-22 11:30:02 -070088
David Ahern5aa0b032015-03-30 14:35:57 -060089 if (name) {
90 name += 5; /* strlen("Name:") */
91
92 while (*name && isspace(*name))
93 ++name;
94
95 nl = strchr(name, '\n');
96 if (nl)
97 *nl = '\0';
98
99 size = strlen(name);
100 if (size >= len)
101 size = len - 1;
102 memcpy(comm, name, size);
103 comm[size] = '\0';
104 } else {
105 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200106 }
107
David Ahern5aa0b032015-03-30 14:35:57 -0600108 if (tgids) {
109 tgids += 5; /* strlen("Tgid:") */
David Ahernca6c41c2015-03-30 14:35:58 -0600110 *tgid = atoi(tgids);
David Ahern5aa0b032015-03-30 14:35:57 -0600111 } else {
112 pr_debug("Tgid: string not found for pid %d\n", pid);
113 }
David Ahernf5faf722011-12-22 11:30:02 -0700114
David Ahernca6c41c2015-03-30 14:35:58 -0600115 if (ppids) {
116 ppids += 5; /* strlen("PPid:") */
117 *ppid = atoi(ppids);
118 } else {
119 pr_debug("PPid: string not found for pid %d\n", pid);
120 }
121
122 return 0;
David Ahernf5faf722011-12-22 11:30:02 -0700123}
124
David Ahernca6c41c2015-03-30 14:35:58 -0600125static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
126 struct machine *machine,
127 pid_t *tgid, pid_t *ppid)
David Ahernf5faf722011-12-22 11:30:02 -0700128{
David Ahernf5faf722011-12-22 11:30:02 -0700129 size_t size;
David Ahernca6c41c2015-03-30 14:35:58 -0600130
131 *ppid = -1;
David Ahernf5faf722011-12-22 11:30:02 -0700132
133 memset(&event->comm, 0, sizeof(event->comm));
134
David Ahernca6c41c2015-03-30 14:35:58 -0600135 if (machine__is_host(machine)) {
136 if (perf_event__get_comm_ids(pid, event->comm.comm,
137 sizeof(event->comm.comm),
138 tgid, ppid) != 0) {
139 return -1;
140 }
141 } else {
142 *tgid = machine->pid;
143 }
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500144
David Ahernca6c41c2015-03-30 14:35:58 -0600145 if (*tgid < 0)
146 return -1;
David Ahernf5faf722011-12-22 11:30:02 -0700147
David Ahernca6c41c2015-03-30 14:35:58 -0600148 event->comm.pid = *tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200149 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700150
151 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300152 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200153 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200154 event->comm.header.size = (sizeof(event->comm) -
155 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200156 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500157 event->comm.tid = pid;
David Ahernca6c41c2015-03-30 14:35:58 -0600158
159 return 0;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300160}
161
162static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
163 union perf_event *event, pid_t pid,
164 perf_event__handler_t process,
165 struct machine *machine)
166{
David Ahernca6c41c2015-03-30 14:35:58 -0600167 pid_t tgid, ppid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300168
David Ahernca6c41c2015-03-30 14:35:58 -0600169 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
170 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200171
Don Zickusbfd66cc2014-02-26 10:45:26 -0500172 if (process(tool, event, &synth_sample, machine) != 0)
173 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600174
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200175 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200176}
177
Don Zickus363b7852014-03-14 10:43:44 -0400178static int perf_event__synthesize_fork(struct perf_tool *tool,
David Ahernca6c41c2015-03-30 14:35:58 -0600179 union perf_event *event,
180 pid_t pid, pid_t tgid, pid_t ppid,
181 perf_event__handler_t process,
Don Zickus363b7852014-03-14 10:43:44 -0400182 struct machine *machine)
183{
184 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
185
David Ahern7764a382015-04-09 12:48:27 -0400186 /*
187 * for main thread set parent to ppid from status file. For other
188 * threads set parent pid to main thread. ie., assume main thread
189 * spawns all threads in a process
190 */
191 if (tgid == pid) {
192 event->fork.ppid = ppid;
193 event->fork.ptid = ppid;
194 } else {
195 event->fork.ppid = tgid;
196 event->fork.ptid = tgid;
197 }
Don Zickus363b7852014-03-14 10:43:44 -0400198 event->fork.pid = tgid;
199 event->fork.tid = pid;
200 event->fork.header.type = PERF_RECORD_FORK;
201
202 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
203
204 if (process(tool, event, &synth_sample, machine) != 0)
205 return -1;
206
207 return 0;
208}
209
Jiri Olsaa18382b2014-01-07 13:47:20 +0100210int perf_event__synthesize_mmap_events(struct perf_tool *tool,
211 union perf_event *event,
212 pid_t pid, pid_t tgid,
213 perf_event__handler_t process,
214 struct machine *machine,
215 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200216{
217 char filename[PATH_MAX];
218 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600219 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200220
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500221 if (machine__is_default_guest(machine))
222 return 0;
223
Dongsheng Yang99563462013-12-20 15:52:57 -0500224 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
225 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200226
227 fp = fopen(filename, "r");
228 if (fp == NULL) {
229 /*
230 * We raced with a task exiting - just return:
231 */
232 pr_debug("couldn't open %s\n", filename);
233 return -1;
234 }
235
Don Zickusa5a5ba72014-05-30 10:49:42 -0400236 event->header.type = PERF_RECORD_MMAP2;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200237
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200238 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900239 char bf[BUFSIZ];
240 char prot[5];
241 char execname[PATH_MAX];
242 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400243 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200244 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200245 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900246
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200247 if (fgets(bf, sizeof(bf), fp) == NULL)
248 break;
249
Namhyung Kim60648032012-11-11 23:20:50 +0900250 /* ensure null termination since stack will be reused. */
251 strcpy(execname, "");
252
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200253 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400254 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
255 &event->mmap2.start, &event->mmap2.len, prot,
256 &event->mmap2.pgoff, &event->mmap2.maj,
257 &event->mmap2.min,
258 &ino, execname);
259
Don Zickus9d4ecc82013-11-13 15:32:06 -0300260 /*
261 * Anon maps don't have the execname.
262 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400263 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200264 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400265
266 event->mmap2.ino = (u64)ino;
267
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300268 /*
269 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
270 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500271 if (machine__is_host(machine))
272 event->header.misc = PERF_RECORD_MISC_USER;
273 else
274 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900275
Don Zickus7ef80702014-05-19 15:13:49 -0400276 /* map protection and flags bits */
277 event->mmap2.prot = 0;
278 event->mmap2.flags = 0;
279 if (prot[0] == 'r')
280 event->mmap2.prot |= PROT_READ;
281 if (prot[1] == 'w')
282 event->mmap2.prot |= PROT_WRITE;
283 if (prot[2] == 'x')
284 event->mmap2.prot |= PROT_EXEC;
285
286 if (prot[3] == 's')
287 event->mmap2.flags |= MAP_SHARED;
288 else
289 event->mmap2.flags |= MAP_PRIVATE;
290
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300291 if (prot[2] != 'x') {
292 if (!mmap_data || prot[0] != 'r')
293 continue;
294
295 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
296 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200297
Namhyung Kim60648032012-11-11 23:20:50 +0900298 if (!strcmp(execname, ""))
299 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200300
Namhyung Kim60648032012-11-11 23:20:50 +0900301 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400302 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900303 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400304 event->mmap2.len -= event->mmap.start;
305 event->mmap2.header.size = (sizeof(event->mmap2) -
306 (sizeof(event->mmap2.filename) - size));
307 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
308 event->mmap2.header.size += machine->id_hdr_size;
309 event->mmap2.pid = tgid;
310 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000311
Namhyung Kim60648032012-11-11 23:20:50 +0900312 if (process(tool, event, &synth_sample, machine) != 0) {
313 rc = -1;
314 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200315 }
316 }
317
318 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600319 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200320}
321
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200322int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200323 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200324 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200325{
David Ahern1e6d5322012-08-26 12:24:42 -0600326 int rc = 0;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200327 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300328 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200329 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200330 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200331 if (event == NULL) {
332 pr_debug("Not enough memory synthesizing mmap event "
333 "for kernel modules\n");
334 return -1;
335 }
336
337 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200338
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800339 /*
340 * kernel uses 0 for user space maps, see kernel/perf_event.c
341 * __perf_event_mmap
342 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300343 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200344 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800345 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200346 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800347
348 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200349 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200350 size_t size;
351 struct map *pos = rb_entry(nd, struct map, rb_node);
352
353 if (pos->dso->kernel)
354 continue;
355
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300356 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200357 event->mmap.header.type = PERF_RECORD_MMAP;
358 event->mmap.header.size = (sizeof(event->mmap) -
359 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200360 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
361 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200362 event->mmap.start = pos->start;
363 event->mmap.len = pos->end - pos->start;
364 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200365
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200366 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200367 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600368 if (process(tool, event, &synth_sample, machine) != 0) {
369 rc = -1;
370 break;
371 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200372 }
373
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200374 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600375 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200376}
377
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200378static int __event__synthesize_thread(union perf_event *comm_event,
379 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400380 union perf_event *fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700381 pid_t pid, int full,
382 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200383 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300384 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200385{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500386 char filename[PATH_MAX];
387 DIR *tasks;
388 struct dirent dirent, *next;
David Ahernca6c41c2015-03-30 14:35:58 -0600389 pid_t tgid, ppid;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500390
391 /* special case: only send one comm event using passed in pid */
392 if (!full) {
393 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
394 process, machine);
395
396 if (tgid == -1)
397 return -1;
398
399 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
400 process, machine, mmap_data);
401 }
402
403 if (machine__is_default_guest(machine))
404 return 0;
405
406 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
407 machine->root_dir, pid);
408
409 tasks = opendir(filename);
410 if (tasks == NULL) {
411 pr_debug("couldn't open %s\n", filename);
412 return 0;
413 }
414
415 while (!readdir_r(tasks, &dirent, &next) && next) {
416 char *end;
417 int rc = 0;
418 pid_t _pid;
419
420 _pid = strtol(dirent.d_name, &end, 10);
421 if (*end)
422 continue;
423
David Ahernca6c41c2015-03-30 14:35:58 -0600424 if (perf_event__prepare_comm(comm_event, _pid, machine,
425 &tgid, &ppid) != 0)
Don Zickusbfd66cc2014-02-26 10:45:26 -0500426 return -1;
427
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300428 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
David Ahernca6c41c2015-03-30 14:35:58 -0600429 ppid, process, machine) < 0)
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300430 return -1;
431 /*
432 * Send the prepared comm event
433 */
434 if (process(tool, comm_event, &synth_sample, machine) != 0)
435 return -1;
436
Don Zickus363b7852014-03-14 10:43:44 -0400437 if (_pid == pid) {
438 /* process the parent's maps too */
439 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
440 process, machine, mmap_data);
Don Zickus363b7852014-03-14 10:43:44 -0400441 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500442
443 if (rc)
444 return rc;
445 }
446
447 closedir(tasks);
448 return 0;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200449}
450
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200451int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200452 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200453 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300454 struct machine *machine,
455 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200456{
Don Zickus363b7852014-03-14 10:43:44 -0400457 union perf_event *comm_event, *mmap_event, *fork_event;
David Aherndefd8d32011-12-22 11:30:01 -0700458 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200459
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200460 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200461 if (comm_event == NULL)
462 goto out;
463
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200464 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200465 if (mmap_event == NULL)
466 goto out_free_comm;
467
Don Zickus363b7852014-03-14 10:43:44 -0400468 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
469 if (fork_event == NULL)
470 goto out_free_mmap;
471
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200472 err = 0;
473 for (thread = 0; thread < threads->nr; ++thread) {
474 if (__event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400475 fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700476 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300477 process, tool, machine,
478 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200479 err = -1;
480 break;
481 }
David Aherndefd8d32011-12-22 11:30:01 -0700482
483 /*
484 * comm.pid is set to thread group id by
485 * perf_event__synthesize_comm
486 */
487 if ((int) comm_event->comm.pid != threads->map[thread]) {
488 bool need_leader = true;
489
490 /* is thread group leader in thread_map? */
491 for (j = 0; j < threads->nr; ++j) {
492 if ((int) comm_event->comm.pid == threads->map[j]) {
493 need_leader = false;
494 break;
495 }
496 }
497
498 /* if not, generate events for it */
499 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300500 __event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400501 fork_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300502 comm_event->comm.pid, 0,
503 process, tool, machine,
504 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700505 err = -1;
506 break;
507 }
508 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200509 }
Don Zickus363b7852014-03-14 10:43:44 -0400510 free(fork_event);
511out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200512 free(mmap_event);
513out_free_comm:
514 free(comm_event);
515out:
516 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200517}
518
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200519int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200520 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300521 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200522{
523 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500524 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200525 struct dirent dirent, *next;
Don Zickus363b7852014-03-14 10:43:44 -0400526 union perf_event *comm_event, *mmap_event, *fork_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200527 int err = -1;
528
Namhyung Kim574799b2014-03-17 10:45:49 +0900529 if (machine__is_default_guest(machine))
530 return 0;
531
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200532 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200533 if (comm_event == NULL)
534 goto out;
535
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200536 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200537 if (mmap_event == NULL)
538 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200539
Don Zickus363b7852014-03-14 10:43:44 -0400540 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
541 if (fork_event == NULL)
542 goto out_free_mmap;
543
Dongsheng Yang99563462013-12-20 15:52:57 -0500544 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
545 proc = opendir(proc_path);
546
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200547 if (proc == NULL)
Don Zickus363b7852014-03-14 10:43:44 -0400548 goto out_free_fork;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200549
550 while (!readdir_r(proc, &dirent, &next) && next) {
551 char *end;
552 pid_t pid = strtol(dirent.d_name, &end, 10);
553
554 if (*end) /* only interested in proper numerical dirents */
555 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300556 /*
557 * We may race with exiting thread, so don't stop just because
558 * one thread couldn't be synthesized.
559 */
Don Zickus363b7852014-03-14 10:43:44 -0400560 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
561 1, process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200562 }
563
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200564 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600565 closedir(proc);
Don Zickus363b7852014-03-14 10:43:44 -0400566out_free_fork:
567 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200568out_free_mmap:
569 free(mmap_event);
570out_free_comm:
571 free(comm_event);
572out:
573 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200574}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200575
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200576struct process_symbol_args {
577 const char *name;
578 u64 start;
579};
580
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200581static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700582 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200583{
584 struct process_symbol_args *args = arg;
585
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200586 /*
587 * Must be a function or at least an alias, as in PARISC64, where "_text" is
588 * an 'A' to the same address as "_stext".
589 */
590 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
591 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200592 return 0;
593
594 args->start = start;
595 return 1;
596}
597
Adrian Hunter29b596b2014-01-29 16:14:37 +0200598u64 kallsyms__get_function_start(const char *kallsyms_filename,
599 const char *symbol_name)
600{
601 struct process_symbol_args args = { .name = symbol_name, };
602
603 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
604 return 0;
605
606 return args.start;
607}
608
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200609int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200610 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200611 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200612{
613 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200614 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800615 char name_buff[PATH_MAX];
616 struct map *map;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200617 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200618 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700619 union perf_event *event;
620
621 if (machine->vmlinux_maps[0] == NULL)
622 return -1;
623
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200624 /*
625 * We should get this from /sys/kernel/sections/.text, but till that is
626 * available use this, and after it is use this as a fallback for older
627 * kernels.
628 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700629 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200630 if (event == NULL) {
631 pr_debug("Not enough memory synthesizing mmap event "
632 "for kernel modules\n");
633 return -1;
634 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200635
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300636 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300637 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800638 /*
639 * kernel uses PERF_RECORD_MISC_USER for user space maps,
640 * see kernel/perf_event.c __perf_event_mmap
641 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200642 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800643 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200644 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100645 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200646
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300647 map = machine->vmlinux_maps[MAP__FUNCTION];
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200648 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200649 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200650 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300651 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200652 event->mmap.header.type = PERF_RECORD_MMAP;
653 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200654 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200655 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200656 event->mmap.start = map->start;
657 event->mmap.len = map->end - event->mmap.start;
658 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200659
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200660 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200661 free(event);
662
663 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200664}
665
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200666size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
667{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300668 const char *s;
669
670 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
671 s = " exec";
672 else
673 s = "";
674
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300675 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 -0200676}
677
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300678int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200679 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200680 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200681 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200682{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200683 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200684}
685
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300686int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200687 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200688 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300689 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200690{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200691 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800692}
693
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200694size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
695{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300696 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200697 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300698 event->mmap.len, event->mmap.pgoff,
699 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
700 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200701}
702
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200703size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
704{
705 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -0400706 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200707 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
708 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
709 event->mmap2.min, event->mmap2.ino,
710 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400711 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
712 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
713 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
714 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200715 event->mmap2.filename);
716}
717
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300718int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200719 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200720 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200721 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800722{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200723 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200724}
725
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200726int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
727 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200728 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200729 struct machine *machine)
730{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200731 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200732}
733
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200734size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
735{
736 return fprintf(fp, "(%d:%d):(%d:%d)\n",
737 event->fork.pid, event->fork.tid,
738 event->fork.ppid, event->fork.ptid);
739}
740
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300741int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200742 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200743 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300744 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200745{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200746 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200747}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200748
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300749int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
750 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200751 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300752 struct machine *machine)
753{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200754 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300755}
756
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200757size_t perf_event__fprintf(union perf_event *event, FILE *fp)
758{
759 size_t ret = fprintf(fp, "PERF_RECORD_%s",
760 perf_event__name(event->header.type));
761
762 switch (event->header.type) {
763 case PERF_RECORD_COMM:
764 ret += perf_event__fprintf_comm(event, fp);
765 break;
766 case PERF_RECORD_FORK:
767 case PERF_RECORD_EXIT:
768 ret += perf_event__fprintf_task(event, fp);
769 break;
770 case PERF_RECORD_MMAP:
771 ret += perf_event__fprintf_mmap(event, fp);
772 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200773 case PERF_RECORD_MMAP2:
774 ret += perf_event__fprintf_mmap2(event, fp);
775 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200776 default:
777 ret += fprintf(fp, "\n");
778 }
779
780 return ret;
781}
782
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300783int perf_event__process(struct perf_tool *tool __maybe_unused,
784 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200785 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300786 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530787{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200788 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530789}
790
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300791void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200792 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300793 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200794{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300795 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300796 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300797 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200798
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300799 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300800 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200801 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800802 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300803 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200804
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200805 if (machine == NULL) {
806 al->map = NULL;
807 return;
808 }
809
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800810 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200811 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300812 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300813 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800814 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200815 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800816 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
817 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300818 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300819 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500820 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
821 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800822 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500823 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200824 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800825
826 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
827 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
828 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300829 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800830 if ((cpumode == PERF_RECORD_MISC_USER ||
831 cpumode == PERF_RECORD_MISC_KERNEL) &&
832 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300833 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800834
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200835 return;
836 }
837try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200838 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200839 if (al->map == NULL) {
840 /*
841 * If this is outside of all known maps, and is a negative
842 * address, try to look it up in the kernel dso, as it might be
843 * a vsyscall or vdso (which executes in user-mode).
844 *
845 * XXX This is nasty, we should have a symbol list in the
846 * "[vdso]" dso, but for now lets use the old trick of looking
847 * in the whole kernel symbol list.
848 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300849 if (cpumode == PERF_RECORD_MISC_USER && machine &&
850 mg != &machine->kmaps &&
851 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300852 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +0300853 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200854 goto try_again;
855 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300856 } else {
857 /*
858 * Kernel maps might be changed when loading symbols so loading
859 * must be done prior to using kernel maps.
860 */
861 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300862 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200863 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300864 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200865}
866
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300867void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200868 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300869 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200870{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300871 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200872 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300873 al->sym = map__find_symbol(al->map, al->addr,
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300874 thread->mg->machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200875 else
876 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200877}
878
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200879int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200880 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200881 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300882 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200883{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200884 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300885 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900886 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200887
888 if (thread == NULL)
889 return -1;
890
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200891 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300892 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200893 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300894 *
895 * This should have happened earlier, when we processed the kernel MMAP
896 * events, but for older perf.data files there was no such thing, so do
897 * it now.
898 */
899 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200900 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
901 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200902
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300903 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200904 dump_printf(" ...... dso: %s\n",
905 al->map ? al->map->dso->long_name :
906 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -0300907
908 if (thread__is_filtered(thread))
909 al->filtered |= (1 << HIST_FILTER__THREAD);
910
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300911 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200912 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200913
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300914 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900915 struct dso *dso = al->map->dso;
916
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300917 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900918 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
919 dso->short_name) ||
920 (dso->short_name != dso->long_name &&
921 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300922 dso->long_name))))) {
923 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300924 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300925
Adrian Huntere44baa32013-08-08 14:32:25 +0300926 al->sym = map__find_symbol(al->map, al->addr,
927 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300928 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200929
Feng Tang1500b932012-09-07 16:42:23 +0800930 if (symbol_conf.sym_list &&
931 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300932 al->sym->name))) {
933 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300934 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200935
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200936 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200937}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300938
939bool is_bts_event(struct perf_event_attr *attr)
940{
941 return attr->type == PERF_TYPE_HARDWARE &&
942 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
943 attr->sample_period == 1;
944}
945
946bool sample_addr_correlates_sym(struct perf_event_attr *attr)
947{
948 if (attr->type == PERF_TYPE_SOFTWARE &&
949 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
950 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
951 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
952 return true;
953
954 if (is_bts_event(attr))
955 return true;
956
957 return false;
958}
959
960void perf_event__preprocess_sample_addr(union perf_event *event,
961 struct perf_sample *sample,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300962 struct thread *thread,
963 struct addr_location *al)
964{
965 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
966
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300967 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300968 if (!al->map)
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300969 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300970 sample->addr, al);
971
972 al->cpu = sample->cpu;
973 al->sym = NULL;
974
975 if (al->map)
976 al->sym = map__find_symbol(al->map, al->addr, NULL);
977}