trace-event-perl.c 18 KB

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