28 #include <linux/bitmap.h> 29 #include <linux/time64.h> 37 #include "../../perf.h" 38 #include "../callchain.h" 39 #include "../machine.h" 40 #include "../thread.h" 42 #include "../trace-event.h" 54 const char *file = __FILE__;
64 #define TRACE_EVENT_TYPE_MAX \ 65 ((1 << (sizeof(unsigned short) * 8)) - 1) 75 const char *field_name,
76 const char *field_value,
77 const char *field_str)
79 unsigned long long 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);
104 const char *field_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);
133 const char *field_name,
134 const char *field_value,
135 const char *field_str)
137 unsigned long long 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);
162 const char *field_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);
194 struct print_arg *
args)
199 switch (args->type) {
227 case PRINT_INT_ARRAY:
233 case PRINT_DYNAMIC_ARRAY:
234 case PRINT_DYNAMIC_ARRAY_LEN:
242 if (strcmp(args->op.op,
":") == 0)
249 pr_err(
"Unsupported print arg type\n");
273 pr_err(
"Failed to resolve callchain. Skipping\n");
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,
306 !hv_stores(elem,
"sym", newRV_noinc((SV*)sym))) {
315 const char *dsoname =
"[unknown]";
316 if (map && map->
dso) {
322 if (!hv_stores(elem,
"dso", newSVpv(dsoname,0))) {
329 av_push(list, newRV_noinc((SV*)elem));
333 return newRV_noinc((SV*)list);
341 struct event_format *
event = evsel->
tp_format;
342 struct format_field *
field;
343 static char handler[256];
344 unsigned long long val;
347 int cpu = sample->
cpu;
354 if (evsel->
attr.type != PERF_TYPE_TRACEPOINT)
358 pr_debug(
"ug! no event found for type %" PRIu64, (u64)evsel->
attr.config);
364 sprintf(handler,
"%s::%s",
event->system,
event->name);
366 if (!test_and_set_bit(
event->id, events_defined))
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)));
390 for (field =
event->format.fields; field; field = field->next) {
391 if (field->flags & FIELD_IS_STRING) {
393 if (field->flags & FIELD_IS_DYNAMIC) {
394 offset = *(
int *)(data + field->offset);
397 offset = field->offset;
398 XPUSHs(sv_2mortal(newSVpv((
char *)data + offset, 0)));
402 if (field->flags & 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)));
422 call_pv(
"main::trace_unhandled", G_SCALAR);
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);
468 if (get_cv(
"main::trace_begin", 0))
469 call_pv(
"main::trace_begin", G_DISCARD | G_NOARGS);
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];
529 if (get_cv(
"main::trace_end", 0))
530 call_pv(
"main::trace_end", G_DISCARD | G_NOARGS);
540 struct event_format *
event = NULL;
541 struct format_field *
f;
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\ 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 & FIELD_IS_STRING ||
650 f->flags & FIELD_IS_FLAG ||
651 f->flags & FIELD_IS_SYMBOLIC)
653 else if (f->flags & 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 & 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 & 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);
#define TRACE_EVENT_TYPE_MAX
static void define_symbolic_field(const char *ev_name, const char *field_name)
static void define_event_symbols(struct event_format *event, const char *ev_name, struct print_arg *args)
struct event_format * trace_find_next_event(struct pevent *pevent, struct event_format *event)
struct scripting_ops perl_scripting_ops
void boot_Perf__Trace__Context(pTHX_ CV *cv)
static int perl_generate_script(struct pevent *pevent, const char *outfile)
struct ip_callchain * callchain
static void define_symbolic_values(struct print_flag_sym *field, const char *ev_name, const char *field_name)
void boot_DynaLoader(pTHX_ CV *cv)
static char * cur_field_name
static void perl_process_event_generic(union perf_event *event, struct perf_sample *sample, struct perf_evsel *evsel)
struct scripting_context * scripting_context
static void callchain_cursor_commit(struct callchain_cursor *cursor)
static void define_flag_values(struct print_flag_sym *field, const char *ev_name, const char *field_name)
unsigned long long eval_flag(const char *flag)
unsigned int scripting_max_stack
static void define_flag_field(const char *ev_name, const char *field_name, const char *delim)
static int perl_stop_script(void)
const char * thread__comm_str(const struct thread *thread)
#define pr_debug(fmt,...)
unsigned long long read_size(struct event_format *event, void *ptr, int size)
static void perl_process_event(union perf_event *event, struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al)
static void callchain_cursor_advance(struct callchain_cursor *cursor)
static int zero_flag_atom
struct event_format * tp_format
static int perl_flush_script(void)
static int perl_start_script(const char *script, int argc, const char **argv)
static SV * perl_process_callchain(struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al)
struct perf_event_header header
static void define_flag_value(const char *ev_name, const char *field_name, const char *field_value, const char *field_str)
static void run_start_sub(void)
static int sym(yyscan_t scanner, int type, int config)
static unsigned int nsecs
static void define_symbolic_value(const char *ev_name, const char *field_name, const char *field_value, const char *field_str)
static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX)
int thread__resolve_callchain(struct thread *thread, struct callchain_cursor *cursor, struct perf_evsel *evsel, struct perf_sample *sample, struct symbol **parent, struct addr_location *root_al, int max_stack)
static void perl_process_tracepoint(struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al)
struct perf_event_attr attr
unsigned long long raw_field_value(struct event_format *event, const char *name, void *data)
static struct callchain_cursor_node * callchain_cursor_current(struct callchain_cursor *cursor)