perf tools: Handle both versions of ftrace output
[linux-2.6-block.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
3#include "util/util.h"
4#include "util/cache.h"
5#include "util/symbol.h"
6#include "util/thread.h"
7#include "util/header.h"
8
9#include "util/parse-options.h"
10
11#include "perf.h"
12#include "util/debug.h"
13
14#include "util/trace-event.h"
016e92fb 15#include "util/data_map.h"
5f9c39dc
FW
16
17static char const *input_name = "perf.data";
5f9c39dc
FW
18
19static unsigned long total = 0;
20static unsigned long total_comm = 0;
21
5f9c39dc
FW
22static struct perf_header *header;
23static u64 sample_type;
24
016e92fb
FW
25static char *cwd;
26static int cwdlen;
27
5f9c39dc
FW
28
29static int
30process_comm_event(event_t *event, unsigned long offset, unsigned long head)
31{
d5b889f2 32 struct thread *thread = threads__findnew(event->comm.pid);
5f9c39dc 33
cdd6c482 34 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
5f9c39dc
FW
35 (void *)(offset + head),
36 (void *)(long)(event->header.size),
37 event->comm.comm, event->comm.pid);
38
39 if (thread == NULL ||
40 thread__set_comm(thread, event->comm.comm)) {
cdd6c482 41 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
5f9c39dc
FW
42 return -1;
43 }
44 total_comm++;
45
46 return 0;
47}
48
49static int
50process_sample_event(event_t *event, unsigned long offset, unsigned long head)
51{
5f9c39dc 52 u64 ip = event->ip.ip;
6ddf259d 53 u64 timestamp = -1;
cd6feeea 54 u32 cpu = -1;
5f9c39dc
FW
55 u64 period = 1;
56 void *more_data = event->ip.__more_data;
d5b889f2 57 struct thread *thread = threads__findnew(event->ip.pid);
5f9c39dc 58
6ddf259d
IM
59 if (sample_type & PERF_SAMPLE_TIME) {
60 timestamp = *(u64 *)more_data;
61 more_data += sizeof(u64);
62 }
63
cd6feeea
IM
64 if (sample_type & PERF_SAMPLE_CPU) {
65 cpu = *(u32 *)more_data;
66 more_data += sizeof(u32);
67 more_data += sizeof(u32); /* reserved */
68 }
69
5f9c39dc
FW
70 if (sample_type & PERF_SAMPLE_PERIOD) {
71 period = *(u64 *)more_data;
72 more_data += sizeof(u64);
73 }
74
cdd6c482 75 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
5f9c39dc
FW
76 (void *)(offset + head),
77 (void *)(long)(event->header.size),
78 event->header.misc,
79 event->ip.pid, event->ip.tid,
80 (void *)(long)ip,
81 (long long)period);
82
83 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
84
85 if (thread == NULL) {
86 eprintf("problem processing %d event, skipping it.\n",
87 event->header.type);
88 return -1;
89 }
90
5f9c39dc
FW
91 if (sample_type & PERF_SAMPLE_RAW) {
92 struct {
93 u32 size;
94 char data[0];
95 } *raw = more_data;
96
97 /*
98 * FIXME: better resolve from pid from the struct trace_entry
99 * field, although it should be the same than this perf
100 * event pid
101 */
6ddf259d 102 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
5f9c39dc
FW
103 }
104 total += period;
105
106 return 0;
107}
108
016e92fb 109static int sample_type_check(u64 type)
5f9c39dc 110{
016e92fb 111 sample_type = type;
5f9c39dc 112
016e92fb
FW
113 if (!(sample_type & PERF_SAMPLE_RAW)) {
114 fprintf(stderr,
115 "No trace sample to read. Did you call perf record "
116 "without -R?");
5f9c39dc
FW
117 return -1;
118 }
119
120 return 0;
121}
122
016e92fb
FW
123static struct perf_file_handler file_handler = {
124 .process_sample_event = process_sample_event,
125 .process_comm_event = process_comm_event,
126 .sample_type_check = sample_type_check,
127};
128
5f9c39dc
FW
129static int __cmd_trace(void)
130{
d5b889f2 131 register_idle_thread();
016e92fb 132 register_perf_file_handler(&file_handler);
5f9c39dc 133
016e92fb 134 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
5f9c39dc
FW
135}
136
137static const char * const annotate_usage[] = {
138 "perf trace [<options>] <command>",
139 NULL
140};
141
142static const struct option options[] = {
143 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
144 "dump raw trace in ASCII"),
145 OPT_BOOLEAN('v', "verbose", &verbose,
146 "be more verbose (show symbol address, etc)"),
1909629f 147 OPT_END()
5f9c39dc
FW
148};
149
150int cmd_trace(int argc, const char **argv, const char *prefix __used)
151{
152 symbol__init();
5f9c39dc
FW
153
154 argc = parse_options(argc, argv, options, annotate_usage, 0);
155 if (argc) {
156 /*
157 * Special case: if there's an argument left then assume tha
158 * it's a symbol filter:
159 */
160 if (argc > 1)
161 usage_with_options(annotate_usage, options);
162 }
163
5f9c39dc
FW
164 setup_pager();
165
166 return __cmd_trace();
167}