trace-event-perl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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(union perf_event *pevent __unused,
  208. struct perf_sample *sample,
  209. struct perf_session *session __unused,
  210. struct thread *thread)
  211. {
  212. struct format_field *field;
  213. static char handler[256];
  214. unsigned long long val;
  215. unsigned long s, ns;
  216. struct event *event;
  217. int type;
  218. int pid;
  219. int cpu = sample->cpu;
  220. void *data = sample->raw_data;
  221. unsigned long long nsecs = sample->time;
  222. char *comm = thread->comm;
  223. dSP;
  224. type = trace_parse_common_type(data);
  225. event = find_cache_event(type);
  226. if (!event)
  227. die("ug! no event found for type %d", type);
  228. pid = trace_parse_common_pid(data);
  229. sprintf(handler, "%s::%s", event->system, event->name);
  230. s = nsecs / NSECS_PER_SEC;
  231. ns = nsecs - s * NSECS_PER_SEC;
  232. scripting_context->event_data = data;
  233. ENTER;
  234. SAVETMPS;
  235. PUSHMARK(SP);
  236. XPUSHs(sv_2mortal(newSVpv(handler, 0)));
  237. XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
  238. XPUSHs(sv_2mortal(newSVuv(cpu)));
  239. XPUSHs(sv_2mortal(newSVuv(s)));
  240. XPUSHs(sv_2mortal(newSVuv(ns)));
  241. XPUSHs(sv_2mortal(newSViv(pid)));
  242. XPUSHs(sv_2mortal(newSVpv(comm, 0)));
  243. /* common fields other than pid can be accessed via xsub fns */
  244. for (field = event->format.fields; field; field = field->next) {
  245. if (field->flags & FIELD_IS_STRING) {
  246. int offset;
  247. if (field->flags & FIELD_IS_DYNAMIC) {
  248. offset = *(int *)(data + field->offset);
  249. offset &= 0xffff;
  250. } else
  251. offset = field->offset;
  252. XPUSHs(sv_2mortal(newSVpv((char *)data + offset, 0)));
  253. } else { /* FIELD_IS_NUMERIC */
  254. val = read_size(data + field->offset, field->size);
  255. if (field->flags & FIELD_IS_SIGNED) {
  256. XPUSHs(sv_2mortal(newSViv(val)));
  257. } else {
  258. XPUSHs(sv_2mortal(newSVuv(val)));
  259. }
  260. }
  261. }
  262. PUTBACK;
  263. if (get_cv(handler, 0))
  264. call_pv(handler, G_SCALAR);
  265. else if (get_cv("main::trace_unhandled", 0)) {
  266. XPUSHs(sv_2mortal(newSVpv(handler, 0)));
  267. XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
  268. XPUSHs(sv_2mortal(newSVuv(cpu)));
  269. XPUSHs(sv_2mortal(newSVuv(nsecs)));
  270. XPUSHs(sv_2mortal(newSViv(pid)));
  271. XPUSHs(sv_2mortal(newSVpv(comm, 0)));
  272. call_pv("main::trace_unhandled", G_SCALAR);
  273. }
  274. SPAGAIN;
  275. PUTBACK;
  276. FREETMPS;
  277. LEAVE;
  278. }
  279. static void run_start_sub(void)
  280. {
  281. dSP; /* access to Perl stack */
  282. PUSHMARK(SP);
  283. if (get_cv("main::trace_begin", 0))
  284. call_pv("main::trace_begin", G_DISCARD | G_NOARGS);
  285. }
  286. /*
  287. * Start trace script
  288. */
  289. static int perl_start_script(const char *script, int argc, const char **argv)
  290. {
  291. const char **command_line;
  292. int i, err = 0;
  293. command_line = malloc((argc + 2) * sizeof(const char *));
  294. command_line[0] = "";
  295. command_line[1] = script;
  296. for (i = 2; i < argc + 2; i++)
  297. command_line[i] = argv[i - 2];
  298. my_perl = perl_alloc();
  299. perl_construct(my_perl);
  300. if (perl_parse(my_perl, xs_init, argc + 2, (char **)command_line,
  301. (char **)NULL)) {
  302. err = -1;
  303. goto error;
  304. }
  305. if (perl_run(my_perl)) {
  306. err = -1;
  307. goto error;
  308. }
  309. if (SvTRUE(ERRSV)) {
  310. err = -1;
  311. goto error;
  312. }
  313. run_start_sub();
  314. free(command_line);
  315. return 0;
  316. error:
  317. perl_free(my_perl);
  318. free(command_line);
  319. return err;
  320. }
  321. /*
  322. * Stop trace script
  323. */
  324. static int perl_stop_script(void)
  325. {
  326. dSP; /* access to Perl stack */
  327. PUSHMARK(SP);
  328. if (get_cv("main::trace_end", 0))
  329. call_pv("main::trace_end", G_DISCARD | G_NOARGS);
  330. perl_destruct(my_perl);
  331. perl_free(my_perl);
  332. return 0;
  333. }
  334. static int perl_generate_script(const char *outfile)
  335. {
  336. struct event *event = NULL;
  337. struct format_field *f;
  338. char fname[PATH_MAX];
  339. int not_first, count;
  340. FILE *ofp;
  341. sprintf(fname, "%s.pl", outfile);
  342. ofp = fopen(fname, "w");
  343. if (ofp == NULL) {
  344. fprintf(stderr, "couldn't open %s\n", fname);
  345. return -1;
  346. }
  347. fprintf(ofp, "# perf script event handlers, "
  348. "generated by perf script -g perl\n");
  349. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  350. " License version 2\n\n");
  351. fprintf(ofp, "# The common_* event handler fields are the most useful "
  352. "fields common to\n");
  353. fprintf(ofp, "# all events. They don't necessarily correspond to "
  354. "the 'common_*' fields\n");
  355. fprintf(ofp, "# in the format files. Those fields not available as "
  356. "handler params can\n");
  357. fprintf(ofp, "# be retrieved using Perl functions of the form "
  358. "common_*($context).\n");
  359. fprintf(ofp, "# See Context.pm for the list of available "
  360. "functions.\n\n");
  361. fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
  362. "Perf-Trace-Util/lib\";\n");
  363. fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
  364. fprintf(ofp, "use Perf::Trace::Core;\n");
  365. fprintf(ofp, "use Perf::Trace::Context;\n");
  366. fprintf(ofp, "use Perf::Trace::Util;\n\n");
  367. fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
  368. fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n\n");
  369. while ((event = trace_find_next_event(event))) {
  370. fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
  371. fprintf(ofp, "\tmy (");
  372. fprintf(ofp, "$event_name, ");
  373. fprintf(ofp, "$context, ");
  374. fprintf(ofp, "$common_cpu, ");
  375. fprintf(ofp, "$common_secs, ");
  376. fprintf(ofp, "$common_nsecs,\n");
  377. fprintf(ofp, "\t $common_pid, ");
  378. fprintf(ofp, "$common_comm,\n\t ");
  379. not_first = 0;
  380. count = 0;
  381. for (f = event->format.fields; f; f = f->next) {
  382. if (not_first++)
  383. fprintf(ofp, ", ");
  384. if (++count % 5 == 0)
  385. fprintf(ofp, "\n\t ");
  386. fprintf(ofp, "$%s", f->name);
  387. }
  388. fprintf(ofp, ") = @_;\n\n");
  389. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  390. "$common_secs, $common_nsecs,\n\t "
  391. "$common_pid, $common_comm);\n\n");
  392. fprintf(ofp, "\tprintf(\"");
  393. not_first = 0;
  394. count = 0;
  395. for (f = event->format.fields; f; f = f->next) {
  396. if (not_first++)
  397. fprintf(ofp, ", ");
  398. if (count && count % 4 == 0) {
  399. fprintf(ofp, "\".\n\t \"");
  400. }
  401. count++;
  402. fprintf(ofp, "%s=", f->name);
  403. if (f->flags & FIELD_IS_STRING ||
  404. f->flags & FIELD_IS_FLAG ||
  405. f->flags & FIELD_IS_SYMBOLIC)
  406. fprintf(ofp, "%%s");
  407. else if (f->flags & FIELD_IS_SIGNED)
  408. fprintf(ofp, "%%d");
  409. else
  410. fprintf(ofp, "%%u");
  411. }
  412. fprintf(ofp, "\\n\",\n\t ");
  413. not_first = 0;
  414. count = 0;
  415. for (f = event->format.fields; f; f = f->next) {
  416. if (not_first++)
  417. fprintf(ofp, ", ");
  418. if (++count % 5 == 0)
  419. fprintf(ofp, "\n\t ");
  420. if (f->flags & FIELD_IS_FLAG) {
  421. if ((count - 1) % 5 != 0) {
  422. fprintf(ofp, "\n\t ");
  423. count = 4;
  424. }
  425. fprintf(ofp, "flag_str(\"");
  426. fprintf(ofp, "%s::%s\", ", event->system,
  427. event->name);
  428. fprintf(ofp, "\"%s\", $%s)", f->name,
  429. f->name);
  430. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  431. if ((count - 1) % 5 != 0) {
  432. fprintf(ofp, "\n\t ");
  433. count = 4;
  434. }
  435. fprintf(ofp, "symbol_str(\"");
  436. fprintf(ofp, "%s::%s\", ", event->system,
  437. event->name);
  438. fprintf(ofp, "\"%s\", $%s)", f->name,
  439. f->name);
  440. } else
  441. fprintf(ofp, "$%s", f->name);
  442. }
  443. fprintf(ofp, ");\n");
  444. fprintf(ofp, "}\n\n");
  445. }
  446. fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
  447. "$common_cpu, $common_secs, $common_nsecs,\n\t "
  448. "$common_pid, $common_comm) = @_;\n\n");
  449. fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
  450. "$common_secs, $common_nsecs,\n\t $common_pid, "
  451. "$common_comm);\n}\n\n");
  452. fprintf(ofp, "sub print_header\n{\n"
  453. "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
  454. "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
  455. "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}");
  456. fclose(ofp);
  457. fprintf(stderr, "generated Perl script: %s\n", fname);
  458. return 0;
  459. }
  460. struct scripting_ops perl_scripting_ops = {
  461. .name = "Perl",
  462. .start_script = perl_start_script,
  463. .stop_script = perl_stop_script,
  464. .process_event = perl_process_event,
  465. .generate_script = perl_generate_script,
  466. };