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"
15 #include "util/data_map.h"
17 static char const *input_name
= "perf.data";
19 static unsigned long total
= 0;
20 static unsigned long total_comm
= 0;
22 static struct rb_root threads
;
23 static struct thread
*last_match
;
25 static struct perf_header
*header
;
26 static u64 sample_type
;
33 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
35 struct thread
*thread
;
37 thread
= threads__findnew(event
->comm
.pid
, &threads
, &last_match
);
39 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
40 (void *)(offset
+ head
),
41 (void *)(long)(event
->header
.size
),
42 event
->comm
.comm
, event
->comm
.pid
);
45 thread__set_comm(thread
, event
->comm
.comm
)) {
46 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
55 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
57 struct thread
*thread
;
58 u64 ip
= event
->ip
.ip
;
62 void *more_data
= event
->ip
.__more_data
;
64 thread
= threads__findnew(event
->ip
.pid
, &threads
, &last_match
);
66 if (sample_type
& PERF_SAMPLE_TIME
) {
67 timestamp
= *(u64
*)more_data
;
68 more_data
+= sizeof(u64
);
71 if (sample_type
& PERF_SAMPLE_CPU
) {
72 cpu
= *(u32
*)more_data
;
73 more_data
+= sizeof(u32
);
74 more_data
+= sizeof(u32
); /* reserved */
77 if (sample_type
& PERF_SAMPLE_PERIOD
) {
78 period
= *(u64
*)more_data
;
79 more_data
+= sizeof(u64
);
82 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
83 (void *)(offset
+ head
),
84 (void *)(long)(event
->header
.size
),
86 event
->ip
.pid
, event
->ip
.tid
,
90 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
93 eprintf("problem processing %d event, skipping it.\n",
98 if (sample_type
& PERF_SAMPLE_RAW
) {
105 * FIXME: better resolve from pid from the struct trace_entry
106 * field, although it should be the same than this perf
109 print_event(cpu
, raw
->data
, raw
->size
, timestamp
, thread
->comm
);
116 static int sample_type_check(u64 type
)
120 if (!(sample_type
& PERF_SAMPLE_RAW
)) {
122 "No trace sample to read. Did you call perf record "
130 static struct perf_file_handler file_handler
= {
131 .process_sample_event
= process_sample_event
,
132 .process_comm_event
= process_comm_event
,
133 .sample_type_check
= sample_type_check
,
136 static int __cmd_trace(void)
138 register_idle_thread(&threads
, &last_match
);
139 register_perf_file_handler(&file_handler
);
141 return mmap_dispatch_perf_file(&header
, input_name
, 0, 0, &cwdlen
, &cwd
);
144 static const char * const annotate_usage
[] = {
145 "perf trace [<options>] <command>",
149 static const struct option options
[] = {
150 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
151 "dump raw trace in ASCII"),
152 OPT_BOOLEAN('v', "verbose", &verbose
,
153 "be more verbose (show symbol address, etc)"),
157 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
161 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
164 * Special case: if there's an argument left then assume tha
165 * it's a symbol filter:
168 usage_with_options(annotate_usage
, options
);
173 return __cmd_trace();