trace-event-python.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * trace-event-python. Feed trace events to an embedded Python interpreter.
  3. *
  4. * Copyright (C) 2010 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 <Python.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include "../../perf.h"
  27. #include "../evsel.h"
  28. #include "../util.h"
  29. #include "../event.h"
  30. #include "../thread.h"
  31. #include "../trace-event.h"
  32. PyMODINIT_FUNC initperf_trace_context(void);
  33. #define FTRACE_MAX_EVENT \
  34. ((1 << (sizeof(unsigned short) * 8)) - 1)
  35. struct event_format *events[FTRACE_MAX_EVENT];
  36. #define MAX_FIELDS 64
  37. #define N_COMMON_FIELDS 7
  38. extern struct scripting_context *scripting_context;
  39. static char *cur_field_name;
  40. static int zero_flag_atom;
  41. static PyObject *main_module, *main_dict;
  42. static void handler_call_die(const char *handler_name)
  43. {
  44. PyErr_Print();
  45. Py_FatalError("problem in Python trace event handler");
  46. }
  47. static void define_value(enum print_arg_type field_type,
  48. const char *ev_name,
  49. const char *field_name,
  50. const char *field_value,
  51. const char *field_str)
  52. {
  53. const char *handler_name = "define_flag_value";
  54. PyObject *handler, *t, *retval;
  55. unsigned long long value;
  56. unsigned n = 0;
  57. if (field_type == PRINT_SYMBOL)
  58. handler_name = "define_symbolic_value";
  59. t = PyTuple_New(4);
  60. if (!t)
  61. Py_FatalError("couldn't create Python tuple");
  62. value = eval_flag(field_value);
  63. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  64. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  65. PyTuple_SetItem(t, n++, PyInt_FromLong(value));
  66. PyTuple_SetItem(t, n++, PyString_FromString(field_str));
  67. handler = PyDict_GetItemString(main_dict, handler_name);
  68. if (handler && PyCallable_Check(handler)) {
  69. retval = PyObject_CallObject(handler, t);
  70. if (retval == NULL)
  71. handler_call_die(handler_name);
  72. }
  73. Py_DECREF(t);
  74. }
  75. static void define_values(enum print_arg_type field_type,
  76. struct print_flag_sym *field,
  77. const char *ev_name,
  78. const char *field_name)
  79. {
  80. define_value(field_type, ev_name, field_name, field->value,
  81. field->str);
  82. if (field->next)
  83. define_values(field_type, field->next, ev_name, field_name);
  84. }
  85. static void define_field(enum print_arg_type field_type,
  86. const char *ev_name,
  87. const char *field_name,
  88. const char *delim)
  89. {
  90. const char *handler_name = "define_flag_field";
  91. PyObject *handler, *t, *retval;
  92. unsigned n = 0;
  93. if (field_type == PRINT_SYMBOL)
  94. handler_name = "define_symbolic_field";
  95. if (field_type == PRINT_FLAGS)
  96. t = PyTuple_New(3);
  97. else
  98. t = PyTuple_New(2);
  99. if (!t)
  100. Py_FatalError("couldn't create Python tuple");
  101. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  102. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  103. if (field_type == PRINT_FLAGS)
  104. PyTuple_SetItem(t, n++, PyString_FromString(delim));
  105. handler = PyDict_GetItemString(main_dict, handler_name);
  106. if (handler && PyCallable_Check(handler)) {
  107. retval = PyObject_CallObject(handler, t);
  108. if (retval == NULL)
  109. handler_call_die(handler_name);
  110. }
  111. Py_DECREF(t);
  112. }
  113. static void define_event_symbols(struct event_format *event,
  114. const char *ev_name,
  115. struct print_arg *args)
  116. {
  117. switch (args->type) {
  118. case PRINT_NULL:
  119. break;
  120. case PRINT_ATOM:
  121. define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
  122. args->atom.atom);
  123. zero_flag_atom = 0;
  124. break;
  125. case PRINT_FIELD:
  126. if (cur_field_name)
  127. free(cur_field_name);
  128. cur_field_name = strdup(args->field.name);
  129. break;
  130. case PRINT_FLAGS:
  131. define_event_symbols(event, ev_name, args->flags.field);
  132. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  133. args->flags.delim);
  134. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  135. cur_field_name);
  136. break;
  137. case PRINT_SYMBOL:
  138. define_event_symbols(event, ev_name, args->symbol.field);
  139. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  140. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  141. cur_field_name);
  142. break;
  143. case PRINT_HEX:
  144. define_event_symbols(event, ev_name, args->hex.field);
  145. define_event_symbols(event, ev_name, args->hex.size);
  146. break;
  147. case PRINT_STRING:
  148. break;
  149. case PRINT_TYPE:
  150. define_event_symbols(event, ev_name, args->typecast.item);
  151. break;
  152. case PRINT_OP:
  153. if (strcmp(args->op.op, ":") == 0)
  154. zero_flag_atom = 1;
  155. define_event_symbols(event, ev_name, args->op.left);
  156. define_event_symbols(event, ev_name, args->op.right);
  157. break;
  158. default:
  159. /* gcc warns for these? */
  160. case PRINT_BSTRING:
  161. case PRINT_DYNAMIC_ARRAY:
  162. case PRINT_FUNC:
  163. /* we should warn... */
  164. return;
  165. }
  166. if (args->next)
  167. define_event_symbols(event, ev_name, args->next);
  168. }
  169. static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
  170. {
  171. static char ev_name[256];
  172. struct event_format *event;
  173. int type = evsel->attr.config;
  174. /*
  175. * XXX: Do we really need to cache this since now we have evsel->tp_format
  176. * cached already? Need to re-read this "cache" routine that as well calls
  177. * define_event_symbols() :-\
  178. */
  179. if (events[type])
  180. return events[type];
  181. events[type] = event = evsel->tp_format;
  182. if (!event)
  183. return NULL;
  184. sprintf(ev_name, "%s__%s", event->system, event->name);
  185. define_event_symbols(event, ev_name, event->print_fmt.args);
  186. return event;
  187. }
  188. static void python_process_tracepoint(union perf_event *perf_event
  189. __maybe_unused,
  190. struct perf_sample *sample,
  191. struct perf_evsel *evsel,
  192. struct machine *machine __maybe_unused,
  193. struct addr_location *al)
  194. {
  195. PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
  196. static char handler_name[256];
  197. struct format_field *field;
  198. unsigned long long val;
  199. unsigned long s, ns;
  200. struct event_format *event;
  201. unsigned n = 0;
  202. int pid;
  203. int cpu = sample->cpu;
  204. void *data = sample->raw_data;
  205. unsigned long long nsecs = sample->time;
  206. struct thread *thread = al->thread;
  207. char *comm = thread->comm;
  208. t = PyTuple_New(MAX_FIELDS);
  209. if (!t)
  210. Py_FatalError("couldn't create Python tuple");
  211. event = find_cache_event(evsel);
  212. if (!event)
  213. die("ug! no event found for type %d", (int)evsel->attr.config);
  214. pid = raw_field_value(event, "common_pid", data);
  215. sprintf(handler_name, "%s__%s", event->system, event->name);
  216. handler = PyDict_GetItemString(main_dict, handler_name);
  217. if (handler && !PyCallable_Check(handler))
  218. handler = NULL;
  219. if (!handler) {
  220. dict = PyDict_New();
  221. if (!dict)
  222. Py_FatalError("couldn't create Python dict");
  223. }
  224. s = nsecs / NSECS_PER_SEC;
  225. ns = nsecs - s * NSECS_PER_SEC;
  226. scripting_context->event_data = data;
  227. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  228. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  229. PyTuple_SetItem(t, n++, context);
  230. if (handler) {
  231. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  232. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  233. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  234. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  235. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  236. } else {
  237. PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
  238. PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
  239. PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
  240. PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
  241. PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
  242. }
  243. for (field = event->format.fields; field; field = field->next) {
  244. if (field->flags & FIELD_IS_STRING) {
  245. int offset;
  246. if (field->flags & FIELD_IS_DYNAMIC) {
  247. offset = *(int *)(data + field->offset);
  248. offset &= 0xffff;
  249. } else
  250. offset = field->offset;
  251. obj = PyString_FromString((char *)data + offset);
  252. } else { /* FIELD_IS_NUMERIC */
  253. val = read_size(event, data + field->offset,
  254. field->size);
  255. if (field->flags & FIELD_IS_SIGNED) {
  256. if ((long long)val >= LONG_MIN &&
  257. (long long)val <= LONG_MAX)
  258. obj = PyInt_FromLong(val);
  259. else
  260. obj = PyLong_FromLongLong(val);
  261. } else {
  262. if (val <= LONG_MAX)
  263. obj = PyInt_FromLong(val);
  264. else
  265. obj = PyLong_FromUnsignedLongLong(val);
  266. }
  267. }
  268. if (handler)
  269. PyTuple_SetItem(t, n++, obj);
  270. else
  271. PyDict_SetItemString(dict, field->name, obj);
  272. }
  273. if (!handler)
  274. PyTuple_SetItem(t, n++, dict);
  275. if (_PyTuple_Resize(&t, n) == -1)
  276. Py_FatalError("error resizing Python tuple");
  277. if (handler) {
  278. retval = PyObject_CallObject(handler, t);
  279. if (retval == NULL)
  280. handler_call_die(handler_name);
  281. } else {
  282. handler = PyDict_GetItemString(main_dict, "trace_unhandled");
  283. if (handler && PyCallable_Check(handler)) {
  284. retval = PyObject_CallObject(handler, t);
  285. if (retval == NULL)
  286. handler_call_die("trace_unhandled");
  287. }
  288. Py_DECREF(dict);
  289. }
  290. Py_DECREF(t);
  291. }
  292. static void python_process_general_event(union perf_event *perf_event
  293. __maybe_unused,
  294. struct perf_sample *sample,
  295. struct perf_evsel *evsel,
  296. struct machine *machine __maybe_unused,
  297. struct addr_location *al)
  298. {
  299. PyObject *handler, *retval, *t, *dict;
  300. static char handler_name[64];
  301. unsigned n = 0;
  302. struct thread *thread = al->thread;
  303. /*
  304. * Use the MAX_FIELDS to make the function expandable, though
  305. * currently there is only one item for the tuple.
  306. */
  307. t = PyTuple_New(MAX_FIELDS);
  308. if (!t)
  309. Py_FatalError("couldn't create Python tuple");
  310. dict = PyDict_New();
  311. if (!dict)
  312. Py_FatalError("couldn't create Python dictionary");
  313. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  314. handler = PyDict_GetItemString(main_dict, handler_name);
  315. if (!handler || !PyCallable_Check(handler))
  316. goto exit;
  317. PyDict_SetItemString(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  318. PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize(
  319. (const char *)&evsel->attr, sizeof(evsel->attr)));
  320. PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize(
  321. (const char *)sample, sizeof(*sample)));
  322. PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize(
  323. (const char *)sample->raw_data, sample->raw_size));
  324. PyDict_SetItemString(dict, "comm",
  325. PyString_FromString(thread->comm));
  326. if (al->map) {
  327. PyDict_SetItemString(dict, "dso",
  328. PyString_FromString(al->map->dso->name));
  329. }
  330. if (al->sym) {
  331. PyDict_SetItemString(dict, "symbol",
  332. PyString_FromString(al->sym->name));
  333. }
  334. PyTuple_SetItem(t, n++, dict);
  335. if (_PyTuple_Resize(&t, n) == -1)
  336. Py_FatalError("error resizing Python tuple");
  337. retval = PyObject_CallObject(handler, t);
  338. if (retval == NULL)
  339. handler_call_die(handler_name);
  340. exit:
  341. Py_DECREF(dict);
  342. Py_DECREF(t);
  343. }
  344. static void python_process_event(union perf_event *perf_event,
  345. struct perf_sample *sample,
  346. struct perf_evsel *evsel,
  347. struct machine *machine,
  348. struct addr_location *al)
  349. {
  350. switch (evsel->attr.type) {
  351. case PERF_TYPE_TRACEPOINT:
  352. python_process_tracepoint(perf_event, sample, evsel,
  353. machine, al);
  354. break;
  355. /* Reserve for future process_hw/sw/raw APIs */
  356. default:
  357. python_process_general_event(perf_event, sample, evsel,
  358. machine, al);
  359. }
  360. }
  361. static int run_start_sub(void)
  362. {
  363. PyObject *handler, *retval;
  364. int err = 0;
  365. main_module = PyImport_AddModule("__main__");
  366. if (main_module == NULL)
  367. return -1;
  368. Py_INCREF(main_module);
  369. main_dict = PyModule_GetDict(main_module);
  370. if (main_dict == NULL) {
  371. err = -1;
  372. goto error;
  373. }
  374. Py_INCREF(main_dict);
  375. handler = PyDict_GetItemString(main_dict, "trace_begin");
  376. if (handler == NULL || !PyCallable_Check(handler))
  377. goto out;
  378. retval = PyObject_CallObject(handler, NULL);
  379. if (retval == NULL)
  380. handler_call_die("trace_begin");
  381. Py_DECREF(retval);
  382. return err;
  383. error:
  384. Py_XDECREF(main_dict);
  385. Py_XDECREF(main_module);
  386. out:
  387. return err;
  388. }
  389. /*
  390. * Start trace script
  391. */
  392. static int python_start_script(const char *script, int argc, const char **argv)
  393. {
  394. const char **command_line;
  395. char buf[PATH_MAX];
  396. int i, err = 0;
  397. FILE *fp;
  398. command_line = malloc((argc + 1) * sizeof(const char *));
  399. command_line[0] = script;
  400. for (i = 1; i < argc + 1; i++)
  401. command_line[i] = argv[i - 1];
  402. Py_Initialize();
  403. initperf_trace_context();
  404. PySys_SetArgv(argc + 1, (char **)command_line);
  405. fp = fopen(script, "r");
  406. if (!fp) {
  407. sprintf(buf, "Can't open python script \"%s\"", script);
  408. perror(buf);
  409. err = -1;
  410. goto error;
  411. }
  412. err = PyRun_SimpleFile(fp, script);
  413. if (err) {
  414. fprintf(stderr, "Error running python script %s\n", script);
  415. goto error;
  416. }
  417. err = run_start_sub();
  418. if (err) {
  419. fprintf(stderr, "Error starting python script %s\n", script);
  420. goto error;
  421. }
  422. free(command_line);
  423. return err;
  424. error:
  425. Py_Finalize();
  426. free(command_line);
  427. return err;
  428. }
  429. /*
  430. * Stop trace script
  431. */
  432. static int python_stop_script(void)
  433. {
  434. PyObject *handler, *retval;
  435. int err = 0;
  436. handler = PyDict_GetItemString(main_dict, "trace_end");
  437. if (handler == NULL || !PyCallable_Check(handler))
  438. goto out;
  439. retval = PyObject_CallObject(handler, NULL);
  440. if (retval == NULL)
  441. handler_call_die("trace_end");
  442. else
  443. Py_DECREF(retval);
  444. out:
  445. Py_XDECREF(main_dict);
  446. Py_XDECREF(main_module);
  447. Py_Finalize();
  448. return err;
  449. }
  450. static int python_generate_script(struct pevent *pevent, const char *outfile)
  451. {
  452. struct event_format *event = NULL;
  453. struct format_field *f;
  454. char fname[PATH_MAX];
  455. int not_first, count;
  456. FILE *ofp;
  457. sprintf(fname, "%s.py", outfile);
  458. ofp = fopen(fname, "w");
  459. if (ofp == NULL) {
  460. fprintf(stderr, "couldn't open %s\n", fname);
  461. return -1;
  462. }
  463. fprintf(ofp, "# perf script event handlers, "
  464. "generated by perf script -g python\n");
  465. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  466. " License version 2\n\n");
  467. fprintf(ofp, "# The common_* event handler fields are the most useful "
  468. "fields common to\n");
  469. fprintf(ofp, "# all events. They don't necessarily correspond to "
  470. "the 'common_*' fields\n");
  471. fprintf(ofp, "# in the format files. Those fields not available as "
  472. "handler params can\n");
  473. fprintf(ofp, "# be retrieved using Python functions of the form "
  474. "common_*(context).\n");
  475. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  476. "of available functions.\n\n");
  477. fprintf(ofp, "import os\n");
  478. fprintf(ofp, "import sys\n\n");
  479. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  480. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  481. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  482. fprintf(ofp, "from Core import *\n\n\n");
  483. fprintf(ofp, "def trace_begin():\n");
  484. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  485. fprintf(ofp, "def trace_end():\n");
  486. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  487. while ((event = trace_find_next_event(pevent, event))) {
  488. fprintf(ofp, "def %s__%s(", event->system, event->name);
  489. fprintf(ofp, "event_name, ");
  490. fprintf(ofp, "context, ");
  491. fprintf(ofp, "common_cpu,\n");
  492. fprintf(ofp, "\tcommon_secs, ");
  493. fprintf(ofp, "common_nsecs, ");
  494. fprintf(ofp, "common_pid, ");
  495. fprintf(ofp, "common_comm,\n\t");
  496. not_first = 0;
  497. count = 0;
  498. for (f = event->format.fields; f; f = f->next) {
  499. if (not_first++)
  500. fprintf(ofp, ", ");
  501. if (++count % 5 == 0)
  502. fprintf(ofp, "\n\t");
  503. fprintf(ofp, "%s", f->name);
  504. }
  505. fprintf(ofp, "):\n");
  506. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  507. "common_secs, common_nsecs,\n\t\t\t"
  508. "common_pid, common_comm)\n\n");
  509. fprintf(ofp, "\t\tprint \"");
  510. not_first = 0;
  511. count = 0;
  512. for (f = event->format.fields; f; f = f->next) {
  513. if (not_first++)
  514. fprintf(ofp, ", ");
  515. if (count && count % 3 == 0) {
  516. fprintf(ofp, "\" \\\n\t\t\"");
  517. }
  518. count++;
  519. fprintf(ofp, "%s=", f->name);
  520. if (f->flags & FIELD_IS_STRING ||
  521. f->flags & FIELD_IS_FLAG ||
  522. f->flags & FIELD_IS_SYMBOLIC)
  523. fprintf(ofp, "%%s");
  524. else if (f->flags & FIELD_IS_SIGNED)
  525. fprintf(ofp, "%%d");
  526. else
  527. fprintf(ofp, "%%u");
  528. }
  529. fprintf(ofp, "\\n\" %% \\\n\t\t(");
  530. not_first = 0;
  531. count = 0;
  532. for (f = event->format.fields; f; f = f->next) {
  533. if (not_first++)
  534. fprintf(ofp, ", ");
  535. if (++count % 5 == 0)
  536. fprintf(ofp, "\n\t\t");
  537. if (f->flags & FIELD_IS_FLAG) {
  538. if ((count - 1) % 5 != 0) {
  539. fprintf(ofp, "\n\t\t");
  540. count = 4;
  541. }
  542. fprintf(ofp, "flag_str(\"");
  543. fprintf(ofp, "%s__%s\", ", event->system,
  544. event->name);
  545. fprintf(ofp, "\"%s\", %s)", f->name,
  546. f->name);
  547. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  548. if ((count - 1) % 5 != 0) {
  549. fprintf(ofp, "\n\t\t");
  550. count = 4;
  551. }
  552. fprintf(ofp, "symbol_str(\"");
  553. fprintf(ofp, "%s__%s\", ", event->system,
  554. event->name);
  555. fprintf(ofp, "\"%s\", %s)", f->name,
  556. f->name);
  557. } else
  558. fprintf(ofp, "%s", f->name);
  559. }
  560. fprintf(ofp, "),\n\n");
  561. }
  562. fprintf(ofp, "def trace_unhandled(event_name, context, "
  563. "event_fields_dict):\n");
  564. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  565. "for k,v in sorted(event_fields_dict.items())])\n\n");
  566. fprintf(ofp, "def print_header("
  567. "event_name, cpu, secs, nsecs, pid, comm):\n"
  568. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  569. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  570. fclose(ofp);
  571. fprintf(stderr, "generated Python script: %s\n", fname);
  572. return 0;
  573. }
  574. struct scripting_ops python_scripting_ops = {
  575. .name = "Python",
  576. .start_script = python_start_script,
  577. .stop_script = python_stop_script,
  578. .process_event = python_process_event,
  579. .generate_script = python_generate_script,
  580. };