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",
62 static const char *perf_ns__names[] = {
63 [NET_NS_INDEX] = "net",
64 [UTS_NS_INDEX] = "uts",
65 [IPC_NS_INDEX] = "ipc",
66 [PID_NS_INDEX] = "pid",
67 [USER_NS_INDEX] = "user",
68 [MNT_NS_INDEX] = "mnt",
69 [CGROUP_NS_INDEX] = "cgroup",
72 const char *perf_event__name(unsigned int id)
74 if (id >= ARRAY_SIZE(perf_event__names))
76 if (!perf_event__names[id])
78 return perf_event__names[id];
81 static const char *perf_ns__name(unsigned int id)
83 if (id >= ARRAY_SIZE(perf_ns__names))
85 return perf_ns__names[id];
88 static int perf_tool__process_synth_event(struct perf_tool *tool,
89 union perf_event *event,
90 struct machine *machine,
91 perf_event__handler_t process)
93 struct perf_sample synth_sample = {
100 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
103 return process(tool, event, &synth_sample, machine);
107 * Assumes that the first 4095 bytes of /proc/pid/stat contains
108 * the comm, tgid and ppid.
110 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
111 pid_t *tgid, pid_t *ppid)
113 char filename[PATH_MAX];
118 char *name, *tgids, *ppids;
123 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
125 fd = open(filename, O_RDONLY);
127 pr_debug("couldn't open %s\n", filename);
131 n = read(fd, bf, sizeof(bf) - 1);
134 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
140 name = strstr(bf, "Name:");
141 tgids = strstr(bf, "Tgid:");
142 ppids = strstr(bf, "PPid:");
147 name += 5; /* strlen("Name:") */
150 nl = strchr(name, '\n');
157 memcpy(comm, name, size);
160 pr_debug("Name: string not found for pid %d\n", pid);
164 tgids += 5; /* strlen("Tgid:") */
167 pr_debug("Tgid: string not found for pid %d\n", pid);
171 ppids += 5; /* strlen("PPid:") */
174 pr_debug("PPid: string not found for pid %d\n", pid);
180 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
181 struct machine *machine,
182 pid_t *tgid, pid_t *ppid)
188 memset(&event->comm, 0, sizeof(event->comm));
190 if (machine__is_host(machine)) {
191 if (perf_event__get_comm_ids(pid, event->comm.comm,
192 sizeof(event->comm.comm),
197 *tgid = machine->pid;
203 event->comm.pid = *tgid;
204 event->comm.header.type = PERF_RECORD_COMM;
206 size = strlen(event->comm.comm) + 1;
207 size = PERF_ALIGN(size, sizeof(u64));
208 memset(event->comm.comm + size, 0, machine->id_hdr_size);
209 event->comm.header.size = (sizeof(event->comm) -
210 (sizeof(event->comm.comm) - size) +
211 machine->id_hdr_size);
212 event->comm.tid = pid;
217 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
218 union perf_event *event, pid_t pid,
219 perf_event__handler_t process,
220 struct machine *machine)
224 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
227 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
233 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
234 struct perf_ns_link_info *ns_link_info)
239 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
240 if (stat64(proc_ns, &st) == 0) {
241 ns_link_info->dev = st.st_dev;
242 ns_link_info->ino = st.st_ino;
246 int perf_event__synthesize_namespaces(struct perf_tool *tool,
247 union perf_event *event,
248 pid_t pid, pid_t tgid,
249 perf_event__handler_t process,
250 struct machine *machine)
253 struct perf_ns_link_info *ns_link_info;
255 if (!tool || !tool->namespace_events)
258 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
259 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
260 machine->id_hdr_size));
262 event->namespaces.pid = tgid;
263 event->namespaces.tid = pid;
265 event->namespaces.nr_namespaces = NR_NAMESPACES;
267 ns_link_info = event->namespaces.link_info;
269 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
270 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
273 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
275 event->namespaces.header.size = (sizeof(event->namespaces) +
276 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
277 machine->id_hdr_size);
279 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
285 static int perf_event__synthesize_fork(struct perf_tool *tool,
286 union perf_event *event,
287 pid_t pid, pid_t tgid, pid_t ppid,
288 perf_event__handler_t process,
289 struct machine *machine)
291 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
294 * for main thread set parent to ppid from status file. For other
295 * threads set parent pid to main thread. ie., assume main thread
296 * spawns all threads in a process
299 event->fork.ppid = ppid;
300 event->fork.ptid = ppid;
302 event->fork.ppid = tgid;
303 event->fork.ptid = tgid;
305 event->fork.pid = tgid;
306 event->fork.tid = pid;
307 event->fork.header.type = PERF_RECORD_FORK;
309 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
311 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
317 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
318 union perf_event *event,
319 pid_t pid, pid_t tgid,
320 perf_event__handler_t process,
321 struct machine *machine,
323 unsigned int proc_map_timeout)
325 char filename[PATH_MAX];
327 unsigned long long t;
328 bool truncation = false;
329 unsigned long long timeout = proc_map_timeout * 1000000ULL;
331 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
332 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
334 if (machine__is_default_guest(machine))
337 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
338 machine->root_dir, pid, pid);
340 fp = fopen(filename, "r");
343 * We raced with a task exiting - just return:
345 pr_debug("couldn't open %s\n", filename);
349 event->header.type = PERF_RECORD_MMAP2;
355 char execname[PATH_MAX];
356 char anonstr[] = "//anon";
361 if (fgets(bf, sizeof(bf), fp) == NULL)
364 if ((rdclock() - t) > timeout) {
365 pr_warning("Reading %s time out. "
366 "You may want to increase "
367 "the time limit by --proc-map-timeout\n",
373 /* ensure null termination since stack will be reused. */
374 strcpy(execname, "");
376 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
377 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
378 &event->mmap2.start, &event->mmap2.len, prot,
379 &event->mmap2.pgoff, &event->mmap2.maj,
384 * Anon maps don't have the execname.
389 event->mmap2.ino = (u64)ino;
392 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
394 if (machine__is_host(machine))
395 event->header.misc = PERF_RECORD_MISC_USER;
397 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
399 /* map protection and flags bits */
400 event->mmap2.prot = 0;
401 event->mmap2.flags = 0;
403 event->mmap2.prot |= PROT_READ;
405 event->mmap2.prot |= PROT_WRITE;
407 event->mmap2.prot |= PROT_EXEC;
410 event->mmap2.flags |= MAP_SHARED;
412 event->mmap2.flags |= MAP_PRIVATE;
414 if (prot[2] != 'x') {
415 if (!mmap_data || prot[0] != 'r')
418 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
423 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
425 if (!strcmp(execname, ""))
426 strcpy(execname, anonstr);
428 if (hugetlbfs_mnt_len &&
429 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
430 strcpy(execname, anonstr);
431 event->mmap2.flags |= MAP_HUGETLB;
434 size = strlen(execname) + 1;
435 memcpy(event->mmap2.filename, execname, size);
436 size = PERF_ALIGN(size, sizeof(u64));
437 event->mmap2.len -= event->mmap.start;
438 event->mmap2.header.size = (sizeof(event->mmap2) -
439 (sizeof(event->mmap2.filename) - size));
440 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
441 event->mmap2.header.size += machine->id_hdr_size;
442 event->mmap2.pid = tgid;
443 event->mmap2.tid = pid;
445 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
458 int perf_event__synthesize_modules(struct perf_tool *tool,
459 perf_event__handler_t process,
460 struct machine *machine)
464 struct map_groups *kmaps = &machine->kmaps;
465 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
466 union perf_event *event = zalloc((sizeof(event->mmap) +
467 machine->id_hdr_size));
469 pr_debug("Not enough memory synthesizing mmap event "
470 "for kernel modules\n");
474 event->header.type = PERF_RECORD_MMAP;
477 * kernel uses 0 for user space maps, see kernel/perf_event.c
480 if (machine__is_host(machine))
481 event->header.misc = PERF_RECORD_MISC_KERNEL;
483 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
485 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
488 if (__map__is_kernel(pos))
491 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
492 event->mmap.header.type = PERF_RECORD_MMAP;
493 event->mmap.header.size = (sizeof(event->mmap) -
494 (sizeof(event->mmap.filename) - size));
495 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
496 event->mmap.header.size += machine->id_hdr_size;
497 event->mmap.start = pos->start;
498 event->mmap.len = pos->end - pos->start;
499 event->mmap.pid = machine->pid;
501 memcpy(event->mmap.filename, pos->dso->long_name,
502 pos->dso->long_name_len + 1);
503 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
513 static int __event__synthesize_thread(union perf_event *comm_event,
514 union perf_event *mmap_event,
515 union perf_event *fork_event,
516 union perf_event *namespaces_event,
518 perf_event__handler_t process,
519 struct perf_tool *tool,
520 struct machine *machine,
522 unsigned int proc_map_timeout)
524 char filename[PATH_MAX];
526 struct dirent *dirent;
530 /* special case: only send one comm event using passed in pid */
532 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
538 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
539 tgid, process, machine) < 0)
543 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
544 process, machine, mmap_data,
548 if (machine__is_default_guest(machine))
551 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
552 machine->root_dir, pid);
554 tasks = opendir(filename);
556 pr_debug("couldn't open %s\n", filename);
560 while ((dirent = readdir(tasks)) != NULL) {
564 _pid = strtol(dirent->d_name, &end, 10);
569 if (perf_event__prepare_comm(comm_event, _pid, machine,
573 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
574 ppid, process, machine) < 0)
577 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
578 tgid, process, machine) < 0)
582 * Send the prepared comm event
584 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
589 /* process the parent's maps too */
590 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
591 process, machine, mmap_data, proc_map_timeout);
601 int perf_event__synthesize_thread_map(struct perf_tool *tool,
602 struct thread_map *threads,
603 perf_event__handler_t process,
604 struct machine *machine,
606 unsigned int proc_map_timeout)
608 union perf_event *comm_event, *mmap_event, *fork_event;
609 union perf_event *namespaces_event;
610 int err = -1, thread, j;
612 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
613 if (comm_event == NULL)
616 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
617 if (mmap_event == NULL)
620 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
621 if (fork_event == NULL)
624 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
625 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
626 machine->id_hdr_size);
627 if (namespaces_event == NULL)
631 for (thread = 0; thread < threads->nr; ++thread) {
632 if (__event__synthesize_thread(comm_event, mmap_event,
633 fork_event, namespaces_event,
634 thread_map__pid(threads, thread), 0,
635 process, tool, machine,
636 mmap_data, proc_map_timeout)) {
642 * comm.pid is set to thread group id by
643 * perf_event__synthesize_comm
645 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
646 bool need_leader = true;
648 /* is thread group leader in thread_map? */
649 for (j = 0; j < threads->nr; ++j) {
650 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
656 /* if not, generate events for it */
658 __event__synthesize_thread(comm_event, mmap_event,
659 fork_event, namespaces_event,
660 comm_event->comm.pid, 0,
661 process, tool, machine,
662 mmap_data, proc_map_timeout)) {
668 free(namespaces_event);
679 int perf_event__synthesize_threads(struct perf_tool *tool,
680 perf_event__handler_t process,
681 struct machine *machine,
683 unsigned int proc_map_timeout)
686 char proc_path[PATH_MAX];
687 struct dirent *dirent;
688 union perf_event *comm_event, *mmap_event, *fork_event;
689 union perf_event *namespaces_event;
692 if (machine__is_default_guest(machine))
695 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
696 if (comm_event == NULL)
699 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
700 if (mmap_event == NULL)
703 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
704 if (fork_event == NULL)
707 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
708 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
709 machine->id_hdr_size);
710 if (namespaces_event == NULL)
713 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
714 proc = opendir(proc_path);
717 goto out_free_namespaces;
719 while ((dirent = readdir(proc)) != NULL) {
721 pid_t pid = strtol(dirent->d_name, &end, 10);
723 if (*end) /* only interested in proper numerical dirents */
726 * We may race with exiting thread, so don't stop just because
727 * one thread couldn't be synthesized.
729 __event__synthesize_thread(comm_event, mmap_event, fork_event,
730 namespaces_event, pid, 1, process,
731 tool, machine, mmap_data,
738 free(namespaces_event);
749 struct process_symbol_args {
754 static int find_symbol_cb(void *arg, const char *name, char type,
757 struct process_symbol_args *args = arg;
760 * Must be a function or at least an alias, as in PARISC64, where "_text" is
761 * an 'A' to the same address as "_stext".
763 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
764 type == 'A') || strcmp(name, args->name))
771 u64 kallsyms__get_function_start(const char *kallsyms_filename,
772 const char *symbol_name)
774 struct process_symbol_args args = { .name = symbol_name, };
776 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
782 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
783 perf_event__handler_t process,
784 struct machine *machine)
787 const char *mmap_name;
788 char name_buff[PATH_MAX];
789 struct map *map = machine__kernel_map(machine);
792 union perf_event *event;
794 if (symbol_conf.kptr_restrict)
800 * We should get this from /sys/kernel/sections/.text, but till that is
801 * available use this, and after it is use this as a fallback for older
804 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
806 pr_debug("Not enough memory synthesizing mmap event "
807 "for kernel modules\n");
811 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
812 if (machine__is_host(machine)) {
814 * kernel uses PERF_RECORD_MISC_USER for user space maps,
815 * see kernel/perf_event.c __perf_event_mmap
817 event->header.misc = PERF_RECORD_MISC_KERNEL;
819 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
822 kmap = map__kmap(map);
823 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
824 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
825 size = PERF_ALIGN(size, sizeof(u64));
826 event->mmap.header.type = PERF_RECORD_MMAP;
827 event->mmap.header.size = (sizeof(event->mmap) -
828 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
829 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
830 event->mmap.start = map->start;
831 event->mmap.len = map->end - event->mmap.start;
832 event->mmap.pid = machine->pid;
834 err = perf_tool__process_synth_event(tool, event, machine, process);
840 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
841 struct thread_map *threads,
842 perf_event__handler_t process,
843 struct machine *machine)
845 union perf_event *event;
848 size = sizeof(event->thread_map);
849 size += threads->nr * sizeof(event->thread_map.entries[0]);
851 event = zalloc(size);
855 event->header.type = PERF_RECORD_THREAD_MAP;
856 event->header.size = size;
857 event->thread_map.nr = threads->nr;
859 for (i = 0; i < threads->nr; i++) {
860 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
861 char *comm = thread_map__comm(threads, i);
866 entry->pid = thread_map__pid(threads, i);
867 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
870 err = process(tool, event, NULL, machine);
876 static void synthesize_cpus(struct cpu_map_entries *cpus,
883 for (i = 0; i < map->nr; i++)
884 cpus->cpu[i] = map->map[i];
887 static void synthesize_mask(struct cpu_map_mask *mask,
888 struct cpu_map *map, int max)
892 mask->nr = BITS_TO_LONGS(max);
893 mask->long_size = sizeof(long);
895 for (i = 0; i < map->nr; i++)
896 set_bit(map->map[i], mask->mask);
899 static size_t cpus_size(struct cpu_map *map)
901 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
904 static size_t mask_size(struct cpu_map *map, int *max)
910 for (i = 0; i < map->nr; i++) {
911 /* bit possition of the cpu is + 1 */
912 int bit = map->map[i] + 1;
918 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
921 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
923 size_t size_cpus, size_mask;
924 bool is_dummy = cpu_map__empty(map);
927 * Both array and mask data have variable size based
928 * on the number of cpus and their actual values.
929 * The size of the 'struct cpu_map_data' is:
931 * array = size of 'struct cpu_map_entries' +
932 * number of cpus * sizeof(u64)
934 * mask = size of 'struct cpu_map_mask' +
935 * maximum cpu bit converted to size of longs
937 * and finaly + the size of 'struct cpu_map_data'.
939 size_cpus = cpus_size(map);
940 size_mask = mask_size(map, max);
942 if (is_dummy || (size_cpus < size_mask)) {
944 *type = PERF_CPU_MAP__CPUS;
947 *type = PERF_CPU_MAP__MASK;
950 *size += sizeof(struct cpu_map_data);
951 return zalloc(*size);
954 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
960 case PERF_CPU_MAP__CPUS:
961 synthesize_cpus((struct cpu_map_entries *) data->data, map);
963 case PERF_CPU_MAP__MASK:
964 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
970 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
972 size_t size = sizeof(struct cpu_map_event);
973 struct cpu_map_event *event;
977 event = cpu_map_data__alloc(map, &size, &type, &max);
981 event->header.type = PERF_RECORD_CPU_MAP;
982 event->header.size = size;
983 event->data.type = type;
985 cpu_map_data__synthesize(&event->data, map, type, max);
989 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
991 perf_event__handler_t process,
992 struct machine *machine)
994 struct cpu_map_event *event;
997 event = cpu_map_event__new(map);
1001 err = process(tool, (union perf_event *) event, NULL, machine);
1007 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1008 struct perf_stat_config *config,
1009 perf_event__handler_t process,
1010 struct machine *machine)
1012 struct stat_config_event *event;
1013 int size, i = 0, err;
1015 size = sizeof(*event);
1016 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1018 event = zalloc(size);
1022 event->header.type = PERF_RECORD_STAT_CONFIG;
1023 event->header.size = size;
1024 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1026 #define ADD(__term, __val) \
1027 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1028 event->data[i].val = __val; \
1031 ADD(AGGR_MODE, config->aggr_mode)
1032 ADD(INTERVAL, config->interval)
1033 ADD(SCALE, config->scale)
1035 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1036 "stat config terms unbalanced\n");
1039 err = process(tool, (union perf_event *) event, NULL, machine);
1045 int perf_event__synthesize_stat(struct perf_tool *tool,
1046 u32 cpu, u32 thread, u64 id,
1047 struct perf_counts_values *count,
1048 perf_event__handler_t process,
1049 struct machine *machine)
1051 struct stat_event event;
1053 event.header.type = PERF_RECORD_STAT;
1054 event.header.size = sizeof(event);
1055 event.header.misc = 0;
1059 event.thread = thread;
1060 event.val = count->val;
1061 event.ena = count->ena;
1062 event.run = count->run;
1064 return process(tool, (union perf_event *) &event, NULL, machine);
1067 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1068 u64 evtime, u64 type,
1069 perf_event__handler_t process,
1070 struct machine *machine)
1072 struct stat_round_event event;
1074 event.header.type = PERF_RECORD_STAT_ROUND;
1075 event.header.size = sizeof(event);
1076 event.header.misc = 0;
1078 event.time = evtime;
1081 return process(tool, (union perf_event *) &event, NULL, machine);
1084 void perf_event__read_stat_config(struct perf_stat_config *config,
1085 struct stat_config_event *event)
1089 for (i = 0; i < event->nr; i++) {
1091 switch (event->data[i].tag) {
1092 #define CASE(__term, __val) \
1093 case PERF_STAT_CONFIG_TERM__##__term: \
1094 config->__val = event->data[i].val; \
1097 CASE(AGGR_MODE, aggr_mode)
1099 CASE(INTERVAL, interval)
1102 pr_warning("unknown stat config term %" PRIu64 "\n",
1103 event->data[i].tag);
1108 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1112 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1117 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1120 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1123 struct perf_ns_link_info *ns_link_info;
1124 u32 nr_namespaces, idx;
1126 ns_link_info = event->namespaces.link_info;
1127 nr_namespaces = event->namespaces.nr_namespaces;
1129 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1130 event->namespaces.pid,
1131 event->namespaces.tid,
1134 for (idx = 0; idx < nr_namespaces; idx++) {
1135 if (idx && (idx % 4 == 0))
1136 ret += fprintf(fp, "\n\t\t ");
1138 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1139 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1140 (u64)ns_link_info[idx].ino,
1141 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1147 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1148 union perf_event *event,
1149 struct perf_sample *sample,
1150 struct machine *machine)
1152 return machine__process_comm_event(machine, event, sample);
1155 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1156 union perf_event *event,
1157 struct perf_sample *sample,
1158 struct machine *machine)
1160 return machine__process_namespaces_event(machine, event, sample);
1163 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1164 union perf_event *event,
1165 struct perf_sample *sample,
1166 struct machine *machine)
1168 return machine__process_lost_event(machine, event, sample);
1171 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1172 union perf_event *event,
1173 struct perf_sample *sample __maybe_unused,
1174 struct machine *machine)
1176 return machine__process_aux_event(machine, event);
1179 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1180 union perf_event *event,
1181 struct perf_sample *sample __maybe_unused,
1182 struct machine *machine)
1184 return machine__process_itrace_start_event(machine, event);
1187 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1188 union perf_event *event,
1189 struct perf_sample *sample,
1190 struct machine *machine)
1192 return machine__process_lost_samples_event(machine, event, sample);
1195 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1196 union perf_event *event,
1197 struct perf_sample *sample __maybe_unused,
1198 struct machine *machine)
1200 return machine__process_switch_event(machine, event);
1203 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1205 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1206 event->mmap.pid, event->mmap.tid, event->mmap.start,
1207 event->mmap.len, event->mmap.pgoff,
1208 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1209 event->mmap.filename);
1212 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1214 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1215 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1216 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1217 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1218 event->mmap2.min, event->mmap2.ino,
1219 event->mmap2.ino_generation,
1220 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1221 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1222 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1223 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1224 event->mmap2.filename);
1227 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1229 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1232 ret = fprintf(fp, " nr: ");
1235 ret += thread_map__fprintf(threads, fp);
1237 ret += fprintf(fp, "failed to get threads from event\n");
1239 thread_map__put(threads);
1243 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1245 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1248 ret = fprintf(fp, ": ");
1251 ret += cpu_map__fprintf(cpus, fp);
1253 ret += fprintf(fp, "failed to get cpumap from event\n");
1259 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1260 union perf_event *event,
1261 struct perf_sample *sample,
1262 struct machine *machine)
1264 return machine__process_mmap_event(machine, event, sample);
1267 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1268 union perf_event *event,
1269 struct perf_sample *sample,
1270 struct machine *machine)
1272 return machine__process_mmap2_event(machine, event, sample);
1275 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1277 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1278 event->fork.pid, event->fork.tid,
1279 event->fork.ppid, event->fork.ptid);
1282 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1283 union perf_event *event,
1284 struct perf_sample *sample,
1285 struct machine *machine)
1287 return machine__process_fork_event(machine, event, sample);
1290 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1291 union perf_event *event,
1292 struct perf_sample *sample,
1293 struct machine *machine)
1295 return machine__process_exit_event(machine, event, sample);
1298 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1300 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n",
1301 event->aux.aux_offset, event->aux.aux_size,
1303 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1304 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1305 event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : "");
1308 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1310 return fprintf(fp, " pid: %u tid: %u\n",
1311 event->itrace_start.pid, event->itrace_start.tid);
1314 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1316 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1317 const char *in_out = out ? "OUT" : "IN ";
1319 if (event->header.type == PERF_RECORD_SWITCH)
1320 return fprintf(fp, " %s\n", in_out);
1322 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1323 in_out, out ? "next" : "prev",
1324 event->context_switch.next_prev_pid,
1325 event->context_switch.next_prev_tid);
1328 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1330 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1331 perf_event__name(event->header.type));
1333 switch (event->header.type) {
1334 case PERF_RECORD_COMM:
1335 ret += perf_event__fprintf_comm(event, fp);
1337 case PERF_RECORD_FORK:
1338 case PERF_RECORD_EXIT:
1339 ret += perf_event__fprintf_task(event, fp);
1341 case PERF_RECORD_MMAP:
1342 ret += perf_event__fprintf_mmap(event, fp);
1344 case PERF_RECORD_NAMESPACES:
1345 ret += perf_event__fprintf_namespaces(event, fp);
1347 case PERF_RECORD_MMAP2:
1348 ret += perf_event__fprintf_mmap2(event, fp);
1350 case PERF_RECORD_AUX:
1351 ret += perf_event__fprintf_aux(event, fp);
1353 case PERF_RECORD_ITRACE_START:
1354 ret += perf_event__fprintf_itrace_start(event, fp);
1356 case PERF_RECORD_SWITCH:
1357 case PERF_RECORD_SWITCH_CPU_WIDE:
1358 ret += perf_event__fprintf_switch(event, fp);
1361 ret += fprintf(fp, "\n");
1367 int perf_event__process(struct perf_tool *tool __maybe_unused,
1368 union perf_event *event,
1369 struct perf_sample *sample,
1370 struct machine *machine)
1372 return machine__process_event(machine, event, sample);
1375 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1376 enum map_type type, u64 addr,
1377 struct addr_location *al)
1379 struct map_groups *mg = thread->mg;
1380 struct machine *machine = mg->machine;
1381 bool load_map = false;
1383 al->machine = machine;
1384 al->thread = thread;
1386 al->cpumode = cpumode;
1389 if (machine == NULL) {
1394 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1396 mg = &machine->kmaps;
1398 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1400 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1402 mg = &machine->kmaps;
1404 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1410 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1411 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1413 al->filtered |= (1 << HIST_FILTER__GUEST);
1414 if ((cpumode == PERF_RECORD_MISC_USER ||
1415 cpumode == PERF_RECORD_MISC_KERNEL) &&
1417 al->filtered |= (1 << HIST_FILTER__HOST);
1422 al->map = map_groups__find(mg, type, al->addr);
1423 if (al->map == NULL) {
1425 * If this is outside of all known maps, and is a negative
1426 * address, try to look it up in the kernel dso, as it might be
1427 * a vsyscall or vdso (which executes in user-mode).
1429 * XXX This is nasty, we should have a symbol list in the
1430 * "[vdso]" dso, but for now lets use the old trick of looking
1431 * in the whole kernel symbol list.
1433 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1434 mg != &machine->kmaps &&
1435 machine__kernel_ip(machine, al->addr)) {
1436 mg = &machine->kmaps;
1442 * Kernel maps might be changed when loading symbols so loading
1443 * must be done prior to using kernel maps.
1447 al->addr = al->map->map_ip(al->map, al->addr);
1451 void thread__find_addr_location(struct thread *thread,
1452 u8 cpumode, enum map_type type, u64 addr,
1453 struct addr_location *al)
1455 thread__find_addr_map(thread, cpumode, type, addr, al);
1456 if (al->map != NULL)
1457 al->sym = map__find_symbol(al->map, al->addr);
1463 * Callers need to drop the reference to al->thread, obtained in
1464 * machine__findnew_thread()
1466 int machine__resolve(struct machine *machine, struct addr_location *al,
1467 struct perf_sample *sample)
1469 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1475 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1477 * Have we already created the kernel maps for this machine?
1479 * This should have happened earlier, when we processed the kernel MMAP
1480 * events, but for older perf.data files there was no such thing, so do
1483 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1484 machine__kernel_map(machine) == NULL)
1485 machine__create_kernel_maps(machine);
1487 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1488 dump_printf(" ...... dso: %s\n",
1489 al->map ? al->map->dso->long_name :
1490 al->level == 'H' ? "[hypervisor]" : "<not found>");
1492 if (thread__is_filtered(thread))
1493 al->filtered |= (1 << HIST_FILTER__THREAD);
1496 al->cpu = sample->cpu;
1500 struct perf_env *env = machine->env;
1502 if (env && env->cpu)
1503 al->socket = env->cpu[al->cpu].socket_id;
1507 struct dso *dso = al->map->dso;
1509 if (symbol_conf.dso_list &&
1510 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1512 (dso->short_name != dso->long_name &&
1513 strlist__has_entry(symbol_conf.dso_list,
1514 dso->long_name))))) {
1515 al->filtered |= (1 << HIST_FILTER__DSO);
1518 al->sym = map__find_symbol(al->map, al->addr);
1521 if (symbol_conf.sym_list &&
1522 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1524 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1531 * The preprocess_sample method will return with reference counts for the
1532 * in it, when done using (and perhaps getting ref counts if needing to
1533 * keep a pointer to one of those entries) it must be paired with
1534 * addr_location__put(), so that the refcounts can be decremented.
1536 void addr_location__put(struct addr_location *al)
1538 thread__zput(al->thread);
1541 bool is_bts_event(struct perf_event_attr *attr)
1543 return attr->type == PERF_TYPE_HARDWARE &&
1544 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1545 attr->sample_period == 1;
1548 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1550 if (attr->type == PERF_TYPE_SOFTWARE &&
1551 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1552 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1553 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1556 if (is_bts_event(attr))
1562 void thread__resolve(struct thread *thread, struct addr_location *al,
1563 struct perf_sample *sample)
1565 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1567 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1570 al->cpu = sample->cpu;
1574 al->sym = map__find_symbol(al->map, al->addr);