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",
48 [PERF_RECORD_TIME_CONV
] = "TIME_CONV",
51 const char *perf_event__name(unsigned int id
)
53 if (id
>= ARRAY_SIZE(perf_event__names
))
55 if (!perf_event__names
[id
])
57 return perf_event__names
[id
];
60 static int perf_tool__process_synth_event(struct perf_tool
*tool
,
61 union perf_event
*event
,
62 struct machine
*machine
,
63 perf_event__handler_t process
)
65 struct perf_sample synth_sample
= {
72 .cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
,
75 return process(tool
, event
, &synth_sample
, machine
);
79 * Assumes that the first 4095 bytes of /proc/pid/stat contains
80 * the comm, tgid and ppid.
82 static int perf_event__get_comm_ids(pid_t pid
, char *comm
, size_t len
,
83 pid_t
*tgid
, pid_t
*ppid
)
85 char filename
[PATH_MAX
];
90 char *nl
, *name
, *tgids
, *ppids
;
95 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
97 fd
= open(filename
, O_RDONLY
);
99 pr_debug("couldn't open %s\n", filename
);
103 n
= read(fd
, bf
, sizeof(bf
) - 1);
106 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
112 name
= strstr(bf
, "Name:");
113 tgids
= strstr(bf
, "Tgid:");
114 ppids
= strstr(bf
, "PPid:");
117 name
+= 5; /* strlen("Name:") */
119 while (*name
&& isspace(*name
))
122 nl
= strchr(name
, '\n');
129 memcpy(comm
, name
, size
);
132 pr_debug("Name: string not found for pid %d\n", pid
);
136 tgids
+= 5; /* strlen("Tgid:") */
139 pr_debug("Tgid: string not found for pid %d\n", pid
);
143 ppids
+= 5; /* strlen("PPid:") */
146 pr_debug("PPid: string not found for pid %d\n", pid
);
152 static int perf_event__prepare_comm(union perf_event
*event
, pid_t pid
,
153 struct machine
*machine
,
154 pid_t
*tgid
, pid_t
*ppid
)
160 memset(&event
->comm
, 0, sizeof(event
->comm
));
162 if (machine__is_host(machine
)) {
163 if (perf_event__get_comm_ids(pid
, event
->comm
.comm
,
164 sizeof(event
->comm
.comm
),
169 *tgid
= machine
->pid
;
175 event
->comm
.pid
= *tgid
;
176 event
->comm
.header
.type
= PERF_RECORD_COMM
;
178 size
= strlen(event
->comm
.comm
) + 1;
179 size
= PERF_ALIGN(size
, sizeof(u64
));
180 memset(event
->comm
.comm
+ size
, 0, machine
->id_hdr_size
);
181 event
->comm
.header
.size
= (sizeof(event
->comm
) -
182 (sizeof(event
->comm
.comm
) - size
) +
183 machine
->id_hdr_size
);
184 event
->comm
.tid
= pid
;
189 pid_t
perf_event__synthesize_comm(struct perf_tool
*tool
,
190 union perf_event
*event
, pid_t pid
,
191 perf_event__handler_t process
,
192 struct machine
*machine
)
196 if (perf_event__prepare_comm(event
, pid
, machine
, &tgid
, &ppid
) != 0)
199 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
205 static int perf_event__synthesize_fork(struct perf_tool
*tool
,
206 union perf_event
*event
,
207 pid_t pid
, pid_t tgid
, pid_t ppid
,
208 perf_event__handler_t process
,
209 struct machine
*machine
)
211 memset(&event
->fork
, 0, sizeof(event
->fork
) + machine
->id_hdr_size
);
214 * for main thread set parent to ppid from status file. For other
215 * threads set parent pid to main thread. ie., assume main thread
216 * spawns all threads in a process
219 event
->fork
.ppid
= ppid
;
220 event
->fork
.ptid
= ppid
;
222 event
->fork
.ppid
= tgid
;
223 event
->fork
.ptid
= tgid
;
225 event
->fork
.pid
= tgid
;
226 event
->fork
.tid
= pid
;
227 event
->fork
.header
.type
= PERF_RECORD_FORK
;
229 event
->fork
.header
.size
= (sizeof(event
->fork
) + machine
->id_hdr_size
);
231 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0)
237 int perf_event__synthesize_mmap_events(struct perf_tool
*tool
,
238 union perf_event
*event
,
239 pid_t pid
, pid_t tgid
,
240 perf_event__handler_t process
,
241 struct machine
*machine
,
243 unsigned int proc_map_timeout
)
245 char filename
[PATH_MAX
];
247 unsigned long long t
;
248 bool truncation
= false;
249 unsigned long long timeout
= proc_map_timeout
* 1000000ULL;
252 if (machine__is_default_guest(machine
))
255 snprintf(filename
, sizeof(filename
), "%s/proc/%d/maps",
256 machine
->root_dir
, pid
);
258 fp
= fopen(filename
, "r");
261 * We raced with a task exiting - just return:
263 pr_debug("couldn't open %s\n", filename
);
267 event
->header
.type
= PERF_RECORD_MMAP2
;
273 char execname
[PATH_MAX
];
274 char anonstr
[] = "//anon";
279 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
282 if ((rdclock() - t
) > timeout
) {
283 pr_warning("Reading %s time out. "
284 "You may want to increase "
285 "the time limit by --proc-map-timeout\n",
291 /* ensure null termination since stack will be reused. */
292 strcpy(execname
, "");
294 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
295 n
= sscanf(bf
, "%"PRIx64
"-%"PRIx64
" %s %"PRIx64
" %x:%x %u %[^\n]\n",
296 &event
->mmap2
.start
, &event
->mmap2
.len
, prot
,
297 &event
->mmap2
.pgoff
, &event
->mmap2
.maj
,
302 * Anon maps don't have the execname.
307 event
->mmap2
.ino
= (u64
)ino
;
310 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
312 if (machine__is_host(machine
))
313 event
->header
.misc
= PERF_RECORD_MISC_USER
;
315 event
->header
.misc
= PERF_RECORD_MISC_GUEST_USER
;
317 /* map protection and flags bits */
318 event
->mmap2
.prot
= 0;
319 event
->mmap2
.flags
= 0;
321 event
->mmap2
.prot
|= PROT_READ
;
323 event
->mmap2
.prot
|= PROT_WRITE
;
325 event
->mmap2
.prot
|= PROT_EXEC
;
328 event
->mmap2
.flags
|= MAP_SHARED
;
330 event
->mmap2
.flags
|= MAP_PRIVATE
;
332 if (prot
[2] != 'x') {
333 if (!mmap_data
|| prot
[0] != 'r')
336 event
->header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
341 event
->header
.misc
|= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
;
343 if (!strcmp(execname
, ""))
344 strcpy(execname
, anonstr
);
346 size
= strlen(execname
) + 1;
347 memcpy(event
->mmap2
.filename
, execname
, size
);
348 size
= PERF_ALIGN(size
, sizeof(u64
));
349 event
->mmap2
.len
-= event
->mmap
.start
;
350 event
->mmap2
.header
.size
= (sizeof(event
->mmap2
) -
351 (sizeof(event
->mmap2
.filename
) - size
));
352 memset(event
->mmap2
.filename
+ size
, 0, machine
->id_hdr_size
);
353 event
->mmap2
.header
.size
+= machine
->id_hdr_size
;
354 event
->mmap2
.pid
= tgid
;
355 event
->mmap2
.tid
= pid
;
357 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
370 int perf_event__synthesize_modules(struct perf_tool
*tool
,
371 perf_event__handler_t process
,
372 struct machine
*machine
)
376 struct map_groups
*kmaps
= &machine
->kmaps
;
377 struct maps
*maps
= &kmaps
->maps
[MAP__FUNCTION
];
378 union perf_event
*event
= zalloc((sizeof(event
->mmap
) +
379 machine
->id_hdr_size
));
381 pr_debug("Not enough memory synthesizing mmap event "
382 "for kernel modules\n");
386 event
->header
.type
= PERF_RECORD_MMAP
;
389 * kernel uses 0 for user space maps, see kernel/perf_event.c
392 if (machine__is_host(machine
))
393 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
395 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
397 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
400 if (__map__is_kernel(pos
))
403 size
= PERF_ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
404 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
405 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
406 (sizeof(event
->mmap
.filename
) - size
));
407 memset(event
->mmap
.filename
+ size
, 0, machine
->id_hdr_size
);
408 event
->mmap
.header
.size
+= machine
->id_hdr_size
;
409 event
->mmap
.start
= pos
->start
;
410 event
->mmap
.len
= pos
->end
- pos
->start
;
411 event
->mmap
.pid
= machine
->pid
;
413 memcpy(event
->mmap
.filename
, pos
->dso
->long_name
,
414 pos
->dso
->long_name_len
+ 1);
415 if (perf_tool__process_synth_event(tool
, event
, machine
, process
) != 0) {
425 static int __event__synthesize_thread(union perf_event
*comm_event
,
426 union perf_event
*mmap_event
,
427 union perf_event
*fork_event
,
429 perf_event__handler_t process
,
430 struct perf_tool
*tool
,
431 struct machine
*machine
,
433 unsigned int proc_map_timeout
)
435 char filename
[PATH_MAX
];
437 struct dirent
*dirent
;
441 /* special case: only send one comm event using passed in pid */
443 tgid
= perf_event__synthesize_comm(tool
, comm_event
, pid
,
449 return perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
450 process
, machine
, mmap_data
,
454 if (machine__is_default_guest(machine
))
457 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task",
458 machine
->root_dir
, pid
);
460 tasks
= opendir(filename
);
462 pr_debug("couldn't open %s\n", filename
);
466 while ((dirent
= readdir(tasks
)) != NULL
) {
470 _pid
= strtol(dirent
->d_name
, &end
, 10);
475 if (perf_event__prepare_comm(comm_event
, _pid
, machine
,
479 if (perf_event__synthesize_fork(tool
, fork_event
, _pid
, tgid
,
480 ppid
, process
, machine
) < 0)
483 * Send the prepared comm event
485 if (perf_tool__process_synth_event(tool
, comm_event
, machine
, process
) != 0)
490 /* process the parent's maps too */
491 rc
= perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
492 process
, machine
, mmap_data
, proc_map_timeout
);
502 int perf_event__synthesize_thread_map(struct perf_tool
*tool
,
503 struct thread_map
*threads
,
504 perf_event__handler_t process
,
505 struct machine
*machine
,
507 unsigned int proc_map_timeout
)
509 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
510 int err
= -1, thread
, j
;
512 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
513 if (comm_event
== NULL
)
516 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
517 if (mmap_event
== NULL
)
520 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
521 if (fork_event
== NULL
)
525 for (thread
= 0; thread
< threads
->nr
; ++thread
) {
526 if (__event__synthesize_thread(comm_event
, mmap_event
,
528 thread_map__pid(threads
, thread
), 0,
529 process
, tool
, machine
,
530 mmap_data
, proc_map_timeout
)) {
536 * comm.pid is set to thread group id by
537 * perf_event__synthesize_comm
539 if ((int) comm_event
->comm
.pid
!= thread_map__pid(threads
, thread
)) {
540 bool need_leader
= true;
542 /* is thread group leader in thread_map? */
543 for (j
= 0; j
< threads
->nr
; ++j
) {
544 if ((int) comm_event
->comm
.pid
== thread_map__pid(threads
, j
)) {
550 /* if not, generate events for it */
552 __event__synthesize_thread(comm_event
, mmap_event
,
554 comm_event
->comm
.pid
, 0,
555 process
, tool
, machine
,
556 mmap_data
, proc_map_timeout
)) {
571 int perf_event__synthesize_threads(struct perf_tool
*tool
,
572 perf_event__handler_t process
,
573 struct machine
*machine
,
575 unsigned int proc_map_timeout
)
578 char proc_path
[PATH_MAX
];
579 struct dirent
*dirent
;
580 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
583 if (machine__is_default_guest(machine
))
586 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
587 if (comm_event
== NULL
)
590 mmap_event
= malloc(sizeof(mmap_event
->mmap2
) + machine
->id_hdr_size
);
591 if (mmap_event
== NULL
)
594 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
595 if (fork_event
== NULL
)
598 snprintf(proc_path
, sizeof(proc_path
), "%s/proc", machine
->root_dir
);
599 proc
= opendir(proc_path
);
604 while ((dirent
= readdir(proc
)) != NULL
) {
606 pid_t pid
= strtol(dirent
->d_name
, &end
, 10);
608 if (*end
) /* only interested in proper numerical dirents */
611 * We may race with exiting thread, so don't stop just because
612 * one thread couldn't be synthesized.
614 __event__synthesize_thread(comm_event
, mmap_event
, fork_event
, pid
,
615 1, process
, tool
, machine
, mmap_data
,
631 struct process_symbol_args
{
636 static int find_symbol_cb(void *arg
, const char *name
, char type
,
639 struct process_symbol_args
*args
= arg
;
642 * Must be a function or at least an alias, as in PARISC64, where "_text" is
643 * an 'A' to the same address as "_stext".
645 if (!(symbol_type__is_a(type
, MAP__FUNCTION
) ||
646 type
== 'A') || strcmp(name
, args
->name
))
653 u64
kallsyms__get_function_start(const char *kallsyms_filename
,
654 const char *symbol_name
)
656 struct process_symbol_args args
= { .name
= symbol_name
, };
658 if (kallsyms__parse(kallsyms_filename
, &args
, find_symbol_cb
) <= 0)
664 int perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
665 perf_event__handler_t process
,
666 struct machine
*machine
)
669 const char *mmap_name
;
670 char name_buff
[PATH_MAX
];
671 struct map
*map
= machine__kernel_map(machine
);
674 union perf_event
*event
;
676 if (symbol_conf
.kptr_restrict
)
682 * We should get this from /sys/kernel/sections/.text, but till that is
683 * available use this, and after it is use this as a fallback for older
686 event
= zalloc((sizeof(event
->mmap
) + machine
->id_hdr_size
));
688 pr_debug("Not enough memory synthesizing mmap event "
689 "for kernel modules\n");
693 mmap_name
= machine__mmap_name(machine
, name_buff
, sizeof(name_buff
));
694 if (machine__is_host(machine
)) {
696 * kernel uses PERF_RECORD_MISC_USER for user space maps,
697 * see kernel/perf_event.c __perf_event_mmap
699 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
701 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
704 kmap
= map__kmap(map
);
705 size
= snprintf(event
->mmap
.filename
, sizeof(event
->mmap
.filename
),
706 "%s%s", mmap_name
, kmap
->ref_reloc_sym
->name
) + 1;
707 size
= PERF_ALIGN(size
, sizeof(u64
));
708 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
709 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
710 (sizeof(event
->mmap
.filename
) - size
) + machine
->id_hdr_size
);
711 event
->mmap
.pgoff
= kmap
->ref_reloc_sym
->addr
;
712 event
->mmap
.start
= map
->start
;
713 event
->mmap
.len
= map
->end
- event
->mmap
.start
;
714 event
->mmap
.pid
= machine
->pid
;
716 err
= perf_tool__process_synth_event(tool
, event
, machine
, process
);
722 int perf_event__synthesize_thread_map2(struct perf_tool
*tool
,
723 struct thread_map
*threads
,
724 perf_event__handler_t process
,
725 struct machine
*machine
)
727 union perf_event
*event
;
730 size
= sizeof(event
->thread_map
);
731 size
+= threads
->nr
* sizeof(event
->thread_map
.entries
[0]);
733 event
= zalloc(size
);
737 event
->header
.type
= PERF_RECORD_THREAD_MAP
;
738 event
->header
.size
= size
;
739 event
->thread_map
.nr
= threads
->nr
;
741 for (i
= 0; i
< threads
->nr
; i
++) {
742 struct thread_map_event_entry
*entry
= &event
->thread_map
.entries
[i
];
743 char *comm
= thread_map__comm(threads
, i
);
748 entry
->pid
= thread_map__pid(threads
, i
);
749 strncpy((char *) &entry
->comm
, comm
, sizeof(entry
->comm
));
752 err
= process(tool
, event
, NULL
, machine
);
758 static void synthesize_cpus(struct cpu_map_entries
*cpus
,
765 for (i
= 0; i
< map
->nr
; i
++)
766 cpus
->cpu
[i
] = map
->map
[i
];
769 static void synthesize_mask(struct cpu_map_mask
*mask
,
770 struct cpu_map
*map
, int max
)
774 mask
->nr
= BITS_TO_LONGS(max
);
775 mask
->long_size
= sizeof(long);
777 for (i
= 0; i
< map
->nr
; i
++)
778 set_bit(map
->map
[i
], mask
->mask
);
781 static size_t cpus_size(struct cpu_map
*map
)
783 return sizeof(struct cpu_map_entries
) + map
->nr
* sizeof(u16
);
786 static size_t mask_size(struct cpu_map
*map
, int *max
)
792 for (i
= 0; i
< map
->nr
; i
++) {
793 /* bit possition of the cpu is + 1 */
794 int bit
= map
->map
[i
] + 1;
800 return sizeof(struct cpu_map_mask
) + BITS_TO_LONGS(*max
) * sizeof(long);
803 void *cpu_map_data__alloc(struct cpu_map
*map
, size_t *size
, u16
*type
, int *max
)
805 size_t size_cpus
, size_mask
;
806 bool is_dummy
= cpu_map__empty(map
);
809 * Both array and mask data have variable size based
810 * on the number of cpus and their actual values.
811 * The size of the 'struct cpu_map_data' is:
813 * array = size of 'struct cpu_map_entries' +
814 * number of cpus * sizeof(u64)
816 * mask = size of 'struct cpu_map_mask' +
817 * maximum cpu bit converted to size of longs
819 * and finaly + the size of 'struct cpu_map_data'.
821 size_cpus
= cpus_size(map
);
822 size_mask
= mask_size(map
, max
);
824 if (is_dummy
|| (size_cpus
< size_mask
)) {
826 *type
= PERF_CPU_MAP__CPUS
;
829 *type
= PERF_CPU_MAP__MASK
;
832 *size
+= sizeof(struct cpu_map_data
);
833 return zalloc(*size
);
836 void cpu_map_data__synthesize(struct cpu_map_data
*data
, struct cpu_map
*map
,
842 case PERF_CPU_MAP__CPUS
:
843 synthesize_cpus((struct cpu_map_entries
*) data
->data
, map
);
845 case PERF_CPU_MAP__MASK
:
846 synthesize_mask((struct cpu_map_mask
*) data
->data
, map
, max
);
852 static struct cpu_map_event
* cpu_map_event__new(struct cpu_map
*map
)
854 size_t size
= sizeof(struct cpu_map_event
);
855 struct cpu_map_event
*event
;
859 event
= cpu_map_data__alloc(map
, &size
, &type
, &max
);
863 event
->header
.type
= PERF_RECORD_CPU_MAP
;
864 event
->header
.size
= size
;
865 event
->data
.type
= type
;
867 cpu_map_data__synthesize(&event
->data
, map
, type
, max
);
871 int perf_event__synthesize_cpu_map(struct perf_tool
*tool
,
873 perf_event__handler_t process
,
874 struct machine
*machine
)
876 struct cpu_map_event
*event
;
879 event
= cpu_map_event__new(map
);
883 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
889 int perf_event__synthesize_stat_config(struct perf_tool
*tool
,
890 struct perf_stat_config
*config
,
891 perf_event__handler_t process
,
892 struct machine
*machine
)
894 struct stat_config_event
*event
;
895 int size
, i
= 0, err
;
897 size
= sizeof(*event
);
898 size
+= (PERF_STAT_CONFIG_TERM__MAX
* sizeof(event
->data
[0]));
900 event
= zalloc(size
);
904 event
->header
.type
= PERF_RECORD_STAT_CONFIG
;
905 event
->header
.size
= size
;
906 event
->nr
= PERF_STAT_CONFIG_TERM__MAX
;
908 #define ADD(__term, __val) \
909 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
910 event->data[i].val = __val; \
913 ADD(AGGR_MODE
, config
->aggr_mode
)
914 ADD(INTERVAL
, config
->interval
)
915 ADD(SCALE
, config
->scale
)
917 WARN_ONCE(i
!= PERF_STAT_CONFIG_TERM__MAX
,
918 "stat config terms unbalanced\n");
921 err
= process(tool
, (union perf_event
*) event
, NULL
, machine
);
927 int perf_event__synthesize_stat(struct perf_tool
*tool
,
928 u32 cpu
, u32 thread
, u64 id
,
929 struct perf_counts_values
*count
,
930 perf_event__handler_t process
,
931 struct machine
*machine
)
933 struct stat_event event
;
935 event
.header
.type
= PERF_RECORD_STAT
;
936 event
.header
.size
= sizeof(event
);
937 event
.header
.misc
= 0;
941 event
.thread
= thread
;
942 event
.val
= count
->val
;
943 event
.ena
= count
->ena
;
944 event
.run
= count
->run
;
946 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
949 int perf_event__synthesize_stat_round(struct perf_tool
*tool
,
950 u64 evtime
, u64 type
,
951 perf_event__handler_t process
,
952 struct machine
*machine
)
954 struct stat_round_event event
;
956 event
.header
.type
= PERF_RECORD_STAT_ROUND
;
957 event
.header
.size
= sizeof(event
);
958 event
.header
.misc
= 0;
963 return process(tool
, (union perf_event
*) &event
, NULL
, machine
);
966 void perf_event__read_stat_config(struct perf_stat_config
*config
,
967 struct stat_config_event
*event
)
971 for (i
= 0; i
< event
->nr
; i
++) {
973 switch (event
->data
[i
].tag
) {
974 #define CASE(__term, __val) \
975 case PERF_STAT_CONFIG_TERM__##__term: \
976 config->__val = event->data[i].val; \
979 CASE(AGGR_MODE
, aggr_mode
)
981 CASE(INTERVAL
, interval
)
984 pr_warning("unknown stat config term %" PRIu64
"\n",
990 size_t perf_event__fprintf_comm(union perf_event
*event
, FILE *fp
)
994 if (event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
)
999 return fprintf(fp
, "%s: %s:%d/%d\n", s
, event
->comm
.comm
, event
->comm
.pid
, event
->comm
.tid
);
1002 int perf_event__process_comm(struct perf_tool
*tool __maybe_unused
,
1003 union perf_event
*event
,
1004 struct perf_sample
*sample
,
1005 struct machine
*machine
)
1007 return machine__process_comm_event(machine
, event
, sample
);
1010 int perf_event__process_lost(struct perf_tool
*tool __maybe_unused
,
1011 union perf_event
*event
,
1012 struct perf_sample
*sample
,
1013 struct machine
*machine
)
1015 return machine__process_lost_event(machine
, event
, sample
);
1018 int perf_event__process_aux(struct perf_tool
*tool __maybe_unused
,
1019 union perf_event
*event
,
1020 struct perf_sample
*sample __maybe_unused
,
1021 struct machine
*machine
)
1023 return machine__process_aux_event(machine
, event
);
1026 int perf_event__process_itrace_start(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_itrace_start_event(machine
, event
);
1034 int perf_event__process_lost_samples(struct perf_tool
*tool __maybe_unused
,
1035 union perf_event
*event
,
1036 struct perf_sample
*sample
,
1037 struct machine
*machine
)
1039 return machine__process_lost_samples_event(machine
, event
, sample
);
1042 int perf_event__process_switch(struct perf_tool
*tool __maybe_unused
,
1043 union perf_event
*event
,
1044 struct perf_sample
*sample __maybe_unused
,
1045 struct machine
*machine
)
1047 return machine__process_switch_event(machine
, event
);
1050 size_t perf_event__fprintf_mmap(union perf_event
*event
, FILE *fp
)
1052 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
"]: %c %s\n",
1053 event
->mmap
.pid
, event
->mmap
.tid
, event
->mmap
.start
,
1054 event
->mmap
.len
, event
->mmap
.pgoff
,
1055 (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
) ? 'r' : 'x',
1056 event
->mmap
.filename
);
1059 size_t perf_event__fprintf_mmap2(union perf_event
*event
, FILE *fp
)
1061 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
1062 " %02x:%02x %"PRIu64
" %"PRIu64
"]: %c%c%c%c %s\n",
1063 event
->mmap2
.pid
, event
->mmap2
.tid
, event
->mmap2
.start
,
1064 event
->mmap2
.len
, event
->mmap2
.pgoff
, event
->mmap2
.maj
,
1065 event
->mmap2
.min
, event
->mmap2
.ino
,
1066 event
->mmap2
.ino_generation
,
1067 (event
->mmap2
.prot
& PROT_READ
) ? 'r' : '-',
1068 (event
->mmap2
.prot
& PROT_WRITE
) ? 'w' : '-',
1069 (event
->mmap2
.prot
& PROT_EXEC
) ? 'x' : '-',
1070 (event
->mmap2
.flags
& MAP_SHARED
) ? 's' : 'p',
1071 event
->mmap2
.filename
);
1074 size_t perf_event__fprintf_thread_map(union perf_event
*event
, FILE *fp
)
1076 struct thread_map
*threads
= thread_map__new_event(&event
->thread_map
);
1079 ret
= fprintf(fp
, " nr: ");
1082 ret
+= thread_map__fprintf(threads
, fp
);
1084 ret
+= fprintf(fp
, "failed to get threads from event\n");
1086 thread_map__put(threads
);
1090 size_t perf_event__fprintf_cpu_map(union perf_event
*event
, FILE *fp
)
1092 struct cpu_map
*cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
1095 ret
= fprintf(fp
, ": ");
1098 ret
+= cpu_map__fprintf(cpus
, fp
);
1100 ret
+= fprintf(fp
, "failed to get cpumap from event\n");
1106 int perf_event__process_mmap(struct perf_tool
*tool __maybe_unused
,
1107 union perf_event
*event
,
1108 struct perf_sample
*sample
,
1109 struct machine
*machine
)
1111 return machine__process_mmap_event(machine
, event
, sample
);
1114 int perf_event__process_mmap2(struct perf_tool
*tool __maybe_unused
,
1115 union perf_event
*event
,
1116 struct perf_sample
*sample
,
1117 struct machine
*machine
)
1119 return machine__process_mmap2_event(machine
, event
, sample
);
1122 size_t perf_event__fprintf_task(union perf_event
*event
, FILE *fp
)
1124 return fprintf(fp
, "(%d:%d):(%d:%d)\n",
1125 event
->fork
.pid
, event
->fork
.tid
,
1126 event
->fork
.ppid
, event
->fork
.ptid
);
1129 int perf_event__process_fork(struct perf_tool
*tool __maybe_unused
,
1130 union perf_event
*event
,
1131 struct perf_sample
*sample
,
1132 struct machine
*machine
)
1134 return machine__process_fork_event(machine
, event
, sample
);
1137 int perf_event__process_exit(struct perf_tool
*tool __maybe_unused
,
1138 union perf_event
*event
,
1139 struct perf_sample
*sample
,
1140 struct machine
*machine
)
1142 return machine__process_exit_event(machine
, event
, sample
);
1145 size_t perf_event__fprintf_aux(union perf_event
*event
, FILE *fp
)
1147 return fprintf(fp
, " offset: %#"PRIx64
" size: %#"PRIx64
" flags: %#"PRIx64
" [%s%s]\n",
1148 event
->aux
.aux_offset
, event
->aux
.aux_size
,
1150 event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
? "T" : "",
1151 event
->aux
.flags
& PERF_AUX_FLAG_OVERWRITE
? "O" : "");
1154 size_t perf_event__fprintf_itrace_start(union perf_event
*event
, FILE *fp
)
1156 return fprintf(fp
, " pid: %u tid: %u\n",
1157 event
->itrace_start
.pid
, event
->itrace_start
.tid
);
1160 size_t perf_event__fprintf_switch(union perf_event
*event
, FILE *fp
)
1162 bool out
= event
->header
.misc
& PERF_RECORD_MISC_SWITCH_OUT
;
1163 const char *in_out
= out
? "OUT" : "IN ";
1165 if (event
->header
.type
== PERF_RECORD_SWITCH
)
1166 return fprintf(fp
, " %s\n", in_out
);
1168 return fprintf(fp
, " %s %s pid/tid: %5u/%-5u\n",
1169 in_out
, out
? "next" : "prev",
1170 event
->context_switch
.next_prev_pid
,
1171 event
->context_switch
.next_prev_tid
);
1174 size_t perf_event__fprintf(union perf_event
*event
, FILE *fp
)
1176 size_t ret
= fprintf(fp
, "PERF_RECORD_%s",
1177 perf_event__name(event
->header
.type
));
1179 switch (event
->header
.type
) {
1180 case PERF_RECORD_COMM
:
1181 ret
+= perf_event__fprintf_comm(event
, fp
);
1183 case PERF_RECORD_FORK
:
1184 case PERF_RECORD_EXIT
:
1185 ret
+= perf_event__fprintf_task(event
, fp
);
1187 case PERF_RECORD_MMAP
:
1188 ret
+= perf_event__fprintf_mmap(event
, fp
);
1190 case PERF_RECORD_MMAP2
:
1191 ret
+= perf_event__fprintf_mmap2(event
, fp
);
1193 case PERF_RECORD_AUX
:
1194 ret
+= perf_event__fprintf_aux(event
, fp
);
1196 case PERF_RECORD_ITRACE_START
:
1197 ret
+= perf_event__fprintf_itrace_start(event
, fp
);
1199 case PERF_RECORD_SWITCH
:
1200 case PERF_RECORD_SWITCH_CPU_WIDE
:
1201 ret
+= perf_event__fprintf_switch(event
, fp
);
1204 ret
+= fprintf(fp
, "\n");
1210 int perf_event__process(struct perf_tool
*tool __maybe_unused
,
1211 union perf_event
*event
,
1212 struct perf_sample
*sample
,
1213 struct machine
*machine
)
1215 return machine__process_event(machine
, event
, sample
);
1218 void thread__find_addr_map(struct thread
*thread
, u8 cpumode
,
1219 enum map_type type
, u64 addr
,
1220 struct addr_location
*al
)
1222 struct map_groups
*mg
= thread
->mg
;
1223 struct machine
*machine
= mg
->machine
;
1224 bool load_map
= false;
1226 al
->machine
= machine
;
1227 al
->thread
= thread
;
1229 al
->cpumode
= cpumode
;
1232 if (machine
== NULL
) {
1237 if (cpumode
== PERF_RECORD_MISC_KERNEL
&& perf_host
) {
1239 mg
= &machine
->kmaps
;
1241 } else if (cpumode
== PERF_RECORD_MISC_USER
&& perf_host
) {
1243 } else if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
1245 mg
= &machine
->kmaps
;
1247 } else if (cpumode
== PERF_RECORD_MISC_GUEST_USER
&& perf_guest
) {
1253 if ((cpumode
== PERF_RECORD_MISC_GUEST_USER
||
1254 cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) &&
1256 al
->filtered
|= (1 << HIST_FILTER__GUEST
);
1257 if ((cpumode
== PERF_RECORD_MISC_USER
||
1258 cpumode
== PERF_RECORD_MISC_KERNEL
) &&
1260 al
->filtered
|= (1 << HIST_FILTER__HOST
);
1265 al
->map
= map_groups__find(mg
, type
, al
->addr
);
1266 if (al
->map
== NULL
) {
1268 * If this is outside of all known maps, and is a negative
1269 * address, try to look it up in the kernel dso, as it might be
1270 * a vsyscall or vdso (which executes in user-mode).
1272 * XXX This is nasty, we should have a symbol list in the
1273 * "[vdso]" dso, but for now lets use the old trick of looking
1274 * in the whole kernel symbol list.
1276 if (cpumode
== PERF_RECORD_MISC_USER
&& machine
&&
1277 mg
!= &machine
->kmaps
&&
1278 machine__kernel_ip(machine
, al
->addr
)) {
1279 mg
= &machine
->kmaps
;
1285 * Kernel maps might be changed when loading symbols so loading
1286 * must be done prior to using kernel maps.
1289 map__load(al
->map
, machine
->symbol_filter
);
1290 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
1294 void thread__find_addr_location(struct thread
*thread
,
1295 u8 cpumode
, enum map_type type
, u64 addr
,
1296 struct addr_location
*al
)
1298 thread__find_addr_map(thread
, cpumode
, type
, addr
, al
);
1299 if (al
->map
!= NULL
)
1300 al
->sym
= map__find_symbol(al
->map
, al
->addr
,
1301 thread
->mg
->machine
->symbol_filter
);
1307 * Callers need to drop the reference to al->thread, obtained in
1308 * machine__findnew_thread()
1310 int machine__resolve(struct machine
*machine
, struct addr_location
*al
,
1311 struct perf_sample
*sample
)
1313 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
1319 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread
), thread
->tid
);
1321 * Have we already created the kernel maps for this machine?
1323 * This should have happened earlier, when we processed the kernel MMAP
1324 * events, but for older perf.data files there was no such thing, so do
1327 if (sample
->cpumode
== PERF_RECORD_MISC_KERNEL
&&
1328 machine__kernel_map(machine
) == NULL
)
1329 machine__create_kernel_maps(machine
);
1331 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, sample
->ip
, al
);
1332 dump_printf(" ...... dso: %s\n",
1333 al
->map
? al
->map
->dso
->long_name
:
1334 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
1336 if (thread__is_filtered(thread
))
1337 al
->filtered
|= (1 << HIST_FILTER__THREAD
);
1340 al
->cpu
= sample
->cpu
;
1344 struct perf_env
*env
= machine
->env
;
1346 if (env
&& env
->cpu
)
1347 al
->socket
= env
->cpu
[al
->cpu
].socket_id
;
1351 struct dso
*dso
= al
->map
->dso
;
1353 if (symbol_conf
.dso_list
&&
1354 (!dso
|| !(strlist__has_entry(symbol_conf
.dso_list
,
1356 (dso
->short_name
!= dso
->long_name
&&
1357 strlist__has_entry(symbol_conf
.dso_list
,
1358 dso
->long_name
))))) {
1359 al
->filtered
|= (1 << HIST_FILTER__DSO
);
1362 al
->sym
= map__find_symbol(al
->map
, al
->addr
,
1363 machine
->symbol_filter
);
1366 if (symbol_conf
.sym_list
&&
1367 (!al
->sym
|| !strlist__has_entry(symbol_conf
.sym_list
,
1369 al
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1376 * The preprocess_sample method will return with reference counts for the
1377 * in it, when done using (and perhaps getting ref counts if needing to
1378 * keep a pointer to one of those entries) it must be paired with
1379 * addr_location__put(), so that the refcounts can be decremented.
1381 void addr_location__put(struct addr_location
*al
)
1383 thread__zput(al
->thread
);
1386 bool is_bts_event(struct perf_event_attr
*attr
)
1388 return attr
->type
== PERF_TYPE_HARDWARE
&&
1389 (attr
->config
& PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
1390 attr
->sample_period
== 1;
1393 bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
1395 if (attr
->type
== PERF_TYPE_SOFTWARE
&&
1396 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
||
1397 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
||
1398 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
))
1401 if (is_bts_event(attr
))
1407 void thread__resolve(struct thread
*thread
, struct addr_location
*al
,
1408 struct perf_sample
*sample
)
1410 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, sample
->addr
, al
);
1412 thread__find_addr_map(thread
, sample
->cpumode
, MAP__VARIABLE
,
1415 al
->cpu
= sample
->cpu
;
1419 al
->sym
= map__find_symbol(al
->map
, al
->addr
, NULL
);