trace-event-python.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 <stdbool.h>
  26. #include <errno.h>
  27. #include <linux/bitmap.h>
  28. #include "../../perf.h"
  29. #include "../debug.h"
  30. #include "../callchain.h"
  31. #include "../evsel.h"
  32. #include "../util.h"
  33. #include "../event.h"
  34. #include "../thread.h"
  35. #include "../comm.h"
  36. #include "../machine.h"
  37. #include "../db-export.h"
  38. #include "../thread-stack.h"
  39. #include "../trace-event.h"
  40. #include "../machine.h"
  41. #include "../call-path.h"
  42. #include "thread_map.h"
  43. #include "cpumap.h"
  44. #include "stat.h"
  45. PyMODINIT_FUNC initperf_trace_context(void);
  46. #define TRACE_EVENT_TYPE_MAX \
  47. ((1 << (sizeof(unsigned short) * 8)) - 1)
  48. static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
  49. #define MAX_FIELDS 64
  50. #define N_COMMON_FIELDS 7
  51. extern struct scripting_context *scripting_context;
  52. static char *cur_field_name;
  53. static int zero_flag_atom;
  54. static PyObject *main_module, *main_dict;
  55. struct tables {
  56. struct db_export dbe;
  57. PyObject *evsel_handler;
  58. PyObject *machine_handler;
  59. PyObject *thread_handler;
  60. PyObject *comm_handler;
  61. PyObject *comm_thread_handler;
  62. PyObject *dso_handler;
  63. PyObject *symbol_handler;
  64. PyObject *branch_type_handler;
  65. PyObject *sample_handler;
  66. PyObject *call_path_handler;
  67. PyObject *call_return_handler;
  68. bool db_export_mode;
  69. };
  70. static struct tables tables_global;
  71. static void handler_call_die(const char *handler_name) NORETURN;
  72. static void handler_call_die(const char *handler_name)
  73. {
  74. PyErr_Print();
  75. Py_FatalError("problem in Python trace event handler");
  76. // Py_FatalError does not return
  77. // but we have to make the compiler happy
  78. abort();
  79. }
  80. /*
  81. * Insert val into into the dictionary and decrement the reference counter.
  82. * This is necessary for dictionaries since PyDict_SetItemString() does not
  83. * steal a reference, as opposed to PyTuple_SetItem().
  84. */
  85. static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
  86. {
  87. PyDict_SetItemString(dict, key, val);
  88. Py_DECREF(val);
  89. }
  90. static PyObject *get_handler(const char *handler_name)
  91. {
  92. PyObject *handler;
  93. handler = PyDict_GetItemString(main_dict, handler_name);
  94. if (handler && !PyCallable_Check(handler))
  95. return NULL;
  96. return handler;
  97. }
  98. static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
  99. {
  100. PyObject *retval;
  101. retval = PyObject_CallObject(handler, args);
  102. if (retval == NULL)
  103. handler_call_die(die_msg);
  104. Py_DECREF(retval);
  105. }
  106. static void try_call_object(const char *handler_name, PyObject *args)
  107. {
  108. PyObject *handler;
  109. handler = get_handler(handler_name);
  110. if (handler)
  111. call_object(handler, args, handler_name);
  112. }
  113. static void define_value(enum print_arg_type field_type,
  114. const char *ev_name,
  115. const char *field_name,
  116. const char *field_value,
  117. const char *field_str)
  118. {
  119. const char *handler_name = "define_flag_value";
  120. PyObject *t;
  121. unsigned long long value;
  122. unsigned n = 0;
  123. if (field_type == PRINT_SYMBOL)
  124. handler_name = "define_symbolic_value";
  125. t = PyTuple_New(4);
  126. if (!t)
  127. Py_FatalError("couldn't create Python tuple");
  128. value = eval_flag(field_value);
  129. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  130. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  131. PyTuple_SetItem(t, n++, PyInt_FromLong(value));
  132. PyTuple_SetItem(t, n++, PyString_FromString(field_str));
  133. try_call_object(handler_name, t);
  134. Py_DECREF(t);
  135. }
  136. static void define_values(enum print_arg_type field_type,
  137. struct print_flag_sym *field,
  138. const char *ev_name,
  139. const char *field_name)
  140. {
  141. define_value(field_type, ev_name, field_name, field->value,
  142. field->str);
  143. if (field->next)
  144. define_values(field_type, field->next, ev_name, field_name);
  145. }
  146. static void define_field(enum print_arg_type field_type,
  147. const char *ev_name,
  148. const char *field_name,
  149. const char *delim)
  150. {
  151. const char *handler_name = "define_flag_field";
  152. PyObject *t;
  153. unsigned n = 0;
  154. if (field_type == PRINT_SYMBOL)
  155. handler_name = "define_symbolic_field";
  156. if (field_type == PRINT_FLAGS)
  157. t = PyTuple_New(3);
  158. else
  159. t = PyTuple_New(2);
  160. if (!t)
  161. Py_FatalError("couldn't create Python tuple");
  162. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  163. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  164. if (field_type == PRINT_FLAGS)
  165. PyTuple_SetItem(t, n++, PyString_FromString(delim));
  166. try_call_object(handler_name, t);
  167. Py_DECREF(t);
  168. }
  169. static void define_event_symbols(struct event_format *event,
  170. const char *ev_name,
  171. struct print_arg *args)
  172. {
  173. if (args == NULL)
  174. return;
  175. switch (args->type) {
  176. case PRINT_NULL:
  177. break;
  178. case PRINT_ATOM:
  179. define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
  180. args->atom.atom);
  181. zero_flag_atom = 0;
  182. break;
  183. case PRINT_FIELD:
  184. free(cur_field_name);
  185. cur_field_name = strdup(args->field.name);
  186. break;
  187. case PRINT_FLAGS:
  188. define_event_symbols(event, ev_name, args->flags.field);
  189. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  190. args->flags.delim);
  191. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  192. cur_field_name);
  193. break;
  194. case PRINT_SYMBOL:
  195. define_event_symbols(event, ev_name, args->symbol.field);
  196. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  197. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  198. cur_field_name);
  199. break;
  200. case PRINT_HEX:
  201. define_event_symbols(event, ev_name, args->hex.field);
  202. define_event_symbols(event, ev_name, args->hex.size);
  203. break;
  204. case PRINT_INT_ARRAY:
  205. define_event_symbols(event, ev_name, args->int_array.field);
  206. define_event_symbols(event, ev_name, args->int_array.count);
  207. define_event_symbols(event, ev_name, args->int_array.el_size);
  208. break;
  209. case PRINT_STRING:
  210. break;
  211. case PRINT_TYPE:
  212. define_event_symbols(event, ev_name, args->typecast.item);
  213. break;
  214. case PRINT_OP:
  215. if (strcmp(args->op.op, ":") == 0)
  216. zero_flag_atom = 1;
  217. define_event_symbols(event, ev_name, args->op.left);
  218. define_event_symbols(event, ev_name, args->op.right);
  219. break;
  220. default:
  221. /* gcc warns for these? */
  222. case PRINT_BSTRING:
  223. case PRINT_DYNAMIC_ARRAY:
  224. case PRINT_DYNAMIC_ARRAY_LEN:
  225. case PRINT_FUNC:
  226. case PRINT_BITMASK:
  227. /* we should warn... */
  228. return;
  229. }
  230. if (args->next)
  231. define_event_symbols(event, ev_name, args->next);
  232. }
  233. static PyObject *get_field_numeric_entry(struct event_format *event,
  234. struct format_field *field, void *data)
  235. {
  236. bool is_array = field->flags & FIELD_IS_ARRAY;
  237. PyObject *obj, *list = NULL;
  238. unsigned long long val;
  239. unsigned int item_size, n_items, i;
  240. if (is_array) {
  241. list = PyList_New(field->arraylen);
  242. item_size = field->size / field->arraylen;
  243. n_items = field->arraylen;
  244. } else {
  245. item_size = field->size;
  246. n_items = 1;
  247. }
  248. for (i = 0; i < n_items; i++) {
  249. val = read_size(event, data + field->offset + i * item_size,
  250. item_size);
  251. if (field->flags & FIELD_IS_SIGNED) {
  252. if ((long long)val >= LONG_MIN &&
  253. (long long)val <= LONG_MAX)
  254. obj = PyInt_FromLong(val);
  255. else
  256. obj = PyLong_FromLongLong(val);
  257. } else {
  258. if (val <= LONG_MAX)
  259. obj = PyInt_FromLong(val);
  260. else
  261. obj = PyLong_FromUnsignedLongLong(val);
  262. }
  263. if (is_array)
  264. PyList_SET_ITEM(list, i, obj);
  265. }
  266. if (is_array)
  267. obj = list;
  268. return obj;
  269. }
  270. static PyObject *python_process_callchain(struct perf_sample *sample,
  271. struct perf_evsel *evsel,
  272. struct addr_location *al)
  273. {
  274. PyObject *pylist;
  275. pylist = PyList_New(0);
  276. if (!pylist)
  277. Py_FatalError("couldn't create Python list");
  278. if (!symbol_conf.use_callchain || !sample->callchain)
  279. goto exit;
  280. if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  281. sample, NULL, NULL,
  282. scripting_max_stack) != 0) {
  283. pr_err("Failed to resolve callchain. Skipping\n");
  284. goto exit;
  285. }
  286. callchain_cursor_commit(&callchain_cursor);
  287. while (1) {
  288. PyObject *pyelem;
  289. struct callchain_cursor_node *node;
  290. node = callchain_cursor_current(&callchain_cursor);
  291. if (!node)
  292. break;
  293. pyelem = PyDict_New();
  294. if (!pyelem)
  295. Py_FatalError("couldn't create Python dictionary");
  296. pydict_set_item_string_decref(pyelem, "ip",
  297. PyLong_FromUnsignedLongLong(node->ip));
  298. if (node->sym) {
  299. PyObject *pysym = PyDict_New();
  300. if (!pysym)
  301. Py_FatalError("couldn't create Python dictionary");
  302. pydict_set_item_string_decref(pysym, "start",
  303. PyLong_FromUnsignedLongLong(node->sym->start));
  304. pydict_set_item_string_decref(pysym, "end",
  305. PyLong_FromUnsignedLongLong(node->sym->end));
  306. pydict_set_item_string_decref(pysym, "binding",
  307. PyInt_FromLong(node->sym->binding));
  308. pydict_set_item_string_decref(pysym, "name",
  309. PyString_FromStringAndSize(node->sym->name,
  310. node->sym->namelen));
  311. pydict_set_item_string_decref(pyelem, "sym", pysym);
  312. }
  313. if (node->map) {
  314. struct map *map = node->map;
  315. const char *dsoname = "[unknown]";
  316. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  317. if (symbol_conf.show_kernel_path && map->dso->long_name)
  318. dsoname = map->dso->long_name;
  319. else if (map->dso->name)
  320. dsoname = map->dso->name;
  321. }
  322. pydict_set_item_string_decref(pyelem, "dso",
  323. PyString_FromString(dsoname));
  324. }
  325. callchain_cursor_advance(&callchain_cursor);
  326. PyList_Append(pylist, pyelem);
  327. Py_DECREF(pyelem);
  328. }
  329. exit:
  330. return pylist;
  331. }
  332. static void python_process_tracepoint(struct perf_sample *sample,
  333. struct perf_evsel *evsel,
  334. struct addr_location *al)
  335. {
  336. struct event_format *event = evsel->tp_format;
  337. PyObject *handler, *context, *t, *obj, *callchain;
  338. PyObject *dict = NULL;
  339. static char handler_name[256];
  340. struct format_field *field;
  341. unsigned long s, ns;
  342. unsigned n = 0;
  343. int pid;
  344. int cpu = sample->cpu;
  345. void *data = sample->raw_data;
  346. unsigned long long nsecs = sample->time;
  347. const char *comm = thread__comm_str(al->thread);
  348. t = PyTuple_New(MAX_FIELDS);
  349. if (!t)
  350. Py_FatalError("couldn't create Python tuple");
  351. if (!event)
  352. die("ug! no event found for type %d", (int)evsel->attr.config);
  353. pid = raw_field_value(event, "common_pid", data);
  354. sprintf(handler_name, "%s__%s", event->system, event->name);
  355. if (!test_and_set_bit(event->id, events_defined))
  356. define_event_symbols(event, handler_name, event->print_fmt.args);
  357. handler = get_handler(handler_name);
  358. if (!handler) {
  359. dict = PyDict_New();
  360. if (!dict)
  361. Py_FatalError("couldn't create Python dict");
  362. }
  363. s = nsecs / NSECS_PER_SEC;
  364. ns = nsecs - s * NSECS_PER_SEC;
  365. scripting_context->event_data = data;
  366. scripting_context->pevent = evsel->tp_format->pevent;
  367. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  368. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  369. PyTuple_SetItem(t, n++, context);
  370. /* ip unwinding */
  371. callchain = python_process_callchain(sample, evsel, al);
  372. if (handler) {
  373. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  374. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  375. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  376. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  377. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  378. PyTuple_SetItem(t, n++, callchain);
  379. } else {
  380. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  381. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  382. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  383. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  384. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  385. pydict_set_item_string_decref(dict, "common_callchain", callchain);
  386. }
  387. for (field = event->format.fields; field; field = field->next) {
  388. if (field->flags & FIELD_IS_STRING) {
  389. int offset;
  390. if (field->flags & FIELD_IS_DYNAMIC) {
  391. offset = *(int *)(data + field->offset);
  392. offset &= 0xffff;
  393. } else
  394. offset = field->offset;
  395. obj = PyString_FromString((char *)data + offset);
  396. } else { /* FIELD_IS_NUMERIC */
  397. obj = get_field_numeric_entry(event, field, data);
  398. }
  399. if (handler)
  400. PyTuple_SetItem(t, n++, obj);
  401. else
  402. pydict_set_item_string_decref(dict, field->name, obj);
  403. }
  404. if (!handler)
  405. PyTuple_SetItem(t, n++, dict);
  406. if (_PyTuple_Resize(&t, n) == -1)
  407. Py_FatalError("error resizing Python tuple");
  408. if (handler) {
  409. call_object(handler, t, handler_name);
  410. } else {
  411. try_call_object("trace_unhandled", t);
  412. Py_DECREF(dict);
  413. }
  414. Py_DECREF(t);
  415. }
  416. static PyObject *tuple_new(unsigned int sz)
  417. {
  418. PyObject *t;
  419. t = PyTuple_New(sz);
  420. if (!t)
  421. Py_FatalError("couldn't create Python tuple");
  422. return t;
  423. }
  424. static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
  425. {
  426. #if BITS_PER_LONG == 64
  427. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  428. #endif
  429. #if BITS_PER_LONG == 32
  430. return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
  431. #endif
  432. }
  433. static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
  434. {
  435. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  436. }
  437. static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
  438. {
  439. return PyTuple_SetItem(t, pos, PyString_FromString(s));
  440. }
  441. static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
  442. {
  443. struct tables *tables = container_of(dbe, struct tables, dbe);
  444. PyObject *t;
  445. t = tuple_new(2);
  446. tuple_set_u64(t, 0, evsel->db_id);
  447. tuple_set_string(t, 1, perf_evsel__name(evsel));
  448. call_object(tables->evsel_handler, t, "evsel_table");
  449. Py_DECREF(t);
  450. return 0;
  451. }
  452. static int python_export_machine(struct db_export *dbe,
  453. struct machine *machine)
  454. {
  455. struct tables *tables = container_of(dbe, struct tables, dbe);
  456. PyObject *t;
  457. t = tuple_new(3);
  458. tuple_set_u64(t, 0, machine->db_id);
  459. tuple_set_s32(t, 1, machine->pid);
  460. tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
  461. call_object(tables->machine_handler, t, "machine_table");
  462. Py_DECREF(t);
  463. return 0;
  464. }
  465. static int python_export_thread(struct db_export *dbe, struct thread *thread,
  466. u64 main_thread_db_id, struct machine *machine)
  467. {
  468. struct tables *tables = container_of(dbe, struct tables, dbe);
  469. PyObject *t;
  470. t = tuple_new(5);
  471. tuple_set_u64(t, 0, thread->db_id);
  472. tuple_set_u64(t, 1, machine->db_id);
  473. tuple_set_u64(t, 2, main_thread_db_id);
  474. tuple_set_s32(t, 3, thread->pid_);
  475. tuple_set_s32(t, 4, thread->tid);
  476. call_object(tables->thread_handler, t, "thread_table");
  477. Py_DECREF(t);
  478. return 0;
  479. }
  480. static int python_export_comm(struct db_export *dbe, struct comm *comm)
  481. {
  482. struct tables *tables = container_of(dbe, struct tables, dbe);
  483. PyObject *t;
  484. t = tuple_new(2);
  485. tuple_set_u64(t, 0, comm->db_id);
  486. tuple_set_string(t, 1, comm__str(comm));
  487. call_object(tables->comm_handler, t, "comm_table");
  488. Py_DECREF(t);
  489. return 0;
  490. }
  491. static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
  492. struct comm *comm, struct thread *thread)
  493. {
  494. struct tables *tables = container_of(dbe, struct tables, dbe);
  495. PyObject *t;
  496. t = tuple_new(3);
  497. tuple_set_u64(t, 0, db_id);
  498. tuple_set_u64(t, 1, comm->db_id);
  499. tuple_set_u64(t, 2, thread->db_id);
  500. call_object(tables->comm_thread_handler, t, "comm_thread_table");
  501. Py_DECREF(t);
  502. return 0;
  503. }
  504. static int python_export_dso(struct db_export *dbe, struct dso *dso,
  505. struct machine *machine)
  506. {
  507. struct tables *tables = container_of(dbe, struct tables, dbe);
  508. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  509. PyObject *t;
  510. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  511. t = tuple_new(5);
  512. tuple_set_u64(t, 0, dso->db_id);
  513. tuple_set_u64(t, 1, machine->db_id);
  514. tuple_set_string(t, 2, dso->short_name);
  515. tuple_set_string(t, 3, dso->long_name);
  516. tuple_set_string(t, 4, sbuild_id);
  517. call_object(tables->dso_handler, t, "dso_table");
  518. Py_DECREF(t);
  519. return 0;
  520. }
  521. static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
  522. struct dso *dso)
  523. {
  524. struct tables *tables = container_of(dbe, struct tables, dbe);
  525. u64 *sym_db_id = symbol__priv(sym);
  526. PyObject *t;
  527. t = tuple_new(6);
  528. tuple_set_u64(t, 0, *sym_db_id);
  529. tuple_set_u64(t, 1, dso->db_id);
  530. tuple_set_u64(t, 2, sym->start);
  531. tuple_set_u64(t, 3, sym->end);
  532. tuple_set_s32(t, 4, sym->binding);
  533. tuple_set_string(t, 5, sym->name);
  534. call_object(tables->symbol_handler, t, "symbol_table");
  535. Py_DECREF(t);
  536. return 0;
  537. }
  538. static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
  539. const char *name)
  540. {
  541. struct tables *tables = container_of(dbe, struct tables, dbe);
  542. PyObject *t;
  543. t = tuple_new(2);
  544. tuple_set_s32(t, 0, branch_type);
  545. tuple_set_string(t, 1, name);
  546. call_object(tables->branch_type_handler, t, "branch_type_table");
  547. Py_DECREF(t);
  548. return 0;
  549. }
  550. static int python_export_sample(struct db_export *dbe,
  551. struct export_sample *es)
  552. {
  553. struct tables *tables = container_of(dbe, struct tables, dbe);
  554. PyObject *t;
  555. t = tuple_new(22);
  556. tuple_set_u64(t, 0, es->db_id);
  557. tuple_set_u64(t, 1, es->evsel->db_id);
  558. tuple_set_u64(t, 2, es->al->machine->db_id);
  559. tuple_set_u64(t, 3, es->al->thread->db_id);
  560. tuple_set_u64(t, 4, es->comm_db_id);
  561. tuple_set_u64(t, 5, es->dso_db_id);
  562. tuple_set_u64(t, 6, es->sym_db_id);
  563. tuple_set_u64(t, 7, es->offset);
  564. tuple_set_u64(t, 8, es->sample->ip);
  565. tuple_set_u64(t, 9, es->sample->time);
  566. tuple_set_s32(t, 10, es->sample->cpu);
  567. tuple_set_u64(t, 11, es->addr_dso_db_id);
  568. tuple_set_u64(t, 12, es->addr_sym_db_id);
  569. tuple_set_u64(t, 13, es->addr_offset);
  570. tuple_set_u64(t, 14, es->sample->addr);
  571. tuple_set_u64(t, 15, es->sample->period);
  572. tuple_set_u64(t, 16, es->sample->weight);
  573. tuple_set_u64(t, 17, es->sample->transaction);
  574. tuple_set_u64(t, 18, es->sample->data_src);
  575. tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
  576. tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
  577. tuple_set_u64(t, 21, es->call_path_id);
  578. call_object(tables->sample_handler, t, "sample_table");
  579. Py_DECREF(t);
  580. return 0;
  581. }
  582. static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
  583. {
  584. struct tables *tables = container_of(dbe, struct tables, dbe);
  585. PyObject *t;
  586. u64 parent_db_id, sym_db_id;
  587. parent_db_id = cp->parent ? cp->parent->db_id : 0;
  588. sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
  589. t = tuple_new(4);
  590. tuple_set_u64(t, 0, cp->db_id);
  591. tuple_set_u64(t, 1, parent_db_id);
  592. tuple_set_u64(t, 2, sym_db_id);
  593. tuple_set_u64(t, 3, cp->ip);
  594. call_object(tables->call_path_handler, t, "call_path_table");
  595. Py_DECREF(t);
  596. return 0;
  597. }
  598. static int python_export_call_return(struct db_export *dbe,
  599. struct call_return *cr)
  600. {
  601. struct tables *tables = container_of(dbe, struct tables, dbe);
  602. u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
  603. PyObject *t;
  604. t = tuple_new(11);
  605. tuple_set_u64(t, 0, cr->db_id);
  606. tuple_set_u64(t, 1, cr->thread->db_id);
  607. tuple_set_u64(t, 2, comm_db_id);
  608. tuple_set_u64(t, 3, cr->cp->db_id);
  609. tuple_set_u64(t, 4, cr->call_time);
  610. tuple_set_u64(t, 5, cr->return_time);
  611. tuple_set_u64(t, 6, cr->branch_count);
  612. tuple_set_u64(t, 7, cr->call_ref);
  613. tuple_set_u64(t, 8, cr->return_ref);
  614. tuple_set_u64(t, 9, cr->cp->parent->db_id);
  615. tuple_set_s32(t, 10, cr->flags);
  616. call_object(tables->call_return_handler, t, "call_return_table");
  617. Py_DECREF(t);
  618. return 0;
  619. }
  620. static int python_process_call_return(struct call_return *cr, void *data)
  621. {
  622. struct db_export *dbe = data;
  623. return db_export__call_return(dbe, cr);
  624. }
  625. static void python_process_general_event(struct perf_sample *sample,
  626. struct perf_evsel *evsel,
  627. struct addr_location *al)
  628. {
  629. PyObject *handler, *t, *dict, *callchain, *dict_sample;
  630. static char handler_name[64];
  631. unsigned n = 0;
  632. /*
  633. * Use the MAX_FIELDS to make the function expandable, though
  634. * currently there is only one item for the tuple.
  635. */
  636. t = PyTuple_New(MAX_FIELDS);
  637. if (!t)
  638. Py_FatalError("couldn't create Python tuple");
  639. dict = PyDict_New();
  640. if (!dict)
  641. Py_FatalError("couldn't create Python dictionary");
  642. dict_sample = PyDict_New();
  643. if (!dict_sample)
  644. Py_FatalError("couldn't create Python dictionary");
  645. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  646. handler = get_handler(handler_name);
  647. if (!handler)
  648. goto exit;
  649. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  650. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  651. (const char *)&evsel->attr, sizeof(evsel->attr)));
  652. pydict_set_item_string_decref(dict_sample, "pid",
  653. PyInt_FromLong(sample->pid));
  654. pydict_set_item_string_decref(dict_sample, "tid",
  655. PyInt_FromLong(sample->tid));
  656. pydict_set_item_string_decref(dict_sample, "cpu",
  657. PyInt_FromLong(sample->cpu));
  658. pydict_set_item_string_decref(dict_sample, "ip",
  659. PyLong_FromUnsignedLongLong(sample->ip));
  660. pydict_set_item_string_decref(dict_sample, "time",
  661. PyLong_FromUnsignedLongLong(sample->time));
  662. pydict_set_item_string_decref(dict_sample, "period",
  663. PyLong_FromUnsignedLongLong(sample->period));
  664. pydict_set_item_string_decref(dict, "sample", dict_sample);
  665. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  666. (const char *)sample->raw_data, sample->raw_size));
  667. pydict_set_item_string_decref(dict, "comm",
  668. PyString_FromString(thread__comm_str(al->thread)));
  669. if (al->map) {
  670. pydict_set_item_string_decref(dict, "dso",
  671. PyString_FromString(al->map->dso->name));
  672. }
  673. if (al->sym) {
  674. pydict_set_item_string_decref(dict, "symbol",
  675. PyString_FromString(al->sym->name));
  676. }
  677. /* ip unwinding */
  678. callchain = python_process_callchain(sample, evsel, al);
  679. pydict_set_item_string_decref(dict, "callchain", callchain);
  680. PyTuple_SetItem(t, n++, dict);
  681. if (_PyTuple_Resize(&t, n) == -1)
  682. Py_FatalError("error resizing Python tuple");
  683. call_object(handler, t, handler_name);
  684. exit:
  685. Py_DECREF(dict);
  686. Py_DECREF(t);
  687. }
  688. static void python_process_event(union perf_event *event,
  689. struct perf_sample *sample,
  690. struct perf_evsel *evsel,
  691. struct addr_location *al)
  692. {
  693. struct tables *tables = &tables_global;
  694. switch (evsel->attr.type) {
  695. case PERF_TYPE_TRACEPOINT:
  696. python_process_tracepoint(sample, evsel, al);
  697. break;
  698. /* Reserve for future process_hw/sw/raw APIs */
  699. default:
  700. if (tables->db_export_mode)
  701. db_export__sample(&tables->dbe, event, sample, evsel, al);
  702. else
  703. python_process_general_event(sample, evsel, al);
  704. }
  705. }
  706. static void get_handler_name(char *str, size_t size,
  707. struct perf_evsel *evsel)
  708. {
  709. char *p = str;
  710. scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
  711. while ((p = strchr(p, ':'))) {
  712. *p = '_';
  713. p++;
  714. }
  715. }
  716. static void
  717. process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
  718. struct perf_counts_values *count)
  719. {
  720. PyObject *handler, *t;
  721. static char handler_name[256];
  722. int n = 0;
  723. t = PyTuple_New(MAX_FIELDS);
  724. if (!t)
  725. Py_FatalError("couldn't create Python tuple");
  726. get_handler_name(handler_name, sizeof(handler_name),
  727. counter);
  728. handler = get_handler(handler_name);
  729. if (!handler) {
  730. pr_debug("can't find python handler %s\n", handler_name);
  731. return;
  732. }
  733. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  734. PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
  735. tuple_set_u64(t, n++, tstamp);
  736. tuple_set_u64(t, n++, count->val);
  737. tuple_set_u64(t, n++, count->ena);
  738. tuple_set_u64(t, n++, count->run);
  739. if (_PyTuple_Resize(&t, n) == -1)
  740. Py_FatalError("error resizing Python tuple");
  741. call_object(handler, t, handler_name);
  742. Py_DECREF(t);
  743. }
  744. static void python_process_stat(struct perf_stat_config *config,
  745. struct perf_evsel *counter, u64 tstamp)
  746. {
  747. struct thread_map *threads = counter->threads;
  748. struct cpu_map *cpus = counter->cpus;
  749. int cpu, thread;
  750. if (config->aggr_mode == AGGR_GLOBAL) {
  751. process_stat(counter, -1, -1, tstamp,
  752. &counter->counts->aggr);
  753. return;
  754. }
  755. for (thread = 0; thread < threads->nr; thread++) {
  756. for (cpu = 0; cpu < cpus->nr; cpu++) {
  757. process_stat(counter, cpus->map[cpu],
  758. thread_map__pid(threads, thread), tstamp,
  759. perf_counts(counter->counts, cpu, thread));
  760. }
  761. }
  762. }
  763. static void python_process_stat_interval(u64 tstamp)
  764. {
  765. PyObject *handler, *t;
  766. static const char handler_name[] = "stat__interval";
  767. int n = 0;
  768. t = PyTuple_New(MAX_FIELDS);
  769. if (!t)
  770. Py_FatalError("couldn't create Python tuple");
  771. handler = get_handler(handler_name);
  772. if (!handler) {
  773. pr_debug("can't find python handler %s\n", handler_name);
  774. return;
  775. }
  776. tuple_set_u64(t, n++, tstamp);
  777. if (_PyTuple_Resize(&t, n) == -1)
  778. Py_FatalError("error resizing Python tuple");
  779. call_object(handler, t, handler_name);
  780. Py_DECREF(t);
  781. }
  782. static int run_start_sub(void)
  783. {
  784. main_module = PyImport_AddModule("__main__");
  785. if (main_module == NULL)
  786. return -1;
  787. Py_INCREF(main_module);
  788. main_dict = PyModule_GetDict(main_module);
  789. if (main_dict == NULL)
  790. goto error;
  791. Py_INCREF(main_dict);
  792. try_call_object("trace_begin", NULL);
  793. return 0;
  794. error:
  795. Py_XDECREF(main_dict);
  796. Py_XDECREF(main_module);
  797. return -1;
  798. }
  799. #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
  800. tables->handler_name = get_handler(#table_name); \
  801. if (tables->handler_name) \
  802. tables->dbe.export_ ## name = python_export_ ## name; \
  803. } while (0)
  804. #define SET_TABLE_HANDLER(name) \
  805. SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
  806. static void set_table_handlers(struct tables *tables)
  807. {
  808. const char *perf_db_export_mode = "perf_db_export_mode";
  809. const char *perf_db_export_calls = "perf_db_export_calls";
  810. const char *perf_db_export_callchains = "perf_db_export_callchains";
  811. PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
  812. bool export_calls = false;
  813. bool export_callchains = false;
  814. int ret;
  815. memset(tables, 0, sizeof(struct tables));
  816. if (db_export__init(&tables->dbe))
  817. Py_FatalError("failed to initialize export");
  818. db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
  819. if (!db_export_mode)
  820. return;
  821. ret = PyObject_IsTrue(db_export_mode);
  822. if (ret == -1)
  823. handler_call_die(perf_db_export_mode);
  824. if (!ret)
  825. return;
  826. /* handle export calls */
  827. tables->dbe.crp = NULL;
  828. db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
  829. if (db_export_calls) {
  830. ret = PyObject_IsTrue(db_export_calls);
  831. if (ret == -1)
  832. handler_call_die(perf_db_export_calls);
  833. export_calls = !!ret;
  834. }
  835. if (export_calls) {
  836. tables->dbe.crp =
  837. call_return_processor__new(python_process_call_return,
  838. &tables->dbe);
  839. if (!tables->dbe.crp)
  840. Py_FatalError("failed to create calls processor");
  841. }
  842. /* handle export callchains */
  843. tables->dbe.cpr = NULL;
  844. db_export_callchains = PyDict_GetItemString(main_dict,
  845. perf_db_export_callchains);
  846. if (db_export_callchains) {
  847. ret = PyObject_IsTrue(db_export_callchains);
  848. if (ret == -1)
  849. handler_call_die(perf_db_export_callchains);
  850. export_callchains = !!ret;
  851. }
  852. if (export_callchains) {
  853. /*
  854. * Attempt to use the call path root from the call return
  855. * processor, if the call return processor is in use. Otherwise,
  856. * we allocate a new call path root. This prevents exporting
  857. * duplicate call path ids when both are in use simultaniously.
  858. */
  859. if (tables->dbe.crp)
  860. tables->dbe.cpr = tables->dbe.crp->cpr;
  861. else
  862. tables->dbe.cpr = call_path_root__new();
  863. if (!tables->dbe.cpr)
  864. Py_FatalError("failed to create call path root");
  865. }
  866. tables->db_export_mode = true;
  867. /*
  868. * Reserve per symbol space for symbol->db_id via symbol__priv()
  869. */
  870. symbol_conf.priv_size = sizeof(u64);
  871. SET_TABLE_HANDLER(evsel);
  872. SET_TABLE_HANDLER(machine);
  873. SET_TABLE_HANDLER(thread);
  874. SET_TABLE_HANDLER(comm);
  875. SET_TABLE_HANDLER(comm_thread);
  876. SET_TABLE_HANDLER(dso);
  877. SET_TABLE_HANDLER(symbol);
  878. SET_TABLE_HANDLER(branch_type);
  879. SET_TABLE_HANDLER(sample);
  880. SET_TABLE_HANDLER(call_path);
  881. SET_TABLE_HANDLER(call_return);
  882. }
  883. /*
  884. * Start trace script
  885. */
  886. static int python_start_script(const char *script, int argc, const char **argv)
  887. {
  888. struct tables *tables = &tables_global;
  889. const char **command_line;
  890. char buf[PATH_MAX];
  891. int i, err = 0;
  892. FILE *fp;
  893. command_line = malloc((argc + 1) * sizeof(const char *));
  894. command_line[0] = script;
  895. for (i = 1; i < argc + 1; i++)
  896. command_line[i] = argv[i - 1];
  897. Py_Initialize();
  898. initperf_trace_context();
  899. PySys_SetArgv(argc + 1, (char **)command_line);
  900. fp = fopen(script, "r");
  901. if (!fp) {
  902. sprintf(buf, "Can't open python script \"%s\"", script);
  903. perror(buf);
  904. err = -1;
  905. goto error;
  906. }
  907. err = PyRun_SimpleFile(fp, script);
  908. if (err) {
  909. fprintf(stderr, "Error running python script %s\n", script);
  910. goto error;
  911. }
  912. err = run_start_sub();
  913. if (err) {
  914. fprintf(stderr, "Error starting python script %s\n", script);
  915. goto error;
  916. }
  917. set_table_handlers(tables);
  918. if (tables->db_export_mode) {
  919. err = db_export__branch_types(&tables->dbe);
  920. if (err)
  921. goto error;
  922. }
  923. free(command_line);
  924. return err;
  925. error:
  926. Py_Finalize();
  927. free(command_line);
  928. return err;
  929. }
  930. static int python_flush_script(void)
  931. {
  932. struct tables *tables = &tables_global;
  933. return db_export__flush(&tables->dbe);
  934. }
  935. /*
  936. * Stop trace script
  937. */
  938. static int python_stop_script(void)
  939. {
  940. struct tables *tables = &tables_global;
  941. try_call_object("trace_end", NULL);
  942. db_export__exit(&tables->dbe);
  943. Py_XDECREF(main_dict);
  944. Py_XDECREF(main_module);
  945. Py_Finalize();
  946. return 0;
  947. }
  948. static int python_generate_script(struct pevent *pevent, const char *outfile)
  949. {
  950. struct event_format *event = NULL;
  951. struct format_field *f;
  952. char fname[PATH_MAX];
  953. int not_first, count;
  954. FILE *ofp;
  955. sprintf(fname, "%s.py", outfile);
  956. ofp = fopen(fname, "w");
  957. if (ofp == NULL) {
  958. fprintf(stderr, "couldn't open %s\n", fname);
  959. return -1;
  960. }
  961. fprintf(ofp, "# perf script event handlers, "
  962. "generated by perf script -g python\n");
  963. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  964. " License version 2\n\n");
  965. fprintf(ofp, "# The common_* event handler fields are the most useful "
  966. "fields common to\n");
  967. fprintf(ofp, "# all events. They don't necessarily correspond to "
  968. "the 'common_*' fields\n");
  969. fprintf(ofp, "# in the format files. Those fields not available as "
  970. "handler params can\n");
  971. fprintf(ofp, "# be retrieved using Python functions of the form "
  972. "common_*(context).\n");
  973. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  974. "of available functions.\n\n");
  975. fprintf(ofp, "import os\n");
  976. fprintf(ofp, "import sys\n\n");
  977. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  978. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  979. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  980. fprintf(ofp, "from Core import *\n\n\n");
  981. fprintf(ofp, "def trace_begin():\n");
  982. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  983. fprintf(ofp, "def trace_end():\n");
  984. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  985. while ((event = trace_find_next_event(pevent, event))) {
  986. fprintf(ofp, "def %s__%s(", event->system, event->name);
  987. fprintf(ofp, "event_name, ");
  988. fprintf(ofp, "context, ");
  989. fprintf(ofp, "common_cpu,\n");
  990. fprintf(ofp, "\tcommon_secs, ");
  991. fprintf(ofp, "common_nsecs, ");
  992. fprintf(ofp, "common_pid, ");
  993. fprintf(ofp, "common_comm,\n\t");
  994. fprintf(ofp, "common_callchain, ");
  995. not_first = 0;
  996. count = 0;
  997. for (f = event->format.fields; f; f = f->next) {
  998. if (not_first++)
  999. fprintf(ofp, ", ");
  1000. if (++count % 5 == 0)
  1001. fprintf(ofp, "\n\t");
  1002. fprintf(ofp, "%s", f->name);
  1003. }
  1004. fprintf(ofp, "):\n");
  1005. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  1006. "common_secs, common_nsecs,\n\t\t\t"
  1007. "common_pid, common_comm)\n\n");
  1008. fprintf(ofp, "\t\tprint \"");
  1009. not_first = 0;
  1010. count = 0;
  1011. for (f = event->format.fields; f; f = f->next) {
  1012. if (not_first++)
  1013. fprintf(ofp, ", ");
  1014. if (count && count % 3 == 0) {
  1015. fprintf(ofp, "\" \\\n\t\t\"");
  1016. }
  1017. count++;
  1018. fprintf(ofp, "%s=", f->name);
  1019. if (f->flags & FIELD_IS_STRING ||
  1020. f->flags & FIELD_IS_FLAG ||
  1021. f->flags & FIELD_IS_ARRAY ||
  1022. f->flags & FIELD_IS_SYMBOLIC)
  1023. fprintf(ofp, "%%s");
  1024. else if (f->flags & FIELD_IS_SIGNED)
  1025. fprintf(ofp, "%%d");
  1026. else
  1027. fprintf(ofp, "%%u");
  1028. }
  1029. fprintf(ofp, "\" %% \\\n\t\t(");
  1030. not_first = 0;
  1031. count = 0;
  1032. for (f = event->format.fields; f; f = f->next) {
  1033. if (not_first++)
  1034. fprintf(ofp, ", ");
  1035. if (++count % 5 == 0)
  1036. fprintf(ofp, "\n\t\t");
  1037. if (f->flags & FIELD_IS_FLAG) {
  1038. if ((count - 1) % 5 != 0) {
  1039. fprintf(ofp, "\n\t\t");
  1040. count = 4;
  1041. }
  1042. fprintf(ofp, "flag_str(\"");
  1043. fprintf(ofp, "%s__%s\", ", event->system,
  1044. event->name);
  1045. fprintf(ofp, "\"%s\", %s)", f->name,
  1046. f->name);
  1047. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  1048. if ((count - 1) % 5 != 0) {
  1049. fprintf(ofp, "\n\t\t");
  1050. count = 4;
  1051. }
  1052. fprintf(ofp, "symbol_str(\"");
  1053. fprintf(ofp, "%s__%s\", ", event->system,
  1054. event->name);
  1055. fprintf(ofp, "\"%s\", %s)", f->name,
  1056. f->name);
  1057. } else
  1058. fprintf(ofp, "%s", f->name);
  1059. }
  1060. fprintf(ofp, ")\n\n");
  1061. fprintf(ofp, "\t\tfor node in common_callchain:");
  1062. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  1063. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  1064. fprintf(ofp, "\n\t\t\telse:");
  1065. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  1066. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  1067. }
  1068. fprintf(ofp, "def trace_unhandled(event_name, context, "
  1069. "event_fields_dict):\n");
  1070. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  1071. "for k,v in sorted(event_fields_dict.items())])\n\n");
  1072. fprintf(ofp, "def print_header("
  1073. "event_name, cpu, secs, nsecs, pid, comm):\n"
  1074. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  1075. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  1076. fclose(ofp);
  1077. fprintf(stderr, "generated Python script: %s\n", fname);
  1078. return 0;
  1079. }
  1080. struct scripting_ops python_scripting_ops = {
  1081. .name = "Python",
  1082. .start_script = python_start_script,
  1083. .flush_script = python_flush_script,
  1084. .stop_script = python_stop_script,
  1085. .process_event = python_process_event,
  1086. .process_stat = python_process_stat,
  1087. .process_stat_interval = python_process_stat_interval,
  1088. .generate_script = python_generate_script,
  1089. };