perf tools: Make perf_event__synthesize_mmap_events() scale
[linux-2.6-block.git] / tools / perf / util / event.c
CommitLineData
234fbbf5 1#include <linux/types.h>
2f7db555 2#include <linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
d7e404af 3#include <api/fs/fs.h>
234fbbf5
ACM
4#include "event.h"
5#include "debug.h"
b3cef7f6 6#include "hist.h"
f62d3f0f 7#include "machine.h"
c410a338 8#include "sort.h"
234fbbf5 9#include "string.h"
c410a338 10#include "strlist.h"
62daacb5 11#include "thread.h"
7c940c18 12#include "thread_map.h"
c506c96b 13#include "symbol/kallsyms.h"
67424342
JO
14#include "asm/bug.h"
15#include "stat.h"
234fbbf5 16
8115d60c 17static const char *perf_event__names[] = {
3cb6d154
FW
18 [0] = "TOTAL",
19 [PERF_RECORD_MMAP] = "MMAP",
5c5e854b 20 [PERF_RECORD_MMAP2] = "MMAP2",
3cb6d154
FW
21 [PERF_RECORD_LOST] = "LOST",
22 [PERF_RECORD_COMM] = "COMM",
23 [PERF_RECORD_EXIT] = "EXIT",
24 [PERF_RECORD_THROTTLE] = "THROTTLE",
25 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
26 [PERF_RECORD_FORK] = "FORK",
27 [PERF_RECORD_READ] = "READ",
28 [PERF_RECORD_SAMPLE] = "SAMPLE",
4a96f7a0 29 [PERF_RECORD_AUX] = "AUX",
0ad21f68 30 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
c4937a91 31 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
0286039f
AH
32 [PERF_RECORD_SWITCH] = "SWITCH",
33 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
f3b3614a 34 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
3cb6d154
FW
35 [PERF_RECORD_HEADER_ATTR] = "ATTR",
36 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
37 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
38 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
39 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
3c659eed 40 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
a16ac023
AH
41 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
42 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
e9bf54d2 43 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
5f3339d2 44 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
6640b6c2 45 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
374fb9e3 46 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
d80518c9 47 [PERF_RECORD_STAT] = "STAT",
2d8f0f18 48 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
ffe77725 49 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
46bc29b9 50 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
c8446b9b
ACM
51};
52
f3b3614a
HB
53static const char *perf_ns__names[] = {
54 [NET_NS_INDEX] = "net",
55 [UTS_NS_INDEX] = "uts",
56 [IPC_NS_INDEX] = "ipc",
57 [PID_NS_INDEX] = "pid",
58 [USER_NS_INDEX] = "user",
59 [MNT_NS_INDEX] = "mnt",
60 [CGROUP_NS_INDEX] = "cgroup",
61};
62
8115d60c 63const char *perf_event__name(unsigned int id)
3835bc00 64{
8115d60c 65 if (id >= ARRAY_SIZE(perf_event__names))
3835bc00 66 return "INVALID";
8115d60c 67 if (!perf_event__names[id])
3835bc00 68 return "UNKNOWN";
8115d60c 69 return perf_event__names[id];
3835bc00
TG
70}
71
f3b3614a
HB
72static const char *perf_ns__name(unsigned int id)
73{
74 if (id >= ARRAY_SIZE(perf_ns__names))
75 return "UNKNOWN";
76 return perf_ns__names[id];
77}
78
3ea223ad
ACM
79static int perf_tool__process_synth_event(struct perf_tool *tool,
80 union perf_event *event,
81 struct machine *machine,
82 perf_event__handler_t process)
83{
84 struct perf_sample synth_sample = {
640c03ce
ACM
85 .pid = -1,
86 .tid = -1,
87 .time = -1,
88 .stream_id = -1,
89 .cpu = -1,
90 .period = 1,
3ea223ad
ACM
91 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
92 };
93
94 return process(tool, event, &synth_sample, machine);
640c03ce
ACM
95};
96
5aa0b030
DA
97/*
98 * Assumes that the first 4095 bytes of /proc/pid/stat contains
ca6c41c5 99 * the comm, tgid and ppid.
5aa0b030 100 */
ca6c41c5
DA
101static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
102 pid_t *tgid, pid_t *ppid)
234fbbf5 103{
234fbbf5 104 char filename[PATH_MAX];
5aa0b030
DA
105 char bf[4096];
106 int fd;
38349665
AH
107 size_t size = 0;
108 ssize_t n;
ca6c41c5
DA
109 char *nl, *name, *tgids, *ppids;
110
111 *tgid = -1;
112 *ppid = -1;
234fbbf5
ACM
113
114 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
115
5aa0b030
DA
116 fd = open(filename, O_RDONLY);
117 if (fd < 0) {
234fbbf5 118 pr_debug("couldn't open %s\n", filename);
ca6c41c5 119 return -1;
234fbbf5
ACM
120 }
121
5aa0b030
DA
122 n = read(fd, bf, sizeof(bf) - 1);
123 close(fd);
124 if (n <= 0) {
ca6c41c5 125 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
5aa0b030
DA
126 pid);
127 return -1;
234fbbf5 128 }
5aa0b030 129 bf[n] = '\0';
234fbbf5 130
5aa0b030
DA
131 name = strstr(bf, "Name:");
132 tgids = strstr(bf, "Tgid:");
ca6c41c5 133 ppids = strstr(bf, "PPid:");
5aa0b030
DA
134
135 if (name) {
136 name += 5; /* strlen("Name:") */
137
138 while (*name && isspace(*name))
139 ++name;
140
141 nl = strchr(name, '\n');
142 if (nl)
143 *nl = '\0';
144
145 size = strlen(name);
146 if (size >= len)
147 size = len - 1;
148 memcpy(comm, name, size);
149 comm[size] = '\0';
150 } else {
151 pr_debug("Name: string not found for pid %d\n", pid);
152 }
153
154 if (tgids) {
155 tgids += 5; /* strlen("Tgid:") */
ca6c41c5 156 *tgid = atoi(tgids);
5aa0b030
DA
157 } else {
158 pr_debug("Tgid: string not found for pid %d\n", pid);
159 }
f5faf726 160
ca6c41c5
DA
161 if (ppids) {
162 ppids += 5; /* strlen("PPid:") */
163 *ppid = atoi(ppids);
164 } else {
165 pr_debug("PPid: string not found for pid %d\n", pid);
166 }
167
168 return 0;
f5faf726
DA
169}
170
ca6c41c5
DA
171static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
172 struct machine *machine,
173 pid_t *tgid, pid_t *ppid)
f5faf726 174{
f5faf726 175 size_t size;
ca6c41c5
DA
176
177 *ppid = -1;
f5faf726
DA
178
179 memset(&event->comm, 0, sizeof(event->comm));
180
ca6c41c5
DA
181 if (machine__is_host(machine)) {
182 if (perf_event__get_comm_ids(pid, event->comm.comm,
183 sizeof(event->comm.comm),
184 tgid, ppid) != 0) {
185 return -1;
186 }
187 } else {
188 *tgid = machine->pid;
189 }
f5db57c4 190
ca6c41c5
DA
191 if (*tgid < 0)
192 return -1;
f5faf726 193
ca6c41c5 194 event->comm.pid = *tgid;
9c90a61c 195 event->comm.header.type = PERF_RECORD_COMM;
f5faf726
DA
196
197 size = strlen(event->comm.comm) + 1;
9ac3e487 198 size = PERF_ALIGN(size, sizeof(u64));
743eb868 199 memset(event->comm.comm + size, 0, machine->id_hdr_size);
9c90a61c
ACM
200 event->comm.header.size = (sizeof(event->comm) -
201 (sizeof(event->comm.comm) - size) +
743eb868 202 machine->id_hdr_size);
bfd66cc7 203 event->comm.tid = pid;
ca6c41c5
DA
204
205 return 0;
4aa5f4f7
ACM
206}
207
e803cf97 208pid_t perf_event__synthesize_comm(struct perf_tool *tool,
4aa5f4f7
ACM
209 union perf_event *event, pid_t pid,
210 perf_event__handler_t process,
211 struct machine *machine)
212{
ca6c41c5 213 pid_t tgid, ppid;
4aa5f4f7 214
ca6c41c5
DA
215 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
216 return -1;
f5faf726 217
3ea223ad 218 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
bfd66cc7 219 return -1;
234fbbf5 220
9c90a61c 221 return tgid;
234fbbf5
ACM
222}
223
e907caf3
HB
224static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
225 struct perf_ns_link_info *ns_link_info)
226{
227 struct stat64 st;
228 char proc_ns[128];
229
230 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
231 if (stat64(proc_ns, &st) == 0) {
232 ns_link_info->dev = st.st_dev;
233 ns_link_info->ino = st.st_ino;
234 }
235}
236
237int perf_event__synthesize_namespaces(struct perf_tool *tool,
238 union perf_event *event,
239 pid_t pid, pid_t tgid,
240 perf_event__handler_t process,
241 struct machine *machine)
242{
243 u32 idx;
244 struct perf_ns_link_info *ns_link_info;
245
246 if (!tool || !tool->namespace_events)
247 return 0;
248
249 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
250 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
251 machine->id_hdr_size));
252
253 event->namespaces.pid = tgid;
254 event->namespaces.tid = pid;
255
256 event->namespaces.nr_namespaces = NR_NAMESPACES;
257
258 ns_link_info = event->namespaces.link_info;
259
260 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
261 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
262 &ns_link_info[idx]);
263
264 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
265
266 event->namespaces.header.size = (sizeof(event->namespaces) +
267 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
268 machine->id_hdr_size);
269
270 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
271 return -1;
272
273 return 0;
274}
275
363b785f 276static int perf_event__synthesize_fork(struct perf_tool *tool,
ca6c41c5
DA
277 union perf_event *event,
278 pid_t pid, pid_t tgid, pid_t ppid,
279 perf_event__handler_t process,
363b785f
DZ
280 struct machine *machine)
281{
282 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
283
7764a385
DA
284 /*
285 * for main thread set parent to ppid from status file. For other
286 * threads set parent pid to main thread. ie., assume main thread
287 * spawns all threads in a process
288 */
289 if (tgid == pid) {
290 event->fork.ppid = ppid;
291 event->fork.ptid = ppid;
292 } else {
293 event->fork.ppid = tgid;
294 event->fork.ptid = tgid;
295 }
363b785f
DZ
296 event->fork.pid = tgid;
297 event->fork.tid = pid;
298 event->fork.header.type = PERF_RECORD_FORK;
299
300 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
301
3ea223ad 302 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
363b785f
DZ
303 return -1;
304
305 return 0;
306}
307
a18382b6
JO
308int perf_event__synthesize_mmap_events(struct perf_tool *tool,
309 union perf_event *event,
310 pid_t pid, pid_t tgid,
311 perf_event__handler_t process,
312 struct machine *machine,
9d9cad76
KL
313 bool mmap_data,
314 unsigned int proc_map_timeout)
234fbbf5
ACM
315{
316 char filename[PATH_MAX];
317 FILE *fp;
930e6fcd
KL
318 unsigned long long t;
319 bool truncation = false;
9d9cad76 320 unsigned long long timeout = proc_map_timeout * 1000000ULL;
1e6d5322 321 int rc = 0;
d7e404af
WN
322 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
323 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
234fbbf5 324
c239c25a
DY
325 if (machine__is_default_guest(machine))
326 return 0;
327
88b897a3
SE
328 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
329 machine->root_dir, pid, pid);
234fbbf5
ACM
330
331 fp = fopen(filename, "r");
332 if (fp == NULL) {
333 /*
334 * We raced with a task exiting - just return:
335 */
336 pr_debug("couldn't open %s\n", filename);
337 return -1;
338 }
339
a5a5ba72 340 event->header.type = PERF_RECORD_MMAP2;
930e6fcd 341 t = rdclock();
9c90a61c 342
234fbbf5 343 while (1) {
60648033
NK
344 char bf[BUFSIZ];
345 char prot[5];
346 char execname[PATH_MAX];
347 char anonstr[] = "//anon";
a5a5ba72 348 unsigned int ino;
234fbbf5 349 size_t size;
5c5e854b 350 ssize_t n;
60648033 351
234fbbf5
ACM
352 if (fgets(bf, sizeof(bf), fp) == NULL)
353 break;
354
9d9cad76
KL
355 if ((rdclock() - t) > timeout) {
356 pr_warning("Reading %s time out. "
357 "You may want to increase "
358 "the time limit by --proc-map-timeout\n",
359 filename);
930e6fcd
KL
360 truncation = true;
361 goto out;
362 }
363
60648033
NK
364 /* ensure null termination since stack will be reused. */
365 strcpy(execname, "");
366
234fbbf5 367 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
89fee59b 368 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
a5a5ba72
DZ
369 &event->mmap2.start, &event->mmap2.len, prot,
370 &event->mmap2.pgoff, &event->mmap2.maj,
371 &event->mmap2.min,
372 &ino, execname);
373
9d4ecc88
DZ
374 /*
375 * Anon maps don't have the execname.
376 */
a5a5ba72 377 if (n < 7)
5c5e854b 378 continue;
a5a5ba72
DZ
379
380 event->mmap2.ino = (u64)ino;
381
62605dc5
ACM
382 /*
383 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
384 */
73547aac
DY
385 if (machine__is_host(machine))
386 event->header.misc = PERF_RECORD_MISC_USER;
387 else
388 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
60648033 389
7ef80703
DZ
390 /* map protection and flags bits */
391 event->mmap2.prot = 0;
392 event->mmap2.flags = 0;
393 if (prot[0] == 'r')
394 event->mmap2.prot |= PROT_READ;
395 if (prot[1] == 'w')
396 event->mmap2.prot |= PROT_WRITE;
397 if (prot[2] == 'x')
398 event->mmap2.prot |= PROT_EXEC;
399
400 if (prot[3] == 's')
401 event->mmap2.flags |= MAP_SHARED;
402 else
403 event->mmap2.flags |= MAP_PRIVATE;
404
62605dc5
ACM
405 if (prot[2] != 'x') {
406 if (!mmap_data || prot[0] != 'r')
407 continue;
408
409 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
410 }
60648033 411
930e6fcd
KL
412out:
413 if (truncation)
414 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
415
60648033
NK
416 if (!strcmp(execname, ""))
417 strcpy(execname, anonstr);
fbef103f 418
973186ca
AH
419 if (hugetlbfs_mnt_len &&
420 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
d7e404af
WN
421 strcpy(execname, anonstr);
422 event->mmap2.flags |= MAP_HUGETLB;
423 }
60648033
NK
424
425 size = strlen(execname) + 1;
a5a5ba72 426 memcpy(event->mmap2.filename, execname, size);
60648033 427 size = PERF_ALIGN(size, sizeof(u64));
a5a5ba72
DZ
428 event->mmap2.len -= event->mmap.start;
429 event->mmap2.header.size = (sizeof(event->mmap2) -
430 (sizeof(event->mmap2.filename) - size));
431 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
432 event->mmap2.header.size += machine->id_hdr_size;
433 event->mmap2.pid = tgid;
434 event->mmap2.tid = pid;
60648033 435
3ea223ad 436 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
60648033
NK
437 rc = -1;
438 break;
234fbbf5 439 }
930e6fcd
KL
440
441 if (truncation)
442 break;
234fbbf5
ACM
443 }
444
445 fclose(fp);
1e6d5322 446 return rc;
234fbbf5
ACM
447}
448
45694aa7 449int perf_event__synthesize_modules(struct perf_tool *tool,
d20deb64 450 perf_event__handler_t process,
8115d60c 451 struct machine *machine)
b7cece76 452{
1e6d5322 453 int rc = 0;
4bb7123d 454 struct map *pos;
23346f21 455 struct map_groups *kmaps = &machine->kmaps;
1eee78ae 456 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
8115d60c 457 union perf_event *event = zalloc((sizeof(event->mmap) +
743eb868 458 machine->id_hdr_size));
9c90a61c
ACM
459 if (event == NULL) {
460 pr_debug("Not enough memory synthesizing mmap event "
461 "for kernel modules\n");
462 return -1;
463 }
464
465 event->header.type = PERF_RECORD_MMAP;
b7cece76 466
a1645ce1
ZY
467 /*
468 * kernel uses 0 for user space maps, see kernel/perf_event.c
469 * __perf_event_mmap
470 */
23346f21 471 if (machine__is_host(machine))
9c90a61c 472 event->header.misc = PERF_RECORD_MISC_KERNEL;
a1645ce1 473 else
9c90a61c 474 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
a1645ce1 475
4bb7123d 476 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
b7cece76 477 size_t size;
b7cece76 478
ab9c2bdc 479 if (__map__is_kernel(pos))
b7cece76
ACM
480 continue;
481
9ac3e487 482 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
9c90a61c
ACM
483 event->mmap.header.type = PERF_RECORD_MMAP;
484 event->mmap.header.size = (sizeof(event->mmap) -
485 (sizeof(event->mmap.filename) - size));
743eb868
ACM
486 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
487 event->mmap.header.size += machine->id_hdr_size;
9c90a61c
ACM
488 event->mmap.start = pos->start;
489 event->mmap.len = pos->end - pos->start;
490 event->mmap.pid = machine->pid;
491
492 memcpy(event->mmap.filename, pos->dso->long_name,
b7cece76 493 pos->dso->long_name_len + 1);
3ea223ad 494 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
1e6d5322
DA
495 rc = -1;
496 break;
497 }
b7cece76
ACM
498 }
499
9c90a61c 500 free(event);
1e6d5322 501 return rc;
b7cece76
ACM
502}
503
8115d60c
ACM
504static int __event__synthesize_thread(union perf_event *comm_event,
505 union perf_event *mmap_event,
363b785f 506 union perf_event *fork_event,
e907caf3 507 union perf_event *namespaces_event,
defd8d38 508 pid_t pid, int full,
e907caf3 509 perf_event__handler_t process,
45694aa7 510 struct perf_tool *tool,
9d9cad76
KL
511 struct machine *machine,
512 bool mmap_data,
513 unsigned int proc_map_timeout)
234fbbf5 514{
bfd66cc7
DZ
515 char filename[PATH_MAX];
516 DIR *tasks;
7093b4c9 517 struct dirent *dirent;
ca6c41c5 518 pid_t tgid, ppid;
d998b732 519 int rc = 0;
bfd66cc7
DZ
520
521 /* special case: only send one comm event using passed in pid */
522 if (!full) {
523 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
524 process, machine);
525
526 if (tgid == -1)
527 return -1;
528
e907caf3
HB
529 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
530 tgid, process, machine) < 0)
531 return -1;
532
533
bfd66cc7 534 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
9d9cad76
KL
535 process, machine, mmap_data,
536 proc_map_timeout);
bfd66cc7
DZ
537 }
538
539 if (machine__is_default_guest(machine))
540 return 0;
541
542 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
543 machine->root_dir, pid);
544
545 tasks = opendir(filename);
546 if (tasks == NULL) {
547 pr_debug("couldn't open %s\n", filename);
548 return 0;
549 }
550
7093b4c9 551 while ((dirent = readdir(tasks)) != NULL) {
bfd66cc7 552 char *end;
bfd66cc7
DZ
553 pid_t _pid;
554
7093b4c9 555 _pid = strtol(dirent->d_name, &end, 10);
bfd66cc7
DZ
556 if (*end)
557 continue;
558
d998b732 559 rc = -1;
ca6c41c5
DA
560 if (perf_event__prepare_comm(comm_event, _pid, machine,
561 &tgid, &ppid) != 0)
d998b732 562 break;
bfd66cc7 563
4aa5f4f7 564 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
ca6c41c5 565 ppid, process, machine) < 0)
d998b732 566 break;
e907caf3
HB
567
568 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
569 tgid, process, machine) < 0)
570 break;
571
4aa5f4f7
ACM
572 /*
573 * Send the prepared comm event
574 */
3ea223ad 575 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
d998b732 576 break;
4aa5f4f7 577
d998b732 578 rc = 0;
363b785f
DZ
579 if (_pid == pid) {
580 /* process the parent's maps too */
581 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
9d9cad76 582 process, machine, mmap_data, proc_map_timeout);
d998b732
ACM
583 if (rc)
584 break;
363b785f 585 }
bfd66cc7
DZ
586 }
587
588 closedir(tasks);
d998b732 589 return rc;
234fbbf5
ACM
590}
591
45694aa7 592int perf_event__synthesize_thread_map(struct perf_tool *tool,
d20deb64 593 struct thread_map *threads,
7c940c18 594 perf_event__handler_t process,
62605dc5 595 struct machine *machine,
9d9cad76
KL
596 bool mmap_data,
597 unsigned int proc_map_timeout)
9c90a61c 598{
363b785f 599 union perf_event *comm_event, *mmap_event, *fork_event;
e907caf3 600 union perf_event *namespaces_event;
defd8d38 601 int err = -1, thread, j;
9c90a61c 602
743eb868 603 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
9c90a61c
ACM
604 if (comm_event == NULL)
605 goto out;
606
b0fb978e 607 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
9c90a61c
ACM
608 if (mmap_event == NULL)
609 goto out_free_comm;
610
363b785f
DZ
611 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
612 if (fork_event == NULL)
613 goto out_free_mmap;
614
e907caf3
HB
615 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
616 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
617 machine->id_hdr_size);
618 if (namespaces_event == NULL)
619 goto out_free_fork;
620
401b8e13
ACM
621 err = 0;
622 for (thread = 0; thread < threads->nr; ++thread) {
623 if (__event__synthesize_thread(comm_event, mmap_event,
e907caf3 624 fork_event, namespaces_event,
e13798c7 625 thread_map__pid(threads, thread), 0,
62605dc5 626 process, tool, machine,
9d9cad76 627 mmap_data, proc_map_timeout)) {
401b8e13
ACM
628 err = -1;
629 break;
630 }
defd8d38
DA
631
632 /*
633 * comm.pid is set to thread group id by
634 * perf_event__synthesize_comm
635 */
e13798c7 636 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
defd8d38
DA
637 bool need_leader = true;
638
639 /* is thread group leader in thread_map? */
640 for (j = 0; j < threads->nr; ++j) {
e13798c7 641 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
defd8d38
DA
642 need_leader = false;
643 break;
644 }
645 }
646
647 /* if not, generate events for it */
648 if (need_leader &&
62605dc5 649 __event__synthesize_thread(comm_event, mmap_event,
e907caf3 650 fork_event, namespaces_event,
62605dc5
ACM
651 comm_event->comm.pid, 0,
652 process, tool, machine,
9d9cad76 653 mmap_data, proc_map_timeout)) {
defd8d38
DA
654 err = -1;
655 break;
656 }
657 }
401b8e13 658 }
e907caf3
HB
659 free(namespaces_event);
660out_free_fork:
363b785f
DZ
661 free(fork_event);
662out_free_mmap:
9c90a61c
ACM
663 free(mmap_event);
664out_free_comm:
665 free(comm_event);
666out:
667 return err;
668}
669
45694aa7 670int perf_event__synthesize_threads(struct perf_tool *tool,
d20deb64 671 perf_event__handler_t process,
9d9cad76
KL
672 struct machine *machine,
673 bool mmap_data,
674 unsigned int proc_map_timeout)
234fbbf5
ACM
675{
676 DIR *proc;
99563465 677 char proc_path[PATH_MAX];
7093b4c9 678 struct dirent *dirent;
363b785f 679 union perf_event *comm_event, *mmap_event, *fork_event;
e907caf3 680 union perf_event *namespaces_event;
9c90a61c
ACM
681 int err = -1;
682
574799bf
NK
683 if (machine__is_default_guest(machine))
684 return 0;
685
743eb868 686 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
9c90a61c
ACM
687 if (comm_event == NULL)
688 goto out;
689
b0fb978e 690 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
9c90a61c
ACM
691 if (mmap_event == NULL)
692 goto out_free_comm;
234fbbf5 693
363b785f
DZ
694 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
695 if (fork_event == NULL)
696 goto out_free_mmap;
697
e907caf3
HB
698 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
699 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
700 machine->id_hdr_size);
701 if (namespaces_event == NULL)
702 goto out_free_fork;
703
99563465
DY
704 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
705 proc = opendir(proc_path);
706
9c90a61c 707 if (proc == NULL)
e907caf3 708 goto out_free_namespaces;
234fbbf5 709
7093b4c9 710 while ((dirent = readdir(proc)) != NULL) {
234fbbf5 711 char *end;
7093b4c9 712 pid_t pid = strtol(dirent->d_name, &end, 10);
234fbbf5
ACM
713
714 if (*end) /* only interested in proper numerical dirents */
715 continue;
ba361c92
ACM
716 /*
717 * We may race with exiting thread, so don't stop just because
718 * one thread couldn't be synthesized.
719 */
e907caf3
HB
720 __event__synthesize_thread(comm_event, mmap_event, fork_event,
721 namespaces_event, pid, 1, process,
722 tool, machine, mmap_data,
9d9cad76 723 proc_map_timeout);
234fbbf5
ACM
724 }
725
9c90a61c 726 err = 0;
1e6d5322 727 closedir(proc);
e907caf3
HB
728out_free_namespaces:
729 free(namespaces_event);
363b785f
DZ
730out_free_fork:
731 free(fork_event);
9c90a61c
ACM
732out_free_mmap:
733 free(mmap_event);
734out_free_comm:
735 free(comm_event);
736out:
737 return err;
234fbbf5 738}
62daacb5 739
56b03f3c
ACM
740struct process_symbol_args {
741 const char *name;
742 u64 start;
743};
744
3b01a413 745static int find_symbol_cb(void *arg, const char *name, char type,
82151520 746 u64 start)
56b03f3c
ACM
747{
748 struct process_symbol_args *args = arg;
749
881516eb
ACM
750 /*
751 * Must be a function or at least an alias, as in PARISC64, where "_text" is
752 * an 'A' to the same address as "_stext".
753 */
754 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
755 type == 'A') || strcmp(name, args->name))
56b03f3c
ACM
756 return 0;
757
758 args->start = start;
759 return 1;
760}
761
29b596b5
AH
762u64 kallsyms__get_function_start(const char *kallsyms_filename,
763 const char *symbol_name)
764{
765 struct process_symbol_args args = { .name = symbol_name, };
766
767 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
768 return 0;
769
770 return args.start;
771}
772
45694aa7 773int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
d20deb64 774 perf_event__handler_t process,
0ae617be 775 struct machine *machine)
56b03f3c
ACM
776{
777 size_t size;
0ae617be 778 const char *mmap_name;
a1645ce1 779 char name_buff[PATH_MAX];
a5e813c6 780 struct map *map = machine__kernel_map(machine);
0ae617be 781 struct kmap *kmap;
9c90a61c 782 int err;
a5c2a4c9
AK
783 union perf_event *event;
784
3dc6c1d5
WN
785 if (symbol_conf.kptr_restrict)
786 return -1;
77e65977 787 if (map == NULL)
a5c2a4c9
AK
788 return -1;
789
56b03f3c
ACM
790 /*
791 * We should get this from /sys/kernel/sections/.text, but till that is
792 * available use this, and after it is use this as a fallback for older
793 * kernels.
794 */
a5c2a4c9 795 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
9c90a61c
ACM
796 if (event == NULL) {
797 pr_debug("Not enough memory synthesizing mmap event "
798 "for kernel modules\n");
799 return -1;
800 }
56b03f3c 801
48ea8f54 802 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
23346f21 803 if (machine__is_host(machine)) {
a1645ce1
ZY
804 /*
805 * kernel uses PERF_RECORD_MISC_USER for user space maps,
806 * see kernel/perf_event.c __perf_event_mmap
807 */
9c90a61c 808 event->header.misc = PERF_RECORD_MISC_KERNEL;
a1645ce1 809 } else {
9c90a61c 810 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
0b9e01a4 811 }
56b03f3c 812
0ae617be 813 kmap = map__kmap(map);
9c90a61c 814 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
0ae617be 815 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
9ac3e487 816 size = PERF_ALIGN(size, sizeof(u64));
9c90a61c
ACM
817 event->mmap.header.type = PERF_RECORD_MMAP;
818 event->mmap.header.size = (sizeof(event->mmap) -
743eb868 819 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
0ae617be 820 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
9c90a61c
ACM
821 event->mmap.start = map->start;
822 event->mmap.len = map->end - event->mmap.start;
823 event->mmap.pid = machine->pid;
824
3ea223ad 825 err = perf_tool__process_synth_event(tool, event, machine, process);
9c90a61c
ACM
826 free(event);
827
828 return err;
56b03f3c
ACM
829}
830
99471c96
JO
831int perf_event__synthesize_thread_map2(struct perf_tool *tool,
832 struct thread_map *threads,
833 perf_event__handler_t process,
834 struct machine *machine)
835{
836 union perf_event *event;
837 int i, err, size;
838
839 size = sizeof(event->thread_map);
840 size += threads->nr * sizeof(event->thread_map.entries[0]);
841
842 event = zalloc(size);
843 if (!event)
844 return -ENOMEM;
845
846 event->header.type = PERF_RECORD_THREAD_MAP;
847 event->header.size = size;
848 event->thread_map.nr = threads->nr;
849
850 for (i = 0; i < threads->nr; i++) {
851 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
852 char *comm = thread_map__comm(threads, i);
853
854 if (!comm)
855 comm = (char *) "";
856
857 entry->pid = thread_map__pid(threads, i);
858 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
859 }
860
861 err = process(tool, event, NULL, machine);
862
863 free(event);
864 return err;
865}
866
6c872901
JO
867static void synthesize_cpus(struct cpu_map_entries *cpus,
868 struct cpu_map *map)
869{
870 int i;
871
872 cpus->nr = map->nr;
873
874 for (i = 0; i < map->nr; i++)
875 cpus->cpu[i] = map->map[i];
876}
877
878static void synthesize_mask(struct cpu_map_mask *mask,
879 struct cpu_map *map, int max)
880{
881 int i;
882
883 mask->nr = BITS_TO_LONGS(max);
884 mask->long_size = sizeof(long);
885
886 for (i = 0; i < map->nr; i++)
887 set_bit(map->map[i], mask->mask);
888}
889
890static size_t cpus_size(struct cpu_map *map)
891{
892 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
893}
894
895static size_t mask_size(struct cpu_map *map, int *max)
896{
897 int i;
898
899 *max = 0;
900
901 for (i = 0; i < map->nr; i++) {
902 /* bit possition of the cpu is + 1 */
903 int bit = map->map[i] + 1;
904
905 if (bit > *max)
906 *max = bit;
907 }
908
909 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
910}
911
912void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
913{
914 size_t size_cpus, size_mask;
915 bool is_dummy = cpu_map__empty(map);
916
917 /*
918 * Both array and mask data have variable size based
919 * on the number of cpus and their actual values.
920 * The size of the 'struct cpu_map_data' is:
921 *
922 * array = size of 'struct cpu_map_entries' +
923 * number of cpus * sizeof(u64)
924 *
925 * mask = size of 'struct cpu_map_mask' +
926 * maximum cpu bit converted to size of longs
927 *
928 * and finaly + the size of 'struct cpu_map_data'.
929 */
930 size_cpus = cpus_size(map);
931 size_mask = mask_size(map, max);
932
933 if (is_dummy || (size_cpus < size_mask)) {
934 *size += size_cpus;
935 *type = PERF_CPU_MAP__CPUS;
936 } else {
937 *size += size_mask;
938 *type = PERF_CPU_MAP__MASK;
939 }
940
941 *size += sizeof(struct cpu_map_data);
942 return zalloc(*size);
943}
944
945void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
946 u16 type, int max)
947{
948 data->type = type;
949
950 switch (type) {
951 case PERF_CPU_MAP__CPUS:
952 synthesize_cpus((struct cpu_map_entries *) data->data, map);
953 break;
954 case PERF_CPU_MAP__MASK:
955 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
956 default:
957 break;
958 };
959}
960
961static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
962{
963 size_t size = sizeof(struct cpu_map_event);
964 struct cpu_map_event *event;
965 int max;
966 u16 type;
967
968 event = cpu_map_data__alloc(map, &size, &type, &max);
969 if (!event)
970 return NULL;
971
972 event->header.type = PERF_RECORD_CPU_MAP;
973 event->header.size = size;
974 event->data.type = type;
975
976 cpu_map_data__synthesize(&event->data, map, type, max);
977 return event;
978}
979
980int perf_event__synthesize_cpu_map(struct perf_tool *tool,
981 struct cpu_map *map,
982 perf_event__handler_t process,
983 struct machine *machine)
984{
985 struct cpu_map_event *event;
986 int err;
987
988 event = cpu_map_event__new(map);
989 if (!event)
990 return -ENOMEM;
991
992 err = process(tool, (union perf_event *) event, NULL, machine);
993
994 free(event);
995 return err;
996}
997
67424342
JO
998int perf_event__synthesize_stat_config(struct perf_tool *tool,
999 struct perf_stat_config *config,
1000 perf_event__handler_t process,
1001 struct machine *machine)
1002{
1003 struct stat_config_event *event;
1004 int size, i = 0, err;
1005
1006 size = sizeof(*event);
1007 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1008
1009 event = zalloc(size);
1010 if (!event)
1011 return -ENOMEM;
1012
1013 event->header.type = PERF_RECORD_STAT_CONFIG;
1014 event->header.size = size;
1015 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1016
1017#define ADD(__term, __val) \
1018 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1019 event->data[i].val = __val; \
1020 i++;
1021
1022 ADD(AGGR_MODE, config->aggr_mode)
1023 ADD(INTERVAL, config->interval)
1024 ADD(SCALE, config->scale)
1025
1026 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1027 "stat config terms unbalanced\n");
1028#undef ADD
1029
1030 err = process(tool, (union perf_event *) event, NULL, machine);
1031
1032 free(event);
1033 return err;
1034}
1035
5796f8f0
JO
1036int perf_event__synthesize_stat(struct perf_tool *tool,
1037 u32 cpu, u32 thread, u64 id,
1038 struct perf_counts_values *count,
1039 perf_event__handler_t process,
1040 struct machine *machine)
1041{
1042 struct stat_event event;
1043
1044 event.header.type = PERF_RECORD_STAT;
1045 event.header.size = sizeof(event);
1046 event.header.misc = 0;
1047
1048 event.id = id;
1049 event.cpu = cpu;
1050 event.thread = thread;
1051 event.val = count->val;
1052 event.ena = count->ena;
1053 event.run = count->run;
1054
1055 return process(tool, (union perf_event *) &event, NULL, machine);
1056}
1057
d4c22591
JO
1058int perf_event__synthesize_stat_round(struct perf_tool *tool,
1059 u64 evtime, u64 type,
1060 perf_event__handler_t process,
1061 struct machine *machine)
1062{
1063 struct stat_round_event event;
1064
1065 event.header.type = PERF_RECORD_STAT_ROUND;
1066 event.header.size = sizeof(event);
1067 event.header.misc = 0;
1068
1069 event.time = evtime;
1070 event.type = type;
1071
1072 return process(tool, (union perf_event *) &event, NULL, machine);
1073}
1074
8e381596
JO
1075void perf_event__read_stat_config(struct perf_stat_config *config,
1076 struct stat_config_event *event)
1077{
1078 unsigned i;
1079
1080 for (i = 0; i < event->nr; i++) {
1081
1082 switch (event->data[i].tag) {
1083#define CASE(__term, __val) \
1084 case PERF_STAT_CONFIG_TERM__##__term: \
1085 config->__val = event->data[i].val; \
1086 break;
1087
1088 CASE(AGGR_MODE, aggr_mode)
1089 CASE(SCALE, scale)
1090 CASE(INTERVAL, interval)
1091#undef CASE
1092 default:
1093 pr_warning("unknown stat config term %" PRIu64 "\n",
1094 event->data[i].tag);
1095 }
1096 }
1097}
1098
482ad897
ACM
1099size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1100{
022c50d0
AH
1101 const char *s;
1102
1103 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1104 s = " exec";
1105 else
1106 s = "";
1107
50674065 1108 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
482ad897
ACM
1109}
1110
f3b3614a
HB
1111size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1112{
1113 size_t ret = 0;
1114 struct perf_ns_link_info *ns_link_info;
1115 u32 nr_namespaces, idx;
1116
1117 ns_link_info = event->namespaces.link_info;
1118 nr_namespaces = event->namespaces.nr_namespaces;
1119
1120 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1121 event->namespaces.pid,
1122 event->namespaces.tid,
1123 nr_namespaces);
1124
1125 for (idx = 0; idx < nr_namespaces; idx++) {
1126 if (idx && (idx % 4 == 0))
1127 ret += fprintf(fp, "\n\t\t ");
1128
1129 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1130 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1131 (u64)ns_link_info[idx].ino,
1132 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1133 }
1134
1135 return ret;
1136}
1137
1d037ca1 1138int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
d20deb64 1139 union perf_event *event,
162f0bef 1140 struct perf_sample *sample,
743eb868 1141 struct machine *machine)
62daacb5 1142{
162f0bef 1143 return machine__process_comm_event(machine, event, sample);
62daacb5
ACM
1144}
1145
f3b3614a
HB
1146int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1147 union perf_event *event,
1148 struct perf_sample *sample,
1149 struct machine *machine)
1150{
1151 return machine__process_namespaces_event(machine, event, sample);
1152}
1153
1d037ca1 1154int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
d20deb64 1155 union perf_event *event,
162f0bef 1156 struct perf_sample *sample,
b0a7d1a0 1157 struct machine *machine)
62daacb5 1158{
162f0bef 1159 return machine__process_lost_event(machine, event, sample);
a1645ce1 1160}
b7cece76 1161
4a96f7a0
AH
1162int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1163 union perf_event *event,
1164 struct perf_sample *sample __maybe_unused,
1165 struct machine *machine)
1166{
1167 return machine__process_aux_event(machine, event);
1168}
1169
0ad21f68
AH
1170int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1171 union perf_event *event,
1172 struct perf_sample *sample __maybe_unused,
1173 struct machine *machine)
1174{
1175 return machine__process_itrace_start_event(machine, event);
1176}
1177
c4937a91
KL
1178int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1179 union perf_event *event,
1180 struct perf_sample *sample,
1181 struct machine *machine)
1182{
1183 return machine__process_lost_samples_event(machine, event, sample);
1184}
1185
0286039f
AH
1186int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1187 union perf_event *event,
1188 struct perf_sample *sample __maybe_unused,
1189 struct machine *machine)
1190{
1191 return machine__process_switch_event(machine, event);
1192}
1193
482ad897
ACM
1194size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1195{
62605dc5 1196 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
482ad897 1197 event->mmap.pid, event->mmap.tid, event->mmap.start,
62605dc5
ACM
1198 event->mmap.len, event->mmap.pgoff,
1199 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1200 event->mmap.filename);
482ad897
ACM
1201}
1202
5c5e854b
SE
1203size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1204{
1205 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
7ef80703 1206 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
5c5e854b
SE
1207 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1208 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1209 event->mmap2.min, event->mmap2.ino,
1210 event->mmap2.ino_generation,
7ef80703
DZ
1211 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1212 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1213 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1214 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
5c5e854b
SE
1215 event->mmap2.filename);
1216}
1217
ec7fa596
JO
1218size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1219{
1220 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1221 size_t ret;
1222
1223 ret = fprintf(fp, " nr: ");
1224
1225 if (threads)
1226 ret += thread_map__fprintf(threads, fp);
1227 else
1228 ret += fprintf(fp, "failed to get threads from event\n");
1229
1230 thread_map__put(threads);
1231 return ret;
1232}
1233
eb12a1af
JO
1234size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1235{
1236 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1237 size_t ret;
1238
a24020e6 1239 ret = fprintf(fp, ": ");
eb12a1af
JO
1240
1241 if (cpus)
1242 ret += cpu_map__fprintf(cpus, fp);
1243 else
1244 ret += fprintf(fp, "failed to get cpumap from event\n");
1245
1246 cpu_map__put(cpus);
1247 return ret;
1248}
1249
b0a7d1a0 1250int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
d20deb64 1251 union perf_event *event,
162f0bef 1252 struct perf_sample *sample,
743eb868 1253 struct machine *machine)
a1645ce1 1254{
162f0bef 1255 return machine__process_mmap_event(machine, event, sample);
62daacb5
ACM
1256}
1257
5c5e854b
SE
1258int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1259 union perf_event *event,
162f0bef 1260 struct perf_sample *sample,
5c5e854b
SE
1261 struct machine *machine)
1262{
162f0bef 1263 return machine__process_mmap2_event(machine, event, sample);
5c5e854b
SE
1264}
1265
482ad897
ACM
1266size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1267{
1268 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1269 event->fork.pid, event->fork.tid,
1270 event->fork.ppid, event->fork.ptid);
1271}
1272
f62d3f0f 1273int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
d20deb64 1274 union perf_event *event,
162f0bef 1275 struct perf_sample *sample,
f62d3f0f 1276 struct machine *machine)
62daacb5 1277{
162f0bef 1278 return machine__process_fork_event(machine, event, sample);
62daacb5 1279}
1ed091c4 1280
f62d3f0f
ACM
1281int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1282 union perf_event *event,
162f0bef 1283 struct perf_sample *sample,
f62d3f0f
ACM
1284 struct machine *machine)
1285{
162f0bef 1286 return machine__process_exit_event(machine, event, sample);
f62d3f0f
ACM
1287}
1288
4a96f7a0
AH
1289size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1290{
1291 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
1292 event->aux.aux_offset, event->aux.aux_size,
1293 event->aux.flags,
1294 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1295 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
1296}
1297
0ad21f68
AH
1298size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1299{
1300 return fprintf(fp, " pid: %u tid: %u\n",
1301 event->itrace_start.pid, event->itrace_start.tid);
1302}
1303
0286039f
AH
1304size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1305{
1306 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1307 const char *in_out = out ? "OUT" : "IN ";
1308
1309 if (event->header.type == PERF_RECORD_SWITCH)
1310 return fprintf(fp, " %s\n", in_out);
1311
1312 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1313 in_out, out ? "next" : "prev",
1314 event->context_switch.next_prev_pid,
1315 event->context_switch.next_prev_tid);
1316}
1317
482ad897
ACM
1318size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1319{
1320 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1321 perf_event__name(event->header.type));
1322
1323 switch (event->header.type) {
1324 case PERF_RECORD_COMM:
1325 ret += perf_event__fprintf_comm(event, fp);
1326 break;
1327 case PERF_RECORD_FORK:
1328 case PERF_RECORD_EXIT:
1329 ret += perf_event__fprintf_task(event, fp);
1330 break;
1331 case PERF_RECORD_MMAP:
1332 ret += perf_event__fprintf_mmap(event, fp);
f3b3614a
HB
1333 break;
1334 case PERF_RECORD_NAMESPACES:
1335 ret += perf_event__fprintf_namespaces(event, fp);
482ad897 1336 break;
5c5e854b
SE
1337 case PERF_RECORD_MMAP2:
1338 ret += perf_event__fprintf_mmap2(event, fp);
1339 break;
4a96f7a0
AH
1340 case PERF_RECORD_AUX:
1341 ret += perf_event__fprintf_aux(event, fp);
1342 break;
0ad21f68
AH
1343 case PERF_RECORD_ITRACE_START:
1344 ret += perf_event__fprintf_itrace_start(event, fp);
1345 break;
0286039f
AH
1346 case PERF_RECORD_SWITCH:
1347 case PERF_RECORD_SWITCH_CPU_WIDE:
1348 ret += perf_event__fprintf_switch(event, fp);
1349 break;
482ad897
ACM
1350 default:
1351 ret += fprintf(fp, "\n");
1352 }
1353
1354 return ret;
1355}
1356
b0a7d1a0
ACM
1357int perf_event__process(struct perf_tool *tool __maybe_unused,
1358 union perf_event *event,
162f0bef 1359 struct perf_sample *sample,
b0a7d1a0 1360 struct machine *machine)
b83f920e 1361{
162f0bef 1362 return machine__process_event(machine, event, sample);
b83f920e
SD
1363}
1364
bb871a9c 1365void thread__find_addr_map(struct thread *thread, u8 cpumode,
743eb868 1366 enum map_type type, u64 addr,
326f59bf 1367 struct addr_location *al)
1ed091c4 1368{
93d5731d 1369 struct map_groups *mg = thread->mg;
bb871a9c 1370 struct machine *machine = mg->machine;
5b7ba82a 1371 bool load_map = false;
1ed091c4 1372
cc22e575 1373 al->machine = machine;
316c7136 1374 al->thread = thread;
1ed091c4 1375 al->addr = addr;
a1645ce1 1376 al->cpumode = cpumode;
b3cef7f6 1377 al->filtered = 0;
1ed091c4 1378
743eb868
ACM
1379 if (machine == NULL) {
1380 al->map = NULL;
1381 return;
1382 }
1383
a1645ce1 1384 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1ed091c4 1385 al->level = 'k';
23346f21 1386 mg = &machine->kmaps;
5b7ba82a 1387 load_map = true;
a1645ce1 1388 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1ed091c4 1389 al->level = '.';
a1645ce1
ZY
1390 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1391 al->level = 'g';
23346f21 1392 mg = &machine->kmaps;
5b7ba82a 1393 load_map = true;
fb50bb43
DY
1394 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1395 al->level = 'u';
a1645ce1 1396 } else {
fb50bb43 1397 al->level = 'H';
1ed091c4 1398 al->map = NULL;
a1645ce1
ZY
1399
1400 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1401 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1402 !perf_guest)
b3cef7f6 1403 al->filtered |= (1 << HIST_FILTER__GUEST);
a1645ce1
ZY
1404 if ((cpumode == PERF_RECORD_MISC_USER ||
1405 cpumode == PERF_RECORD_MISC_KERNEL) &&
1406 !perf_host)
b3cef7f6 1407 al->filtered |= (1 << HIST_FILTER__HOST);
a1645ce1 1408
1ed091c4
ACM
1409 return;
1410 }
1411try_again:
9958e1f0 1412 al->map = map_groups__find(mg, type, al->addr);
1ed091c4
ACM
1413 if (al->map == NULL) {
1414 /*
1415 * If this is outside of all known maps, and is a negative
1416 * address, try to look it up in the kernel dso, as it might be
1417 * a vsyscall or vdso (which executes in user-mode).
1418 *
1419 * XXX This is nasty, we should have a symbol list in the
1420 * "[vdso]" dso, but for now lets use the old trick of looking
1421 * in the whole kernel symbol list.
1422 */
fbe2af45
AH
1423 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1424 mg != &machine->kmaps &&
1425 machine__kernel_ip(machine, al->addr)) {
23346f21 1426 mg = &machine->kmaps;
1f2a7069 1427 load_map = true;
1ed091c4
ACM
1428 goto try_again;
1429 }
5b7ba82a
AH
1430 } else {
1431 /*
1432 * Kernel maps might be changed when loading symbols so loading
1433 * must be done prior to using kernel maps.
1434 */
1435 if (load_map)
be39db9f 1436 map__load(al->map);
1ed091c4 1437 al->addr = al->map->map_ip(al->map, al->addr);
5b7ba82a 1438 }
59ee68ec
ACM
1439}
1440
bb871a9c 1441void thread__find_addr_location(struct thread *thread,
743eb868 1442 u8 cpumode, enum map_type type, u64 addr,
61710bde 1443 struct addr_location *al)
59ee68ec 1444{
bb871a9c 1445 thread__find_addr_map(thread, cpumode, type, addr, al);
59ee68ec 1446 if (al->map != NULL)
be39db9f 1447 al->sym = map__find_symbol(al->map, al->addr);
59ee68ec
ACM
1448 else
1449 al->sym = NULL;
1ed091c4
ACM
1450}
1451
b91fc39f
ACM
1452/*
1453 * Callers need to drop the reference to al->thread, obtained in
1454 * machine__findnew_thread()
1455 */
bb3eb566
ACM
1456int machine__resolve(struct machine *machine, struct addr_location *al,
1457 struct perf_sample *sample)
1ed091c4 1458{
ef89325f 1459 struct thread *thread = machine__findnew_thread(machine, sample->pid,
13ce34df 1460 sample->tid);
41a37e20 1461
1ed091c4
ACM
1462 if (thread == NULL)
1463 return -1;
1464
b9c5143a 1465 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1f626bc3 1466 /*
743eb868 1467 * Have we already created the kernel maps for this machine?
1f626bc3
ACM
1468 *
1469 * This should have happened earlier, when we processed the kernel MMAP
1470 * events, but for older perf.data files there was no such thing, so do
1471 * it now.
1472 */
473398a2 1473 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
a5e813c6 1474 machine__kernel_map(machine) == NULL)
743eb868 1475 machine__create_kernel_maps(machine);
1ed091c4 1476
473398a2 1477 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1ed091c4
ACM
1478 dump_printf(" ...... dso: %s\n",
1479 al->map ? al->map->dso->long_name :
1480 al->level == 'H' ? "[hypervisor]" : "<not found>");
466fa764
NK
1481
1482 if (thread__is_filtered(thread))
1483 al->filtered |= (1 << HIST_FILTER__THREAD);
1484
96415e4d 1485 al->sym = NULL;
8d50e5b4 1486 al->cpu = sample->cpu;
0c4c4deb
KL
1487 al->socket = -1;
1488
1489 if (al->cpu >= 0) {
1490 struct perf_env *env = machine->env;
1491
1492 if (env && env->cpu)
1493 al->socket = env->cpu[al->cpu].socket_id;
1494 }
96415e4d
ACM
1495
1496 if (al->map) {
cb8f4e9a
NK
1497 struct dso *dso = al->map->dso;
1498
96415e4d 1499 if (symbol_conf.dso_list &&
cb8f4e9a
NK
1500 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1501 dso->short_name) ||
1502 (dso->short_name != dso->long_name &&
1503 strlist__has_entry(symbol_conf.dso_list,
b3cef7f6
NK
1504 dso->long_name))))) {
1505 al->filtered |= (1 << HIST_FILTER__DSO);
b3cef7f6 1506 }
96415e4d 1507
be39db9f 1508 al->sym = map__find_symbol(al->map, al->addr);
96415e4d 1509 }
c410a338 1510
1500b93b
FT
1511 if (symbol_conf.sym_list &&
1512 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
b3cef7f6
NK
1513 al->sym->name))) {
1514 al->filtered |= (1 << HIST_FILTER__SYMBOL);
b3cef7f6 1515 }
c410a338 1516
c410a338 1517 return 0;
1ed091c4 1518}
9b0d2d87 1519
b91fc39f
ACM
1520/*
1521 * The preprocess_sample method will return with reference counts for the
1522 * in it, when done using (and perhaps getting ref counts if needing to
1523 * keep a pointer to one of those entries) it must be paired with
1524 * addr_location__put(), so that the refcounts can be decremented.
1525 */
1526void addr_location__put(struct addr_location *al)
1527{
1528 thread__zput(al->thread);
1529}
1530
9b0d2d87
AH
1531bool is_bts_event(struct perf_event_attr *attr)
1532{
1533 return attr->type == PERF_TYPE_HARDWARE &&
1534 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1535 attr->sample_period == 1;
1536}
1537
1538bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1539{
1540 if (attr->type == PERF_TYPE_SOFTWARE &&
1541 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1542 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1543 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1544 return true;
1545
1546 if (is_bts_event(attr))
1547 return true;
1548
1549 return false;
1550}
1551
c2740a87
ACM
1552void thread__resolve(struct thread *thread, struct addr_location *al,
1553 struct perf_sample *sample)
9b0d2d87 1554{
473398a2 1555 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
9b0d2d87 1556 if (!al->map)
473398a2 1557 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
9b0d2d87
AH
1558 sample->addr, al);
1559
1560 al->cpu = sample->cpu;
1561 al->sym = NULL;
1562
1563 if (al->map)
be39db9f 1564 al->sym = map__find_symbol(al->map, al->addr);
9b0d2d87 1565}