trace-event-python.c 19 KB

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