trace-event-perl.c 15 KB

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