Merge commit 'v2.6.34-rc6' into perf/core
[pandora-kernel.git] / tools / perf / builtin-trace.c
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 #include "util/exec_cmd.h"
9 #include "util/trace-event.h"
10 #include "util/session.h"
11
12 static char const               *script_name;
13 static char const               *generate_script_lang;
14 static bool                     debug_ordering;
15 static u64                      last_timestamp;
16
17 static int default_start_script(const char *script __unused,
18                                 int argc __unused,
19                                 const char **argv __unused)
20 {
21         return 0;
22 }
23
24 static int default_stop_script(void)
25 {
26         return 0;
27 }
28
29 static int default_generate_script(const char *outfile __unused)
30 {
31         return 0;
32 }
33
34 static struct scripting_ops default_scripting_ops = {
35         .start_script           = default_start_script,
36         .stop_script            = default_stop_script,
37         .process_event          = print_event,
38         .generate_script        = default_generate_script,
39 };
40
41 static struct scripting_ops     *scripting_ops;
42
43 static void setup_scripting(void)
44 {
45         /* make sure PERF_EXEC_PATH is set for scripts */
46         perf_set_argv_exec_path(perf_exec_path());
47
48         setup_perl_scripting();
49         setup_python_scripting();
50
51         scripting_ops = &default_scripting_ops;
52 }
53
54 static int cleanup_scripting(void)
55 {
56         return scripting_ops->stop_script();
57 }
58
59 #include "util/parse-options.h"
60
61 #include "perf.h"
62 #include "util/debug.h"
63
64 #include "util/trace-event.h"
65 #include "util/exec_cmd.h"
66
67 static char const               *input_name = "perf.data";
68
69 static int process_sample_event(event_t *event, struct perf_session *session)
70 {
71         struct sample_data data;
72         struct thread *thread;
73
74         memset(&data, 0, sizeof(data));
75         data.time = -1;
76         data.cpu = -1;
77         data.period = 1;
78
79         event__parse_sample(event, session->sample_type, &data);
80
81         dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
82                     data.pid, data.tid, data.ip, data.period);
83
84         thread = perf_session__findnew(session, event->ip.pid);
85         if (thread == NULL) {
86                 pr_debug("problem processing %d event, skipping it.\n",
87                          event->header.type);
88                 return -1;
89         }
90
91         if (session->sample_type & PERF_SAMPLE_RAW) {
92                 if (debug_ordering) {
93                         if (data.time < last_timestamp) {
94                                 pr_err("Samples misordered, previous: %llu "
95                                         "this: %llu\n", last_timestamp,
96                                         data.time);
97                         }
98                         last_timestamp = data.time;
99                 }
100                 /*
101                  * FIXME: better resolve from pid from the struct trace_entry
102                  * field, although it should be the same than this perf
103                  * event pid
104                  */
105                 scripting_ops->process_event(data.cpu, data.raw_data,
106                                              data.raw_size,
107                                              data.time, thread->comm);
108         }
109
110         session->events_stats.total += data.period;
111         return 0;
112 }
113
114 static struct perf_event_ops event_ops = {
115         .sample = process_sample_event,
116         .comm   = event__process_comm,
117         .attr   = event__process_attr,
118         .event_type = event__process_event_type,
119         .tracing_data = event__process_tracing_data,
120         .build_id = event__process_build_id,
121         .ordered_samples = true,
122 };
123
124 extern volatile int session_done;
125
126 static void sig_handler(int sig __unused)
127 {
128         session_done = 1;
129 }
130
131 static int __cmd_trace(struct perf_session *session)
132 {
133         signal(SIGINT, sig_handler);
134
135         return perf_session__process_events(session, &event_ops);
136 }
137
138 struct script_spec {
139         struct list_head        node;
140         struct scripting_ops    *ops;
141         char                    spec[0];
142 };
143
144 LIST_HEAD(script_specs);
145
146 static struct script_spec *script_spec__new(const char *spec,
147                                             struct scripting_ops *ops)
148 {
149         struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
150
151         if (s != NULL) {
152                 strcpy(s->spec, spec);
153                 s->ops = ops;
154         }
155
156         return s;
157 }
158
159 static void script_spec__delete(struct script_spec *s)
160 {
161         free(s->spec);
162         free(s);
163 }
164
165 static void script_spec__add(struct script_spec *s)
166 {
167         list_add_tail(&s->node, &script_specs);
168 }
169
170 static struct script_spec *script_spec__find(const char *spec)
171 {
172         struct script_spec *s;
173
174         list_for_each_entry(s, &script_specs, node)
175                 if (strcasecmp(s->spec, spec) == 0)
176                         return s;
177         return NULL;
178 }
179
180 static struct script_spec *script_spec__findnew(const char *spec,
181                                                 struct scripting_ops *ops)
182 {
183         struct script_spec *s = script_spec__find(spec);
184
185         if (s)
186                 return s;
187
188         s = script_spec__new(spec, ops);
189         if (!s)
190                 goto out_delete_spec;
191
192         script_spec__add(s);
193
194         return s;
195
196 out_delete_spec:
197         script_spec__delete(s);
198
199         return NULL;
200 }
201
202 int script_spec_register(const char *spec, struct scripting_ops *ops)
203 {
204         struct script_spec *s;
205
206         s = script_spec__find(spec);
207         if (s)
208                 return -1;
209
210         s = script_spec__findnew(spec, ops);
211         if (!s)
212                 return -1;
213
214         return 0;
215 }
216
217 static struct scripting_ops *script_spec__lookup(const char *spec)
218 {
219         struct script_spec *s = script_spec__find(spec);
220         if (!s)
221                 return NULL;
222
223         return s->ops;
224 }
225
226 static void list_available_languages(void)
227 {
228         struct script_spec *s;
229
230         fprintf(stderr, "\n");
231         fprintf(stderr, "Scripting language extensions (used in "
232                 "perf trace -s [spec:]script.[spec]):\n\n");
233
234         list_for_each_entry(s, &script_specs, node)
235                 fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
236
237         fprintf(stderr, "\n");
238 }
239
240 static int parse_scriptname(const struct option *opt __used,
241                             const char *str, int unset __used)
242 {
243         char spec[PATH_MAX];
244         const char *script, *ext;
245         int len;
246
247         if (strcmp(str, "lang") == 0) {
248                 list_available_languages();
249                 exit(0);
250         }
251
252         script = strchr(str, ':');
253         if (script) {
254                 len = script - str;
255                 if (len >= PATH_MAX) {
256                         fprintf(stderr, "invalid language specifier");
257                         return -1;
258                 }
259                 strncpy(spec, str, len);
260                 spec[len] = '\0';
261                 scripting_ops = script_spec__lookup(spec);
262                 if (!scripting_ops) {
263                         fprintf(stderr, "invalid language specifier");
264                         return -1;
265                 }
266                 script++;
267         } else {
268                 script = str;
269                 ext = strchr(script, '.');
270                 if (!ext) {
271                         fprintf(stderr, "invalid script extension");
272                         return -1;
273                 }
274                 scripting_ops = script_spec__lookup(++ext);
275                 if (!scripting_ops) {
276                         fprintf(stderr, "invalid script extension");
277                         return -1;
278                 }
279         }
280
281         script_name = strdup(script);
282
283         return 0;
284 }
285
286 #define for_each_lang(scripts_dir, lang_dirent, lang_next)              \
287         while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) &&     \
288                lang_next)                                               \
289                 if (lang_dirent.d_type == DT_DIR &&                     \
290                     (strcmp(lang_dirent.d_name, ".")) &&                \
291                     (strcmp(lang_dirent.d_name, "..")))
292
293 #define for_each_script(lang_dir, script_dirent, script_next)           \
294         while (!readdir_r(lang_dir, &script_dirent, &script_next) &&    \
295                script_next)                                             \
296                 if (script_dirent.d_type != DT_DIR)
297
298
299 #define RECORD_SUFFIX                   "-record"
300 #define REPORT_SUFFIX                   "-report"
301
302 struct script_desc {
303         struct list_head        node;
304         char                    *name;
305         char                    *half_liner;
306         char                    *args;
307 };
308
309 LIST_HEAD(script_descs);
310
311 static struct script_desc *script_desc__new(const char *name)
312 {
313         struct script_desc *s = zalloc(sizeof(*s));
314
315         if (s != NULL)
316                 s->name = strdup(name);
317
318         return s;
319 }
320
321 static void script_desc__delete(struct script_desc *s)
322 {
323         free(s->name);
324         free(s);
325 }
326
327 static void script_desc__add(struct script_desc *s)
328 {
329         list_add_tail(&s->node, &script_descs);
330 }
331
332 static struct script_desc *script_desc__find(const char *name)
333 {
334         struct script_desc *s;
335
336         list_for_each_entry(s, &script_descs, node)
337                 if (strcasecmp(s->name, name) == 0)
338                         return s;
339         return NULL;
340 }
341
342 static struct script_desc *script_desc__findnew(const char *name)
343 {
344         struct script_desc *s = script_desc__find(name);
345
346         if (s)
347                 return s;
348
349         s = script_desc__new(name);
350         if (!s)
351                 goto out_delete_desc;
352
353         script_desc__add(s);
354
355         return s;
356
357 out_delete_desc:
358         script_desc__delete(s);
359
360         return NULL;
361 }
362
363 static char *ends_with(char *str, const char *suffix)
364 {
365         size_t suffix_len = strlen(suffix);
366         char *p = str;
367
368         if (strlen(str) > suffix_len) {
369                 p = str + strlen(str) - suffix_len;
370                 if (!strncmp(p, suffix, suffix_len))
371                         return p;
372         }
373
374         return NULL;
375 }
376
377 static char *ltrim(char *str)
378 {
379         int len = strlen(str);
380
381         while (len && isspace(*str)) {
382                 len--;
383                 str++;
384         }
385
386         return str;
387 }
388
389 static int read_script_info(struct script_desc *desc, const char *filename)
390 {
391         char line[BUFSIZ], *p;
392         FILE *fp;
393
394         fp = fopen(filename, "r");
395         if (!fp)
396                 return -1;
397
398         while (fgets(line, sizeof(line), fp)) {
399                 p = ltrim(line);
400                 if (strlen(p) == 0)
401                         continue;
402                 if (*p != '#')
403                         continue;
404                 p++;
405                 if (strlen(p) && *p == '!')
406                         continue;
407
408                 p = ltrim(p);
409                 if (strlen(p) && p[strlen(p) - 1] == '\n')
410                         p[strlen(p) - 1] = '\0';
411
412                 if (!strncmp(p, "description:", strlen("description:"))) {
413                         p += strlen("description:");
414                         desc->half_liner = strdup(ltrim(p));
415                         continue;
416                 }
417
418                 if (!strncmp(p, "args:", strlen("args:"))) {
419                         p += strlen("args:");
420                         desc->args = strdup(ltrim(p));
421                         continue;
422                 }
423         }
424
425         fclose(fp);
426
427         return 0;
428 }
429
430 static int list_available_scripts(const struct option *opt __used,
431                                   const char *s __used, int unset __used)
432 {
433         struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
434         char scripts_path[MAXPATHLEN];
435         DIR *scripts_dir, *lang_dir;
436         char script_path[MAXPATHLEN];
437         char lang_path[MAXPATHLEN];
438         struct script_desc *desc;
439         char first_half[BUFSIZ];
440         char *script_root;
441         char *str;
442
443         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
444
445         scripts_dir = opendir(scripts_path);
446         if (!scripts_dir)
447                 return -1;
448
449         for_each_lang(scripts_dir, lang_dirent, lang_next) {
450                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
451                          lang_dirent.d_name);
452                 lang_dir = opendir(lang_path);
453                 if (!lang_dir)
454                         continue;
455
456                 for_each_script(lang_dir, script_dirent, script_next) {
457                         script_root = strdup(script_dirent.d_name);
458                         str = ends_with(script_root, REPORT_SUFFIX);
459                         if (str) {
460                                 *str = '\0';
461                                 desc = script_desc__findnew(script_root);
462                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
463                                          lang_path, script_dirent.d_name);
464                                 read_script_info(desc, script_path);
465                         }
466                         free(script_root);
467                 }
468         }
469
470         fprintf(stdout, "List of available trace scripts:\n");
471         list_for_each_entry(desc, &script_descs, node) {
472                 sprintf(first_half, "%s %s", desc->name,
473                         desc->args ? desc->args : "");
474                 fprintf(stdout, "  %-36s %s\n", first_half,
475                         desc->half_liner ? desc->half_liner : "");
476         }
477
478         exit(0);
479 }
480
481 static char *get_script_path(const char *script_root, const char *suffix)
482 {
483         struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
484         char scripts_path[MAXPATHLEN];
485         char script_path[MAXPATHLEN];
486         DIR *scripts_dir, *lang_dir;
487         char lang_path[MAXPATHLEN];
488         char *str, *__script_root;
489         char *path = NULL;
490
491         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
492
493         scripts_dir = opendir(scripts_path);
494         if (!scripts_dir)
495                 return NULL;
496
497         for_each_lang(scripts_dir, lang_dirent, lang_next) {
498                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
499                          lang_dirent.d_name);
500                 lang_dir = opendir(lang_path);
501                 if (!lang_dir)
502                         continue;
503
504                 for_each_script(lang_dir, script_dirent, script_next) {
505                         __script_root = strdup(script_dirent.d_name);
506                         str = ends_with(__script_root, suffix);
507                         if (str) {
508                                 *str = '\0';
509                                 if (strcmp(__script_root, script_root))
510                                         continue;
511                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
512                                          lang_path, script_dirent.d_name);
513                                 path = strdup(script_path);
514                                 free(__script_root);
515                                 break;
516                         }
517                         free(__script_root);
518                 }
519         }
520
521         return path;
522 }
523
524 static const char * const trace_usage[] = {
525         "perf trace [<options>] <command>",
526         NULL
527 };
528
529 static const struct option options[] = {
530         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
531                     "dump raw trace in ASCII"),
532         OPT_INCR('v', "verbose", &verbose,
533                     "be more verbose (show symbol address, etc)"),
534         OPT_BOOLEAN('L', "Latency", &latency_format,
535                     "show latency attributes (irqs/preemption disabled, etc)"),
536         OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
537                            list_available_scripts),
538         OPT_CALLBACK('s', "script", NULL, "name",
539                      "script file name (lang:script name, script name, or *)",
540                      parse_scriptname),
541         OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
542                    "generate perf-trace.xx script in specified language"),
543         OPT_STRING('i', "input", &input_name, "file",
544                     "input file name"),
545         OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
546                    "check that samples time ordering is monotonic"),
547
548         OPT_END()
549 };
550
551 int cmd_trace(int argc, const char **argv, const char *prefix __used)
552 {
553         struct perf_session *session;
554         const char *suffix = NULL;
555         const char **__argv;
556         char *script_path;
557         int i, err;
558
559         if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
560                 if (argc < 3) {
561                         fprintf(stderr,
562                                 "Please specify a record script\n");
563                         return -1;
564                 }
565                 suffix = RECORD_SUFFIX;
566         }
567
568         if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
569                 if (argc < 3) {
570                         fprintf(stderr,
571                                 "Please specify a report script\n");
572                         return -1;
573                 }
574                 suffix = REPORT_SUFFIX;
575         }
576
577         if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
578                 char *record_script_path, *report_script_path;
579                 int live_pipe[2];
580                 pid_t pid;
581
582                 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
583                 if (!record_script_path) {
584                         fprintf(stderr, "record script not found\n");
585                         return -1;
586                 }
587
588                 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
589                 if (!report_script_path) {
590                         fprintf(stderr, "report script not found\n");
591                         return -1;
592                 }
593
594                 if (pipe(live_pipe) < 0) {
595                         perror("failed to create pipe");
596                         exit(-1);
597                 }
598
599                 pid = fork();
600                 if (pid < 0) {
601                         perror("failed to fork");
602                         exit(-1);
603                 }
604
605                 if (!pid) {
606                         dup2(live_pipe[1], 1);
607                         close(live_pipe[0]);
608
609                         __argv = malloc(5 * sizeof(const char *));
610                         __argv[0] = "/bin/sh";
611                         __argv[1] = record_script_path;
612                         __argv[2] = "-o";
613                         __argv[3] = "-";
614                         __argv[4] = NULL;
615
616                         execvp("/bin/sh", (char **)__argv);
617                         exit(-1);
618                 }
619
620                 dup2(live_pipe[0], 0);
621                 close(live_pipe[1]);
622
623                 __argv = malloc((argc + 3) * sizeof(const char *));
624                 __argv[0] = "/bin/sh";
625                 __argv[1] = report_script_path;
626                 for (i = 2; i < argc; i++)
627                         __argv[i] = argv[i];
628                 __argv[i++] = "-i";
629                 __argv[i++] = "-";
630                 __argv[i++] = NULL;
631
632                 execvp("/bin/sh", (char **)__argv);
633                 exit(-1);
634         }
635
636         if (suffix) {
637                 script_path = get_script_path(argv[2], suffix);
638                 if (!script_path) {
639                         fprintf(stderr, "script not found\n");
640                         return -1;
641                 }
642
643                 __argv = malloc((argc + 1) * sizeof(const char *));
644                 __argv[0] = "/bin/sh";
645                 __argv[1] = script_path;
646                 for (i = 3; i < argc; i++)
647                         __argv[i - 1] = argv[i];
648                 __argv[argc - 1] = NULL;
649
650                 execvp("/bin/sh", (char **)__argv);
651                 exit(-1);
652         }
653
654         setup_scripting();
655
656         argc = parse_options(argc, argv, options, trace_usage,
657                              PARSE_OPT_STOP_AT_NON_OPTION);
658
659         if (symbol__init() < 0)
660                 return -1;
661         if (!script_name)
662                 setup_pager();
663
664         session = perf_session__new(input_name, O_RDONLY, 0);
665         if (session == NULL)
666                 return -ENOMEM;
667
668         if (strcmp(input_name, "-") &&
669             !perf_session__has_traces(session, "record -R"))
670                 return -EINVAL;
671
672         if (generate_script_lang) {
673                 struct stat perf_stat;
674
675                 int input = open(input_name, O_RDONLY);
676                 if (input < 0) {
677                         perror("failed to open file");
678                         exit(-1);
679                 }
680
681                 err = fstat(input, &perf_stat);
682                 if (err < 0) {
683                         perror("failed to stat file");
684                         exit(-1);
685                 }
686
687                 if (!perf_stat.st_size) {
688                         fprintf(stderr, "zero-sized file, nothing to do!\n");
689                         exit(0);
690                 }
691
692                 scripting_ops = script_spec__lookup(generate_script_lang);
693                 if (!scripting_ops) {
694                         fprintf(stderr, "invalid language specifier");
695                         return -1;
696                 }
697
698                 err = scripting_ops->generate_script("perf-trace");
699                 goto out;
700         }
701
702         if (script_name) {
703                 err = scripting_ops->start_script(script_name, argc, argv);
704                 if (err)
705                         goto out;
706         }
707
708         err = __cmd_trace(session);
709
710         perf_session__delete(session);
711         cleanup_scripting();
712 out:
713         return err;
714 }