blob: 023dd3548a942c635f6533bbfc2f8529c807d854 [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
54 * the comm and tgid.
55 */
David Ahernf5faf722011-12-22 11:30:02 -070056static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020057{
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020058 char filename[PATH_MAX];
David Ahern5aa0b032015-03-30 14:35:57 -060059 char bf[4096];
60 int fd;
61 size_t size = 0, n;
David Ahernf5faf722011-12-22 11:30:02 -070062 pid_t tgid = -1;
David Ahern5aa0b032015-03-30 14:35:57 -060063 char *nl, *name, *tgids;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020064
65 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
66
David Ahern5aa0b032015-03-30 14:35:57 -060067 fd = open(filename, O_RDONLY);
68 if (fd < 0) {
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020069 pr_debug("couldn't open %s\n", filename);
70 return 0;
71 }
72
David Ahern5aa0b032015-03-30 14:35:57 -060073 n = read(fd, bf, sizeof(bf) - 1);
74 close(fd);
75 if (n <= 0) {
76 pr_warning("Couldn't get COMM and tgid for pid %d\n",
77 pid);
78 return -1;
79 }
80 bf[n] = '\0';
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -020081
David Ahern5aa0b032015-03-30 14:35:57 -060082 name = strstr(bf, "Name:");
83 tgids = strstr(bf, "Tgid:");
David Ahernf5faf722011-12-22 11:30:02 -070084
David Ahern5aa0b032015-03-30 14:35:57 -060085 if (name) {
86 name += 5; /* strlen("Name:") */
87
88 while (*name && isspace(*name))
89 ++name;
90
91 nl = strchr(name, '\n');
92 if (nl)
93 *nl = '\0';
94
95 size = strlen(name);
96 if (size >= len)
97 size = len - 1;
98 memcpy(comm, name, size);
99 comm[size] = '\0';
100 } else {
101 pr_debug("Name: string not found for pid %d\n", pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200102 }
103
David Ahern5aa0b032015-03-30 14:35:57 -0600104 if (tgids) {
105 tgids += 5; /* strlen("Tgid:") */
106 tgid = atoi(tgids);
107 } else {
108 pr_debug("Tgid: string not found for pid %d\n", pid);
109 }
David Ahernf5faf722011-12-22 11:30:02 -0700110
111 return tgid;
112}
113
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300114static pid_t perf_event__prepare_comm(union perf_event *event, pid_t pid,
David Ahernf5faf722011-12-22 11:30:02 -0700115 struct machine *machine)
116{
David Ahernf5faf722011-12-22 11:30:02 -0700117 size_t size;
David Ahernf5faf722011-12-22 11:30:02 -0700118 pid_t tgid;
119
120 memset(&event->comm, 0, sizeof(event->comm));
121
Dongsheng Yangf5db57c42013-12-20 15:52:59 -0500122 if (machine__is_host(machine))
123 tgid = perf_event__get_comm_tgid(pid, event->comm.comm,
124 sizeof(event->comm.comm));
125 else
126 tgid = machine->pid;
127
David Ahernf5faf722011-12-22 11:30:02 -0700128 if (tgid < 0)
129 goto out;
130
131 event->comm.pid = tgid;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200132 event->comm.header.type = PERF_RECORD_COMM;
David Ahernf5faf722011-12-22 11:30:02 -0700133
134 size = strlen(event->comm.comm) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300135 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200136 memset(event->comm.comm + size, 0, machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200137 event->comm.header.size = (sizeof(event->comm) -
138 (sizeof(event->comm.comm) - size) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200139 machine->id_hdr_size);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500140 event->comm.tid = pid;
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300141out:
142 return tgid;
143}
144
145static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
146 union perf_event *event, pid_t pid,
147 perf_event__handler_t process,
148 struct machine *machine)
149{
150 pid_t tgid = perf_event__prepare_comm(event, pid, machine);
151
152 if (tgid == -1)
153 goto out;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200154
Don Zickusbfd66cc2014-02-26 10:45:26 -0500155 if (process(tool, event, &synth_sample, machine) != 0)
156 return -1;
David Ahern1e6d5322012-08-26 12:24:42 -0600157
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200158out:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200159 return tgid;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200160}
161
Don Zickus363b7852014-03-14 10:43:44 -0400162static int perf_event__synthesize_fork(struct perf_tool *tool,
163 union perf_event *event, pid_t pid,
164 pid_t tgid, perf_event__handler_t process,
165 struct machine *machine)
166{
167 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
168
Don Zickus363b7852014-03-14 10:43:44 -0400169 event->fork.ppid = tgid;
170 event->fork.ptid = tgid;
171 event->fork.pid = tgid;
172 event->fork.tid = pid;
173 event->fork.header.type = PERF_RECORD_FORK;
174
175 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
176
177 if (process(tool, event, &synth_sample, machine) != 0)
178 return -1;
179
180 return 0;
181}
182
Jiri Olsaa18382b2014-01-07 13:47:20 +0100183int perf_event__synthesize_mmap_events(struct perf_tool *tool,
184 union perf_event *event,
185 pid_t pid, pid_t tgid,
186 perf_event__handler_t process,
187 struct machine *machine,
188 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200189{
190 char filename[PATH_MAX];
191 FILE *fp;
David Ahern1e6d5322012-08-26 12:24:42 -0600192 int rc = 0;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200193
Dongsheng Yangc239c25a2013-12-20 15:53:00 -0500194 if (machine__is_default_guest(machine))
195 return 0;
196
Dongsheng Yang99563462013-12-20 15:52:57 -0500197 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
198 machine->root_dir, pid);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200199
200 fp = fopen(filename, "r");
201 if (fp == NULL) {
202 /*
203 * We raced with a task exiting - just return:
204 */
205 pr_debug("couldn't open %s\n", filename);
206 return -1;
207 }
208
Don Zickusa5a5ba72014-05-30 10:49:42 -0400209 event->header.type = PERF_RECORD_MMAP2;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200210
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200211 while (1) {
Namhyung Kim60648032012-11-11 23:20:50 +0900212 char bf[BUFSIZ];
213 char prot[5];
214 char execname[PATH_MAX];
215 char anonstr[] = "//anon";
Don Zickusa5a5ba72014-05-30 10:49:42 -0400216 unsigned int ino;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200217 size_t size;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200218 ssize_t n;
Namhyung Kim60648032012-11-11 23:20:50 +0900219
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200220 if (fgets(bf, sizeof(bf), fp) == NULL)
221 break;
222
Namhyung Kim60648032012-11-11 23:20:50 +0900223 /* ensure null termination since stack will be reused. */
224 strcpy(execname, "");
225
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200226 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400227 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n",
228 &event->mmap2.start, &event->mmap2.len, prot,
229 &event->mmap2.pgoff, &event->mmap2.maj,
230 &event->mmap2.min,
231 &ino, execname);
232
Don Zickus9d4ecc82013-11-13 15:32:06 -0300233 /*
234 * Anon maps don't have the execname.
235 */
Don Zickusa5a5ba72014-05-30 10:49:42 -0400236 if (n < 7)
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200237 continue;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400238
239 event->mmap2.ino = (u64)ino;
240
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300241 /*
242 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
243 */
Dongsheng Yang73547aa2013-12-20 15:52:58 -0500244 if (machine__is_host(machine))
245 event->header.misc = PERF_RECORD_MISC_USER;
246 else
247 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
Namhyung Kim60648032012-11-11 23:20:50 +0900248
Don Zickus7ef80702014-05-19 15:13:49 -0400249 /* map protection and flags bits */
250 event->mmap2.prot = 0;
251 event->mmap2.flags = 0;
252 if (prot[0] == 'r')
253 event->mmap2.prot |= PROT_READ;
254 if (prot[1] == 'w')
255 event->mmap2.prot |= PROT_WRITE;
256 if (prot[2] == 'x')
257 event->mmap2.prot |= PROT_EXEC;
258
259 if (prot[3] == 's')
260 event->mmap2.flags |= MAP_SHARED;
261 else
262 event->mmap2.flags |= MAP_PRIVATE;
263
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300264 if (prot[2] != 'x') {
265 if (!mmap_data || prot[0] != 'r')
266 continue;
267
268 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
269 }
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200270
Namhyung Kim60648032012-11-11 23:20:50 +0900271 if (!strcmp(execname, ""))
272 strcpy(execname, anonstr);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200273
Namhyung Kim60648032012-11-11 23:20:50 +0900274 size = strlen(execname) + 1;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400275 memcpy(event->mmap2.filename, execname, size);
Namhyung Kim60648032012-11-11 23:20:50 +0900276 size = PERF_ALIGN(size, sizeof(u64));
Don Zickusa5a5ba72014-05-30 10:49:42 -0400277 event->mmap2.len -= event->mmap.start;
278 event->mmap2.header.size = (sizeof(event->mmap2) -
279 (sizeof(event->mmap2.filename) - size));
280 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
281 event->mmap2.header.size += machine->id_hdr_size;
282 event->mmap2.pid = tgid;
283 event->mmap2.tid = pid;
Anton Blanchard6a0e55d2011-08-30 09:15:06 +1000284
Namhyung Kim60648032012-11-11 23:20:50 +0900285 if (process(tool, event, &synth_sample, machine) != 0) {
286 rc = -1;
287 break;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200288 }
289 }
290
291 fclose(fp);
David Ahern1e6d5322012-08-26 12:24:42 -0600292 return rc;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200293}
294
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200295int perf_event__synthesize_modules(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200296 perf_event__handler_t process,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200297 struct machine *machine)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200298{
David Ahern1e6d5322012-08-26 12:24:42 -0600299 int rc = 0;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200300 struct rb_node *nd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300301 struct map_groups *kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200302 union perf_event *event = zalloc((sizeof(event->mmap) +
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200303 machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200304 if (event == NULL) {
305 pr_debug("Not enough memory synthesizing mmap event "
306 "for kernel modules\n");
307 return -1;
308 }
309
310 event->header.type = PERF_RECORD_MMAP;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200311
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800312 /*
313 * kernel uses 0 for user space maps, see kernel/perf_event.c
314 * __perf_event_mmap
315 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300316 if (machine__is_host(machine))
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200317 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800318 else
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200319 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800320
321 for (nd = rb_first(&kmaps->maps[MAP__FUNCTION]);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200322 nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200323 size_t size;
324 struct map *pos = rb_entry(nd, struct map, rb_node);
325
326 if (pos->dso->kernel)
327 continue;
328
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300329 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200330 event->mmap.header.type = PERF_RECORD_MMAP;
331 event->mmap.header.size = (sizeof(event->mmap) -
332 (sizeof(event->mmap.filename) - size));
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200333 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
334 event->mmap.header.size += machine->id_hdr_size;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200335 event->mmap.start = pos->start;
336 event->mmap.len = pos->end - pos->start;
337 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200338
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200339 memcpy(event->mmap.filename, pos->dso->long_name,
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200340 pos->dso->long_name_len + 1);
David Ahern1e6d5322012-08-26 12:24:42 -0600341 if (process(tool, event, &synth_sample, machine) != 0) {
342 rc = -1;
343 break;
344 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200345 }
346
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200347 free(event);
David Ahern1e6d5322012-08-26 12:24:42 -0600348 return rc;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200349}
350
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200351static int __event__synthesize_thread(union perf_event *comm_event,
352 union perf_event *mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400353 union perf_event *fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700354 pid_t pid, int full,
355 perf_event__handler_t process,
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200356 struct perf_tool *tool,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300357 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200358{
Don Zickusbfd66cc2014-02-26 10:45:26 -0500359 char filename[PATH_MAX];
360 DIR *tasks;
361 struct dirent dirent, *next;
362 pid_t tgid;
363
364 /* special case: only send one comm event using passed in pid */
365 if (!full) {
366 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
367 process, machine);
368
369 if (tgid == -1)
370 return -1;
371
372 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
373 process, machine, mmap_data);
374 }
375
376 if (machine__is_default_guest(machine))
377 return 0;
378
379 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
380 machine->root_dir, pid);
381
382 tasks = opendir(filename);
383 if (tasks == NULL) {
384 pr_debug("couldn't open %s\n", filename);
385 return 0;
386 }
387
388 while (!readdir_r(tasks, &dirent, &next) && next) {
389 char *end;
390 int rc = 0;
391 pid_t _pid;
392
393 _pid = strtol(dirent.d_name, &end, 10);
394 if (*end)
395 continue;
396
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300397 tgid = perf_event__prepare_comm(comm_event, _pid, machine);
Don Zickusbfd66cc2014-02-26 10:45:26 -0500398 if (tgid == -1)
399 return -1;
400
Arnaldo Carvalho de Melo4aa5f4f2015-02-27 19:52:10 -0300401 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
402 process, machine) < 0)
403 return -1;
404 /*
405 * Send the prepared comm event
406 */
407 if (process(tool, comm_event, &synth_sample, machine) != 0)
408 return -1;
409
Don Zickus363b7852014-03-14 10:43:44 -0400410 if (_pid == pid) {
411 /* process the parent's maps too */
412 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
413 process, machine, mmap_data);
Don Zickus363b7852014-03-14 10:43:44 -0400414 }
Don Zickusbfd66cc2014-02-26 10:45:26 -0500415
416 if (rc)
417 return rc;
418 }
419
420 closedir(tasks);
421 return 0;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200422}
423
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200424int perf_event__synthesize_thread_map(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200425 struct thread_map *threads,
Arnaldo Carvalho de Melo7c940c12011-02-11 11:45:54 -0200426 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300427 struct machine *machine,
428 bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200429{
Don Zickus363b7852014-03-14 10:43:44 -0400430 union perf_event *comm_event, *mmap_event, *fork_event;
David Aherndefd8d32011-12-22 11:30:01 -0700431 int err = -1, thread, j;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200432
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200433 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200434 if (comm_event == NULL)
435 goto out;
436
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200437 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200438 if (mmap_event == NULL)
439 goto out_free_comm;
440
Don Zickus363b7852014-03-14 10:43:44 -0400441 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
442 if (fork_event == NULL)
443 goto out_free_mmap;
444
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200445 err = 0;
446 for (thread = 0; thread < threads->nr; ++thread) {
447 if (__event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400448 fork_event,
David Aherndefd8d32011-12-22 11:30:01 -0700449 threads->map[thread], 0,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300450 process, tool, machine,
451 mmap_data)) {
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200452 err = -1;
453 break;
454 }
David Aherndefd8d32011-12-22 11:30:01 -0700455
456 /*
457 * comm.pid is set to thread group id by
458 * perf_event__synthesize_comm
459 */
460 if ((int) comm_event->comm.pid != threads->map[thread]) {
461 bool need_leader = true;
462
463 /* is thread group leader in thread_map? */
464 for (j = 0; j < threads->nr; ++j) {
465 if ((int) comm_event->comm.pid == threads->map[j]) {
466 need_leader = false;
467 break;
468 }
469 }
470
471 /* if not, generate events for it */
472 if (need_leader &&
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300473 __event__synthesize_thread(comm_event, mmap_event,
Don Zickus363b7852014-03-14 10:43:44 -0400474 fork_event,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300475 comm_event->comm.pid, 0,
476 process, tool, machine,
477 mmap_data)) {
David Aherndefd8d32011-12-22 11:30:01 -0700478 err = -1;
479 break;
480 }
481 }
Arnaldo Carvalho de Melo401b8e12011-02-10 12:52:47 -0200482 }
Don Zickus363b7852014-03-14 10:43:44 -0400483 free(fork_event);
484out_free_mmap:
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200485 free(mmap_event);
486out_free_comm:
487 free(comm_event);
488out:
489 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200490}
491
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200492int perf_event__synthesize_threads(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200493 perf_event__handler_t process,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300494 struct machine *machine, bool mmap_data)
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200495{
496 DIR *proc;
Dongsheng Yang99563462013-12-20 15:52:57 -0500497 char proc_path[PATH_MAX];
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200498 struct dirent dirent, *next;
Don Zickus363b7852014-03-14 10:43:44 -0400499 union perf_event *comm_event, *mmap_event, *fork_event;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200500 int err = -1;
501
Namhyung Kim574799b2014-03-17 10:45:49 +0900502 if (machine__is_default_guest(machine))
503 return 0;
504
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200505 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200506 if (comm_event == NULL)
507 goto out;
508
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200509 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200510 if (mmap_event == NULL)
511 goto out_free_comm;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200512
Don Zickus363b7852014-03-14 10:43:44 -0400513 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
514 if (fork_event == NULL)
515 goto out_free_mmap;
516
Dongsheng Yang99563462013-12-20 15:52:57 -0500517 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
518 proc = opendir(proc_path);
519
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200520 if (proc == NULL)
Don Zickus363b7852014-03-14 10:43:44 -0400521 goto out_free_fork;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200522
523 while (!readdir_r(proc, &dirent, &next) && next) {
524 char *end;
525 pid_t pid = strtol(dirent.d_name, &end, 10);
526
527 if (*end) /* only interested in proper numerical dirents */
528 continue;
Arnaldo Carvalho de Meloba361c92012-10-17 18:50:13 -0300529 /*
530 * We may race with exiting thread, so don't stop just because
531 * one thread couldn't be synthesized.
532 */
Don Zickus363b7852014-03-14 10:43:44 -0400533 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
534 1, process, tool, machine, mmap_data);
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200535 }
536
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200537 err = 0;
David Ahern1e6d5322012-08-26 12:24:42 -0600538 closedir(proc);
Don Zickus363b7852014-03-14 10:43:44 -0400539out_free_fork:
540 free(fork_event);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200541out_free_mmap:
542 free(mmap_event);
543out_free_comm:
544 free(comm_event);
545out:
546 return err;
Arnaldo Carvalho de Melo234fbbf2009-10-26 19:23:18 -0200547}
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200548
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200549struct process_symbol_args {
550 const char *name;
551 u64 start;
552};
553
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200554static int find_symbol_cb(void *arg, const char *name, char type,
Cody P Schafer82151522012-08-10 15:22:48 -0700555 u64 start)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200556{
557 struct process_symbol_args *args = arg;
558
Arnaldo Carvalho de Melo881516e2010-01-15 18:08:27 -0200559 /*
560 * Must be a function or at least an alias, as in PARISC64, where "_text" is
561 * an 'A' to the same address as "_stext".
562 */
563 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
564 type == 'A') || strcmp(name, args->name))
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200565 return 0;
566
567 args->start = start;
568 return 1;
569}
570
Adrian Hunter29b596b2014-01-29 16:14:37 +0200571u64 kallsyms__get_function_start(const char *kallsyms_filename,
572 const char *symbol_name)
573{
574 struct process_symbol_args args = { .name = symbol_name, };
575
576 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
577 return 0;
578
579 return args.start;
580}
581
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200582int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200583 perf_event__handler_t process,
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200584 struct machine *machine)
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200585{
586 size_t size;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200587 const char *mmap_name;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800588 char name_buff[PATH_MAX];
589 struct map *map;
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200590 struct kmap *kmap;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200591 int err;
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700592 union perf_event *event;
593
594 if (machine->vmlinux_maps[0] == NULL)
595 return -1;
596
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200597 /*
598 * We should get this from /sys/kernel/sections/.text, but till that is
599 * available use this, and after it is use this as a fallback for older
600 * kernels.
601 */
Andi Kleena5c2a4c2014-09-24 14:39:54 -0700602 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200603 if (event == NULL) {
604 pr_debug("Not enough memory synthesizing mmap event "
605 "for kernel modules\n");
606 return -1;
607 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200608
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300609 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300610 if (machine__is_host(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800611 /*
612 * kernel uses PERF_RECORD_MISC_USER for user space maps,
613 * see kernel/perf_event.c __perf_event_mmap
614 */
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200615 event->header.misc = PERF_RECORD_MISC_KERNEL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800616 } else {
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200617 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
Thomas Jarosch0b9e01a2013-01-25 11:20:47 +0100618 }
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200619
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300620 map = machine->vmlinux_maps[MAP__FUNCTION];
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200621 kmap = map__kmap(map);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200622 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200623 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300624 size = PERF_ALIGN(size, sizeof(u64));
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200625 event->mmap.header.type = PERF_RECORD_MMAP;
626 event->mmap.header.size = (sizeof(event->mmap) -
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200627 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
Adrian Hunter0ae617b2014-01-29 16:14:40 +0200628 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200629 event->mmap.start = map->start;
630 event->mmap.len = map->end - event->mmap.start;
631 event->mmap.pid = machine->pid;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200632
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200633 err = process(tool, event, &synth_sample, machine);
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200634 free(event);
635
636 return err;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200637}
638
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200639size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
640{
Adrian Hunter022c50d2014-07-14 13:02:27 +0300641 const char *s;
642
643 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
644 s = " exec";
645 else
646 s = "";
647
Arnaldo Carvalho de Melo50674062015-02-24 17:20:31 -0300648 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 -0200649}
650
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300651int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200652 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200653 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200654 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200655{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200656 return machine__process_comm_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200657}
658
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300659int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200660 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200661 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300662 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200663{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200664 return machine__process_lost_event(machine, event, sample);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800665}
666
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200667size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
668{
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300669 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200670 event->mmap.pid, event->mmap.tid, event->mmap.start,
Arnaldo Carvalho de Melo62605dc502013-11-11 09:44:09 -0300671 event->mmap.len, event->mmap.pgoff,
672 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
673 event->mmap.filename);
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200674}
675
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200676size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
677{
678 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
Don Zickus7ef80702014-05-19 15:13:49 -0400679 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200680 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
681 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
682 event->mmap2.min, event->mmap2.ino,
683 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -0400684 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
685 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
686 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
687 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200688 event->mmap2.filename);
689}
690
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300691int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200692 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200693 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200694 struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800695{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200696 return machine__process_mmap_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200697}
698
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200699int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
700 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200701 struct perf_sample *sample,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200702 struct machine *machine)
703{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200704 return machine__process_mmap2_event(machine, event, sample);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200705}
706
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200707size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
708{
709 return fprintf(fp, "(%d:%d):(%d:%d)\n",
710 event->fork.pid, event->fork.tid,
711 event->fork.ppid, event->fork.ptid);
712}
713
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300714int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200715 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200716 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300717 struct machine *machine)
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200718{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200719 return machine__process_fork_event(machine, event, sample);
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200720}
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200721
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300722int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
723 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200724 struct perf_sample *sample,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300725 struct machine *machine)
726{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200727 return machine__process_exit_event(machine, event, sample);
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300728}
729
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200730size_t perf_event__fprintf(union perf_event *event, FILE *fp)
731{
732 size_t ret = fprintf(fp, "PERF_RECORD_%s",
733 perf_event__name(event->header.type));
734
735 switch (event->header.type) {
736 case PERF_RECORD_COMM:
737 ret += perf_event__fprintf_comm(event, fp);
738 break;
739 case PERF_RECORD_FORK:
740 case PERF_RECORD_EXIT:
741 ret += perf_event__fprintf_task(event, fp);
742 break;
743 case PERF_RECORD_MMAP:
744 ret += perf_event__fprintf_mmap(event, fp);
745 break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200746 case PERF_RECORD_MMAP2:
747 ret += perf_event__fprintf_mmap2(event, fp);
748 break;
Arnaldo Carvalho de Melo482ad892011-12-02 11:06:37 -0200749 default:
750 ret += fprintf(fp, "\n");
751 }
752
753 return ret;
754}
755
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300756int perf_event__process(struct perf_tool *tool __maybe_unused,
757 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200758 struct perf_sample *sample,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300759 struct machine *machine)
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530760{
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200761 return machine__process_event(machine, event, sample);
Srikar Dronamrajub83f9202010-08-02 18:08:51 +0530762}
763
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300764void thread__find_addr_map(struct thread *thread, u8 cpumode,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200765 enum map_type type, u64 addr,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300766 struct addr_location *al)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200767{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300768 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300769 struct machine *machine = mg->machine;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300770 bool load_map = false;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200771
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300772 al->machine = machine;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300773 al->thread = thread;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200774 al->addr = addr;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800775 al->cpumode = cpumode;
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300776 al->filtered = 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200777
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200778 if (machine == NULL) {
779 al->map = NULL;
780 return;
781 }
782
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800783 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200784 al->level = 'k';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300785 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300786 load_map = true;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800787 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200788 al->level = '.';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800789 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
790 al->level = 'g';
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300791 mg = &machine->kmaps;
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300792 load_map = true;
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500793 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
794 al->level = 'u';
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800795 } else {
Dongsheng Yangfb50bb42013-12-20 15:52:56 -0500796 al->level = 'H';
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200797 al->map = NULL;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800798
799 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
800 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
801 !perf_guest)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300802 al->filtered |= (1 << HIST_FILTER__GUEST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800803 if ((cpumode == PERF_RECORD_MISC_USER ||
804 cpumode == PERF_RECORD_MISC_KERNEL) &&
805 !perf_host)
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300806 al->filtered |= (1 << HIST_FILTER__HOST);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800807
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200808 return;
809 }
810try_again:
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200811 al->map = map_groups__find(mg, type, al->addr);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200812 if (al->map == NULL) {
813 /*
814 * If this is outside of all known maps, and is a negative
815 * address, try to look it up in the kernel dso, as it might be
816 * a vsyscall or vdso (which executes in user-mode).
817 *
818 * XXX This is nasty, we should have a symbol list in the
819 * "[vdso]" dso, but for now lets use the old trick of looking
820 * in the whole kernel symbol list.
821 */
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300822 if (cpumode == PERF_RECORD_MISC_USER && machine &&
823 mg != &machine->kmaps &&
824 machine__kernel_ip(machine, al->addr)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300825 mg = &machine->kmaps;
Adrian Hunter1f2a7062014-07-14 13:02:31 +0300826 load_map = true;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200827 goto try_again;
828 }
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300829 } else {
830 /*
831 * Kernel maps might be changed when loading symbols so loading
832 * must be done prior to using kernel maps.
833 */
834 if (load_map)
Adrian Hunter326f59b2013-08-08 14:32:27 +0300835 map__load(al->map, machine->symbol_filter);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200836 al->addr = al->map->map_ip(al->map, al->addr);
Adrian Hunter5b7ba822013-08-07 14:38:46 +0300837 }
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200838}
839
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300840void thread__find_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200841 u8 cpumode, enum map_type type, u64 addr,
Adrian Hunter61710bd2013-08-08 14:32:26 +0300842 struct addr_location *al)
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200843{
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300844 thread__find_addr_map(thread, cpumode, type, addr, al);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200845 if (al->map != NULL)
Adrian Hunter61710bd2013-08-08 14:32:26 +0300846 al->sym = map__find_symbol(al->map, al->addr,
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300847 thread->mg->machine->symbol_filter);
Arnaldo Carvalho de Melo59ee68e2010-01-14 23:45:29 -0200848 else
849 al->sym = NULL;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200850}
851
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200852int perf_event__preprocess_sample(const union perf_event *event,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200853 struct machine *machine,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200854 struct addr_location *al,
Adrian Huntere44baa32013-08-08 14:32:25 +0300855 struct perf_sample *sample)
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200856{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200857 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Adrian Hunteref893252013-08-27 11:23:06 +0300858 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +0900859 sample->tid);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200860
861 if (thread == NULL)
862 return -1;
863
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200864 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300865 /*
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200866 * Have we already created the kernel maps for this machine?
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -0300867 *
868 * This should have happened earlier, when we processed the kernel MMAP
869 * events, but for older perf.data files there was no such thing, so do
870 * it now.
871 */
872 if (cpumode == PERF_RECORD_MISC_KERNEL &&
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200873 machine->vmlinux_maps[MAP__FUNCTION] == NULL)
874 machine__create_kernel_maps(machine);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200875
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300876 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200877 dump_printf(" ...... dso: %s\n",
878 al->map ? al->map->dso->long_name :
879 al->level == 'H' ? "[hypervisor]" : "<not found>");
Namhyung Kim466fa762014-03-17 17:12:27 -0300880
881 if (thread__is_filtered(thread))
882 al->filtered |= (1 << HIST_FILTER__THREAD);
883
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300884 al->sym = NULL;
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200885 al->cpu = sample->cpu;
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200886
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300887 if (al->map) {
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900888 struct dso *dso = al->map->dso;
889
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300890 if (symbol_conf.dso_list &&
Namhyung Kimcb8f4e92011-12-13 00:16:55 +0900891 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
892 dso->short_name) ||
893 (dso->short_name != dso->long_name &&
894 strlist__has_entry(symbol_conf.dso_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300895 dso->long_name))))) {
896 al->filtered |= (1 << HIST_FILTER__DSO);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300897 }
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300898
Adrian Huntere44baa32013-08-08 14:32:25 +0300899 al->sym = map__find_symbol(al->map, al->addr,
900 machine->symbol_filter);
Arnaldo Carvalho de Melo96415e42010-03-24 16:40:15 -0300901 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200902
Feng Tang1500b932012-09-07 16:42:23 +0800903 if (symbol_conf.sym_list &&
904 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300905 al->sym->name))) {
906 al->filtered |= (1 << HIST_FILTER__SYMBOL);
Namhyung Kimb3cef7f2014-03-17 16:59:21 -0300907 }
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200908
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200909 return 0;
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200910}
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300911
912bool is_bts_event(struct perf_event_attr *attr)
913{
914 return attr->type == PERF_TYPE_HARDWARE &&
915 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
916 attr->sample_period == 1;
917}
918
919bool sample_addr_correlates_sym(struct perf_event_attr *attr)
920{
921 if (attr->type == PERF_TYPE_SOFTWARE &&
922 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
923 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
924 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
925 return true;
926
927 if (is_bts_event(attr))
928 return true;
929
930 return false;
931}
932
933void perf_event__preprocess_sample_addr(union perf_event *event,
934 struct perf_sample *sample,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300935 struct thread *thread,
936 struct addr_location *al)
937{
938 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
939
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300940 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300941 if (!al->map)
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300942 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
Adrian Hunter9b0d2d82014-07-22 16:17:39 +0300943 sample->addr, al);
944
945 al->cpu = sample->cpu;
946 al->sym = NULL;
947
948 if (al->map)
949 al->sym = map__find_symbol(al->map, al->addr, NULL);
950}