perf: Don't print traces when debugging ordering
[deliverable/linux.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"
cf72344d
IM
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
94c744b6 10#include "util/session.h"
5f9c39dc 11
956ffd02
TZ
12static char const *script_name;
13static char const *generate_script_lang;
e1889d75
FW
14static bool debug_ordering;
15static u64 last_timestamp;
6fcf7ddb 16static u64 nr_unordered;
956ffd02 17
586bc5cc
TZ
18static int default_start_script(const char *script __unused,
19 int argc __unused,
20 const char **argv __unused)
956ffd02
TZ
21{
22 return 0;
23}
24
25static int default_stop_script(void)
26{
27 return 0;
28}
29
586bc5cc 30static int default_generate_script(const char *outfile __unused)
956ffd02
TZ
31{
32 return 0;
33}
34
35static struct scripting_ops default_scripting_ops = {
36 .start_script = default_start_script,
37 .stop_script = default_stop_script,
38 .process_event = print_event,
39 .generate_script = default_generate_script,
40};
41
42static struct scripting_ops *scripting_ops;
43
44static void setup_scripting(void)
45{
46 /* make sure PERF_EXEC_PATH is set for scripts */
47 perf_set_argv_exec_path(perf_exec_path());
48
16c632de 49 setup_perl_scripting();
7e4b21b8 50 setup_python_scripting();
16c632de 51
956ffd02
TZ
52 scripting_ops = &default_scripting_ops;
53}
54
55static int cleanup_scripting(void)
56{
3824a4e8
TZ
57 pr_debug("\nperf trace script stopped\n");
58
956ffd02
TZ
59 return scripting_ops->stop_script();
60}
61
5f9c39dc
FW
62#include "util/parse-options.h"
63
64#include "perf.h"
65#include "util/debug.h"
66
67#include "util/trace-event.h"
956ffd02 68#include "util/exec_cmd.h"
5f9c39dc 69
956ffd02 70static char const *input_name = "perf.data";
5f9c39dc 71
b3165f41 72static int process_sample_event(event_t *event, struct perf_session *session)
5f9c39dc 73{
180f95e2
OH
74 struct sample_data data;
75 struct thread *thread;
6ddf259d 76
180f95e2
OH
77 memset(&data, 0, sizeof(data));
78 data.time = -1;
79 data.cpu = -1;
80 data.period = 1;
cd6feeea 81
c019879b 82 event__parse_sample(event, session->sample_type, &data);
5f9c39dc 83
0d755034
ACM
84 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
85 data.pid, data.tid, data.ip, data.period);
5f9c39dc 86
b3165f41 87 thread = perf_session__findnew(session, event->ip.pid);
5f9c39dc 88 if (thread == NULL) {
6beba7ad
ACM
89 pr_debug("problem processing %d event, skipping it.\n",
90 event->header.type);
5f9c39dc
FW
91 return -1;
92 }
93
c019879b 94 if (session->sample_type & PERF_SAMPLE_RAW) {
e1889d75
FW
95 if (debug_ordering) {
96 if (data.time < last_timestamp) {
97 pr_err("Samples misordered, previous: %llu "
98 "this: %llu\n", last_timestamp,
99 data.time);
6fcf7ddb 100 nr_unordered++;
e1889d75
FW
101 }
102 last_timestamp = data.time;
6fcf7ddb 103 return 0;
e1889d75 104 }
5f9c39dc
FW
105 /*
106 * FIXME: better resolve from pid from the struct trace_entry
107 * field, although it should be the same than this perf
108 * event pid
109 */
180f95e2
OH
110 scripting_ops->process_event(data.cpu, data.raw_data,
111 data.raw_size,
112 data.time, thread->comm);
5f9c39dc 113 }
5f9c39dc 114
cee75ac7 115 session->hists.stats.total_period += data.period;
5f9c39dc
FW
116 return 0;
117}
118
301a0b02 119static struct perf_event_ops event_ops = {
55aa640f
ACM
120 .sample = process_sample_event,
121 .comm = event__process_comm,
2c46dbb5 122 .attr = event__process_attr,
cd19a035 123 .event_type = event__process_event_type,
9215545e 124 .tracing_data = event__process_tracing_data,
c7929e47 125 .build_id = event__process_build_id,
e0a808c6 126 .ordered_samples = true,
016e92fb
FW
127};
128
c239da3b
TZ
129extern volatile int session_done;
130
131static void sig_handler(int sig __unused)
132{
133 session_done = 1;
134}
135
d8f66248 136static int __cmd_trace(struct perf_session *session)
5f9c39dc 137{
6fcf7ddb
FW
138 int ret;
139
c239da3b
TZ
140 signal(SIGINT, sig_handler);
141
6fcf7ddb
FW
142 ret = perf_session__process_events(session, &event_ops);
143
144 if (debug_ordering)
145 pr_err("Misordered timestamps: %llu\n", nr_unordered);
146
147 return ret;
5f9c39dc
FW
148}
149
956ffd02
TZ
150struct 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
f526d68b 259 if (strcmp(str, "lang") == 0) {
956ffd02 260 list_available_languages();
f526d68b 261 exit(0);
956ffd02
TZ
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
4b9c0c59
TZ
298#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
299 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
300 lang_next) \
301 if (lang_dirent.d_type == DT_DIR && \
302 (strcmp(lang_dirent.d_name, ".")) && \
303 (strcmp(lang_dirent.d_name, "..")))
304
305#define for_each_script(lang_dir, script_dirent, script_next) \
306 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
307 script_next) \
308 if (script_dirent.d_type != DT_DIR)
309
310
311#define RECORD_SUFFIX "-record"
312#define REPORT_SUFFIX "-report"
313
314struct script_desc {
315 struct list_head node;
316 char *name;
317 char *half_liner;
318 char *args;
319};
320
321LIST_HEAD(script_descs);
322
323static struct script_desc *script_desc__new(const char *name)
324{
325 struct script_desc *s = zalloc(sizeof(*s));
326
327 if (s != NULL)
328 s->name = strdup(name);
329
330 return s;
331}
332
333static void script_desc__delete(struct script_desc *s)
334{
335 free(s->name);
336 free(s);
337}
338
339static void script_desc__add(struct script_desc *s)
340{
341 list_add_tail(&s->node, &script_descs);
342}
343
344static struct script_desc *script_desc__find(const char *name)
345{
346 struct script_desc *s;
347
348 list_for_each_entry(s, &script_descs, node)
349 if (strcasecmp(s->name, name) == 0)
350 return s;
351 return NULL;
352}
353
354static struct script_desc *script_desc__findnew(const char *name)
355{
356 struct script_desc *s = script_desc__find(name);
357
358 if (s)
359 return s;
360
361 s = script_desc__new(name);
362 if (!s)
363 goto out_delete_desc;
364
365 script_desc__add(s);
366
367 return s;
368
369out_delete_desc:
370 script_desc__delete(s);
371
372 return NULL;
373}
374
375static char *ends_with(char *str, const char *suffix)
376{
377 size_t suffix_len = strlen(suffix);
378 char *p = str;
379
380 if (strlen(str) > suffix_len) {
381 p = str + strlen(str) - suffix_len;
382 if (!strncmp(p, suffix, suffix_len))
383 return p;
384 }
385
386 return NULL;
387}
388
389static char *ltrim(char *str)
390{
391 int len = strlen(str);
392
393 while (len && isspace(*str)) {
394 len--;
395 str++;
396 }
397
398 return str;
399}
400
401static int read_script_info(struct script_desc *desc, const char *filename)
402{
403 char line[BUFSIZ], *p;
404 FILE *fp;
405
406 fp = fopen(filename, "r");
407 if (!fp)
408 return -1;
409
410 while (fgets(line, sizeof(line), fp)) {
411 p = ltrim(line);
412 if (strlen(p) == 0)
413 continue;
414 if (*p != '#')
415 continue;
416 p++;
417 if (strlen(p) && *p == '!')
418 continue;
419
420 p = ltrim(p);
421 if (strlen(p) && p[strlen(p) - 1] == '\n')
422 p[strlen(p) - 1] = '\0';
423
424 if (!strncmp(p, "description:", strlen("description:"))) {
425 p += strlen("description:");
426 desc->half_liner = strdup(ltrim(p));
427 continue;
428 }
429
430 if (!strncmp(p, "args:", strlen("args:"))) {
431 p += strlen("args:");
432 desc->args = strdup(ltrim(p));
433 continue;
434 }
435 }
436
437 fclose(fp);
438
439 return 0;
440}
441
442static int list_available_scripts(const struct option *opt __used,
443 const char *s __used, int unset __used)
444{
445 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
446 char scripts_path[MAXPATHLEN];
447 DIR *scripts_dir, *lang_dir;
448 char script_path[MAXPATHLEN];
449 char lang_path[MAXPATHLEN];
450 struct script_desc *desc;
451 char first_half[BUFSIZ];
452 char *script_root;
453 char *str;
454
455 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
456
457 scripts_dir = opendir(scripts_path);
458 if (!scripts_dir)
459 return -1;
460
461 for_each_lang(scripts_dir, lang_dirent, lang_next) {
462 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
463 lang_dirent.d_name);
464 lang_dir = opendir(lang_path);
465 if (!lang_dir)
466 continue;
467
468 for_each_script(lang_dir, script_dirent, script_next) {
469 script_root = strdup(script_dirent.d_name);
470 str = ends_with(script_root, REPORT_SUFFIX);
471 if (str) {
472 *str = '\0';
473 desc = script_desc__findnew(script_root);
474 snprintf(script_path, MAXPATHLEN, "%s/%s",
475 lang_path, script_dirent.d_name);
476 read_script_info(desc, script_path);
477 }
478 free(script_root);
479 }
480 }
481
482 fprintf(stdout, "List of available trace scripts:\n");
483 list_for_each_entry(desc, &script_descs, node) {
484 sprintf(first_half, "%s %s", desc->name,
485 desc->args ? desc->args : "");
486 fprintf(stdout, " %-36s %s\n", first_half,
487 desc->half_liner ? desc->half_liner : "");
488 }
489
490 exit(0);
491}
492
3875294f
TZ
493static char *get_script_path(const char *script_root, const char *suffix)
494{
495 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
496 char scripts_path[MAXPATHLEN];
497 char script_path[MAXPATHLEN];
498 DIR *scripts_dir, *lang_dir;
499 char lang_path[MAXPATHLEN];
500 char *str, *__script_root;
501 char *path = NULL;
502
503 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
504
505 scripts_dir = opendir(scripts_path);
506 if (!scripts_dir)
507 return NULL;
508
509 for_each_lang(scripts_dir, lang_dirent, lang_next) {
510 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
511 lang_dirent.d_name);
512 lang_dir = opendir(lang_path);
513 if (!lang_dir)
514 continue;
515
516 for_each_script(lang_dir, script_dirent, script_next) {
517 __script_root = strdup(script_dirent.d_name);
518 str = ends_with(__script_root, suffix);
519 if (str) {
520 *str = '\0';
521 if (strcmp(__script_root, script_root))
522 continue;
523 snprintf(script_path, MAXPATHLEN, "%s/%s",
524 lang_path, script_dirent.d_name);
525 path = strdup(script_path);
526 free(__script_root);
527 break;
528 }
529 free(__script_root);
530 }
531 }
532
533 return path;
534}
535
0422a4fc 536static const char * const trace_usage[] = {
5f9c39dc
FW
537 "perf trace [<options>] <command>",
538 NULL
539};
540
541static const struct option options[] = {
542 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
543 "dump raw trace in ASCII"),
c0555642 544 OPT_INCR('v', "verbose", &verbose,
5f9c39dc 545 "be more verbose (show symbol address, etc)"),
4b9c0c59 546 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 547 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
548 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
549 list_available_scripts),
956ffd02
TZ
550 OPT_CALLBACK('s', "script", NULL, "name",
551 "script file name (lang:script name, script name, or *)",
552 parse_scriptname),
553 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
554 "generate perf-trace.xx script in specified language"),
408f0d18
HM
555 OPT_STRING('i', "input", &input_name, "file",
556 "input file name"),
e1889d75
FW
557 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
558 "check that samples time ordering is monotonic"),
956ffd02 559
1909629f 560 OPT_END()
5f9c39dc
FW
561};
562
563int cmd_trace(int argc, const char **argv, const char *prefix __used)
564{
d8f66248 565 struct perf_session *session;
3875294f
TZ
566 const char *suffix = NULL;
567 const char **__argv;
568 char *script_path;
569 int i, err;
570
571 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
572 if (argc < 3) {
573 fprintf(stderr,
574 "Please specify a record script\n");
575 return -1;
576 }
577 suffix = RECORD_SUFFIX;
578 }
579
580 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
581 if (argc < 3) {
582 fprintf(stderr,
583 "Please specify a report script\n");
584 return -1;
585 }
586 suffix = REPORT_SUFFIX;
587 }
588
a0cccc2e
TZ
589 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
590 char *record_script_path, *report_script_path;
591 int live_pipe[2];
592 pid_t pid;
593
594 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
595 if (!record_script_path) {
596 fprintf(stderr, "record script not found\n");
597 return -1;
598 }
599
600 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
601 if (!report_script_path) {
602 fprintf(stderr, "report script not found\n");
603 return -1;
604 }
605
606 if (pipe(live_pipe) < 0) {
607 perror("failed to create pipe");
608 exit(-1);
609 }
610
611 pid = fork();
612 if (pid < 0) {
613 perror("failed to fork");
614 exit(-1);
615 }
616
617 if (!pid) {
618 dup2(live_pipe[1], 1);
619 close(live_pipe[0]);
620
621 __argv = malloc(5 * sizeof(const char *));
622 __argv[0] = "/bin/sh";
623 __argv[1] = record_script_path;
624 __argv[2] = "-o";
625 __argv[3] = "-";
626 __argv[4] = NULL;
627
628 execvp("/bin/sh", (char **)__argv);
629 exit(-1);
630 }
631
632 dup2(live_pipe[0], 0);
633 close(live_pipe[1]);
634
635 __argv = malloc((argc + 3) * sizeof(const char *));
636 __argv[0] = "/bin/sh";
637 __argv[1] = report_script_path;
638 for (i = 2; i < argc; i++)
639 __argv[i] = argv[i];
640 __argv[i++] = "-i";
641 __argv[i++] = "-";
642 __argv[i++] = NULL;
643
644 execvp("/bin/sh", (char **)__argv);
645 exit(-1);
646 }
647
3875294f
TZ
648 if (suffix) {
649 script_path = get_script_path(argv[2], suffix);
650 if (!script_path) {
651 fprintf(stderr, "script not found\n");
652 return -1;
653 }
654
655 __argv = malloc((argc + 1) * sizeof(const char *));
656 __argv[0] = "/bin/sh";
657 __argv[1] = script_path;
658 for (i = 3; i < argc; i++)
659 __argv[i - 1] = argv[i];
660 __argv[argc - 1] = NULL;
661
662 execvp("/bin/sh", (char **)__argv);
663 exit(-1);
664 }
956ffd02 665
956ffd02
TZ
666 setup_scripting();
667
0422a4fc 668 argc = parse_options(argc, argv, options, trace_usage,
586bc5cc 669 PARSE_OPT_STOP_AT_NON_OPTION);
5f9c39dc 670
655000e7
ACM
671 if (symbol__init() < 0)
672 return -1;
cf4fee50
TZ
673 if (!script_name)
674 setup_pager();
5f9c39dc 675
454c407e 676 session = perf_session__new(input_name, O_RDONLY, 0, false);
d8f66248
ACM
677 if (session == NULL)
678 return -ENOMEM;
679
c239da3b
TZ
680 if (strcmp(input_name, "-") &&
681 !perf_session__has_traces(session, "record -R"))
d549c769
ACM
682 return -EINVAL;
683
956ffd02
TZ
684 if (generate_script_lang) {
685 struct stat perf_stat;
686
687 int input = open(input_name, O_RDONLY);
688 if (input < 0) {
689 perror("failed to open file");
690 exit(-1);
691 }
692
693 err = fstat(input, &perf_stat);
694 if (err < 0) {
695 perror("failed to stat file");
696 exit(-1);
697 }
698
699 if (!perf_stat.st_size) {
700 fprintf(stderr, "zero-sized file, nothing to do!\n");
701 exit(0);
702 }
703
704 scripting_ops = script_spec__lookup(generate_script_lang);
705 if (!scripting_ops) {
706 fprintf(stderr, "invalid language specifier");
707 return -1;
708 }
709
956ffd02
TZ
710 err = scripting_ops->generate_script("perf-trace");
711 goto out;
712 }
713
714 if (script_name) {
586bc5cc 715 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02
TZ
716 if (err)
717 goto out;
3824a4e8 718 pr_debug("perf trace started with script %s\n\n", script_name);
956ffd02
TZ
719 }
720
d8f66248 721 err = __cmd_trace(session);
956ffd02 722
d8f66248 723 perf_session__delete(session);
956ffd02
TZ
724 cleanup_scripting();
725out:
726 return err;
5f9c39dc 727}
This page took 0.085303 seconds and 5 git commands to generate.