blob: c681e85a912ca5d75b17515fef4197319bd567d5 [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();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060047 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -060048
Tom Zanussi956ffd02009-11-25 01:15:46 -060049 scripting_ops = &default_scripting_ops;
50}
51
52static int cleanup_scripting(void)
53{
54 return scripting_ops->stop_script();
55}
56
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020057#include "util/parse-options.h"
58
59#include "perf.h"
60#include "util/debug.h"
61
62#include "util/trace-event.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060063#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020064
Tom Zanussi956ffd02009-11-25 01:15:46 -060065static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020066
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020067static int process_sample_event(event_t *event, struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090069 struct sample_data data;
70 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090072 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020076
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020077 event__parse_sample(event, session->sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020078
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -020079 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
80 data.pid, data.tid, data.ip, data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020081
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020082 thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020084 pr_debug("problem processing %d event, skipping it.\n",
85 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020086 return -1;
87 }
88
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020089 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020090 /*
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
94 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090095 scripting_ops->process_event(data.cpu, data.raw_data,
96 data.raw_size,
97 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020098 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020099
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200100 session->events_stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200101 return 0;
102}
103
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200104static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200105 .sample = process_sample_event,
106 .comm = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200107};
108
Tom Zanussic239da32010-04-01 23:59:18 -0500109extern volatile int session_done;
110
111static void sig_handler(int sig __unused)
112{
113 session_done = 1;
114}
115
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200116static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200117{
Tom Zanussic239da32010-04-01 23:59:18 -0500118 signal(SIGINT, sig_handler);
119
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200120 return perf_session__process_events(session, &event_ops);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200121}
122
Tom Zanussi956ffd02009-11-25 01:15:46 -0600123struct script_spec {
124 struct list_head node;
125 struct scripting_ops *ops;
126 char spec[0];
127};
128
129LIST_HEAD(script_specs);
130
131static struct script_spec *script_spec__new(const char *spec,
132 struct scripting_ops *ops)
133{
134 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
135
136 if (s != NULL) {
137 strcpy(s->spec, spec);
138 s->ops = ops;
139 }
140
141 return s;
142}
143
144static void script_spec__delete(struct script_spec *s)
145{
146 free(s->spec);
147 free(s);
148}
149
150static void script_spec__add(struct script_spec *s)
151{
152 list_add_tail(&s->node, &script_specs);
153}
154
155static struct script_spec *script_spec__find(const char *spec)
156{
157 struct script_spec *s;
158
159 list_for_each_entry(s, &script_specs, node)
160 if (strcasecmp(s->spec, spec) == 0)
161 return s;
162 return NULL;
163}
164
165static struct script_spec *script_spec__findnew(const char *spec,
166 struct scripting_ops *ops)
167{
168 struct script_spec *s = script_spec__find(spec);
169
170 if (s)
171 return s;
172
173 s = script_spec__new(spec, ops);
174 if (!s)
175 goto out_delete_spec;
176
177 script_spec__add(s);
178
179 return s;
180
181out_delete_spec:
182 script_spec__delete(s);
183
184 return NULL;
185}
186
187int script_spec_register(const char *spec, struct scripting_ops *ops)
188{
189 struct script_spec *s;
190
191 s = script_spec__find(spec);
192 if (s)
193 return -1;
194
195 s = script_spec__findnew(spec, ops);
196 if (!s)
197 return -1;
198
199 return 0;
200}
201
202static struct scripting_ops *script_spec__lookup(const char *spec)
203{
204 struct script_spec *s = script_spec__find(spec);
205 if (!s)
206 return NULL;
207
208 return s->ops;
209}
210
211static void list_available_languages(void)
212{
213 struct script_spec *s;
214
215 fprintf(stderr, "\n");
216 fprintf(stderr, "Scripting language extensions (used in "
217 "perf trace -s [spec:]script.[spec]):\n\n");
218
219 list_for_each_entry(s, &script_specs, node)
220 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
221
222 fprintf(stderr, "\n");
223}
224
225static int parse_scriptname(const struct option *opt __used,
226 const char *str, int unset __used)
227{
228 char spec[PATH_MAX];
229 const char *script, *ext;
230 int len;
231
Tom Zanussif526d682010-01-27 02:27:52 -0600232 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600233 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600234 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600235 }
236
237 script = strchr(str, ':');
238 if (script) {
239 len = script - str;
240 if (len >= PATH_MAX) {
241 fprintf(stderr, "invalid language specifier");
242 return -1;
243 }
244 strncpy(spec, str, len);
245 spec[len] = '\0';
246 scripting_ops = script_spec__lookup(spec);
247 if (!scripting_ops) {
248 fprintf(stderr, "invalid language specifier");
249 return -1;
250 }
251 script++;
252 } else {
253 script = str;
254 ext = strchr(script, '.');
255 if (!ext) {
256 fprintf(stderr, "invalid script extension");
257 return -1;
258 }
259 scripting_ops = script_spec__lookup(++ext);
260 if (!scripting_ops) {
261 fprintf(stderr, "invalid script extension");
262 return -1;
263 }
264 }
265
266 script_name = strdup(script);
267
268 return 0;
269}
270
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600271#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
272 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
273 lang_next) \
274 if (lang_dirent.d_type == DT_DIR && \
275 (strcmp(lang_dirent.d_name, ".")) && \
276 (strcmp(lang_dirent.d_name, "..")))
277
278#define for_each_script(lang_dir, script_dirent, script_next) \
279 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
280 script_next) \
281 if (script_dirent.d_type != DT_DIR)
282
283
284#define RECORD_SUFFIX "-record"
285#define REPORT_SUFFIX "-report"
286
287struct script_desc {
288 struct list_head node;
289 char *name;
290 char *half_liner;
291 char *args;
292};
293
294LIST_HEAD(script_descs);
295
296static struct script_desc *script_desc__new(const char *name)
297{
298 struct script_desc *s = zalloc(sizeof(*s));
299
300 if (s != NULL)
301 s->name = strdup(name);
302
303 return s;
304}
305
306static void script_desc__delete(struct script_desc *s)
307{
308 free(s->name);
309 free(s);
310}
311
312static void script_desc__add(struct script_desc *s)
313{
314 list_add_tail(&s->node, &script_descs);
315}
316
317static struct script_desc *script_desc__find(const char *name)
318{
319 struct script_desc *s;
320
321 list_for_each_entry(s, &script_descs, node)
322 if (strcasecmp(s->name, name) == 0)
323 return s;
324 return NULL;
325}
326
327static struct script_desc *script_desc__findnew(const char *name)
328{
329 struct script_desc *s = script_desc__find(name);
330
331 if (s)
332 return s;
333
334 s = script_desc__new(name);
335 if (!s)
336 goto out_delete_desc;
337
338 script_desc__add(s);
339
340 return s;
341
342out_delete_desc:
343 script_desc__delete(s);
344
345 return NULL;
346}
347
348static char *ends_with(char *str, const char *suffix)
349{
350 size_t suffix_len = strlen(suffix);
351 char *p = str;
352
353 if (strlen(str) > suffix_len) {
354 p = str + strlen(str) - suffix_len;
355 if (!strncmp(p, suffix, suffix_len))
356 return p;
357 }
358
359 return NULL;
360}
361
362static char *ltrim(char *str)
363{
364 int len = strlen(str);
365
366 while (len && isspace(*str)) {
367 len--;
368 str++;
369 }
370
371 return str;
372}
373
374static int read_script_info(struct script_desc *desc, const char *filename)
375{
376 char line[BUFSIZ], *p;
377 FILE *fp;
378
379 fp = fopen(filename, "r");
380 if (!fp)
381 return -1;
382
383 while (fgets(line, sizeof(line), fp)) {
384 p = ltrim(line);
385 if (strlen(p) == 0)
386 continue;
387 if (*p != '#')
388 continue;
389 p++;
390 if (strlen(p) && *p == '!')
391 continue;
392
393 p = ltrim(p);
394 if (strlen(p) && p[strlen(p) - 1] == '\n')
395 p[strlen(p) - 1] = '\0';
396
397 if (!strncmp(p, "description:", strlen("description:"))) {
398 p += strlen("description:");
399 desc->half_liner = strdup(ltrim(p));
400 continue;
401 }
402
403 if (!strncmp(p, "args:", strlen("args:"))) {
404 p += strlen("args:");
405 desc->args = strdup(ltrim(p));
406 continue;
407 }
408 }
409
410 fclose(fp);
411
412 return 0;
413}
414
415static int list_available_scripts(const struct option *opt __used,
416 const char *s __used, int unset __used)
417{
418 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
419 char scripts_path[MAXPATHLEN];
420 DIR *scripts_dir, *lang_dir;
421 char script_path[MAXPATHLEN];
422 char lang_path[MAXPATHLEN];
423 struct script_desc *desc;
424 char first_half[BUFSIZ];
425 char *script_root;
426 char *str;
427
428 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
429
430 scripts_dir = opendir(scripts_path);
431 if (!scripts_dir)
432 return -1;
433
434 for_each_lang(scripts_dir, lang_dirent, lang_next) {
435 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
436 lang_dirent.d_name);
437 lang_dir = opendir(lang_path);
438 if (!lang_dir)
439 continue;
440
441 for_each_script(lang_dir, script_dirent, script_next) {
442 script_root = strdup(script_dirent.d_name);
443 str = ends_with(script_root, REPORT_SUFFIX);
444 if (str) {
445 *str = '\0';
446 desc = script_desc__findnew(script_root);
447 snprintf(script_path, MAXPATHLEN, "%s/%s",
448 lang_path, script_dirent.d_name);
449 read_script_info(desc, script_path);
450 }
451 free(script_root);
452 }
453 }
454
455 fprintf(stdout, "List of available trace scripts:\n");
456 list_for_each_entry(desc, &script_descs, node) {
457 sprintf(first_half, "%s %s", desc->name,
458 desc->args ? desc->args : "");
459 fprintf(stdout, " %-36s %s\n", first_half,
460 desc->half_liner ? desc->half_liner : "");
461 }
462
463 exit(0);
464}
465
Tom Zanussi38752942009-12-15 02:53:39 -0600466static char *get_script_path(const char *script_root, const char *suffix)
467{
468 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
469 char scripts_path[MAXPATHLEN];
470 char script_path[MAXPATHLEN];
471 DIR *scripts_dir, *lang_dir;
472 char lang_path[MAXPATHLEN];
473 char *str, *__script_root;
474 char *path = NULL;
475
476 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
477
478 scripts_dir = opendir(scripts_path);
479 if (!scripts_dir)
480 return NULL;
481
482 for_each_lang(scripts_dir, lang_dirent, lang_next) {
483 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
484 lang_dirent.d_name);
485 lang_dir = opendir(lang_path);
486 if (!lang_dir)
487 continue;
488
489 for_each_script(lang_dir, script_dirent, script_next) {
490 __script_root = strdup(script_dirent.d_name);
491 str = ends_with(__script_root, suffix);
492 if (str) {
493 *str = '\0';
494 if (strcmp(__script_root, script_root))
495 continue;
496 snprintf(script_path, MAXPATHLEN, "%s/%s",
497 lang_path, script_dirent.d_name);
498 path = strdup(script_path);
499 free(__script_root);
500 break;
501 }
502 free(__script_root);
503 }
504 }
505
506 return path;
507}
508
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200509static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200510 "perf trace [<options>] <command>",
511 NULL
512};
513
514static const struct option options[] = {
515 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
516 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000517 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200518 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600519 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400520 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600521 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
522 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600523 OPT_CALLBACK('s', "script", NULL, "name",
524 "script file name (lang:script name, script name, or *)",
525 parse_scriptname),
526 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
527 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900528 OPT_STRING('i', "input", &input_name, "file",
529 "input file name"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600530
Masami Hiramatsu19096292009-08-21 14:56:03 -0400531 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200532};
533
534int cmd_trace(int argc, const char **argv, const char *prefix __used)
535{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200536 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600537 const char *suffix = NULL;
538 const char **__argv;
539 char *script_path;
540 int i, err;
541
542 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
543 if (argc < 3) {
544 fprintf(stderr,
545 "Please specify a record script\n");
546 return -1;
547 }
548 suffix = RECORD_SUFFIX;
549 }
550
551 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
552 if (argc < 3) {
553 fprintf(stderr,
554 "Please specify a report script\n");
555 return -1;
556 }
557 suffix = REPORT_SUFFIX;
558 }
559
560 if (suffix) {
561 script_path = get_script_path(argv[2], suffix);
562 if (!script_path) {
563 fprintf(stderr, "script not found\n");
564 return -1;
565 }
566
567 __argv = malloc((argc + 1) * sizeof(const char *));
568 __argv[0] = "/bin/sh";
569 __argv[1] = script_path;
570 for (i = 3; i < argc; i++)
571 __argv[i - 1] = argv[i];
572 __argv[argc - 1] = NULL;
573
574 execvp("/bin/sh", (char **)__argv);
575 exit(-1);
576 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600577
Tom Zanussi956ffd02009-11-25 01:15:46 -0600578 setup_scripting();
579
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200580 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600581 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200582
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200583 if (symbol__init() < 0)
584 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600585 if (!script_name)
586 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200587
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200588 session = perf_session__new(input_name, O_RDONLY, 0);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200589 if (session == NULL)
590 return -ENOMEM;
591
Tom Zanussic239da32010-04-01 23:59:18 -0500592 if (strcmp(input_name, "-") &&
593 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200594 return -EINVAL;
595
Tom Zanussi956ffd02009-11-25 01:15:46 -0600596 if (generate_script_lang) {
597 struct stat perf_stat;
598
599 int input = open(input_name, O_RDONLY);
600 if (input < 0) {
601 perror("failed to open file");
602 exit(-1);
603 }
604
605 err = fstat(input, &perf_stat);
606 if (err < 0) {
607 perror("failed to stat file");
608 exit(-1);
609 }
610
611 if (!perf_stat.st_size) {
612 fprintf(stderr, "zero-sized file, nothing to do!\n");
613 exit(0);
614 }
615
616 scripting_ops = script_spec__lookup(generate_script_lang);
617 if (!scripting_ops) {
618 fprintf(stderr, "invalid language specifier");
619 return -1;
620 }
621
Tom Zanussi956ffd02009-11-25 01:15:46 -0600622 err = scripting_ops->generate_script("perf-trace");
623 goto out;
624 }
625
626 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600627 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600628 if (err)
629 goto out;
630 }
631
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200632 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600633
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200634 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600635 cleanup_scripting();
636out:
637 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200638}