trace-event-perl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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, &callchain_cursor, evsel,
  228. sample, NULL, NULL, scripting_max_stack) != 0) {
  229. pr_err("Failed to resolve callchain. Skipping\n");
  230. goto exit;
  231. }
  232. callchain_cursor_commit(&callchain_cursor);
  233. while (1) {
  234. HV *elem;
  235. struct callchain_cursor_node *node;
  236. node = callchain_cursor_current(&callchain_cursor);
  237. if (!node)
  238. break;
  239. elem = newHV();
  240. if (!elem)
  241. goto exit;
  242. if (!hv_stores(elem, "ip", newSVuv(node->ip))) {
  243. hv_undef(elem);
  244. goto exit;
  245. }
  246. if (node->sym) {
  247. HV *sym = newHV();
  248. if (!sym) {
  249. hv_undef(elem);
  250. goto exit;
  251. }
  252. if (!hv_stores(sym, "start", newSVuv(node->sym->start)) ||
  253. !hv_stores(sym, "end", newSVuv(node->sym->end)) ||
  254. !hv_stores(sym, "binding", newSVuv(node->sym->binding)) ||
  255. !hv_stores(sym, "name", newSVpvn(node->sym->name,
  256. node->sym->namelen)) ||
  257. !hv_stores(elem, "sym", newRV_noinc((SV*)sym))) {
  258. hv_undef(sym);
  259. hv_undef(elem);
  260. goto exit;
  261. }
  262. }
  263. if (node->map) {
  264. struct map *map = node->map;
  265. const char *dsoname = "[unknown]";
  266. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  267. if (symbol_conf.show_kernel_path && map->dso->long_name)
  268. dsoname = map->dso->long_name;
  269. else if (map->dso->name)
  270. dsoname = map->dso->name;
  271. }
  272. if (!hv_stores(elem, "dso", newSVpv(dsoname,0))) {
  273. hv_undef(elem);
  274. goto exit;
  275. }
  276. }
  277. callchain_cursor_advance(&callchain_cursor);
  278. av_push(list, newRV_noinc((SV*)elem));
  279. }
  280. exit:
  281. return newRV_noinc((SV*)list);
  282. }
  283. static void perl_process_tracepoint(struct perf_sample *sample,
  284. struct perf_evsel *evsel,
  285. struct addr_location *al)
  286. {
  287. struct thread *thread = al->thread;
  288. struct event_format *event = evsel->tp_format;
  289. struct format_field *field;
  290. static char handler[256];
  291. unsigned long long val;
  292. unsigned long s, ns;
  293. int pid;
  294. int cpu = sample->cpu;
  295. void *data = sample->raw_data;
  296. unsigned long long nsecs = sample->time;
  297. const char *comm = thread__comm_str(thread);
  298. dSP;
  299. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  300. return;
  301. if (!event)
  302. die("ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
  303. pid = raw_field_value(event, "common_pid", data);
  304. sprintf(handler, "%s::%s", event->system, event->name);
  305. if (!test_and_set_bit(event->id, events_defined))
  306. define_event_symbols(event, handler, event->print_fmt.args);
  307. s = nsecs / NSECS_PER_SEC;
  308. ns = nsecs - s * NSECS_PER_SEC;
  309. scripting_context->event_data = data;
  310. scripting_context->pevent = evsel->tp_format->pevent;
  311. ENTER;
  312. SAVETMPS;
  313. PUSHMARK(SP);
  314. XPUSHs(sv_2mortal(newSVpv(handler, 0)));
  315. XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
  316. XPUSHs(sv_2mortal(newSVuv(cpu)));
  317. XPUSHs(sv_2mortal(newSVuv(s)));
  318. XPUSHs(sv_2mortal(newSVuv(ns)));
  319. XPUSHs(sv_2mortal(newSViv(pid)));
  320. XPUSHs(sv_2mortal(newSVpv(comm, 0)));
  321. XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
  322. /* common fields other than pid can be accessed via xsub fns */
  323. for (field = event->format.fields; field; field = field->next) {
  324. if (field->flags & FIELD_IS_STRING) {
  325. int offset;
  326. if (field->flags & FIELD_IS_DYNAMIC) {
  327. offset = *(int *)(data + field->offset);
  328. offset &= 0xffff;
  329. } else
  330. offset = field->offset;
  331. XPUSHs(sv_2mortal(newSVpv((char *)data + offset, 0)));
  332. } else { /* FIELD_IS_NUMERIC */
  333. val = read_size(event, data + field->offset,
  334. field->size);
  335. if (field->flags & FIELD_IS_SIGNED) {
  336. XPUSHs(sv_2mortal(newSViv(val)));
  337. } else {
  338. XPUSHs(sv_2mortal(newSVuv(val)));
  339. }
  340. }
  341. }
  342. PUTBACK;
  343. if (get_cv(handler, 0))
  344. call_pv(handler, G_SCALAR);
  345. else if (get_cv("main::trace_unhandled", 0)) {
  346. XPUSHs(sv_2mortal(newSVpv(handler, 0)));
  347. XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
  348. XPUSHs(sv_2mortal(newSVuv(cpu)));
  349. XPUSHs(sv_2mortal(newSVuv(nsecs)));
  350. XPUSHs(sv_2mortal(newSViv(pid)));
  351. XPUSHs(sv_2mortal(newSVpv(comm, 0)));
  352. XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
  353. call_pv("main::trace_unhandled", G_SCALAR);
  354. }
  355. SPAGAIN;
  356. PUTBACK;
  357. FREETMPS;
  358. LEAVE;
  359. }
  360. static void perl_process_event_generic(union perf_event *event,
  361. struct perf_sample *sample,
  362. struct perf_evsel *evsel)
  363. {
  364. dSP;
  365. if (!get_cv("process_event", 0))
  366. return;
  367. ENTER;
  368. SAVETMPS;
  369. PUSHMARK(SP);
  370. XPUSHs(sv_2mortal(newSVpvn((const char *)event, event->header.size)));
  371. XPUSHs(sv_2mortal(newSVpvn((const char *)&evsel->attr, sizeof(evsel->attr))));
  372. XPUSHs(sv_2mortal(newSVpvn((const char *)sample, sizeof(*sample))));
  373. XPUSHs(sv_2mortal(newSVpvn((const char *)sample->raw_data, sample->raw_size)));
  374. PUTBACK;
  375. call_pv("process_event", G_SCALAR);
  376. SPAGAIN;
  377. PUTBACK;
  378. FREETMPS;
  379. LEAVE;
  380. }
  381. static void perl_process_event(union perf_event *event,
  382. struct perf_sample *sample,
  383. struct perf_evsel *evsel,
  384. struct addr_location *al)
  385. {
  386. perl_process_tracepoint(sample, evsel, al);
  387. perl_process_event_generic(event, sample, evsel);
  388. }
  389. static void run_start_sub(void)
  390. {
  391. dSP; /* access to Perl stack */
  392. PUSHMARK(SP);
  393. if (get_cv("main::trace_begin", 0))
  394. call_pv("main::trace_begin", G_DISCARD | G_NOARGS);
  395. }
  396. /*
  397. * Start trace script
  398. */
  399. static int perl_start_script(const char *script, int argc, const char **argv)
  400. {
  401. const char **command_line;
  402. int i, err = 0;
  403. command_line = malloc((argc + 2) * sizeof(const char *));
  404. command_line[0] = "";
  405. command_line[1] = script;
  406. for (i = 2; i < argc + 2; i++)
  407. command_line[i] = argv[i - 2];
  408. my_perl = perl_alloc();
  409. perl_construct(my_perl);
  410. if (perl_parse(my_perl, xs_init, argc + 2, (char **)command_line,
  411. (char **)NULL)) {
  412. err = -1;
  413. goto error;
  414. }
  415. if (perl_run(my_perl)) {
  416. err = -1;
  417. goto error;
  418. }
  419. if (SvTRUE(ERRSV)) {
  420. err = -1;
  421. goto error;
  422. }
  423. run_start_sub();
  424. free(command_line);
  425. return 0;
  426. error:
  427. perl_free(my_perl);
  428. free(command_line);
  429. return err;
  430. }
  431. static int perl_flush_script(void)
  432. {
  433. return 0;
  434. }
  435. /*
  436. * Stop trace script
  437. */
  438. static int perl_stop_script(void)
  439. {
  440. dSP; /* access to Perl stack */
  441. PUSHMARK(SP);
  442. if (get_cv("main::trace_end", 0))
  443. call_pv("main::trace_end", G_DISCARD | G_NOARGS);
  444. perl_destruct(my_perl);
  445. perl_free(my_perl);
  446. return 0;
  447. }
  448. static int perl_generate_script(struct pevent *pevent, const char *outfile)
  449. {
  450. struct event_format *event = NULL;
  451. struct format_field *f;
  452. char fname[PATH_MAX];
  453. int not_first, count;
  454. FILE *ofp;
  455. sprintf(fname, "%s.pl", outfile);
  456. ofp = fopen(fname, "w");
  457. if (ofp == NULL) {
  458. fprintf(stderr, "couldn't open %s\n", fname);
  459. return -1;
  460. }
  461. fprintf(ofp, "# perf script event handlers, "
  462. "generated by perf script -g perl\n");
  463. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  464. " License version 2\n\n");
  465. fprintf(ofp, "# The common_* event handler fields are the most useful "
  466. "fields common to\n");
  467. fprintf(ofp, "# all events. They don't necessarily correspond to "
  468. "the 'common_*' fields\n");
  469. fprintf(ofp, "# in the format files. Those fields not available as "
  470. "handler params can\n");
  471. fprintf(ofp, "# be retrieved using Perl functions of the form "
  472. "common_*($context).\n");
  473. fprintf(ofp, "# See Context.pm for the list of available "
  474. "functions.\n\n");
  475. fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
  476. "Perf-Trace-Util/lib\";\n");
  477. fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
  478. fprintf(ofp, "use Perf::Trace::Core;\n");
  479. fprintf(ofp, "use Perf::Trace::Context;\n");
  480. fprintf(ofp, "use Perf::Trace::Util;\n\n");
  481. fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
  482. fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n");
  483. fprintf(ofp, "\n\
  484. sub print_backtrace\n\
  485. {\n\
  486. my $callchain = shift;\n\
  487. for my $node (@$callchain)\n\
  488. {\n\
  489. if(exists $node->{sym})\n\
  490. {\n\
  491. printf( \"\\t[\\%%x] \\%%s\\n\", $node->{ip}, $node->{sym}{name});\n\
  492. }\n\
  493. else\n\
  494. {\n\
  495. printf( \"\\t[\\%%x]\\n\", $node{ip});\n\
  496. }\n\
  497. }\n\
  498. }\n\n\
  499. ");
  500. while ((event = trace_find_next_event(pevent, event))) {
  501. fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
  502. fprintf(ofp, "\tmy (");
  503. fprintf(ofp, "$event_name, ");
  504. fprintf(ofp, "$context, ");
  505. fprintf(ofp, "$common_cpu, ");
  506. fprintf(ofp, "$common_secs, ");
  507. fprintf(ofp, "$common_nsecs,\n");
  508. fprintf(ofp, "\t $common_pid, ");
  509. fprintf(ofp, "$common_comm, ");
  510. fprintf(ofp, "$common_callchain,\n\t ");
  511. not_first = 0;
  512. count = 0;
  513. for (f = event->format.fields; f; f = f->next) {
  514. if (not_first++)
  515. fprintf(ofp, ", ");
  516. if (++count % 5 == 0)
  517. fprintf(ofp, "\n\t ");
  518. fprintf(ofp, "$%s", f->name);
  519. }
  520. fprintf(ofp, ") = @_;\n\n");
  521. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  522. "$common_secs, $common_nsecs,\n\t "
  523. "$common_pid, $common_comm, $common_callchain);\n\n");
  524. fprintf(ofp, "\tprintf(\"");
  525. not_first = 0;
  526. count = 0;
  527. for (f = event->format.fields; f; f = f->next) {
  528. if (not_first++)
  529. fprintf(ofp, ", ");
  530. if (count && count % 4 == 0) {
  531. fprintf(ofp, "\".\n\t \"");
  532. }
  533. count++;
  534. fprintf(ofp, "%s=", f->name);
  535. if (f->flags & FIELD_IS_STRING ||
  536. f->flags & FIELD_IS_FLAG ||
  537. f->flags & FIELD_IS_SYMBOLIC)
  538. fprintf(ofp, "%%s");
  539. else if (f->flags & FIELD_IS_SIGNED)
  540. fprintf(ofp, "%%d");
  541. else
  542. fprintf(ofp, "%%u");
  543. }
  544. fprintf(ofp, "\\n\",\n\t ");
  545. not_first = 0;
  546. count = 0;
  547. for (f = event->format.fields; f; f = f->next) {
  548. if (not_first++)
  549. fprintf(ofp, ", ");
  550. if (++count % 5 == 0)
  551. fprintf(ofp, "\n\t ");
  552. if (f->flags & FIELD_IS_FLAG) {
  553. if ((count - 1) % 5 != 0) {
  554. fprintf(ofp, "\n\t ");
  555. count = 4;
  556. }
  557. fprintf(ofp, "flag_str(\"");
  558. fprintf(ofp, "%s::%s\", ", event->system,
  559. event->name);
  560. fprintf(ofp, "\"%s\", $%s)", f->name,
  561. f->name);
  562. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  563. if ((count - 1) % 5 != 0) {
  564. fprintf(ofp, "\n\t ");
  565. count = 4;
  566. }
  567. fprintf(ofp, "symbol_str(\"");
  568. fprintf(ofp, "%s::%s\", ", event->system,
  569. event->name);
  570. fprintf(ofp, "\"%s\", $%s)", f->name,
  571. f->name);
  572. } else
  573. fprintf(ofp, "$%s", f->name);
  574. }
  575. fprintf(ofp, ");\n\n");
  576. fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
  577. fprintf(ofp, "}\n\n");
  578. }
  579. fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
  580. "$common_cpu, $common_secs, $common_nsecs,\n\t "
  581. "$common_pid, $common_comm, $common_callchain) = @_;\n\n");
  582. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  583. "$common_secs, $common_nsecs,\n\t $common_pid, "
  584. "$common_comm, $common_callchain);\n");
  585. fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
  586. fprintf(ofp, "}\n\n");
  587. fprintf(ofp, "sub print_header\n{\n"
  588. "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
  589. "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
  590. "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
  591. fprintf(ofp,
  592. "\n# Packed byte string args of process_event():\n"
  593. "#\n"
  594. "# $event:\tunion perf_event\tutil/event.h\n"
  595. "# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
  596. "# $sample:\tstruct perf_sample\tutil/event.h\n"
  597. "# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
  598. "\n"
  599. "sub process_event\n"
  600. "{\n"
  601. "\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
  602. "\n"
  603. "\tmy @event\t= unpack(\"LSS\", $event);\n"
  604. "\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
  605. "\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
  606. "\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
  607. "\n"
  608. "\tuse Data::Dumper;\n"
  609. "\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
  610. "}\n");
  611. fclose(ofp);
  612. fprintf(stderr, "generated Perl script: %s\n", fname);
  613. return 0;
  614. }
  615. struct scripting_ops perl_scripting_ops = {
  616. .name = "Perl",
  617. .start_script = perl_start_script,
  618. .flush_script = perl_flush_script,
  619. .stop_script = perl_stop_script,
  620. .process_event = perl_process_event,
  621. .generate_script = perl_generate_script,
  622. };