1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.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 "event.h"
14 #include "debug.h"
15 #include "hist.h"
16 #include "machine.h"
17 #include "sort.h"
18 #include "string2.h"
19 #include "strlist.h"
20 #include "thread.h"
21 #include "thread_map.h"
22 #include "sane_ctype.h"
23 #include "symbol/kallsyms.h"
24 #include "asm/bug.h"
25 #include "stat.h"
26
27 static const char *perf_event__names[] = {
28 [0] = "TOTAL",
29 [PERF_RECORD_MMAP] = "MMAP",
30 [PERF_RECORD_MMAP2] = "MMAP2",
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",
39 [PERF_RECORD_AUX] = "AUX",
40 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
41 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
42 [PERF_RECORD_SWITCH] = "SWITCH",
43 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
44 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
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",
50 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
51 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
52 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
53 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
54 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
55 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
56 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
57 [PERF_RECORD_STAT] = "STAT",
58 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
59 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
60 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
61 [PERF_RECORD_HEADER_FEATURE] = "FEATURE",
62 };
63
64 static 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
perf_event__name(unsigned int id)74 const char *perf_event__name(unsigned int id)
75 {
76 if (id >= ARRAY_SIZE(perf_event__names))
77 return "INVALID";
78 if (!perf_event__names[id])
79 return "UNKNOWN";
80 return perf_event__names[id];
81 }
82
perf_ns__name(unsigned int id)83 static 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
perf_tool__process_synth_event(struct perf_tool * tool,union perf_event * event,struct machine * machine,perf_event__handler_t process)90 static 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 = {
96 .pid = -1,
97 .tid = -1,
98 .time = -1,
99 .stream_id = -1,
100 .cpu = -1,
101 .period = 1,
102 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
103 };
104
105 return process(tool, event, &synth_sample, machine);
106 };
107
108 /*
109 * Assumes that the first 4095 bytes of /proc/pid/stat contains
110 * the comm, tgid and ppid.
111 */
perf_event__get_comm_ids(pid_t pid,char * comm,size_t len,pid_t * tgid,pid_t * ppid)112 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
113 pid_t *tgid, pid_t *ppid)
114 {
115 char filename[PATH_MAX];
116 char bf[4096];
117 int fd;
118 size_t size = 0;
119 ssize_t n;
120 char *name, *tgids, *ppids;
121
122 *tgid = -1;
123 *ppid = -1;
124
125 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
126
127 fd = open(filename, O_RDONLY);
128 if (fd < 0) {
129 pr_debug("couldn't open %s\n", filename);
130 return -1;
131 }
132
133 n = read(fd, bf, sizeof(bf) - 1);
134 close(fd);
135 if (n <= 0) {
136 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
137 pid);
138 return -1;
139 }
140 bf[n] = '\0';
141
142 name = strstr(bf, "Name:");
143 tgids = strstr(bf, "Tgid:");
144 ppids = strstr(bf, "PPid:");
145
146 if (name) {
147 char *nl;
148
149 name += 5; /* strlen("Name:") */
150 name = ltrim(name);
151
152 nl = strchr(name, '\n');
153 if (nl)
154 *nl = '\0';
155
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:") */
167 *tgid = atoi(tgids);
168 } else {
169 pr_debug("Tgid: string not found for pid %d\n", pid);
170 }
171
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;
180 }
181
perf_event__prepare_comm(union perf_event * event,pid_t pid,struct machine * machine,pid_t * tgid,pid_t * ppid)182 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
183 struct machine *machine,
184 pid_t *tgid, pid_t *ppid)
185 {
186 size_t size;
187
188 *ppid = -1;
189
190 memset(&event->comm, 0, sizeof(event->comm));
191
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 }
201
202 if (*tgid < 0)
203 return -1;
204
205 event->comm.pid = *tgid;
206 event->comm.header.type = PERF_RECORD_COMM;
207
208 size = strlen(event->comm.comm) + 1;
209 size = PERF_ALIGN(size, sizeof(u64));
210 memset(event->comm.comm + size, 0, machine->id_hdr_size);
211 event->comm.header.size = (sizeof(event->comm) -
212 (sizeof(event->comm.comm) - size) +
213 machine->id_hdr_size);
214 event->comm.tid = pid;
215
216 return 0;
217 }
218
perf_event__synthesize_comm(struct perf_tool * tool,union perf_event * event,pid_t pid,perf_event__handler_t process,struct machine * machine)219 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
220 union perf_event *event, pid_t pid,
221 perf_event__handler_t process,
222 struct machine *machine)
223 {
224 pid_t tgid, ppid;
225
226 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
227 return -1;
228
229 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
230 return -1;
231
232 return tgid;
233 }
234
perf_event__get_ns_link_info(pid_t pid,const char * ns,struct perf_ns_link_info * ns_link_info)235 static 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
perf_event__synthesize_namespaces(struct perf_tool * tool,union perf_event * event,pid_t pid,pid_t tgid,perf_event__handler_t process,struct machine * machine)248 int 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
perf_event__synthesize_fork(struct perf_tool * tool,union perf_event * event,pid_t pid,pid_t tgid,pid_t ppid,perf_event__handler_t process,struct machine * machine)287 static int perf_event__synthesize_fork(struct perf_tool *tool,
288 union perf_event *event,
289 pid_t pid, pid_t tgid, pid_t ppid,
290 perf_event__handler_t process,
291 struct machine *machine)
292 {
293 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
294
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 }
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
313 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
314 return -1;
315
316 return 0;
317 }
318
perf_event__synthesize_mmap_events(struct perf_tool * tool,union perf_event * event,pid_t pid,pid_t tgid,perf_event__handler_t process,struct machine * machine,bool mmap_data,unsigned int proc_map_timeout)319 int 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,
324 bool mmap_data,
325 unsigned int proc_map_timeout)
326 {
327 char filename[PATH_MAX];
328 FILE *fp;
329 unsigned long long t;
330 bool truncation = false;
331 unsigned long long timeout = proc_map_timeout * 1000000ULL;
332 int rc = 0;
333 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
334 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
335
336 if (machine__is_default_guest(machine))
337 return 0;
338
339 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
340 machine->root_dir, pid, pid);
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
351 event->header.type = PERF_RECORD_MMAP2;
352 t = rdclock();
353
354 while (1) {
355 char bf[BUFSIZ];
356 char prot[5];
357 char execname[PATH_MAX];
358 char anonstr[] = "//anon";
359 unsigned int ino;
360 size_t size;
361 ssize_t n;
362
363 if (fgets(bf, sizeof(bf), fp) == NULL)
364 break;
365
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);
371 truncation = true;
372 goto out;
373 }
374
375 /* ensure null termination since stack will be reused. */
376 strcpy(execname, "");
377
378 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
379 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
380 &event->mmap2.start, &event->mmap2.len, prot,
381 &event->mmap2.pgoff, &event->mmap2.maj,
382 &event->mmap2.min,
383 &ino, execname);
384
385 /*
386 * Anon maps don't have the execname.
387 */
388 if (n < 7)
389 continue;
390
391 event->mmap2.ino = (u64)ino;
392
393 /*
394 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
395 */
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;
400
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
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 }
422
423 out:
424 if (truncation)
425 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
426
427 if (!strcmp(execname, ""))
428 strcpy(execname, anonstr);
429
430 if (hugetlbfs_mnt_len &&
431 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
432 strcpy(execname, anonstr);
433 event->mmap2.flags |= MAP_HUGETLB;
434 }
435
436 size = strlen(execname) + 1;
437 memcpy(event->mmap2.filename, execname, size);
438 size = PERF_ALIGN(size, sizeof(u64));
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;
446
447 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
448 rc = -1;
449 break;
450 }
451
452 if (truncation)
453 break;
454 }
455
456 fclose(fp);
457 return rc;
458 }
459
perf_event__synthesize_modules(struct perf_tool * tool,perf_event__handler_t process,struct machine * machine)460 int perf_event__synthesize_modules(struct perf_tool *tool,
461 perf_event__handler_t process,
462 struct machine *machine)
463 {
464 int rc = 0;
465 struct map *pos;
466 struct map_groups *kmaps = &machine->kmaps;
467 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
468 union perf_event *event = zalloc((sizeof(event->mmap) +
469 machine->id_hdr_size));
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;
477
478 /*
479 * kernel uses 0 for user space maps, see kernel/perf_event.c
480 * __perf_event_mmap
481 */
482 if (machine__is_host(machine))
483 event->header.misc = PERF_RECORD_MISC_KERNEL;
484 else
485 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
486
487 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
488 size_t size;
489
490 if (__map__is_kernel(pos))
491 continue;
492
493 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
494 event->mmap.header.type = PERF_RECORD_MMAP;
495 event->mmap.header.size = (sizeof(event->mmap) -
496 (sizeof(event->mmap.filename) - size));
497 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
498 event->mmap.header.size += machine->id_hdr_size;
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,
504 pos->dso->long_name_len + 1);
505 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
506 rc = -1;
507 break;
508 }
509 }
510
511 free(event);
512 return rc;
513 }
514
__event__synthesize_thread(union perf_event * comm_event,union perf_event * mmap_event,union perf_event * fork_event,union perf_event * namespaces_event,pid_t pid,int full,perf_event__handler_t process,struct perf_tool * tool,struct machine * machine,bool mmap_data,unsigned int proc_map_timeout)515 static int __event__synthesize_thread(union perf_event *comm_event,
516 union perf_event *mmap_event,
517 union perf_event *fork_event,
518 union perf_event *namespaces_event,
519 pid_t pid, int full,
520 perf_event__handler_t process,
521 struct perf_tool *tool,
522 struct machine *machine,
523 bool mmap_data,
524 unsigned int proc_map_timeout)
525 {
526 char filename[PATH_MAX];
527 DIR *tasks;
528 struct dirent *dirent;
529 pid_t tgid, ppid;
530 int rc = 0;
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
540 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
541 tgid, process, machine) < 0)
542 return -1;
543
544
545 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
546 process, machine, mmap_data,
547 proc_map_timeout);
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
562 while ((dirent = readdir(tasks)) != NULL) {
563 char *end;
564 pid_t _pid;
565
566 _pid = strtol(dirent->d_name, &end, 10);
567 if (*end)
568 continue;
569
570 rc = -1;
571 if (perf_event__prepare_comm(comm_event, _pid, machine,
572 &tgid, &ppid) != 0)
573 break;
574
575 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
576 ppid, process, machine) < 0)
577 break;
578
579 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
580 tgid, process, machine) < 0)
581 break;
582
583 /*
584 * Send the prepared comm event
585 */
586 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
587 break;
588
589 rc = 0;
590 if (_pid == pid) {
591 /* process the parent's maps too */
592 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
593 process, machine, mmap_data, proc_map_timeout);
594 if (rc)
595 break;
596 }
597 }
598
599 closedir(tasks);
600 return rc;
601 }
602
perf_event__synthesize_thread_map(struct perf_tool * tool,struct thread_map * threads,perf_event__handler_t process,struct machine * machine,bool mmap_data,unsigned int proc_map_timeout)603 int perf_event__synthesize_thread_map(struct perf_tool *tool,
604 struct thread_map *threads,
605 perf_event__handler_t process,
606 struct machine *machine,
607 bool mmap_data,
608 unsigned int proc_map_timeout)
609 {
610 union perf_event *comm_event, *mmap_event, *fork_event;
611 union perf_event *namespaces_event;
612 int err = -1, thread, j;
613
614 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
615 if (comm_event == NULL)
616 goto out;
617
618 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
619 if (mmap_event == NULL)
620 goto out_free_comm;
621
622 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
623 if (fork_event == NULL)
624 goto out_free_mmap;
625
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
632 err = 0;
633 for (thread = 0; thread < threads->nr; ++thread) {
634 if (__event__synthesize_thread(comm_event, mmap_event,
635 fork_event, namespaces_event,
636 thread_map__pid(threads, thread), 0,
637 process, tool, machine,
638 mmap_data, proc_map_timeout)) {
639 err = -1;
640 break;
641 }
642
643 /*
644 * comm.pid is set to thread group id by
645 * perf_event__synthesize_comm
646 */
647 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
648 bool need_leader = true;
649
650 /* is thread group leader in thread_map? */
651 for (j = 0; j < threads->nr; ++j) {
652 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
653 need_leader = false;
654 break;
655 }
656 }
657
658 /* if not, generate events for it */
659 if (need_leader &&
660 __event__synthesize_thread(comm_event, mmap_event,
661 fork_event, namespaces_event,
662 comm_event->comm.pid, 0,
663 process, tool, machine,
664 mmap_data, proc_map_timeout)) {
665 err = -1;
666 break;
667 }
668 }
669 }
670 free(namespaces_event);
671 out_free_fork:
672 free(fork_event);
673 out_free_mmap:
674 free(mmap_event);
675 out_free_comm:
676 free(comm_event);
677 out:
678 return err;
679 }
680
perf_event__synthesize_threads(struct perf_tool * tool,perf_event__handler_t process,struct machine * machine,bool mmap_data,unsigned int proc_map_timeout)681 int perf_event__synthesize_threads(struct perf_tool *tool,
682 perf_event__handler_t process,
683 struct machine *machine,
684 bool mmap_data,
685 unsigned int proc_map_timeout)
686 {
687 DIR *proc;
688 char proc_path[PATH_MAX];
689 struct dirent *dirent;
690 union perf_event *comm_event, *mmap_event, *fork_event;
691 union perf_event *namespaces_event;
692 int err = -1;
693
694 if (machine__is_default_guest(machine))
695 return 0;
696
697 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
698 if (comm_event == NULL)
699 goto out;
700
701 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
702 if (mmap_event == NULL)
703 goto out_free_comm;
704
705 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
706 if (fork_event == NULL)
707 goto out_free_mmap;
708
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
715 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
716 proc = opendir(proc_path);
717
718 if (proc == NULL)
719 goto out_free_namespaces;
720
721 while ((dirent = readdir(proc)) != NULL) {
722 char *end;
723 pid_t pid = strtol(dirent->d_name, &end, 10);
724
725 if (*end) /* only interested in proper numerical dirents */
726 continue;
727 /*
728 * We may race with exiting thread, so don't stop just because
729 * one thread couldn't be synthesized.
730 */
731 __event__synthesize_thread(comm_event, mmap_event, fork_event,
732 namespaces_event, pid, 1, process,
733 tool, machine, mmap_data,
734 proc_map_timeout);
735 }
736
737 err = 0;
738 closedir(proc);
739 out_free_namespaces:
740 free(namespaces_event);
741 out_free_fork:
742 free(fork_event);
743 out_free_mmap:
744 free(mmap_event);
745 out_free_comm:
746 free(comm_event);
747 out:
748 return err;
749 }
750
751 struct process_symbol_args {
752 const char *name;
753 u64 start;
754 };
755
find_symbol_cb(void * arg,const char * name,char type,u64 start)756 static int find_symbol_cb(void *arg, const char *name, char type,
757 u64 start)
758 {
759 struct process_symbol_args *args = arg;
760
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))
767 return 0;
768
769 args->start = start;
770 return 1;
771 }
772
kallsyms__get_function_start(const char * kallsyms_filename,const char * symbol_name,u64 * addr)773 int kallsyms__get_function_start(const char *kallsyms_filename,
774 const char *symbol_name, u64 *addr)
775 {
776 struct process_symbol_args args = { .name = symbol_name, };
777
778 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
779 return -1;
780
781 *addr = args.start;
782 return 0;
783 }
784
perf_event__synthesize_kernel_mmap(struct perf_tool * tool,perf_event__handler_t process,struct machine * machine)785 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
786 perf_event__handler_t process,
787 struct machine *machine)
788 {
789 size_t size;
790 const char *mmap_name;
791 char name_buff[PATH_MAX];
792 struct map *map = machine__kernel_map(machine);
793 struct kmap *kmap;
794 int err;
795 union perf_event *event;
796
797 if (symbol_conf.kptr_restrict)
798 return -1;
799 if (map == NULL)
800 return -1;
801
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 */
807 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
808 if (event == NULL) {
809 pr_debug("Not enough memory synthesizing mmap event "
810 "for kernel modules\n");
811 return -1;
812 }
813
814 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
815 if (machine__is_host(machine)) {
816 /*
817 * kernel uses PERF_RECORD_MISC_USER for user space maps,
818 * see kernel/perf_event.c __perf_event_mmap
819 */
820 event->header.misc = PERF_RECORD_MISC_KERNEL;
821 } else {
822 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
823 }
824
825 kmap = map__kmap(map);
826 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
827 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
828 size = PERF_ALIGN(size, sizeof(u64));
829 event->mmap.header.type = PERF_RECORD_MMAP;
830 event->mmap.header.size = (sizeof(event->mmap) -
831 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
832 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
833 event->mmap.start = map->start;
834 event->mmap.len = map->end - event->mmap.start;
835 event->mmap.pid = machine->pid;
836
837 err = perf_tool__process_synth_event(tool, event, machine, process);
838 free(event);
839
840 return err;
841 }
842
perf_event__synthesize_thread_map2(struct perf_tool * tool,struct thread_map * threads,perf_event__handler_t process,struct machine * machine)843 int 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
synthesize_cpus(struct cpu_map_entries * cpus,struct cpu_map * map)879 static 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
synthesize_mask(struct cpu_map_mask * mask,struct cpu_map * map,int max)890 static 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
cpus_size(struct cpu_map * map)902 static size_t cpus_size(struct cpu_map *map)
903 {
904 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
905 }
906
mask_size(struct cpu_map * map,int * max)907 static 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
cpu_map_data__alloc(struct cpu_map * map,size_t * size,u16 * type,int * max)924 void *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 *size = PERF_ALIGN(*size, sizeof(u64));
955 return zalloc(*size);
956 }
957
cpu_map_data__synthesize(struct cpu_map_data * data,struct cpu_map * map,u16 type,int max)958 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
959 u16 type, int max)
960 {
961 data->type = type;
962
963 switch (type) {
964 case PERF_CPU_MAP__CPUS:
965 synthesize_cpus((struct cpu_map_entries *) data->data, map);
966 break;
967 case PERF_CPU_MAP__MASK:
968 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
969 default:
970 break;
971 };
972 }
973
cpu_map_event__new(struct cpu_map * map)974 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
975 {
976 size_t size = sizeof(struct cpu_map_event);
977 struct cpu_map_event *event;
978 int max;
979 u16 type;
980
981 event = cpu_map_data__alloc(map, &size, &type, &max);
982 if (!event)
983 return NULL;
984
985 event->header.type = PERF_RECORD_CPU_MAP;
986 event->header.size = size;
987 event->data.type = type;
988
989 cpu_map_data__synthesize(&event->data, map, type, max);
990 return event;
991 }
992
perf_event__synthesize_cpu_map(struct perf_tool * tool,struct cpu_map * map,perf_event__handler_t process,struct machine * machine)993 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
994 struct cpu_map *map,
995 perf_event__handler_t process,
996 struct machine *machine)
997 {
998 struct cpu_map_event *event;
999 int err;
1000
1001 event = cpu_map_event__new(map);
1002 if (!event)
1003 return -ENOMEM;
1004
1005 err = process(tool, (union perf_event *) event, NULL, machine);
1006
1007 free(event);
1008 return err;
1009 }
1010
perf_event__synthesize_stat_config(struct perf_tool * tool,struct perf_stat_config * config,perf_event__handler_t process,struct machine * machine)1011 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1012 struct perf_stat_config *config,
1013 perf_event__handler_t process,
1014 struct machine *machine)
1015 {
1016 struct stat_config_event *event;
1017 int size, i = 0, err;
1018
1019 size = sizeof(*event);
1020 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1021
1022 event = zalloc(size);
1023 if (!event)
1024 return -ENOMEM;
1025
1026 event->header.type = PERF_RECORD_STAT_CONFIG;
1027 event->header.size = size;
1028 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1029
1030 #define ADD(__term, __val) \
1031 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1032 event->data[i].val = __val; \
1033 i++;
1034
1035 ADD(AGGR_MODE, config->aggr_mode)
1036 ADD(INTERVAL, config->interval)
1037 ADD(SCALE, config->scale)
1038
1039 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1040 "stat config terms unbalanced\n");
1041 #undef ADD
1042
1043 err = process(tool, (union perf_event *) event, NULL, machine);
1044
1045 free(event);
1046 return err;
1047 }
1048
perf_event__synthesize_stat(struct perf_tool * tool,u32 cpu,u32 thread,u64 id,struct perf_counts_values * count,perf_event__handler_t process,struct machine * machine)1049 int perf_event__synthesize_stat(struct perf_tool *tool,
1050 u32 cpu, u32 thread, u64 id,
1051 struct perf_counts_values *count,
1052 perf_event__handler_t process,
1053 struct machine *machine)
1054 {
1055 struct stat_event event;
1056
1057 event.header.type = PERF_RECORD_STAT;
1058 event.header.size = sizeof(event);
1059 event.header.misc = 0;
1060
1061 event.id = id;
1062 event.cpu = cpu;
1063 event.thread = thread;
1064 event.val = count->val;
1065 event.ena = count->ena;
1066 event.run = count->run;
1067
1068 return process(tool, (union perf_event *) &event, NULL, machine);
1069 }
1070
perf_event__synthesize_stat_round(struct perf_tool * tool,u64 evtime,u64 type,perf_event__handler_t process,struct machine * machine)1071 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1072 u64 evtime, u64 type,
1073 perf_event__handler_t process,
1074 struct machine *machine)
1075 {
1076 struct stat_round_event event;
1077
1078 event.header.type = PERF_RECORD_STAT_ROUND;
1079 event.header.size = sizeof(event);
1080 event.header.misc = 0;
1081
1082 event.time = evtime;
1083 event.type = type;
1084
1085 return process(tool, (union perf_event *) &event, NULL, machine);
1086 }
1087
perf_event__read_stat_config(struct perf_stat_config * config,struct stat_config_event * event)1088 void perf_event__read_stat_config(struct perf_stat_config *config,
1089 struct stat_config_event *event)
1090 {
1091 unsigned i;
1092
1093 for (i = 0; i < event->nr; i++) {
1094
1095 switch (event->data[i].tag) {
1096 #define CASE(__term, __val) \
1097 case PERF_STAT_CONFIG_TERM__##__term: \
1098 config->__val = event->data[i].val; \
1099 break;
1100
1101 CASE(AGGR_MODE, aggr_mode)
1102 CASE(SCALE, scale)
1103 CASE(INTERVAL, interval)
1104 #undef CASE
1105 default:
1106 pr_warning("unknown stat config term %" PRIu64 "\n",
1107 event->data[i].tag);
1108 }
1109 }
1110 }
1111
perf_event__fprintf_comm(union perf_event * event,FILE * fp)1112 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1113 {
1114 const char *s;
1115
1116 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1117 s = " exec";
1118 else
1119 s = "";
1120
1121 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1122 }
1123
perf_event__fprintf_namespaces(union perf_event * event,FILE * fp)1124 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1125 {
1126 size_t ret = 0;
1127 struct perf_ns_link_info *ns_link_info;
1128 u32 nr_namespaces, idx;
1129
1130 ns_link_info = event->namespaces.link_info;
1131 nr_namespaces = event->namespaces.nr_namespaces;
1132
1133 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1134 event->namespaces.pid,
1135 event->namespaces.tid,
1136 nr_namespaces);
1137
1138 for (idx = 0; idx < nr_namespaces; idx++) {
1139 if (idx && (idx % 4 == 0))
1140 ret += fprintf(fp, "\n\t\t ");
1141
1142 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1143 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1144 (u64)ns_link_info[idx].ino,
1145 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1146 }
1147
1148 return ret;
1149 }
1150
perf_event__process_comm(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1151 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1152 union perf_event *event,
1153 struct perf_sample *sample,
1154 struct machine *machine)
1155 {
1156 return machine__process_comm_event(machine, event, sample);
1157 }
1158
perf_event__process_namespaces(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1159 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1160 union perf_event *event,
1161 struct perf_sample *sample,
1162 struct machine *machine)
1163 {
1164 return machine__process_namespaces_event(machine, event, sample);
1165 }
1166
perf_event__process_lost(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1167 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1168 union perf_event *event,
1169 struct perf_sample *sample,
1170 struct machine *machine)
1171 {
1172 return machine__process_lost_event(machine, event, sample);
1173 }
1174
perf_event__process_aux(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)1175 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1176 union perf_event *event,
1177 struct perf_sample *sample __maybe_unused,
1178 struct machine *machine)
1179 {
1180 return machine__process_aux_event(machine, event);
1181 }
1182
perf_event__process_itrace_start(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)1183 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1184 union perf_event *event,
1185 struct perf_sample *sample __maybe_unused,
1186 struct machine *machine)
1187 {
1188 return machine__process_itrace_start_event(machine, event);
1189 }
1190
perf_event__process_lost_samples(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1191 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1192 union perf_event *event,
1193 struct perf_sample *sample,
1194 struct machine *machine)
1195 {
1196 return machine__process_lost_samples_event(machine, event, sample);
1197 }
1198
perf_event__process_switch(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)1199 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1200 union perf_event *event,
1201 struct perf_sample *sample __maybe_unused,
1202 struct machine *machine)
1203 {
1204 return machine__process_switch_event(machine, event);
1205 }
1206
perf_event__fprintf_mmap(union perf_event * event,FILE * fp)1207 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1208 {
1209 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1210 event->mmap.pid, event->mmap.tid, event->mmap.start,
1211 event->mmap.len, event->mmap.pgoff,
1212 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1213 event->mmap.filename);
1214 }
1215
perf_event__fprintf_mmap2(union perf_event * event,FILE * fp)1216 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1217 {
1218 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1219 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1220 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1221 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1222 event->mmap2.min, event->mmap2.ino,
1223 event->mmap2.ino_generation,
1224 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1225 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1226 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1227 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1228 event->mmap2.filename);
1229 }
1230
perf_event__fprintf_thread_map(union perf_event * event,FILE * fp)1231 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1232 {
1233 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1234 size_t ret;
1235
1236 ret = fprintf(fp, " nr: ");
1237
1238 if (threads)
1239 ret += thread_map__fprintf(threads, fp);
1240 else
1241 ret += fprintf(fp, "failed to get threads from event\n");
1242
1243 thread_map__put(threads);
1244 return ret;
1245 }
1246
perf_event__fprintf_cpu_map(union perf_event * event,FILE * fp)1247 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1248 {
1249 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1250 size_t ret;
1251
1252 ret = fprintf(fp, ": ");
1253
1254 if (cpus)
1255 ret += cpu_map__fprintf(cpus, fp);
1256 else
1257 ret += fprintf(fp, "failed to get cpumap from event\n");
1258
1259 cpu_map__put(cpus);
1260 return ret;
1261 }
1262
perf_event__process_mmap(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1263 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1264 union perf_event *event,
1265 struct perf_sample *sample,
1266 struct machine *machine)
1267 {
1268 return machine__process_mmap_event(machine, event, sample);
1269 }
1270
perf_event__process_mmap2(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1271 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1272 union perf_event *event,
1273 struct perf_sample *sample,
1274 struct machine *machine)
1275 {
1276 return machine__process_mmap2_event(machine, event, sample);
1277 }
1278
perf_event__fprintf_task(union perf_event * event,FILE * fp)1279 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1280 {
1281 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1282 event->fork.pid, event->fork.tid,
1283 event->fork.ppid, event->fork.ptid);
1284 }
1285
perf_event__process_fork(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1286 int perf_event__process_fork(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_fork_event(machine, event, sample);
1292 }
1293
perf_event__process_exit(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1294 int perf_event__process_exit(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_exit_event(machine, event, sample);
1300 }
1301
perf_event__fprintf_aux(union perf_event * event,FILE * fp)1302 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1303 {
1304 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n",
1305 event->aux.aux_offset, event->aux.aux_size,
1306 event->aux.flags,
1307 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1308 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1309 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
1310 }
1311
perf_event__fprintf_itrace_start(union perf_event * event,FILE * fp)1312 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1313 {
1314 return fprintf(fp, " pid: %u tid: %u\n",
1315 event->itrace_start.pid, event->itrace_start.tid);
1316 }
1317
perf_event__fprintf_switch(union perf_event * event,FILE * fp)1318 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1319 {
1320 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1321 const char *in_out = out ? "OUT" : "IN ";
1322
1323 if (event->header.type == PERF_RECORD_SWITCH)
1324 return fprintf(fp, " %s\n", in_out);
1325
1326 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1327 in_out, out ? "next" : "prev",
1328 event->context_switch.next_prev_pid,
1329 event->context_switch.next_prev_tid);
1330 }
1331
perf_event__fprintf(union perf_event * event,FILE * fp)1332 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1333 {
1334 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1335 perf_event__name(event->header.type));
1336
1337 switch (event->header.type) {
1338 case PERF_RECORD_COMM:
1339 ret += perf_event__fprintf_comm(event, fp);
1340 break;
1341 case PERF_RECORD_FORK:
1342 case PERF_RECORD_EXIT:
1343 ret += perf_event__fprintf_task(event, fp);
1344 break;
1345 case PERF_RECORD_MMAP:
1346 ret += perf_event__fprintf_mmap(event, fp);
1347 break;
1348 case PERF_RECORD_NAMESPACES:
1349 ret += perf_event__fprintf_namespaces(event, fp);
1350 break;
1351 case PERF_RECORD_MMAP2:
1352 ret += perf_event__fprintf_mmap2(event, fp);
1353 break;
1354 case PERF_RECORD_AUX:
1355 ret += perf_event__fprintf_aux(event, fp);
1356 break;
1357 case PERF_RECORD_ITRACE_START:
1358 ret += perf_event__fprintf_itrace_start(event, fp);
1359 break;
1360 case PERF_RECORD_SWITCH:
1361 case PERF_RECORD_SWITCH_CPU_WIDE:
1362 ret += perf_event__fprintf_switch(event, fp);
1363 break;
1364 default:
1365 ret += fprintf(fp, "\n");
1366 }
1367
1368 return ret;
1369 }
1370
perf_event__process(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample,struct machine * machine)1371 int perf_event__process(struct perf_tool *tool __maybe_unused,
1372 union perf_event *event,
1373 struct perf_sample *sample,
1374 struct machine *machine)
1375 {
1376 return machine__process_event(machine, event, sample);
1377 }
1378
thread__find_addr_map(struct thread * thread,u8 cpumode,enum map_type type,u64 addr,struct addr_location * al)1379 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1380 enum map_type type, u64 addr,
1381 struct addr_location *al)
1382 {
1383 struct map_groups *mg = thread->mg;
1384 struct machine *machine = mg->machine;
1385 bool load_map = false;
1386
1387 al->machine = machine;
1388 al->thread = thread;
1389 al->addr = addr;
1390 al->cpumode = cpumode;
1391 al->filtered = 0;
1392
1393 if (machine == NULL) {
1394 al->map = NULL;
1395 return;
1396 }
1397
1398 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1399 al->level = 'k';
1400 mg = &machine->kmaps;
1401 load_map = true;
1402 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1403 al->level = '.';
1404 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1405 al->level = 'g';
1406 mg = &machine->kmaps;
1407 load_map = true;
1408 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1409 al->level = 'u';
1410 } else {
1411 al->level = 'H';
1412 al->map = NULL;
1413
1414 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1415 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1416 !perf_guest)
1417 al->filtered |= (1 << HIST_FILTER__GUEST);
1418 if ((cpumode == PERF_RECORD_MISC_USER ||
1419 cpumode == PERF_RECORD_MISC_KERNEL) &&
1420 !perf_host)
1421 al->filtered |= (1 << HIST_FILTER__HOST);
1422
1423 return;
1424 }
1425 try_again:
1426 al->map = map_groups__find(mg, type, al->addr);
1427 if (al->map == NULL) {
1428 /*
1429 * If this is outside of all known maps, and is a negative
1430 * address, try to look it up in the kernel dso, as it might be
1431 * a vsyscall or vdso (which executes in user-mode).
1432 *
1433 * XXX This is nasty, we should have a symbol list in the
1434 * "[vdso]" dso, but for now lets use the old trick of looking
1435 * in the whole kernel symbol list.
1436 */
1437 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1438 mg != &machine->kmaps &&
1439 machine__kernel_ip(machine, al->addr)) {
1440 mg = &machine->kmaps;
1441 load_map = true;
1442 goto try_again;
1443 }
1444 } else {
1445 /*
1446 * Kernel maps might be changed when loading symbols so loading
1447 * must be done prior to using kernel maps.
1448 */
1449 if (load_map)
1450 map__load(al->map);
1451 al->addr = al->map->map_ip(al->map, al->addr);
1452 }
1453 }
1454
thread__find_addr_location(struct thread * thread,u8 cpumode,enum map_type type,u64 addr,struct addr_location * al)1455 void thread__find_addr_location(struct thread *thread,
1456 u8 cpumode, enum map_type type, u64 addr,
1457 struct addr_location *al)
1458 {
1459 thread__find_addr_map(thread, cpumode, type, addr, al);
1460 if (al->map != NULL)
1461 al->sym = map__find_symbol(al->map, al->addr);
1462 else
1463 al->sym = NULL;
1464 }
1465
1466 /*
1467 * Callers need to drop the reference to al->thread, obtained in
1468 * machine__findnew_thread()
1469 */
machine__resolve(struct machine * machine,struct addr_location * al,struct perf_sample * sample)1470 int machine__resolve(struct machine *machine, struct addr_location *al,
1471 struct perf_sample *sample)
1472 {
1473 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1474 sample->tid);
1475
1476 if (thread == NULL)
1477 return -1;
1478
1479 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1480 /*
1481 * Have we already created the kernel maps for this machine?
1482 *
1483 * This should have happened earlier, when we processed the kernel MMAP
1484 * events, but for older perf.data files there was no such thing, so do
1485 * it now.
1486 */
1487 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1488 machine__kernel_map(machine) == NULL)
1489 machine__create_kernel_maps(machine);
1490
1491 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1492 dump_printf(" ...... dso: %s\n",
1493 al->map ? al->map->dso->long_name :
1494 al->level == 'H' ? "[hypervisor]" : "<not found>");
1495
1496 if (thread__is_filtered(thread))
1497 al->filtered |= (1 << HIST_FILTER__THREAD);
1498
1499 al->sym = NULL;
1500 al->cpu = sample->cpu;
1501 al->socket = -1;
1502
1503 if (al->cpu >= 0) {
1504 struct perf_env *env = machine->env;
1505
1506 if (env && env->cpu)
1507 al->socket = env->cpu[al->cpu].socket_id;
1508 }
1509
1510 if (al->map) {
1511 struct dso *dso = al->map->dso;
1512
1513 if (symbol_conf.dso_list &&
1514 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1515 dso->short_name) ||
1516 (dso->short_name != dso->long_name &&
1517 strlist__has_entry(symbol_conf.dso_list,
1518 dso->long_name))))) {
1519 al->filtered |= (1 << HIST_FILTER__DSO);
1520 }
1521
1522 al->sym = map__find_symbol(al->map, al->addr);
1523 }
1524
1525 if (symbol_conf.sym_list &&
1526 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1527 al->sym->name))) {
1528 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1529 }
1530
1531 return 0;
1532 }
1533
1534 /*
1535 * The preprocess_sample method will return with reference counts for the
1536 * in it, when done using (and perhaps getting ref counts if needing to
1537 * keep a pointer to one of those entries) it must be paired with
1538 * addr_location__put(), so that the refcounts can be decremented.
1539 */
addr_location__put(struct addr_location * al)1540 void addr_location__put(struct addr_location *al)
1541 {
1542 thread__zput(al->thread);
1543 }
1544
is_bts_event(struct perf_event_attr * attr)1545 bool is_bts_event(struct perf_event_attr *attr)
1546 {
1547 return attr->type == PERF_TYPE_HARDWARE &&
1548 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1549 attr->sample_period == 1;
1550 }
1551
sample_addr_correlates_sym(struct perf_event_attr * attr)1552 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1553 {
1554 if (attr->type == PERF_TYPE_SOFTWARE &&
1555 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1556 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1557 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1558 return true;
1559
1560 if (is_bts_event(attr))
1561 return true;
1562
1563 return false;
1564 }
1565
thread__resolve(struct thread * thread,struct addr_location * al,struct perf_sample * sample)1566 void thread__resolve(struct thread *thread, struct addr_location *al,
1567 struct perf_sample *sample)
1568 {
1569 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1570 if (!al->map)
1571 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1572 sample->addr, al);
1573
1574 al->cpu = sample->cpu;
1575 al->sym = NULL;
1576
1577 if (al->map)
1578 al->sym = map__find_symbol(al->map, al->addr);
1579 }
1580