blob: 1509744429c8df1cacc3a9c55749ee8b8a4abc68 [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,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500107 .attr = event__process_attr,
Tom Zanussicd19a032010-04-01 23:59:20 -0500108 .event_type = event__process_event_type,
Tom Zanussi92155452010-04-01 23:59:21 -0500109 .tracing_data = event__process_tracing_data,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200110};
111
Tom Zanussic239da32010-04-01 23:59:18 -0500112extern volatile int session_done;
113
114static void sig_handler(int sig __unused)
115{
116 session_done = 1;
117}
118
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200119static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200120{
Tom Zanussic239da32010-04-01 23:59:18 -0500121 signal(SIGINT, sig_handler);
122
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200123 return perf_session__process_events(session, &event_ops);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200124}
125
Tom Zanussi956ffd02009-11-25 01:15:46 -0600126struct script_spec {
127 struct list_head node;
128 struct scripting_ops *ops;
129 char spec[0];
130};
131
132LIST_HEAD(script_specs);
133
134static struct script_spec *script_spec__new(const char *spec,
135 struct scripting_ops *ops)
136{
137 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
138
139 if (s != NULL) {
140 strcpy(s->spec, spec);
141 s->ops = ops;
142 }
143
144 return s;
145}
146
147static void script_spec__delete(struct script_spec *s)
148{
149 free(s->spec);
150 free(s);
151}
152
153static void script_spec__add(struct script_spec *s)
154{
155 list_add_tail(&s->node, &script_specs);
156}
157
158static struct script_spec *script_spec__find(const char *spec)
159{
160 struct script_spec *s;
161
162 list_for_each_entry(s, &script_specs, node)
163 if (strcasecmp(s->spec, spec) == 0)
164 return s;
165 return NULL;
166}
167
168static struct script_spec *script_spec__findnew(const char *spec,
169 struct scripting_ops *ops)
170{
171 struct script_spec *s = script_spec__find(spec);
172
173 if (s)
174 return s;
175
176 s = script_spec__new(spec, ops);
177 if (!s)
178 goto out_delete_spec;
179
180 script_spec__add(s);
181
182 return s;
183
184out_delete_spec:
185 script_spec__delete(s);
186
187 return NULL;
188}
189
190int script_spec_register(const char *spec, struct scripting_ops *ops)
191{
192 struct script_spec *s;
193
194 s = script_spec__find(spec);
195 if (s)
196 return -1;
197
198 s = script_spec__findnew(spec, ops);
199 if (!s)
200 return -1;
201
202 return 0;
203}
204
205static struct scripting_ops *script_spec__lookup(const char *spec)
206{
207 struct script_spec *s = script_spec__find(spec);
208 if (!s)
209 return NULL;
210
211 return s->ops;
212}
213
214static void list_available_languages(void)
215{
216 struct script_spec *s;
217
218 fprintf(stderr, "\n");
219 fprintf(stderr, "Scripting language extensions (used in "
220 "perf trace -s [spec:]script.[spec]):\n\n");
221
222 list_for_each_entry(s, &script_specs, node)
223 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
224
225 fprintf(stderr, "\n");
226}
227
228static int parse_scriptname(const struct option *opt __used,
229 const char *str, int unset __used)
230{
231 char spec[PATH_MAX];
232 const char *script, *ext;
233 int len;
234
Tom Zanussif526d682010-01-27 02:27:52 -0600235 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600236 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600237 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600238 }
239
240 script = strchr(str, ':');
241 if (script) {
242 len = script - str;
243 if (len >= PATH_MAX) {
244 fprintf(stderr, "invalid language specifier");
245 return -1;
246 }
247 strncpy(spec, str, len);
248 spec[len] = '\0';
249 scripting_ops = script_spec__lookup(spec);
250 if (!scripting_ops) {
251 fprintf(stderr, "invalid language specifier");
252 return -1;
253 }
254 script++;
255 } else {
256 script = str;
257 ext = strchr(script, '.');
258 if (!ext) {
259 fprintf(stderr, "invalid script extension");
260 return -1;
261 }
262 scripting_ops = script_spec__lookup(++ext);
263 if (!scripting_ops) {
264 fprintf(stderr, "invalid script extension");
265 return -1;
266 }
267 }
268
269 script_name = strdup(script);
270
271 return 0;
272}
273
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600274#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
275 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
276 lang_next) \
277 if (lang_dirent.d_type == DT_DIR && \
278 (strcmp(lang_dirent.d_name, ".")) && \
279 (strcmp(lang_dirent.d_name, "..")))
280
281#define for_each_script(lang_dir, script_dirent, script_next) \
282 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
283 script_next) \
284 if (script_dirent.d_type != DT_DIR)
285
286
287#define RECORD_SUFFIX "-record"
288#define REPORT_SUFFIX "-report"
289
290struct script_desc {
291 struct list_head node;
292 char *name;
293 char *half_liner;
294 char *args;
295};
296
297LIST_HEAD(script_descs);
298
299static struct script_desc *script_desc__new(const char *name)
300{
301 struct script_desc *s = zalloc(sizeof(*s));
302
303 if (s != NULL)
304 s->name = strdup(name);
305
306 return s;
307}
308
309static void script_desc__delete(struct script_desc *s)
310{
311 free(s->name);
312 free(s);
313}
314
315static void script_desc__add(struct script_desc *s)
316{
317 list_add_tail(&s->node, &script_descs);
318}
319
320static struct script_desc *script_desc__find(const char *name)
321{
322 struct script_desc *s;
323
324 list_for_each_entry(s, &script_descs, node)
325 if (strcasecmp(s->name, name) == 0)
326 return s;
327 return NULL;
328}
329
330static struct script_desc *script_desc__findnew(const char *name)
331{
332 struct script_desc *s = script_desc__find(name);
333
334 if (s)
335 return s;
336
337 s = script_desc__new(name);
338 if (!s)
339 goto out_delete_desc;
340
341 script_desc__add(s);
342
343 return s;
344
345out_delete_desc:
346 script_desc__delete(s);
347
348 return NULL;
349}
350
351static char *ends_with(char *str, const char *suffix)
352{
353 size_t suffix_len = strlen(suffix);
354 char *p = str;
355
356 if (strlen(str) > suffix_len) {
357 p = str + strlen(str) - suffix_len;
358 if (!strncmp(p, suffix, suffix_len))
359 return p;
360 }
361
362 return NULL;
363}
364
365static char *ltrim(char *str)
366{
367 int len = strlen(str);
368
369 while (len && isspace(*str)) {
370 len--;
371 str++;
372 }
373
374 return str;
375}
376
377static int read_script_info(struct script_desc *desc, const char *filename)
378{
379 char line[BUFSIZ], *p;
380 FILE *fp;
381
382 fp = fopen(filename, "r");
383 if (!fp)
384 return -1;
385
386 while (fgets(line, sizeof(line), fp)) {
387 p = ltrim(line);
388 if (strlen(p) == 0)
389 continue;
390 if (*p != '#')
391 continue;
392 p++;
393 if (strlen(p) && *p == '!')
394 continue;
395
396 p = ltrim(p);
397 if (strlen(p) && p[strlen(p) - 1] == '\n')
398 p[strlen(p) - 1] = '\0';
399
400 if (!strncmp(p, "description:", strlen("description:"))) {
401 p += strlen("description:");
402 desc->half_liner = strdup(ltrim(p));
403 continue;
404 }
405
406 if (!strncmp(p, "args:", strlen("args:"))) {
407 p += strlen("args:");
408 desc->args = strdup(ltrim(p));
409 continue;
410 }
411 }
412
413 fclose(fp);
414
415 return 0;
416}
417
418static int list_available_scripts(const struct option *opt __used,
419 const char *s __used, int unset __used)
420{
421 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
422 char scripts_path[MAXPATHLEN];
423 DIR *scripts_dir, *lang_dir;
424 char script_path[MAXPATHLEN];
425 char lang_path[MAXPATHLEN];
426 struct script_desc *desc;
427 char first_half[BUFSIZ];
428 char *script_root;
429 char *str;
430
431 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
432
433 scripts_dir = opendir(scripts_path);
434 if (!scripts_dir)
435 return -1;
436
437 for_each_lang(scripts_dir, lang_dirent, lang_next) {
438 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
439 lang_dirent.d_name);
440 lang_dir = opendir(lang_path);
441 if (!lang_dir)
442 continue;
443
444 for_each_script(lang_dir, script_dirent, script_next) {
445 script_root = strdup(script_dirent.d_name);
446 str = ends_with(script_root, REPORT_SUFFIX);
447 if (str) {
448 *str = '\0';
449 desc = script_desc__findnew(script_root);
450 snprintf(script_path, MAXPATHLEN, "%s/%s",
451 lang_path, script_dirent.d_name);
452 read_script_info(desc, script_path);
453 }
454 free(script_root);
455 }
456 }
457
458 fprintf(stdout, "List of available trace scripts:\n");
459 list_for_each_entry(desc, &script_descs, node) {
460 sprintf(first_half, "%s %s", desc->name,
461 desc->args ? desc->args : "");
462 fprintf(stdout, " %-36s %s\n", first_half,
463 desc->half_liner ? desc->half_liner : "");
464 }
465
466 exit(0);
467}
468
Tom Zanussi38752942009-12-15 02:53:39 -0600469static char *get_script_path(const char *script_root, const char *suffix)
470{
471 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
472 char scripts_path[MAXPATHLEN];
473 char script_path[MAXPATHLEN];
474 DIR *scripts_dir, *lang_dir;
475 char lang_path[MAXPATHLEN];
476 char *str, *__script_root;
477 char *path = NULL;
478
479 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
480
481 scripts_dir = opendir(scripts_path);
482 if (!scripts_dir)
483 return NULL;
484
485 for_each_lang(scripts_dir, lang_dirent, lang_next) {
486 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
487 lang_dirent.d_name);
488 lang_dir = opendir(lang_path);
489 if (!lang_dir)
490 continue;
491
492 for_each_script(lang_dir, script_dirent, script_next) {
493 __script_root = strdup(script_dirent.d_name);
494 str = ends_with(__script_root, suffix);
495 if (str) {
496 *str = '\0';
497 if (strcmp(__script_root, script_root))
498 continue;
499 snprintf(script_path, MAXPATHLEN, "%s/%s",
500 lang_path, script_dirent.d_name);
501 path = strdup(script_path);
502 free(__script_root);
503 break;
504 }
505 free(__script_root);
506 }
507 }
508
509 return path;
510}
511
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200512static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200513 "perf trace [<options>] <command>",
514 NULL
515};
516
517static const struct option options[] = {
518 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
519 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000520 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200521 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600522 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400523 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600524 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
525 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600526 OPT_CALLBACK('s', "script", NULL, "name",
527 "script file name (lang:script name, script name, or *)",
528 parse_scriptname),
529 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
530 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900531 OPT_STRING('i', "input", &input_name, "file",
532 "input file name"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600533
Masami Hiramatsu19096292009-08-21 14:56:03 -0400534 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200535};
536
537int cmd_trace(int argc, const char **argv, const char *prefix __used)
538{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200539 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600540 const char *suffix = NULL;
541 const char **__argv;
542 char *script_path;
543 int i, err;
544
545 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
546 if (argc < 3) {
547 fprintf(stderr,
548 "Please specify a record script\n");
549 return -1;
550 }
551 suffix = RECORD_SUFFIX;
552 }
553
554 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
555 if (argc < 3) {
556 fprintf(stderr,
557 "Please specify a report script\n");
558 return -1;
559 }
560 suffix = REPORT_SUFFIX;
561 }
562
563 if (suffix) {
564 script_path = get_script_path(argv[2], suffix);
565 if (!script_path) {
566 fprintf(stderr, "script not found\n");
567 return -1;
568 }
569
570 __argv = malloc((argc + 1) * sizeof(const char *));
571 __argv[0] = "/bin/sh";
572 __argv[1] = script_path;
573 for (i = 3; i < argc; i++)
574 __argv[i - 1] = argv[i];
575 __argv[argc - 1] = NULL;
576
577 execvp("/bin/sh", (char **)__argv);
578 exit(-1);
579 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600580
Tom Zanussi956ffd02009-11-25 01:15:46 -0600581 setup_scripting();
582
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200583 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600584 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200585
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200586 if (symbol__init() < 0)
587 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600588 if (!script_name)
589 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200590
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200591 session = perf_session__new(input_name, O_RDONLY, 0);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200592 if (session == NULL)
593 return -ENOMEM;
594
Tom Zanussic239da32010-04-01 23:59:18 -0500595 if (strcmp(input_name, "-") &&
596 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200597 return -EINVAL;
598
Tom Zanussi956ffd02009-11-25 01:15:46 -0600599 if (generate_script_lang) {
600 struct stat perf_stat;
601
602 int input = open(input_name, O_RDONLY);
603 if (input < 0) {
604 perror("failed to open file");
605 exit(-1);
606 }
607
608 err = fstat(input, &perf_stat);
609 if (err < 0) {
610 perror("failed to stat file");
611 exit(-1);
612 }
613
614 if (!perf_stat.st_size) {
615 fprintf(stderr, "zero-sized file, nothing to do!\n");
616 exit(0);
617 }
618
619 scripting_ops = script_spec__lookup(generate_script_lang);
620 if (!scripting_ops) {
621 fprintf(stderr, "invalid language specifier");
622 return -1;
623 }
624
Tom Zanussi956ffd02009-11-25 01:15:46 -0600625 err = scripting_ops->generate_script("perf-trace");
626 goto out;
627 }
628
629 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600630 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600631 if (err)
632 goto out;
633 }
634
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200635 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600636
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200637 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600638 cleanup_scripting();
639out:
640 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200641}