blob: db526091f58099d54a2181175f147339ccfbe83f [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",
Frederic Weisbecker3cb6d152011-05-23 13:06:27 +020028 [PERF_RECORD_HEADER_ATTR] = "ATTR",
29 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
30 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
31 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
32 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
Adrian Hunter3c659ee2014-10-27 15:49:22 +020033 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
Adrian Huntera16ac022015-04-09 18:53:43 +030034 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
35 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
Adrian Huntere9bf54d2015-04-09 18:53:47 +030036 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -030037};
38
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020039const char *perf_event__name(unsigned int id)
Thomas Gleixner3835bc002010-12-07 12:48:42 +000040{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020041 if (id >= ARRAY_SIZE(perf_event__names))
Thomas Gleixner3835bc002010-12-07 12:48:42 +000042 return "INVALID";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020043 if (!perf_event__names[id])
Thomas Gleixner3835bc002010-12-07 12:48:42 +000044 return "UNKNOWN";
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020045 return perf_event__names[id];
Thomas Gleixner3835bc002010-12-07 12:48:42 +000046}
47
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020048static struct perf_sample synth_sample = {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020049 .pid = -1,
50 .tid = -1,
51 .time = -1,
52 .stream_id = -1,
53 .cpu = -1,
54 .period = 1,
55};
56
David Ahern5aa0b032015-03-30 14:35:57 -060057/*
58 * Assumes that the first 4095 bytes of /proc/pid/stat contains
David Ahernca6c41c2015-03-30 14:35:58 -060059 * the comm, tgid and ppid.
David Ahern5aa0b032015-03-30 14:35:57 -060060 */
David Ahernca6c41c2015-03-30 14:35:58 -060061static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
62 pid_t *tgid, pid_t *ppid)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020063{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020064 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -060065 char bf[4096];
66 int fd;
67 size_t size = 0, n;
David Ahernca6c41c2015-03-30 14:35:58 -060068 char *nl, *name, *tgids, *ppids;
69
70 *tgid = -1;
71 *ppid = -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020072
73 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
74
David Ahern5aa0b032015-03-30 14:35:57 -060075 fd = open(filename, O_RDONLY);
76 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020077 pr_debug("couldn't open %s\n", filename);
David Ahernca6c41c2015-03-30 14:35:58 -060078 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020079 }
80
David Ahern5aa0b032015-03-30 14:35:57 -060081 n = read(fd, bf, sizeof(bf) - 1);
82 close(fd);
83 if (n <= 0) {
David Ahernca6c41c2015-03-30 14:35:58 -060084 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
David Ahern5aa0b032015-03-30 14:35:57 -060085 pid);
86 return -1;
87 }
88 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020089
David Ahern5aa0b032015-03-30 14:35:57 -060090 name = strstr(bf, "Name:");
91 tgids = strstr(bf, "Tgid:");
David Ahernca6c41c2015-03-30 14:35:58 -060092 ppids = strstr(bf, "PPid:");
David Ahernf5faf722011-12-22 11:30:02 -070093
David Ahern5aa0b032015-03-30 14:35:57 -060094 if (name) {
95 name += 5; /* strlen("Name:") */
96
97 while (*name && isspace(*name))
98 ++name;
99
100 nl = strchr(name, '\n');
101 if (nl)
102 *nl = '\0';
103
104 size = strlen(name);
105 if (size >= len)
106 size = len - 1;
107 memcpy(comm, name, size);
108 comm[size] = '\0';
109 } else {
110 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200111 }
112
David Ahern5aa0b032015-03-30 14:35:57 -0600113 if (tgids) {
114 tgids += 5; /* strlen("Tgid:") */
David Ahernca6c41c2015-03-30 14:35:58 -0600115 *tgid = atoi(tgids);
David Ahern5aa0b032015-03-30 14:35:57 -0600116 } else {
117 pr_debug("Tgid: string not found for pid %d\n", pid);
118 }
David Ahernf5faf722011-12-22 11:30:02 -0700119
David Ahernca6c41c2015-03-30 14:35:58 -0600120 if (ppids) {
121 ppids += 5; /* strlen("PPid:") */
122 *ppid = atoi(ppids);
123 } else {
124 pr_debug("PPid: string not found for pid %d\n", pid);
125 }
126
127 return 0;
David Ahernf5faf722011-12-22 11:30:02 -0700128}
129
David Ahernca6c41c2015-03-30 14:35:58 -0600130static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
131 struct machine *machine,
132 pid_t *tgid, pid_t *ppid)
David Ahernf5faf722011-12-22 11:30:02 -0700133{
David Ahernf5faf722011-12-22 11:30:02 -0700134 size_t size;
David Ahernca6c41c2015-03-30 14:35:58 -0600135
136 *ppid = -1;
David Ahernf5faf722011-12-22 11:30:02 -0700137
138 memset(&event->comm, 0, sizeof(event->comm));
139
David Ahernca6c41c2015-03-30 14:35:58 -0600140 if (machine__is_host(machine)) {
141 if (perf_event__get_comm_ids(pid, event->comm.comm,
142 sizeof(event->comm.comm),
143 tgid, ppid) != 0) {
144 return -1;
145 }
146 } else {
147 *tgid = machine->pid;
148 }
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500149
David Ahernca6c41c2015-03-30 14:35:58 -0600150 if (*tgid < 0)
151 return -1;
David Ahernf5faf722011-12-22 11:30:02 -0700152
David Ahernca6c41c2015-03-30 14:35:58 -0600153 event->comm.pid = *tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200154 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700155
156 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300157 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200158 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200159 event->comm.header.size = (sizeof(event->comm) -
160 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200161 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500162 event->comm.tid = pid;
David Ahernca6c41c2015-03-30 14:35:58 -0600163
164 return 0;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300165}
166
167static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
168 union perf_event *event, pid_t pid,
169 perf_event__handler_t process,
170 struct machine *machine)
171{
David Ahernca6c41c2015-03-30 14:35:58 -0600172 pid_t tgid, ppid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300173
David Ahernca6c41c2015-03-30 14:35:58 -0600174 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
175 return -1;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200176
Don Zickusbfd66cc2014-02-26 10:45:26 -0500177 if (process(tool, event, &synth_sample, machine) != 0)
178 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600179
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200180 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200181}
182
Don Zickus363b7852014-03-14 10:43:44 -0400183static int perf_event__synthesize_fork(struct perf_tool *tool,
David Ahernca6c41c2015-03-30 14:35:58 -0600184 union perf_event *event,
185 pid_t pid, pid_t tgid, pid_t ppid,
186 perf_event__handler_t process,
Don Zickus363b7852014-03-14 10:43:44 -0400187 struct machine *machine)
188{
189 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
190
David Ahern7764a382015-04-09 12:48:27 -0400191 /*
192 * for main thread set parent to ppid from status file. For other
193 * threads set parent pid to main thread. ie., assume main thread
194 * spawns all threads in a process
195 */
196 if (tgid == pid) {
197 event->fork.ppid = ppid;
198 event->fork.ptid = ppid;
199 } else {
200 event->fork.ppid = tgid;
201 event->fork.ptid = tgid;
202 }
Don Zickus363b7852014-03-14 10:43:44 -0400203 event->fork.pid = tgid;
204 event->fork.tid = pid;
205 event->fork.header.type = PERF_RECORD_FORK;
206
207 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
208
209 if (process(tool, event, &synth_sample, machine) != 0)
210 return -1;
211
212 return 0;
213}
214
Jiri Olsaa18382b2014-01-07 13:47:20 +0100215int perf_event__synthesize_mmap_events(struct perf_tool *tool,
216 union perf_event *event,
217 pid_t pid, pid_t tgid,
218 perf_event__handler_t process,
219 struct machine *machine,
220 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200221{
222 char filename[PATH_MAX];
223 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600224 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200225
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500226 if (machine__is_default_guest(machine))
227 return 0;
228
Dongsheng Yang99563462013-12-20 15:52:57 -0500229 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
230 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200231
232 fp = fopen(filename, "r");
233 if (fp == NULL) {
234 /*
235 * We raced with a task exiting - just return:
236 */
237 pr_debug("couldn't open %s\n", filename);
238 return -1;
239 }
240
Don Zickusa5a5ba72014-05-30 10:49:42 -0400241 event->header.type = PERF_RECORD_MMAP2;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200242
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200243 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900244 char bf[BUFSIZ];
245 char prot[5];
246 char execname[PATH_MAX];
247 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400248 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200249 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200250 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900251
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200252 if (fgets(bf, sizeof(bf), fp) == NULL)
253 break;
254
Namhyung Kim60648032012-11-11 23:20:50 +0900255 /* ensure null termination since stack will be reused. */
256 strcpy(execname, "");
257
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200258 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400259 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
260 &event->mmap2.start, &event->mmap2.len, prot,
261 &event->mmap2.pgoff, &event->mmap2.maj,
262 &event->mmap2.min,
263 &ino, execname);
264
Don Zickus9d4ecc82013-11-13 15:32:06 -0300265 /*
266 * Anon maps don't have the execname.
267 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400268 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200269 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400270
271 event->mmap2.ino = (u64)ino;
272
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300273 /*
274 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
275 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500276 if (machine__is_host(machine))
277 event->header.misc = PERF_RECORD_MISC_USER;
278 else
279 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900280
Don Zickus7ef80702014-05-19 15:13:49 -0400281 /* map protection and flags bits */
282 event->mmap2.prot = 0;
283 event->mmap2.flags = 0;
284 if (prot[0] == 'r')
285 event->mmap2.prot |= PROT_READ;
286 if (prot[1] == 'w')
287 event->mmap2.prot |= PROT_WRITE;
288 if (prot[2] == 'x')
289 event->mmap2.prot |= PROT_EXEC;
290
291 if (prot[3] == 's')
292 event->mmap2.flags |= MAP_SHARED;
293 else
294 event->mmap2.flags |= MAP_PRIVATE;
295
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300296 if (prot[2] != 'x') {
297 if (!mmap_data || prot[0] != 'r')
298 continue;
299
300 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
301 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200302
Namhyung Kim60648032012-11-11 23:20:50 +0900303 if (!strcmp(execname, ""))
304 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200305
Namhyung Kim60648032012-11-11 23:20:50 +0900306 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400307 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900308 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400309 event->mmap2.len -= event->mmap.start;
310 event->mmap2.header.size = (sizeof(event->mmap2) -
311 (sizeof(event->mmap2.filename) - size));
312 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
313 event->mmap2.header.size += machine->id_hdr_size;
314 event->mmap2.pid = tgid;
315 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000316
Namhyung Kim60648032012-11-11 23:20:50 +0900317 if (process(tool, event, &synth_sample, machine) != 0) {
318 rc = -1;
319 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200320 }
321 }
322
323 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600324 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200325}
326
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200327int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200328 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200329 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200330{
David Ahern1e6d5322012-08-26 12:24:42 -0600331 int rc = 0;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200332 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300333 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200334 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200335 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200336 if (event == NULL) {
337 pr_debug("Not enough memory synthesizing mmap event "
338 "for kernel modules\n");
339 return -1;
340 }
341
342 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200343
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800344 /*
345 * kernel uses 0 for user space maps, see kernel/perf_event.c
346 * __perf_event_mmap
347 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300348 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200349 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800350 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200351 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800352
353 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200354 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200355 size_t size;
356 struct map *pos = rb_entry(nd, struct map, rb_node);
357
358 if (pos->dso->kernel)
359 continue;
360
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300361 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200362 event->mmap.header.type = PERF_RECORD_MMAP;
363 event->mmap.header.size = (sizeof(event->mmap) -
364 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200365 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
366 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200367 event->mmap.start = pos->start;
368 event->mmap.len = pos->end - pos->start;
369 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200370
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200371 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200372 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600373 if (process(tool, event, &synth_sample, machine) != 0) {
374 rc = -1;
375 break;
376 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200377 }
378
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200379 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600380 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200381}
382
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200383static int __event__synthesize_thread(union perf_event *comm_event,
384 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400385 union perf_event *fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700386 pid_t pid, int full,
387 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200388 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300389 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200390{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500391 char filename[PATH_MAX];
392 DIR *tasks;
393 struct dirent dirent, *next;
David Ahernca6c41c2015-03-30 14:35:58 -0600394 pid_t tgid, ppid;
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300395 int rc = 0;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500396
397 /* special case: only send one comm event using passed in pid */
398 if (!full) {
399 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
400 process, machine);
401
402 if (tgid == -1)
403 return -1;
404
405 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
406 process, machine, mmap_data);
407 }
408
409 if (machine__is_default_guest(machine))
410 return 0;
411
412 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
413 machine->root_dir, pid);
414
415 tasks = opendir(filename);
416 if (tasks == NULL) {
417 pr_debug("couldn't open %s\n", filename);
418 return 0;
419 }
420
421 while (!readdir_r(tasks, &dirent, &next) && next) {
422 char *end;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500423 pid_t _pid;
424
425 _pid = strtol(dirent.d_name, &end, 10);
426 if (*end)
427 continue;
428
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300429 rc = -1;
David Ahernca6c41c2015-03-30 14:35:58 -0600430 if (perf_event__prepare_comm(comm_event, _pid, machine,
431 &tgid, &ppid) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300432 break;
Don Zickusbfd66cc2014-02-26 10:45:26 -0500433
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300434 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
David Ahernca6c41c2015-03-30 14:35:58 -0600435 ppid, process, machine) < 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300436 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300437 /*
438 * Send the prepared comm event
439 */
440 if (process(tool, comm_event, &synth_sample, machine) != 0)
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300441 break;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300442
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300443 rc = 0;
Don Zickus363b7852014-03-14 10:43:44 -0400444 if (_pid == pid) {
445 /* process the parent's maps too */
446 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
447 process, machine, mmap_data);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300448 if (rc)
449 break;
Don Zickus363b7852014-03-14 10:43:44 -0400450 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500451 }
452
453 closedir(tasks);
Arnaldo Carvalho de Melod998b732015-04-08 11:57:03 -0300454 return rc;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200455}
456
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200457int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200458 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200459 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300460 struct machine *machine,
461 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200462{
Don Zickus363b7852014-03-14 10:43:44 -0400463 union perf_event *comm_event, *mmap_event, *fork_event;
David Aherndefd8d32011-12-22 11:30:01 -0700464 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200465
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200466 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200467 if (comm_event == NULL)
468 goto out;
469
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200470 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200471 if (mmap_event == NULL)
472 goto out_free_comm;
473
Don Zickus363b7852014-03-14 10:43:44 -0400474 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
475 if (fork_event == NULL)
476 goto out_free_mmap;
477
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200478 err = 0;
479 for (thread = 0; thread < threads->nr; ++thread) {
480 if (__event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400481 fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700482 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300483 process, tool, machine,
484 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200485 err = -1;
486 break;
487 }
David Aherndefd8d32011-12-22 11:30:01 -0700488
489 /*
490 * comm.pid is set to thread group id by
491 * perf_event__synthesize_comm
492 */
493 if ((int) comm_event->comm.pid != threads->map[thread]) {
494 bool need_leader = true;
495
496 /* is thread group leader in thread_map? */
497 for (j = 0; j < threads->nr; ++j) {
498 if ((int) comm_event->comm.pid == threads->map[j]) {
499 need_leader = false;
500 break;
501 }
502 }
503
504 /* if not, generate events for it */
505 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300506 __event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400507 fork_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300508 comm_event->comm.pid, 0,
509 process, tool, machine,
510 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700511 err = -1;
512 break;
513 }
514 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200515 }
Don Zickus363b7852014-03-14 10:43:44 -0400516 free(fork_event);
517out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200518 free(mmap_event);
519out_free_comm:
520 free(comm_event);
521out:
522 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200523}
524
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200525int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200526 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300527 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200528{
529 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500530 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200531 struct dirent dirent, *next;
Don Zickus363b7852014-03-14 10:43:44 -0400532 union perf_event *comm_event, *mmap_event, *fork_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200533 int err = -1;
534
Namhyung Kim574799b2014-03-17 10:45:49 +0900535 if (machine__is_default_guest(machine))
536 return 0;
537
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200538 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200539 if (comm_event == NULL)
540 goto out;
541
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200542 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200543 if (mmap_event == NULL)
544 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200545
Don Zickus363b7852014-03-14 10:43:44 -0400546 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
547 if (fork_event == NULL)
548 goto out_free_mmap;
549
Dongsheng Yang99563462013-12-20 15:52:57 -0500550 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
551 proc = opendir(proc_path);
552
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200553 if (proc == NULL)
Don Zickus363b7852014-03-14 10:43:44 -0400554 goto out_free_fork;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200555
556 while (!readdir_r(proc, &dirent, &next) && next) {
557 char *end;
558 pid_t pid = strtol(dirent.d_name, &end, 10);
559
560 if (*end) /* only interested in proper numerical dirents */
561 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300562 /*
563 * We may race with exiting thread, so don't stop just because
564 * one thread couldn't be synthesized.
565 */
Don Zickus363b7852014-03-14 10:43:44 -0400566 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
567 1, process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200568 }
569
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200570 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600571 closedir(proc);
Don Zickus363b7852014-03-14 10:43:44 -0400572out_free_fork:
573 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200574out_free_mmap:
575 free(mmap_event);
576out_free_comm:
577 free(comm_event);
578out:
579 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200580}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200581
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200582struct process_symbol_args {
583 const char *name;
584 u64 start;
585};
586
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200587static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700588 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200589{
590 struct process_symbol_args *args = arg;
591
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200592 /*
593 * Must be a function or at least an alias, as in PARISC64, where "_text" is
594 * an 'A' to the same address as "_stext".
595 */
596 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
597 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200598 return 0;
599
600 args->start = start;
601 return 1;
602}
603
Adrian Hunter29b596b2014-01-29 16:14:37 +0200604u64 kallsyms__get_function_start(const char *kallsyms_filename,
605 const char *symbol_name)
606{
607 struct process_symbol_args args = { .name = symbol_name, };
608
609 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
610 return 0;
611
612 return args.start;
613}
614
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200615int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200616 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200617 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200618{
619 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200620 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800621 char name_buff[PATH_MAX];
622 struct map *map;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200623 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200624 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700625 union perf_event *event;
626
627 if (machine->vmlinux_maps[0] == NULL)
628 return -1;
629
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200630 /*
631 * We should get this from /sys/kernel/sections/.text, but till that is
632 * available use this, and after it is use this as a fallback for older
633 * kernels.
634 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700635 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200636 if (event == NULL) {
637 pr_debug("Not enough memory synthesizing mmap event "
638 "for kernel modules\n");
639 return -1;
640 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200641
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300642 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300643 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800644 /*
645 * kernel uses PERF_RECORD_MISC_USER for user space maps,
646 * see kernel/perf_event.c __perf_event_mmap
647 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200648 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800649 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200650 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100651 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200652
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300653 map = machine->vmlinux_maps[MAP__FUNCTION];
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200654 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200655 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200656 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300657 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200658 event->mmap.header.type = PERF_RECORD_MMAP;
659 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200660 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200661 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200662 event->mmap.start = map->start;
663 event->mmap.len = map->end - event->mmap.start;
664 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200665
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200666 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200667 free(event);
668
669 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200670}
671
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200672size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
673{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300674 const char *s;
675
676 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
677 s = " exec";
678 else
679 s = "";
680
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300681 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 -0200682}
683
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300684int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200685 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200686 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200687 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200688{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200689 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200690}
691
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300692int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200693 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200694 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300695 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200696{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200697 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800698}
699
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300700int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
701 union perf_event *event,
702 struct perf_sample *sample __maybe_unused,
703 struct machine *machine)
704{
705 return machine__process_aux_event(machine, event);
706}
707
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300708int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
709 union perf_event *event,
710 struct perf_sample *sample __maybe_unused,
711 struct machine *machine)
712{
713 return machine__process_itrace_start_event(machine, event);
714}
715
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200716size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
717{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300718 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200719 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300720 event->mmap.len, event->mmap.pgoff,
721 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
722 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200723}
724
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200725size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
726{
727 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -0400728 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200729 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
730 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
731 event->mmap2.min, event->mmap2.ino,
732 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400733 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
734 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
735 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
736 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200737 event->mmap2.filename);
738}
739
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300740int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200741 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200742 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200743 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800744{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200745 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200746}
747
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200748int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
749 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200750 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200751 struct machine *machine)
752{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200753 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200754}
755
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200756size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
757{
758 return fprintf(fp, "(%d:%d):(%d:%d)\n",
759 event->fork.pid, event->fork.tid,
760 event->fork.ppid, event->fork.ptid);
761}
762
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300763int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200764 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200765 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300766 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200767{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200768 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200769}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200770
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300771int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
772 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200773 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300774 struct machine *machine)
775{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200776 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300777}
778
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300779size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
780{
781 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
782 event->aux.aux_offset, event->aux.aux_size,
783 event->aux.flags,
784 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
785 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
786}
787
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300788size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
789{
790 return fprintf(fp, " pid: %u tid: %u\n",
791 event->itrace_start.pid, event->itrace_start.tid);
792}
793
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200794size_t perf_event__fprintf(union perf_event *event, FILE *fp)
795{
796 size_t ret = fprintf(fp, "PERF_RECORD_%s",
797 perf_event__name(event->header.type));
798
799 switch (event->header.type) {
800 case PERF_RECORD_COMM:
801 ret += perf_event__fprintf_comm(event, fp);
802 break;
803 case PERF_RECORD_FORK:
804 case PERF_RECORD_EXIT:
805 ret += perf_event__fprintf_task(event, fp);
806 break;
807 case PERF_RECORD_MMAP:
808 ret += perf_event__fprintf_mmap(event, fp);
809 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200810 case PERF_RECORD_MMAP2:
811 ret += perf_event__fprintf_mmap2(event, fp);
812 break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300813 case PERF_RECORD_AUX:
814 ret += perf_event__fprintf_aux(event, fp);
815 break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300816 case PERF_RECORD_ITRACE_START:
817 ret += perf_event__fprintf_itrace_start(event, fp);
818 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200819 default:
820 ret += fprintf(fp, "\n");
821 }
822
823 return ret;
824}
825
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300826int perf_event__process(struct perf_tool *tool __maybe_unused,
827 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200828 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300829 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530830{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200831 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530832}
833
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300834void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200835 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300836 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200837{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300838 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300839 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300840 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200841
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300842 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300843 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200844 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800845 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300846 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200847
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200848 if (machine == NULL) {
849 al->map = NULL;
850 return;
851 }
852
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800853 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200854 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300855 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300856 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800857 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200858 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800859 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
860 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300861 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300862 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500863 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
864 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800865 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500866 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200867 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800868
869 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
870 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
871 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300872 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800873 if ((cpumode == PERF_RECORD_MISC_USER ||
874 cpumode == PERF_RECORD_MISC_KERNEL) &&
875 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300876 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800877
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200878 return;
879 }
880try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200881 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200882 if (al->map == NULL) {
883 /*
884 * If this is outside of all known maps, and is a negative
885 * address, try to look it up in the kernel dso, as it might be
886 * a vsyscall or vdso (which executes in user-mode).
887 *
888 * XXX This is nasty, we should have a symbol list in the
889 * "[vdso]" dso, but for now lets use the old trick of looking
890 * in the whole kernel symbol list.
891 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300892 if (cpumode == PERF_RECORD_MISC_USER && machine &&
893 mg != &machine->kmaps &&
894 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300895 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +0300896 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200897 goto try_again;
898 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300899 } else {
900 /*
901 * Kernel maps might be changed when loading symbols so loading
902 * must be done prior to using kernel maps.
903 */
904 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300905 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200906 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300907 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200908}
909
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300910void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200911 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300912 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200913{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300914 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200915 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300916 al->sym = map__find_symbol(al->map, al->addr,
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300917 thread->mg->machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200918 else
919 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200920}
921
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200922int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200923 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200924 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300925 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200926{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200927 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300928 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900929 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200930
931 if (thread == NULL)
932 return -1;
933
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200934 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300935 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200936 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300937 *
938 * This should have happened earlier, when we processed the kernel MMAP
939 * events, but for older perf.data files there was no such thing, so do
940 * it now.
941 */
942 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200943 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
944 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200945
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300946 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200947 dump_printf(" ...... dso: %s\n",
948 al->map ? al->map->dso->long_name :
949 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -0300950
951 if (thread__is_filtered(thread))
952 al->filtered |= (1 << HIST_FILTER__THREAD);
953
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300954 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200955 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200956
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300957 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900958 struct dso *dso = al->map->dso;
959
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300960 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900961 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
962 dso->short_name) ||
963 (dso->short_name != dso->long_name &&
964 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300965 dso->long_name))))) {
966 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300967 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300968
Adrian Huntere44baa32013-08-08 14:32:25 +0300969 al->sym = map__find_symbol(al->map, al->addr,
970 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300971 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200972
Feng Tang1500b932012-09-07 16:42:23 +0800973 if (symbol_conf.sym_list &&
974 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300975 al->sym->name))) {
976 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300977 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200978
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200979 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200980}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300981
982bool is_bts_event(struct perf_event_attr *attr)
983{
984 return attr->type == PERF_TYPE_HARDWARE &&
985 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
986 attr->sample_period == 1;
987}
988
989bool sample_addr_correlates_sym(struct perf_event_attr *attr)
990{
991 if (attr->type == PERF_TYPE_SOFTWARE &&
992 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
993 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
994 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
995 return true;
996
997 if (is_bts_event(attr))
998 return true;
999
1000 return false;
1001}
1002
1003void perf_event__preprocess_sample_addr(union perf_event *event,
1004 struct perf_sample *sample,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001005 struct thread *thread,
1006 struct addr_location *al)
1007{
1008 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1009
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001010 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001011 if (!al->map)
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001012 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +03001013 sample->addr, al);
1014
1015 al->cpu = sample->cpu;
1016 al->sym = NULL;
1017
1018 if (al->map)
1019 al->sym = map__find_symbol(al->map, al->addr, NULL);
1020}