4 #include "util/cache.h"
5 #include "util/symbol.h"
6 #include "util/thread.h"
7 #include "util/header.h"
9 #include "util/parse-options.h"
12 #include "util/debug.h"
14 #include "util/trace-event.h"
16 static char const *input_name
= "perf.data";
18 static unsigned long page_size
;
19 static unsigned long mmap_window
= 32;
21 static unsigned long total
= 0;
22 static unsigned long total_comm
= 0;
24 static struct rb_root threads
;
25 static struct thread
*last_match
;
27 static struct perf_header
*header
;
28 static u64 sample_type
;
32 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
34 struct thread
*thread
;
36 thread
= threads__findnew(event
->comm
.pid
, &threads
, &last_match
);
38 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
39 (void *)(offset
+ head
),
40 (void *)(long)(event
->header
.size
),
41 event
->comm
.comm
, event
->comm
.pid
);
44 thread__set_comm(thread
, event
->comm
.comm
)) {
45 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
54 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
58 struct dso
*dso
= NULL
;
59 struct thread
*thread
;
60 u64 ip
= event
->ip
.ip
;
64 void *more_data
= event
->ip
.__more_data
;
67 thread
= threads__findnew(event
->ip
.pid
, &threads
, &last_match
);
69 if (sample_type
& PERF_SAMPLE_TIME
) {
70 timestamp
= *(u64
*)more_data
;
71 more_data
+= sizeof(u64
);
74 if (sample_type
& PERF_SAMPLE_CPU
) {
75 cpu
= *(u32
*)more_data
;
76 more_data
+= sizeof(u32
);
77 more_data
+= sizeof(u32
); /* reserved */
80 if (sample_type
& PERF_SAMPLE_PERIOD
) {
81 period
= *(u64
*)more_data
;
82 more_data
+= sizeof(u64
);
85 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
86 (void *)(offset
+ head
),
87 (void *)(long)(event
->header
.size
),
89 event
->ip
.pid
, event
->ip
.tid
,
93 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
96 eprintf("problem processing %d event, skipping it.\n",
101 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
103 if (cpumode
== PERF_RECORD_MISC_KERNEL
) {
109 dump_printf(" ...... dso: %s\n", dso
->name
);
111 } else if (cpumode
== PERF_RECORD_MISC_USER
) {
120 dso
= hypervisor_dso
;
122 dump_printf(" ...... dso: [hypervisor]\n");
125 if (sample_type
& PERF_SAMPLE_RAW
) {
132 * FIXME: better resolve from pid from the struct trace_entry
133 * field, although it should be the same than this perf
136 print_event(cpu
, raw
->data
, raw
->size
, timestamp
, thread
->comm
);
144 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
148 switch (event
->header
.type
) {
149 case PERF_RECORD_MMAP
... PERF_RECORD_LOST
:
152 case PERF_RECORD_COMM
:
153 return process_comm_event(event
, offset
, head
);
155 case PERF_RECORD_EXIT
... PERF_RECORD_READ
:
158 case PERF_RECORD_SAMPLE
:
159 return process_sample_event(event
, offset
, head
);
161 case PERF_RECORD_MAX
:
169 static int __cmd_trace(void)
171 int ret
, rc
= EXIT_FAILURE
;
172 unsigned long offset
= 0;
173 unsigned long head
= 0;
174 struct stat perf_stat
;
180 register_idle_thread(&threads
, &last_match
);
182 input
= open(input_name
, O_RDONLY
);
184 perror("failed to open file");
188 ret
= fstat(input
, &perf_stat
);
190 perror("failed to stat file");
194 if (!perf_stat
.st_size
) {
195 fprintf(stderr
, "zero-sized file, nothing to do!\n");
198 header
= perf_header__read(input
);
199 head
= header
->data_offset
;
200 sample_type
= perf_header__sample_type(header
);
202 if (!(sample_type
& PERF_SAMPLE_RAW
))
203 die("No trace sample to read. Did you call perf record "
206 if (load_kernel() < 0) {
207 perror("failed to load kernel symbols");
212 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
213 MAP_SHARED
, input
, offset
);
214 if (buf
== MAP_FAILED
) {
215 perror("failed to mmap file");
220 event
= (event_t
*)(buf
+ head
);
222 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
223 unsigned long shift
= page_size
* (head
/ page_size
);
226 res
= munmap(buf
, page_size
* mmap_window
);
234 size
= event
->header
.size
;
236 if (!size
|| process_event(event
, offset
, head
) < 0) {
239 * assume we lost track of the stream, check alignment, and
240 * increment a single u64 in the hope to catch on again 'soon'.
243 if (unlikely(head
& 7))
251 if (offset
+ head
< (unsigned long)perf_stat
.st_size
)
260 static const char * const annotate_usage
[] = {
261 "perf trace [<options>] <command>",
265 static const struct option options
[] = {
266 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
267 "dump raw trace in ASCII"),
268 OPT_BOOLEAN('v', "verbose", &verbose
,
269 "be more verbose (show symbol address, etc)"),
273 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
276 page_size
= getpagesize();
278 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
281 * Special case: if there's an argument left then assume tha
282 * it's a symbol filter:
285 usage_with_options(annotate_usage
, options
);
290 return __cmd_trace();