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