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_EVENT_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_EVENT_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;
63 void *more_data = event->ip.__more_data;
66 thread = threads__findnew(event->ip.pid, &threads, &last_match);
68 if (sample_type & PERF_SAMPLE_CPU) {
69 cpu = *(u32 *)more_data;
70 more_data += sizeof(u32);
71 more_data += sizeof(u32); /* reserved */
74 if (sample_type & PERF_SAMPLE_PERIOD) {
75 period = *(u64 *)more_data;
76 more_data += sizeof(u64);
79 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
80 (void *)(offset + head),
81 (void *)(long)(event->header.size),
83 event->ip.pid, event->ip.tid,
87 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
90 eprintf("problem processing %d event, skipping it.\n",
95 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
97 if (cpumode == PERF_EVENT_MISC_KERNEL) {
103 dump_printf(" ...... dso: %s\n", dso->name);
105 } else if (cpumode == PERF_EVENT_MISC_USER) {
114 dso = hypervisor_dso;
116 dump_printf(" ...... dso: [hypervisor]\n");
119 if (sample_type & PERF_SAMPLE_RAW) {
126 * FIXME: better resolve from pid from the struct trace_entry
127 * field, although it should be the same than this perf
130 print_event(cpu, raw->data, raw->size, 0, thread->comm);
138 process_event(event_t *event, unsigned long offset, unsigned long head)
142 switch (event->header.type) {
143 case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
146 case PERF_EVENT_COMM:
147 return process_comm_event(event, offset, head);
149 case PERF_EVENT_EXIT ... PERF_EVENT_READ:
152 case PERF_EVENT_SAMPLE:
153 return process_sample_event(event, offset, head);
163 static int __cmd_trace(void)
165 int ret, rc = EXIT_FAILURE;
166 unsigned long offset = 0;
167 unsigned long head = 0;
168 struct stat perf_stat;
174 register_idle_thread(&threads, &last_match);
176 input = open(input_name, O_RDONLY);
178 perror("failed to open file");
182 ret = fstat(input, &perf_stat);
184 perror("failed to stat file");
188 if (!perf_stat.st_size) {
189 fprintf(stderr, "zero-sized file, nothing to do!\n");
192 header = perf_header__read(input);
193 sample_type = perf_header__sample_type(header);
195 if (!(sample_type & PERF_SAMPLE_RAW))
196 die("No trace sample to read. Did you call perf record "
199 if (load_kernel() < 0) {
200 perror("failed to load kernel symbols");
205 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
206 MAP_SHARED, input, offset);
207 if (buf == MAP_FAILED) {
208 perror("failed to mmap file");
213 event = (event_t *)(buf + head);
215 size = event->header.size;
219 if (head + event->header.size >= page_size * mmap_window) {
220 unsigned long shift = page_size * (head / page_size);
223 res = munmap(buf, page_size * mmap_window);
231 size = event->header.size;
234 if (!size || process_event(event, offset, head) < 0) {
237 * assume we lost track of the stream, check alignment, and
238 * increment a single u64 in the hope to catch on again 'soon'.
241 if (unlikely(head & 7))
249 if (offset + head < (unsigned long)perf_stat.st_size)
258 static const char * const annotate_usage[] = {
259 "perf trace [<options>] <command>",
263 static const struct option options[] = {
264 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
265 "dump raw trace in ASCII"),
266 OPT_BOOLEAN('v', "verbose", &verbose,
267 "be more verbose (show symbol address, etc)"),
271 int cmd_trace(int argc, const char **argv, const char *prefix __used)
274 page_size = getpagesize();
276 argc = parse_options(argc, argv, options, annotate_usage, 0);
279 * Special case: if there's an argument left then assume tha
280 * it's a symbol filter:
283 usage_with_options(annotate_usage, options);
289 return __cmd_trace();