trace-event-perl.c 13 KB

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