blob: fb3f3c220211839c8eb79aaa6765749ef87ef806 [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
22static struct rb_root threads;
23static struct thread *last_match;
24
25static struct perf_header *header;
26static u64 sample_type;
27
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020028static char *cwd;
29static int cwdlen;
30
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020031
32static int
33process_comm_event(event_t *event, unsigned long offset, unsigned long head)
34{
35 struct thread *thread;
36
37 thread = threads__findnew(event->comm.pid, &threads, &last_match);
38
Ingo Molnarcdd6c482009-09-21 12:02:48 +020039 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020040 (void *)(offset + head),
41 (void *)(long)(event->header.size),
42 event->comm.comm, event->comm.pid);
43
44 if (thread == NULL ||
45 thread__set_comm(thread, event->comm.comm)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020046 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020047 return -1;
48 }
49 total_comm++;
50
51 return 0;
52}
53
54static int
55process_sample_event(event_t *event, unsigned long offset, unsigned long head)
56{
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020057 struct thread *thread;
58 u64 ip = event->ip.ip;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020059 u64 timestamp = -1;
Ingo Molnarcd6feee2009-09-02 20:20:38 +020060 u32 cpu = -1;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020061 u64 period = 1;
62 void *more_data = event->ip.__more_data;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020063
64 thread = threads__findnew(event->ip.pid, &threads, &last_match);
65
Ingo Molnar6ddf2592009-09-03 12:00:22 +020066 if (sample_type & PERF_SAMPLE_TIME) {
67 timestamp = *(u64 *)more_data;
68 more_data += sizeof(u64);
69 }
70
Ingo Molnarcd6feee2009-09-02 20:20:38 +020071 if (sample_type & PERF_SAMPLE_CPU) {
72 cpu = *(u32 *)more_data;
73 more_data += sizeof(u32);
74 more_data += sizeof(u32); /* reserved */
75 }
76
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020077 if (sample_type & PERF_SAMPLE_PERIOD) {
78 period = *(u64 *)more_data;
79 more_data += sizeof(u64);
80 }
81
Ingo Molnarcdd6c482009-09-21 12:02:48 +020082 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083 (void *)(offset + head),
84 (void *)(long)(event->header.size),
85 event->header.misc,
86 event->ip.pid, event->ip.tid,
87 (void *)(long)ip,
88 (long long)period);
89
90 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
91
92 if (thread == NULL) {
93 eprintf("problem processing %d event, skipping it.\n",
94 event->header.type);
95 return -1;
96 }
97
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020098 if (sample_type & PERF_SAMPLE_RAW) {
99 struct {
100 u32 size;
101 char data[0];
102 } *raw = more_data;
103
104 /*
105 * FIXME: better resolve from pid from the struct trace_entry
106 * field, although it should be the same than this perf
107 * event pid
108 */
Ingo Molnar6ddf2592009-09-03 12:00:22 +0200109 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200110 }
111 total += period;
112
113 return 0;
114}
115
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200116static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200117{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200118 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200119
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200120 if (!(sample_type & PERF_SAMPLE_RAW)) {
121 fprintf(stderr,
122 "No trace sample to read. Did you call perf record "
123 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200124 return -1;
125 }
126
127 return 0;
128}
129
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200130static struct perf_file_handler file_handler = {
131 .process_sample_event = process_sample_event,
132 .process_comm_event = process_comm_event,
133 .sample_type_check = sample_type_check,
134};
135
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200136static int __cmd_trace(void)
137{
Frederic Weisbecker3a2684c2009-08-31 06:45:19 +0200138 register_idle_thread(&threads, &last_match);
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200139 register_perf_file_handler(&file_handler);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200140
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200141 return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200142}
143
144static const char * const annotate_usage[] = {
145 "perf trace [<options>] <command>",
146 NULL
147};
148
149static const struct option options[] = {
150 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
151 "dump raw trace in ASCII"),
152 OPT_BOOLEAN('v', "verbose", &verbose,
153 "be more verbose (show symbol address, etc)"),
Masami Hiramatsu19096292009-08-21 14:56:03 -0400154 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200155};
156
157int cmd_trace(int argc, const char **argv, const char *prefix __used)
158{
159 symbol__init();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200160
161 argc = parse_options(argc, argv, options, annotate_usage, 0);
162 if (argc) {
163 /*
164 * Special case: if there's an argument left then assume tha
165 * it's a symbol filter:
166 */
167 if (argc > 1)
168 usage_with_options(annotate_usage, options);
169 }
170
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200171 setup_pager();
172
173 return __cmd_trace();
174}