trace-event-perl.c 15 KB

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