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