1 #include <linux/types.h>
11 #include "thread_map.h"
12 #include "symbol/kallsyms.h"
16 static const char *perf_event__names[] = {
18 [PERF_RECORD_MMAP] = "MMAP",
19 [PERF_RECORD_MMAP2] = "MMAP2",
20 [PERF_RECORD_LOST] = "LOST",
21 [PERF_RECORD_COMM] = "COMM",
22 [PERF_RECORD_EXIT] = "EXIT",
23 [PERF_RECORD_THROTTLE] = "THROTTLE",
24 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
25 [PERF_RECORD_FORK] = "FORK",
26 [PERF_RECORD_READ] = "READ",
27 [PERF_RECORD_SAMPLE] = "SAMPLE",
28 [PERF_RECORD_AUX] = "AUX",
29 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
30 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
31 [PERF_RECORD_SWITCH] = "SWITCH",
32 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
33 [PERF_RECORD_HEADER_ATTR] = "ATTR",
34 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
35 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
36 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
37 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
38 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
39 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
40 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
41 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
42 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
43 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
44 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
45 [PERF_RECORD_STAT] = "STAT",
46 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
47 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
50 const char *perf_event__name(unsigned int id)
52 if (id >= ARRAY_SIZE(perf_event__names))
54 if (!perf_event__names[id])
56 return perf_event__names[id];
59 static int perf_tool__process_synth_event(struct perf_tool *tool,
60 union perf_event *event,
61 struct machine *machine,
62 perf_event__handler_t process)
64 struct perf_sample synth_sample = {
71 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
74 return process(tool, event, &synth_sample, machine);
78 * Assumes that the first 4095 bytes of /proc/pid/stat contains
79 * the comm, tgid and ppid.
81 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
82 pid_t *tgid, pid_t *ppid)
84 char filename[PATH_MAX];
89 char *nl, *name, *tgids, *ppids;
94 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
96 fd = open(filename, O_RDONLY);
98 pr_debug("couldn't open %s\n", filename);
102 n = read(fd, bf, sizeof(bf) - 1);
105 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
111 name = strstr(bf, "Name:");
112 tgids = strstr(bf, "Tgid:");
113 ppids = strstr(bf, "PPid:");
116 name += 5; /* strlen("Name:") */
118 while (*name && isspace(*name))
121 nl = strchr(name, '\n');
128 memcpy(comm, name, size);
131 pr_debug("Name: string not found for pid %d\n", pid);
135 tgids += 5; /* strlen("Tgid:") */
138 pr_debug("Tgid: string not found for pid %d\n", pid);
142 ppids += 5; /* strlen("PPid:") */
145 pr_debug("PPid: string not found for pid %d\n", pid);
151 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
152 struct machine *machine,
153 pid_t *tgid, pid_t *ppid)
159 memset(&event->comm, 0, sizeof(event->comm));
161 if (machine__is_host(machine)) {
162 if (perf_event__get_comm_ids(pid, event->comm.comm,
163 sizeof(event->comm.comm),
168 *tgid = machine->pid;
174 event->comm.pid = *tgid;
175 event->comm.header.type = PERF_RECORD_COMM;
177 size = strlen(event->comm.comm) + 1;
178 size = PERF_ALIGN(size, sizeof(u64));
179 memset(event->comm.comm + size, 0, machine->id_hdr_size);
180 event->comm.header.size = (sizeof(event->comm) -
181 (sizeof(event->comm.comm) - size) +
182 machine->id_hdr_size);
183 event->comm.tid = pid;
188 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
189 union perf_event *event, pid_t pid,
190 perf_event__handler_t process,
191 struct machine *machine)
195 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
198 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
204 static int perf_event__synthesize_fork(struct perf_tool *tool,
205 union perf_event *event,
206 pid_t pid, pid_t tgid, pid_t ppid,
207 perf_event__handler_t process,
208 struct machine *machine)
210 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
213 * for main thread set parent to ppid from status file. For other
214 * threads set parent pid to main thread. ie., assume main thread
215 * spawns all threads in a process
218 event->fork.ppid = ppid;
219 event->fork.ptid = ppid;
221 event->fork.ppid = tgid;
222 event->fork.ptid = tgid;
224 event->fork.pid = tgid;
225 event->fork.tid = pid;
226 event->fork.header.type = PERF_RECORD_FORK;
228 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
230 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
236 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
237 union perf_event *event,
238 pid_t pid, pid_t tgid,
239 perf_event__handler_t process,
240 struct machine *machine,
242 unsigned int proc_map_timeout)
244 char filename[PATH_MAX];
246 unsigned long long t;
247 bool truncation = false;
248 unsigned long long timeout = proc_map_timeout * 1000000ULL;
251 if (machine__is_default_guest(machine))
254 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
255 machine->root_dir, pid);
257 fp = fopen(filename, "r");
260 * We raced with a task exiting - just return:
262 pr_debug("couldn't open %s\n", filename);
266 event->header.type = PERF_RECORD_MMAP2;
272 char execname[PATH_MAX];
273 char anonstr[] = "//anon";
278 if (fgets(bf, sizeof(bf), fp) == NULL)
281 if ((rdclock() - t) > timeout) {
282 pr_warning("Reading %s time out. "
283 "You may want to increase "
284 "the time limit by --proc-map-timeout\n",
290 /* ensure null termination since stack will be reused. */
291 strcpy(execname, "");
293 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
294 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
295 &event->mmap2.start, &event->mmap2.len, prot,
296 &event->mmap2.pgoff, &event->mmap2.maj,
301 * Anon maps don't have the execname.
306 event->mmap2.ino = (u64)ino;
309 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
311 if (machine__is_host(machine))
312 event->header.misc = PERF_RECORD_MISC_USER;
314 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
316 /* map protection and flags bits */
317 event->mmap2.prot = 0;
318 event->mmap2.flags = 0;
320 event->mmap2.prot |= PROT_READ;
322 event->mmap2.prot |= PROT_WRITE;
324 event->mmap2.prot |= PROT_EXEC;
327 event->mmap2.flags |= MAP_SHARED;
329 event->mmap2.flags |= MAP_PRIVATE;
331 if (prot[2] != 'x') {
332 if (!mmap_data || prot[0] != 'r')
335 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
340 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
342 if (!strcmp(execname, ""))
343 strcpy(execname, anonstr);
345 size = strlen(execname) + 1;
346 memcpy(event->mmap2.filename, execname, size);
347 size = PERF_ALIGN(size, sizeof(u64));
348 event->mmap2.len -= event->mmap.start;
349 event->mmap2.header.size = (sizeof(event->mmap2) -
350 (sizeof(event->mmap2.filename) - size));
351 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
352 event->mmap2.header.size += machine->id_hdr_size;
353 event->mmap2.pid = tgid;
354 event->mmap2.tid = pid;
356 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
369 int perf_event__synthesize_modules(struct perf_tool *tool,
370 perf_event__handler_t process,
371 struct machine *machine)
375 struct map_groups *kmaps = &machine->kmaps;
376 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
377 union perf_event *event = zalloc((sizeof(event->mmap) +
378 machine->id_hdr_size));
380 pr_debug("Not enough memory synthesizing mmap event "
381 "for kernel modules\n");
385 event->header.type = PERF_RECORD_MMAP;
388 * kernel uses 0 for user space maps, see kernel/perf_event.c
391 if (machine__is_host(machine))
392 event->header.misc = PERF_RECORD_MISC_KERNEL;
394 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
396 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
399 if (__map__is_kernel(pos))
402 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
403 event->mmap.header.type = PERF_RECORD_MMAP;
404 event->mmap.header.size = (sizeof(event->mmap) -
405 (sizeof(event->mmap.filename) - size));
406 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
407 event->mmap.header.size += machine->id_hdr_size;
408 event->mmap.start = pos->start;
409 event->mmap.len = pos->end - pos->start;
410 event->mmap.pid = machine->pid;
412 memcpy(event->mmap.filename, pos->dso->long_name,
413 pos->dso->long_name_len + 1);
414 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
424 static int __event__synthesize_thread(union perf_event *comm_event,
425 union perf_event *mmap_event,
426 union perf_event *fork_event,
428 perf_event__handler_t process,
429 struct perf_tool *tool,
430 struct machine *machine,
432 unsigned int proc_map_timeout)
434 char filename[PATH_MAX];
436 struct dirent dirent, *next;
440 /* special case: only send one comm event using passed in pid */
442 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
448 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
449 process, machine, mmap_data,
453 if (machine__is_default_guest(machine))
456 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
457 machine->root_dir, pid);
459 tasks = opendir(filename);
461 pr_debug("couldn't open %s\n", filename);
465 while (!readdir_r(tasks, &dirent, &next) && next) {
469 _pid = strtol(dirent.d_name, &end, 10);
474 if (perf_event__prepare_comm(comm_event, _pid, machine,
478 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
479 ppid, process, machine) < 0)
482 * Send the prepared comm event
484 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
489 /* process the parent's maps too */
490 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
491 process, machine, mmap_data, proc_map_timeout);
501 int perf_event__synthesize_thread_map(struct perf_tool *tool,
502 struct thread_map *threads,
503 perf_event__handler_t process,
504 struct machine *machine,
506 unsigned int proc_map_timeout)
508 union perf_event *comm_event, *mmap_event, *fork_event;
509 int err = -1, thread, j;
511 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
512 if (comm_event == NULL)
515 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
516 if (mmap_event == NULL)
519 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
520 if (fork_event == NULL)
524 for (thread = 0; thread < threads->nr; ++thread) {
525 if (__event__synthesize_thread(comm_event, mmap_event,
527 thread_map__pid(threads, thread), 0,
528 process, tool, machine,
529 mmap_data, proc_map_timeout)) {
535 * comm.pid is set to thread group id by
536 * perf_event__synthesize_comm
538 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
539 bool need_leader = true;
541 /* is thread group leader in thread_map? */
542 for (j = 0; j < threads->nr; ++j) {
543 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
549 /* if not, generate events for it */
551 __event__synthesize_thread(comm_event, mmap_event,
553 comm_event->comm.pid, 0,
554 process, tool, machine,
555 mmap_data, proc_map_timeout)) {
570 int perf_event__synthesize_threads(struct perf_tool *tool,
571 perf_event__handler_t process,
572 struct machine *machine,
574 unsigned int proc_map_timeout)
577 char proc_path[PATH_MAX];
578 struct dirent dirent, *next;
579 union perf_event *comm_event, *mmap_event, *fork_event;
582 if (machine__is_default_guest(machine))
585 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
586 if (comm_event == NULL)
589 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
590 if (mmap_event == NULL)
593 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
594 if (fork_event == NULL)
597 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
598 proc = opendir(proc_path);
603 while (!readdir_r(proc, &dirent, &next) && next) {
605 pid_t pid = strtol(dirent.d_name, &end, 10);
607 if (*end) /* only interested in proper numerical dirents */
610 * We may race with exiting thread, so don't stop just because
611 * one thread couldn't be synthesized.
613 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
614 1, process, tool, machine, mmap_data,
630 struct process_symbol_args {
635 static int find_symbol_cb(void *arg, const char *name, char type,
638 struct process_symbol_args *args = arg;
641 * Must be a function or at least an alias, as in PARISC64, where "_text" is
642 * an 'A' to the same address as "_stext".
644 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
645 type == 'A') || strcmp(name, args->name))
652 u64 kallsyms__get_function_start(const char *kallsyms_filename,
653 const char *symbol_name)
655 struct process_symbol_args args = { .name = symbol_name, };
657 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
663 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
664 perf_event__handler_t process,
665 struct machine *machine)
668 const char *mmap_name;
669 char name_buff[PATH_MAX];
670 struct map *map = machine__kernel_map(machine);
673 union perf_event *event;
679 * We should get this from /sys/kernel/sections/.text, but till that is
680 * available use this, and after it is use this as a fallback for older
683 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
685 pr_debug("Not enough memory synthesizing mmap event "
686 "for kernel modules\n");
690 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
691 if (machine__is_host(machine)) {
693 * kernel uses PERF_RECORD_MISC_USER for user space maps,
694 * see kernel/perf_event.c __perf_event_mmap
696 event->header.misc = PERF_RECORD_MISC_KERNEL;
698 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
701 kmap = map__kmap(map);
702 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
703 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
704 size = PERF_ALIGN(size, sizeof(u64));
705 event->mmap.header.type = PERF_RECORD_MMAP;
706 event->mmap.header.size = (sizeof(event->mmap) -
707 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
708 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
709 event->mmap.start = map->start;
710 event->mmap.len = map->end - event->mmap.start;
711 event->mmap.pid = machine->pid;
713 err = perf_tool__process_synth_event(tool, event, machine, process);
719 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
720 struct thread_map *threads,
721 perf_event__handler_t process,
722 struct machine *machine)
724 union perf_event *event;
727 size = sizeof(event->thread_map);
728 size += threads->nr * sizeof(event->thread_map.entries[0]);
730 event = zalloc(size);
734 event->header.type = PERF_RECORD_THREAD_MAP;
735 event->header.size = size;
736 event->thread_map.nr = threads->nr;
738 for (i = 0; i < threads->nr; i++) {
739 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
740 char *comm = thread_map__comm(threads, i);
745 entry->pid = thread_map__pid(threads, i);
746 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
749 err = process(tool, event, NULL, machine);
755 static void synthesize_cpus(struct cpu_map_entries *cpus,
762 for (i = 0; i < map->nr; i++)
763 cpus->cpu[i] = map->map[i];
766 static void synthesize_mask(struct cpu_map_mask *mask,
767 struct cpu_map *map, int max)
771 mask->nr = BITS_TO_LONGS(max);
772 mask->long_size = sizeof(long);
774 for (i = 0; i < map->nr; i++)
775 set_bit(map->map[i], mask->mask);
778 static size_t cpus_size(struct cpu_map *map)
780 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
783 static size_t mask_size(struct cpu_map *map, int *max)
789 for (i = 0; i < map->nr; i++) {
790 /* bit possition of the cpu is + 1 */
791 int bit = map->map[i] + 1;
797 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
800 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
802 size_t size_cpus, size_mask;
803 bool is_dummy = cpu_map__empty(map);
806 * Both array and mask data have variable size based
807 * on the number of cpus and their actual values.
808 * The size of the 'struct cpu_map_data' is:
810 * array = size of 'struct cpu_map_entries' +
811 * number of cpus * sizeof(u64)
813 * mask = size of 'struct cpu_map_mask' +
814 * maximum cpu bit converted to size of longs
816 * and finaly + the size of 'struct cpu_map_data'.
818 size_cpus = cpus_size(map);
819 size_mask = mask_size(map, max);
821 if (is_dummy || (size_cpus < size_mask)) {
823 *type = PERF_CPU_MAP__CPUS;
826 *type = PERF_CPU_MAP__MASK;
829 *size += sizeof(struct cpu_map_data);
830 return zalloc(*size);
833 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
839 case PERF_CPU_MAP__CPUS:
840 synthesize_cpus((struct cpu_map_entries *) data->data, map);
842 case PERF_CPU_MAP__MASK:
843 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
849 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
851 size_t size = sizeof(struct cpu_map_event);
852 struct cpu_map_event *event;
856 event = cpu_map_data__alloc(map, &size, &type, &max);
860 event->header.type = PERF_RECORD_CPU_MAP;
861 event->header.size = size;
862 event->data.type = type;
864 cpu_map_data__synthesize(&event->data, map, type, max);
868 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
870 perf_event__handler_t process,
871 struct machine *machine)
873 struct cpu_map_event *event;
876 event = cpu_map_event__new(map);
880 err = process(tool, (union perf_event *) event, NULL, machine);
886 int perf_event__synthesize_stat_config(struct perf_tool *tool,
887 struct perf_stat_config *config,
888 perf_event__handler_t process,
889 struct machine *machine)
891 struct stat_config_event *event;
892 int size, i = 0, err;
894 size = sizeof(*event);
895 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
897 event = zalloc(size);
901 event->header.type = PERF_RECORD_STAT_CONFIG;
902 event->header.size = size;
903 event->nr = PERF_STAT_CONFIG_TERM__MAX;
905 #define ADD(__term, __val) \
906 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
907 event->data[i].val = __val; \
910 ADD(AGGR_MODE, config->aggr_mode)
911 ADD(INTERVAL, config->interval)
912 ADD(SCALE, config->scale)
914 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
915 "stat config terms unbalanced\n");
918 err = process(tool, (union perf_event *) event, NULL, machine);
924 int perf_event__synthesize_stat(struct perf_tool *tool,
925 u32 cpu, u32 thread, u64 id,
926 struct perf_counts_values *count,
927 perf_event__handler_t process,
928 struct machine *machine)
930 struct stat_event event;
932 event.header.type = PERF_RECORD_STAT;
933 event.header.size = sizeof(event);
934 event.header.misc = 0;
938 event.thread = thread;
939 event.val = count->val;
940 event.ena = count->ena;
941 event.run = count->run;
943 return process(tool, (union perf_event *) &event, NULL, machine);
946 int perf_event__synthesize_stat_round(struct perf_tool *tool,
947 u64 evtime, u64 type,
948 perf_event__handler_t process,
949 struct machine *machine)
951 struct stat_round_event event;
953 event.header.type = PERF_RECORD_STAT_ROUND;
954 event.header.size = sizeof(event);
955 event.header.misc = 0;
960 return process(tool, (union perf_event *) &event, NULL, machine);
963 void perf_event__read_stat_config(struct perf_stat_config *config,
964 struct stat_config_event *event)
968 for (i = 0; i < event->nr; i++) {
970 switch (event->data[i].tag) {
971 #define CASE(__term, __val) \
972 case PERF_STAT_CONFIG_TERM__##__term: \
973 config->__val = event->data[i].val; \
976 CASE(AGGR_MODE, aggr_mode)
978 CASE(INTERVAL, interval)
981 pr_warning("unknown stat config term %" PRIu64 "\n",
987 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
991 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
996 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
999 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1000 union perf_event *event,
1001 struct perf_sample *sample,
1002 struct machine *machine)
1004 return machine__process_comm_event(machine, event, sample);
1007 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1008 union perf_event *event,
1009 struct perf_sample *sample,
1010 struct machine *machine)
1012 return machine__process_lost_event(machine, event, sample);
1015 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1016 union perf_event *event,
1017 struct perf_sample *sample __maybe_unused,
1018 struct machine *machine)
1020 return machine__process_aux_event(machine, event);
1023 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1024 union perf_event *event,
1025 struct perf_sample *sample __maybe_unused,
1026 struct machine *machine)
1028 return machine__process_itrace_start_event(machine, event);
1031 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1032 union perf_event *event,
1033 struct perf_sample *sample,
1034 struct machine *machine)
1036 return machine__process_lost_samples_event(machine, event, sample);
1039 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1040 union perf_event *event,
1041 struct perf_sample *sample __maybe_unused,
1042 struct machine *machine)
1044 return machine__process_switch_event(machine, event);
1047 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1049 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1050 event->mmap.pid, event->mmap.tid, event->mmap.start,
1051 event->mmap.len, event->mmap.pgoff,
1052 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1053 event->mmap.filename);
1056 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1058 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1059 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1060 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1061 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1062 event->mmap2.min, event->mmap2.ino,
1063 event->mmap2.ino_generation,
1064 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1065 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1066 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1067 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1068 event->mmap2.filename);
1071 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1073 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1076 ret = fprintf(fp, " nr: ");
1079 ret += thread_map__fprintf(threads, fp);
1081 ret += fprintf(fp, "failed to get threads from event\n");
1083 thread_map__put(threads);
1087 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1089 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1092 ret = fprintf(fp, " nr: ");
1095 ret += cpu_map__fprintf(cpus, fp);
1097 ret += fprintf(fp, "failed to get cpumap from event\n");
1103 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1104 union perf_event *event,
1105 struct perf_sample *sample,
1106 struct machine *machine)
1108 return machine__process_mmap_event(machine, event, sample);
1111 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1112 union perf_event *event,
1113 struct perf_sample *sample,
1114 struct machine *machine)
1116 return machine__process_mmap2_event(machine, event, sample);
1119 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1121 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1122 event->fork.pid, event->fork.tid,
1123 event->fork.ppid, event->fork.ptid);
1126 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1127 union perf_event *event,
1128 struct perf_sample *sample,
1129 struct machine *machine)
1131 return machine__process_fork_event(machine, event, sample);
1134 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1135 union perf_event *event,
1136 struct perf_sample *sample,
1137 struct machine *machine)
1139 return machine__process_exit_event(machine, event, sample);
1142 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1144 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
1145 event->aux.aux_offset, event->aux.aux_size,
1147 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1148 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
1151 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1153 return fprintf(fp, " pid: %u tid: %u\n",
1154 event->itrace_start.pid, event->itrace_start.tid);
1157 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1159 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1160 const char *in_out = out ? "OUT" : "IN ";
1162 if (event->header.type == PERF_RECORD_SWITCH)
1163 return fprintf(fp, " %s\n", in_out);
1165 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1166 in_out, out ? "next" : "prev",
1167 event->context_switch.next_prev_pid,
1168 event->context_switch.next_prev_tid);
1171 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1173 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1174 perf_event__name(event->header.type));
1176 switch (event->header.type) {
1177 case PERF_RECORD_COMM:
1178 ret += perf_event__fprintf_comm(event, fp);
1180 case PERF_RECORD_FORK:
1181 case PERF_RECORD_EXIT:
1182 ret += perf_event__fprintf_task(event, fp);
1184 case PERF_RECORD_MMAP:
1185 ret += perf_event__fprintf_mmap(event, fp);
1187 case PERF_RECORD_MMAP2:
1188 ret += perf_event__fprintf_mmap2(event, fp);
1190 case PERF_RECORD_AUX:
1191 ret += perf_event__fprintf_aux(event, fp);
1193 case PERF_RECORD_ITRACE_START:
1194 ret += perf_event__fprintf_itrace_start(event, fp);
1196 case PERF_RECORD_SWITCH:
1197 case PERF_RECORD_SWITCH_CPU_WIDE:
1198 ret += perf_event__fprintf_switch(event, fp);
1201 ret += fprintf(fp, "\n");
1207 int perf_event__process(struct perf_tool *tool __maybe_unused,
1208 union perf_event *event,
1209 struct perf_sample *sample,
1210 struct machine *machine)
1212 return machine__process_event(machine, event, sample);
1215 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1216 enum map_type type, u64 addr,
1217 struct addr_location *al)
1219 struct map_groups *mg = thread->mg;
1220 struct machine *machine = mg->machine;
1221 bool load_map = false;
1223 al->machine = machine;
1224 al->thread = thread;
1226 al->cpumode = cpumode;
1229 if (machine == NULL) {
1234 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1236 mg = &machine->kmaps;
1238 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1240 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1242 mg = &machine->kmaps;
1244 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1250 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1251 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1253 al->filtered |= (1 << HIST_FILTER__GUEST);
1254 if ((cpumode == PERF_RECORD_MISC_USER ||
1255 cpumode == PERF_RECORD_MISC_KERNEL) &&
1257 al->filtered |= (1 << HIST_FILTER__HOST);
1262 al->map = map_groups__find(mg, type, al->addr);
1263 if (al->map == NULL) {
1265 * If this is outside of all known maps, and is a negative
1266 * address, try to look it up in the kernel dso, as it might be
1267 * a vsyscall or vdso (which executes in user-mode).
1269 * XXX This is nasty, we should have a symbol list in the
1270 * "[vdso]" dso, but for now lets use the old trick of looking
1271 * in the whole kernel symbol list.
1273 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1274 mg != &machine->kmaps &&
1275 machine__kernel_ip(machine, al->addr)) {
1276 mg = &machine->kmaps;
1282 * Kernel maps might be changed when loading symbols so loading
1283 * must be done prior to using kernel maps.
1286 map__load(al->map, machine->symbol_filter);
1287 al->addr = al->map->map_ip(al->map, al->addr);
1291 void thread__find_addr_location(struct thread *thread,
1292 u8 cpumode, enum map_type type, u64 addr,
1293 struct addr_location *al)
1295 thread__find_addr_map(thread, cpumode, type, addr, al);
1296 if (al->map != NULL)
1297 al->sym = map__find_symbol(al->map, al->addr,
1298 thread->mg->machine->symbol_filter);
1304 * Callers need to drop the reference to al->thread, obtained in
1305 * machine__findnew_thread()
1307 int machine__resolve(struct machine *machine, struct addr_location *al,
1308 struct perf_sample *sample)
1310 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1316 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1318 * Have we already created the kernel maps for this machine?
1320 * This should have happened earlier, when we processed the kernel MMAP
1321 * events, but for older perf.data files there was no such thing, so do
1324 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1325 machine__kernel_map(machine) == NULL)
1326 machine__create_kernel_maps(machine);
1328 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1329 dump_printf(" ...... dso: %s\n",
1330 al->map ? al->map->dso->long_name :
1331 al->level == 'H' ? "[hypervisor]" : "<not found>");
1333 if (thread__is_filtered(thread))
1334 al->filtered |= (1 << HIST_FILTER__THREAD);
1337 al->cpu = sample->cpu;
1341 struct perf_env *env = machine->env;
1343 if (env && env->cpu)
1344 al->socket = env->cpu[al->cpu].socket_id;
1348 struct dso *dso = al->map->dso;
1350 if (symbol_conf.dso_list &&
1351 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1353 (dso->short_name != dso->long_name &&
1354 strlist__has_entry(symbol_conf.dso_list,
1355 dso->long_name))))) {
1356 al->filtered |= (1 << HIST_FILTER__DSO);
1359 al->sym = map__find_symbol(al->map, al->addr,
1360 machine->symbol_filter);
1363 if (symbol_conf.sym_list &&
1364 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1366 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1373 * The preprocess_sample method will return with reference counts for the
1374 * in it, when done using (and perhaps getting ref counts if needing to
1375 * keep a pointer to one of those entries) it must be paired with
1376 * addr_location__put(), so that the refcounts can be decremented.
1378 void addr_location__put(struct addr_location *al)
1380 thread__zput(al->thread);
1383 bool is_bts_event(struct perf_event_attr *attr)
1385 return attr->type == PERF_TYPE_HARDWARE &&
1386 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1387 attr->sample_period == 1;
1390 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1392 if (attr->type == PERF_TYPE_SOFTWARE &&
1393 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1394 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1395 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1398 if (is_bts_event(attr))
1404 void thread__resolve(struct thread *thread, struct addr_location *al,
1405 struct perf_sample *sample)
1407 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1409 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1412 al->cpu = sample->cpu;
1416 al->sym = map__find_symbol(al->map, al->addr, NULL);