trace-event-perl.c 18 KB

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