trace-event-perl.c 14 KB

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