trace-event-perl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. perl_run(my_perl);
  315. if (SvTRUE(ERRSV)) {
  316. err = -1;
  317. goto error;
  318. }
  319. run_start_sub();
  320. free(command_line);
  321. fprintf(stderr, "perf trace started with Perl script %s\n\n", script);
  322. return 0;
  323. error:
  324. perl_free(my_perl);
  325. free(command_line);
  326. return err;
  327. }
  328. /*
  329. * Stop trace script
  330. */
  331. static int perl_stop_script(void)
  332. {
  333. dSP; /* access to Perl stack */
  334. PUSHMARK(SP);
  335. if (get_cv("main::trace_end", 0))
  336. call_pv("main::trace_end", G_DISCARD | G_NOARGS);
  337. perl_destruct(my_perl);
  338. perl_free(my_perl);
  339. fprintf(stderr, "\nperf trace Perl script stopped\n");
  340. return 0;
  341. }
  342. static int perl_generate_script(const char *outfile)
  343. {
  344. struct event *event = NULL;
  345. struct format_field *f;
  346. char fname[PATH_MAX];
  347. int not_first, count;
  348. FILE *ofp;
  349. sprintf(fname, "%s.pl", outfile);
  350. ofp = fopen(fname, "w");
  351. if (ofp == NULL) {
  352. fprintf(stderr, "couldn't open %s\n", fname);
  353. return -1;
  354. }
  355. fprintf(ofp, "# perf trace event handlers, "
  356. "generated by perf trace -g perl\n");
  357. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  358. " License version 2\n\n");
  359. fprintf(ofp, "# The common_* event handler fields are the most useful "
  360. "fields common to\n");
  361. fprintf(ofp, "# all events. They don't necessarily correspond to "
  362. "the 'common_*' fields\n");
  363. fprintf(ofp, "# in the format files. Those fields not available as "
  364. "handler params can\n");
  365. fprintf(ofp, "# be retrieved using Perl functions of the form "
  366. "common_*($context).\n");
  367. fprintf(ofp, "# See Context.pm for the list of available "
  368. "functions.\n\n");
  369. fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
  370. "Perf-Trace-Util/lib\";\n");
  371. fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
  372. fprintf(ofp, "use Perf::Trace::Core;\n");
  373. fprintf(ofp, "use Perf::Trace::Context;\n");
  374. fprintf(ofp, "use Perf::Trace::Util;\n\n");
  375. fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
  376. fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n\n");
  377. while ((event = trace_find_next_event(event))) {
  378. fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
  379. fprintf(ofp, "\tmy (");
  380. fprintf(ofp, "$event_name, ");
  381. fprintf(ofp, "$context, ");
  382. fprintf(ofp, "$common_cpu, ");
  383. fprintf(ofp, "$common_secs, ");
  384. fprintf(ofp, "$common_nsecs,\n");
  385. fprintf(ofp, "\t $common_pid, ");
  386. fprintf(ofp, "$common_comm,\n\t ");
  387. not_first = 0;
  388. count = 0;
  389. for (f = event->format.fields; f; f = f->next) {
  390. if (not_first++)
  391. fprintf(ofp, ", ");
  392. if (++count % 5 == 0)
  393. fprintf(ofp, "\n\t ");
  394. fprintf(ofp, "$%s", f->name);
  395. }
  396. fprintf(ofp, ") = @_;\n\n");
  397. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  398. "$common_secs, $common_nsecs,\n\t "
  399. "$common_pid, $common_comm);\n\n");
  400. fprintf(ofp, "\tprintf(\"");
  401. not_first = 0;
  402. count = 0;
  403. for (f = event->format.fields; f; f = f->next) {
  404. if (not_first++)
  405. fprintf(ofp, ", ");
  406. if (count && count % 4 == 0) {
  407. fprintf(ofp, "\".\n\t \"");
  408. }
  409. count++;
  410. fprintf(ofp, "%s=", f->name);
  411. if (f->flags & FIELD_IS_STRING ||
  412. f->flags & FIELD_IS_FLAG ||
  413. f->flags & FIELD_IS_SYMBOLIC)
  414. fprintf(ofp, "%%s");
  415. else if (f->flags & FIELD_IS_SIGNED)
  416. fprintf(ofp, "%%d");
  417. else
  418. fprintf(ofp, "%%u");
  419. }
  420. fprintf(ofp, "\\n\",\n\t ");
  421. not_first = 0;
  422. count = 0;
  423. for (f = event->format.fields; f; f = f->next) {
  424. if (not_first++)
  425. fprintf(ofp, ", ");
  426. if (++count % 5 == 0)
  427. fprintf(ofp, "\n\t ");
  428. if (f->flags & FIELD_IS_FLAG) {
  429. if ((count - 1) % 5 != 0) {
  430. fprintf(ofp, "\n\t ");
  431. count = 4;
  432. }
  433. fprintf(ofp, "flag_str(\"");
  434. fprintf(ofp, "%s::%s\", ", event->system,
  435. event->name);
  436. fprintf(ofp, "\"%s\", $%s)", f->name,
  437. f->name);
  438. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  439. if ((count - 1) % 5 != 0) {
  440. fprintf(ofp, "\n\t ");
  441. count = 4;
  442. }
  443. fprintf(ofp, "symbol_str(\"");
  444. fprintf(ofp, "%s::%s\", ", event->system,
  445. event->name);
  446. fprintf(ofp, "\"%s\", $%s)", f->name,
  447. f->name);
  448. } else
  449. fprintf(ofp, "$%s", f->name);
  450. }
  451. fprintf(ofp, ");\n");
  452. fprintf(ofp, "}\n\n");
  453. }
  454. fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
  455. "$common_cpu, $common_secs, $common_nsecs,\n\t "
  456. "$common_pid, $common_comm) = @_;\n\n");
  457. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  458. "$common_secs, $common_nsecs,\n\t $common_pid, "
  459. "$common_comm);\n}\n\n");
  460. fprintf(ofp, "sub print_header\n{\n"
  461. "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
  462. "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
  463. "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}");
  464. fclose(ofp);
  465. fprintf(stderr, "generated Perl script: %s\n", fname);
  466. return 0;
  467. }
  468. struct scripting_ops perl_scripting_ops = {
  469. .name = "Perl",
  470. .start_script = perl_start_script,
  471. .stop_script = perl_stop_script,
  472. .process_event = perl_process_event,
  473. .generate_script = perl_generate_script,
  474. };
  475. static void print_unsupported_msg(void)
  476. {
  477. fprintf(stderr, "Perl scripting not supported."
  478. " Install libperl and rebuild perf to enable it.\n"
  479. "For example:\n # apt-get install libperl-dev (ubuntu)"
  480. "\n # yum install perl-ExtUtils-Embed (Fedora)"
  481. "\n etc.\n");
  482. }
  483. static int perl_start_script_unsupported(const char *script __unused,
  484. int argc __unused,
  485. const char **argv __unused)
  486. {
  487. print_unsupported_msg();
  488. return -1;
  489. }
  490. static int perl_stop_script_unsupported(void)
  491. {
  492. return 0;
  493. }
  494. static void perl_process_event_unsupported(int cpu __unused,
  495. void *data __unused,
  496. int size __unused,
  497. unsigned long long nsecs __unused,
  498. char *comm __unused)
  499. {
  500. }
  501. static int perl_generate_script_unsupported(const char *outfile __unused)
  502. {
  503. print_unsupported_msg();
  504. return -1;
  505. }
  506. struct scripting_ops perl_scripting_unsupported_ops = {
  507. .name = "Perl",
  508. .start_script = perl_start_script_unsupported,
  509. .stop_script = perl_stop_script_unsupported,
  510. .process_event = perl_process_event_unsupported,
  511. .generate_script = perl_generate_script_unsupported,
  512. };
  513. static void register_perl_scripting(struct scripting_ops *scripting_ops)
  514. {
  515. int err;
  516. err = script_spec_register("Perl", scripting_ops);
  517. if (err)
  518. die("error registering Perl script extension");
  519. err = script_spec_register("pl", scripting_ops);
  520. if (err)
  521. die("error registering pl script extension");
  522. scripting_context = malloc(sizeof(struct scripting_context));
  523. }
  524. #ifdef NO_LIBPERL
  525. void setup_perl_scripting(void)
  526. {
  527. register_perl_scripting(&perl_scripting_unsupported_ops);
  528. }
  529. #else
  530. void setup_perl_scripting(void)
  531. {
  532. register_perl_scripting(&perl_scripting_ops);
  533. }
  534. #endif