perf trace: Add OPT_END to option array of perf-trace
[linux-2.6/kvm.git] / tools / perf / builtin-trace.c
blobdd3c2e7c9a18e7639d3dc49166c703554e6cee3e
1 #include "builtin.h"
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"
9 #include "util/parse-options.h"
11 #include "perf.h"
12 #include "util/debug.h"
14 #include "util/trace-event.h"
16 static char const *input_name = "perf.data";
17 static int input;
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;
31 static int
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_EVENT_COMM: %s:%d\n",
39 (void *)(offset + head),
40 (void *)(long)(event->header.size),
41 event->comm.comm, event->comm.pid);
43 if (thread == NULL ||
44 thread__set_comm(thread, event->comm.comm)) {
45 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
46 return -1;
48 total_comm++;
50 return 0;
53 static int
54 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
56 char level;
57 int show = 0;
58 struct dso *dso = NULL;
59 struct thread *thread;
60 u64 ip = event->ip.ip;
61 u64 period = 1;
62 void *more_data = event->ip.__more_data;
63 int cpumode;
65 thread = threads__findnew(event->ip.pid, &threads, &last_match);
67 if (sample_type & PERF_SAMPLE_PERIOD) {
68 period = *(u64 *)more_data;
69 more_data += sizeof(u64);
72 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
73 (void *)(offset + head),
74 (void *)(long)(event->header.size),
75 event->header.misc,
76 event->ip.pid, event->ip.tid,
77 (void *)(long)ip,
78 (long long)period);
80 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
82 if (thread == NULL) {
83 eprintf("problem processing %d event, skipping it.\n",
84 event->header.type);
85 return -1;
88 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
90 if (cpumode == PERF_EVENT_MISC_KERNEL) {
91 show = SHOW_KERNEL;
92 level = 'k';
94 dso = kernel_dso;
96 dump_printf(" ...... dso: %s\n", dso->name);
98 } else if (cpumode == PERF_EVENT_MISC_USER) {
100 show = SHOW_USER;
101 level = '.';
103 } else {
104 show = SHOW_HV;
105 level = 'H';
107 dso = hypervisor_dso;
109 dump_printf(" ...... dso: [hypervisor]\n");
112 if (sample_type & PERF_SAMPLE_RAW) {
113 struct {
114 u32 size;
115 char data[0];
116 } *raw = more_data;
119 * FIXME: better resolve from pid from the struct trace_entry
120 * field, although it should be the same than this perf
121 * event pid
123 print_event(0, raw->data, raw->size, 0, thread->comm);
125 total += period;
127 return 0;
130 static int
131 process_event(event_t *event, unsigned long offset, unsigned long head)
133 trace_event(event);
135 switch (event->header.type) {
136 case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
137 return 0;
139 case PERF_EVENT_COMM:
140 return process_comm_event(event, offset, head);
142 case PERF_EVENT_EXIT ... PERF_EVENT_READ:
143 return 0;
145 case PERF_EVENT_SAMPLE:
146 return process_sample_event(event, offset, head);
148 case PERF_EVENT_MAX:
149 default:
150 return -1;
153 return 0;
156 static int __cmd_trace(void)
158 int ret, rc = EXIT_FAILURE;
159 unsigned long offset = 0;
160 unsigned long head = 0;
161 struct stat perf_stat;
162 event_t *event;
163 uint32_t size;
164 char *buf;
166 trace_report();
168 input = open(input_name, O_RDONLY);
169 if (input < 0) {
170 perror("failed to open file");
171 exit(-1);
174 ret = fstat(input, &perf_stat);
175 if (ret < 0) {
176 perror("failed to stat file");
177 exit(-1);
180 if (!perf_stat.st_size) {
181 fprintf(stderr, "zero-sized file, nothing to do!\n");
182 exit(0);
184 header = perf_header__read(input);
185 sample_type = perf_header__sample_type(header);
187 if (!(sample_type & PERF_SAMPLE_RAW))
188 die("No trace sample to read. Did you call perf record "
189 "without -R?");
191 if (load_kernel() < 0) {
192 perror("failed to load kernel symbols");
193 return EXIT_FAILURE;
196 remap:
197 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
198 MAP_SHARED, input, offset);
199 if (buf == MAP_FAILED) {
200 perror("failed to mmap file");
201 exit(-1);
204 more:
205 event = (event_t *)(buf + head);
207 size = event->header.size;
208 if (!size)
209 size = 8;
211 if (head + event->header.size >= page_size * mmap_window) {
212 unsigned long shift = page_size * (head / page_size);
213 int res;
215 res = munmap(buf, page_size * mmap_window);
216 assert(res == 0);
218 offset += shift;
219 head -= shift;
220 goto remap;
223 size = event->header.size;
226 if (!size || process_event(event, offset, head) < 0) {
229 * assume we lost track of the stream, check alignment, and
230 * increment a single u64 in the hope to catch on again 'soon'.
233 if (unlikely(head & 7))
234 head &= ~7ULL;
236 size = 8;
239 head += size;
241 if (offset + head < (unsigned long)perf_stat.st_size)
242 goto more;
244 rc = EXIT_SUCCESS;
245 close(input);
247 return rc;
250 static const char * const annotate_usage[] = {
251 "perf trace [<options>] <command>",
252 NULL
255 static const struct option options[] = {
256 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
257 "dump raw trace in ASCII"),
258 OPT_BOOLEAN('v', "verbose", &verbose,
259 "be more verbose (show symbol address, etc)"),
260 OPT_END()
263 int cmd_trace(int argc, const char **argv, const char *prefix __used)
265 symbol__init();
266 page_size = getpagesize();
268 argc = parse_options(argc, argv, options, annotate_usage, 0);
269 if (argc) {
271 * Special case: if there's an argument left then assume tha
272 * it's a symbol filter:
274 if (argc > 1)
275 usage_with_options(annotate_usage, options);
279 setup_pager();
281 return __cmd_trace();