1 #include <linux/types.h>
2 #include <uapi/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_HEADER_ATTR] = "ATTR",
35 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
36 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
37 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
38 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
39 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
40 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
41 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
42 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
43 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
44 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
45 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
46 [PERF_RECORD_STAT] = "STAT",
47 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
48 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
49 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
52 const char *perf_event__name(unsigned int id)
54 if (id >= ARRAY_SIZE(perf_event__names))
56 if (!perf_event__names[id])
58 return perf_event__names[id];
61 static int perf_tool__process_synth_event(struct perf_tool *tool,
62 union perf_event *event,
63 struct machine *machine,
64 perf_event__handler_t process)
66 struct perf_sample synth_sample = {
73 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
76 return process(tool, event, &synth_sample, machine);
80 * Assumes that the first 4095 bytes of /proc/pid/stat contains
81 * the comm, tgid and ppid.
83 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
84 pid_t *tgid, pid_t *ppid)
86 char filename[PATH_MAX];
91 char *nl, *name, *tgids, *ppids;
96 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
98 fd = open(filename, O_RDONLY);
100 pr_debug("couldn't open %s\n", filename);
104 n = read(fd, bf, sizeof(bf) - 1);
107 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
113 name = strstr(bf, "Name:");
114 tgids = strstr(bf, "Tgid:");
115 ppids = strstr(bf, "PPid:");
118 name += 5; /* strlen("Name:") */
120 while (*name && isspace(*name))
123 nl = strchr(name, '\n');
130 memcpy(comm, name, size);
133 pr_debug("Name: string not found for pid %d\n", pid);
137 tgids += 5; /* strlen("Tgid:") */
140 pr_debug("Tgid: string not found for pid %d\n", pid);
144 ppids += 5; /* strlen("PPid:") */
147 pr_debug("PPid: string not found for pid %d\n", pid);
153 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
154 struct machine *machine,
155 pid_t *tgid, pid_t *ppid)
161 memset(&event->comm, 0, sizeof(event->comm));
163 if (machine__is_host(machine)) {
164 if (perf_event__get_comm_ids(pid, event->comm.comm,
165 sizeof(event->comm.comm),
170 *tgid = machine->pid;
176 event->comm.pid = *tgid;
177 event->comm.header.type = PERF_RECORD_COMM;
179 size = strlen(event->comm.comm) + 1;
180 size = PERF_ALIGN(size, sizeof(u64));
181 memset(event->comm.comm + size, 0, machine->id_hdr_size);
182 event->comm.header.size = (sizeof(event->comm) -
183 (sizeof(event->comm.comm) - size) +
184 machine->id_hdr_size);
185 event->comm.tid = pid;
190 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
191 union perf_event *event, pid_t pid,
192 perf_event__handler_t process,
193 struct machine *machine)
197 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
200 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
206 static int perf_event__synthesize_fork(struct perf_tool *tool,
207 union perf_event *event,
208 pid_t pid, pid_t tgid, pid_t ppid,
209 perf_event__handler_t process,
210 struct machine *machine)
212 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
215 * for main thread set parent to ppid from status file. For other
216 * threads set parent pid to main thread. ie., assume main thread
217 * spawns all threads in a process
220 event->fork.ppid = ppid;
221 event->fork.ptid = ppid;
223 event->fork.ppid = tgid;
224 event->fork.ptid = tgid;
226 event->fork.pid = tgid;
227 event->fork.tid = pid;
228 event->fork.header.type = PERF_RECORD_FORK;
230 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
232 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
238 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
239 union perf_event *event,
240 pid_t pid, pid_t tgid,
241 perf_event__handler_t process,
242 struct machine *machine,
244 unsigned int proc_map_timeout)
246 char filename[PATH_MAX];
248 unsigned long long t;
249 bool truncation = false;
250 unsigned long long timeout = proc_map_timeout * 1000000ULL;
252 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
253 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
255 if (machine__is_default_guest(machine))
258 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
259 machine->root_dir, pid);
261 fp = fopen(filename, "r");
264 * We raced with a task exiting - just return:
266 pr_debug("couldn't open %s\n", filename);
270 event->header.type = PERF_RECORD_MMAP2;
276 char execname[PATH_MAX];
277 char anonstr[] = "//anon";
282 if (fgets(bf, sizeof(bf), fp) == NULL)
285 if ((rdclock() - t) > timeout) {
286 pr_warning("Reading %s time out. "
287 "You may want to increase "
288 "the time limit by --proc-map-timeout\n",
294 /* ensure null termination since stack will be reused. */
295 strcpy(execname, "");
297 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
298 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
299 &event->mmap2.start, &event->mmap2.len, prot,
300 &event->mmap2.pgoff, &event->mmap2.maj,
305 * Anon maps don't have the execname.
310 event->mmap2.ino = (u64)ino;
313 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
315 if (machine__is_host(machine))
316 event->header.misc = PERF_RECORD_MISC_USER;
318 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
320 /* map protection and flags bits */
321 event->mmap2.prot = 0;
322 event->mmap2.flags = 0;
324 event->mmap2.prot |= PROT_READ;
326 event->mmap2.prot |= PROT_WRITE;
328 event->mmap2.prot |= PROT_EXEC;
331 event->mmap2.flags |= MAP_SHARED;
333 event->mmap2.flags |= MAP_PRIVATE;
335 if (prot[2] != 'x') {
336 if (!mmap_data || prot[0] != 'r')
339 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
344 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
346 if (!strcmp(execname, ""))
347 strcpy(execname, anonstr);
349 if (!strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
350 strcpy(execname, anonstr);
351 event->mmap2.flags |= MAP_HUGETLB;
354 size = strlen(execname) + 1;
355 memcpy(event->mmap2.filename, execname, size);
356 size = PERF_ALIGN(size, sizeof(u64));
357 event->mmap2.len -= event->mmap.start;
358 event->mmap2.header.size = (sizeof(event->mmap2) -
359 (sizeof(event->mmap2.filename) - size));
360 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
361 event->mmap2.header.size += machine->id_hdr_size;
362 event->mmap2.pid = tgid;
363 event->mmap2.tid = pid;
365 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
378 int perf_event__synthesize_modules(struct perf_tool *tool,
379 perf_event__handler_t process,
380 struct machine *machine)
384 struct map_groups *kmaps = &machine->kmaps;
385 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
386 union perf_event *event = zalloc((sizeof(event->mmap) +
387 machine->id_hdr_size));
389 pr_debug("Not enough memory synthesizing mmap event "
390 "for kernel modules\n");
394 event->header.type = PERF_RECORD_MMAP;
397 * kernel uses 0 for user space maps, see kernel/perf_event.c
400 if (machine__is_host(machine))
401 event->header.misc = PERF_RECORD_MISC_KERNEL;
403 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
405 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
408 if (__map__is_kernel(pos))
411 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
412 event->mmap.header.type = PERF_RECORD_MMAP;
413 event->mmap.header.size = (sizeof(event->mmap) -
414 (sizeof(event->mmap.filename) - size));
415 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
416 event->mmap.header.size += machine->id_hdr_size;
417 event->mmap.start = pos->start;
418 event->mmap.len = pos->end - pos->start;
419 event->mmap.pid = machine->pid;
421 memcpy(event->mmap.filename, pos->dso->long_name,
422 pos->dso->long_name_len + 1);
423 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
433 static int __event__synthesize_thread(union perf_event *comm_event,
434 union perf_event *mmap_event,
435 union perf_event *fork_event,
437 perf_event__handler_t process,
438 struct perf_tool *tool,
439 struct machine *machine,
441 unsigned int proc_map_timeout)
443 char filename[PATH_MAX];
445 struct dirent *dirent;
449 /* special case: only send one comm event using passed in pid */
451 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
457 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
458 process, machine, mmap_data,
462 if (machine__is_default_guest(machine))
465 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
466 machine->root_dir, pid);
468 tasks = opendir(filename);
470 pr_debug("couldn't open %s\n", filename);
474 while ((dirent = readdir(tasks)) != NULL) {
478 _pid = strtol(dirent->d_name, &end, 10);
483 if (perf_event__prepare_comm(comm_event, _pid, machine,
487 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
488 ppid, process, machine) < 0)
491 * Send the prepared comm event
493 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
498 /* process the parent's maps too */
499 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
500 process, machine, mmap_data, proc_map_timeout);
510 int perf_event__synthesize_thread_map(struct perf_tool *tool,
511 struct thread_map *threads,
512 perf_event__handler_t process,
513 struct machine *machine,
515 unsigned int proc_map_timeout)
517 union perf_event *comm_event, *mmap_event, *fork_event;
518 int err = -1, thread, j;
520 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
521 if (comm_event == NULL)
524 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
525 if (mmap_event == NULL)
528 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
529 if (fork_event == NULL)
533 for (thread = 0; thread < threads->nr; ++thread) {
534 if (__event__synthesize_thread(comm_event, mmap_event,
536 thread_map__pid(threads, thread), 0,
537 process, tool, machine,
538 mmap_data, proc_map_timeout)) {
544 * comm.pid is set to thread group id by
545 * perf_event__synthesize_comm
547 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
548 bool need_leader = true;
550 /* is thread group leader in thread_map? */
551 for (j = 0; j < threads->nr; ++j) {
552 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
558 /* if not, generate events for it */
560 __event__synthesize_thread(comm_event, mmap_event,
562 comm_event->comm.pid, 0,
563 process, tool, machine,
564 mmap_data, proc_map_timeout)) {
579 int perf_event__synthesize_threads(struct perf_tool *tool,
580 perf_event__handler_t process,
581 struct machine *machine,
583 unsigned int proc_map_timeout)
586 char proc_path[PATH_MAX];
587 struct dirent *dirent;
588 union perf_event *comm_event, *mmap_event, *fork_event;
591 if (machine__is_default_guest(machine))
594 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
595 if (comm_event == NULL)
598 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
599 if (mmap_event == NULL)
602 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
603 if (fork_event == NULL)
606 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
607 proc = opendir(proc_path);
612 while ((dirent = readdir(proc)) != NULL) {
614 pid_t pid = strtol(dirent->d_name, &end, 10);
616 if (*end) /* only interested in proper numerical dirents */
619 * We may race with exiting thread, so don't stop just because
620 * one thread couldn't be synthesized.
622 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
623 1, process, tool, machine, mmap_data,
639 struct process_symbol_args {
644 static int find_symbol_cb(void *arg, const char *name, char type,
647 struct process_symbol_args *args = arg;
650 * Must be a function or at least an alias, as in PARISC64, where "_text" is
651 * an 'A' to the same address as "_stext".
653 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
654 type == 'A') || strcmp(name, args->name))
661 u64 kallsyms__get_function_start(const char *kallsyms_filename,
662 const char *symbol_name)
664 struct process_symbol_args args = { .name = symbol_name, };
666 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
672 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
673 perf_event__handler_t process,
674 struct machine *machine)
677 const char *mmap_name;
678 char name_buff[PATH_MAX];
679 struct map *map = machine__kernel_map(machine);
682 union perf_event *event;
684 if (symbol_conf.kptr_restrict)
690 * We should get this from /sys/kernel/sections/.text, but till that is
691 * available use this, and after it is use this as a fallback for older
694 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
696 pr_debug("Not enough memory synthesizing mmap event "
697 "for kernel modules\n");
701 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
702 if (machine__is_host(machine)) {
704 * kernel uses PERF_RECORD_MISC_USER for user space maps,
705 * see kernel/perf_event.c __perf_event_mmap
707 event->header.misc = PERF_RECORD_MISC_KERNEL;
709 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
712 kmap = map__kmap(map);
713 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
714 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
715 size = PERF_ALIGN(size, sizeof(u64));
716 event->mmap.header.type = PERF_RECORD_MMAP;
717 event->mmap.header.size = (sizeof(event->mmap) -
718 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
719 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
720 event->mmap.start = map->start;
721 event->mmap.len = map->end - event->mmap.start;
722 event->mmap.pid = machine->pid;
724 err = perf_tool__process_synth_event(tool, event, machine, process);
730 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
731 struct thread_map *threads,
732 perf_event__handler_t process,
733 struct machine *machine)
735 union perf_event *event;
738 size = sizeof(event->thread_map);
739 size += threads->nr * sizeof(event->thread_map.entries[0]);
741 event = zalloc(size);
745 event->header.type = PERF_RECORD_THREAD_MAP;
746 event->header.size = size;
747 event->thread_map.nr = threads->nr;
749 for (i = 0; i < threads->nr; i++) {
750 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
751 char *comm = thread_map__comm(threads, i);
756 entry->pid = thread_map__pid(threads, i);
757 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
760 err = process(tool, event, NULL, machine);
766 static void synthesize_cpus(struct cpu_map_entries *cpus,
773 for (i = 0; i < map->nr; i++)
774 cpus->cpu[i] = map->map[i];
777 static void synthesize_mask(struct cpu_map_mask *mask,
778 struct cpu_map *map, int max)
782 mask->nr = BITS_TO_LONGS(max);
783 mask->long_size = sizeof(long);
785 for (i = 0; i < map->nr; i++)
786 set_bit(map->map[i], mask->mask);
789 static size_t cpus_size(struct cpu_map *map)
791 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
794 static size_t mask_size(struct cpu_map *map, int *max)
800 for (i = 0; i < map->nr; i++) {
801 /* bit possition of the cpu is + 1 */
802 int bit = map->map[i] + 1;
808 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
811 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
813 size_t size_cpus, size_mask;
814 bool is_dummy = cpu_map__empty(map);
817 * Both array and mask data have variable size based
818 * on the number of cpus and their actual values.
819 * The size of the 'struct cpu_map_data' is:
821 * array = size of 'struct cpu_map_entries' +
822 * number of cpus * sizeof(u64)
824 * mask = size of 'struct cpu_map_mask' +
825 * maximum cpu bit converted to size of longs
827 * and finaly + the size of 'struct cpu_map_data'.
829 size_cpus = cpus_size(map);
830 size_mask = mask_size(map, max);
832 if (is_dummy || (size_cpus < size_mask)) {
834 *type = PERF_CPU_MAP__CPUS;
837 *type = PERF_CPU_MAP__MASK;
840 *size += sizeof(struct cpu_map_data);
841 return zalloc(*size);
844 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
850 case PERF_CPU_MAP__CPUS:
851 synthesize_cpus((struct cpu_map_entries *) data->data, map);
853 case PERF_CPU_MAP__MASK:
854 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
860 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
862 size_t size = sizeof(struct cpu_map_event);
863 struct cpu_map_event *event;
867 event = cpu_map_data__alloc(map, &size, &type, &max);
871 event->header.type = PERF_RECORD_CPU_MAP;
872 event->header.size = size;
873 event->data.type = type;
875 cpu_map_data__synthesize(&event->data, map, type, max);
879 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
881 perf_event__handler_t process,
882 struct machine *machine)
884 struct cpu_map_event *event;
887 event = cpu_map_event__new(map);
891 err = process(tool, (union perf_event *) event, NULL, machine);
897 int perf_event__synthesize_stat_config(struct perf_tool *tool,
898 struct perf_stat_config *config,
899 perf_event__handler_t process,
900 struct machine *machine)
902 struct stat_config_event *event;
903 int size, i = 0, err;
905 size = sizeof(*event);
906 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
908 event = zalloc(size);
912 event->header.type = PERF_RECORD_STAT_CONFIG;
913 event->header.size = size;
914 event->nr = PERF_STAT_CONFIG_TERM__MAX;
916 #define ADD(__term, __val) \
917 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
918 event->data[i].val = __val; \
921 ADD(AGGR_MODE, config->aggr_mode)
922 ADD(INTERVAL, config->interval)
923 ADD(SCALE, config->scale)
925 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
926 "stat config terms unbalanced\n");
929 err = process(tool, (union perf_event *) event, NULL, machine);
935 int perf_event__synthesize_stat(struct perf_tool *tool,
936 u32 cpu, u32 thread, u64 id,
937 struct perf_counts_values *count,
938 perf_event__handler_t process,
939 struct machine *machine)
941 struct stat_event event;
943 event.header.type = PERF_RECORD_STAT;
944 event.header.size = sizeof(event);
945 event.header.misc = 0;
949 event.thread = thread;
950 event.val = count->val;
951 event.ena = count->ena;
952 event.run = count->run;
954 return process(tool, (union perf_event *) &event, NULL, machine);
957 int perf_event__synthesize_stat_round(struct perf_tool *tool,
958 u64 evtime, u64 type,
959 perf_event__handler_t process,
960 struct machine *machine)
962 struct stat_round_event event;
964 event.header.type = PERF_RECORD_STAT_ROUND;
965 event.header.size = sizeof(event);
966 event.header.misc = 0;
971 return process(tool, (union perf_event *) &event, NULL, machine);
974 void perf_event__read_stat_config(struct perf_stat_config *config,
975 struct stat_config_event *event)
979 for (i = 0; i < event->nr; i++) {
981 switch (event->data[i].tag) {
982 #define CASE(__term, __val) \
983 case PERF_STAT_CONFIG_TERM__##__term: \
984 config->__val = event->data[i].val; \
987 CASE(AGGR_MODE, aggr_mode)
989 CASE(INTERVAL, interval)
992 pr_warning("unknown stat config term %" PRIu64 "\n",
998 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1002 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1007 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1010 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1011 union perf_event *event,
1012 struct perf_sample *sample,
1013 struct machine *machine)
1015 return machine__process_comm_event(machine, event, sample);
1018 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1019 union perf_event *event,
1020 struct perf_sample *sample,
1021 struct machine *machine)
1023 return machine__process_lost_event(machine, event, sample);
1026 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1027 union perf_event *event,
1028 struct perf_sample *sample __maybe_unused,
1029 struct machine *machine)
1031 return machine__process_aux_event(machine, event);
1034 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1035 union perf_event *event,
1036 struct perf_sample *sample __maybe_unused,
1037 struct machine *machine)
1039 return machine__process_itrace_start_event(machine, event);
1042 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1043 union perf_event *event,
1044 struct perf_sample *sample,
1045 struct machine *machine)
1047 return machine__process_lost_samples_event(machine, event, sample);
1050 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1051 union perf_event *event,
1052 struct perf_sample *sample __maybe_unused,
1053 struct machine *machine)
1055 return machine__process_switch_event(machine, event);
1058 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1060 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1061 event->mmap.pid, event->mmap.tid, event->mmap.start,
1062 event->mmap.len, event->mmap.pgoff,
1063 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1064 event->mmap.filename);
1067 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1069 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1070 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1071 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1072 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1073 event->mmap2.min, event->mmap2.ino,
1074 event->mmap2.ino_generation,
1075 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1076 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1077 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1078 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1079 event->mmap2.filename);
1082 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1084 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1087 ret = fprintf(fp, " nr: ");
1090 ret += thread_map__fprintf(threads, fp);
1092 ret += fprintf(fp, "failed to get threads from event\n");
1094 thread_map__put(threads);
1098 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1100 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1103 ret = fprintf(fp, ": ");
1106 ret += cpu_map__fprintf(cpus, fp);
1108 ret += fprintf(fp, "failed to get cpumap from event\n");
1114 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1115 union perf_event *event,
1116 struct perf_sample *sample,
1117 struct machine *machine)
1119 return machine__process_mmap_event(machine, event, sample);
1122 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1123 union perf_event *event,
1124 struct perf_sample *sample,
1125 struct machine *machine)
1127 return machine__process_mmap2_event(machine, event, sample);
1130 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1132 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1133 event->fork.pid, event->fork.tid,
1134 event->fork.ppid, event->fork.ptid);
1137 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1138 union perf_event *event,
1139 struct perf_sample *sample,
1140 struct machine *machine)
1142 return machine__process_fork_event(machine, event, sample);
1145 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1146 union perf_event *event,
1147 struct perf_sample *sample,
1148 struct machine *machine)
1150 return machine__process_exit_event(machine, event, sample);
1153 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1155 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
1156 event->aux.aux_offset, event->aux.aux_size,
1158 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1159 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
1162 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1164 return fprintf(fp, " pid: %u tid: %u\n",
1165 event->itrace_start.pid, event->itrace_start.tid);
1168 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1170 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1171 const char *in_out = out ? "OUT" : "IN ";
1173 if (event->header.type == PERF_RECORD_SWITCH)
1174 return fprintf(fp, " %s\n", in_out);
1176 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1177 in_out, out ? "next" : "prev",
1178 event->context_switch.next_prev_pid,
1179 event->context_switch.next_prev_tid);
1182 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1184 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1185 perf_event__name(event->header.type));
1187 switch (event->header.type) {
1188 case PERF_RECORD_COMM:
1189 ret += perf_event__fprintf_comm(event, fp);
1191 case PERF_RECORD_FORK:
1192 case PERF_RECORD_EXIT:
1193 ret += perf_event__fprintf_task(event, fp);
1195 case PERF_RECORD_MMAP:
1196 ret += perf_event__fprintf_mmap(event, fp);
1198 case PERF_RECORD_MMAP2:
1199 ret += perf_event__fprintf_mmap2(event, fp);
1201 case PERF_RECORD_AUX:
1202 ret += perf_event__fprintf_aux(event, fp);
1204 case PERF_RECORD_ITRACE_START:
1205 ret += perf_event__fprintf_itrace_start(event, fp);
1207 case PERF_RECORD_SWITCH:
1208 case PERF_RECORD_SWITCH_CPU_WIDE:
1209 ret += perf_event__fprintf_switch(event, fp);
1212 ret += fprintf(fp, "\n");
1218 int perf_event__process(struct perf_tool *tool __maybe_unused,
1219 union perf_event *event,
1220 struct perf_sample *sample,
1221 struct machine *machine)
1223 return machine__process_event(machine, event, sample);
1226 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1227 enum map_type type, u64 addr,
1228 struct addr_location *al)
1230 struct map_groups *mg = thread->mg;
1231 struct machine *machine = mg->machine;
1232 bool load_map = false;
1234 al->machine = machine;
1235 al->thread = thread;
1237 al->cpumode = cpumode;
1240 if (machine == NULL) {
1245 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1247 mg = &machine->kmaps;
1249 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1251 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1253 mg = &machine->kmaps;
1255 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1261 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1262 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1264 al->filtered |= (1 << HIST_FILTER__GUEST);
1265 if ((cpumode == PERF_RECORD_MISC_USER ||
1266 cpumode == PERF_RECORD_MISC_KERNEL) &&
1268 al->filtered |= (1 << HIST_FILTER__HOST);
1273 al->map = map_groups__find(mg, type, al->addr);
1274 if (al->map == NULL) {
1276 * If this is outside of all known maps, and is a negative
1277 * address, try to look it up in the kernel dso, as it might be
1278 * a vsyscall or vdso (which executes in user-mode).
1280 * XXX This is nasty, we should have a symbol list in the
1281 * "[vdso]" dso, but for now lets use the old trick of looking
1282 * in the whole kernel symbol list.
1284 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1285 mg != &machine->kmaps &&
1286 machine__kernel_ip(machine, al->addr)) {
1287 mg = &machine->kmaps;
1293 * Kernel maps might be changed when loading symbols so loading
1294 * must be done prior to using kernel maps.
1298 al->addr = al->map->map_ip(al->map, al->addr);
1302 void thread__find_addr_location(struct thread *thread,
1303 u8 cpumode, enum map_type type, u64 addr,
1304 struct addr_location *al)
1306 thread__find_addr_map(thread, cpumode, type, addr, al);
1307 if (al->map != NULL)
1308 al->sym = map__find_symbol(al->map, al->addr);
1314 * Callers need to drop the reference to al->thread, obtained in
1315 * machine__findnew_thread()
1317 int machine__resolve(struct machine *machine, struct addr_location *al,
1318 struct perf_sample *sample)
1320 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1326 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1328 * Have we already created the kernel maps for this machine?
1330 * This should have happened earlier, when we processed the kernel MMAP
1331 * events, but for older perf.data files there was no such thing, so do
1334 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1335 machine__kernel_map(machine) == NULL)
1336 machine__create_kernel_maps(machine);
1338 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1339 dump_printf(" ...... dso: %s\n",
1340 al->map ? al->map->dso->long_name :
1341 al->level == 'H' ? "[hypervisor]" : "<not found>");
1343 if (thread__is_filtered(thread))
1344 al->filtered |= (1 << HIST_FILTER__THREAD);
1347 al->cpu = sample->cpu;
1351 struct perf_env *env = machine->env;
1353 if (env && env->cpu)
1354 al->socket = env->cpu[al->cpu].socket_id;
1358 struct dso *dso = al->map->dso;
1360 if (symbol_conf.dso_list &&
1361 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1363 (dso->short_name != dso->long_name &&
1364 strlist__has_entry(symbol_conf.dso_list,
1365 dso->long_name))))) {
1366 al->filtered |= (1 << HIST_FILTER__DSO);
1369 al->sym = map__find_symbol(al->map, al->addr);
1372 if (symbol_conf.sym_list &&
1373 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1375 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1382 * The preprocess_sample method will return with reference counts for the
1383 * in it, when done using (and perhaps getting ref counts if needing to
1384 * keep a pointer to one of those entries) it must be paired with
1385 * addr_location__put(), so that the refcounts can be decremented.
1387 void addr_location__put(struct addr_location *al)
1389 thread__zput(al->thread);
1392 bool is_bts_event(struct perf_event_attr *attr)
1394 return attr->type == PERF_TYPE_HARDWARE &&
1395 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1396 attr->sample_period == 1;
1399 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1401 if (attr->type == PERF_TYPE_SOFTWARE &&
1402 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1403 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1404 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1407 if (is_bts_event(attr))
1413 void thread__resolve(struct thread *thread, struct addr_location *al,
1414 struct perf_sample *sample)
1416 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1418 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1421 al->cpu = sample->cpu;
1425 al->sym = map__find_symbol(al->map, al->addr);