blob: e96bb534b9489d86ed0cf12ba712aee68b5a602b [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
Tom Zanussi956ffd02009-11-25 01:15:46 -06009static char const *script_name;
10static char const *generate_script_lang;
11
12static int default_start_script(const char *script __attribute((unused)))
13{
14 return 0;
15}
16
17static int default_stop_script(void)
18{
19 return 0;
20}
21
22static int default_generate_script(const char *outfile __attribute ((unused)))
23{
24 return 0;
25}
26
27static struct scripting_ops default_scripting_ops = {
28 .start_script = default_start_script,
29 .stop_script = default_stop_script,
30 .process_event = print_event,
31 .generate_script = default_generate_script,
32};
33
34static struct scripting_ops *scripting_ops;
35
36static void setup_scripting(void)
37{
38 /* make sure PERF_EXEC_PATH is set for scripts */
39 perf_set_argv_exec_path(perf_exec_path());
40
41 scripting_ops = &default_scripting_ops;
42}
43
44static int cleanup_scripting(void)
45{
46 return scripting_ops->stop_script();
47}
48
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020049#include "util/parse-options.h"
50
51#include "perf.h"
52#include "util/debug.h"
53
54#include "util/trace-event.h"
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020055#include "util/data_map.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060056#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020057
Tom Zanussi956ffd02009-11-25 01:15:46 -060058static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020059
Tom Zanussi956ffd02009-11-25 01:15:46 -060060static struct perf_header *header;
61static u64 sample_type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020062
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020063static int process_sample_event(event_t *event)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020064{
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020065 u64 ip = event->ip.ip;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020066 u64 timestamp = -1;
Ingo Molnarcd6feee2009-09-02 20:20:38 +020067 u32 cpu = -1;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068 u64 period = 1;
69 void *more_data = event->ip.__more_data;
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -030070 struct thread *thread = threads__findnew(event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
Ingo Molnar6ddf2592009-09-03 12:00:22 +020072 if (sample_type & PERF_SAMPLE_TIME) {
73 timestamp = *(u64 *)more_data;
74 more_data += sizeof(u64);
75 }
76
Ingo Molnarcd6feee2009-09-02 20:20:38 +020077 if (sample_type & PERF_SAMPLE_CPU) {
78 cpu = *(u32 *)more_data;
79 more_data += sizeof(u32);
80 more_data += sizeof(u32); /* reserved */
81 }
82
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083 if (sample_type & PERF_SAMPLE_PERIOD) {
84 period = *(u64 *)more_data;
85 more_data += sizeof(u64);
86 }
87
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020088 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020089 event->header.misc,
90 event->ip.pid, event->ip.tid,
91 (void *)(long)ip,
92 (long long)period);
93
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020094 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020095 pr_debug("problem processing %d event, skipping it.\n",
96 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020097 return -1;
98 }
99
Julia Lawallf39cdf22009-10-17 08:43:17 +0200100 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
101
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200102 if (sample_type & PERF_SAMPLE_RAW) {
103 struct {
104 u32 size;
105 char data[0];
106 } *raw = more_data;
107
108 /*
109 * FIXME: better resolve from pid from the struct trace_entry
110 * field, although it should be the same than this perf
111 * event pid
112 */
Tom Zanussi956ffd02009-11-25 01:15:46 -0600113 scripting_ops->process_event(cpu, raw->data, raw->size,
114 timestamp, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200115 }
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200116 event__stats.total += period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200117
118 return 0;
119}
120
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200121static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200122{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200123 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200124
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200125 if (!(sample_type & PERF_SAMPLE_RAW)) {
126 fprintf(stderr,
127 "No trace sample to read. Did you call perf record "
128 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200129 return -1;
130 }
131
132 return 0;
133}
134
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200135static struct perf_file_handler file_handler = {
136 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200137 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200138 .sample_type_check = sample_type_check,
139};
140
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200141static int __cmd_trace(void)
142{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300143 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200144 register_perf_file_handler(&file_handler);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200145
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200146 return mmap_dispatch_perf_file(&header, input_name,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200147 0, 0, &event__cwdlen, &event__cwd);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200148}
149
Tom Zanussi956ffd02009-11-25 01:15:46 -0600150struct script_spec {
151 struct list_head node;
152 struct scripting_ops *ops;
153 char spec[0];
154};
155
156LIST_HEAD(script_specs);
157
158static struct script_spec *script_spec__new(const char *spec,
159 struct scripting_ops *ops)
160{
161 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
162
163 if (s != NULL) {
164 strcpy(s->spec, spec);
165 s->ops = ops;
166 }
167
168 return s;
169}
170
171static void script_spec__delete(struct script_spec *s)
172{
173 free(s->spec);
174 free(s);
175}
176
177static void script_spec__add(struct script_spec *s)
178{
179 list_add_tail(&s->node, &script_specs);
180}
181
182static struct script_spec *script_spec__find(const char *spec)
183{
184 struct script_spec *s;
185
186 list_for_each_entry(s, &script_specs, node)
187 if (strcasecmp(s->spec, spec) == 0)
188 return s;
189 return NULL;
190}
191
192static struct script_spec *script_spec__findnew(const char *spec,
193 struct scripting_ops *ops)
194{
195 struct script_spec *s = script_spec__find(spec);
196
197 if (s)
198 return s;
199
200 s = script_spec__new(spec, ops);
201 if (!s)
202 goto out_delete_spec;
203
204 script_spec__add(s);
205
206 return s;
207
208out_delete_spec:
209 script_spec__delete(s);
210
211 return NULL;
212}
213
214int script_spec_register(const char *spec, struct scripting_ops *ops)
215{
216 struct script_spec *s;
217
218 s = script_spec__find(spec);
219 if (s)
220 return -1;
221
222 s = script_spec__findnew(spec, ops);
223 if (!s)
224 return -1;
225
226 return 0;
227}
228
229static struct scripting_ops *script_spec__lookup(const char *spec)
230{
231 struct script_spec *s = script_spec__find(spec);
232 if (!s)
233 return NULL;
234
235 return s->ops;
236}
237
238static void list_available_languages(void)
239{
240 struct script_spec *s;
241
242 fprintf(stderr, "\n");
243 fprintf(stderr, "Scripting language extensions (used in "
244 "perf trace -s [spec:]script.[spec]):\n\n");
245
246 list_for_each_entry(s, &script_specs, node)
247 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
248
249 fprintf(stderr, "\n");
250}
251
252static int parse_scriptname(const struct option *opt __used,
253 const char *str, int unset __used)
254{
255 char spec[PATH_MAX];
256 const char *script, *ext;
257 int len;
258
259 if (strcmp(str, "list") == 0) {
260 list_available_languages();
261 return 0;
262 }
263
264 script = strchr(str, ':');
265 if (script) {
266 len = script - str;
267 if (len >= PATH_MAX) {
268 fprintf(stderr, "invalid language specifier");
269 return -1;
270 }
271 strncpy(spec, str, len);
272 spec[len] = '\0';
273 scripting_ops = script_spec__lookup(spec);
274 if (!scripting_ops) {
275 fprintf(stderr, "invalid language specifier");
276 return -1;
277 }
278 script++;
279 } else {
280 script = str;
281 ext = strchr(script, '.');
282 if (!ext) {
283 fprintf(stderr, "invalid script extension");
284 return -1;
285 }
286 scripting_ops = script_spec__lookup(++ext);
287 if (!scripting_ops) {
288 fprintf(stderr, "invalid script extension");
289 return -1;
290 }
291 }
292
293 script_name = strdup(script);
294
295 return 0;
296}
297
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200298static const char * const annotate_usage[] = {
299 "perf trace [<options>] <command>",
300 NULL
301};
302
303static const struct option options[] = {
304 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
305 "dump raw trace in ASCII"),
306 OPT_BOOLEAN('v', "verbose", &verbose,
307 "be more verbose (show symbol address, etc)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400308 OPT_BOOLEAN('l', "latency", &latency_format,
309 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600310 OPT_CALLBACK('s', "script", NULL, "name",
311 "script file name (lang:script name, script name, or *)",
312 parse_scriptname),
313 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
314 "generate perf-trace.xx script in specified language"),
315
Masami Hiramatsu19096292009-08-21 14:56:03 -0400316 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200317};
318
319int cmd_trace(int argc, const char **argv, const char *prefix __used)
320{
Tom Zanussi956ffd02009-11-25 01:15:46 -0600321 int err;
322
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200323 symbol__init(0);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200324
Tom Zanussi956ffd02009-11-25 01:15:46 -0600325 setup_scripting();
326
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200327 argc = parse_options(argc, argv, options, annotate_usage, 0);
328 if (argc) {
329 /*
330 * Special case: if there's an argument left then assume tha
331 * it's a symbol filter:
332 */
333 if (argc > 1)
334 usage_with_options(annotate_usage, options);
335 }
336
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200337 setup_pager();
338
Tom Zanussi956ffd02009-11-25 01:15:46 -0600339 if (generate_script_lang) {
340 struct stat perf_stat;
341
342 int input = open(input_name, O_RDONLY);
343 if (input < 0) {
344 perror("failed to open file");
345 exit(-1);
346 }
347
348 err = fstat(input, &perf_stat);
349 if (err < 0) {
350 perror("failed to stat file");
351 exit(-1);
352 }
353
354 if (!perf_stat.st_size) {
355 fprintf(stderr, "zero-sized file, nothing to do!\n");
356 exit(0);
357 }
358
359 scripting_ops = script_spec__lookup(generate_script_lang);
360 if (!scripting_ops) {
361 fprintf(stderr, "invalid language specifier");
362 return -1;
363 }
364
365 header = perf_header__new();
366 if (header == NULL)
367 return -1;
368
369 perf_header__read(header, input);
370 err = scripting_ops->generate_script("perf-trace");
371 goto out;
372 }
373
374 if (script_name) {
375 err = scripting_ops->start_script(script_name);
376 if (err)
377 goto out;
378 }
379
380 err = __cmd_trace();
381
382 cleanup_scripting();
383out:
384 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200385}