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