perf probe: Add argv_split() from lib/argv_split.c
[pandora-kernel.git] / 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 #include "util/data_map.h"
16
17 static char             const *input_name = "perf.data";
18
19 static struct perf_header *header;
20 static u64              sample_type;
21
22 static int process_sample_event(event_t *event)
23 {
24         u64 ip = event->ip.ip;
25         u64 timestamp = -1;
26         u32 cpu = -1;
27         u64 period = 1;
28         void *more_data = event->ip.__more_data;
29         struct thread *thread = threads__findnew(event->ip.pid);
30
31         if (sample_type & PERF_SAMPLE_TIME) {
32                 timestamp = *(u64 *)more_data;
33                 more_data += sizeof(u64);
34         }
35
36         if (sample_type & PERF_SAMPLE_CPU) {
37                 cpu = *(u32 *)more_data;
38                 more_data += sizeof(u32);
39                 more_data += sizeof(u32); /* reserved */
40         }
41
42         if (sample_type & PERF_SAMPLE_PERIOD) {
43                 period = *(u64 *)more_data;
44                 more_data += sizeof(u64);
45         }
46
47         dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
48                 event->header.misc,
49                 event->ip.pid, event->ip.tid,
50                 (void *)(long)ip,
51                 (long long)period);
52
53         if (thread == NULL) {
54                 pr_debug("problem processing %d event, skipping it.\n",
55                          event->header.type);
56                 return -1;
57         }
58
59         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
60
61         if (sample_type & PERF_SAMPLE_RAW) {
62                 struct {
63                         u32 size;
64                         char data[0];
65                 } *raw = more_data;
66
67                 /*
68                  * FIXME: better resolve from pid from the struct trace_entry
69                  * field, although it should be the same than this perf
70                  * event pid
71                  */
72                 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
73         }
74         event__stats.total += period;
75
76         return 0;
77 }
78
79 static int sample_type_check(u64 type)
80 {
81         sample_type = type;
82
83         if (!(sample_type & PERF_SAMPLE_RAW)) {
84                 fprintf(stderr,
85                         "No trace sample to read. Did you call perf record "
86                         "without -R?");
87                 return -1;
88         }
89
90         return 0;
91 }
92
93 static struct perf_file_handler file_handler = {
94         .process_sample_event   = process_sample_event,
95         .process_comm_event     = event__process_comm,
96         .sample_type_check      = sample_type_check,
97 };
98
99 static int __cmd_trace(void)
100 {
101         register_idle_thread();
102         register_perf_file_handler(&file_handler);
103
104         return mmap_dispatch_perf_file(&header, input_name,
105                                        0, 0, &event__cwdlen, &event__cwd);
106 }
107
108 static const char * const annotate_usage[] = {
109         "perf trace [<options>] <command>",
110         NULL
111 };
112
113 static const struct option options[] = {
114         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
115                     "dump raw trace in ASCII"),
116         OPT_BOOLEAN('v', "verbose", &verbose,
117                     "be more verbose (show symbol address, etc)"),
118         OPT_BOOLEAN('l', "latency", &latency_format,
119                     "show latency attributes (irqs/preemption disabled, etc)"),
120         OPT_END()
121 };
122
123 int cmd_trace(int argc, const char **argv, const char *prefix __used)
124 {
125         symbol__init(0);
126
127         argc = parse_options(argc, argv, options, annotate_usage, 0);
128         if (argc) {
129                 /*
130                  * Special case: if there's an argument left then assume tha
131                  * it's a symbol filter:
132                  */
133                 if (argc > 1)
134                         usage_with_options(annotate_usage, options);
135         }
136
137         setup_pager();
138
139         return __cmd_trace();
140 }