trace-event-perl.c 16 KB

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