4 #include <linux/kernel.h>
5 #include <linux/types.h>
9 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
10 #include <api/fs/fs.h>
19 #include "thread_map.h"
20 #include "sane_ctype.h"
21 #include "symbol/kallsyms.h"
25 static const char *perf_event__names[] = {
27 [PERF_RECORD_MMAP] = "MMAP",
28 [PERF_RECORD_MMAP2] = "MMAP2",
29 [PERF_RECORD_LOST] = "LOST",
30 [PERF_RECORD_COMM] = "COMM",
31 [PERF_RECORD_EXIT] = "EXIT",
32 [PERF_RECORD_THROTTLE] = "THROTTLE",
33 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
34 [PERF_RECORD_FORK] = "FORK",
35 [PERF_RECORD_READ] = "READ",
36 [PERF_RECORD_SAMPLE] = "SAMPLE",
37 [PERF_RECORD_AUX] = "AUX",
38 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
39 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
40 [PERF_RECORD_SWITCH] = "SWITCH",
41 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
42 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
43 [PERF_RECORD_HEADER_ATTR] = "ATTR",
44 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
45 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
46 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
47 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
48 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
49 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
50 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
51 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
52 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
53 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
54 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
55 [PERF_RECORD_STAT] = "STAT",
56 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
57 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
58 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
61 static const char *perf_ns__names[] = {
62 [NET_NS_INDEX] = "net",
63 [UTS_NS_INDEX] = "uts",
64 [IPC_NS_INDEX] = "ipc",
65 [PID_NS_INDEX] = "pid",
66 [USER_NS_INDEX] = "user",
67 [MNT_NS_INDEX] = "mnt",
68 [CGROUP_NS_INDEX] = "cgroup",
71 const char *perf_event__name(unsigned int id)
73 if (id >= ARRAY_SIZE(perf_event__names))
75 if (!perf_event__names[id])
77 return perf_event__names[id];
80 static const char *perf_ns__name(unsigned int id)
82 if (id >= ARRAY_SIZE(perf_ns__names))
84 return perf_ns__names[id];
87 static int perf_tool__process_synth_event(struct perf_tool *tool,
88 union perf_event *event,
89 struct machine *machine,
90 perf_event__handler_t process)
92 struct perf_sample synth_sample = {
99 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
102 return process(tool, event, &synth_sample, machine);
106 * Assumes that the first 4095 bytes of /proc/pid/stat contains
107 * the comm, tgid and ppid.
109 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
110 pid_t *tgid, pid_t *ppid)
112 char filename[PATH_MAX];
117 char *name, *tgids, *ppids;
122 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
124 fd = open(filename, O_RDONLY);
126 pr_debug("couldn't open %s\n", filename);
130 n = read(fd, bf, sizeof(bf) - 1);
133 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
139 name = strstr(bf, "Name:");
140 tgids = strstr(bf, "Tgid:");
141 ppids = strstr(bf, "PPid:");
146 name += 5; /* strlen("Name:") */
149 nl = strchr(name, '\n');
156 memcpy(comm, name, size);
159 pr_debug("Name: string not found for pid %d\n", pid);
163 tgids += 5; /* strlen("Tgid:") */
166 pr_debug("Tgid: string not found for pid %d\n", pid);
170 ppids += 5; /* strlen("PPid:") */
173 pr_debug("PPid: string not found for pid %d\n", pid);
179 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
180 struct machine *machine,
181 pid_t *tgid, pid_t *ppid)
187 memset(&event->comm, 0, sizeof(event->comm));
189 if (machine__is_host(machine)) {
190 if (perf_event__get_comm_ids(pid, event->comm.comm,
191 sizeof(event->comm.comm),
196 *tgid = machine->pid;
202 event->comm.pid = *tgid;
203 event->comm.header.type = PERF_RECORD_COMM;
205 size = strlen(event->comm.comm) + 1;
206 size = PERF_ALIGN(size, sizeof(u64));
207 memset(event->comm.comm + size, 0, machine->id_hdr_size);
208 event->comm.header.size = (sizeof(event->comm) -
209 (sizeof(event->comm.comm) - size) +
210 machine->id_hdr_size);
211 event->comm.tid = pid;
216 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
217 union perf_event *event, pid_t pid,
218 perf_event__handler_t process,
219 struct machine *machine)
223 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
226 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
232 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
233 struct perf_ns_link_info *ns_link_info)
238 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
239 if (stat64(proc_ns, &st) == 0) {
240 ns_link_info->dev = st.st_dev;
241 ns_link_info->ino = st.st_ino;
245 int perf_event__synthesize_namespaces(struct perf_tool *tool,
246 union perf_event *event,
247 pid_t pid, pid_t tgid,
248 perf_event__handler_t process,
249 struct machine *machine)
252 struct perf_ns_link_info *ns_link_info;
254 if (!tool || !tool->namespace_events)
257 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
258 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
259 machine->id_hdr_size));
261 event->namespaces.pid = tgid;
262 event->namespaces.tid = pid;
264 event->namespaces.nr_namespaces = NR_NAMESPACES;
266 ns_link_info = event->namespaces.link_info;
268 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
269 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
272 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
274 event->namespaces.header.size = (sizeof(event->namespaces) +
275 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
276 machine->id_hdr_size);
278 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
284 static int perf_event__synthesize_fork(struct perf_tool *tool,
285 union perf_event *event,
286 pid_t pid, pid_t tgid, pid_t ppid,
287 perf_event__handler_t process,
288 struct machine *machine)
290 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
293 * for main thread set parent to ppid from status file. For other
294 * threads set parent pid to main thread. ie., assume main thread
295 * spawns all threads in a process
298 event->fork.ppid = ppid;
299 event->fork.ptid = ppid;
301 event->fork.ppid = tgid;
302 event->fork.ptid = tgid;
304 event->fork.pid = tgid;
305 event->fork.tid = pid;
306 event->fork.header.type = PERF_RECORD_FORK;
308 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
310 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
316 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
317 union perf_event *event,
318 pid_t pid, pid_t tgid,
319 perf_event__handler_t process,
320 struct machine *machine,
322 unsigned int proc_map_timeout)
324 char filename[PATH_MAX];
326 unsigned long long t;
327 bool truncation = false;
328 unsigned long long timeout = proc_map_timeout * 1000000ULL;
330 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
331 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
333 if (machine__is_default_guest(machine))
336 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
337 machine->root_dir, pid, pid);
339 fp = fopen(filename, "r");
342 * We raced with a task exiting - just return:
344 pr_debug("couldn't open %s\n", filename);
348 event->header.type = PERF_RECORD_MMAP2;
354 char execname[PATH_MAX];
355 char anonstr[] = "//anon";
360 if (fgets(bf, sizeof(bf), fp) == NULL)
363 if ((rdclock() - t) > timeout) {
364 pr_warning("Reading %s time out. "
365 "You may want to increase "
366 "the time limit by --proc-map-timeout\n",
372 /* ensure null termination since stack will be reused. */
373 strcpy(execname, "");
375 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
376 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
377 &event->mmap2.start, &event->mmap2.len, prot,
378 &event->mmap2.pgoff, &event->mmap2.maj,
383 * Anon maps don't have the execname.
388 event->mmap2.ino = (u64)ino;
391 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
393 if (machine__is_host(machine))
394 event->header.misc = PERF_RECORD_MISC_USER;
396 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
398 /* map protection and flags bits */
399 event->mmap2.prot = 0;
400 event->mmap2.flags = 0;
402 event->mmap2.prot |= PROT_READ;
404 event->mmap2.prot |= PROT_WRITE;
406 event->mmap2.prot |= PROT_EXEC;
409 event->mmap2.flags |= MAP_SHARED;
411 event->mmap2.flags |= MAP_PRIVATE;
413 if (prot[2] != 'x') {
414 if (!mmap_data || prot[0] != 'r')
417 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
422 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
424 if (!strcmp(execname, ""))
425 strcpy(execname, anonstr);
427 if (hugetlbfs_mnt_len &&
428 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
429 strcpy(execname, anonstr);
430 event->mmap2.flags |= MAP_HUGETLB;
433 size = strlen(execname) + 1;
434 memcpy(event->mmap2.filename, execname, size);
435 size = PERF_ALIGN(size, sizeof(u64));
436 event->mmap2.len -= event->mmap.start;
437 event->mmap2.header.size = (sizeof(event->mmap2) -
438 (sizeof(event->mmap2.filename) - size));
439 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
440 event->mmap2.header.size += machine->id_hdr_size;
441 event->mmap2.pid = tgid;
442 event->mmap2.tid = pid;
444 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
457 int perf_event__synthesize_modules(struct perf_tool *tool,
458 perf_event__handler_t process,
459 struct machine *machine)
463 struct map_groups *kmaps = &machine->kmaps;
464 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
465 union perf_event *event = zalloc((sizeof(event->mmap) +
466 machine->id_hdr_size));
468 pr_debug("Not enough memory synthesizing mmap event "
469 "for kernel modules\n");
473 event->header.type = PERF_RECORD_MMAP;
476 * kernel uses 0 for user space maps, see kernel/perf_event.c
479 if (machine__is_host(machine))
480 event->header.misc = PERF_RECORD_MISC_KERNEL;
482 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
484 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
487 if (__map__is_kernel(pos))
490 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
491 event->mmap.header.type = PERF_RECORD_MMAP;
492 event->mmap.header.size = (sizeof(event->mmap) -
493 (sizeof(event->mmap.filename) - size));
494 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
495 event->mmap.header.size += machine->id_hdr_size;
496 event->mmap.start = pos->start;
497 event->mmap.len = pos->end - pos->start;
498 event->mmap.pid = machine->pid;
500 memcpy(event->mmap.filename, pos->dso->long_name,
501 pos->dso->long_name_len + 1);
502 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
512 static int __event__synthesize_thread(union perf_event *comm_event,
513 union perf_event *mmap_event,
514 union perf_event *fork_event,
515 union perf_event *namespaces_event,
517 perf_event__handler_t process,
518 struct perf_tool *tool,
519 struct machine *machine,
521 unsigned int proc_map_timeout)
523 char filename[PATH_MAX];
525 struct dirent *dirent;
529 /* special case: only send one comm event using passed in pid */
531 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
537 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
538 tgid, process, machine) < 0)
542 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
543 process, machine, mmap_data,
547 if (machine__is_default_guest(machine))
550 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
551 machine->root_dir, pid);
553 tasks = opendir(filename);
555 pr_debug("couldn't open %s\n", filename);
559 while ((dirent = readdir(tasks)) != NULL) {
563 _pid = strtol(dirent->d_name, &end, 10);
568 if (perf_event__prepare_comm(comm_event, _pid, machine,
572 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
573 ppid, process, machine) < 0)
576 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
577 tgid, process, machine) < 0)
581 * Send the prepared comm event
583 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
588 /* process the parent's maps too */
589 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
590 process, machine, mmap_data, proc_map_timeout);
600 int perf_event__synthesize_thread_map(struct perf_tool *tool,
601 struct thread_map *threads,
602 perf_event__handler_t process,
603 struct machine *machine,
605 unsigned int proc_map_timeout)
607 union perf_event *comm_event, *mmap_event, *fork_event;
608 union perf_event *namespaces_event;
609 int err = -1, thread, j;
611 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
612 if (comm_event == NULL)
615 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
616 if (mmap_event == NULL)
619 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
620 if (fork_event == NULL)
623 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
624 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
625 machine->id_hdr_size);
626 if (namespaces_event == NULL)
630 for (thread = 0; thread < threads->nr; ++thread) {
631 if (__event__synthesize_thread(comm_event, mmap_event,
632 fork_event, namespaces_event,
633 thread_map__pid(threads, thread), 0,
634 process, tool, machine,
635 mmap_data, proc_map_timeout)) {
641 * comm.pid is set to thread group id by
642 * perf_event__synthesize_comm
644 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
645 bool need_leader = true;
647 /* is thread group leader in thread_map? */
648 for (j = 0; j < threads->nr; ++j) {
649 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
655 /* if not, generate events for it */
657 __event__synthesize_thread(comm_event, mmap_event,
658 fork_event, namespaces_event,
659 comm_event->comm.pid, 0,
660 process, tool, machine,
661 mmap_data, proc_map_timeout)) {
667 free(namespaces_event);
678 int perf_event__synthesize_threads(struct perf_tool *tool,
679 perf_event__handler_t process,
680 struct machine *machine,
682 unsigned int proc_map_timeout)
685 char proc_path[PATH_MAX];
686 struct dirent *dirent;
687 union perf_event *comm_event, *mmap_event, *fork_event;
688 union perf_event *namespaces_event;
691 if (machine__is_default_guest(machine))
694 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
695 if (comm_event == NULL)
698 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
699 if (mmap_event == NULL)
702 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
703 if (fork_event == NULL)
706 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
707 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
708 machine->id_hdr_size);
709 if (namespaces_event == NULL)
712 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
713 proc = opendir(proc_path);
716 goto out_free_namespaces;
718 while ((dirent = readdir(proc)) != NULL) {
720 pid_t pid = strtol(dirent->d_name, &end, 10);
722 if (*end) /* only interested in proper numerical dirents */
725 * We may race with exiting thread, so don't stop just because
726 * one thread couldn't be synthesized.
728 __event__synthesize_thread(comm_event, mmap_event, fork_event,
729 namespaces_event, pid, 1, process,
730 tool, machine, mmap_data,
737 free(namespaces_event);
748 struct process_symbol_args {
753 static int find_symbol_cb(void *arg, const char *name, char type,
756 struct process_symbol_args *args = arg;
759 * Must be a function or at least an alias, as in PARISC64, where "_text" is
760 * an 'A' to the same address as "_stext".
762 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
763 type == 'A') || strcmp(name, args->name))
770 u64 kallsyms__get_function_start(const char *kallsyms_filename,
771 const char *symbol_name)
773 struct process_symbol_args args = { .name = symbol_name, };
775 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
781 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
782 perf_event__handler_t process,
783 struct machine *machine)
786 const char *mmap_name;
787 char name_buff[PATH_MAX];
788 struct map *map = machine__kernel_map(machine);
791 union perf_event *event;
793 if (symbol_conf.kptr_restrict)
799 * We should get this from /sys/kernel/sections/.text, but till that is
800 * available use this, and after it is use this as a fallback for older
803 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
805 pr_debug("Not enough memory synthesizing mmap event "
806 "for kernel modules\n");
810 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
811 if (machine__is_host(machine)) {
813 * kernel uses PERF_RECORD_MISC_USER for user space maps,
814 * see kernel/perf_event.c __perf_event_mmap
816 event->header.misc = PERF_RECORD_MISC_KERNEL;
818 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
821 kmap = map__kmap(map);
822 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
823 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
824 size = PERF_ALIGN(size, sizeof(u64));
825 event->mmap.header.type = PERF_RECORD_MMAP;
826 event->mmap.header.size = (sizeof(event->mmap) -
827 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
828 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
829 event->mmap.start = map->start;
830 event->mmap.len = map->end - event->mmap.start;
831 event->mmap.pid = machine->pid;
833 err = perf_tool__process_synth_event(tool, event, machine, process);
839 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
840 struct thread_map *threads,
841 perf_event__handler_t process,
842 struct machine *machine)
844 union perf_event *event;
847 size = sizeof(event->thread_map);
848 size += threads->nr * sizeof(event->thread_map.entries[0]);
850 event = zalloc(size);
854 event->header.type = PERF_RECORD_THREAD_MAP;
855 event->header.size = size;
856 event->thread_map.nr = threads->nr;
858 for (i = 0; i < threads->nr; i++) {
859 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
860 char *comm = thread_map__comm(threads, i);
865 entry->pid = thread_map__pid(threads, i);
866 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
869 err = process(tool, event, NULL, machine);
875 static void synthesize_cpus(struct cpu_map_entries *cpus,
882 for (i = 0; i < map->nr; i++)
883 cpus->cpu[i] = map->map[i];
886 static void synthesize_mask(struct cpu_map_mask *mask,
887 struct cpu_map *map, int max)
891 mask->nr = BITS_TO_LONGS(max);
892 mask->long_size = sizeof(long);
894 for (i = 0; i < map->nr; i++)
895 set_bit(map->map[i], mask->mask);
898 static size_t cpus_size(struct cpu_map *map)
900 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
903 static size_t mask_size(struct cpu_map *map, int *max)
909 for (i = 0; i < map->nr; i++) {
910 /* bit possition of the cpu is + 1 */
911 int bit = map->map[i] + 1;
917 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
920 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
922 size_t size_cpus, size_mask;
923 bool is_dummy = cpu_map__empty(map);
926 * Both array and mask data have variable size based
927 * on the number of cpus and their actual values.
928 * The size of the 'struct cpu_map_data' is:
930 * array = size of 'struct cpu_map_entries' +
931 * number of cpus * sizeof(u64)
933 * mask = size of 'struct cpu_map_mask' +
934 * maximum cpu bit converted to size of longs
936 * and finaly + the size of 'struct cpu_map_data'.
938 size_cpus = cpus_size(map);
939 size_mask = mask_size(map, max);
941 if (is_dummy || (size_cpus < size_mask)) {
943 *type = PERF_CPU_MAP__CPUS;
946 *type = PERF_CPU_MAP__MASK;
949 *size += sizeof(struct cpu_map_data);
950 return zalloc(*size);
953 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
959 case PERF_CPU_MAP__CPUS:
960 synthesize_cpus((struct cpu_map_entries *) data->data, map);
962 case PERF_CPU_MAP__MASK:
963 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
969 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
971 size_t size = sizeof(struct cpu_map_event);
972 struct cpu_map_event *event;
976 event = cpu_map_data__alloc(map, &size, &type, &max);
980 event->header.type = PERF_RECORD_CPU_MAP;
981 event->header.size = size;
982 event->data.type = type;
984 cpu_map_data__synthesize(&event->data, map, type, max);
988 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
990 perf_event__handler_t process,
991 struct machine *machine)
993 struct cpu_map_event *event;
996 event = cpu_map_event__new(map);
1000 err = process(tool, (union perf_event *) event, NULL, machine);
1006 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1007 struct perf_stat_config *config,
1008 perf_event__handler_t process,
1009 struct machine *machine)
1011 struct stat_config_event *event;
1012 int size, i = 0, err;
1014 size = sizeof(*event);
1015 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1017 event = zalloc(size);
1021 event->header.type = PERF_RECORD_STAT_CONFIG;
1022 event->header.size = size;
1023 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1025 #define ADD(__term, __val) \
1026 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1027 event->data[i].val = __val; \
1030 ADD(AGGR_MODE, config->aggr_mode)
1031 ADD(INTERVAL, config->interval)
1032 ADD(SCALE, config->scale)
1034 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1035 "stat config terms unbalanced\n");
1038 err = process(tool, (union perf_event *) event, NULL, machine);
1044 int perf_event__synthesize_stat(struct perf_tool *tool,
1045 u32 cpu, u32 thread, u64 id,
1046 struct perf_counts_values *count,
1047 perf_event__handler_t process,
1048 struct machine *machine)
1050 struct stat_event event;
1052 event.header.type = PERF_RECORD_STAT;
1053 event.header.size = sizeof(event);
1054 event.header.misc = 0;
1058 event.thread = thread;
1059 event.val = count->val;
1060 event.ena = count->ena;
1061 event.run = count->run;
1063 return process(tool, (union perf_event *) &event, NULL, machine);
1066 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1067 u64 evtime, u64 type,
1068 perf_event__handler_t process,
1069 struct machine *machine)
1071 struct stat_round_event event;
1073 event.header.type = PERF_RECORD_STAT_ROUND;
1074 event.header.size = sizeof(event);
1075 event.header.misc = 0;
1077 event.time = evtime;
1080 return process(tool, (union perf_event *) &event, NULL, machine);
1083 void perf_event__read_stat_config(struct perf_stat_config *config,
1084 struct stat_config_event *event)
1088 for (i = 0; i < event->nr; i++) {
1090 switch (event->data[i].tag) {
1091 #define CASE(__term, __val) \
1092 case PERF_STAT_CONFIG_TERM__##__term: \
1093 config->__val = event->data[i].val; \
1096 CASE(AGGR_MODE, aggr_mode)
1098 CASE(INTERVAL, interval)
1101 pr_warning("unknown stat config term %" PRIu64 "\n",
1102 event->data[i].tag);
1107 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1111 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1116 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1119 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1122 struct perf_ns_link_info *ns_link_info;
1123 u32 nr_namespaces, idx;
1125 ns_link_info = event->namespaces.link_info;
1126 nr_namespaces = event->namespaces.nr_namespaces;
1128 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1129 event->namespaces.pid,
1130 event->namespaces.tid,
1133 for (idx = 0; idx < nr_namespaces; idx++) {
1134 if (idx && (idx % 4 == 0))
1135 ret += fprintf(fp, "\n\t\t ");
1137 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1138 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1139 (u64)ns_link_info[idx].ino,
1140 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1146 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1147 union perf_event *event,
1148 struct perf_sample *sample,
1149 struct machine *machine)
1151 return machine__process_comm_event(machine, event, sample);
1154 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1155 union perf_event *event,
1156 struct perf_sample *sample,
1157 struct machine *machine)
1159 return machine__process_namespaces_event(machine, event, sample);
1162 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1163 union perf_event *event,
1164 struct perf_sample *sample,
1165 struct machine *machine)
1167 return machine__process_lost_event(machine, event, sample);
1170 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1171 union perf_event *event,
1172 struct perf_sample *sample __maybe_unused,
1173 struct machine *machine)
1175 return machine__process_aux_event(machine, event);
1178 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1179 union perf_event *event,
1180 struct perf_sample *sample __maybe_unused,
1181 struct machine *machine)
1183 return machine__process_itrace_start_event(machine, event);
1186 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1187 union perf_event *event,
1188 struct perf_sample *sample,
1189 struct machine *machine)
1191 return machine__process_lost_samples_event(machine, event, sample);
1194 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1195 union perf_event *event,
1196 struct perf_sample *sample __maybe_unused,
1197 struct machine *machine)
1199 return machine__process_switch_event(machine, event);
1202 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1204 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1205 event->mmap.pid, event->mmap.tid, event->mmap.start,
1206 event->mmap.len, event->mmap.pgoff,
1207 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1208 event->mmap.filename);
1211 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1213 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1214 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1215 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1216 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1217 event->mmap2.min, event->mmap2.ino,
1218 event->mmap2.ino_generation,
1219 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1220 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1221 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1222 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1223 event->mmap2.filename);
1226 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1228 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1231 ret = fprintf(fp, " nr: ");
1234 ret += thread_map__fprintf(threads, fp);
1236 ret += fprintf(fp, "failed to get threads from event\n");
1238 thread_map__put(threads);
1242 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1244 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1247 ret = fprintf(fp, ": ");
1250 ret += cpu_map__fprintf(cpus, fp);
1252 ret += fprintf(fp, "failed to get cpumap from event\n");
1258 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1259 union perf_event *event,
1260 struct perf_sample *sample,
1261 struct machine *machine)
1263 return machine__process_mmap_event(machine, event, sample);
1266 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1267 union perf_event *event,
1268 struct perf_sample *sample,
1269 struct machine *machine)
1271 return machine__process_mmap2_event(machine, event, sample);
1274 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1276 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1277 event->fork.pid, event->fork.tid,
1278 event->fork.ppid, event->fork.ptid);
1281 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1282 union perf_event *event,
1283 struct perf_sample *sample,
1284 struct machine *machine)
1286 return machine__process_fork_event(machine, event, sample);
1289 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1290 union perf_event *event,
1291 struct perf_sample *sample,
1292 struct machine *machine)
1294 return machine__process_exit_event(machine, event, sample);
1297 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1299 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n",
1300 event->aux.aux_offset, event->aux.aux_size,
1302 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1303 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1304 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
1307 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1309 return fprintf(fp, " pid: %u tid: %u\n",
1310 event->itrace_start.pid, event->itrace_start.tid);
1313 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1315 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1316 const char *in_out = out ? "OUT" : "IN ";
1318 if (event->header.type == PERF_RECORD_SWITCH)
1319 return fprintf(fp, " %s\n", in_out);
1321 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1322 in_out, out ? "next" : "prev",
1323 event->context_switch.next_prev_pid,
1324 event->context_switch.next_prev_tid);
1327 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1329 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1330 perf_event__name(event->header.type));
1332 switch (event->header.type) {
1333 case PERF_RECORD_COMM:
1334 ret += perf_event__fprintf_comm(event, fp);
1336 case PERF_RECORD_FORK:
1337 case PERF_RECORD_EXIT:
1338 ret += perf_event__fprintf_task(event, fp);
1340 case PERF_RECORD_MMAP:
1341 ret += perf_event__fprintf_mmap(event, fp);
1343 case PERF_RECORD_NAMESPACES:
1344 ret += perf_event__fprintf_namespaces(event, fp);
1346 case PERF_RECORD_MMAP2:
1347 ret += perf_event__fprintf_mmap2(event, fp);
1349 case PERF_RECORD_AUX:
1350 ret += perf_event__fprintf_aux(event, fp);
1352 case PERF_RECORD_ITRACE_START:
1353 ret += perf_event__fprintf_itrace_start(event, fp);
1355 case PERF_RECORD_SWITCH:
1356 case PERF_RECORD_SWITCH_CPU_WIDE:
1357 ret += perf_event__fprintf_switch(event, fp);
1360 ret += fprintf(fp, "\n");
1366 int perf_event__process(struct perf_tool *tool __maybe_unused,
1367 union perf_event *event,
1368 struct perf_sample *sample,
1369 struct machine *machine)
1371 return machine__process_event(machine, event, sample);
1374 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1375 enum map_type type, u64 addr,
1376 struct addr_location *al)
1378 struct map_groups *mg = thread->mg;
1379 struct machine *machine = mg->machine;
1380 bool load_map = false;
1382 al->machine = machine;
1383 al->thread = thread;
1385 al->cpumode = cpumode;
1388 if (machine == NULL) {
1393 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1395 mg = &machine->kmaps;
1397 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1399 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1401 mg = &machine->kmaps;
1403 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1409 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1410 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1412 al->filtered |= (1 << HIST_FILTER__GUEST);
1413 if ((cpumode == PERF_RECORD_MISC_USER ||
1414 cpumode == PERF_RECORD_MISC_KERNEL) &&
1416 al->filtered |= (1 << HIST_FILTER__HOST);
1421 al->map = map_groups__find(mg, type, al->addr);
1422 if (al->map == NULL) {
1424 * If this is outside of all known maps, and is a negative
1425 * address, try to look it up in the kernel dso, as it might be
1426 * a vsyscall or vdso (which executes in user-mode).
1428 * XXX This is nasty, we should have a symbol list in the
1429 * "[vdso]" dso, but for now lets use the old trick of looking
1430 * in the whole kernel symbol list.
1432 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1433 mg != &machine->kmaps &&
1434 machine__kernel_ip(machine, al->addr)) {
1435 mg = &machine->kmaps;
1441 * Kernel maps might be changed when loading symbols so loading
1442 * must be done prior to using kernel maps.
1446 al->addr = al->map->map_ip(al->map, al->addr);
1450 void thread__find_addr_location(struct thread *thread,
1451 u8 cpumode, enum map_type type, u64 addr,
1452 struct addr_location *al)
1454 thread__find_addr_map(thread, cpumode, type, addr, al);
1455 if (al->map != NULL)
1456 al->sym = map__find_symbol(al->map, al->addr);
1462 * Callers need to drop the reference to al->thread, obtained in
1463 * machine__findnew_thread()
1465 int machine__resolve(struct machine *machine, struct addr_location *al,
1466 struct perf_sample *sample)
1468 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1474 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1476 * Have we already created the kernel maps for this machine?
1478 * This should have happened earlier, when we processed the kernel MMAP
1479 * events, but for older perf.data files there was no such thing, so do
1482 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1483 machine__kernel_map(machine) == NULL)
1484 machine__create_kernel_maps(machine);
1486 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1487 dump_printf(" ...... dso: %s\n",
1488 al->map ? al->map->dso->long_name :
1489 al->level == 'H' ? "[hypervisor]" : "<not found>");
1491 if (thread__is_filtered(thread))
1492 al->filtered |= (1 << HIST_FILTER__THREAD);
1495 al->cpu = sample->cpu;
1499 struct perf_env *env = machine->env;
1501 if (env && env->cpu)
1502 al->socket = env->cpu[al->cpu].socket_id;
1506 struct dso *dso = al->map->dso;
1508 if (symbol_conf.dso_list &&
1509 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1511 (dso->short_name != dso->long_name &&
1512 strlist__has_entry(symbol_conf.dso_list,
1513 dso->long_name))))) {
1514 al->filtered |= (1 << HIST_FILTER__DSO);
1517 al->sym = map__find_symbol(al->map, al->addr);
1520 if (symbol_conf.sym_list &&
1521 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1523 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1530 * The preprocess_sample method will return with reference counts for the
1531 * in it, when done using (and perhaps getting ref counts if needing to
1532 * keep a pointer to one of those entries) it must be paired with
1533 * addr_location__put(), so that the refcounts can be decremented.
1535 void addr_location__put(struct addr_location *al)
1537 thread__zput(al->thread);
1540 bool is_bts_event(struct perf_event_attr *attr)
1542 return attr->type == PERF_TYPE_HARDWARE &&
1543 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1544 attr->sample_period == 1;
1547 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1549 if (attr->type == PERF_TYPE_SOFTWARE &&
1550 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1551 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1552 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1555 if (is_bts_event(attr))
1561 void thread__resolve(struct thread *thread, struct addr_location *al,
1562 struct perf_sample *sample)
1564 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1566 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1569 al->cpu = sample->cpu;
1573 al->sym = map__find_symbol(al->map, al->addr);