2 * trace-event-perl. Feed perf script events to an embedded Perl interpreter.
4 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/bitmap.h>
29 #include <linux/time64.h>
32 /* perl needs the following define, right after including stdbool.h */
37 #include "../../perf.h"
38 #include "../callchain.h"
39 #include "../machine.h"
40 #include "../thread.h"
42 #include "../trace-event.h"
46 void boot_Perf__Trace__Context(pTHX_ CV
*cv
);
47 void boot_DynaLoader(pTHX_ CV
*cv
);
48 typedef PerlInterpreter
* INTERP
;
54 const char *file
= __FILE__
;
57 newXS("Perf::Trace::Context::bootstrap", boot_Perf__Trace__Context
,
59 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader
, file
);
64 #define TRACE_EVENT_TYPE_MAX \
65 ((1 << (sizeof(unsigned short) * 8)) - 1)
67 static DECLARE_BITMAP(events_defined
, TRACE_EVENT_TYPE_MAX
);
69 extern struct scripting_context
*scripting_context
;
71 static char *cur_field_name
;
72 static int zero_flag_atom
;
74 static void define_symbolic_value(const char *ev_name
,
75 const char *field_name
,
76 const char *field_value
,
77 const char *field_str
)
79 unsigned long long value
;
82 value
= eval_flag(field_value
);
88 XPUSHs(sv_2mortal(newSVpv(ev_name
, 0)));
89 XPUSHs(sv_2mortal(newSVpv(field_name
, 0)));
90 XPUSHs(sv_2mortal(newSVuv(value
)));
91 XPUSHs(sv_2mortal(newSVpv(field_str
, 0)));
94 if (get_cv("main::define_symbolic_value", 0))
95 call_pv("main::define_symbolic_value", G_SCALAR
);
102 static void define_symbolic_values(struct tep_print_flag_sym
*field
,
104 const char *field_name
)
106 define_symbolic_value(ev_name
, field_name
, field
->value
, field
->str
);
108 define_symbolic_values(field
->next
, ev_name
, field_name
);
111 static void define_symbolic_field(const char *ev_name
,
112 const char *field_name
)
120 XPUSHs(sv_2mortal(newSVpv(ev_name
, 0)));
121 XPUSHs(sv_2mortal(newSVpv(field_name
, 0)));
124 if (get_cv("main::define_symbolic_field", 0))
125 call_pv("main::define_symbolic_field", G_SCALAR
);
132 static void define_flag_value(const char *ev_name
,
133 const char *field_name
,
134 const char *field_value
,
135 const char *field_str
)
137 unsigned long long value
;
140 value
= eval_flag(field_value
);
146 XPUSHs(sv_2mortal(newSVpv(ev_name
, 0)));
147 XPUSHs(sv_2mortal(newSVpv(field_name
, 0)));
148 XPUSHs(sv_2mortal(newSVuv(value
)));
149 XPUSHs(sv_2mortal(newSVpv(field_str
, 0)));
152 if (get_cv("main::define_flag_value", 0))
153 call_pv("main::define_flag_value", G_SCALAR
);
160 static void define_flag_values(struct tep_print_flag_sym
*field
,
162 const char *field_name
)
164 define_flag_value(ev_name
, field_name
, field
->value
, field
->str
);
166 define_flag_values(field
->next
, ev_name
, field_name
);
169 static void define_flag_field(const char *ev_name
,
170 const char *field_name
,
179 XPUSHs(sv_2mortal(newSVpv(ev_name
, 0)));
180 XPUSHs(sv_2mortal(newSVpv(field_name
, 0)));
181 XPUSHs(sv_2mortal(newSVpv(delim
, 0)));
184 if (get_cv("main::define_flag_field", 0))
185 call_pv("main::define_flag_field", G_SCALAR
);
192 static void define_event_symbols(struct tep_event_format
*event
,
194 struct tep_print_arg
*args
)
199 switch (args
->type
) {
203 define_flag_value(ev_name
, cur_field_name
, "0",
207 case TEP_PRINT_FIELD
:
208 free(cur_field_name
);
209 cur_field_name
= strdup(args
->field
.name
);
211 case TEP_PRINT_FLAGS
:
212 define_event_symbols(event
, ev_name
, args
->flags
.field
);
213 define_flag_field(ev_name
, cur_field_name
, args
->flags
.delim
);
214 define_flag_values(args
->flags
.flags
, ev_name
, cur_field_name
);
216 case TEP_PRINT_SYMBOL
:
217 define_event_symbols(event
, ev_name
, args
->symbol
.field
);
218 define_symbolic_field(ev_name
, cur_field_name
);
219 define_symbolic_values(args
->symbol
.symbols
, ev_name
,
223 case TEP_PRINT_HEX_STR
:
224 define_event_symbols(event
, ev_name
, args
->hex
.field
);
225 define_event_symbols(event
, ev_name
, args
->hex
.size
);
227 case TEP_PRINT_INT_ARRAY
:
228 define_event_symbols(event
, ev_name
, args
->int_array
.field
);
229 define_event_symbols(event
, ev_name
, args
->int_array
.count
);
230 define_event_symbols(event
, ev_name
, args
->int_array
.el_size
);
232 case TEP_PRINT_BSTRING
:
233 case TEP_PRINT_DYNAMIC_ARRAY
:
234 case TEP_PRINT_DYNAMIC_ARRAY_LEN
:
235 case TEP_PRINT_STRING
:
236 case TEP_PRINT_BITMASK
:
239 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
242 if (strcmp(args
->op
.op
, ":") == 0)
244 define_event_symbols(event
, ev_name
, args
->op
.left
);
245 define_event_symbols(event
, ev_name
, args
->op
.right
);
249 pr_err("Unsupported print arg type\n");
250 /* we should warn... */
255 define_event_symbols(event
, ev_name
, args
->next
);
258 static SV
*perl_process_callchain(struct perf_sample
*sample
,
259 struct perf_evsel
*evsel
,
260 struct addr_location
*al
)
268 if (!symbol_conf
.use_callchain
|| !sample
->callchain
)
271 if (thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
272 sample
, NULL
, NULL
, scripting_max_stack
) != 0) {
273 pr_err("Failed to resolve callchain. Skipping\n");
276 callchain_cursor_commit(&callchain_cursor
);
281 struct callchain_cursor_node
*node
;
282 node
= callchain_cursor_current(&callchain_cursor
);
290 if (!hv_stores(elem
, "ip", newSVuv(node
->ip
))) {
301 if (!hv_stores(sym
, "start", newSVuv(node
->sym
->start
)) ||
302 !hv_stores(sym
, "end", newSVuv(node
->sym
->end
)) ||
303 !hv_stores(sym
, "binding", newSVuv(node
->sym
->binding
)) ||
304 !hv_stores(sym
, "name", newSVpvn(node
->sym
->name
,
305 node
->sym
->namelen
)) ||
306 !hv_stores(elem
, "sym", newRV_noinc((SV
*)sym
))) {
314 struct map
*map
= node
->map
;
315 const char *dsoname
= "[unknown]";
316 if (map
&& map
->dso
) {
317 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
318 dsoname
= map
->dso
->long_name
;
320 dsoname
= map
->dso
->name
;
322 if (!hv_stores(elem
, "dso", newSVpv(dsoname
,0))) {
328 callchain_cursor_advance(&callchain_cursor
);
329 av_push(list
, newRV_noinc((SV
*)elem
));
333 return newRV_noinc((SV
*)list
);
336 static void perl_process_tracepoint(struct perf_sample
*sample
,
337 struct perf_evsel
*evsel
,
338 struct addr_location
*al
)
340 struct thread
*thread
= al
->thread
;
341 struct tep_event_format
*event
= evsel
->tp_format
;
342 struct tep_format_field
*field
;
343 static char handler
[256];
344 unsigned long long val
;
347 int cpu
= sample
->cpu
;
348 void *data
= sample
->raw_data
;
349 unsigned long long nsecs
= sample
->time
;
350 const char *comm
= thread__comm_str(thread
);
354 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
358 pr_debug("ug! no event found for type %" PRIu64
, (u64
)evsel
->attr
.config
);
362 pid
= raw_field_value(event
, "common_pid", data
);
364 sprintf(handler
, "%s::%s", event
->system
, event
->name
);
366 if (!test_and_set_bit(event
->id
, events_defined
))
367 define_event_symbols(event
, handler
, event
->print_fmt
.args
);
369 s
= nsecs
/ NSEC_PER_SEC
;
370 ns
= nsecs
- s
* NSEC_PER_SEC
;
372 scripting_context
->event_data
= data
;
373 scripting_context
->pevent
= evsel
->tp_format
->pevent
;
379 XPUSHs(sv_2mortal(newSVpv(handler
, 0)));
380 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context
))));
381 XPUSHs(sv_2mortal(newSVuv(cpu
)));
382 XPUSHs(sv_2mortal(newSVuv(s
)));
383 XPUSHs(sv_2mortal(newSVuv(ns
)));
384 XPUSHs(sv_2mortal(newSViv(pid
)));
385 XPUSHs(sv_2mortal(newSVpv(comm
, 0)));
386 XPUSHs(sv_2mortal(perl_process_callchain(sample
, evsel
, al
)));
388 /* common fields other than pid can be accessed via xsub fns */
390 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
391 if (field
->flags
& TEP_FIELD_IS_STRING
) {
393 if (field
->flags
& TEP_FIELD_IS_DYNAMIC
) {
394 offset
= *(int *)(data
+ field
->offset
);
397 offset
= field
->offset
;
398 XPUSHs(sv_2mortal(newSVpv((char *)data
+ offset
, 0)));
399 } else { /* FIELD_IS_NUMERIC */
400 val
= read_size(event
, data
+ field
->offset
,
402 if (field
->flags
& TEP_FIELD_IS_SIGNED
) {
403 XPUSHs(sv_2mortal(newSViv(val
)));
405 XPUSHs(sv_2mortal(newSVuv(val
)));
412 if (get_cv(handler
, 0))
413 call_pv(handler
, G_SCALAR
);
414 else if (get_cv("main::trace_unhandled", 0)) {
415 XPUSHs(sv_2mortal(newSVpv(handler
, 0)));
416 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context
))));
417 XPUSHs(sv_2mortal(newSVuv(cpu
)));
418 XPUSHs(sv_2mortal(newSVuv(nsecs
)));
419 XPUSHs(sv_2mortal(newSViv(pid
)));
420 XPUSHs(sv_2mortal(newSVpv(comm
, 0)));
421 XPUSHs(sv_2mortal(perl_process_callchain(sample
, evsel
, al
)));
422 call_pv("main::trace_unhandled", G_SCALAR
);
430 static void perl_process_event_generic(union perf_event
*event
,
431 struct perf_sample
*sample
,
432 struct perf_evsel
*evsel
)
436 if (!get_cv("process_event", 0))
442 XPUSHs(sv_2mortal(newSVpvn((const char *)event
, event
->header
.size
)));
443 XPUSHs(sv_2mortal(newSVpvn((const char *)&evsel
->attr
, sizeof(evsel
->attr
))));
444 XPUSHs(sv_2mortal(newSVpvn((const char *)sample
, sizeof(*sample
))));
445 XPUSHs(sv_2mortal(newSVpvn((const char *)sample
->raw_data
, sample
->raw_size
)));
447 call_pv("process_event", G_SCALAR
);
454 static void perl_process_event(union perf_event
*event
,
455 struct perf_sample
*sample
,
456 struct perf_evsel
*evsel
,
457 struct addr_location
*al
)
459 perl_process_tracepoint(sample
, evsel
, al
);
460 perl_process_event_generic(event
, sample
, evsel
);
463 static void run_start_sub(void)
465 dSP
; /* access to Perl stack */
468 if (get_cv("main::trace_begin", 0))
469 call_pv("main::trace_begin", G_DISCARD
| G_NOARGS
);
475 static int perl_start_script(const char *script
, int argc
, const char **argv
)
477 const char **command_line
;
480 command_line
= malloc((argc
+ 2) * sizeof(const char *));
481 command_line
[0] = "";
482 command_line
[1] = script
;
483 for (i
= 2; i
< argc
+ 2; i
++)
484 command_line
[i
] = argv
[i
- 2];
486 my_perl
= perl_alloc();
487 perl_construct(my_perl
);
489 if (perl_parse(my_perl
, xs_init
, argc
+ 2, (char **)command_line
,
495 if (perl_run(my_perl
)) {
516 static int perl_flush_script(void)
524 static int perl_stop_script(void)
526 dSP
; /* access to Perl stack */
529 if (get_cv("main::trace_end", 0))
530 call_pv("main::trace_end", G_DISCARD
| G_NOARGS
);
532 perl_destruct(my_perl
);
538 static int perl_generate_script(struct tep_handle
*pevent
, const char *outfile
)
540 struct tep_event_format
*event
= NULL
;
541 struct tep_format_field
*f
;
542 char fname
[PATH_MAX
];
543 int not_first
, count
;
546 sprintf(fname
, "%s.pl", outfile
);
547 ofp
= fopen(fname
, "w");
549 fprintf(stderr
, "couldn't open %s\n", fname
);
553 fprintf(ofp
, "# perf script event handlers, "
554 "generated by perf script -g perl\n");
556 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
557 " License version 2\n\n");
559 fprintf(ofp
, "# The common_* event handler fields are the most useful "
560 "fields common to\n");
562 fprintf(ofp
, "# all events. They don't necessarily correspond to "
563 "the 'common_*' fields\n");
565 fprintf(ofp
, "# in the format files. Those fields not available as "
566 "handler params can\n");
568 fprintf(ofp
, "# be retrieved using Perl functions of the form "
569 "common_*($context).\n");
571 fprintf(ofp
, "# See Context.pm for the list of available "
574 fprintf(ofp
, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
575 "Perf-Trace-Util/lib\";\n");
577 fprintf(ofp
, "use lib \"./Perf-Trace-Util/lib\";\n");
578 fprintf(ofp
, "use Perf::Trace::Core;\n");
579 fprintf(ofp
, "use Perf::Trace::Context;\n");
580 fprintf(ofp
, "use Perf::Trace::Util;\n\n");
582 fprintf(ofp
, "sub trace_begin\n{\n\t# optional\n}\n\n");
583 fprintf(ofp
, "sub trace_end\n{\n\t# optional\n}\n");
587 sub print_backtrace\n\
589 my $callchain = shift;\n\
590 for my $node (@$callchain)\n\
592 if(exists $node->{sym})\n\
594 printf( \"\\t[\\%%x] \\%%s\\n\", $node->{ip}, $node->{sym}{name});\n\
598 printf( \"\\t[\\%%x]\\n\", $node{ip});\n\
605 while ((event
= trace_find_next_event(pevent
, event
))) {
606 fprintf(ofp
, "sub %s::%s\n{\n", event
->system
, event
->name
);
607 fprintf(ofp
, "\tmy (");
609 fprintf(ofp
, "$event_name, ");
610 fprintf(ofp
, "$context, ");
611 fprintf(ofp
, "$common_cpu, ");
612 fprintf(ofp
, "$common_secs, ");
613 fprintf(ofp
, "$common_nsecs,\n");
614 fprintf(ofp
, "\t $common_pid, ");
615 fprintf(ofp
, "$common_comm, ");
616 fprintf(ofp
, "$common_callchain,\n\t ");
621 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
624 if (++count
% 5 == 0)
625 fprintf(ofp
, "\n\t ");
627 fprintf(ofp
, "$%s", f
->name
);
629 fprintf(ofp
, ") = @_;\n\n");
631 fprintf(ofp
, "\tprint_header($event_name, $common_cpu, "
632 "$common_secs, $common_nsecs,\n\t "
633 "$common_pid, $common_comm, $common_callchain);\n\n");
635 fprintf(ofp
, "\tprintf(\"");
640 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
643 if (count
&& count
% 4 == 0) {
644 fprintf(ofp
, "\".\n\t \"");
648 fprintf(ofp
, "%s=", f
->name
);
649 if (f
->flags
& TEP_FIELD_IS_STRING
||
650 f
->flags
& TEP_FIELD_IS_FLAG
||
651 f
->flags
& TEP_FIELD_IS_SYMBOLIC
)
653 else if (f
->flags
& TEP_FIELD_IS_SIGNED
)
659 fprintf(ofp
, "\\n\",\n\t ");
664 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
668 if (++count
% 5 == 0)
669 fprintf(ofp
, "\n\t ");
671 if (f
->flags
& TEP_FIELD_IS_FLAG
) {
672 if ((count
- 1) % 5 != 0) {
673 fprintf(ofp
, "\n\t ");
676 fprintf(ofp
, "flag_str(\"");
677 fprintf(ofp
, "%s::%s\", ", event
->system
,
679 fprintf(ofp
, "\"%s\", $%s)", f
->name
,
681 } else if (f
->flags
& TEP_FIELD_IS_SYMBOLIC
) {
682 if ((count
- 1) % 5 != 0) {
683 fprintf(ofp
, "\n\t ");
686 fprintf(ofp
, "symbol_str(\"");
687 fprintf(ofp
, "%s::%s\", ", event
->system
,
689 fprintf(ofp
, "\"%s\", $%s)", f
->name
,
692 fprintf(ofp
, "$%s", f
->name
);
695 fprintf(ofp
, ");\n\n");
697 fprintf(ofp
, "\tprint_backtrace($common_callchain);\n");
699 fprintf(ofp
, "}\n\n");
702 fprintf(ofp
, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
703 "$common_cpu, $common_secs, $common_nsecs,\n\t "
704 "$common_pid, $common_comm, $common_callchain) = @_;\n\n");
706 fprintf(ofp
, "\tprint_header($event_name, $common_cpu, "
707 "$common_secs, $common_nsecs,\n\t $common_pid, "
708 "$common_comm, $common_callchain);\n");
709 fprintf(ofp
, "\tprint_backtrace($common_callchain);\n");
710 fprintf(ofp
, "}\n\n");
712 fprintf(ofp
, "sub print_header\n{\n"
713 "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
714 "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
715 "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
718 "\n# Packed byte string args of process_event():\n"
720 "# $event:\tunion perf_event\tutil/event.h\n"
721 "# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
722 "# $sample:\tstruct perf_sample\tutil/event.h\n"
723 "# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
725 "sub process_event\n"
727 "\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
729 "\tmy @event\t= unpack(\"LSS\", $event);\n"
730 "\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
731 "\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
732 "\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
734 "\tuse Data::Dumper;\n"
735 "\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
740 fprintf(stderr
, "generated Perl script: %s\n", fname
);
745 struct scripting_ops perl_scripting_ops
= {
747 .start_script
= perl_start_script
,
748 .flush_script
= perl_flush_script
,
749 .stop_script
= perl_stop_script
,
750 .process_event
= perl_process_event
,
751 .generate_script
= perl_generate_script
,