blob: 0756664666f1021289ac3f610bc06ab685ecac12 [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"
Ingo Molnarcf723442009-11-28 10:11:00 +01008#include "util/exec_cmd.h"
9#include "util/trace-event.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020010#include "util/session.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020011
Tom Zanussi956ffd02009-11-25 01:15:46 -060012static char const *script_name;
13static char const *generate_script_lang;
14
15static int default_start_script(const char *script __attribute((unused)))
16{
17 return 0;
18}
19
20static int default_stop_script(void)
21{
22 return 0;
23}
24
25static int default_generate_script(const char *outfile __attribute ((unused)))
26{
27 return 0;
28}
29
30static struct scripting_ops default_scripting_ops = {
31 .start_script = default_start_script,
32 .stop_script = default_stop_script,
33 .process_event = print_event,
34 .generate_script = default_generate_script,
35};
36
37static struct scripting_ops *scripting_ops;
38
39static void setup_scripting(void)
40{
41 /* make sure PERF_EXEC_PATH is set for scripts */
42 perf_set_argv_exec_path(perf_exec_path());
43
Tom Zanussi16c632d2009-11-25 01:15:48 -060044 setup_perl_scripting();
45
Tom Zanussi956ffd02009-11-25 01:15:46 -060046 scripting_ops = &default_scripting_ops;
47}
48
49static int cleanup_scripting(void)
50{
51 return scripting_ops->stop_script();
52}
53
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020054#include "util/parse-options.h"
55
56#include "perf.h"
57#include "util/debug.h"
58
59#include "util/trace-event.h"
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020060#include "util/data_map.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060061#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020062
Tom Zanussi956ffd02009-11-25 01:15:46 -060063static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020064
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020065static struct perf_session *session;
Tom Zanussi956ffd02009-11-25 01:15:46 -060066static u64 sample_type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020067
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020068static int process_sample_event(event_t *event)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020069{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090070 struct sample_data data;
71 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020072
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090073 memset(&data, 0, sizeof(data));
74 data.time = -1;
75 data.cpu = -1;
76 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020077
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090078 event__parse_sample(event, sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020079
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020080 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020081 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090082 data.pid, data.tid,
83 (void *)(long)data.ip,
84 (long long)data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020085
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090086 thread = threads__findnew(event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020087 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020088 pr_debug("problem processing %d event, skipping it.\n",
89 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020090 return -1;
91 }
92
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020093 if (sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020094 /*
95 * FIXME: better resolve from pid from the struct trace_entry
96 * field, although it should be the same than this perf
97 * event pid
98 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090099 scripting_ops->process_event(data.cpu, data.raw_data,
100 data.raw_size,
101 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200102 }
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900103 event__stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200104
105 return 0;
106}
107
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200108static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200109{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200110 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200111
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200112 if (!(sample_type & PERF_SAMPLE_RAW)) {
113 fprintf(stderr,
114 "No trace sample to read. Did you call perf record "
115 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200116 return -1;
117 }
118
119 return 0;
120}
121
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200122static struct perf_file_handler file_handler = {
123 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200124 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200125 .sample_type_check = sample_type_check,
126};
127
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200128static int __cmd_trace(void)
129{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200130 int err;
131
132 session = perf_session__new(input_name, O_RDONLY, 0);
133 if (session == NULL)
134 return -ENOMEM;
135
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300136 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200137 register_perf_file_handler(&file_handler);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200138
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200139 err = perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
140 perf_session__delete(session);
141 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200142}
143
Tom Zanussi956ffd02009-11-25 01:15:46 -0600144struct script_spec {
145 struct list_head node;
146 struct scripting_ops *ops;
147 char spec[0];
148};
149
150LIST_HEAD(script_specs);
151
152static struct script_spec *script_spec__new(const char *spec,
153 struct scripting_ops *ops)
154{
155 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
156
157 if (s != NULL) {
158 strcpy(s->spec, spec);
159 s->ops = ops;
160 }
161
162 return s;
163}
164
165static void script_spec__delete(struct script_spec *s)
166{
167 free(s->spec);
168 free(s);
169}
170
171static void script_spec__add(struct script_spec *s)
172{
173 list_add_tail(&s->node, &script_specs);
174}
175
176static struct script_spec *script_spec__find(const char *spec)
177{
178 struct script_spec *s;
179
180 list_for_each_entry(s, &script_specs, node)
181 if (strcasecmp(s->spec, spec) == 0)
182 return s;
183 return NULL;
184}
185
186static struct script_spec *script_spec__findnew(const char *spec,
187 struct scripting_ops *ops)
188{
189 struct script_spec *s = script_spec__find(spec);
190
191 if (s)
192 return s;
193
194 s = script_spec__new(spec, ops);
195 if (!s)
196 goto out_delete_spec;
197
198 script_spec__add(s);
199
200 return s;
201
202out_delete_spec:
203 script_spec__delete(s);
204
205 return NULL;
206}
207
208int script_spec_register(const char *spec, struct scripting_ops *ops)
209{
210 struct script_spec *s;
211
212 s = script_spec__find(spec);
213 if (s)
214 return -1;
215
216 s = script_spec__findnew(spec, ops);
217 if (!s)
218 return -1;
219
220 return 0;
221}
222
223static struct scripting_ops *script_spec__lookup(const char *spec)
224{
225 struct script_spec *s = script_spec__find(spec);
226 if (!s)
227 return NULL;
228
229 return s->ops;
230}
231
232static void list_available_languages(void)
233{
234 struct script_spec *s;
235
236 fprintf(stderr, "\n");
237 fprintf(stderr, "Scripting language extensions (used in "
238 "perf trace -s [spec:]script.[spec]):\n\n");
239
240 list_for_each_entry(s, &script_specs, node)
241 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
242
243 fprintf(stderr, "\n");
244}
245
246static int parse_scriptname(const struct option *opt __used,
247 const char *str, int unset __used)
248{
249 char spec[PATH_MAX];
250 const char *script, *ext;
251 int len;
252
253 if (strcmp(str, "list") == 0) {
254 list_available_languages();
255 return 0;
256 }
257
258 script = strchr(str, ':');
259 if (script) {
260 len = script - str;
261 if (len >= PATH_MAX) {
262 fprintf(stderr, "invalid language specifier");
263 return -1;
264 }
265 strncpy(spec, str, len);
266 spec[len] = '\0';
267 scripting_ops = script_spec__lookup(spec);
268 if (!scripting_ops) {
269 fprintf(stderr, "invalid language specifier");
270 return -1;
271 }
272 script++;
273 } else {
274 script = str;
275 ext = strchr(script, '.');
276 if (!ext) {
277 fprintf(stderr, "invalid script extension");
278 return -1;
279 }
280 scripting_ops = script_spec__lookup(++ext);
281 if (!scripting_ops) {
282 fprintf(stderr, "invalid script extension");
283 return -1;
284 }
285 }
286
287 script_name = strdup(script);
288
289 return 0;
290}
291
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200292static const char * const annotate_usage[] = {
293 "perf trace [<options>] <command>",
294 NULL
295};
296
297static const struct option options[] = {
298 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
299 "dump raw trace in ASCII"),
300 OPT_BOOLEAN('v', "verbose", &verbose,
301 "be more verbose (show symbol address, etc)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400302 OPT_BOOLEAN('l', "latency", &latency_format,
303 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600304 OPT_CALLBACK('s', "script", NULL, "name",
305 "script file name (lang:script name, script name, or *)",
306 parse_scriptname),
307 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
308 "generate perf-trace.xx script in specified language"),
309
Masami Hiramatsu19096292009-08-21 14:56:03 -0400310 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200311};
312
313int cmd_trace(int argc, const char **argv, const char *prefix __used)
314{
Tom Zanussi956ffd02009-11-25 01:15:46 -0600315 int err;
316
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200317 symbol__init(0);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200318
Tom Zanussi956ffd02009-11-25 01:15:46 -0600319 setup_scripting();
320
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200321 argc = parse_options(argc, argv, options, annotate_usage, 0);
322 if (argc) {
323 /*
324 * Special case: if there's an argument left then assume tha
325 * it's a symbol filter:
326 */
327 if (argc > 1)
328 usage_with_options(annotate_usage, options);
329 }
330
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200331 setup_pager();
332
Tom Zanussi956ffd02009-11-25 01:15:46 -0600333 if (generate_script_lang) {
334 struct stat perf_stat;
335
336 int input = open(input_name, O_RDONLY);
337 if (input < 0) {
338 perror("failed to open file");
339 exit(-1);
340 }
341
342 err = fstat(input, &perf_stat);
343 if (err < 0) {
344 perror("failed to stat file");
345 exit(-1);
346 }
347
348 if (!perf_stat.st_size) {
349 fprintf(stderr, "zero-sized file, nothing to do!\n");
350 exit(0);
351 }
352
353 scripting_ops = script_spec__lookup(generate_script_lang);
354 if (!scripting_ops) {
355 fprintf(stderr, "invalid language specifier");
356 return -1;
357 }
358
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200359 perf_header__read(&session->header, input);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600360 err = scripting_ops->generate_script("perf-trace");
361 goto out;
362 }
363
364 if (script_name) {
365 err = scripting_ops->start_script(script_name);
366 if (err)
367 goto out;
368 }
369
370 err = __cmd_trace();
371
372 cleanup_scripting();
373out:
374 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200375}