trace-event-python.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. free(cur_field_name);
  137. cur_field_name = strdup(args->field.name);
  138. break;
  139. case PRINT_FLAGS:
  140. define_event_symbols(event, ev_name, args->flags.field);
  141. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  142. args->flags.delim);
  143. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  144. cur_field_name);
  145. break;
  146. case PRINT_SYMBOL:
  147. define_event_symbols(event, ev_name, args->symbol.field);
  148. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  149. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  150. cur_field_name);
  151. break;
  152. case PRINT_HEX:
  153. define_event_symbols(event, ev_name, args->hex.field);
  154. define_event_symbols(event, ev_name, args->hex.size);
  155. break;
  156. case PRINT_STRING:
  157. break;
  158. case PRINT_TYPE:
  159. define_event_symbols(event, ev_name, args->typecast.item);
  160. break;
  161. case PRINT_OP:
  162. if (strcmp(args->op.op, ":") == 0)
  163. zero_flag_atom = 1;
  164. define_event_symbols(event, ev_name, args->op.left);
  165. define_event_symbols(event, ev_name, args->op.right);
  166. break;
  167. default:
  168. /* gcc warns for these? */
  169. case PRINT_BSTRING:
  170. case PRINT_DYNAMIC_ARRAY:
  171. case PRINT_FUNC:
  172. case PRINT_BITMASK:
  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 PyObject *get_field_numeric_entry(struct event_format *event,
  199. struct format_field *field, void *data)
  200. {
  201. bool is_array = field->flags & FIELD_IS_ARRAY;
  202. PyObject *obj, *list = NULL;
  203. unsigned long long val;
  204. unsigned int item_size, n_items, i;
  205. if (is_array) {
  206. list = PyList_New(field->arraylen);
  207. item_size = field->size / field->arraylen;
  208. n_items = field->arraylen;
  209. } else {
  210. item_size = field->size;
  211. n_items = 1;
  212. }
  213. for (i = 0; i < n_items; i++) {
  214. val = read_size(event, data + field->offset + i * item_size,
  215. item_size);
  216. if (field->flags & FIELD_IS_SIGNED) {
  217. if ((long long)val >= LONG_MIN &&
  218. (long long)val <= LONG_MAX)
  219. obj = PyInt_FromLong(val);
  220. else
  221. obj = PyLong_FromLongLong(val);
  222. } else {
  223. if (val <= LONG_MAX)
  224. obj = PyInt_FromLong(val);
  225. else
  226. obj = PyLong_FromUnsignedLongLong(val);
  227. }
  228. if (is_array)
  229. PyList_SET_ITEM(list, i, obj);
  230. }
  231. if (is_array)
  232. obj = list;
  233. return obj;
  234. }
  235. static void python_process_tracepoint(struct perf_sample *sample,
  236. struct perf_evsel *evsel,
  237. struct thread *thread,
  238. struct addr_location *al)
  239. {
  240. PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
  241. static char handler_name[256];
  242. struct format_field *field;
  243. unsigned long s, ns;
  244. struct event_format *event;
  245. unsigned n = 0;
  246. int pid;
  247. int cpu = sample->cpu;
  248. void *data = sample->raw_data;
  249. unsigned long long nsecs = sample->time;
  250. const char *comm = thread__comm_str(thread);
  251. t = PyTuple_New(MAX_FIELDS);
  252. if (!t)
  253. Py_FatalError("couldn't create Python tuple");
  254. event = find_cache_event(evsel);
  255. if (!event)
  256. die("ug! no event found for type %d", (int)evsel->attr.config);
  257. pid = raw_field_value(event, "common_pid", data);
  258. sprintf(handler_name, "%s__%s", event->system, event->name);
  259. handler = PyDict_GetItemString(main_dict, handler_name);
  260. if (handler && !PyCallable_Check(handler))
  261. handler = NULL;
  262. if (!handler) {
  263. dict = PyDict_New();
  264. if (!dict)
  265. Py_FatalError("couldn't create Python dict");
  266. }
  267. s = nsecs / NSECS_PER_SEC;
  268. ns = nsecs - s * NSECS_PER_SEC;
  269. scripting_context->event_data = data;
  270. scripting_context->pevent = evsel->tp_format->pevent;
  271. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  272. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  273. PyTuple_SetItem(t, n++, context);
  274. if (handler) {
  275. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  276. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  277. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  278. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  279. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  280. } else {
  281. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  282. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  283. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  284. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  285. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  286. }
  287. for (field = event->format.fields; field; field = field->next) {
  288. if (field->flags & FIELD_IS_STRING) {
  289. int offset;
  290. if (field->flags & FIELD_IS_DYNAMIC) {
  291. offset = *(int *)(data + field->offset);
  292. offset &= 0xffff;
  293. } else
  294. offset = field->offset;
  295. obj = PyString_FromString((char *)data + offset);
  296. } else { /* FIELD_IS_NUMERIC */
  297. obj = get_field_numeric_entry(event, field, data);
  298. }
  299. if (handler)
  300. PyTuple_SetItem(t, n++, obj);
  301. else
  302. pydict_set_item_string_decref(dict, field->name, obj);
  303. }
  304. if (!handler)
  305. PyTuple_SetItem(t, n++, dict);
  306. if (_PyTuple_Resize(&t, n) == -1)
  307. Py_FatalError("error resizing Python tuple");
  308. if (handler) {
  309. retval = PyObject_CallObject(handler, t);
  310. if (retval == NULL)
  311. handler_call_die(handler_name);
  312. } else {
  313. handler = PyDict_GetItemString(main_dict, "trace_unhandled");
  314. if (handler && PyCallable_Check(handler)) {
  315. retval = PyObject_CallObject(handler, t);
  316. if (retval == NULL)
  317. handler_call_die("trace_unhandled");
  318. }
  319. Py_DECREF(dict);
  320. }
  321. Py_DECREF(t);
  322. }
  323. static void python_process_general_event(struct perf_sample *sample,
  324. struct perf_evsel *evsel,
  325. struct thread *thread,
  326. struct addr_location *al)
  327. {
  328. PyObject *handler, *retval, *t, *dict;
  329. static char handler_name[64];
  330. unsigned n = 0;
  331. /*
  332. * Use the MAX_FIELDS to make the function expandable, though
  333. * currently there is only one item for the tuple.
  334. */
  335. t = PyTuple_New(MAX_FIELDS);
  336. if (!t)
  337. Py_FatalError("couldn't create Python tuple");
  338. dict = PyDict_New();
  339. if (!dict)
  340. Py_FatalError("couldn't create Python dictionary");
  341. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  342. handler = PyDict_GetItemString(main_dict, handler_name);
  343. if (!handler || !PyCallable_Check(handler))
  344. goto exit;
  345. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  346. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  347. (const char *)&evsel->attr, sizeof(evsel->attr)));
  348. pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize(
  349. (const char *)sample, sizeof(*sample)));
  350. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  351. (const char *)sample->raw_data, sample->raw_size));
  352. pydict_set_item_string_decref(dict, "comm",
  353. PyString_FromString(thread__comm_str(thread)));
  354. if (al->map) {
  355. pydict_set_item_string_decref(dict, "dso",
  356. PyString_FromString(al->map->dso->name));
  357. }
  358. if (al->sym) {
  359. pydict_set_item_string_decref(dict, "symbol",
  360. PyString_FromString(al->sym->name));
  361. }
  362. PyTuple_SetItem(t, n++, dict);
  363. if (_PyTuple_Resize(&t, n) == -1)
  364. Py_FatalError("error resizing Python tuple");
  365. retval = PyObject_CallObject(handler, t);
  366. if (retval == NULL)
  367. handler_call_die(handler_name);
  368. exit:
  369. Py_DECREF(dict);
  370. Py_DECREF(t);
  371. }
  372. static void python_process_event(union perf_event *event __maybe_unused,
  373. struct perf_sample *sample,
  374. struct perf_evsel *evsel,
  375. struct thread *thread,
  376. struct addr_location *al)
  377. {
  378. switch (evsel->attr.type) {
  379. case PERF_TYPE_TRACEPOINT:
  380. python_process_tracepoint(sample, evsel, thread, al);
  381. break;
  382. /* Reserve for future process_hw/sw/raw APIs */
  383. default:
  384. python_process_general_event(sample, evsel, thread, al);
  385. }
  386. }
  387. static int run_start_sub(void)
  388. {
  389. PyObject *handler, *retval;
  390. int err = 0;
  391. main_module = PyImport_AddModule("__main__");
  392. if (main_module == NULL)
  393. return -1;
  394. Py_INCREF(main_module);
  395. main_dict = PyModule_GetDict(main_module);
  396. if (main_dict == NULL) {
  397. err = -1;
  398. goto error;
  399. }
  400. Py_INCREF(main_dict);
  401. handler = PyDict_GetItemString(main_dict, "trace_begin");
  402. if (handler == NULL || !PyCallable_Check(handler))
  403. goto out;
  404. retval = PyObject_CallObject(handler, NULL);
  405. if (retval == NULL)
  406. handler_call_die("trace_begin");
  407. Py_DECREF(retval);
  408. return err;
  409. error:
  410. Py_XDECREF(main_dict);
  411. Py_XDECREF(main_module);
  412. out:
  413. return err;
  414. }
  415. /*
  416. * Start trace script
  417. */
  418. static int python_start_script(const char *script, int argc, const char **argv)
  419. {
  420. const char **command_line;
  421. char buf[PATH_MAX];
  422. int i, err = 0;
  423. FILE *fp;
  424. command_line = malloc((argc + 1) * sizeof(const char *));
  425. command_line[0] = script;
  426. for (i = 1; i < argc + 1; i++)
  427. command_line[i] = argv[i - 1];
  428. Py_Initialize();
  429. initperf_trace_context();
  430. PySys_SetArgv(argc + 1, (char **)command_line);
  431. fp = fopen(script, "r");
  432. if (!fp) {
  433. sprintf(buf, "Can't open python script \"%s\"", script);
  434. perror(buf);
  435. err = -1;
  436. goto error;
  437. }
  438. err = PyRun_SimpleFile(fp, script);
  439. if (err) {
  440. fprintf(stderr, "Error running python script %s\n", script);
  441. goto error;
  442. }
  443. err = run_start_sub();
  444. if (err) {
  445. fprintf(stderr, "Error starting python script %s\n", script);
  446. goto error;
  447. }
  448. free(command_line);
  449. return err;
  450. error:
  451. Py_Finalize();
  452. free(command_line);
  453. return err;
  454. }
  455. /*
  456. * Stop trace script
  457. */
  458. static int python_stop_script(void)
  459. {
  460. PyObject *handler, *retval;
  461. int err = 0;
  462. handler = PyDict_GetItemString(main_dict, "trace_end");
  463. if (handler == NULL || !PyCallable_Check(handler))
  464. goto out;
  465. retval = PyObject_CallObject(handler, NULL);
  466. if (retval == NULL)
  467. handler_call_die("trace_end");
  468. else
  469. Py_DECREF(retval);
  470. out:
  471. Py_XDECREF(main_dict);
  472. Py_XDECREF(main_module);
  473. Py_Finalize();
  474. return err;
  475. }
  476. static int python_generate_script(struct pevent *pevent, const char *outfile)
  477. {
  478. struct event_format *event = NULL;
  479. struct format_field *f;
  480. char fname[PATH_MAX];
  481. int not_first, count;
  482. FILE *ofp;
  483. sprintf(fname, "%s.py", outfile);
  484. ofp = fopen(fname, "w");
  485. if (ofp == NULL) {
  486. fprintf(stderr, "couldn't open %s\n", fname);
  487. return -1;
  488. }
  489. fprintf(ofp, "# perf script event handlers, "
  490. "generated by perf script -g python\n");
  491. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  492. " License version 2\n\n");
  493. fprintf(ofp, "# The common_* event handler fields are the most useful "
  494. "fields common to\n");
  495. fprintf(ofp, "# all events. They don't necessarily correspond to "
  496. "the 'common_*' fields\n");
  497. fprintf(ofp, "# in the format files. Those fields not available as "
  498. "handler params can\n");
  499. fprintf(ofp, "# be retrieved using Python functions of the form "
  500. "common_*(context).\n");
  501. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  502. "of available functions.\n\n");
  503. fprintf(ofp, "import os\n");
  504. fprintf(ofp, "import sys\n\n");
  505. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  506. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  507. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  508. fprintf(ofp, "from Core import *\n\n\n");
  509. fprintf(ofp, "def trace_begin():\n");
  510. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  511. fprintf(ofp, "def trace_end():\n");
  512. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  513. while ((event = trace_find_next_event(pevent, event))) {
  514. fprintf(ofp, "def %s__%s(", event->system, event->name);
  515. fprintf(ofp, "event_name, ");
  516. fprintf(ofp, "context, ");
  517. fprintf(ofp, "common_cpu,\n");
  518. fprintf(ofp, "\tcommon_secs, ");
  519. fprintf(ofp, "common_nsecs, ");
  520. fprintf(ofp, "common_pid, ");
  521. fprintf(ofp, "common_comm,\n\t");
  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 % 5 == 0)
  528. fprintf(ofp, "\n\t");
  529. fprintf(ofp, "%s", f->name);
  530. }
  531. fprintf(ofp, "):\n");
  532. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  533. "common_secs, common_nsecs,\n\t\t\t"
  534. "common_pid, common_comm)\n\n");
  535. fprintf(ofp, "\t\tprint \"");
  536. not_first = 0;
  537. count = 0;
  538. for (f = event->format.fields; f; f = f->next) {
  539. if (not_first++)
  540. fprintf(ofp, ", ");
  541. if (count && count % 3 == 0) {
  542. fprintf(ofp, "\" \\\n\t\t\"");
  543. }
  544. count++;
  545. fprintf(ofp, "%s=", f->name);
  546. if (f->flags & FIELD_IS_STRING ||
  547. f->flags & FIELD_IS_FLAG ||
  548. f->flags & FIELD_IS_ARRAY ||
  549. f->flags & FIELD_IS_SYMBOLIC)
  550. fprintf(ofp, "%%s");
  551. else if (f->flags & FIELD_IS_SIGNED)
  552. fprintf(ofp, "%%d");
  553. else
  554. fprintf(ofp, "%%u");
  555. }
  556. fprintf(ofp, "\\n\" %% \\\n\t\t(");
  557. not_first = 0;
  558. count = 0;
  559. for (f = event->format.fields; f; f = f->next) {
  560. if (not_first++)
  561. fprintf(ofp, ", ");
  562. if (++count % 5 == 0)
  563. fprintf(ofp, "\n\t\t");
  564. if (f->flags & FIELD_IS_FLAG) {
  565. if ((count - 1) % 5 != 0) {
  566. fprintf(ofp, "\n\t\t");
  567. count = 4;
  568. }
  569. fprintf(ofp, "flag_str(\"");
  570. fprintf(ofp, "%s__%s\", ", event->system,
  571. event->name);
  572. fprintf(ofp, "\"%s\", %s)", f->name,
  573. f->name);
  574. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  575. if ((count - 1) % 5 != 0) {
  576. fprintf(ofp, "\n\t\t");
  577. count = 4;
  578. }
  579. fprintf(ofp, "symbol_str(\"");
  580. fprintf(ofp, "%s__%s\", ", event->system,
  581. event->name);
  582. fprintf(ofp, "\"%s\", %s)", f->name,
  583. f->name);
  584. } else
  585. fprintf(ofp, "%s", f->name);
  586. }
  587. fprintf(ofp, "),\n\n");
  588. }
  589. fprintf(ofp, "def trace_unhandled(event_name, context, "
  590. "event_fields_dict):\n");
  591. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  592. "for k,v in sorted(event_fields_dict.items())])\n\n");
  593. fprintf(ofp, "def print_header("
  594. "event_name, cpu, secs, nsecs, pid, comm):\n"
  595. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  596. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  597. fclose(ofp);
  598. fprintf(stderr, "generated Python script: %s\n", fname);
  599. return 0;
  600. }
  601. struct scripting_ops python_scripting_ops = {
  602. .name = "Python",
  603. .start_script = python_start_script,
  604. .stop_script = python_stop_script,
  605. .process_event = python_process_event,
  606. .generate_script = python_generate_script,
  607. };