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