trace-event-python.c 18 KB

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