trace-event-perl.c 13 KB

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