trace-event-perl.c 18 KB

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