trace-event-perl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * trace-event-perl. Feed perf script events to an embedded Perl interpreter.
  3. *
  4. * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <linux/bitmap.h>
  27. #include "../util.h"
  28. #include <EXTERN.h>
  29. #include <perl.h>
  30. #include "../../perf.h"
  31. #include "../callchain.h"
  32. #include "../machine.h"
  33. #include "../thread.h"
  34. #include "../event.h"
  35. #include "../trace-event.h"
  36. #include "../evsel.h"
  37. #include "../debug.h"
  38. void boot_Perf__Trace__Context(pTHX_ CV *cv);
  39. void boot_DynaLoader(pTHX_ CV *cv);
  40. typedef PerlInterpreter * INTERP;
  41. void xs_init(pTHX);
  42. void xs_init(pTHX)
  43. {
  44. const char *file = __FILE__;
  45. dXSUB_SYS;
  46. newXS("Perf::Trace::Context::bootstrap", boot_Perf__Trace__Context,
  47. file);
  48. newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  49. }
  50. INTERP my_perl;
  51. #define TRACE_EVENT_TYPE_MAX \
  52. ((1 << (sizeof(unsigned short) * 8)) - 1)
  53. static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
  54. extern struct scripting_context *scripting_context;
  55. static char *cur_field_name;
  56. static int zero_flag_atom;
  57. static void define_symbolic_value(const char *ev_name,
  58. const char *field_name,
  59. const char *field_value,
  60. const char *field_str)
  61. {
  62. unsigned long long value;
  63. dSP;
  64. value = eval_flag(field_value);
  65. ENTER;
  66. SAVETMPS;
  67. PUSHMARK(SP);
  68. XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
  69. XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
  70. XPUSHs(sv_2mortal(newSVuv(value)));
  71. XPUSHs(sv_2mortal(newSVpv(field_str, 0)));
  72. PUTBACK;
  73. if (get_cv("main::define_symbolic_value", 0))
  74. call_pv("main::define_symbolic_value", G_SCALAR);
  75. SPAGAIN;
  76. PUTBACK;
  77. FREETMPS;
  78. LEAVE;
  79. }
  80. static void define_symbolic_values(struct print_flag_sym *field,
  81. const char *ev_name,
  82. const char *field_name)
  83. {
  84. define_symbolic_value(ev_name, field_name, field->value, field->str);
  85. if (field->next)
  86. define_symbolic_values(field->next, ev_name, field_name);
  87. }
  88. static void define_symbolic_field(const char *ev_name,
  89. const char *field_name)
  90. {
  91. dSP;
  92. ENTER;
  93. SAVETMPS;
  94. PUSHMARK(SP);
  95. XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
  96. XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
  97. PUTBACK;
  98. if (get_cv("main::define_symbolic_field", 0))
  99. call_pv("main::define_symbolic_field", G_SCALAR);
  100. SPAGAIN;
  101. PUTBACK;
  102. FREETMPS;
  103. LEAVE;
  104. }
  105. static void define_flag_value(const char *ev_name,
  106. const char *field_name,
  107. const char *field_value,
  108. const char *field_str)
  109. {
  110. unsigned long long value;
  111. dSP;
  112. value = eval_flag(field_value);
  113. ENTER;
  114. SAVETMPS;
  115. PUSHMARK(SP);
  116. XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
  117. XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
  118. XPUSHs(sv_2mortal(newSVuv(value)));
  119. XPUSHs(sv_2mortal(newSVpv(field_str, 0)));
  120. PUTBACK;
  121. if (get_cv("main::define_flag_value", 0))
  122. call_pv("main::define_flag_value", G_SCALAR);
  123. SPAGAIN;
  124. PUTBACK;
  125. FREETMPS;
  126. LEAVE;
  127. }
  128. static void define_flag_values(struct print_flag_sym *field,
  129. const char *ev_name,
  130. const char *field_name)
  131. {
  132. define_flag_value(ev_name, field_name, field->value, field->str);
  133. if (field->next)
  134. define_flag_values(field->next, ev_name, field_name);
  135. }
  136. static void define_flag_field(const char *ev_name,
  137. const char *field_name,
  138. const char *delim)
  139. {
  140. dSP;
  141. ENTER;
  142. SAVETMPS;
  143. PUSHMARK(SP);
  144. XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
  145. XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
  146. XPUSHs(sv_2mortal(newSVpv(delim, 0)));
  147. PUTBACK;
  148. if (get_cv("main::define_flag_field", 0))
  149. call_pv("main::define_flag_field", G_SCALAR);
  150. SPAGAIN;
  151. PUTBACK;
  152. FREETMPS;
  153. LEAVE;
  154. }
  155. static void define_event_symbols(struct event_format *event,
  156. const char *ev_name,
  157. struct print_arg *args)
  158. {
  159. if (args == NULL)
  160. return;
  161. switch (args->type) {
  162. case PRINT_NULL:
  163. break;
  164. case PRINT_ATOM:
  165. define_flag_value(ev_name, cur_field_name, "0",
  166. args->atom.atom);
  167. zero_flag_atom = 0;
  168. break;
  169. case PRINT_FIELD:
  170. free(cur_field_name);
  171. cur_field_name = strdup(args->field.name);
  172. break;
  173. case PRINT_FLAGS:
  174. define_event_symbols(event, ev_name, args->flags.field);
  175. define_flag_field(ev_name, cur_field_name, args->flags.delim);
  176. define_flag_values(args->flags.flags, ev_name, cur_field_name);
  177. break;
  178. case PRINT_SYMBOL:
  179. define_event_symbols(event, ev_name, args->symbol.field);
  180. define_symbolic_field(ev_name, cur_field_name);
  181. define_symbolic_values(args->symbol.symbols, ev_name,
  182. cur_field_name);
  183. break;
  184. case PRINT_HEX:
  185. define_event_symbols(event, ev_name, args->hex.field);
  186. define_event_symbols(event, ev_name, args->hex.size);
  187. break;
  188. case PRINT_INT_ARRAY:
  189. define_event_symbols(event, ev_name, args->int_array.field);
  190. define_event_symbols(event, ev_name, args->int_array.count);
  191. define_event_symbols(event, ev_name, args->int_array.el_size);
  192. break;
  193. case PRINT_BSTRING:
  194. case PRINT_DYNAMIC_ARRAY:
  195. case PRINT_DYNAMIC_ARRAY_LEN:
  196. case PRINT_STRING:
  197. case PRINT_BITMASK:
  198. break;
  199. case PRINT_TYPE:
  200. define_event_symbols(event, ev_name, args->typecast.item);
  201. break;
  202. case PRINT_OP:
  203. if (strcmp(args->op.op, ":") == 0)
  204. zero_flag_atom = 1;
  205. define_event_symbols(event, ev_name, args->op.left);
  206. define_event_symbols(event, ev_name, args->op.right);
  207. break;
  208. case PRINT_FUNC:
  209. default:
  210. pr_err("Unsupported print arg type\n");
  211. /* we should warn... */
  212. return;
  213. }
  214. if (args->next)
  215. define_event_symbols(event, ev_name, args->next);
  216. }
  217. static SV *perl_process_callchain(struct perf_sample *sample,
  218. struct perf_evsel *evsel,
  219. struct addr_location *al)
  220. {
  221. AV *list;
  222. list = newAV();
  223. if (!list)
  224. goto exit;
  225. if (!symbol_conf.use_callchain || !sample->callchain)
  226. goto exit;
  227. if (thread__resolve_callchain(al->thread, evsel,
  228. sample, NULL, NULL,
  229. PERF_MAX_STACK_DEPTH) != 0) {
  230. pr_err("Failed to resolve callchain. Skipping\n");
  231. goto exit;
  232. }
  233. callchain_cursor_commit(&callchain_cursor);
  234. while (1) {
  235. HV *elem;
  236. struct callchain_cursor_node *node;
  237. node = callchain_cursor_current(&callchain_cursor);
  238. if (!node)
  239. break;
  240. elem = newHV();
  241. if (!elem)
  242. goto exit;
  243. hv_stores(elem, "ip", newSVuv(node->ip));
  244. if (node->sym) {
  245. HV *sym = newHV();
  246. if (!sym)
  247. goto exit;
  248. hv_stores(sym, "start", newSVuv(node->sym->start));
  249. hv_stores(sym, "end", newSVuv(node->sym->end));
  250. hv_stores(sym, "binding", newSVuv(node->sym->binding));
  251. hv_stores(sym, "name", newSVpvn(node->sym->name,
  252. node->sym->namelen));
  253. hv_stores(elem, "sym", newRV_noinc((SV*)sym));
  254. }
  255. if (node->map) {
  256. struct map *map = node->map;
  257. const char *dsoname = "[unknown]";
  258. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  259. if (symbol_conf.show_kernel_path && map->dso->long_name)
  260. dsoname = map->dso->long_name;
  261. else if (map->dso->name)
  262. dsoname = map->dso->name;
  263. }
  264. hv_stores(elem, "dso", newSVpv(dsoname,0));
  265. }
  266. callchain_cursor_advance(&callchain_cursor);
  267. av_push(list, newRV_noinc((SV*)elem));
  268. }
  269. exit:
  270. return newRV_noinc((SV*)list);
  271. }
  272. static void perl_process_tracepoint(struct perf_sample *sample,
  273. struct perf_evsel *evsel,
  274. struct addr_location *al)
  275. {
  276. struct thread *thread = al->thread;
  277. struct event_format *event = evsel->tp_format;
  278. struct format_field *field;
  279. static char handler[256];
  280. unsigned long long val;
  281. unsigned long s, ns;
  282. int pid;
  283. int cpu = sample->cpu;
  284. void *data = sample->raw_data;
  285. unsigned long long nsecs = sample->time;
  286. const char *comm = thread__comm_str(thread);
  287. dSP;
  288. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  289. return;
  290. if (!event)
  291. die("ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
  292. pid = raw_field_value(event, "common_pid", data);
  293. sprintf(handler, "%s::%s", event->system, event->name);
  294. if (!test_and_set_bit(event->id, events_defined))
  295. define_event_symbols(event, handler, event->print_fmt.args);
  296. s = nsecs / NSECS_PER_SEC;
  297. ns = nsecs - s * NSECS_PER_SEC;
  298. scripting_context->event_data = data;
  299. scripting_context->pevent = evsel->tp_format->pevent;
  300. ENTER;
  301. SAVETMPS;
  302. PUSHMARK(SP);
  303. XPUSHs(sv_2mortal(newSVpv(handler, 0)));
  304. XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
  305. XPUSHs(sv_2mortal(newSVuv(cpu)));
  306. XPUSHs(sv_2mortal(newSVuv(s)));
  307. XPUSHs(sv_2mortal(newSVuv(ns)));
  308. XPUSHs(sv_2mortal(newSViv(pid)));
  309. XPUSHs(sv_2mortal(newSVpv(comm, 0)));
  310. XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
  311. /* common fields other than pid can be accessed via xsub fns */
  312. for (field = event->format.fields; field; field = field->next) {
  313. if (field->flags & FIELD_IS_STRING) {
  314. int offset;
  315. if (field->flags & FIELD_IS_DYNAMIC) {
  316. offset = *(int *)(data + field->offset);
  317. offset &= 0xffff;
  318. } else
  319. offset = field->offset;
  320. XPUSHs(sv_2mortal(newSVpv((char *)data + offset, 0)));
  321. } else { /* FIELD_IS_NUMERIC */
  322. val = read_size(event, data + field->offset,
  323. field->size);
  324. if (field->flags & FIELD_IS_SIGNED) {
  325. XPUSHs(sv_2mortal(newSViv(val)));
  326. } else {
  327. XPUSHs(sv_2mortal(newSVuv(val)));
  328. }
  329. }
  330. }
  331. PUTBACK;
  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. XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
  342. call_pv("main::trace_unhandled", G_SCALAR);
  343. }
  344. SPAGAIN;
  345. PUTBACK;
  346. FREETMPS;
  347. LEAVE;
  348. }
  349. static void perl_process_event_generic(union perf_event *event,
  350. struct perf_sample *sample,
  351. struct perf_evsel *evsel)
  352. {
  353. dSP;
  354. if (!get_cv("process_event", 0))
  355. return;
  356. ENTER;
  357. SAVETMPS;
  358. PUSHMARK(SP);
  359. XPUSHs(sv_2mortal(newSVpvn((const char *)event, event->header.size)));
  360. XPUSHs(sv_2mortal(newSVpvn((const char *)&evsel->attr, sizeof(evsel->attr))));
  361. XPUSHs(sv_2mortal(newSVpvn((const char *)sample, sizeof(*sample))));
  362. XPUSHs(sv_2mortal(newSVpvn((const char *)sample->raw_data, sample->raw_size)));
  363. PUTBACK;
  364. call_pv("process_event", G_SCALAR);
  365. SPAGAIN;
  366. PUTBACK;
  367. FREETMPS;
  368. LEAVE;
  369. }
  370. static void perl_process_event(union perf_event *event,
  371. struct perf_sample *sample,
  372. struct perf_evsel *evsel,
  373. struct addr_location *al)
  374. {
  375. perl_process_tracepoint(sample, evsel, al);
  376. perl_process_event_generic(event, sample, evsel);
  377. }
  378. static void run_start_sub(void)
  379. {
  380. dSP; /* access to Perl stack */
  381. PUSHMARK(SP);
  382. if (get_cv("main::trace_begin", 0))
  383. call_pv("main::trace_begin", G_DISCARD | G_NOARGS);
  384. }
  385. /*
  386. * Start trace script
  387. */
  388. static int perl_start_script(const char *script, int argc, const char **argv)
  389. {
  390. const char **command_line;
  391. int i, err = 0;
  392. command_line = malloc((argc + 2) * sizeof(const char *));
  393. command_line[0] = "";
  394. command_line[1] = script;
  395. for (i = 2; i < argc + 2; i++)
  396. command_line[i] = argv[i - 2];
  397. my_perl = perl_alloc();
  398. perl_construct(my_perl);
  399. if (perl_parse(my_perl, xs_init, argc + 2, (char **)command_line,
  400. (char **)NULL)) {
  401. err = -1;
  402. goto error;
  403. }
  404. if (perl_run(my_perl)) {
  405. err = -1;
  406. goto error;
  407. }
  408. if (SvTRUE(ERRSV)) {
  409. err = -1;
  410. goto error;
  411. }
  412. run_start_sub();
  413. free(command_line);
  414. return 0;
  415. error:
  416. perl_free(my_perl);
  417. free(command_line);
  418. return err;
  419. }
  420. static int perl_flush_script(void)
  421. {
  422. return 0;
  423. }
  424. /*
  425. * Stop trace script
  426. */
  427. static int perl_stop_script(void)
  428. {
  429. dSP; /* access to Perl stack */
  430. PUSHMARK(SP);
  431. if (get_cv("main::trace_end", 0))
  432. call_pv("main::trace_end", G_DISCARD | G_NOARGS);
  433. perl_destruct(my_perl);
  434. perl_free(my_perl);
  435. return 0;
  436. }
  437. static int perl_generate_script(struct pevent *pevent, const char *outfile)
  438. {
  439. struct event_format *event = NULL;
  440. struct format_field *f;
  441. char fname[PATH_MAX];
  442. int not_first, count;
  443. FILE *ofp;
  444. sprintf(fname, "%s.pl", outfile);
  445. ofp = fopen(fname, "w");
  446. if (ofp == NULL) {
  447. fprintf(stderr, "couldn't open %s\n", fname);
  448. return -1;
  449. }
  450. fprintf(ofp, "# perf script event handlers, "
  451. "generated by perf script -g perl\n");
  452. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  453. " License version 2\n\n");
  454. fprintf(ofp, "# The common_* event handler fields are the most useful "
  455. "fields common to\n");
  456. fprintf(ofp, "# all events. They don't necessarily correspond to "
  457. "the 'common_*' fields\n");
  458. fprintf(ofp, "# in the format files. Those fields not available as "
  459. "handler params can\n");
  460. fprintf(ofp, "# be retrieved using Perl functions of the form "
  461. "common_*($context).\n");
  462. fprintf(ofp, "# See Context.pm for the list of available "
  463. "functions.\n\n");
  464. fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
  465. "Perf-Trace-Util/lib\";\n");
  466. fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
  467. fprintf(ofp, "use Perf::Trace::Core;\n");
  468. fprintf(ofp, "use Perf::Trace::Context;\n");
  469. fprintf(ofp, "use Perf::Trace::Util;\n\n");
  470. fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
  471. fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n");
  472. fprintf(ofp, "\n\
  473. sub print_backtrace\n\
  474. {\n\
  475. my $callchain = shift;\n\
  476. for my $node (@$callchain)\n\
  477. {\n\
  478. if(exists $node->{sym})\n\
  479. {\n\
  480. printf( \"\\t[\\%%x] \\%%s\\n\", $node->{ip}, $node->{sym}{name});\n\
  481. }\n\
  482. else\n\
  483. {\n\
  484. printf( \"\\t[\\%%x]\\n\", $node{ip});\n\
  485. }\n\
  486. }\n\
  487. }\n\n\
  488. ");
  489. while ((event = trace_find_next_event(pevent, event))) {
  490. fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
  491. fprintf(ofp, "\tmy (");
  492. fprintf(ofp, "$event_name, ");
  493. fprintf(ofp, "$context, ");
  494. fprintf(ofp, "$common_cpu, ");
  495. fprintf(ofp, "$common_secs, ");
  496. fprintf(ofp, "$common_nsecs,\n");
  497. fprintf(ofp, "\t $common_pid, ");
  498. fprintf(ofp, "$common_comm, ");
  499. fprintf(ofp, "$common_callchain,\n\t ");
  500. not_first = 0;
  501. count = 0;
  502. for (f = event->format.fields; f; f = f->next) {
  503. if (not_first++)
  504. fprintf(ofp, ", ");
  505. if (++count % 5 == 0)
  506. fprintf(ofp, "\n\t ");
  507. fprintf(ofp, "$%s", f->name);
  508. }
  509. fprintf(ofp, ") = @_;\n\n");
  510. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  511. "$common_secs, $common_nsecs,\n\t "
  512. "$common_pid, $common_comm, $common_callchain);\n\n");
  513. fprintf(ofp, "\tprintf(\"");
  514. not_first = 0;
  515. count = 0;
  516. for (f = event->format.fields; f; f = f->next) {
  517. if (not_first++)
  518. fprintf(ofp, ", ");
  519. if (count && count % 4 == 0) {
  520. fprintf(ofp, "\".\n\t \"");
  521. }
  522. count++;
  523. fprintf(ofp, "%s=", f->name);
  524. if (f->flags & FIELD_IS_STRING ||
  525. f->flags & FIELD_IS_FLAG ||
  526. f->flags & FIELD_IS_SYMBOLIC)
  527. fprintf(ofp, "%%s");
  528. else if (f->flags & FIELD_IS_SIGNED)
  529. fprintf(ofp, "%%d");
  530. else
  531. fprintf(ofp, "%%u");
  532. }
  533. fprintf(ofp, "\\n\",\n\t ");
  534. not_first = 0;
  535. count = 0;
  536. for (f = event->format.fields; f; f = f->next) {
  537. if (not_first++)
  538. fprintf(ofp, ", ");
  539. if (++count % 5 == 0)
  540. fprintf(ofp, "\n\t ");
  541. if (f->flags & FIELD_IS_FLAG) {
  542. if ((count - 1) % 5 != 0) {
  543. fprintf(ofp, "\n\t ");
  544. count = 4;
  545. }
  546. fprintf(ofp, "flag_str(\"");
  547. fprintf(ofp, "%s::%s\", ", event->system,
  548. event->name);
  549. fprintf(ofp, "\"%s\", $%s)", f->name,
  550. f->name);
  551. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  552. if ((count - 1) % 5 != 0) {
  553. fprintf(ofp, "\n\t ");
  554. count = 4;
  555. }
  556. fprintf(ofp, "symbol_str(\"");
  557. fprintf(ofp, "%s::%s\", ", event->system,
  558. event->name);
  559. fprintf(ofp, "\"%s\", $%s)", f->name,
  560. f->name);
  561. } else
  562. fprintf(ofp, "$%s", f->name);
  563. }
  564. fprintf(ofp, ");\n\n");
  565. fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
  566. fprintf(ofp, "}\n\n");
  567. }
  568. fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
  569. "$common_cpu, $common_secs, $common_nsecs,\n\t "
  570. "$common_pid, $common_comm, $common_callchain) = @_;\n\n");
  571. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  572. "$common_secs, $common_nsecs,\n\t $common_pid, "
  573. "$common_comm, $common_callchain);\n");
  574. fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
  575. fprintf(ofp, "}\n\n");
  576. fprintf(ofp, "sub print_header\n{\n"
  577. "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
  578. "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
  579. "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
  580. fprintf(ofp,
  581. "\n# Packed byte string args of process_event():\n"
  582. "#\n"
  583. "# $event:\tunion perf_event\tutil/event.h\n"
  584. "# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
  585. "# $sample:\tstruct perf_sample\tutil/event.h\n"
  586. "# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
  587. "\n"
  588. "sub process_event\n"
  589. "{\n"
  590. "\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
  591. "\n"
  592. "\tmy @event\t= unpack(\"LSS\", $event);\n"
  593. "\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
  594. "\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
  595. "\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
  596. "\n"
  597. "\tuse Data::Dumper;\n"
  598. "\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
  599. "}\n");
  600. fclose(ofp);
  601. fprintf(stderr, "generated Perl script: %s\n", fname);
  602. return 0;
  603. }
  604. struct scripting_ops perl_scripting_ops = {
  605. .name = "Perl",
  606. .start_script = perl_start_script,
  607. .flush_script = perl_flush_script,
  608. .stop_script = perl_stop_script,
  609. .process_event = perl_process_event,
  610. .generate_script = perl_generate_script,
  611. };