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>
11 #include <linux/perf_event.h>
20 #include "thread_map.h"
21 #include "sane_ctype.h"
22 #include "symbol/kallsyms.h"
26 static const char *perf_event__names[] = {
28 [PERF_RECORD_MMAP] = "MMAP",
29 [PERF_RECORD_MMAP2] = "MMAP2",
30 [PERF_RECORD_LOST] = "LOST",
31 [PERF_RECORD_COMM] = "COMM",
32 [PERF_RECORD_EXIT] = "EXIT",
33 [PERF_RECORD_THROTTLE] = "THROTTLE",
34 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
35 [PERF_RECORD_FORK] = "FORK",
36 [PERF_RECORD_READ] = "READ",
37 [PERF_RECORD_SAMPLE] = "SAMPLE",
38 [PERF_RECORD_AUX] = "AUX",
39 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
40 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
41 [PERF_RECORD_SWITCH] = "SWITCH",
42 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
43 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
44 [PERF_RECORD_HEADER_ATTR] = "ATTR",
45 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
46 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
47 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
48 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
49 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
50 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
51 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
52 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
53 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
54 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
55 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
56 [PERF_RECORD_STAT] = "STAT",
57 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
58 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
59 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
60 [PERF_RECORD_HEADER_FEATURE] = "FEATURE",
63 static const char *perf_ns__names[] = {
64 [NET_NS_INDEX] = "net",
65 [UTS_NS_INDEX] = "uts",
66 [IPC_NS_INDEX] = "ipc",
67 [PID_NS_INDEX] = "pid",
68 [USER_NS_INDEX] = "user",
69 [MNT_NS_INDEX] = "mnt",
70 [CGROUP_NS_INDEX] = "cgroup",
73 const char *perf_event__name(unsigned int id)
75 if (id >= ARRAY_SIZE(perf_event__names))
77 if (!perf_event__names[id])
79 return perf_event__names[id];
82 static const char *perf_ns__name(unsigned int id)
84 if (id >= ARRAY_SIZE(perf_ns__names))
86 return perf_ns__names[id];
89 static int perf_tool__process_synth_event(struct perf_tool *tool,
90 union perf_event *event,
91 struct machine *machine,
92 perf_event__handler_t process)
94 struct perf_sample synth_sample = {
101 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
104 return process(tool, event, &synth_sample, machine);
108 * Assumes that the first 4095 bytes of /proc/pid/stat contains
109 * the comm, tgid and ppid.
111 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
112 pid_t *tgid, pid_t *ppid)
114 char filename[PATH_MAX];
119 char *name, *tgids, *ppids;
124 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
126 fd = open(filename, O_RDONLY);
128 pr_debug("couldn't open %s\n", filename);
132 n = read(fd, bf, sizeof(bf) - 1);
135 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
141 name = strstr(bf, "Name:");
142 tgids = strstr(bf, "Tgid:");
143 ppids = strstr(bf, "PPid:");
148 name += 5; /* strlen("Name:") */
151 nl = strchr(name, '\n');
158 memcpy(comm, name, size);
161 pr_debug("Name: string not found for pid %d\n", pid);
165 tgids += 5; /* strlen("Tgid:") */
168 pr_debug("Tgid: string not found for pid %d\n", pid);
172 ppids += 5; /* strlen("PPid:") */
175 pr_debug("PPid: string not found for pid %d\n", pid);
181 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
182 struct machine *machine,
183 pid_t *tgid, pid_t *ppid)
189 memset(&event->comm, 0, sizeof(event->comm));
191 if (machine__is_host(machine)) {
192 if (perf_event__get_comm_ids(pid, event->comm.comm,
193 sizeof(event->comm.comm),
198 *tgid = machine->pid;
204 event->comm.pid = *tgid;
205 event->comm.header.type = PERF_RECORD_COMM;
207 size = strlen(event->comm.comm) + 1;
208 size = PERF_ALIGN(size, sizeof(u64));
209 memset(event->comm.comm + size, 0, machine->id_hdr_size);
210 event->comm.header.size = (sizeof(event->comm) -
211 (sizeof(event->comm.comm) - size) +
212 machine->id_hdr_size);
213 event->comm.tid = pid;
218 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
219 union perf_event *event, pid_t pid,
220 perf_event__handler_t process,
221 struct machine *machine)
225 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
228 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
234 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
235 struct perf_ns_link_info *ns_link_info)
240 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
241 if (stat64(proc_ns, &st) == 0) {
242 ns_link_info->dev = st.st_dev;
243 ns_link_info->ino = st.st_ino;
247 int perf_event__synthesize_namespaces(struct perf_tool *tool,
248 union perf_event *event,
249 pid_t pid, pid_t tgid,
250 perf_event__handler_t process,
251 struct machine *machine)
254 struct perf_ns_link_info *ns_link_info;
256 if (!tool || !tool->namespace_events)
259 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
260 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
261 machine->id_hdr_size));
263 event->namespaces.pid = tgid;
264 event->namespaces.tid = pid;
266 event->namespaces.nr_namespaces = NR_NAMESPACES;
268 ns_link_info = event->namespaces.link_info;
270 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
271 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
274 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
276 event->namespaces.header.size = (sizeof(event->namespaces) +
277 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
278 machine->id_hdr_size);
280 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
286 static int perf_event__synthesize_fork(struct perf_tool *tool,
287 union perf_event *event,
288 pid_t pid, pid_t tgid, pid_t ppid,
289 perf_event__handler_t process,
290 struct machine *machine)
292 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
295 * for main thread set parent to ppid from status file. For other
296 * threads set parent pid to main thread. ie., assume main thread
297 * spawns all threads in a process
300 event->fork.ppid = ppid;
301 event->fork.ptid = ppid;
303 event->fork.ppid = tgid;
304 event->fork.ptid = tgid;
306 event->fork.pid = tgid;
307 event->fork.tid = pid;
308 event->fork.header.type = PERF_RECORD_FORK;
310 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
312 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
318 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
319 union perf_event *event,
320 pid_t pid, pid_t tgid,
321 perf_event__handler_t process,
322 struct machine *machine,
324 unsigned int proc_map_timeout)
326 char filename[PATH_MAX];
328 unsigned long long t;
329 bool truncation = false;
330 unsigned long long timeout = proc_map_timeout * 1000000ULL;
332 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
333 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
335 if (machine__is_default_guest(machine))
338 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
339 machine->root_dir, pid, pid);
341 fp = fopen(filename, "r");
344 * We raced with a task exiting - just return:
346 pr_debug("couldn't open %s\n", filename);
350 event->header.type = PERF_RECORD_MMAP2;
356 char execname[PATH_MAX];
357 char anonstr[] = "//anon";
362 if (fgets(bf, sizeof(bf), fp) == NULL)
365 if ((rdclock() - t) > timeout) {
366 pr_warning("Reading %s time out. "
367 "You may want to increase "
368 "the time limit by --proc-map-timeout\n",
374 /* ensure null termination since stack will be reused. */
375 strcpy(execname, "");
377 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
378 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
379 &event->mmap2.start, &event->mmap2.len, prot,
380 &event->mmap2.pgoff, &event->mmap2.maj,
385 * Anon maps don't have the execname.
390 event->mmap2.ino = (u64)ino;
393 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
395 if (machine__is_host(machine))
396 event->header.misc = PERF_RECORD_MISC_USER;
398 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
400 /* map protection and flags bits */
401 event->mmap2.prot = 0;
402 event->mmap2.flags = 0;
404 event->mmap2.prot |= PROT_READ;
406 event->mmap2.prot |= PROT_WRITE;
408 event->mmap2.prot |= PROT_EXEC;
411 event->mmap2.flags |= MAP_SHARED;
413 event->mmap2.flags |= MAP_PRIVATE;
415 if (prot[2] != 'x') {
416 if (!mmap_data || prot[0] != 'r')
419 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
424 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
426 if (!strcmp(execname, ""))
427 strcpy(execname, anonstr);
429 if (hugetlbfs_mnt_len &&
430 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
431 strcpy(execname, anonstr);
432 event->mmap2.flags |= MAP_HUGETLB;
435 size = strlen(execname) + 1;
436 memcpy(event->mmap2.filename, execname, size);
437 size = PERF_ALIGN(size, sizeof(u64));
438 event->mmap2.len -= event->mmap.start;
439 event->mmap2.header.size = (sizeof(event->mmap2) -
440 (sizeof(event->mmap2.filename) - size));
441 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
442 event->mmap2.header.size += machine->id_hdr_size;
443 event->mmap2.pid = tgid;
444 event->mmap2.tid = pid;
446 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
459 int perf_event__synthesize_modules(struct perf_tool *tool,
460 perf_event__handler_t process,
461 struct machine *machine)
465 struct map_groups *kmaps = &machine->kmaps;
466 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
467 union perf_event *event = zalloc((sizeof(event->mmap) +
468 machine->id_hdr_size));
470 pr_debug("Not enough memory synthesizing mmap event "
471 "for kernel modules\n");
475 event->header.type = PERF_RECORD_MMAP;
478 * kernel uses 0 for user space maps, see kernel/perf_event.c
481 if (machine__is_host(machine))
482 event->header.misc = PERF_RECORD_MISC_KERNEL;
484 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
486 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
489 if (__map__is_kernel(pos))
492 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
493 event->mmap.header.type = PERF_RECORD_MMAP;
494 event->mmap.header.size = (sizeof(event->mmap) -
495 (sizeof(event->mmap.filename) - size));
496 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
497 event->mmap.header.size += machine->id_hdr_size;
498 event->mmap.start = pos->start;
499 event->mmap.len = pos->end - pos->start;
500 event->mmap.pid = machine->pid;
502 memcpy(event->mmap.filename, pos->dso->long_name,
503 pos->dso->long_name_len + 1);
504 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
514 static int __event__synthesize_thread(union perf_event *comm_event,
515 union perf_event *mmap_event,
516 union perf_event *fork_event,
517 union perf_event *namespaces_event,
519 perf_event__handler_t process,
520 struct perf_tool *tool,
521 struct machine *machine,
523 unsigned int proc_map_timeout)
525 char filename[PATH_MAX];
527 struct dirent *dirent;
531 /* special case: only send one comm event using passed in pid */
533 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
539 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
540 tgid, process, machine) < 0)
544 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
545 process, machine, mmap_data,
549 if (machine__is_default_guest(machine))
552 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
553 machine->root_dir, pid);
555 tasks = opendir(filename);
557 pr_debug("couldn't open %s\n", filename);
561 while ((dirent = readdir(tasks)) != NULL) {
565 _pid = strtol(dirent->d_name, &end, 10);
570 if (perf_event__prepare_comm(comm_event, _pid, machine,
574 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
575 ppid, process, machine) < 0)
578 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
579 tgid, process, machine) < 0)
583 * Send the prepared comm event
585 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
590 /* process the parent's maps too */
591 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
592 process, machine, mmap_data, proc_map_timeout);
602 int perf_event__synthesize_thread_map(struct perf_tool *tool,
603 struct thread_map *threads,
604 perf_event__handler_t process,
605 struct machine *machine,
607 unsigned int proc_map_timeout)
609 union perf_event *comm_event, *mmap_event, *fork_event;
610 union perf_event *namespaces_event;
611 int err = -1, thread, j;
613 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
614 if (comm_event == NULL)
617 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
618 if (mmap_event == NULL)
621 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
622 if (fork_event == NULL)
625 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
626 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
627 machine->id_hdr_size);
628 if (namespaces_event == NULL)
632 for (thread = 0; thread < threads->nr; ++thread) {
633 if (__event__synthesize_thread(comm_event, mmap_event,
634 fork_event, namespaces_event,
635 thread_map__pid(threads, thread), 0,
636 process, tool, machine,
637 mmap_data, proc_map_timeout)) {
643 * comm.pid is set to thread group id by
644 * perf_event__synthesize_comm
646 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
647 bool need_leader = true;
649 /* is thread group leader in thread_map? */
650 for (j = 0; j < threads->nr; ++j) {
651 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
657 /* if not, generate events for it */
659 __event__synthesize_thread(comm_event, mmap_event,
660 fork_event, namespaces_event,
661 comm_event->comm.pid, 0,
662 process, tool, machine,
663 mmap_data, proc_map_timeout)) {
669 free(namespaces_event);
680 int perf_event__synthesize_threads(struct perf_tool *tool,
681 perf_event__handler_t process,
682 struct machine *machine,
684 unsigned int proc_map_timeout)
687 char proc_path[PATH_MAX];
688 struct dirent *dirent;
689 union perf_event *comm_event, *mmap_event, *fork_event;
690 union perf_event *namespaces_event;
693 if (machine__is_default_guest(machine))
696 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
697 if (comm_event == NULL)
700 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
701 if (mmap_event == NULL)
704 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
705 if (fork_event == NULL)
708 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
709 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
710 machine->id_hdr_size);
711 if (namespaces_event == NULL)
714 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
715 proc = opendir(proc_path);
718 goto out_free_namespaces;
720 while ((dirent = readdir(proc)) != NULL) {
722 pid_t pid = strtol(dirent->d_name, &end, 10);
724 if (*end) /* only interested in proper numerical dirents */
727 * We may race with exiting thread, so don't stop just because
728 * one thread couldn't be synthesized.
730 __event__synthesize_thread(comm_event, mmap_event, fork_event,
731 namespaces_event, pid, 1, process,
732 tool, machine, mmap_data,
739 free(namespaces_event);
750 struct process_symbol_args {
755 static int find_symbol_cb(void *arg, const char *name, char type,
758 struct process_symbol_args *args = arg;
761 * Must be a function or at least an alias, as in PARISC64, where "_text" is
762 * an 'A' to the same address as "_stext".
764 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
765 type == 'A') || strcmp(name, args->name))
772 int kallsyms__get_function_start(const char *kallsyms_filename,
773 const char *symbol_name, u64 *addr)
775 struct process_symbol_args args = { .name = symbol_name, };
777 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
784 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
785 perf_event__handler_t process,
786 struct machine *machine)
789 const char *mmap_name;
790 char name_buff[PATH_MAX];
791 struct map *map = machine__kernel_map(machine);
794 union perf_event *event;
796 if (symbol_conf.kptr_restrict)
802 * We should get this from /sys/kernel/sections/.text, but till that is
803 * available use this, and after it is use this as a fallback for older
806 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
808 pr_debug("Not enough memory synthesizing mmap event "
809 "for kernel modules\n");
813 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
814 if (machine__is_host(machine)) {
816 * kernel uses PERF_RECORD_MISC_USER for user space maps,
817 * see kernel/perf_event.c __perf_event_mmap
819 event->header.misc = PERF_RECORD_MISC_KERNEL;
821 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
824 kmap = map__kmap(map);
825 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
826 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
827 size = PERF_ALIGN(size, sizeof(u64));
828 event->mmap.header.type = PERF_RECORD_MMAP;
829 event->mmap.header.size = (sizeof(event->mmap) -
830 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
831 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
832 event->mmap.start = map->start;
833 event->mmap.len = map->end - event->mmap.start;
834 event->mmap.pid = machine->pid;
836 err = perf_tool__process_synth_event(tool, event, machine, process);
842 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
843 struct thread_map *threads,
844 perf_event__handler_t process,
845 struct machine *machine)
847 union perf_event *event;
850 size = sizeof(event->thread_map);
851 size += threads->nr * sizeof(event->thread_map.entries[0]);
853 event = zalloc(size);
857 event->header.type = PERF_RECORD_THREAD_MAP;
858 event->header.size = size;
859 event->thread_map.nr = threads->nr;
861 for (i = 0; i < threads->nr; i++) {
862 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
863 char *comm = thread_map__comm(threads, i);
868 entry->pid = thread_map__pid(threads, i);
869 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
872 err = process(tool, event, NULL, machine);
878 static void synthesize_cpus(struct cpu_map_entries *cpus,
885 for (i = 0; i < map->nr; i++)
886 cpus->cpu[i] = map->map[i];
889 static void synthesize_mask(struct cpu_map_mask *mask,
890 struct cpu_map *map, int max)
894 mask->nr = BITS_TO_LONGS(max);
895 mask->long_size = sizeof(long);
897 for (i = 0; i < map->nr; i++)
898 set_bit(map->map[i], mask->mask);
901 static size_t cpus_size(struct cpu_map *map)
903 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
906 static size_t mask_size(struct cpu_map *map, int *max)
912 for (i = 0; i < map->nr; i++) {
913 /* bit possition of the cpu is + 1 */
914 int bit = map->map[i] + 1;
920 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
923 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
925 size_t size_cpus, size_mask;
926 bool is_dummy = cpu_map__empty(map);
929 * Both array and mask data have variable size based
930 * on the number of cpus and their actual values.
931 * The size of the 'struct cpu_map_data' is:
933 * array = size of 'struct cpu_map_entries' +
934 * number of cpus * sizeof(u64)
936 * mask = size of 'struct cpu_map_mask' +
937 * maximum cpu bit converted to size of longs
939 * and finaly + the size of 'struct cpu_map_data'.
941 size_cpus = cpus_size(map);
942 size_mask = mask_size(map, max);
944 if (is_dummy || (size_cpus < size_mask)) {
946 *type = PERF_CPU_MAP__CPUS;
949 *type = PERF_CPU_MAP__MASK;
952 *size += sizeof(struct cpu_map_data);
953 return zalloc(*size);
956 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
962 case PERF_CPU_MAP__CPUS:
963 synthesize_cpus((struct cpu_map_entries *) data->data, map);
965 case PERF_CPU_MAP__MASK:
966 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
972 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
974 size_t size = sizeof(struct cpu_map_event);
975 struct cpu_map_event *event;
979 event = cpu_map_data__alloc(map, &size, &type, &max);
983 event->header.type = PERF_RECORD_CPU_MAP;
984 event->header.size = size;
985 event->data.type = type;
987 cpu_map_data__synthesize(&event->data, map, type, max);
991 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
993 perf_event__handler_t process,
994 struct machine *machine)
996 struct cpu_map_event *event;
999 event = cpu_map_event__new(map);
1003 err = process(tool, (union perf_event *) event, NULL, machine);
1009 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1010 struct perf_stat_config *config,
1011 perf_event__handler_t process,
1012 struct machine *machine)
1014 struct stat_config_event *event;
1015 int size, i = 0, err;
1017 size = sizeof(*event);
1018 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1020 event = zalloc(size);
1024 event->header.type = PERF_RECORD_STAT_CONFIG;
1025 event->header.size = size;
1026 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1028 #define ADD(__term, __val) \
1029 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1030 event->data[i].val = __val; \
1033 ADD(AGGR_MODE, config->aggr_mode)
1034 ADD(INTERVAL, config->interval)
1035 ADD(SCALE, config->scale)
1037 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1038 "stat config terms unbalanced\n");
1041 err = process(tool, (union perf_event *) event, NULL, machine);
1047 int perf_event__synthesize_stat(struct perf_tool *tool,
1048 u32 cpu, u32 thread, u64 id,
1049 struct perf_counts_values *count,
1050 perf_event__handler_t process,
1051 struct machine *machine)
1053 struct stat_event event;
1055 event.header.type = PERF_RECORD_STAT;
1056 event.header.size = sizeof(event);
1057 event.header.misc = 0;
1061 event.thread = thread;
1062 event.val = count->val;
1063 event.ena = count->ena;
1064 event.run = count->run;
1066 return process(tool, (union perf_event *) &event, NULL, machine);
1069 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1070 u64 evtime, u64 type,
1071 perf_event__handler_t process,
1072 struct machine *machine)
1074 struct stat_round_event event;
1076 event.header.type = PERF_RECORD_STAT_ROUND;
1077 event.header.size = sizeof(event);
1078 event.header.misc = 0;
1080 event.time = evtime;
1083 return process(tool, (union perf_event *) &event, NULL, machine);
1086 void perf_event__read_stat_config(struct perf_stat_config *config,
1087 struct stat_config_event *event)
1091 for (i = 0; i < event->nr; i++) {
1093 switch (event->data[i].tag) {
1094 #define CASE(__term, __val) \
1095 case PERF_STAT_CONFIG_TERM__##__term: \
1096 config->__val = event->data[i].val; \
1099 CASE(AGGR_MODE, aggr_mode)
1101 CASE(INTERVAL, interval)
1104 pr_warning("unknown stat config term %" PRIu64 "\n",
1105 event->data[i].tag);
1110 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1114 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1119 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1122 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1125 struct perf_ns_link_info *ns_link_info;
1126 u32 nr_namespaces, idx;
1128 ns_link_info = event->namespaces.link_info;
1129 nr_namespaces = event->namespaces.nr_namespaces;
1131 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1132 event->namespaces.pid,
1133 event->namespaces.tid,
1136 for (idx = 0; idx < nr_namespaces; idx++) {
1137 if (idx && (idx % 4 == 0))
1138 ret += fprintf(fp, "\n\t\t ");
1140 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1141 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1142 (u64)ns_link_info[idx].ino,
1143 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1149 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1150 union perf_event *event,
1151 struct perf_sample *sample,
1152 struct machine *machine)
1154 return machine__process_comm_event(machine, event, sample);
1157 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1158 union perf_event *event,
1159 struct perf_sample *sample,
1160 struct machine *machine)
1162 return machine__process_namespaces_event(machine, event, sample);
1165 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1166 union perf_event *event,
1167 struct perf_sample *sample,
1168 struct machine *machine)
1170 return machine__process_lost_event(machine, event, sample);
1173 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1174 union perf_event *event,
1175 struct perf_sample *sample __maybe_unused,
1176 struct machine *machine)
1178 return machine__process_aux_event(machine, event);
1181 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1182 union perf_event *event,
1183 struct perf_sample *sample __maybe_unused,
1184 struct machine *machine)
1186 return machine__process_itrace_start_event(machine, event);
1189 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1190 union perf_event *event,
1191 struct perf_sample *sample,
1192 struct machine *machine)
1194 return machine__process_lost_samples_event(machine, event, sample);
1197 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1198 union perf_event *event,
1199 struct perf_sample *sample __maybe_unused,
1200 struct machine *machine)
1202 return machine__process_switch_event(machine, event);
1205 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1207 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1208 event->mmap.pid, event->mmap.tid, event->mmap.start,
1209 event->mmap.len, event->mmap.pgoff,
1210 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1211 event->mmap.filename);
1214 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1216 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1217 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1218 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1219 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1220 event->mmap2.min, event->mmap2.ino,
1221 event->mmap2.ino_generation,
1222 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1223 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1224 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1225 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1226 event->mmap2.filename);
1229 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1231 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1234 ret = fprintf(fp, " nr: ");
1237 ret += thread_map__fprintf(threads, fp);
1239 ret += fprintf(fp, "failed to get threads from event\n");
1241 thread_map__put(threads);
1245 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1247 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1250 ret = fprintf(fp, ": ");
1253 ret += cpu_map__fprintf(cpus, fp);
1255 ret += fprintf(fp, "failed to get cpumap from event\n");
1261 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1262 union perf_event *event,
1263 struct perf_sample *sample,
1264 struct machine *machine)
1266 return machine__process_mmap_event(machine, event, sample);
1269 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1270 union perf_event *event,
1271 struct perf_sample *sample,
1272 struct machine *machine)
1274 return machine__process_mmap2_event(machine, event, sample);
1277 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1279 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1280 event->fork.pid, event->fork.tid,
1281 event->fork.ppid, event->fork.ptid);
1284 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1285 union perf_event *event,
1286 struct perf_sample *sample,
1287 struct machine *machine)
1289 return machine__process_fork_event(machine, event, sample);
1292 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1293 union perf_event *event,
1294 struct perf_sample *sample,
1295 struct machine *machine)
1297 return machine__process_exit_event(machine, event, sample);
1300 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1302 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n",
1303 event->aux.aux_offset, event->aux.aux_size,
1305 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1306 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1307 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
1310 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1312 return fprintf(fp, " pid: %u tid: %u\n",
1313 event->itrace_start.pid, event->itrace_start.tid);
1316 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1318 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1319 const char *in_out = out ? "OUT" : "IN ";
1321 if (event->header.type == PERF_RECORD_SWITCH)
1322 return fprintf(fp, " %s\n", in_out);
1324 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1325 in_out, out ? "next" : "prev",
1326 event->context_switch.next_prev_pid,
1327 event->context_switch.next_prev_tid);
1330 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1332 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1333 perf_event__name(event->header.type));
1335 switch (event->header.type) {
1336 case PERF_RECORD_COMM:
1337 ret += perf_event__fprintf_comm(event, fp);
1339 case PERF_RECORD_FORK:
1340 case PERF_RECORD_EXIT:
1341 ret += perf_event__fprintf_task(event, fp);
1343 case PERF_RECORD_MMAP:
1344 ret += perf_event__fprintf_mmap(event, fp);
1346 case PERF_RECORD_NAMESPACES:
1347 ret += perf_event__fprintf_namespaces(event, fp);
1349 case PERF_RECORD_MMAP2:
1350 ret += perf_event__fprintf_mmap2(event, fp);
1352 case PERF_RECORD_AUX:
1353 ret += perf_event__fprintf_aux(event, fp);
1355 case PERF_RECORD_ITRACE_START:
1356 ret += perf_event__fprintf_itrace_start(event, fp);
1358 case PERF_RECORD_SWITCH:
1359 case PERF_RECORD_SWITCH_CPU_WIDE:
1360 ret += perf_event__fprintf_switch(event, fp);
1363 ret += fprintf(fp, "\n");
1369 int perf_event__process(struct perf_tool *tool __maybe_unused,
1370 union perf_event *event,
1371 struct perf_sample *sample,
1372 struct machine *machine)
1374 return machine__process_event(machine, event, sample);
1377 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1378 enum map_type type, u64 addr,
1379 struct addr_location *al)
1381 struct map_groups *mg = thread->mg;
1382 struct machine *machine = mg->machine;
1383 bool load_map = false;
1385 al->machine = machine;
1386 al->thread = thread;
1388 al->cpumode = cpumode;
1391 if (machine == NULL) {
1396 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1398 mg = &machine->kmaps;
1400 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1402 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1404 mg = &machine->kmaps;
1406 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1412 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1413 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1415 al->filtered |= (1 << HIST_FILTER__GUEST);
1416 if ((cpumode == PERF_RECORD_MISC_USER ||
1417 cpumode == PERF_RECORD_MISC_KERNEL) &&
1419 al->filtered |= (1 << HIST_FILTER__HOST);
1424 al->map = map_groups__find(mg, type, al->addr);
1425 if (al->map == NULL) {
1427 * If this is outside of all known maps, and is a negative
1428 * address, try to look it up in the kernel dso, as it might be
1429 * a vsyscall or vdso (which executes in user-mode).
1431 * XXX This is nasty, we should have a symbol list in the
1432 * "[vdso]" dso, but for now lets use the old trick of looking
1433 * in the whole kernel symbol list.
1435 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1436 mg != &machine->kmaps &&
1437 machine__kernel_ip(machine, al->addr)) {
1438 mg = &machine->kmaps;
1444 * Kernel maps might be changed when loading symbols so loading
1445 * must be done prior to using kernel maps.
1449 al->addr = al->map->map_ip(al->map, al->addr);
1453 void thread__find_addr_location(struct thread *thread,
1454 u8 cpumode, enum map_type type, u64 addr,
1455 struct addr_location *al)
1457 thread__find_addr_map(thread, cpumode, type, addr, al);
1458 if (al->map != NULL)
1459 al->sym = map__find_symbol(al->map, al->addr);
1465 * Callers need to drop the reference to al->thread, obtained in
1466 * machine__findnew_thread()
1468 int machine__resolve(struct machine *machine, struct addr_location *al,
1469 struct perf_sample *sample)
1471 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1477 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1479 * Have we already created the kernel maps for this machine?
1481 * This should have happened earlier, when we processed the kernel MMAP
1482 * events, but for older perf.data files there was no such thing, so do
1485 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1486 machine__kernel_map(machine) == NULL)
1487 machine__create_kernel_maps(machine);
1489 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1490 dump_printf(" ...... dso: %s\n",
1491 al->map ? al->map->dso->long_name :
1492 al->level == 'H' ? "[hypervisor]" : "<not found>");
1494 if (thread__is_filtered(thread))
1495 al->filtered |= (1 << HIST_FILTER__THREAD);
1498 al->cpu = sample->cpu;
1502 struct perf_env *env = machine->env;
1504 if (env && env->cpu)
1505 al->socket = env->cpu[al->cpu].socket_id;
1509 struct dso *dso = al->map->dso;
1511 if (symbol_conf.dso_list &&
1512 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1514 (dso->short_name != dso->long_name &&
1515 strlist__has_entry(symbol_conf.dso_list,
1516 dso->long_name))))) {
1517 al->filtered |= (1 << HIST_FILTER__DSO);
1520 al->sym = map__find_symbol(al->map, al->addr);
1523 if (symbol_conf.sym_list &&
1524 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1526 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1533 * The preprocess_sample method will return with reference counts for the
1534 * in it, when done using (and perhaps getting ref counts if needing to
1535 * keep a pointer to one of those entries) it must be paired with
1536 * addr_location__put(), so that the refcounts can be decremented.
1538 void addr_location__put(struct addr_location *al)
1540 thread__zput(al->thread);
1543 bool is_bts_event(struct perf_event_attr *attr)
1545 return attr->type == PERF_TYPE_HARDWARE &&
1546 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1547 attr->sample_period == 1;
1550 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1552 if (attr->type == PERF_TYPE_SOFTWARE &&
1553 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1554 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1555 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1558 if (is_bts_event(attr))
1564 void thread__resolve(struct thread *thread, struct addr_location *al,
1565 struct perf_sample *sample)
1567 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1569 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1572 al->cpu = sample->cpu;
1576 al->sym = map__find_symbol(al->map, al->addr);