perf session: Share the common trace sample_check routine as perf_session__has_traces
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-trace.c
blobb0ba2ac37e2c1446ecf5da8374f7207853f37f1c
1 #include "builtin.h"
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"
12 static char const *script_name;
13 static char const *generate_script_lang;
15 static int default_start_script(const char *script __unused,
16 int argc __unused,
17 const char **argv __unused)
19 return 0;
22 static int default_stop_script(void)
24 return 0;
27 static int default_generate_script(const char *outfile __unused)
29 return 0;
32 static 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,
39 static struct scripting_ops *scripting_ops;
41 static void setup_scripting(void)
43 /* make sure PERF_EXEC_PATH is set for scripts */
44 perf_set_argv_exec_path(perf_exec_path());
46 setup_perl_scripting();
48 scripting_ops = &default_scripting_ops;
51 static int cleanup_scripting(void)
53 return scripting_ops->stop_script();
56 #include "util/parse-options.h"
58 #include "perf.h"
59 #include "util/debug.h"
61 #include "util/trace-event.h"
62 #include "util/exec_cmd.h"
64 static char const *input_name = "perf.data";
66 static int process_sample_event(event_t *event, struct perf_session *session)
68 struct sample_data data;
69 struct thread *thread;
71 memset(&data, 0, sizeof(data));
72 data.time = -1;
73 data.cpu = -1;
74 data.period = 1;
76 event__parse_sample(event, session->sample_type, &data);
78 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
79 event->header.misc,
80 data.pid, data.tid,
81 (void *)(long)data.ip,
82 (long long)data.period);
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;
91 if (session->sample_type & PERF_SAMPLE_RAW) {
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
97 scripting_ops->process_event(data.cpu, data.raw_data,
98 data.raw_size,
99 data.time, thread->comm);
102 session->events_stats.total += data.period;
103 return 0;
106 static struct perf_event_ops event_ops = {
107 .process_sample_event = process_sample_event,
108 .process_comm_event = event__process_comm,
109 .sample_type_check = perf_session__has_traces,
112 static int __cmd_trace(struct perf_session *session)
114 return perf_session__process_events(session, &event_ops);
117 struct script_spec {
118 struct list_head node;
119 struct scripting_ops *ops;
120 char spec[0];
123 LIST_HEAD(script_specs);
125 static struct script_spec *script_spec__new(const char *spec,
126 struct scripting_ops *ops)
128 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
130 if (s != NULL) {
131 strcpy(s->spec, spec);
132 s->ops = ops;
135 return s;
138 static void script_spec__delete(struct script_spec *s)
140 free(s->spec);
141 free(s);
144 static void script_spec__add(struct script_spec *s)
146 list_add_tail(&s->node, &script_specs);
149 static struct script_spec *script_spec__find(const char *spec)
151 struct script_spec *s;
153 list_for_each_entry(s, &script_specs, node)
154 if (strcasecmp(s->spec, spec) == 0)
155 return s;
156 return NULL;
159 static struct script_spec *script_spec__findnew(const char *spec,
160 struct scripting_ops *ops)
162 struct script_spec *s = script_spec__find(spec);
164 if (s)
165 return s;
167 s = script_spec__new(spec, ops);
168 if (!s)
169 goto out_delete_spec;
171 script_spec__add(s);
173 return s;
175 out_delete_spec:
176 script_spec__delete(s);
178 return NULL;
181 int script_spec_register(const char *spec, struct scripting_ops *ops)
183 struct script_spec *s;
185 s = script_spec__find(spec);
186 if (s)
187 return -1;
189 s = script_spec__findnew(spec, ops);
190 if (!s)
191 return -1;
193 return 0;
196 static struct scripting_ops *script_spec__lookup(const char *spec)
198 struct script_spec *s = script_spec__find(spec);
199 if (!s)
200 return NULL;
202 return s->ops;
205 static void list_available_languages(void)
207 struct script_spec *s;
209 fprintf(stderr, "\n");
210 fprintf(stderr, "Scripting language extensions (used in "
211 "perf trace -s [spec:]script.[spec]):\n\n");
213 list_for_each_entry(s, &script_specs, node)
214 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
216 fprintf(stderr, "\n");
219 static int parse_scriptname(const struct option *opt __used,
220 const char *str, int unset __used)
222 char spec[PATH_MAX];
223 const char *script, *ext;
224 int len;
226 if (strcmp(str, "list") == 0) {
227 list_available_languages();
228 return 0;
231 script = strchr(str, ':');
232 if (script) {
233 len = script - str;
234 if (len >= PATH_MAX) {
235 fprintf(stderr, "invalid language specifier");
236 return -1;
238 strncpy(spec, str, len);
239 spec[len] = '\0';
240 scripting_ops = script_spec__lookup(spec);
241 if (!scripting_ops) {
242 fprintf(stderr, "invalid language specifier");
243 return -1;
245 script++;
246 } else {
247 script = str;
248 ext = strchr(script, '.');
249 if (!ext) {
250 fprintf(stderr, "invalid script extension");
251 return -1;
253 scripting_ops = script_spec__lookup(++ext);
254 if (!scripting_ops) {
255 fprintf(stderr, "invalid script extension");
256 return -1;
260 script_name = strdup(script);
262 return 0;
265 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
266 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
267 lang_next) \
268 if (lang_dirent.d_type == DT_DIR && \
269 (strcmp(lang_dirent.d_name, ".")) && \
270 (strcmp(lang_dirent.d_name, "..")))
272 #define for_each_script(lang_dir, script_dirent, script_next) \
273 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
274 script_next) \
275 if (script_dirent.d_type != DT_DIR)
278 #define RECORD_SUFFIX "-record"
279 #define REPORT_SUFFIX "-report"
281 struct script_desc {
282 struct list_head node;
283 char *name;
284 char *half_liner;
285 char *args;
288 LIST_HEAD(script_descs);
290 static struct script_desc *script_desc__new(const char *name)
292 struct script_desc *s = zalloc(sizeof(*s));
294 if (s != NULL)
295 s->name = strdup(name);
297 return s;
300 static void script_desc__delete(struct script_desc *s)
302 free(s->name);
303 free(s);
306 static void script_desc__add(struct script_desc *s)
308 list_add_tail(&s->node, &script_descs);
311 static struct script_desc *script_desc__find(const char *name)
313 struct script_desc *s;
315 list_for_each_entry(s, &script_descs, node)
316 if (strcasecmp(s->name, name) == 0)
317 return s;
318 return NULL;
321 static struct script_desc *script_desc__findnew(const char *name)
323 struct script_desc *s = script_desc__find(name);
325 if (s)
326 return s;
328 s = script_desc__new(name);
329 if (!s)
330 goto out_delete_desc;
332 script_desc__add(s);
334 return s;
336 out_delete_desc:
337 script_desc__delete(s);
339 return NULL;
342 static char *ends_with(char *str, const char *suffix)
344 size_t suffix_len = strlen(suffix);
345 char *p = str;
347 if (strlen(str) > suffix_len) {
348 p = str + strlen(str) - suffix_len;
349 if (!strncmp(p, suffix, suffix_len))
350 return p;
353 return NULL;
356 static char *ltrim(char *str)
358 int len = strlen(str);
360 while (len && isspace(*str)) {
361 len--;
362 str++;
365 return str;
368 static int read_script_info(struct script_desc *desc, const char *filename)
370 char line[BUFSIZ], *p;
371 FILE *fp;
373 fp = fopen(filename, "r");
374 if (!fp)
375 return -1;
377 while (fgets(line, sizeof(line), fp)) {
378 p = ltrim(line);
379 if (strlen(p) == 0)
380 continue;
381 if (*p != '#')
382 continue;
383 p++;
384 if (strlen(p) && *p == '!')
385 continue;
387 p = ltrim(p);
388 if (strlen(p) && p[strlen(p) - 1] == '\n')
389 p[strlen(p) - 1] = '\0';
391 if (!strncmp(p, "description:", strlen("description:"))) {
392 p += strlen("description:");
393 desc->half_liner = strdup(ltrim(p));
394 continue;
397 if (!strncmp(p, "args:", strlen("args:"))) {
398 p += strlen("args:");
399 desc->args = strdup(ltrim(p));
400 continue;
404 fclose(fp);
406 return 0;
409 static int list_available_scripts(const struct option *opt __used,
410 const char *s __used, int unset __used)
412 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
413 char scripts_path[MAXPATHLEN];
414 DIR *scripts_dir, *lang_dir;
415 char script_path[MAXPATHLEN];
416 char lang_path[MAXPATHLEN];
417 struct script_desc *desc;
418 char first_half[BUFSIZ];
419 char *script_root;
420 char *str;
422 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
424 scripts_dir = opendir(scripts_path);
425 if (!scripts_dir)
426 return -1;
428 for_each_lang(scripts_dir, lang_dirent, lang_next) {
429 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
430 lang_dirent.d_name);
431 lang_dir = opendir(lang_path);
432 if (!lang_dir)
433 continue;
435 for_each_script(lang_dir, script_dirent, script_next) {
436 script_root = strdup(script_dirent.d_name);
437 str = ends_with(script_root, REPORT_SUFFIX);
438 if (str) {
439 *str = '\0';
440 desc = script_desc__findnew(script_root);
441 snprintf(script_path, MAXPATHLEN, "%s/%s",
442 lang_path, script_dirent.d_name);
443 read_script_info(desc, script_path);
445 free(script_root);
449 fprintf(stdout, "List of available trace scripts:\n");
450 list_for_each_entry(desc, &script_descs, node) {
451 sprintf(first_half, "%s %s", desc->name,
452 desc->args ? desc->args : "");
453 fprintf(stdout, " %-36s %s\n", first_half,
454 desc->half_liner ? desc->half_liner : "");
457 exit(0);
460 static char *get_script_path(const char *script_root, const char *suffix)
462 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
463 char scripts_path[MAXPATHLEN];
464 char script_path[MAXPATHLEN];
465 DIR *scripts_dir, *lang_dir;
466 char lang_path[MAXPATHLEN];
467 char *str, *__script_root;
468 char *path = NULL;
470 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
472 scripts_dir = opendir(scripts_path);
473 if (!scripts_dir)
474 return NULL;
476 for_each_lang(scripts_dir, lang_dirent, lang_next) {
477 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
478 lang_dirent.d_name);
479 lang_dir = opendir(lang_path);
480 if (!lang_dir)
481 continue;
483 for_each_script(lang_dir, script_dirent, script_next) {
484 __script_root = strdup(script_dirent.d_name);
485 str = ends_with(__script_root, suffix);
486 if (str) {
487 *str = '\0';
488 if (strcmp(__script_root, script_root))
489 continue;
490 snprintf(script_path, MAXPATHLEN, "%s/%s",
491 lang_path, script_dirent.d_name);
492 path = strdup(script_path);
493 free(__script_root);
494 break;
496 free(__script_root);
500 return path;
503 static const char * const trace_usage[] = {
504 "perf trace [<options>] <command>",
505 NULL
508 static const struct option options[] = {
509 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
510 "dump raw trace in ASCII"),
511 OPT_BOOLEAN('v', "verbose", &verbose,
512 "be more verbose (show symbol address, etc)"),
513 OPT_BOOLEAN('L', "Latency", &latency_format,
514 "show latency attributes (irqs/preemption disabled, etc)"),
515 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
516 list_available_scripts),
517 OPT_CALLBACK('s', "script", NULL, "name",
518 "script file name (lang:script name, script name, or *)",
519 parse_scriptname),
520 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
521 "generate perf-trace.xx script in specified language"),
523 OPT_END()
526 int cmd_trace(int argc, const char **argv, const char *prefix __used)
528 struct perf_session *session;
529 const char *suffix = NULL;
530 const char **__argv;
531 char *script_path;
532 int i, err;
534 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
535 if (argc < 3) {
536 fprintf(stderr,
537 "Please specify a record script\n");
538 return -1;
540 suffix = RECORD_SUFFIX;
543 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
544 if (argc < 3) {
545 fprintf(stderr,
546 "Please specify a report script\n");
547 return -1;
549 suffix = REPORT_SUFFIX;
552 if (suffix) {
553 script_path = get_script_path(argv[2], suffix);
554 if (!script_path) {
555 fprintf(stderr, "script not found\n");
556 return -1;
559 __argv = malloc((argc + 1) * sizeof(const char *));
560 __argv[0] = "/bin/sh";
561 __argv[1] = script_path;
562 for (i = 3; i < argc; i++)
563 __argv[i - 1] = argv[i];
564 __argv[argc - 1] = NULL;
566 execvp("/bin/sh", (char **)__argv);
567 exit(-1);
570 setup_scripting();
572 argc = parse_options(argc, argv, options, trace_usage,
573 PARSE_OPT_STOP_AT_NON_OPTION);
575 if (symbol__init() < 0)
576 return -1;
577 setup_pager();
579 session = perf_session__new(input_name, O_RDONLY, 0);
580 if (session == NULL)
581 return -ENOMEM;
583 if (generate_script_lang) {
584 struct stat perf_stat;
586 int input = open(input_name, O_RDONLY);
587 if (input < 0) {
588 perror("failed to open file");
589 exit(-1);
592 err = fstat(input, &perf_stat);
593 if (err < 0) {
594 perror("failed to stat file");
595 exit(-1);
598 if (!perf_stat.st_size) {
599 fprintf(stderr, "zero-sized file, nothing to do!\n");
600 exit(0);
603 scripting_ops = script_spec__lookup(generate_script_lang);
604 if (!scripting_ops) {
605 fprintf(stderr, "invalid language specifier");
606 return -1;
609 perf_header__read(&session->header, input);
610 err = scripting_ops->generate_script("perf-trace");
611 goto out;
614 if (script_name) {
615 err = scripting_ops->start_script(script_name, argc, argv);
616 if (err)
617 goto out;
620 err = __cmd_trace(session);
622 perf_session__delete(session);
623 cleanup_scripting();
624 out:
625 return err;