]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-trace.c
perf trace/scripting: Enable scripting shell scripts for live mode
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
1#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"
cf72344d
IM
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
94c744b6 10#include "util/session.h"
5f9c39dc 11
956ffd02
TZ
12static char const *script_name;
13static char const *generate_script_lang;
14
586bc5cc
TZ
15static int default_start_script(const char *script __unused,
16 int argc __unused,
17 const char **argv __unused)
956ffd02
TZ
18{
19 return 0;
20}
21
22static int default_stop_script(void)
23{
24 return 0;
25}
26
586bc5cc 27static int default_generate_script(const char *outfile __unused)
956ffd02
TZ
28{
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
16c632de 46 setup_perl_scripting();
7e4b21b8 47 setup_python_scripting();
16c632de 48
956ffd02
TZ
49 scripting_ops = &default_scripting_ops;
50}
51
52static int cleanup_scripting(void)
53{
54 return scripting_ops->stop_script();
55}
56
5f9c39dc
FW
57#include "util/parse-options.h"
58
59#include "perf.h"
60#include "util/debug.h"
61
62#include "util/trace-event.h"
956ffd02 63#include "util/exec_cmd.h"
5f9c39dc 64
956ffd02 65static char const *input_name = "perf.data";
5f9c39dc 66
b3165f41 67static int process_sample_event(event_t *event, struct perf_session *session)
5f9c39dc 68{
180f95e2
OH
69 struct sample_data data;
70 struct thread *thread;
6ddf259d 71
180f95e2
OH
72 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
cd6feeea 76
c019879b 77 event__parse_sample(event, session->sample_type, &data);
5f9c39dc 78
0d755034
ACM
79 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
80 data.pid, data.tid, data.ip, data.period);
5f9c39dc 81
b3165f41 82 thread = perf_session__findnew(session, event->ip.pid);
5f9c39dc 83 if (thread == NULL) {
6beba7ad
ACM
84 pr_debug("problem processing %d event, skipping it.\n",
85 event->header.type);
5f9c39dc
FW
86 return -1;
87 }
88
c019879b 89 if (session->sample_type & PERF_SAMPLE_RAW) {
5f9c39dc
FW
90 /*
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 */
180f95e2
OH
95 scripting_ops->process_event(data.cpu, data.raw_data,
96 data.raw_size,
97 data.time, thread->comm);
5f9c39dc 98 }
5f9c39dc 99
f823e441 100 session->events_stats.total += data.period;
5f9c39dc
FW
101 return 0;
102}
103
301a0b02 104static struct perf_event_ops event_ops = {
55aa640f
ACM
105 .sample = process_sample_event,
106 .comm = event__process_comm,
2c46dbb5 107 .attr = event__process_attr,
cd19a035 108 .event_type = event__process_event_type,
9215545e 109 .tracing_data = event__process_tracing_data,
c7929e47 110 .build_id = event__process_build_id,
016e92fb
FW
111};
112
c239da3b
TZ
113extern volatile int session_done;
114
115static void sig_handler(int sig __unused)
116{
117 session_done = 1;
118}
119
d8f66248 120static int __cmd_trace(struct perf_session *session)
5f9c39dc 121{
c239da3b
TZ
122 signal(SIGINT, sig_handler);
123
ec913369 124 return perf_session__process_events(session, &event_ops);
5f9c39dc
FW
125}
126
956ffd02
TZ
127struct script_spec {
128 struct list_head node;
129 struct scripting_ops *ops;
130 char spec[0];
131};
132
133LIST_HEAD(script_specs);
134
135static struct script_spec *script_spec__new(const char *spec,
136 struct scripting_ops *ops)
137{
138 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
139
140 if (s != NULL) {
141 strcpy(s->spec, spec);
142 s->ops = ops;
143 }
144
145 return s;
146}
147
148static void script_spec__delete(struct script_spec *s)
149{
150 free(s->spec);
151 free(s);
152}
153
154static void script_spec__add(struct script_spec *s)
155{
156 list_add_tail(&s->node, &script_specs);
157}
158
159static struct script_spec *script_spec__find(const char *spec)
160{
161 struct script_spec *s;
162
163 list_for_each_entry(s, &script_specs, node)
164 if (strcasecmp(s->spec, spec) == 0)
165 return s;
166 return NULL;
167}
168
169static struct script_spec *script_spec__findnew(const char *spec,
170 struct scripting_ops *ops)
171{
172 struct script_spec *s = script_spec__find(spec);
173
174 if (s)
175 return s;
176
177 s = script_spec__new(spec, ops);
178 if (!s)
179 goto out_delete_spec;
180
181 script_spec__add(s);
182
183 return s;
184
185out_delete_spec:
186 script_spec__delete(s);
187
188 return NULL;
189}
190
191int script_spec_register(const char *spec, struct scripting_ops *ops)
192{
193 struct script_spec *s;
194
195 s = script_spec__find(spec);
196 if (s)
197 return -1;
198
199 s = script_spec__findnew(spec, ops);
200 if (!s)
201 return -1;
202
203 return 0;
204}
205
206static struct scripting_ops *script_spec__lookup(const char *spec)
207{
208 struct script_spec *s = script_spec__find(spec);
209 if (!s)
210 return NULL;
211
212 return s->ops;
213}
214
215static void list_available_languages(void)
216{
217 struct script_spec *s;
218
219 fprintf(stderr, "\n");
220 fprintf(stderr, "Scripting language extensions (used in "
221 "perf trace -s [spec:]script.[spec]):\n\n");
222
223 list_for_each_entry(s, &script_specs, node)
224 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
225
226 fprintf(stderr, "\n");
227}
228
229static int parse_scriptname(const struct option *opt __used,
230 const char *str, int unset __used)
231{
232 char spec[PATH_MAX];
233 const char *script, *ext;
234 int len;
235
f526d68b 236 if (strcmp(str, "lang") == 0) {
956ffd02 237 list_available_languages();
f526d68b 238 exit(0);
956ffd02
TZ
239 }
240
241 script = strchr(str, ':');
242 if (script) {
243 len = script - str;
244 if (len >= PATH_MAX) {
245 fprintf(stderr, "invalid language specifier");
246 return -1;
247 }
248 strncpy(spec, str, len);
249 spec[len] = '\0';
250 scripting_ops = script_spec__lookup(spec);
251 if (!scripting_ops) {
252 fprintf(stderr, "invalid language specifier");
253 return -1;
254 }
255 script++;
256 } else {
257 script = str;
258 ext = strchr(script, '.');
259 if (!ext) {
260 fprintf(stderr, "invalid script extension");
261 return -1;
262 }
263 scripting_ops = script_spec__lookup(++ext);
264 if (!scripting_ops) {
265 fprintf(stderr, "invalid script extension");
266 return -1;
267 }
268 }
269
270 script_name = strdup(script);
271
272 return 0;
273}
274
4b9c0c59
TZ
275#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
276 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
277 lang_next) \
278 if (lang_dirent.d_type == DT_DIR && \
279 (strcmp(lang_dirent.d_name, ".")) && \
280 (strcmp(lang_dirent.d_name, "..")))
281
282#define for_each_script(lang_dir, script_dirent, script_next) \
283 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
284 script_next) \
285 if (script_dirent.d_type != DT_DIR)
286
287
288#define RECORD_SUFFIX "-record"
289#define REPORT_SUFFIX "-report"
290
291struct script_desc {
292 struct list_head node;
293 char *name;
294 char *half_liner;
295 char *args;
296};
297
298LIST_HEAD(script_descs);
299
300static struct script_desc *script_desc__new(const char *name)
301{
302 struct script_desc *s = zalloc(sizeof(*s));
303
304 if (s != NULL)
305 s->name = strdup(name);
306
307 return s;
308}
309
310static void script_desc__delete(struct script_desc *s)
311{
312 free(s->name);
313 free(s);
314}
315
316static void script_desc__add(struct script_desc *s)
317{
318 list_add_tail(&s->node, &script_descs);
319}
320
321static struct script_desc *script_desc__find(const char *name)
322{
323 struct script_desc *s;
324
325 list_for_each_entry(s, &script_descs, node)
326 if (strcasecmp(s->name, name) == 0)
327 return s;
328 return NULL;
329}
330
331static struct script_desc *script_desc__findnew(const char *name)
332{
333 struct script_desc *s = script_desc__find(name);
334
335 if (s)
336 return s;
337
338 s = script_desc__new(name);
339 if (!s)
340 goto out_delete_desc;
341
342 script_desc__add(s);
343
344 return s;
345
346out_delete_desc:
347 script_desc__delete(s);
348
349 return NULL;
350}
351
352static char *ends_with(char *str, const char *suffix)
353{
354 size_t suffix_len = strlen(suffix);
355 char *p = str;
356
357 if (strlen(str) > suffix_len) {
358 p = str + strlen(str) - suffix_len;
359 if (!strncmp(p, suffix, suffix_len))
360 return p;
361 }
362
363 return NULL;
364}
365
366static char *ltrim(char *str)
367{
368 int len = strlen(str);
369
370 while (len && isspace(*str)) {
371 len--;
372 str++;
373 }
374
375 return str;
376}
377
378static int read_script_info(struct script_desc *desc, const char *filename)
379{
380 char line[BUFSIZ], *p;
381 FILE *fp;
382
383 fp = fopen(filename, "r");
384 if (!fp)
385 return -1;
386
387 while (fgets(line, sizeof(line), fp)) {
388 p = ltrim(line);
389 if (strlen(p) == 0)
390 continue;
391 if (*p != '#')
392 continue;
393 p++;
394 if (strlen(p) && *p == '!')
395 continue;
396
397 p = ltrim(p);
398 if (strlen(p) && p[strlen(p) - 1] == '\n')
399 p[strlen(p) - 1] = '\0';
400
401 if (!strncmp(p, "description:", strlen("description:"))) {
402 p += strlen("description:");
403 desc->half_liner = strdup(ltrim(p));
404 continue;
405 }
406
407 if (!strncmp(p, "args:", strlen("args:"))) {
408 p += strlen("args:");
409 desc->args = strdup(ltrim(p));
410 continue;
411 }
412 }
413
414 fclose(fp);
415
416 return 0;
417}
418
419static int list_available_scripts(const struct option *opt __used,
420 const char *s __used, int unset __used)
421{
422 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
423 char scripts_path[MAXPATHLEN];
424 DIR *scripts_dir, *lang_dir;
425 char script_path[MAXPATHLEN];
426 char lang_path[MAXPATHLEN];
427 struct script_desc *desc;
428 char first_half[BUFSIZ];
429 char *script_root;
430 char *str;
431
432 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
433
434 scripts_dir = opendir(scripts_path);
435 if (!scripts_dir)
436 return -1;
437
438 for_each_lang(scripts_dir, lang_dirent, lang_next) {
439 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
440 lang_dirent.d_name);
441 lang_dir = opendir(lang_path);
442 if (!lang_dir)
443 continue;
444
445 for_each_script(lang_dir, script_dirent, script_next) {
446 script_root = strdup(script_dirent.d_name);
447 str = ends_with(script_root, REPORT_SUFFIX);
448 if (str) {
449 *str = '\0';
450 desc = script_desc__findnew(script_root);
451 snprintf(script_path, MAXPATHLEN, "%s/%s",
452 lang_path, script_dirent.d_name);
453 read_script_info(desc, script_path);
454 }
455 free(script_root);
456 }
457 }
458
459 fprintf(stdout, "List of available trace scripts:\n");
460 list_for_each_entry(desc, &script_descs, node) {
461 sprintf(first_half, "%s %s", desc->name,
462 desc->args ? desc->args : "");
463 fprintf(stdout, " %-36s %s\n", first_half,
464 desc->half_liner ? desc->half_liner : "");
465 }
466
467 exit(0);
468}
469
3875294f
TZ
470static char *get_script_path(const char *script_root, const char *suffix)
471{
472 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
473 char scripts_path[MAXPATHLEN];
474 char script_path[MAXPATHLEN];
475 DIR *scripts_dir, *lang_dir;
476 char lang_path[MAXPATHLEN];
477 char *str, *__script_root;
478 char *path = NULL;
479
480 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
481
482 scripts_dir = opendir(scripts_path);
483 if (!scripts_dir)
484 return NULL;
485
486 for_each_lang(scripts_dir, lang_dirent, lang_next) {
487 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
488 lang_dirent.d_name);
489 lang_dir = opendir(lang_path);
490 if (!lang_dir)
491 continue;
492
493 for_each_script(lang_dir, script_dirent, script_next) {
494 __script_root = strdup(script_dirent.d_name);
495 str = ends_with(__script_root, suffix);
496 if (str) {
497 *str = '\0';
498 if (strcmp(__script_root, script_root))
499 continue;
500 snprintf(script_path, MAXPATHLEN, "%s/%s",
501 lang_path, script_dirent.d_name);
502 path = strdup(script_path);
503 free(__script_root);
504 break;
505 }
506 free(__script_root);
507 }
508 }
509
510 return path;
511}
512
0422a4fc 513static const char * const trace_usage[] = {
5f9c39dc
FW
514 "perf trace [<options>] <command>",
515 NULL
516};
517
518static const struct option options[] = {
519 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
520 "dump raw trace in ASCII"),
c0555642 521 OPT_INCR('v', "verbose", &verbose,
5f9c39dc 522 "be more verbose (show symbol address, etc)"),
4b9c0c59 523 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 524 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
525 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
526 list_available_scripts),
956ffd02
TZ
527 OPT_CALLBACK('s', "script", NULL, "name",
528 "script file name (lang:script name, script name, or *)",
529 parse_scriptname),
530 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
531 "generate perf-trace.xx script in specified language"),
408f0d18
HM
532 OPT_STRING('i', "input", &input_name, "file",
533 "input file name"),
956ffd02 534
1909629f 535 OPT_END()
5f9c39dc
FW
536};
537
538int cmd_trace(int argc, const char **argv, const char *prefix __used)
539{
d8f66248 540 struct perf_session *session;
3875294f
TZ
541 const char *suffix = NULL;
542 const char **__argv;
543 char *script_path;
544 int i, err;
545
546 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
547 if (argc < 3) {
548 fprintf(stderr,
549 "Please specify a record script\n");
550 return -1;
551 }
552 suffix = RECORD_SUFFIX;
553 }
554
555 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
556 if (argc < 3) {
557 fprintf(stderr,
558 "Please specify a report script\n");
559 return -1;
560 }
561 suffix = REPORT_SUFFIX;
562 }
563
564 if (suffix) {
565 script_path = get_script_path(argv[2], suffix);
566 if (!script_path) {
567 fprintf(stderr, "script not found\n");
568 return -1;
569 }
570
571 __argv = malloc((argc + 1) * sizeof(const char *));
572 __argv[0] = "/bin/sh";
573 __argv[1] = script_path;
574 for (i = 3; i < argc; i++)
575 __argv[i - 1] = argv[i];
576 __argv[argc - 1] = NULL;
577
578 execvp("/bin/sh", (char **)__argv);
579 exit(-1);
580 }
956ffd02 581
956ffd02
TZ
582 setup_scripting();
583
0422a4fc 584 argc = parse_options(argc, argv, options, trace_usage,
586bc5cc 585 PARSE_OPT_STOP_AT_NON_OPTION);
5f9c39dc 586
655000e7
ACM
587 if (symbol__init() < 0)
588 return -1;
cf4fee50
TZ
589 if (!script_name)
590 setup_pager();
5f9c39dc 591
75be6cf4 592 session = perf_session__new(input_name, O_RDONLY, 0);
d8f66248
ACM
593 if (session == NULL)
594 return -ENOMEM;
595
c239da3b
TZ
596 if (strcmp(input_name, "-") &&
597 !perf_session__has_traces(session, "record -R"))
d549c769
ACM
598 return -EINVAL;
599
956ffd02
TZ
600 if (generate_script_lang) {
601 struct stat perf_stat;
602
603 int input = open(input_name, O_RDONLY);
604 if (input < 0) {
605 perror("failed to open file");
606 exit(-1);
607 }
608
609 err = fstat(input, &perf_stat);
610 if (err < 0) {
611 perror("failed to stat file");
612 exit(-1);
613 }
614
615 if (!perf_stat.st_size) {
616 fprintf(stderr, "zero-sized file, nothing to do!\n");
617 exit(0);
618 }
619
620 scripting_ops = script_spec__lookup(generate_script_lang);
621 if (!scripting_ops) {
622 fprintf(stderr, "invalid language specifier");
623 return -1;
624 }
625
956ffd02
TZ
626 err = scripting_ops->generate_script("perf-trace");
627 goto out;
628 }
629
630 if (script_name) {
586bc5cc 631 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02
TZ
632 if (err)
633 goto out;
634 }
635
d8f66248 636 err = __cmd_trace(session);
956ffd02 637
d8f66248 638 perf_session__delete(session);
956ffd02
TZ
639 cleanup_scripting();
640out:
641 return err;
5f9c39dc 642}