trace-event-perl.c 13 KB

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