]> Pileus Git - ~andy/linux/blob - tools/perf/builtin-trace.c
5d4c84d86373753718b1590636969c9acf9d2abf
[~andy/linux] / tools / perf / builtin-trace.c
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"
15
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;
20
21 static unsigned long    total = 0;
22 static unsigned long    total_comm = 0;
23
24 static struct rb_root   threads;
25 static struct thread    *last_match;
26
27 static struct perf_header *header;
28 static u64              sample_type;
29
30
31 static int
32 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
33 {
34         struct thread *thread;
35
36         thread = threads__findnew(event->comm.pid, &threads, &last_match);
37
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);
42
43         if (thread == NULL ||
44             thread__set_comm(thread, event->comm.comm)) {
45                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
46                 return -1;
47         }
48         total_comm++;
49
50         return 0;
51 }
52
53 static int
54 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
55 {
56         struct thread *thread;
57         u64 ip = event->ip.ip;
58         u64 timestamp = -1;
59         u32 cpu = -1;
60         u64 period = 1;
61         void *more_data = event->ip.__more_data;
62
63         thread = threads__findnew(event->ip.pid, &threads, &last_match);
64
65         if (sample_type & PERF_SAMPLE_TIME) {
66                 timestamp = *(u64 *)more_data;
67                 more_data += sizeof(u64);
68         }
69
70         if (sample_type & PERF_SAMPLE_CPU) {
71                 cpu = *(u32 *)more_data;
72                 more_data += sizeof(u32);
73                 more_data += sizeof(u32); /* reserved */
74         }
75
76         if (sample_type & PERF_SAMPLE_PERIOD) {
77                 period = *(u64 *)more_data;
78                 more_data += sizeof(u64);
79         }
80
81         dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
82                 (void *)(offset + head),
83                 (void *)(long)(event->header.size),
84                 event->header.misc,
85                 event->ip.pid, event->ip.tid,
86                 (void *)(long)ip,
87                 (long long)period);
88
89         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
90
91         if (thread == NULL) {
92                 eprintf("problem processing %d event, skipping it.\n",
93                         event->header.type);
94                 return -1;
95         }
96
97         if (sample_type & PERF_SAMPLE_RAW) {
98                 struct {
99                         u32 size;
100                         char data[0];
101                 } *raw = more_data;
102
103                 /*
104                  * FIXME: better resolve from pid from the struct trace_entry
105                  * field, although it should be the same than this perf
106                  * event pid
107                  */
108                 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
109         }
110         total += period;
111
112         return 0;
113 }
114
115 static int
116 process_event(event_t *event, unsigned long offset, unsigned long head)
117 {
118         trace_event(event);
119
120         switch (event->header.type) {
121         case PERF_RECORD_MMAP ... PERF_RECORD_LOST:
122                 return 0;
123
124         case PERF_RECORD_COMM:
125                 return process_comm_event(event, offset, head);
126
127         case PERF_RECORD_EXIT ... PERF_RECORD_READ:
128                 return 0;
129
130         case PERF_RECORD_SAMPLE:
131                 return process_sample_event(event, offset, head);
132
133         case PERF_RECORD_MAX:
134         default:
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 static int __cmd_trace(void)
142 {
143         int ret, rc = EXIT_FAILURE;
144         unsigned long offset = 0;
145         unsigned long head = 0;
146         struct stat perf_stat;
147         event_t *event;
148         uint32_t size;
149         char *buf;
150
151         trace_report();
152         register_idle_thread(&threads, &last_match);
153
154         input = open(input_name, O_RDONLY);
155         if (input < 0) {
156                 perror("failed to open file");
157                 exit(-1);
158         }
159
160         ret = fstat(input, &perf_stat);
161         if (ret < 0) {
162                 perror("failed to stat file");
163                 exit(-1);
164         }
165
166         if (!perf_stat.st_size) {
167                 fprintf(stderr, "zero-sized file, nothing to do!\n");
168                 exit(0);
169         }
170         header = perf_header__read(input);
171         head = header->data_offset;
172         sample_type = perf_header__sample_type(header);
173
174         if (!(sample_type & PERF_SAMPLE_RAW))
175                 die("No trace sample to read. Did you call perf record "
176                     "without -R?");
177
178         if (load_kernel() < 0) {
179                 perror("failed to load kernel symbols");
180                 return EXIT_FAILURE;
181         }
182
183 remap:
184         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
185                            MAP_SHARED, input, offset);
186         if (buf == MAP_FAILED) {
187                 perror("failed to mmap file");
188                 exit(-1);
189         }
190
191 more:
192         event = (event_t *)(buf + head);
193
194         if (head + event->header.size >= page_size * mmap_window) {
195                 unsigned long shift = page_size * (head / page_size);
196                 int res;
197
198                 res = munmap(buf, page_size * mmap_window);
199                 assert(res == 0);
200
201                 offset += shift;
202                 head -= shift;
203                 goto remap;
204         }
205
206         size = event->header.size;
207
208         if (!size || process_event(event, offset, head) < 0) {
209
210                 /*
211                  * assume we lost track of the stream, check alignment, and
212                  * increment a single u64 in the hope to catch on again 'soon'.
213                  */
214
215                 if (unlikely(head & 7))
216                         head &= ~7ULL;
217
218                 size = 8;
219         }
220
221         head += size;
222
223         if (offset + head < (unsigned long)perf_stat.st_size)
224                 goto more;
225
226         rc = EXIT_SUCCESS;
227         close(input);
228
229         return rc;
230 }
231
232 static const char * const annotate_usage[] = {
233         "perf trace [<options>] <command>",
234         NULL
235 };
236
237 static const struct option options[] = {
238         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
239                     "dump raw trace in ASCII"),
240         OPT_BOOLEAN('v', "verbose", &verbose,
241                     "be more verbose (show symbol address, etc)"),
242         OPT_END()
243 };
244
245 int cmd_trace(int argc, const char **argv, const char *prefix __used)
246 {
247         symbol__init();
248         page_size = getpagesize();
249
250         argc = parse_options(argc, argv, options, annotate_usage, 0);
251         if (argc) {
252                 /*
253                  * Special case: if there's an argument left then assume tha
254                  * it's a symbol filter:
255                  */
256                 if (argc > 1)
257                         usage_with_options(annotate_usage, options);
258         }
259
260         setup_pager();
261
262         return __cmd_trace();
263 }