trace-event-python.c 34 KB

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