trace-event-perl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. int common_pc(struct scripting_context *context)
  204. {
  205. int pc;
  206. pc = parse_common_pc(context->event_data);
  207. return pc;
  208. }
  209. int common_flags(struct scripting_context *context)
  210. {
  211. int flags;
  212. flags = parse_common_flags(context->event_data);
  213. return flags;
  214. }
  215. int common_lock_depth(struct scripting_context *context)
  216. {
  217. int lock_depth;
  218. lock_depth = parse_common_lock_depth(context->event_data);
  219. return lock_depth;
  220. }
  221. static void perl_process_event(int cpu, void *data,
  222. int size __unused,
  223. unsigned long long nsecs, char *comm)
  224. {
  225. struct format_field *field;
  226. static char handler[256];
  227. unsigned long long val;
  228. unsigned long s, ns;
  229. struct event *event;
  230. int type;
  231. int pid;
  232. dSP;
  233. type = trace_parse_common_type(data);
  234. event = find_cache_event(type);
  235. if (!event)
  236. die("ug! no event found for type %d", type);
  237. pid = trace_parse_common_pid(data);
  238. sprintf(handler, "%s::%s", event->system, event->name);
  239. s = nsecs / NSECS_PER_SEC;
  240. ns = nsecs - s * NSECS_PER_SEC;
  241. scripting_context->event_data = data;
  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(data + field->offset, field->size);
  264. if (field->flags & FIELD_IS_SIGNED) {
  265. XPUSHs(sv_2mortal(newSViv(val)));
  266. } else {
  267. XPUSHs(sv_2mortal(newSVuv(val)));
  268. }
  269. }
  270. }
  271. PUTBACK;
  272. if (get_cv(handler, 0))
  273. call_pv(handler, G_SCALAR);
  274. else if (get_cv("main::trace_unhandled", 0)) {
  275. XPUSHs(sv_2mortal(newSVpv(handler, 0)));
  276. XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
  277. XPUSHs(sv_2mortal(newSVuv(cpu)));
  278. XPUSHs(sv_2mortal(newSVuv(nsecs)));
  279. XPUSHs(sv_2mortal(newSViv(pid)));
  280. XPUSHs(sv_2mortal(newSVpv(comm, 0)));
  281. call_pv("main::trace_unhandled", G_SCALAR);
  282. }
  283. SPAGAIN;
  284. PUTBACK;
  285. FREETMPS;
  286. LEAVE;
  287. }
  288. static void run_start_sub(void)
  289. {
  290. dSP; /* access to Perl stack */
  291. PUSHMARK(SP);
  292. if (get_cv("main::trace_begin", 0))
  293. call_pv("main::trace_begin", G_DISCARD | G_NOARGS);
  294. }
  295. /*
  296. * Start trace script
  297. */
  298. static int perl_start_script(const char *script, int argc, const char **argv)
  299. {
  300. const char **command_line;
  301. int i, err = 0;
  302. command_line = malloc((argc + 2) * sizeof(const char *));
  303. command_line[0] = "";
  304. command_line[1] = script;
  305. for (i = 2; i < argc + 2; i++)
  306. command_line[i] = argv[i - 2];
  307. my_perl = perl_alloc();
  308. perl_construct(my_perl);
  309. if (perl_parse(my_perl, xs_init, argc + 2, (char **)command_line,
  310. (char **)NULL)) {
  311. err = -1;
  312. goto error;
  313. }
  314. if (perl_run(my_perl)) {
  315. err = -1;
  316. goto error;
  317. }
  318. if (SvTRUE(ERRSV)) {
  319. err = -1;
  320. goto error;
  321. }
  322. run_start_sub();
  323. free(command_line);
  324. fprintf(stderr, "perf trace started with Perl script %s\n\n", script);
  325. return 0;
  326. error:
  327. perl_free(my_perl);
  328. free(command_line);
  329. return err;
  330. }
  331. /*
  332. * Stop trace script
  333. */
  334. static int perl_stop_script(void)
  335. {
  336. dSP; /* access to Perl stack */
  337. PUSHMARK(SP);
  338. if (get_cv("main::trace_end", 0))
  339. call_pv("main::trace_end", G_DISCARD | G_NOARGS);
  340. perl_destruct(my_perl);
  341. perl_free(my_perl);
  342. fprintf(stderr, "\nperf trace Perl script stopped\n");
  343. return 0;
  344. }
  345. static int perl_generate_script(const char *outfile)
  346. {
  347. struct event *event = NULL;
  348. struct format_field *f;
  349. char fname[PATH_MAX];
  350. int not_first, count;
  351. FILE *ofp;
  352. sprintf(fname, "%s.pl", outfile);
  353. ofp = fopen(fname, "w");
  354. if (ofp == NULL) {
  355. fprintf(stderr, "couldn't open %s\n", fname);
  356. return -1;
  357. }
  358. fprintf(ofp, "# perf trace event handlers, "
  359. "generated by perf trace -g perl\n");
  360. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  361. " License version 2\n\n");
  362. fprintf(ofp, "# The common_* event handler fields are the most useful "
  363. "fields common to\n");
  364. fprintf(ofp, "# all events. They don't necessarily correspond to "
  365. "the 'common_*' fields\n");
  366. fprintf(ofp, "# in the format files. Those fields not available as "
  367. "handler params can\n");
  368. fprintf(ofp, "# be retrieved using Perl functions of the form "
  369. "common_*($context).\n");
  370. fprintf(ofp, "# See Context.pm for the list of available "
  371. "functions.\n\n");
  372. fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
  373. "Perf-Trace-Util/lib\";\n");
  374. fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
  375. fprintf(ofp, "use Perf::Trace::Core;\n");
  376. fprintf(ofp, "use Perf::Trace::Context;\n");
  377. fprintf(ofp, "use Perf::Trace::Util;\n\n");
  378. fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
  379. fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n\n");
  380. while ((event = trace_find_next_event(event))) {
  381. fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
  382. fprintf(ofp, "\tmy (");
  383. fprintf(ofp, "$event_name, ");
  384. fprintf(ofp, "$context, ");
  385. fprintf(ofp, "$common_cpu, ");
  386. fprintf(ofp, "$common_secs, ");
  387. fprintf(ofp, "$common_nsecs,\n");
  388. fprintf(ofp, "\t $common_pid, ");
  389. fprintf(ofp, "$common_comm,\n\t ");
  390. not_first = 0;
  391. count = 0;
  392. for (f = event->format.fields; f; f = f->next) {
  393. if (not_first++)
  394. fprintf(ofp, ", ");
  395. if (++count % 5 == 0)
  396. fprintf(ofp, "\n\t ");
  397. fprintf(ofp, "$%s", f->name);
  398. }
  399. fprintf(ofp, ") = @_;\n\n");
  400. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  401. "$common_secs, $common_nsecs,\n\t "
  402. "$common_pid, $common_comm);\n\n");
  403. fprintf(ofp, "\tprintf(\"");
  404. not_first = 0;
  405. count = 0;
  406. for (f = event->format.fields; f; f = f->next) {
  407. if (not_first++)
  408. fprintf(ofp, ", ");
  409. if (count && count % 4 == 0) {
  410. fprintf(ofp, "\".\n\t \"");
  411. }
  412. count++;
  413. fprintf(ofp, "%s=", f->name);
  414. if (f->flags & FIELD_IS_STRING ||
  415. f->flags & FIELD_IS_FLAG ||
  416. f->flags & FIELD_IS_SYMBOLIC)
  417. fprintf(ofp, "%%s");
  418. else if (f->flags & FIELD_IS_SIGNED)
  419. fprintf(ofp, "%%d");
  420. else
  421. fprintf(ofp, "%%u");
  422. }
  423. fprintf(ofp, "\\n\",\n\t ");
  424. not_first = 0;
  425. count = 0;
  426. for (f = event->format.fields; f; f = f->next) {
  427. if (not_first++)
  428. fprintf(ofp, ", ");
  429. if (++count % 5 == 0)
  430. fprintf(ofp, "\n\t ");
  431. if (f->flags & FIELD_IS_FLAG) {
  432. if ((count - 1) % 5 != 0) {
  433. fprintf(ofp, "\n\t ");
  434. count = 4;
  435. }
  436. fprintf(ofp, "flag_str(\"");
  437. fprintf(ofp, "%s::%s\", ", event->system,
  438. event->name);
  439. fprintf(ofp, "\"%s\", $%s)", f->name,
  440. f->name);
  441. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  442. if ((count - 1) % 5 != 0) {
  443. fprintf(ofp, "\n\t ");
  444. count = 4;
  445. }
  446. fprintf(ofp, "symbol_str(\"");
  447. fprintf(ofp, "%s::%s\", ", event->system,
  448. event->name);
  449. fprintf(ofp, "\"%s\", $%s)", f->name,
  450. f->name);
  451. } else
  452. fprintf(ofp, "$%s", f->name);
  453. }
  454. fprintf(ofp, ");\n");
  455. fprintf(ofp, "}\n\n");
  456. }
  457. fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
  458. "$common_cpu, $common_secs, $common_nsecs,\n\t "
  459. "$common_pid, $common_comm) = @_;\n\n");
  460. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  461. "$common_secs, $common_nsecs,\n\t $common_pid, "
  462. "$common_comm);\n}\n\n");
  463. fprintf(ofp, "sub print_header\n{\n"
  464. "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
  465. "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
  466. "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}");
  467. fclose(ofp);
  468. fprintf(stderr, "generated Perl script: %s\n", fname);
  469. return 0;
  470. }
  471. struct scripting_ops perl_scripting_ops = {
  472. .name = "Perl",
  473. .start_script = perl_start_script,
  474. .stop_script = perl_stop_script,
  475. .process_event = perl_process_event,
  476. .generate_script = perl_generate_script,
  477. };
  478. static void print_unsupported_msg(void)
  479. {
  480. fprintf(stderr, "Perl scripting not supported."
  481. " Install libperl and rebuild perf to enable it.\n"
  482. "For example:\n # apt-get install libperl-dev (ubuntu)"
  483. "\n # yum install perl-ExtUtils-Embed (Fedora)"
  484. "\n etc.\n");
  485. }
  486. static int perl_start_script_unsupported(const char *script __unused,
  487. int argc __unused,
  488. const char **argv __unused)
  489. {
  490. print_unsupported_msg();
  491. return -1;
  492. }
  493. static int perl_stop_script_unsupported(void)
  494. {
  495. return 0;
  496. }
  497. static void perl_process_event_unsupported(int cpu __unused,
  498. void *data __unused,
  499. int size __unused,
  500. unsigned long long nsecs __unused,
  501. char *comm __unused)
  502. {
  503. }
  504. static int perl_generate_script_unsupported(const char *outfile __unused)
  505. {
  506. print_unsupported_msg();
  507. return -1;
  508. }
  509. struct scripting_ops perl_scripting_unsupported_ops = {
  510. .name = "Perl",
  511. .start_script = perl_start_script_unsupported,
  512. .stop_script = perl_stop_script_unsupported,
  513. .process_event = perl_process_event_unsupported,
  514. .generate_script = perl_generate_script_unsupported,
  515. };
  516. static void register_perl_scripting(struct scripting_ops *scripting_ops)
  517. {
  518. int err;
  519. err = script_spec_register("Perl", scripting_ops);
  520. if (err)
  521. die("error registering Perl script extension");
  522. err = script_spec_register("pl", scripting_ops);
  523. if (err)
  524. die("error registering pl script extension");
  525. scripting_context = malloc(sizeof(struct scripting_context));
  526. }
  527. #ifdef NO_LIBPERL
  528. void setup_perl_scripting(void)
  529. {
  530. register_perl_scripting(&perl_scripting_unsupported_ops);
  531. }
  532. #else
  533. void setup_perl_scripting(void)
  534. {
  535. register_perl_scripting(&perl_scripting_ops);
  536. }
  537. #endif