trace-event-perl.c 15 KB

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