1 #include <linux/types.h>
2 #include <linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
12 #include "thread_map.h"
13 #include "symbol/kallsyms.h"
17 static const char *perf_event__names[] = {
19 [PERF_RECORD_MMAP] = "MMAP",
20 [PERF_RECORD_MMAP2] = "MMAP2",
21 [PERF_RECORD_LOST] = "LOST",
22 [PERF_RECORD_COMM] = "COMM",
23 [PERF_RECORD_EXIT] = "EXIT",
24 [PERF_RECORD_THROTTLE] = "THROTTLE",
25 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
26 [PERF_RECORD_FORK] = "FORK",
27 [PERF_RECORD_READ] = "READ",
28 [PERF_RECORD_SAMPLE] = "SAMPLE",
29 [PERF_RECORD_AUX] = "AUX",
30 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
31 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
32 [PERF_RECORD_SWITCH] = "SWITCH",
33 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
34 [PERF_RECORD_NAMESPACES] = "NAMESPACES",
35 [PERF_RECORD_HEADER_ATTR] = "ATTR",
36 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
37 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
38 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
39 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
40 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
41 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
42 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
43 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
44 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
45 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
46 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
47 [PERF_RECORD_STAT] = "STAT",
48 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
49 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
50 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
53 static const char *perf_ns__names[] = {
54 [NET_NS_INDEX] = "net",
55 [UTS_NS_INDEX] = "uts",
56 [IPC_NS_INDEX] = "ipc",
57 [PID_NS_INDEX] = "pid",
58 [USER_NS_INDEX] = "user",
59 [MNT_NS_INDEX] = "mnt",
60 [CGROUP_NS_INDEX] = "cgroup",
63 const char *perf_event__name(unsigned int id)
65 if (id >= ARRAY_SIZE(perf_event__names))
67 if (!perf_event__names[id])
69 return perf_event__names[id];
72 static const char *perf_ns__name(unsigned int id)
74 if (id >= ARRAY_SIZE(perf_ns__names))
76 return perf_ns__names[id];
79 static int perf_tool__process_synth_event(struct perf_tool *tool,
80 union perf_event *event,
81 struct machine *machine,
82 perf_event__handler_t process)
84 struct perf_sample synth_sample = {
91 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
94 return process(tool, event, &synth_sample, machine);
98 * Assumes that the first 4095 bytes of /proc/pid/stat contains
99 * the comm, tgid and ppid.
101 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
102 pid_t *tgid, pid_t *ppid)
104 char filename[PATH_MAX];
109 char *nl, *name, *tgids, *ppids;
114 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
116 fd = open(filename, O_RDONLY);
118 pr_debug("couldn't open %s\n", filename);
122 n = read(fd, bf, sizeof(bf) - 1);
125 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
131 name = strstr(bf, "Name:");
132 tgids = strstr(bf, "Tgid:");
133 ppids = strstr(bf, "PPid:");
136 name += 5; /* strlen("Name:") */
138 while (*name && isspace(*name))
141 nl = strchr(name, '\n');
148 memcpy(comm, name, size);
151 pr_debug("Name: string not found for pid %d\n", pid);
155 tgids += 5; /* strlen("Tgid:") */
158 pr_debug("Tgid: string not found for pid %d\n", pid);
162 ppids += 5; /* strlen("PPid:") */
165 pr_debug("PPid: string not found for pid %d\n", pid);
171 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
172 struct machine *machine,
173 pid_t *tgid, pid_t *ppid)
179 memset(&event->comm, 0, sizeof(event->comm));
181 if (machine__is_host(machine)) {
182 if (perf_event__get_comm_ids(pid, event->comm.comm,
183 sizeof(event->comm.comm),
188 *tgid = machine->pid;
194 event->comm.pid = *tgid;
195 event->comm.header.type = PERF_RECORD_COMM;
197 size = strlen(event->comm.comm) + 1;
198 size = PERF_ALIGN(size, sizeof(u64));
199 memset(event->comm.comm + size, 0, machine->id_hdr_size);
200 event->comm.header.size = (sizeof(event->comm) -
201 (sizeof(event->comm.comm) - size) +
202 machine->id_hdr_size);
203 event->comm.tid = pid;
208 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
209 union perf_event *event, pid_t pid,
210 perf_event__handler_t process,
211 struct machine *machine)
215 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
218 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
224 static int perf_event__synthesize_fork(struct perf_tool *tool,
225 union perf_event *event,
226 pid_t pid, pid_t tgid, pid_t ppid,
227 perf_event__handler_t process,
228 struct machine *machine)
230 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
233 * for main thread set parent to ppid from status file. For other
234 * threads set parent pid to main thread. ie., assume main thread
235 * spawns all threads in a process
238 event->fork.ppid = ppid;
239 event->fork.ptid = ppid;
241 event->fork.ppid = tgid;
242 event->fork.ptid = tgid;
244 event->fork.pid = tgid;
245 event->fork.tid = pid;
246 event->fork.header.type = PERF_RECORD_FORK;
248 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
250 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
256 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
257 union perf_event *event,
258 pid_t pid, pid_t tgid,
259 perf_event__handler_t process,
260 struct machine *machine,
262 unsigned int proc_map_timeout)
264 char filename[PATH_MAX];
266 unsigned long long t;
267 bool truncation = false;
268 unsigned long long timeout = proc_map_timeout * 1000000ULL;
270 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
271 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
273 if (machine__is_default_guest(machine))
276 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
277 machine->root_dir, pid);
279 fp = fopen(filename, "r");
282 * We raced with a task exiting - just return:
284 pr_debug("couldn't open %s\n", filename);
288 event->header.type = PERF_RECORD_MMAP2;
294 char execname[PATH_MAX];
295 char anonstr[] = "//anon";
300 if (fgets(bf, sizeof(bf), fp) == NULL)
303 if ((rdclock() - t) > timeout) {
304 pr_warning("Reading %s time out. "
305 "You may want to increase "
306 "the time limit by --proc-map-timeout\n",
312 /* ensure null termination since stack will be reused. */
313 strcpy(execname, "");
315 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
316 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
317 &event->mmap2.start, &event->mmap2.len, prot,
318 &event->mmap2.pgoff, &event->mmap2.maj,
323 * Anon maps don't have the execname.
328 event->mmap2.ino = (u64)ino;
331 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
333 if (machine__is_host(machine))
334 event->header.misc = PERF_RECORD_MISC_USER;
336 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
338 /* map protection and flags bits */
339 event->mmap2.prot = 0;
340 event->mmap2.flags = 0;
342 event->mmap2.prot |= PROT_READ;
344 event->mmap2.prot |= PROT_WRITE;
346 event->mmap2.prot |= PROT_EXEC;
349 event->mmap2.flags |= MAP_SHARED;
351 event->mmap2.flags |= MAP_PRIVATE;
353 if (prot[2] != 'x') {
354 if (!mmap_data || prot[0] != 'r')
357 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
362 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
364 if (!strcmp(execname, ""))
365 strcpy(execname, anonstr);
367 if (hugetlbfs_mnt_len &&
368 !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
369 strcpy(execname, anonstr);
370 event->mmap2.flags |= MAP_HUGETLB;
373 size = strlen(execname) + 1;
374 memcpy(event->mmap2.filename, execname, size);
375 size = PERF_ALIGN(size, sizeof(u64));
376 event->mmap2.len -= event->mmap.start;
377 event->mmap2.header.size = (sizeof(event->mmap2) -
378 (sizeof(event->mmap2.filename) - size));
379 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
380 event->mmap2.header.size += machine->id_hdr_size;
381 event->mmap2.pid = tgid;
382 event->mmap2.tid = pid;
384 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
397 int perf_event__synthesize_modules(struct perf_tool *tool,
398 perf_event__handler_t process,
399 struct machine *machine)
403 struct map_groups *kmaps = &machine->kmaps;
404 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
405 union perf_event *event = zalloc((sizeof(event->mmap) +
406 machine->id_hdr_size));
408 pr_debug("Not enough memory synthesizing mmap event "
409 "for kernel modules\n");
413 event->header.type = PERF_RECORD_MMAP;
416 * kernel uses 0 for user space maps, see kernel/perf_event.c
419 if (machine__is_host(machine))
420 event->header.misc = PERF_RECORD_MISC_KERNEL;
422 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
424 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
427 if (__map__is_kernel(pos))
430 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
431 event->mmap.header.type = PERF_RECORD_MMAP;
432 event->mmap.header.size = (sizeof(event->mmap) -
433 (sizeof(event->mmap.filename) - size));
434 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
435 event->mmap.header.size += machine->id_hdr_size;
436 event->mmap.start = pos->start;
437 event->mmap.len = pos->end - pos->start;
438 event->mmap.pid = machine->pid;
440 memcpy(event->mmap.filename, pos->dso->long_name,
441 pos->dso->long_name_len + 1);
442 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
452 static int __event__synthesize_thread(union perf_event *comm_event,
453 union perf_event *mmap_event,
454 union perf_event *fork_event,
456 perf_event__handler_t process,
457 struct perf_tool *tool,
458 struct machine *machine,
460 unsigned int proc_map_timeout)
462 char filename[PATH_MAX];
464 struct dirent *dirent;
468 /* special case: only send one comm event using passed in pid */
470 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
476 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
477 process, machine, mmap_data,
481 if (machine__is_default_guest(machine))
484 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
485 machine->root_dir, pid);
487 tasks = opendir(filename);
489 pr_debug("couldn't open %s\n", filename);
493 while ((dirent = readdir(tasks)) != NULL) {
497 _pid = strtol(dirent->d_name, &end, 10);
502 if (perf_event__prepare_comm(comm_event, _pid, machine,
506 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
507 ppid, process, machine) < 0)
510 * Send the prepared comm event
512 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
517 /* process the parent's maps too */
518 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
519 process, machine, mmap_data, proc_map_timeout);
529 int perf_event__synthesize_thread_map(struct perf_tool *tool,
530 struct thread_map *threads,
531 perf_event__handler_t process,
532 struct machine *machine,
534 unsigned int proc_map_timeout)
536 union perf_event *comm_event, *mmap_event, *fork_event;
537 int err = -1, thread, j;
539 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
540 if (comm_event == NULL)
543 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
544 if (mmap_event == NULL)
547 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
548 if (fork_event == NULL)
552 for (thread = 0; thread < threads->nr; ++thread) {
553 if (__event__synthesize_thread(comm_event, mmap_event,
555 thread_map__pid(threads, thread), 0,
556 process, tool, machine,
557 mmap_data, proc_map_timeout)) {
563 * comm.pid is set to thread group id by
564 * perf_event__synthesize_comm
566 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
567 bool need_leader = true;
569 /* is thread group leader in thread_map? */
570 for (j = 0; j < threads->nr; ++j) {
571 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
577 /* if not, generate events for it */
579 __event__synthesize_thread(comm_event, mmap_event,
581 comm_event->comm.pid, 0,
582 process, tool, machine,
583 mmap_data, proc_map_timeout)) {
598 int perf_event__synthesize_threads(struct perf_tool *tool,
599 perf_event__handler_t process,
600 struct machine *machine,
602 unsigned int proc_map_timeout)
605 char proc_path[PATH_MAX];
606 struct dirent *dirent;
607 union perf_event *comm_event, *mmap_event, *fork_event;
610 if (machine__is_default_guest(machine))
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 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
626 proc = opendir(proc_path);
631 while ((dirent = readdir(proc)) != NULL) {
633 pid_t pid = strtol(dirent->d_name, &end, 10);
635 if (*end) /* only interested in proper numerical dirents */
638 * We may race with exiting thread, so don't stop just because
639 * one thread couldn't be synthesized.
641 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
642 1, process, tool, machine, mmap_data,
658 struct process_symbol_args {
663 static int find_symbol_cb(void *arg, const char *name, char type,
666 struct process_symbol_args *args = arg;
669 * Must be a function or at least an alias, as in PARISC64, where "_text" is
670 * an 'A' to the same address as "_stext".
672 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
673 type == 'A') || strcmp(name, args->name))
680 u64 kallsyms__get_function_start(const char *kallsyms_filename,
681 const char *symbol_name)
683 struct process_symbol_args args = { .name = symbol_name, };
685 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
691 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
692 perf_event__handler_t process,
693 struct machine *machine)
696 const char *mmap_name;
697 char name_buff[PATH_MAX];
698 struct map *map = machine__kernel_map(machine);
701 union perf_event *event;
703 if (symbol_conf.kptr_restrict)
709 * We should get this from /sys/kernel/sections/.text, but till that is
710 * available use this, and after it is use this as a fallback for older
713 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
715 pr_debug("Not enough memory synthesizing mmap event "
716 "for kernel modules\n");
720 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
721 if (machine__is_host(machine)) {
723 * kernel uses PERF_RECORD_MISC_USER for user space maps,
724 * see kernel/perf_event.c __perf_event_mmap
726 event->header.misc = PERF_RECORD_MISC_KERNEL;
728 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
731 kmap = map__kmap(map);
732 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
733 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
734 size = PERF_ALIGN(size, sizeof(u64));
735 event->mmap.header.type = PERF_RECORD_MMAP;
736 event->mmap.header.size = (sizeof(event->mmap) -
737 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
738 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
739 event->mmap.start = map->start;
740 event->mmap.len = map->end - event->mmap.start;
741 event->mmap.pid = machine->pid;
743 err = perf_tool__process_synth_event(tool, event, machine, process);
749 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
750 struct thread_map *threads,
751 perf_event__handler_t process,
752 struct machine *machine)
754 union perf_event *event;
757 size = sizeof(event->thread_map);
758 size += threads->nr * sizeof(event->thread_map.entries[0]);
760 event = zalloc(size);
764 event->header.type = PERF_RECORD_THREAD_MAP;
765 event->header.size = size;
766 event->thread_map.nr = threads->nr;
768 for (i = 0; i < threads->nr; i++) {
769 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
770 char *comm = thread_map__comm(threads, i);
775 entry->pid = thread_map__pid(threads, i);
776 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
779 err = process(tool, event, NULL, machine);
785 static void synthesize_cpus(struct cpu_map_entries *cpus,
792 for (i = 0; i < map->nr; i++)
793 cpus->cpu[i] = map->map[i];
796 static void synthesize_mask(struct cpu_map_mask *mask,
797 struct cpu_map *map, int max)
801 mask->nr = BITS_TO_LONGS(max);
802 mask->long_size = sizeof(long);
804 for (i = 0; i < map->nr; i++)
805 set_bit(map->map[i], mask->mask);
808 static size_t cpus_size(struct cpu_map *map)
810 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
813 static size_t mask_size(struct cpu_map *map, int *max)
819 for (i = 0; i < map->nr; i++) {
820 /* bit possition of the cpu is + 1 */
821 int bit = map->map[i] + 1;
827 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
830 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
832 size_t size_cpus, size_mask;
833 bool is_dummy = cpu_map__empty(map);
836 * Both array and mask data have variable size based
837 * on the number of cpus and their actual values.
838 * The size of the 'struct cpu_map_data' is:
840 * array = size of 'struct cpu_map_entries' +
841 * number of cpus * sizeof(u64)
843 * mask = size of 'struct cpu_map_mask' +
844 * maximum cpu bit converted to size of longs
846 * and finaly + the size of 'struct cpu_map_data'.
848 size_cpus = cpus_size(map);
849 size_mask = mask_size(map, max);
851 if (is_dummy || (size_cpus < size_mask)) {
853 *type = PERF_CPU_MAP__CPUS;
856 *type = PERF_CPU_MAP__MASK;
859 *size += sizeof(struct cpu_map_data);
860 return zalloc(*size);
863 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
869 case PERF_CPU_MAP__CPUS:
870 synthesize_cpus((struct cpu_map_entries *) data->data, map);
872 case PERF_CPU_MAP__MASK:
873 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
879 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
881 size_t size = sizeof(struct cpu_map_event);
882 struct cpu_map_event *event;
886 event = cpu_map_data__alloc(map, &size, &type, &max);
890 event->header.type = PERF_RECORD_CPU_MAP;
891 event->header.size = size;
892 event->data.type = type;
894 cpu_map_data__synthesize(&event->data, map, type, max);
898 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
900 perf_event__handler_t process,
901 struct machine *machine)
903 struct cpu_map_event *event;
906 event = cpu_map_event__new(map);
910 err = process(tool, (union perf_event *) event, NULL, machine);
916 int perf_event__synthesize_stat_config(struct perf_tool *tool,
917 struct perf_stat_config *config,
918 perf_event__handler_t process,
919 struct machine *machine)
921 struct stat_config_event *event;
922 int size, i = 0, err;
924 size = sizeof(*event);
925 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
927 event = zalloc(size);
931 event->header.type = PERF_RECORD_STAT_CONFIG;
932 event->header.size = size;
933 event->nr = PERF_STAT_CONFIG_TERM__MAX;
935 #define ADD(__term, __val) \
936 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
937 event->data[i].val = __val; \
940 ADD(AGGR_MODE, config->aggr_mode)
941 ADD(INTERVAL, config->interval)
942 ADD(SCALE, config->scale)
944 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
945 "stat config terms unbalanced\n");
948 err = process(tool, (union perf_event *) event, NULL, machine);
954 int perf_event__synthesize_stat(struct perf_tool *tool,
955 u32 cpu, u32 thread, u64 id,
956 struct perf_counts_values *count,
957 perf_event__handler_t process,
958 struct machine *machine)
960 struct stat_event event;
962 event.header.type = PERF_RECORD_STAT;
963 event.header.size = sizeof(event);
964 event.header.misc = 0;
968 event.thread = thread;
969 event.val = count->val;
970 event.ena = count->ena;
971 event.run = count->run;
973 return process(tool, (union perf_event *) &event, NULL, machine);
976 int perf_event__synthesize_stat_round(struct perf_tool *tool,
977 u64 evtime, u64 type,
978 perf_event__handler_t process,
979 struct machine *machine)
981 struct stat_round_event event;
983 event.header.type = PERF_RECORD_STAT_ROUND;
984 event.header.size = sizeof(event);
985 event.header.misc = 0;
990 return process(tool, (union perf_event *) &event, NULL, machine);
993 void perf_event__read_stat_config(struct perf_stat_config *config,
994 struct stat_config_event *event)
998 for (i = 0; i < event->nr; i++) {
1000 switch (event->data[i].tag) {
1001 #define CASE(__term, __val) \
1002 case PERF_STAT_CONFIG_TERM__##__term: \
1003 config->__val = event->data[i].val; \
1006 CASE(AGGR_MODE, aggr_mode)
1008 CASE(INTERVAL, interval)
1011 pr_warning("unknown stat config term %" PRIu64 "\n",
1012 event->data[i].tag);
1017 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1021 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1026 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1029 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1032 struct perf_ns_link_info *ns_link_info;
1033 u32 nr_namespaces, idx;
1035 ns_link_info = event->namespaces.link_info;
1036 nr_namespaces = event->namespaces.nr_namespaces;
1038 ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1039 event->namespaces.pid,
1040 event->namespaces.tid,
1043 for (idx = 0; idx < nr_namespaces; idx++) {
1044 if (idx && (idx % 4 == 0))
1045 ret += fprintf(fp, "\n\t\t ");
1047 ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1048 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1049 (u64)ns_link_info[idx].ino,
1050 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1056 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1057 union perf_event *event,
1058 struct perf_sample *sample,
1059 struct machine *machine)
1061 return machine__process_comm_event(machine, event, sample);
1064 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1065 union perf_event *event,
1066 struct perf_sample *sample,
1067 struct machine *machine)
1069 return machine__process_namespaces_event(machine, event, sample);
1072 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1073 union perf_event *event,
1074 struct perf_sample *sample,
1075 struct machine *machine)
1077 return machine__process_lost_event(machine, event, sample);
1080 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1081 union perf_event *event,
1082 struct perf_sample *sample __maybe_unused,
1083 struct machine *machine)
1085 return machine__process_aux_event(machine, event);
1088 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1089 union perf_event *event,
1090 struct perf_sample *sample __maybe_unused,
1091 struct machine *machine)
1093 return machine__process_itrace_start_event(machine, event);
1096 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1097 union perf_event *event,
1098 struct perf_sample *sample,
1099 struct machine *machine)
1101 return machine__process_lost_samples_event(machine, event, sample);
1104 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1105 union perf_event *event,
1106 struct perf_sample *sample __maybe_unused,
1107 struct machine *machine)
1109 return machine__process_switch_event(machine, event);
1112 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1114 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1115 event->mmap.pid, event->mmap.tid, event->mmap.start,
1116 event->mmap.len, event->mmap.pgoff,
1117 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1118 event->mmap.filename);
1121 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1123 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1124 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1125 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1126 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1127 event->mmap2.min, event->mmap2.ino,
1128 event->mmap2.ino_generation,
1129 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1130 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1131 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1132 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1133 event->mmap2.filename);
1136 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1138 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1141 ret = fprintf(fp, " nr: ");
1144 ret += thread_map__fprintf(threads, fp);
1146 ret += fprintf(fp, "failed to get threads from event\n");
1148 thread_map__put(threads);
1152 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1154 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1157 ret = fprintf(fp, ": ");
1160 ret += cpu_map__fprintf(cpus, fp);
1162 ret += fprintf(fp, "failed to get cpumap from event\n");
1168 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1169 union perf_event *event,
1170 struct perf_sample *sample,
1171 struct machine *machine)
1173 return machine__process_mmap_event(machine, event, sample);
1176 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1177 union perf_event *event,
1178 struct perf_sample *sample,
1179 struct machine *machine)
1181 return machine__process_mmap2_event(machine, event, sample);
1184 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1186 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1187 event->fork.pid, event->fork.tid,
1188 event->fork.ppid, event->fork.ptid);
1191 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1192 union perf_event *event,
1193 struct perf_sample *sample,
1194 struct machine *machine)
1196 return machine__process_fork_event(machine, event, sample);
1199 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1200 union perf_event *event,
1201 struct perf_sample *sample,
1202 struct machine *machine)
1204 return machine__process_exit_event(machine, event, sample);
1207 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1209 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
1210 event->aux.aux_offset, event->aux.aux_size,
1212 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1213 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
1216 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1218 return fprintf(fp, " pid: %u tid: %u\n",
1219 event->itrace_start.pid, event->itrace_start.tid);
1222 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1224 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1225 const char *in_out = out ? "OUT" : "IN ";
1227 if (event->header.type == PERF_RECORD_SWITCH)
1228 return fprintf(fp, " %s\n", in_out);
1230 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1231 in_out, out ? "next" : "prev",
1232 event->context_switch.next_prev_pid,
1233 event->context_switch.next_prev_tid);
1236 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1238 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1239 perf_event__name(event->header.type));
1241 switch (event->header.type) {
1242 case PERF_RECORD_COMM:
1243 ret += perf_event__fprintf_comm(event, fp);
1245 case PERF_RECORD_FORK:
1246 case PERF_RECORD_EXIT:
1247 ret += perf_event__fprintf_task(event, fp);
1249 case PERF_RECORD_MMAP:
1250 ret += perf_event__fprintf_mmap(event, fp);
1252 case PERF_RECORD_NAMESPACES:
1253 ret += perf_event__fprintf_namespaces(event, fp);
1255 case PERF_RECORD_MMAP2:
1256 ret += perf_event__fprintf_mmap2(event, fp);
1258 case PERF_RECORD_AUX:
1259 ret += perf_event__fprintf_aux(event, fp);
1261 case PERF_RECORD_ITRACE_START:
1262 ret += perf_event__fprintf_itrace_start(event, fp);
1264 case PERF_RECORD_SWITCH:
1265 case PERF_RECORD_SWITCH_CPU_WIDE:
1266 ret += perf_event__fprintf_switch(event, fp);
1269 ret += fprintf(fp, "\n");
1275 int perf_event__process(struct perf_tool *tool __maybe_unused,
1276 union perf_event *event,
1277 struct perf_sample *sample,
1278 struct machine *machine)
1280 return machine__process_event(machine, event, sample);
1283 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1284 enum map_type type, u64 addr,
1285 struct addr_location *al)
1287 struct map_groups *mg = thread->mg;
1288 struct machine *machine = mg->machine;
1289 bool load_map = false;
1291 al->machine = machine;
1292 al->thread = thread;
1294 al->cpumode = cpumode;
1297 if (machine == NULL) {
1302 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1304 mg = &machine->kmaps;
1306 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1308 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1310 mg = &machine->kmaps;
1312 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1318 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1319 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1321 al->filtered |= (1 << HIST_FILTER__GUEST);
1322 if ((cpumode == PERF_RECORD_MISC_USER ||
1323 cpumode == PERF_RECORD_MISC_KERNEL) &&
1325 al->filtered |= (1 << HIST_FILTER__HOST);
1330 al->map = map_groups__find(mg, type, al->addr);
1331 if (al->map == NULL) {
1333 * If this is outside of all known maps, and is a negative
1334 * address, try to look it up in the kernel dso, as it might be
1335 * a vsyscall or vdso (which executes in user-mode).
1337 * XXX This is nasty, we should have a symbol list in the
1338 * "[vdso]" dso, but for now lets use the old trick of looking
1339 * in the whole kernel symbol list.
1341 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1342 mg != &machine->kmaps &&
1343 machine__kernel_ip(machine, al->addr)) {
1344 mg = &machine->kmaps;
1350 * Kernel maps might be changed when loading symbols so loading
1351 * must be done prior to using kernel maps.
1355 al->addr = al->map->map_ip(al->map, al->addr);
1359 void thread__find_addr_location(struct thread *thread,
1360 u8 cpumode, enum map_type type, u64 addr,
1361 struct addr_location *al)
1363 thread__find_addr_map(thread, cpumode, type, addr, al);
1364 if (al->map != NULL)
1365 al->sym = map__find_symbol(al->map, al->addr);
1371 * Callers need to drop the reference to al->thread, obtained in
1372 * machine__findnew_thread()
1374 int machine__resolve(struct machine *machine, struct addr_location *al,
1375 struct perf_sample *sample)
1377 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1383 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1385 * Have we already created the kernel maps for this machine?
1387 * This should have happened earlier, when we processed the kernel MMAP
1388 * events, but for older perf.data files there was no such thing, so do
1391 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1392 machine__kernel_map(machine) == NULL)
1393 machine__create_kernel_maps(machine);
1395 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1396 dump_printf(" ...... dso: %s\n",
1397 al->map ? al->map->dso->long_name :
1398 al->level == 'H' ? "[hypervisor]" : "<not found>");
1400 if (thread__is_filtered(thread))
1401 al->filtered |= (1 << HIST_FILTER__THREAD);
1404 al->cpu = sample->cpu;
1408 struct perf_env *env = machine->env;
1410 if (env && env->cpu)
1411 al->socket = env->cpu[al->cpu].socket_id;
1415 struct dso *dso = al->map->dso;
1417 if (symbol_conf.dso_list &&
1418 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1420 (dso->short_name != dso->long_name &&
1421 strlist__has_entry(symbol_conf.dso_list,
1422 dso->long_name))))) {
1423 al->filtered |= (1 << HIST_FILTER__DSO);
1426 al->sym = map__find_symbol(al->map, al->addr);
1429 if (symbol_conf.sym_list &&
1430 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1432 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1439 * The preprocess_sample method will return with reference counts for the
1440 * in it, when done using (and perhaps getting ref counts if needing to
1441 * keep a pointer to one of those entries) it must be paired with
1442 * addr_location__put(), so that the refcounts can be decremented.
1444 void addr_location__put(struct addr_location *al)
1446 thread__zput(al->thread);
1449 bool is_bts_event(struct perf_event_attr *attr)
1451 return attr->type == PERF_TYPE_HARDWARE &&
1452 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1453 attr->sample_period == 1;
1456 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1458 if (attr->type == PERF_TYPE_SOFTWARE &&
1459 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1460 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1461 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1464 if (is_bts_event(attr))
1470 void thread__resolve(struct thread *thread, struct addr_location *al,
1471 struct perf_sample *sample)
1473 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1475 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1478 al->cpu = sample->cpu;
1482 al->sym = map__find_symbol(al->map, al->addr);