32 #include "../../perf.h"
33 #include "../thread.h"
35 #include "../trace-event.h"
46 const char *
file = __FILE__;
56 #define FTRACE_MAX_EVENT \
57 ((1 << (sizeof(unsigned short) * 8)) - 1)
63 static char *cur_field_name;
64 static int zero_flag_atom;
67 const char *field_name,
68 const char *field_value,
69 const char *field_str)
71 unsigned long long value;
80 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
81 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
82 XPUSHs(sv_2mortal(newSVuv(value)));
83 XPUSHs(sv_2mortal(newSVpv(field_str, 0)));
86 if (get_cv(
"main::define_symbolic_value", 0))
87 call_pv(
"main::define_symbolic_value", G_SCALAR);
96 const char *field_name)
100 define_symbolic_values(field->
next, ev_name, field_name);
104 const char *field_name)
112 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
113 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
116 if (get_cv(
"main::define_symbolic_field", 0))
117 call_pv(
"main::define_symbolic_field", G_SCALAR);
125 const char *field_name,
126 const char *field_value,
127 const char *field_str)
129 unsigned long long value;
138 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
139 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
140 XPUSHs(sv_2mortal(newSVuv(value)));
141 XPUSHs(sv_2mortal(newSVpv(field_str, 0)));
144 if (get_cv(
"main::define_flag_value", 0))
145 call_pv(
"main::define_flag_value", G_SCALAR);
154 const char *field_name)
158 define_flag_values(field->
next, ev_name, field_name);
162 const char *field_name,
171 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
172 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
173 XPUSHs(sv_2mortal(newSVpv(delim, 0)));
176 if (get_cv(
"main::define_flag_field", 0))
177 call_pv(
"main::define_flag_field", G_SCALAR);
188 switch (args->
type) {
198 free(cur_field_name);
199 cur_field_name = strdup(args->
field.name);
202 define_event_symbols(event, ev_name, args->
flags.field);
204 define_flag_values(args->
flags.flags, ev_name, cur_field_name);
207 define_event_symbols(event, ev_name, args->
symbol.field);
209 define_symbolic_values(args->
symbol.symbols, ev_name,
213 define_event_symbols(event, ev_name, args->
hex.field);
214 define_event_symbols(event, ev_name, args->
hex.size);
221 define_event_symbols(event, ev_name, args->
typecast.item);
226 define_event_symbols(event, ev_name, args->
op.left);
227 define_event_symbols(event, ev_name, args->
op.right);
231 pr_err(
"Unsupported print arg type\n");
237 define_event_symbols(event, ev_name, args->
next);
242 static char ev_name[256];
255 define_event_symbols(event, ev_name, event->
print_fmt.args);
268 unsigned long long val;
283 event = find_cache_event(evsel);
285 die(
"ug! no event found for type %" PRIu64, evsel->
attr.config);
300 XPUSHs(sv_2mortal(newSVpv(handler, 0)));
301 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
302 XPUSHs(sv_2mortal(newSVuv(cpu)));
303 XPUSHs(sv_2mortal(newSVuv(s)));
304 XPUSHs(sv_2mortal(newSVuv(ns)));
305 XPUSHs(sv_2mortal(newSViv(pid)));
306 XPUSHs(sv_2mortal(newSVpv(comm, 0)));
310 for (field = event->
format.fields; field; field = field->
next) {
314 offset = *(
int *)(data + field->
offset);
318 XPUSHs(sv_2mortal(newSVpv((
char *)data + offset, 0)));
323 XPUSHs(sv_2mortal(newSViv(val)));
325 XPUSHs(sv_2mortal(newSVuv(val)));
332 if (get_cv(handler, 0))
333 call_pv(handler, G_SCALAR);
334 else if (get_cv(
"main::trace_unhandled", 0)) {
335 XPUSHs(sv_2mortal(newSVpv(handler, 0)));
336 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
337 XPUSHs(sv_2mortal(newSVuv(cpu)));
338 XPUSHs(sv_2mortal(newSVuv(nsecs)));
339 XPUSHs(sv_2mortal(newSViv(pid)));
340 XPUSHs(sv_2mortal(newSVpv(comm, 0)));
341 call_pv(
"main::trace_unhandled", G_SCALAR);
349 static void perl_process_event_generic(
union perf_event *event,
357 if (!get_cv(
"process_event", 0))
363 XPUSHs(sv_2mortal(newSVpvn((
const char *)event, event->
header.size)));
364 XPUSHs(sv_2mortal(newSVpvn((
const char *)&evsel->
attr,
sizeof(evsel->
attr))));
365 XPUSHs(sv_2mortal(newSVpvn((
const char *)sample,
sizeof(*sample))));
366 XPUSHs(sv_2mortal(newSVpvn((
const char *)sample->
raw_data, sample->
raw_size)));
368 call_pv(
"process_event", G_SCALAR);
375 static void perl_process_event(
union perf_event *event,
381 perl_process_tracepoint(event, sample, evsel, machine, al);
382 perl_process_event_generic(event, sample, evsel, machine, al);
385 static void run_start_sub(
void)
390 if (get_cv(
"main::trace_begin", 0))
391 call_pv(
"main::trace_begin", G_DISCARD | G_NOARGS);
397 static int perl_start_script(
const char *
script,
int argc,
const char **argv)
402 command_line =
malloc((argc + 2) *
sizeof(
const char *));
403 command_line[0] =
"";
404 command_line[1] = script;
405 for (i = 2; i < argc + 2; i++)
406 command_line[i] = argv[i - 2];
441 static int perl_stop_script(
void)
446 if (get_cv(
"main::trace_end", 0))
447 call_pv(
"main::trace_end", G_DISCARD | G_NOARGS);
455 static int perl_generate_script(
struct pevent *
pevent,
const char *outfile)
460 int not_first,
count;
463 sprintf(fname,
"%s.pl", outfile);
464 ofp = fopen(fname,
"w");
466 fprintf(stderr,
"couldn't open %s\n", fname);
470 fprintf(ofp,
"# perf script event handlers, "
471 "generated by perf script -g perl\n");
473 fprintf(ofp,
"# Licensed under the terms of the GNU GPL"
474 " License version 2\n\n");
476 fprintf(ofp,
"# The common_* event handler fields are the most useful "
477 "fields common to\n");
479 fprintf(ofp,
"# all events. They don't necessarily correspond to "
480 "the 'common_*' fields\n");
482 fprintf(ofp,
"# in the format files. Those fields not available as "
483 "handler params can\n");
485 fprintf(ofp,
"# be retrieved using Perl functions of the form "
486 "common_*($context).\n");
488 fprintf(ofp,
"# See Context.pm for the list of available "
491 fprintf(ofp,
"use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
492 "Perf-Trace-Util/lib\";\n");
494 fprintf(ofp,
"use lib \"./Perf-Trace-Util/lib\";\n");
495 fprintf(ofp,
"use Perf::Trace::Core;\n");
496 fprintf(ofp,
"use Perf::Trace::Context;\n");
497 fprintf(ofp,
"use Perf::Trace::Util;\n\n");
499 fprintf(ofp,
"sub trace_begin\n{\n\t# optional\n}\n\n");
500 fprintf(ofp,
"sub trace_end\n{\n\t# optional\n}\n\n");
503 fprintf(ofp,
"sub %s::%s\n{\n", event->system, event->name);
509 fprintf(ofp,
"$common_secs, ");
510 fprintf(ofp,
"$common_nsecs,\n");
511 fprintf(ofp,
"\t $common_pid, ");
512 fprintf(ofp,
"$common_comm,\n\t ");
517 for (f = event->format.fields; f; f = f->
next) {
520 if (++count % 5 == 0)
527 fprintf(ofp,
"\tprint_header($event_name, $common_cpu, "
528 "$common_secs, $common_nsecs,\n\t "
529 "$common_pid, $common_comm);\n\n");
536 for (f = event->format.fields; f; f = f->
next) {
539 if (count && count % 4 == 0) {
560 for (f = event->format.fields; f; f = f->
next) {
564 if (++count % 5 == 0)
568 if ((count - 1) % 5 != 0) {
573 fprintf(ofp,
"%s::%s\", ", event->system,
578 if ((count - 1) % 5 != 0) {
583 fprintf(ofp,
"%s::%s\", ", event->system,
595 fprintf(ofp,
"sub trace_unhandled\n{\n\tmy ($event_name, $context, "
596 "$common_cpu, $common_secs, $common_nsecs,\n\t "
597 "$common_pid, $common_comm) = @_;\n\n");
599 fprintf(ofp,
"\tprint_header($event_name, $common_cpu, "
600 "$common_secs, $common_nsecs,\n\t $common_pid, "
601 "$common_comm);\n}\n\n");
603 fprintf(ofp,
"sub print_header\n{\n"
604 "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
605 "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
606 "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
609 "\n# Packed byte string args of process_event():\n"
611 "# $event:\tunion perf_event\tutil/event.h\n"
612 "# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
613 "# $sample:\tstruct perf_sample\tutil/event.h\n"
614 "# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
616 "sub process_event\n"
618 "\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
620 "\tmy @event\t= unpack(\"LSS\", $event);\n"
621 "\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
622 "\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
623 "\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
625 "\tuse Data::Dumper;\n"
626 "\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
631 fprintf(stderr,
"generated Perl script: %s\n", fname);
638 .start_script = perl_start_script,
639 .stop_script = perl_stop_script,
640 .process_event = perl_process_event,
641 .generate_script = perl_generate_script,