blob: 1831434aa9387b9feaac7209a378737602706d32 [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
Tom Zanussi586bc5c2009-12-15 02:53:35 -060015static int default_start_script(const char *script __unused,
16 int argc __unused,
17 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060018{
19 return 0;
20}
21
22static int default_stop_script(void)
23{
24 return 0;
25}
26
Tom Zanussi586bc5c2009-12-15 02:53:35 -060027static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060028{
29 return 0;
30}
31
32static struct scripting_ops default_scripting_ops = {
33 .start_script = default_start_script,
34 .stop_script = default_stop_script,
35 .process_event = print_event,
36 .generate_script = default_generate_script,
37};
38
39static struct scripting_ops *scripting_ops;
40
41static void setup_scripting(void)
42{
43 /* make sure PERF_EXEC_PATH is set for scripts */
44 perf_set_argv_exec_path(perf_exec_path());
45
Tom Zanussi16c632d2009-11-25 01:15:48 -060046 setup_perl_scripting();
47
Tom Zanussi956ffd02009-11-25 01:15:46 -060048 scripting_ops = &default_scripting_ops;
49}
50
51static int cleanup_scripting(void)
52{
53 return scripting_ops->stop_script();
54}
55
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020056#include "util/parse-options.h"
57
58#include "perf.h"
59#include "util/debug.h"
60
61#include "util/trace-event.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060062#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020063
Tom Zanussi956ffd02009-11-25 01:15:46 -060064static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020065
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020066static int process_sample_event(event_t *event, struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020067{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090068 struct sample_data data;
69 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020070
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090071 memset(&data, 0, sizeof(data));
72 data.time = -1;
73 data.cpu = -1;
74 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020075
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020076 event__parse_sample(event, session->sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020077
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020078 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020079 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090080 data.pid, data.tid,
81 (void *)(long)data.ip,
82 (long long)data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020084 thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020085 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020086 pr_debug("problem processing %d event, skipping it.\n",
87 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020088 return -1;
89 }
90
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020091 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020092 /*
93 * FIXME: better resolve from pid from the struct trace_entry
94 * field, although it should be the same than this perf
95 * event pid
96 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090097 scripting_ops->process_event(data.cpu, data.raw_data,
98 data.raw_size,
99 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200100 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200101
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200102 session->events_stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200103 return 0;
104}
105
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200106static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200107 .sample = process_sample_event,
108 .comm = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200109};
110
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200111static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200112{
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200113 return perf_session__process_events(session, &event_ops);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200114}
115
Tom Zanussi956ffd02009-11-25 01:15:46 -0600116struct script_spec {
117 struct list_head node;
118 struct scripting_ops *ops;
119 char spec[0];
120};
121
122LIST_HEAD(script_specs);
123
124static struct script_spec *script_spec__new(const char *spec,
125 struct scripting_ops *ops)
126{
127 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
128
129 if (s != NULL) {
130 strcpy(s->spec, spec);
131 s->ops = ops;
132 }
133
134 return s;
135}
136
137static void script_spec__delete(struct script_spec *s)
138{
139 free(s->spec);
140 free(s);
141}
142
143static void script_spec__add(struct script_spec *s)
144{
145 list_add_tail(&s->node, &script_specs);
146}
147
148static struct script_spec *script_spec__find(const char *spec)
149{
150 struct script_spec *s;
151
152 list_for_each_entry(s, &script_specs, node)
153 if (strcasecmp(s->spec, spec) == 0)
154 return s;
155 return NULL;
156}
157
158static struct script_spec *script_spec__findnew(const char *spec,
159 struct scripting_ops *ops)
160{
161 struct script_spec *s = script_spec__find(spec);
162
163 if (s)
164 return s;
165
166 s = script_spec__new(spec, ops);
167 if (!s)
168 goto out_delete_spec;
169
170 script_spec__add(s);
171
172 return s;
173
174out_delete_spec:
175 script_spec__delete(s);
176
177 return NULL;
178}
179
180int script_spec_register(const char *spec, struct scripting_ops *ops)
181{
182 struct script_spec *s;
183
184 s = script_spec__find(spec);
185 if (s)
186 return -1;
187
188 s = script_spec__findnew(spec, ops);
189 if (!s)
190 return -1;
191
192 return 0;
193}
194
195static struct scripting_ops *script_spec__lookup(const char *spec)
196{
197 struct script_spec *s = script_spec__find(spec);
198 if (!s)
199 return NULL;
200
201 return s->ops;
202}
203
204static void list_available_languages(void)
205{
206 struct script_spec *s;
207
208 fprintf(stderr, "\n");
209 fprintf(stderr, "Scripting language extensions (used in "
210 "perf trace -s [spec:]script.[spec]):\n\n");
211
212 list_for_each_entry(s, &script_specs, node)
213 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
214
215 fprintf(stderr, "\n");
216}
217
218static int parse_scriptname(const struct option *opt __used,
219 const char *str, int unset __used)
220{
221 char spec[PATH_MAX];
222 const char *script, *ext;
223 int len;
224
225 if (strcmp(str, "list") == 0) {
226 list_available_languages();
227 return 0;
228 }
229
230 script = strchr(str, ':');
231 if (script) {
232 len = script - str;
233 if (len >= PATH_MAX) {
234 fprintf(stderr, "invalid language specifier");
235 return -1;
236 }
237 strncpy(spec, str, len);
238 spec[len] = '\0';
239 scripting_ops = script_spec__lookup(spec);
240 if (!scripting_ops) {
241 fprintf(stderr, "invalid language specifier");
242 return -1;
243 }
244 script++;
245 } else {
246 script = str;
247 ext = strchr(script, '.');
248 if (!ext) {
249 fprintf(stderr, "invalid script extension");
250 return -1;
251 }
252 scripting_ops = script_spec__lookup(++ext);
253 if (!scripting_ops) {
254 fprintf(stderr, "invalid script extension");
255 return -1;
256 }
257 }
258
259 script_name = strdup(script);
260
261 return 0;
262}
263
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600264#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
265 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
266 lang_next) \
267 if (lang_dirent.d_type == DT_DIR && \
268 (strcmp(lang_dirent.d_name, ".")) && \
269 (strcmp(lang_dirent.d_name, "..")))
270
271#define for_each_script(lang_dir, script_dirent, script_next) \
272 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
273 script_next) \
274 if (script_dirent.d_type != DT_DIR)
275
276
277#define RECORD_SUFFIX "-record"
278#define REPORT_SUFFIX "-report"
279
280struct script_desc {
281 struct list_head node;
282 char *name;
283 char *half_liner;
284 char *args;
285};
286
287LIST_HEAD(script_descs);
288
289static struct script_desc *script_desc__new(const char *name)
290{
291 struct script_desc *s = zalloc(sizeof(*s));
292
293 if (s != NULL)
294 s->name = strdup(name);
295
296 return s;
297}
298
299static void script_desc__delete(struct script_desc *s)
300{
301 free(s->name);
302 free(s);
303}
304
305static void script_desc__add(struct script_desc *s)
306{
307 list_add_tail(&s->node, &script_descs);
308}
309
310static struct script_desc *script_desc__find(const char *name)
311{
312 struct script_desc *s;
313
314 list_for_each_entry(s, &script_descs, node)
315 if (strcasecmp(s->name, name) == 0)
316 return s;
317 return NULL;
318}
319
320static struct script_desc *script_desc__findnew(const char *name)
321{
322 struct script_desc *s = script_desc__find(name);
323
324 if (s)
325 return s;
326
327 s = script_desc__new(name);
328 if (!s)
329 goto out_delete_desc;
330
331 script_desc__add(s);
332
333 return s;
334
335out_delete_desc:
336 script_desc__delete(s);
337
338 return NULL;
339}
340
341static char *ends_with(char *str, const char *suffix)
342{
343 size_t suffix_len = strlen(suffix);
344 char *p = str;
345
346 if (strlen(str) > suffix_len) {
347 p = str + strlen(str) - suffix_len;
348 if (!strncmp(p, suffix, suffix_len))
349 return p;
350 }
351
352 return NULL;
353}
354
355static char *ltrim(char *str)
356{
357 int len = strlen(str);
358
359 while (len && isspace(*str)) {
360 len--;
361 str++;
362 }
363
364 return str;
365}
366
367static int read_script_info(struct script_desc *desc, const char *filename)
368{
369 char line[BUFSIZ], *p;
370 FILE *fp;
371
372 fp = fopen(filename, "r");
373 if (!fp)
374 return -1;
375
376 while (fgets(line, sizeof(line), fp)) {
377 p = ltrim(line);
378 if (strlen(p) == 0)
379 continue;
380 if (*p != '#')
381 continue;
382 p++;
383 if (strlen(p) && *p == '!')
384 continue;
385
386 p = ltrim(p);
387 if (strlen(p) && p[strlen(p) - 1] == '\n')
388 p[strlen(p) - 1] = '\0';
389
390 if (!strncmp(p, "description:", strlen("description:"))) {
391 p += strlen("description:");
392 desc->half_liner = strdup(ltrim(p));
393 continue;
394 }
395
396 if (!strncmp(p, "args:", strlen("args:"))) {
397 p += strlen("args:");
398 desc->args = strdup(ltrim(p));
399 continue;
400 }
401 }
402
403 fclose(fp);
404
405 return 0;
406}
407
408static int list_available_scripts(const struct option *opt __used,
409 const char *s __used, int unset __used)
410{
411 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
412 char scripts_path[MAXPATHLEN];
413 DIR *scripts_dir, *lang_dir;
414 char script_path[MAXPATHLEN];
415 char lang_path[MAXPATHLEN];
416 struct script_desc *desc;
417 char first_half[BUFSIZ];
418 char *script_root;
419 char *str;
420
421 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
422
423 scripts_dir = opendir(scripts_path);
424 if (!scripts_dir)
425 return -1;
426
427 for_each_lang(scripts_dir, lang_dirent, lang_next) {
428 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
429 lang_dirent.d_name);
430 lang_dir = opendir(lang_path);
431 if (!lang_dir)
432 continue;
433
434 for_each_script(lang_dir, script_dirent, script_next) {
435 script_root = strdup(script_dirent.d_name);
436 str = ends_with(script_root, REPORT_SUFFIX);
437 if (str) {
438 *str = '\0';
439 desc = script_desc__findnew(script_root);
440 snprintf(script_path, MAXPATHLEN, "%s/%s",
441 lang_path, script_dirent.d_name);
442 read_script_info(desc, script_path);
443 }
444 free(script_root);
445 }
446 }
447
448 fprintf(stdout, "List of available trace scripts:\n");
449 list_for_each_entry(desc, &script_descs, node) {
450 sprintf(first_half, "%s %s", desc->name,
451 desc->args ? desc->args : "");
452 fprintf(stdout, " %-36s %s\n", first_half,
453 desc->half_liner ? desc->half_liner : "");
454 }
455
456 exit(0);
457}
458
Tom Zanussi38752942009-12-15 02:53:39 -0600459static char *get_script_path(const char *script_root, const char *suffix)
460{
461 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
462 char scripts_path[MAXPATHLEN];
463 char script_path[MAXPATHLEN];
464 DIR *scripts_dir, *lang_dir;
465 char lang_path[MAXPATHLEN];
466 char *str, *__script_root;
467 char *path = NULL;
468
469 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
470
471 scripts_dir = opendir(scripts_path);
472 if (!scripts_dir)
473 return NULL;
474
475 for_each_lang(scripts_dir, lang_dirent, lang_next) {
476 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
477 lang_dirent.d_name);
478 lang_dir = opendir(lang_path);
479 if (!lang_dir)
480 continue;
481
482 for_each_script(lang_dir, script_dirent, script_next) {
483 __script_root = strdup(script_dirent.d_name);
484 str = ends_with(__script_root, suffix);
485 if (str) {
486 *str = '\0';
487 if (strcmp(__script_root, script_root))
488 continue;
489 snprintf(script_path, MAXPATHLEN, "%s/%s",
490 lang_path, script_dirent.d_name);
491 path = strdup(script_path);
492 free(__script_root);
493 break;
494 }
495 free(__script_root);
496 }
497 }
498
499 return path;
500}
501
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200502static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200503 "perf trace [<options>] <command>",
504 NULL
505};
506
507static const struct option options[] = {
508 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
509 "dump raw trace in ASCII"),
510 OPT_BOOLEAN('v', "verbose", &verbose,
511 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600512 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400513 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600514 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
515 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600516 OPT_CALLBACK('s', "script", NULL, "name",
517 "script file name (lang:script name, script name, or *)",
518 parse_scriptname),
519 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
520 "generate perf-trace.xx script in specified language"),
521
Masami Hiramatsu19096292009-08-21 14:56:03 -0400522 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200523};
524
525int cmd_trace(int argc, const char **argv, const char *prefix __used)
526{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200527 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600528 const char *suffix = NULL;
529 const char **__argv;
530 char *script_path;
531 int i, err;
532
533 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
534 if (argc < 3) {
535 fprintf(stderr,
536 "Please specify a record script\n");
537 return -1;
538 }
539 suffix = RECORD_SUFFIX;
540 }
541
542 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
543 if (argc < 3) {
544 fprintf(stderr,
545 "Please specify a report script\n");
546 return -1;
547 }
548 suffix = REPORT_SUFFIX;
549 }
550
551 if (suffix) {
552 script_path = get_script_path(argv[2], suffix);
553 if (!script_path) {
554 fprintf(stderr, "script not found\n");
555 return -1;
556 }
557
558 __argv = malloc((argc + 1) * sizeof(const char *));
559 __argv[0] = "/bin/sh";
560 __argv[1] = script_path;
561 for (i = 3; i < argc; i++)
562 __argv[i - 1] = argv[i];
563 __argv[argc - 1] = NULL;
564
565 execvp("/bin/sh", (char **)__argv);
566 exit(-1);
567 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600568
Tom Zanussi956ffd02009-11-25 01:15:46 -0600569 setup_scripting();
570
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200571 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600572 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200573
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200574 if (symbol__init() < 0)
575 return -1;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200576 setup_pager();
577
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200578 session = perf_session__new(input_name, O_RDONLY, 0);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200579 if (session == NULL)
580 return -ENOMEM;
581
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200582 if (!perf_session__has_traces(session, "record -R"))
583 return -EINVAL;
584
Tom Zanussi956ffd02009-11-25 01:15:46 -0600585 if (generate_script_lang) {
586 struct stat perf_stat;
587
588 int input = open(input_name, O_RDONLY);
589 if (input < 0) {
590 perror("failed to open file");
591 exit(-1);
592 }
593
594 err = fstat(input, &perf_stat);
595 if (err < 0) {
596 perror("failed to stat file");
597 exit(-1);
598 }
599
600 if (!perf_stat.st_size) {
601 fprintf(stderr, "zero-sized file, nothing to do!\n");
602 exit(0);
603 }
604
605 scripting_ops = script_spec__lookup(generate_script_lang);
606 if (!scripting_ops) {
607 fprintf(stderr, "invalid language specifier");
608 return -1;
609 }
610
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200611 perf_header__read(&session->header, input);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600612 err = scripting_ops->generate_script("perf-trace");
613 goto out;
614 }
615
616 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600617 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600618 if (err)
619 goto out;
620 }
621
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200622 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600623
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200624 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600625 cleanup_scripting();
626out:
627 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200628}