perf tools: Consolidate symbol resolving across all tools
[linux-block.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
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"
016e92fb 15#include "util/data_map.h"
5f9c39dc
FW
16
17static char const *input_name = "perf.data";
5f9c39dc 18
5f9c39dc
FW
19static struct perf_header *header;
20static u64 sample_type;
21
62daacb5 22static int process_sample_event(event_t *event)
5f9c39dc 23{
5f9c39dc 24 u64 ip = event->ip.ip;
6ddf259d 25 u64 timestamp = -1;
cd6feeea 26 u32 cpu = -1;
5f9c39dc
FW
27 u64 period = 1;
28 void *more_data = event->ip.__more_data;
d5b889f2 29 struct thread *thread = threads__findnew(event->ip.pid);
5f9c39dc 30
6ddf259d
IM
31 if (sample_type & PERF_SAMPLE_TIME) {
32 timestamp = *(u64 *)more_data;
33 more_data += sizeof(u64);
34 }
35
cd6feeea
IM
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
5f9c39dc
FW
42 if (sample_type & PERF_SAMPLE_PERIOD) {
43 period = *(u64 *)more_data;
44 more_data += sizeof(u64);
45 }
46
62daacb5 47 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
5f9c39dc
FW
48 event->header.misc,
49 event->ip.pid, event->ip.tid,
50 (void *)(long)ip,
51 (long long)period);
52
5f9c39dc 53 if (thread == NULL) {
6beba7ad
ACM
54 pr_debug("problem processing %d event, skipping it.\n",
55 event->header.type);
5f9c39dc
FW
56 return -1;
57 }
58
f39cdf25
JL
59 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
60
5f9c39dc
FW
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 */
6ddf259d 72 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
5f9c39dc 73 }
62daacb5 74 event__stats.total += period;
5f9c39dc
FW
75
76 return 0;
77}
78
016e92fb 79static int sample_type_check(u64 type)
5f9c39dc 80{
016e92fb 81 sample_type = type;
5f9c39dc 82
016e92fb
FW
83 if (!(sample_type & PERF_SAMPLE_RAW)) {
84 fprintf(stderr,
85 "No trace sample to read. Did you call perf record "
86 "without -R?");
5f9c39dc
FW
87 return -1;
88 }
89
90 return 0;
91}
92
016e92fb
FW
93static struct perf_file_handler file_handler = {
94 .process_sample_event = process_sample_event,
62daacb5 95 .process_comm_event = event__process_comm,
016e92fb
FW
96 .sample_type_check = sample_type_check,
97};
98
5f9c39dc
FW
99static int __cmd_trace(void)
100{
d5b889f2 101 register_idle_thread();
016e92fb 102 register_perf_file_handler(&file_handler);
5f9c39dc 103
b32d133a 104 return mmap_dispatch_perf_file(&header, input_name,
62daacb5 105 0, 0, &event__cwdlen, &event__cwd);
5f9c39dc
FW
106}
107
108static const char * const annotate_usage[] = {
109 "perf trace [<options>] <command>",
110 NULL
111};
112
113static 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)"),
cda48461
SR
118 OPT_BOOLEAN('l', "latency", &latency_format,
119 "show latency attributes (irqs/preemption disabled, etc)"),
1909629f 120 OPT_END()
5f9c39dc
FW
121};
122
123int cmd_trace(int argc, const char **argv, const char *prefix __used)
124{
00a192b3 125 symbol__init(0);
5f9c39dc
FW
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
5f9c39dc
FW
137 setup_pager();
138
139 return __cmd_trace();
140}