trace-event-python.c 34 KB

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