blob: 4c129ff0bb162c10d7256c56253078b56b99998d [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#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"
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020015#include "util/data_map.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020016
17static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020018
19static unsigned long total = 0;
20static unsigned long total_comm = 0;
21
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020022static struct perf_header *header;
23static u64 sample_type;
24
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020025static char *cwd;
26static int cwdlen;
27
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020028
29static int
30process_comm_event(event_t *event, unsigned long offset, unsigned long head)
31{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030032 struct thread *thread = threads__findnew(event->comm.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020033
Ingo Molnarcdd6c482009-09-21 12:02:48 +020034 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020035 (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)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020042 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{
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020052 u64 ip = event->ip.ip;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020053 u64 timestamp = -1;
Ingo Molnarcd6feee2009-09-02 20:20:38 +020054 u32 cpu = -1;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020055 u64 period = 1;
56 void *more_data = event->ip.__more_data;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030057 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020058
Ingo Molnar6ddf2592009-09-03 12:00:22 +020059 if (sample_type & PERF_SAMPLE_TIME) {
60 timestamp = *(u64 *)more_data;
61 more_data += sizeof(u64);
62 }
63
Ingo Molnarcd6feee2009-09-02 20:20:38 +020064 if (sample_type & PERF_SAMPLE_CPU) {
65 cpu = *(u32 *)more_data;
66 more_data += sizeof(u32);
67 more_data += sizeof(u32); /* reserved */
68 }
69
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020070 if (sample_type & PERF_SAMPLE_PERIOD) {
71 period = *(u64 *)more_data;
72 more_data += sizeof(u64);
73 }
74
Ingo Molnarcdd6c482009-09-21 12:02:48 +020075 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020076 (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
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083 if (thread == NULL) {
84 eprintf("problem processing %d event, skipping it.\n",
85 event->header.type);
86 return -1;
87 }
88
Julia Lawallf39cdf22009-10-17 08:43:17 +020089 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
90
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020091 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 */
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200102 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200103 }
104 total += period;
105
106 return 0;
107}
108
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200109static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200110{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200111 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200112
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200113 if (!(sample_type & PERF_SAMPLE_RAW)) {
114 fprintf(stderr,
115 "No trace sample to read. Did you call perf record "
116 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200117 return -1;
118 }
119
120 return 0;
121}
122
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200123static 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
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200129static int __cmd_trace(void)
130{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300131 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200132 register_perf_file_handler(&file_handler);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200133
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200134 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200135}
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)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400147 OPT_BOOLEAN('l', "latency", &latency_format,
148 "show latency attributes (irqs/preemption disabled, etc)"),
Masami Hiramatsu19096292009-08-21 14:56:03 -0400149 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200150};
151
152int cmd_trace(int argc, const char **argv, const char *prefix __used)
153{
154 symbol__init();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200155
156 argc = parse_options(argc, argv, options, annotate_usage, 0);
157 if (argc) {
158 /*
159 * Special case: if there's an argument left then assume tha
160 * it's a symbol filter:
161 */
162 if (argc > 1)
163 usage_with_options(annotate_usage, options);
164 }
165
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200166 setup_pager();
167
168 return __cmd_trace();
169}