perf: Convert perf header attrs into attr events
[linux-2.6/cjktty.git] / tools / perf / builtin-trace.c
blobe30eac6af5415ca402374e1e4e7ece01736ad512
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();
47 setup_python_scripting();
49 scripting_ops = &default_scripting_ops;
52 static int cleanup_scripting(void)
54 return scripting_ops->stop_script();
57 #include "util/parse-options.h"
59 #include "perf.h"
60 #include "util/debug.h"
62 #include "util/trace-event.h"
63 #include "util/exec_cmd.h"
65 static char const *input_name = "perf.data";
67 static int process_sample_event(event_t *event, struct perf_session *session)
69 struct sample_data data;
70 struct thread *thread;
72 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
77 event__parse_sample(event, session->sample_type, &data);
79 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
80 data.pid, data.tid, data.ip, data.period);
82 thread = perf_session__findnew(session, event->ip.pid);
83 if (thread == NULL) {
84 pr_debug("problem processing %d event, skipping it.\n",
85 event->header.type);
86 return -1;
89 if (session->sample_type & PERF_SAMPLE_RAW) {
91 * FIXME: better resolve from pid from the struct trace_entry
92 * field, although it should be the same than this perf
93 * event pid
95 scripting_ops->process_event(data.cpu, data.raw_data,
96 data.raw_size,
97 data.time, thread->comm);
100 session->events_stats.total += data.period;
101 return 0;
104 static struct perf_event_ops event_ops = {
105 .sample = process_sample_event,
106 .comm = event__process_comm,
107 .attr = event__process_attr,
110 extern volatile int session_done;
112 static void sig_handler(int sig __unused)
114 session_done = 1;
117 static int __cmd_trace(struct perf_session *session)
119 signal(SIGINT, sig_handler);
121 return perf_session__process_events(session, &event_ops);
124 struct script_spec {
125 struct list_head node;
126 struct scripting_ops *ops;
127 char spec[0];
130 LIST_HEAD(script_specs);
132 static struct script_spec *script_spec__new(const char *spec,
133 struct scripting_ops *ops)
135 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
137 if (s != NULL) {
138 strcpy(s->spec, spec);
139 s->ops = ops;
142 return s;
145 static void script_spec__delete(struct script_spec *s)
147 free(s->spec);
148 free(s);
151 static void script_spec__add(struct script_spec *s)
153 list_add_tail(&s->node, &script_specs);
156 static struct script_spec *script_spec__find(const char *spec)
158 struct script_spec *s;
160 list_for_each_entry(s, &script_specs, node)
161 if (strcasecmp(s->spec, spec) == 0)
162 return s;
163 return NULL;
166 static struct script_spec *script_spec__findnew(const char *spec,
167 struct scripting_ops *ops)
169 struct script_spec *s = script_spec__find(spec);
171 if (s)
172 return s;
174 s = script_spec__new(spec, ops);
175 if (!s)
176 goto out_delete_spec;
178 script_spec__add(s);
180 return s;
182 out_delete_spec:
183 script_spec__delete(s);
185 return NULL;
188 int script_spec_register(const char *spec, struct scripting_ops *ops)
190 struct script_spec *s;
192 s = script_spec__find(spec);
193 if (s)
194 return -1;
196 s = script_spec__findnew(spec, ops);
197 if (!s)
198 return -1;
200 return 0;
203 static struct scripting_ops *script_spec__lookup(const char *spec)
205 struct script_spec *s = script_spec__find(spec);
206 if (!s)
207 return NULL;
209 return s->ops;
212 static void list_available_languages(void)
214 struct script_spec *s;
216 fprintf(stderr, "\n");
217 fprintf(stderr, "Scripting language extensions (used in "
218 "perf trace -s [spec:]script.[spec]):\n\n");
220 list_for_each_entry(s, &script_specs, node)
221 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
223 fprintf(stderr, "\n");
226 static int parse_scriptname(const struct option *opt __used,
227 const char *str, int unset __used)
229 char spec[PATH_MAX];
230 const char *script, *ext;
231 int len;
233 if (strcmp(str, "lang") == 0) {
234 list_available_languages();
235 exit(0);
238 script = strchr(str, ':');
239 if (script) {
240 len = script - str;
241 if (len >= PATH_MAX) {
242 fprintf(stderr, "invalid language specifier");
243 return -1;
245 strncpy(spec, str, len);
246 spec[len] = '\0';
247 scripting_ops = script_spec__lookup(spec);
248 if (!scripting_ops) {
249 fprintf(stderr, "invalid language specifier");
250 return -1;
252 script++;
253 } else {
254 script = str;
255 ext = strchr(script, '.');
256 if (!ext) {
257 fprintf(stderr, "invalid script extension");
258 return -1;
260 scripting_ops = script_spec__lookup(++ext);
261 if (!scripting_ops) {
262 fprintf(stderr, "invalid script extension");
263 return -1;
267 script_name = strdup(script);
269 return 0;
272 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
273 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
274 lang_next) \
275 if (lang_dirent.d_type == DT_DIR && \
276 (strcmp(lang_dirent.d_name, ".")) && \
277 (strcmp(lang_dirent.d_name, "..")))
279 #define for_each_script(lang_dir, script_dirent, script_next) \
280 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
281 script_next) \
282 if (script_dirent.d_type != DT_DIR)
285 #define RECORD_SUFFIX "-record"
286 #define REPORT_SUFFIX "-report"
288 struct script_desc {
289 struct list_head node;
290 char *name;
291 char *half_liner;
292 char *args;
295 LIST_HEAD(script_descs);
297 static struct script_desc *script_desc__new(const char *name)
299 struct script_desc *s = zalloc(sizeof(*s));
301 if (s != NULL)
302 s->name = strdup(name);
304 return s;
307 static void script_desc__delete(struct script_desc *s)
309 free(s->name);
310 free(s);
313 static void script_desc__add(struct script_desc *s)
315 list_add_tail(&s->node, &script_descs);
318 static struct script_desc *script_desc__find(const char *name)
320 struct script_desc *s;
322 list_for_each_entry(s, &script_descs, node)
323 if (strcasecmp(s->name, name) == 0)
324 return s;
325 return NULL;
328 static struct script_desc *script_desc__findnew(const char *name)
330 struct script_desc *s = script_desc__find(name);
332 if (s)
333 return s;
335 s = script_desc__new(name);
336 if (!s)
337 goto out_delete_desc;
339 script_desc__add(s);
341 return s;
343 out_delete_desc:
344 script_desc__delete(s);
346 return NULL;
349 static char *ends_with(char *str, const char *suffix)
351 size_t suffix_len = strlen(suffix);
352 char *p = str;
354 if (strlen(str) > suffix_len) {
355 p = str + strlen(str) - suffix_len;
356 if (!strncmp(p, suffix, suffix_len))
357 return p;
360 return NULL;
363 static char *ltrim(char *str)
365 int len = strlen(str);
367 while (len && isspace(*str)) {
368 len--;
369 str++;
372 return str;
375 static int read_script_info(struct script_desc *desc, const char *filename)
377 char line[BUFSIZ], *p;
378 FILE *fp;
380 fp = fopen(filename, "r");
381 if (!fp)
382 return -1;
384 while (fgets(line, sizeof(line), fp)) {
385 p = ltrim(line);
386 if (strlen(p) == 0)
387 continue;
388 if (*p != '#')
389 continue;
390 p++;
391 if (strlen(p) && *p == '!')
392 continue;
394 p = ltrim(p);
395 if (strlen(p) && p[strlen(p) - 1] == '\n')
396 p[strlen(p) - 1] = '\0';
398 if (!strncmp(p, "description:", strlen("description:"))) {
399 p += strlen("description:");
400 desc->half_liner = strdup(ltrim(p));
401 continue;
404 if (!strncmp(p, "args:", strlen("args:"))) {
405 p += strlen("args:");
406 desc->args = strdup(ltrim(p));
407 continue;
411 fclose(fp);
413 return 0;
416 static int list_available_scripts(const struct option *opt __used,
417 const char *s __used, int unset __used)
419 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
420 char scripts_path[MAXPATHLEN];
421 DIR *scripts_dir, *lang_dir;
422 char script_path[MAXPATHLEN];
423 char lang_path[MAXPATHLEN];
424 struct script_desc *desc;
425 char first_half[BUFSIZ];
426 char *script_root;
427 char *str;
429 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
431 scripts_dir = opendir(scripts_path);
432 if (!scripts_dir)
433 return -1;
435 for_each_lang(scripts_dir, lang_dirent, lang_next) {
436 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
437 lang_dirent.d_name);
438 lang_dir = opendir(lang_path);
439 if (!lang_dir)
440 continue;
442 for_each_script(lang_dir, script_dirent, script_next) {
443 script_root = strdup(script_dirent.d_name);
444 str = ends_with(script_root, REPORT_SUFFIX);
445 if (str) {
446 *str = '\0';
447 desc = script_desc__findnew(script_root);
448 snprintf(script_path, MAXPATHLEN, "%s/%s",
449 lang_path, script_dirent.d_name);
450 read_script_info(desc, script_path);
452 free(script_root);
456 fprintf(stdout, "List of available trace scripts:\n");
457 list_for_each_entry(desc, &script_descs, node) {
458 sprintf(first_half, "%s %s", desc->name,
459 desc->args ? desc->args : "");
460 fprintf(stdout, " %-36s %s\n", first_half,
461 desc->half_liner ? desc->half_liner : "");
464 exit(0);
467 static char *get_script_path(const char *script_root, const char *suffix)
469 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
470 char scripts_path[MAXPATHLEN];
471 char script_path[MAXPATHLEN];
472 DIR *scripts_dir, *lang_dir;
473 char lang_path[MAXPATHLEN];
474 char *str, *__script_root;
475 char *path = NULL;
477 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
479 scripts_dir = opendir(scripts_path);
480 if (!scripts_dir)
481 return NULL;
483 for_each_lang(scripts_dir, lang_dirent, lang_next) {
484 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
485 lang_dirent.d_name);
486 lang_dir = opendir(lang_path);
487 if (!lang_dir)
488 continue;
490 for_each_script(lang_dir, script_dirent, script_next) {
491 __script_root = strdup(script_dirent.d_name);
492 str = ends_with(__script_root, suffix);
493 if (str) {
494 *str = '\0';
495 if (strcmp(__script_root, script_root))
496 continue;
497 snprintf(script_path, MAXPATHLEN, "%s/%s",
498 lang_path, script_dirent.d_name);
499 path = strdup(script_path);
500 free(__script_root);
501 break;
503 free(__script_root);
507 return path;
510 static const char * const trace_usage[] = {
511 "perf trace [<options>] <command>",
512 NULL
515 static const struct option options[] = {
516 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
517 "dump raw trace in ASCII"),
518 OPT_INCR('v', "verbose", &verbose,
519 "be more verbose (show symbol address, etc)"),
520 OPT_BOOLEAN('L', "Latency", &latency_format,
521 "show latency attributes (irqs/preemption disabled, etc)"),
522 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
523 list_available_scripts),
524 OPT_CALLBACK('s', "script", NULL, "name",
525 "script file name (lang:script name, script name, or *)",
526 parse_scriptname),
527 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
528 "generate perf-trace.xx script in specified language"),
529 OPT_STRING('i', "input", &input_name, "file",
530 "input file name"),
532 OPT_END()
535 int cmd_trace(int argc, const char **argv, const char *prefix __used)
537 struct perf_session *session;
538 const char *suffix = NULL;
539 const char **__argv;
540 char *script_path;
541 int i, err;
543 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
544 if (argc < 3) {
545 fprintf(stderr,
546 "Please specify a record script\n");
547 return -1;
549 suffix = RECORD_SUFFIX;
552 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
553 if (argc < 3) {
554 fprintf(stderr,
555 "Please specify a report script\n");
556 return -1;
558 suffix = REPORT_SUFFIX;
561 if (suffix) {
562 script_path = get_script_path(argv[2], suffix);
563 if (!script_path) {
564 fprintf(stderr, "script not found\n");
565 return -1;
568 __argv = malloc((argc + 1) * sizeof(const char *));
569 __argv[0] = "/bin/sh";
570 __argv[1] = script_path;
571 for (i = 3; i < argc; i++)
572 __argv[i - 1] = argv[i];
573 __argv[argc - 1] = NULL;
575 execvp("/bin/sh", (char **)__argv);
576 exit(-1);
579 setup_scripting();
581 argc = parse_options(argc, argv, options, trace_usage,
582 PARSE_OPT_STOP_AT_NON_OPTION);
584 if (symbol__init() < 0)
585 return -1;
586 if (!script_name)
587 setup_pager();
589 session = perf_session__new(input_name, O_RDONLY, 0);
590 if (session == NULL)
591 return -ENOMEM;
593 if (strcmp(input_name, "-") &&
594 !perf_session__has_traces(session, "record -R"))
595 return -EINVAL;
597 if (generate_script_lang) {
598 struct stat perf_stat;
600 int input = open(input_name, O_RDONLY);
601 if (input < 0) {
602 perror("failed to open file");
603 exit(-1);
606 err = fstat(input, &perf_stat);
607 if (err < 0) {
608 perror("failed to stat file");
609 exit(-1);
612 if (!perf_stat.st_size) {
613 fprintf(stderr, "zero-sized file, nothing to do!\n");
614 exit(0);
617 scripting_ops = script_spec__lookup(generate_script_lang);
618 if (!scripting_ops) {
619 fprintf(stderr, "invalid language specifier");
620 return -1;
623 err = scripting_ops->generate_script("perf-trace");
624 goto out;
627 if (script_name) {
628 err = scripting_ops->start_script(script_name, argc, argv);
629 if (err)
630 goto out;
633 err = __cmd_trace(session);
635 perf_session__delete(session);
636 cleanup_scripting();
637 out:
638 return err;