1 #include <linux/types.h>
11 #include "thread_map.h"
12 #include "symbol/kallsyms.h"
14 static const char *perf_event__names
[] = {
16 [PERF_RECORD_MMAP
] = "MMAP",
17 [PERF_RECORD_MMAP2
] = "MMAP2",
18 [PERF_RECORD_LOST
] = "LOST",
19 [PERF_RECORD_COMM
] = "COMM",
20 [PERF_RECORD_EXIT
] = "EXIT",
21 [PERF_RECORD_THROTTLE
] = "THROTTLE",
22 [PERF_RECORD_UNTHROTTLE
] = "UNTHROTTLE",
23 [PERF_RECORD_FORK
] = "FORK",
24 [PERF_RECORD_READ
] = "READ",
25 [PERF_RECORD_SAMPLE
] = "SAMPLE",
26 [PERF_RECORD_AUX
] = "AUX",
27 [PERF_RECORD_ITRACE_START
] = "ITRACE_START",
28 [PERF_RECORD_LOST_SAMPLES
] = "LOST_SAMPLES",
29 [PERF_RECORD_HEADER_ATTR
] = "ATTR",
30 [PERF_RECORD_HEADER_EVENT_TYPE
] = "EVENT_TYPE",
31 [PERF_RECORD_HEADER_TRACING_DATA
] = "TRACING_DATA",
32 [PERF_RECORD_HEADER_BUILD_ID
] = "BUILD_ID",
33 [PERF_RECORD_FINISHED_ROUND
] = "FINISHED_ROUND",
34 [PERF_RECORD_ID_INDEX
] = "ID_INDEX",
35 [PERF_RECORD_AUXTRACE_INFO
] = "AUXTRACE_INFO",
36 [PERF_RECORD_AUXTRACE
] = "AUXTRACE",
37 [PERF_RECORD_AUXTRACE_ERROR
] = "AUXTRACE_ERROR",
40 const char *perf_event__name(unsigned int id
)
42 if (id
>= ARRAY_SIZE(perf_event__names
))
44 if (!perf_event__names
[id
])
46 return perf_event__names
[id
];
49 static struct perf_sample synth_sample
= {
59 * Assumes that the first 4095 bytes of /proc/pid/stat contains
60 * the comm, tgid and ppid.
62 static int perf_event__get_comm_ids(pid_t pid
, char *comm
, size_t len
,
63 pid_t
*tgid
, pid_t
*ppid
)
65 char filename
[PATH_MAX
];
69 char *nl
, *name
, *tgids
, *ppids
;
74 snprintf(filename
, sizeof(filename
), "/proc/%d/status", pid
);
76 fd
= open(filename
, O_RDONLY
);
78 pr_debug("couldn't open %s\n", filename
);
82 n
= read(fd
, bf
, sizeof(bf
) - 1);
85 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
91 name
= strstr(bf
, "Name:");
92 tgids
= strstr(bf
, "Tgid:");
93 ppids
= strstr(bf
, "PPid:");
96 name
+= 5; /* strlen("Name:") */
98 while (*name
&& isspace(*name
))
101 nl
= strchr(name
, '\n');
108 memcpy(comm
, name
, size
);
111 pr_debug("Name: string not found for pid %d\n", pid
);
115 tgids
+= 5; /* strlen("Tgid:") */
118 pr_debug("Tgid: string not found for pid %d\n", pid
);
122 ppids
+= 5; /* strlen("PPid:") */
125 pr_debug("PPid: string not found for pid %d\n", pid
);
131 static int perf_event__prepare_comm(union perf_event
*event
, pid_t pid
,
132 struct machine
*machine
,
133 pid_t
*tgid
, pid_t
*ppid
)
139 memset(&event
->comm
, 0, sizeof(event
->comm
));
141 if (machine__is_host(machine
)) {
142 if (perf_event__get_comm_ids(pid
, event
->comm
.comm
,
143 sizeof(event
->comm
.comm
),
148 *tgid
= machine
->pid
;
154 event
->comm
.pid
= *tgid
;
155 event
->comm
.header
.type
= PERF_RECORD_COMM
;
157 size
= strlen(event
->comm
.comm
) + 1;
158 size
= PERF_ALIGN(size
, sizeof(u64
));
159 memset(event
->comm
.comm
+ size
, 0, machine
->id_hdr_size
);
160 event
->comm
.header
.size
= (sizeof(event
->comm
) -
161 (sizeof(event
->comm
.comm
) - size
) +
162 machine
->id_hdr_size
);
163 event
->comm
.tid
= pid
;
168 static pid_t
perf_event__synthesize_comm(struct perf_tool
*tool
,
169 union perf_event
*event
, pid_t pid
,
170 perf_event__handler_t process
,
171 struct machine
*machine
)
175 if (perf_event__prepare_comm(event
, pid
, machine
, &tgid
, &ppid
) != 0)
178 if (process(tool
, event
, &synth_sample
, machine
) != 0)
184 static int perf_event__synthesize_fork(struct perf_tool
*tool
,
185 union perf_event
*event
,
186 pid_t pid
, pid_t tgid
, pid_t ppid
,
187 perf_event__handler_t process
,
188 struct machine
*machine
)
190 memset(&event
->fork
, 0, sizeof(event
->fork
) + machine
->id_hdr_size
);
193 * for main thread set parent to ppid from status file. For other
194 * threads set parent pid to main thread. ie., assume main thread
195 * spawns all threads in a process
198 event
->fork
.ppid
= ppid
;
199 event
->fork
.ptid
= ppid
;
201 event
->fork
.ppid
= tgid
;
202 event
->fork
.ptid
= tgid
;
204 event
->fork
.pid
= tgid
;
205 event
->fork
.tid
= pid
;
206 event
->fork
.header
.type
= PERF_RECORD_FORK
;
208 event
->fork
.header
.size
= (sizeof(event
->fork
) + machine
->id_hdr_size
);
210 if (process(tool
, event
, &synth_sample
, machine
) != 0)
216 int perf_event__synthesize_mmap_events(struct perf_tool
*tool
,
217 union perf_event
*event
,
218 pid_t pid
, pid_t tgid
,
219 perf_event__handler_t process
,
220 struct machine
*machine
,
222 unsigned int proc_map_timeout
)
224 char filename
[PATH_MAX
];
226 unsigned long long t
;
227 bool truncation
= false;
228 unsigned long long timeout
= proc_map_timeout
* 1000000ULL;
231 if (machine__is_default_guest(machine
))
234 snprintf(filename
, sizeof(filename
), "%s/proc/%d/maps",
235 machine
->root_dir
, pid
);
237 fp
= fopen(filename
, "r");
240 * We raced with a task exiting - just return:
242 pr_debug("couldn't open %s\n", filename
);
246 event
->header
.type
= PERF_RECORD_MMAP2
;
252 char execname
[PATH_MAX
];
253 char anonstr
[] = "//anon";
258 if (fgets(bf
, sizeof(bf
), fp
) == NULL
)
261 if ((rdclock() - t
) > timeout
) {
262 pr_warning("Reading %s time out. "
263 "You may want to increase "
264 "the time limit by --proc-map-timeout\n",
270 /* ensure null termination since stack will be reused. */
271 strcpy(execname
, "");
273 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
274 n
= sscanf(bf
, "%"PRIx64
"-%"PRIx64
" %s %"PRIx64
" %x:%x %u %s\n",
275 &event
->mmap2
.start
, &event
->mmap2
.len
, prot
,
276 &event
->mmap2
.pgoff
, &event
->mmap2
.maj
,
281 * Anon maps don't have the execname.
286 event
->mmap2
.ino
= (u64
)ino
;
289 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
291 if (machine__is_host(machine
))
292 event
->header
.misc
= PERF_RECORD_MISC_USER
;
294 event
->header
.misc
= PERF_RECORD_MISC_GUEST_USER
;
296 /* map protection and flags bits */
297 event
->mmap2
.prot
= 0;
298 event
->mmap2
.flags
= 0;
300 event
->mmap2
.prot
|= PROT_READ
;
302 event
->mmap2
.prot
|= PROT_WRITE
;
304 event
->mmap2
.prot
|= PROT_EXEC
;
307 event
->mmap2
.flags
|= MAP_SHARED
;
309 event
->mmap2
.flags
|= MAP_PRIVATE
;
311 if (prot
[2] != 'x') {
312 if (!mmap_data
|| prot
[0] != 'r')
315 event
->header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
320 event
->header
.misc
|= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
;
322 if (!strcmp(execname
, ""))
323 strcpy(execname
, anonstr
);
325 size
= strlen(execname
) + 1;
326 memcpy(event
->mmap2
.filename
, execname
, size
);
327 size
= PERF_ALIGN(size
, sizeof(u64
));
328 event
->mmap2
.len
-= event
->mmap
.start
;
329 event
->mmap2
.header
.size
= (sizeof(event
->mmap2
) -
330 (sizeof(event
->mmap2
.filename
) - size
));
331 memset(event
->mmap2
.filename
+ size
, 0, machine
->id_hdr_size
);
332 event
->mmap2
.header
.size
+= machine
->id_hdr_size
;
333 event
->mmap2
.pid
= tgid
;
334 event
->mmap2
.tid
= pid
;
336 if (process(tool
, event
, &synth_sample
, machine
) != 0) {
349 int perf_event__synthesize_modules(struct perf_tool
*tool
,
350 perf_event__handler_t process
,
351 struct machine
*machine
)
355 struct map_groups
*kmaps
= &machine
->kmaps
;
356 struct maps
*maps
= &kmaps
->maps
[MAP__FUNCTION
];
357 union perf_event
*event
= zalloc((sizeof(event
->mmap
) +
358 machine
->id_hdr_size
));
360 pr_debug("Not enough memory synthesizing mmap event "
361 "for kernel modules\n");
365 event
->header
.type
= PERF_RECORD_MMAP
;
368 * kernel uses 0 for user space maps, see kernel/perf_event.c
371 if (machine__is_host(machine
))
372 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
374 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
376 for (pos
= maps__first(maps
); pos
; pos
= map__next(pos
)) {
379 if (pos
->dso
->kernel
)
382 size
= PERF_ALIGN(pos
->dso
->long_name_len
+ 1, sizeof(u64
));
383 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
384 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
385 (sizeof(event
->mmap
.filename
) - size
));
386 memset(event
->mmap
.filename
+ size
, 0, machine
->id_hdr_size
);
387 event
->mmap
.header
.size
+= machine
->id_hdr_size
;
388 event
->mmap
.start
= pos
->start
;
389 event
->mmap
.len
= pos
->end
- pos
->start
;
390 event
->mmap
.pid
= machine
->pid
;
392 memcpy(event
->mmap
.filename
, pos
->dso
->long_name
,
393 pos
->dso
->long_name_len
+ 1);
394 if (process(tool
, event
, &synth_sample
, machine
) != 0) {
404 static int __event__synthesize_thread(union perf_event
*comm_event
,
405 union perf_event
*mmap_event
,
406 union perf_event
*fork_event
,
408 perf_event__handler_t process
,
409 struct perf_tool
*tool
,
410 struct machine
*machine
,
412 unsigned int proc_map_timeout
)
414 char filename
[PATH_MAX
];
416 struct dirent dirent
, *next
;
420 /* special case: only send one comm event using passed in pid */
422 tgid
= perf_event__synthesize_comm(tool
, comm_event
, pid
,
428 return perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
429 process
, machine
, mmap_data
,
433 if (machine__is_default_guest(machine
))
436 snprintf(filename
, sizeof(filename
), "%s/proc/%d/task",
437 machine
->root_dir
, pid
);
439 tasks
= opendir(filename
);
441 pr_debug("couldn't open %s\n", filename
);
445 while (!readdir_r(tasks
, &dirent
, &next
) && next
) {
449 _pid
= strtol(dirent
.d_name
, &end
, 10);
454 if (perf_event__prepare_comm(comm_event
, _pid
, machine
,
458 if (perf_event__synthesize_fork(tool
, fork_event
, _pid
, tgid
,
459 ppid
, process
, machine
) < 0)
462 * Send the prepared comm event
464 if (process(tool
, comm_event
, &synth_sample
, machine
) != 0)
469 /* process the parent's maps too */
470 rc
= perf_event__synthesize_mmap_events(tool
, mmap_event
, pid
, tgid
,
471 process
, machine
, mmap_data
, proc_map_timeout
);
481 int perf_event__synthesize_thread_map(struct perf_tool
*tool
,
482 struct thread_map
*threads
,
483 perf_event__handler_t process
,
484 struct machine
*machine
,
486 unsigned int proc_map_timeout
)
488 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
489 int err
= -1, thread
, j
;
491 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
492 if (comm_event
== NULL
)
495 mmap_event
= malloc(sizeof(mmap_event
->mmap
) + machine
->id_hdr_size
);
496 if (mmap_event
== NULL
)
499 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
500 if (fork_event
== NULL
)
504 for (thread
= 0; thread
< threads
->nr
; ++thread
) {
505 if (__event__synthesize_thread(comm_event
, mmap_event
,
507 thread_map__pid(threads
, thread
), 0,
508 process
, tool
, machine
,
509 mmap_data
, proc_map_timeout
)) {
515 * comm.pid is set to thread group id by
516 * perf_event__synthesize_comm
518 if ((int) comm_event
->comm
.pid
!= thread_map__pid(threads
, thread
)) {
519 bool need_leader
= true;
521 /* is thread group leader in thread_map? */
522 for (j
= 0; j
< threads
->nr
; ++j
) {
523 if ((int) comm_event
->comm
.pid
== thread_map__pid(threads
, j
)) {
529 /* if not, generate events for it */
531 __event__synthesize_thread(comm_event
, mmap_event
,
533 comm_event
->comm
.pid
, 0,
534 process
, tool
, machine
,
535 mmap_data
, proc_map_timeout
)) {
550 int perf_event__synthesize_threads(struct perf_tool
*tool
,
551 perf_event__handler_t process
,
552 struct machine
*machine
,
554 unsigned int proc_map_timeout
)
557 char proc_path
[PATH_MAX
];
558 struct dirent dirent
, *next
;
559 union perf_event
*comm_event
, *mmap_event
, *fork_event
;
562 if (machine__is_default_guest(machine
))
565 comm_event
= malloc(sizeof(comm_event
->comm
) + machine
->id_hdr_size
);
566 if (comm_event
== NULL
)
569 mmap_event
= malloc(sizeof(mmap_event
->mmap
) + machine
->id_hdr_size
);
570 if (mmap_event
== NULL
)
573 fork_event
= malloc(sizeof(fork_event
->fork
) + machine
->id_hdr_size
);
574 if (fork_event
== NULL
)
577 snprintf(proc_path
, sizeof(proc_path
), "%s/proc", machine
->root_dir
);
578 proc
= opendir(proc_path
);
583 while (!readdir_r(proc
, &dirent
, &next
) && next
) {
585 pid_t pid
= strtol(dirent
.d_name
, &end
, 10);
587 if (*end
) /* only interested in proper numerical dirents */
590 * We may race with exiting thread, so don't stop just because
591 * one thread couldn't be synthesized.
593 __event__synthesize_thread(comm_event
, mmap_event
, fork_event
, pid
,
594 1, process
, tool
, machine
, mmap_data
,
610 struct process_symbol_args
{
615 static int find_symbol_cb(void *arg
, const char *name
, char type
,
618 struct process_symbol_args
*args
= arg
;
621 * Must be a function or at least an alias, as in PARISC64, where "_text" is
622 * an 'A' to the same address as "_stext".
624 if (!(symbol_type__is_a(type
, MAP__FUNCTION
) ||
625 type
== 'A') || strcmp(name
, args
->name
))
632 u64
kallsyms__get_function_start(const char *kallsyms_filename
,
633 const char *symbol_name
)
635 struct process_symbol_args args
= { .name
= symbol_name
, };
637 if (kallsyms__parse(kallsyms_filename
, &args
, find_symbol_cb
) <= 0)
643 int perf_event__synthesize_kernel_mmap(struct perf_tool
*tool
,
644 perf_event__handler_t process
,
645 struct machine
*machine
)
648 const char *mmap_name
;
649 char name_buff
[PATH_MAX
];
653 union perf_event
*event
;
655 if (machine
->vmlinux_maps
[0] == NULL
)
659 * We should get this from /sys/kernel/sections/.text, but till that is
660 * available use this, and after it is use this as a fallback for older
663 event
= zalloc((sizeof(event
->mmap
) + machine
->id_hdr_size
));
665 pr_debug("Not enough memory synthesizing mmap event "
666 "for kernel modules\n");
670 mmap_name
= machine__mmap_name(machine
, name_buff
, sizeof(name_buff
));
671 if (machine__is_host(machine
)) {
673 * kernel uses PERF_RECORD_MISC_USER for user space maps,
674 * see kernel/perf_event.c __perf_event_mmap
676 event
->header
.misc
= PERF_RECORD_MISC_KERNEL
;
678 event
->header
.misc
= PERF_RECORD_MISC_GUEST_KERNEL
;
681 map
= machine
->vmlinux_maps
[MAP__FUNCTION
];
682 kmap
= map__kmap(map
);
683 size
= snprintf(event
->mmap
.filename
, sizeof(event
->mmap
.filename
),
684 "%s%s", mmap_name
, kmap
->ref_reloc_sym
->name
) + 1;
685 size
= PERF_ALIGN(size
, sizeof(u64
));
686 event
->mmap
.header
.type
= PERF_RECORD_MMAP
;
687 event
->mmap
.header
.size
= (sizeof(event
->mmap
) -
688 (sizeof(event
->mmap
.filename
) - size
) + machine
->id_hdr_size
);
689 event
->mmap
.pgoff
= kmap
->ref_reloc_sym
->addr
;
690 event
->mmap
.start
= map
->start
;
691 event
->mmap
.len
= map
->end
- event
->mmap
.start
;
692 event
->mmap
.pid
= machine
->pid
;
694 err
= process(tool
, event
, &synth_sample
, machine
);
700 size_t perf_event__fprintf_comm(union perf_event
*event
, FILE *fp
)
704 if (event
->header
.misc
& PERF_RECORD_MISC_COMM_EXEC
)
709 return fprintf(fp
, "%s: %s:%d/%d\n", s
, event
->comm
.comm
, event
->comm
.pid
, event
->comm
.tid
);
712 int perf_event__process_comm(struct perf_tool
*tool __maybe_unused
,
713 union perf_event
*event
,
714 struct perf_sample
*sample
,
715 struct machine
*machine
)
717 return machine__process_comm_event(machine
, event
, sample
);
720 int perf_event__process_lost(struct perf_tool
*tool __maybe_unused
,
721 union perf_event
*event
,
722 struct perf_sample
*sample
,
723 struct machine
*machine
)
725 return machine__process_lost_event(machine
, event
, sample
);
728 int perf_event__process_aux(struct perf_tool
*tool __maybe_unused
,
729 union perf_event
*event
,
730 struct perf_sample
*sample __maybe_unused
,
731 struct machine
*machine
)
733 return machine__process_aux_event(machine
, event
);
736 int perf_event__process_itrace_start(struct perf_tool
*tool __maybe_unused
,
737 union perf_event
*event
,
738 struct perf_sample
*sample __maybe_unused
,
739 struct machine
*machine
)
741 return machine__process_itrace_start_event(machine
, event
);
744 int perf_event__process_lost_samples(struct perf_tool
*tool __maybe_unused
,
745 union perf_event
*event
,
746 struct perf_sample
*sample
,
747 struct machine
*machine
)
749 return machine__process_lost_samples_event(machine
, event
, sample
);
752 size_t perf_event__fprintf_mmap(union perf_event
*event
, FILE *fp
)
754 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
"]: %c %s\n",
755 event
->mmap
.pid
, event
->mmap
.tid
, event
->mmap
.start
,
756 event
->mmap
.len
, event
->mmap
.pgoff
,
757 (event
->header
.misc
& PERF_RECORD_MISC_MMAP_DATA
) ? 'r' : 'x',
758 event
->mmap
.filename
);
761 size_t perf_event__fprintf_mmap2(union perf_event
*event
, FILE *fp
)
763 return fprintf(fp
, " %d/%d: [%#" PRIx64
"(%#" PRIx64
") @ %#" PRIx64
764 " %02x:%02x %"PRIu64
" %"PRIu64
"]: %c%c%c%c %s\n",
765 event
->mmap2
.pid
, event
->mmap2
.tid
, event
->mmap2
.start
,
766 event
->mmap2
.len
, event
->mmap2
.pgoff
, event
->mmap2
.maj
,
767 event
->mmap2
.min
, event
->mmap2
.ino
,
768 event
->mmap2
.ino_generation
,
769 (event
->mmap2
.prot
& PROT_READ
) ? 'r' : '-',
770 (event
->mmap2
.prot
& PROT_WRITE
) ? 'w' : '-',
771 (event
->mmap2
.prot
& PROT_EXEC
) ? 'x' : '-',
772 (event
->mmap2
.flags
& MAP_SHARED
) ? 's' : 'p',
773 event
->mmap2
.filename
);
776 int perf_event__process_mmap(struct perf_tool
*tool __maybe_unused
,
777 union perf_event
*event
,
778 struct perf_sample
*sample
,
779 struct machine
*machine
)
781 return machine__process_mmap_event(machine
, event
, sample
);
784 int perf_event__process_mmap2(struct perf_tool
*tool __maybe_unused
,
785 union perf_event
*event
,
786 struct perf_sample
*sample
,
787 struct machine
*machine
)
789 return machine__process_mmap2_event(machine
, event
, sample
);
792 size_t perf_event__fprintf_task(union perf_event
*event
, FILE *fp
)
794 return fprintf(fp
, "(%d:%d):(%d:%d)\n",
795 event
->fork
.pid
, event
->fork
.tid
,
796 event
->fork
.ppid
, event
->fork
.ptid
);
799 int perf_event__process_fork(struct perf_tool
*tool __maybe_unused
,
800 union perf_event
*event
,
801 struct perf_sample
*sample
,
802 struct machine
*machine
)
804 return machine__process_fork_event(machine
, event
, sample
);
807 int perf_event__process_exit(struct perf_tool
*tool __maybe_unused
,
808 union perf_event
*event
,
809 struct perf_sample
*sample
,
810 struct machine
*machine
)
812 return machine__process_exit_event(machine
, event
, sample
);
815 size_t perf_event__fprintf_aux(union perf_event
*event
, FILE *fp
)
817 return fprintf(fp
, " offset: %#"PRIx64
" size: %#"PRIx64
" flags: %#"PRIx64
" [%s%s]\n",
818 event
->aux
.aux_offset
, event
->aux
.aux_size
,
820 event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
? "T" : "",
821 event
->aux
.flags
& PERF_AUX_FLAG_OVERWRITE
? "O" : "");
824 size_t perf_event__fprintf_itrace_start(union perf_event
*event
, FILE *fp
)
826 return fprintf(fp
, " pid: %u tid: %u\n",
827 event
->itrace_start
.pid
, event
->itrace_start
.tid
);
830 size_t perf_event__fprintf(union perf_event
*event
, FILE *fp
)
832 size_t ret
= fprintf(fp
, "PERF_RECORD_%s",
833 perf_event__name(event
->header
.type
));
835 switch (event
->header
.type
) {
836 case PERF_RECORD_COMM
:
837 ret
+= perf_event__fprintf_comm(event
, fp
);
839 case PERF_RECORD_FORK
:
840 case PERF_RECORD_EXIT
:
841 ret
+= perf_event__fprintf_task(event
, fp
);
843 case PERF_RECORD_MMAP
:
844 ret
+= perf_event__fprintf_mmap(event
, fp
);
846 case PERF_RECORD_MMAP2
:
847 ret
+= perf_event__fprintf_mmap2(event
, fp
);
849 case PERF_RECORD_AUX
:
850 ret
+= perf_event__fprintf_aux(event
, fp
);
852 case PERF_RECORD_ITRACE_START
:
853 ret
+= perf_event__fprintf_itrace_start(event
, fp
);
856 ret
+= fprintf(fp
, "\n");
862 int perf_event__process(struct perf_tool
*tool __maybe_unused
,
863 union perf_event
*event
,
864 struct perf_sample
*sample
,
865 struct machine
*machine
)
867 return machine__process_event(machine
, event
, sample
);
870 void thread__find_addr_map(struct thread
*thread
, u8 cpumode
,
871 enum map_type type
, u64 addr
,
872 struct addr_location
*al
)
874 struct map_groups
*mg
= thread
->mg
;
875 struct machine
*machine
= mg
->machine
;
876 bool load_map
= false;
878 al
->machine
= machine
;
881 al
->cpumode
= cpumode
;
884 if (machine
== NULL
) {
889 if (cpumode
== PERF_RECORD_MISC_KERNEL
&& perf_host
) {
891 mg
= &machine
->kmaps
;
893 } else if (cpumode
== PERF_RECORD_MISC_USER
&& perf_host
) {
895 } else if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
897 mg
= &machine
->kmaps
;
899 } else if (cpumode
== PERF_RECORD_MISC_GUEST_USER
&& perf_guest
) {
905 if ((cpumode
== PERF_RECORD_MISC_GUEST_USER
||
906 cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) &&
908 al
->filtered
|= (1 << HIST_FILTER__GUEST
);
909 if ((cpumode
== PERF_RECORD_MISC_USER
||
910 cpumode
== PERF_RECORD_MISC_KERNEL
) &&
912 al
->filtered
|= (1 << HIST_FILTER__HOST
);
917 al
->map
= map_groups__find(mg
, type
, al
->addr
);
918 if (al
->map
== NULL
) {
920 * If this is outside of all known maps, and is a negative
921 * address, try to look it up in the kernel dso, as it might be
922 * a vsyscall or vdso (which executes in user-mode).
924 * XXX This is nasty, we should have a symbol list in the
925 * "[vdso]" dso, but for now lets use the old trick of looking
926 * in the whole kernel symbol list.
928 if (cpumode
== PERF_RECORD_MISC_USER
&& machine
&&
929 mg
!= &machine
->kmaps
&&
930 machine__kernel_ip(machine
, al
->addr
)) {
931 mg
= &machine
->kmaps
;
937 * Kernel maps might be changed when loading symbols so loading
938 * must be done prior to using kernel maps.
941 map__load(al
->map
, machine
->symbol_filter
);
942 al
->addr
= al
->map
->map_ip(al
->map
, al
->addr
);
946 void thread__find_addr_location(struct thread
*thread
,
947 u8 cpumode
, enum map_type type
, u64 addr
,
948 struct addr_location
*al
)
950 thread__find_addr_map(thread
, cpumode
, type
, addr
, al
);
952 al
->sym
= map__find_symbol(al
->map
, al
->addr
,
953 thread
->mg
->machine
->symbol_filter
);
959 * Callers need to drop the reference to al->thread, obtained in
960 * machine__findnew_thread()
962 int perf_event__preprocess_sample(const union perf_event
*event
,
963 struct machine
*machine
,
964 struct addr_location
*al
,
965 struct perf_sample
*sample
)
967 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
968 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
974 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread
), thread
->tid
);
976 * Have we already created the kernel maps for this machine?
978 * This should have happened earlier, when we processed the kernel MMAP
979 * events, but for older perf.data files there was no such thing, so do
982 if (cpumode
== PERF_RECORD_MISC_KERNEL
&&
983 machine
->vmlinux_maps
[MAP__FUNCTION
] == NULL
)
984 machine__create_kernel_maps(machine
);
986 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, sample
->ip
, al
);
987 dump_printf(" ...... dso: %s\n",
988 al
->map
? al
->map
->dso
->long_name
:
989 al
->level
== 'H' ? "[hypervisor]" : "<not found>");
991 if (thread__is_filtered(thread
))
992 al
->filtered
|= (1 << HIST_FILTER__THREAD
);
995 al
->cpu
= sample
->cpu
;
998 struct dso
*dso
= al
->map
->dso
;
1000 if (symbol_conf
.dso_list
&&
1001 (!dso
|| !(strlist__has_entry(symbol_conf
.dso_list
,
1003 (dso
->short_name
!= dso
->long_name
&&
1004 strlist__has_entry(symbol_conf
.dso_list
,
1005 dso
->long_name
))))) {
1006 al
->filtered
|= (1 << HIST_FILTER__DSO
);
1009 al
->sym
= map__find_symbol(al
->map
, al
->addr
,
1010 machine
->symbol_filter
);
1013 if (symbol_conf
.sym_list
&&
1014 (!al
->sym
|| !strlist__has_entry(symbol_conf
.sym_list
,
1016 al
->filtered
|= (1 << HIST_FILTER__SYMBOL
);
1023 * The preprocess_sample method will return with reference counts for the
1024 * in it, when done using (and perhaps getting ref counts if needing to
1025 * keep a pointer to one of those entries) it must be paired with
1026 * addr_location__put(), so that the refcounts can be decremented.
1028 void addr_location__put(struct addr_location
*al
)
1030 thread__zput(al
->thread
);
1033 bool is_bts_event(struct perf_event_attr
*attr
)
1035 return attr
->type
== PERF_TYPE_HARDWARE
&&
1036 (attr
->config
& PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
1037 attr
->sample_period
== 1;
1040 bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
1042 if (attr
->type
== PERF_TYPE_SOFTWARE
&&
1043 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
||
1044 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
||
1045 attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
))
1048 if (is_bts_event(attr
))
1054 void perf_event__preprocess_sample_addr(union perf_event
*event
,
1055 struct perf_sample
*sample
,
1056 struct thread
*thread
,
1057 struct addr_location
*al
)
1059 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1061 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, sample
->addr
, al
);
1063 thread__find_addr_map(thread
, cpumode
, MAP__VARIABLE
,
1066 al
->cpu
= sample
->cpu
;
1070 al
->sym
= map__find_symbol(al
->map
, al
->addr
, NULL
);