trace-event-python.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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 = NULL, *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 int is_printable_array(char *p, unsigned int len)
  333. {
  334. unsigned int i;
  335. if (!p || !len || p[len - 1] != 0)
  336. return 0;
  337. len--;
  338. for (i = 0; i < len; i++) {
  339. if (!isprint(p[i]) && !isspace(p[i]))
  340. return 0;
  341. }
  342. return 1;
  343. }
  344. static void python_process_tracepoint(struct perf_sample *sample,
  345. struct perf_evsel *evsel,
  346. struct addr_location *al)
  347. {
  348. struct event_format *event = evsel->tp_format;
  349. PyObject *handler, *context, *t, *obj = NULL, *callchain;
  350. PyObject *dict = NULL;
  351. static char handler_name[256];
  352. struct format_field *field;
  353. unsigned long s, ns;
  354. unsigned n = 0;
  355. int pid;
  356. int cpu = sample->cpu;
  357. void *data = sample->raw_data;
  358. unsigned long long nsecs = sample->time;
  359. const char *comm = thread__comm_str(al->thread);
  360. t = PyTuple_New(MAX_FIELDS);
  361. if (!t)
  362. Py_FatalError("couldn't create Python tuple");
  363. if (!event) {
  364. snprintf(handler_name, sizeof(handler_name),
  365. "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
  366. Py_FatalError(handler_name);
  367. }
  368. pid = raw_field_value(event, "common_pid", data);
  369. sprintf(handler_name, "%s__%s", event->system, event->name);
  370. if (!test_and_set_bit(event->id, events_defined))
  371. define_event_symbols(event, handler_name, event->print_fmt.args);
  372. handler = get_handler(handler_name);
  373. if (!handler) {
  374. dict = PyDict_New();
  375. if (!dict)
  376. Py_FatalError("couldn't create Python dict");
  377. }
  378. s = nsecs / NSECS_PER_SEC;
  379. ns = nsecs - s * NSECS_PER_SEC;
  380. scripting_context->event_data = data;
  381. scripting_context->pevent = evsel->tp_format->pevent;
  382. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  383. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  384. PyTuple_SetItem(t, n++, context);
  385. /* ip unwinding */
  386. callchain = python_process_callchain(sample, evsel, al);
  387. if (handler) {
  388. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  389. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  390. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  391. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  392. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  393. PyTuple_SetItem(t, n++, callchain);
  394. } else {
  395. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  396. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  397. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  398. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  399. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  400. pydict_set_item_string_decref(dict, "common_callchain", callchain);
  401. }
  402. for (field = event->format.fields; field; field = field->next) {
  403. unsigned int offset, len;
  404. unsigned long long val;
  405. if (field->flags & FIELD_IS_ARRAY) {
  406. offset = field->offset;
  407. len = field->size;
  408. if (field->flags & FIELD_IS_DYNAMIC) {
  409. val = pevent_read_number(scripting_context->pevent,
  410. data + offset, len);
  411. offset = val;
  412. len = offset >> 16;
  413. offset &= 0xffff;
  414. }
  415. if (field->flags & FIELD_IS_STRING &&
  416. is_printable_array(data + offset, len)) {
  417. obj = PyString_FromString((char *) data + offset);
  418. } else {
  419. obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
  420. field->flags &= ~FIELD_IS_STRING;
  421. }
  422. } else { /* FIELD_IS_NUMERIC */
  423. obj = get_field_numeric_entry(event, field, data);
  424. }
  425. if (handler)
  426. PyTuple_SetItem(t, n++, obj);
  427. else
  428. pydict_set_item_string_decref(dict, field->name, obj);
  429. }
  430. if (!handler)
  431. PyTuple_SetItem(t, n++, dict);
  432. if (_PyTuple_Resize(&t, n) == -1)
  433. Py_FatalError("error resizing Python tuple");
  434. if (handler) {
  435. call_object(handler, t, handler_name);
  436. } else {
  437. try_call_object("trace_unhandled", t);
  438. Py_DECREF(dict);
  439. }
  440. Py_DECREF(t);
  441. }
  442. static PyObject *tuple_new(unsigned int sz)
  443. {
  444. PyObject *t;
  445. t = PyTuple_New(sz);
  446. if (!t)
  447. Py_FatalError("couldn't create Python tuple");
  448. return t;
  449. }
  450. static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
  451. {
  452. #if BITS_PER_LONG == 64
  453. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  454. #endif
  455. #if BITS_PER_LONG == 32
  456. return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
  457. #endif
  458. }
  459. static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
  460. {
  461. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  462. }
  463. static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
  464. {
  465. return PyTuple_SetItem(t, pos, PyString_FromString(s));
  466. }
  467. static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
  468. {
  469. struct tables *tables = container_of(dbe, struct tables, dbe);
  470. PyObject *t;
  471. t = tuple_new(2);
  472. tuple_set_u64(t, 0, evsel->db_id);
  473. tuple_set_string(t, 1, perf_evsel__name(evsel));
  474. call_object(tables->evsel_handler, t, "evsel_table");
  475. Py_DECREF(t);
  476. return 0;
  477. }
  478. static int python_export_machine(struct db_export *dbe,
  479. struct machine *machine)
  480. {
  481. struct tables *tables = container_of(dbe, struct tables, dbe);
  482. PyObject *t;
  483. t = tuple_new(3);
  484. tuple_set_u64(t, 0, machine->db_id);
  485. tuple_set_s32(t, 1, machine->pid);
  486. tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
  487. call_object(tables->machine_handler, t, "machine_table");
  488. Py_DECREF(t);
  489. return 0;
  490. }
  491. static int python_export_thread(struct db_export *dbe, struct thread *thread,
  492. u64 main_thread_db_id, struct machine *machine)
  493. {
  494. struct tables *tables = container_of(dbe, struct tables, dbe);
  495. PyObject *t;
  496. t = tuple_new(5);
  497. tuple_set_u64(t, 0, thread->db_id);
  498. tuple_set_u64(t, 1, machine->db_id);
  499. tuple_set_u64(t, 2, main_thread_db_id);
  500. tuple_set_s32(t, 3, thread->pid_);
  501. tuple_set_s32(t, 4, thread->tid);
  502. call_object(tables->thread_handler, t, "thread_table");
  503. Py_DECREF(t);
  504. return 0;
  505. }
  506. static int python_export_comm(struct db_export *dbe, struct comm *comm)
  507. {
  508. struct tables *tables = container_of(dbe, struct tables, dbe);
  509. PyObject *t;
  510. t = tuple_new(2);
  511. tuple_set_u64(t, 0, comm->db_id);
  512. tuple_set_string(t, 1, comm__str(comm));
  513. call_object(tables->comm_handler, t, "comm_table");
  514. Py_DECREF(t);
  515. return 0;
  516. }
  517. static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
  518. struct comm *comm, struct thread *thread)
  519. {
  520. struct tables *tables = container_of(dbe, struct tables, dbe);
  521. PyObject *t;
  522. t = tuple_new(3);
  523. tuple_set_u64(t, 0, db_id);
  524. tuple_set_u64(t, 1, comm->db_id);
  525. tuple_set_u64(t, 2, thread->db_id);
  526. call_object(tables->comm_thread_handler, t, "comm_thread_table");
  527. Py_DECREF(t);
  528. return 0;
  529. }
  530. static int python_export_dso(struct db_export *dbe, struct dso *dso,
  531. struct machine *machine)
  532. {
  533. struct tables *tables = container_of(dbe, struct tables, dbe);
  534. char sbuild_id[SBUILD_ID_SIZE];
  535. PyObject *t;
  536. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  537. t = tuple_new(5);
  538. tuple_set_u64(t, 0, dso->db_id);
  539. tuple_set_u64(t, 1, machine->db_id);
  540. tuple_set_string(t, 2, dso->short_name);
  541. tuple_set_string(t, 3, dso->long_name);
  542. tuple_set_string(t, 4, sbuild_id);
  543. call_object(tables->dso_handler, t, "dso_table");
  544. Py_DECREF(t);
  545. return 0;
  546. }
  547. static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
  548. struct dso *dso)
  549. {
  550. struct tables *tables = container_of(dbe, struct tables, dbe);
  551. u64 *sym_db_id = symbol__priv(sym);
  552. PyObject *t;
  553. t = tuple_new(6);
  554. tuple_set_u64(t, 0, *sym_db_id);
  555. tuple_set_u64(t, 1, dso->db_id);
  556. tuple_set_u64(t, 2, sym->start);
  557. tuple_set_u64(t, 3, sym->end);
  558. tuple_set_s32(t, 4, sym->binding);
  559. tuple_set_string(t, 5, sym->name);
  560. call_object(tables->symbol_handler, t, "symbol_table");
  561. Py_DECREF(t);
  562. return 0;
  563. }
  564. static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
  565. const char *name)
  566. {
  567. struct tables *tables = container_of(dbe, struct tables, dbe);
  568. PyObject *t;
  569. t = tuple_new(2);
  570. tuple_set_s32(t, 0, branch_type);
  571. tuple_set_string(t, 1, name);
  572. call_object(tables->branch_type_handler, t, "branch_type_table");
  573. Py_DECREF(t);
  574. return 0;
  575. }
  576. static int python_export_sample(struct db_export *dbe,
  577. struct export_sample *es)
  578. {
  579. struct tables *tables = container_of(dbe, struct tables, dbe);
  580. PyObject *t;
  581. t = tuple_new(22);
  582. tuple_set_u64(t, 0, es->db_id);
  583. tuple_set_u64(t, 1, es->evsel->db_id);
  584. tuple_set_u64(t, 2, es->al->machine->db_id);
  585. tuple_set_u64(t, 3, es->al->thread->db_id);
  586. tuple_set_u64(t, 4, es->comm_db_id);
  587. tuple_set_u64(t, 5, es->dso_db_id);
  588. tuple_set_u64(t, 6, es->sym_db_id);
  589. tuple_set_u64(t, 7, es->offset);
  590. tuple_set_u64(t, 8, es->sample->ip);
  591. tuple_set_u64(t, 9, es->sample->time);
  592. tuple_set_s32(t, 10, es->sample->cpu);
  593. tuple_set_u64(t, 11, es->addr_dso_db_id);
  594. tuple_set_u64(t, 12, es->addr_sym_db_id);
  595. tuple_set_u64(t, 13, es->addr_offset);
  596. tuple_set_u64(t, 14, es->sample->addr);
  597. tuple_set_u64(t, 15, es->sample->period);
  598. tuple_set_u64(t, 16, es->sample->weight);
  599. tuple_set_u64(t, 17, es->sample->transaction);
  600. tuple_set_u64(t, 18, es->sample->data_src);
  601. tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
  602. tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
  603. tuple_set_u64(t, 21, es->call_path_id);
  604. call_object(tables->sample_handler, t, "sample_table");
  605. Py_DECREF(t);
  606. return 0;
  607. }
  608. static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
  609. {
  610. struct tables *tables = container_of(dbe, struct tables, dbe);
  611. PyObject *t;
  612. u64 parent_db_id, sym_db_id;
  613. parent_db_id = cp->parent ? cp->parent->db_id : 0;
  614. sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
  615. t = tuple_new(4);
  616. tuple_set_u64(t, 0, cp->db_id);
  617. tuple_set_u64(t, 1, parent_db_id);
  618. tuple_set_u64(t, 2, sym_db_id);
  619. tuple_set_u64(t, 3, cp->ip);
  620. call_object(tables->call_path_handler, t, "call_path_table");
  621. Py_DECREF(t);
  622. return 0;
  623. }
  624. static int python_export_call_return(struct db_export *dbe,
  625. struct call_return *cr)
  626. {
  627. struct tables *tables = container_of(dbe, struct tables, dbe);
  628. u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
  629. PyObject *t;
  630. t = tuple_new(11);
  631. tuple_set_u64(t, 0, cr->db_id);
  632. tuple_set_u64(t, 1, cr->thread->db_id);
  633. tuple_set_u64(t, 2, comm_db_id);
  634. tuple_set_u64(t, 3, cr->cp->db_id);
  635. tuple_set_u64(t, 4, cr->call_time);
  636. tuple_set_u64(t, 5, cr->return_time);
  637. tuple_set_u64(t, 6, cr->branch_count);
  638. tuple_set_u64(t, 7, cr->call_ref);
  639. tuple_set_u64(t, 8, cr->return_ref);
  640. tuple_set_u64(t, 9, cr->cp->parent->db_id);
  641. tuple_set_s32(t, 10, cr->flags);
  642. call_object(tables->call_return_handler, t, "call_return_table");
  643. Py_DECREF(t);
  644. return 0;
  645. }
  646. static int python_process_call_return(struct call_return *cr, void *data)
  647. {
  648. struct db_export *dbe = data;
  649. return db_export__call_return(dbe, cr);
  650. }
  651. static void python_process_general_event(struct perf_sample *sample,
  652. struct perf_evsel *evsel,
  653. struct addr_location *al)
  654. {
  655. PyObject *handler, *t, *dict, *callchain, *dict_sample;
  656. static char handler_name[64];
  657. unsigned n = 0;
  658. /*
  659. * Use the MAX_FIELDS to make the function expandable, though
  660. * currently there is only one item for the tuple.
  661. */
  662. t = PyTuple_New(MAX_FIELDS);
  663. if (!t)
  664. Py_FatalError("couldn't create Python tuple");
  665. dict = PyDict_New();
  666. if (!dict)
  667. Py_FatalError("couldn't create Python dictionary");
  668. dict_sample = PyDict_New();
  669. if (!dict_sample)
  670. Py_FatalError("couldn't create Python dictionary");
  671. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  672. handler = get_handler(handler_name);
  673. if (!handler)
  674. goto exit;
  675. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  676. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  677. (const char *)&evsel->attr, sizeof(evsel->attr)));
  678. pydict_set_item_string_decref(dict_sample, "pid",
  679. PyInt_FromLong(sample->pid));
  680. pydict_set_item_string_decref(dict_sample, "tid",
  681. PyInt_FromLong(sample->tid));
  682. pydict_set_item_string_decref(dict_sample, "cpu",
  683. PyInt_FromLong(sample->cpu));
  684. pydict_set_item_string_decref(dict_sample, "ip",
  685. PyLong_FromUnsignedLongLong(sample->ip));
  686. pydict_set_item_string_decref(dict_sample, "time",
  687. PyLong_FromUnsignedLongLong(sample->time));
  688. pydict_set_item_string_decref(dict_sample, "period",
  689. PyLong_FromUnsignedLongLong(sample->period));
  690. pydict_set_item_string_decref(dict, "sample", dict_sample);
  691. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  692. (const char *)sample->raw_data, sample->raw_size));
  693. pydict_set_item_string_decref(dict, "comm",
  694. PyString_FromString(thread__comm_str(al->thread)));
  695. if (al->map) {
  696. pydict_set_item_string_decref(dict, "dso",
  697. PyString_FromString(al->map->dso->name));
  698. }
  699. if (al->sym) {
  700. pydict_set_item_string_decref(dict, "symbol",
  701. PyString_FromString(al->sym->name));
  702. }
  703. /* ip unwinding */
  704. callchain = python_process_callchain(sample, evsel, al);
  705. pydict_set_item_string_decref(dict, "callchain", callchain);
  706. PyTuple_SetItem(t, n++, dict);
  707. if (_PyTuple_Resize(&t, n) == -1)
  708. Py_FatalError("error resizing Python tuple");
  709. call_object(handler, t, handler_name);
  710. exit:
  711. Py_DECREF(dict);
  712. Py_DECREF(t);
  713. }
  714. static void python_process_event(union perf_event *event,
  715. struct perf_sample *sample,
  716. struct perf_evsel *evsel,
  717. struct addr_location *al)
  718. {
  719. struct tables *tables = &tables_global;
  720. switch (evsel->attr.type) {
  721. case PERF_TYPE_TRACEPOINT:
  722. python_process_tracepoint(sample, evsel, al);
  723. break;
  724. /* Reserve for future process_hw/sw/raw APIs */
  725. default:
  726. if (tables->db_export_mode)
  727. db_export__sample(&tables->dbe, event, sample, evsel, al);
  728. else
  729. python_process_general_event(sample, evsel, al);
  730. }
  731. }
  732. static void get_handler_name(char *str, size_t size,
  733. struct perf_evsel *evsel)
  734. {
  735. char *p = str;
  736. scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
  737. while ((p = strchr(p, ':'))) {
  738. *p = '_';
  739. p++;
  740. }
  741. }
  742. static void
  743. process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
  744. struct perf_counts_values *count)
  745. {
  746. PyObject *handler, *t;
  747. static char handler_name[256];
  748. int n = 0;
  749. t = PyTuple_New(MAX_FIELDS);
  750. if (!t)
  751. Py_FatalError("couldn't create Python tuple");
  752. get_handler_name(handler_name, sizeof(handler_name),
  753. counter);
  754. handler = get_handler(handler_name);
  755. if (!handler) {
  756. pr_debug("can't find python handler %s\n", handler_name);
  757. return;
  758. }
  759. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  760. PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
  761. tuple_set_u64(t, n++, tstamp);
  762. tuple_set_u64(t, n++, count->val);
  763. tuple_set_u64(t, n++, count->ena);
  764. tuple_set_u64(t, n++, count->run);
  765. if (_PyTuple_Resize(&t, n) == -1)
  766. Py_FatalError("error resizing Python tuple");
  767. call_object(handler, t, handler_name);
  768. Py_DECREF(t);
  769. }
  770. static void python_process_stat(struct perf_stat_config *config,
  771. struct perf_evsel *counter, u64 tstamp)
  772. {
  773. struct thread_map *threads = counter->threads;
  774. struct cpu_map *cpus = counter->cpus;
  775. int cpu, thread;
  776. if (config->aggr_mode == AGGR_GLOBAL) {
  777. process_stat(counter, -1, -1, tstamp,
  778. &counter->counts->aggr);
  779. return;
  780. }
  781. for (thread = 0; thread < threads->nr; thread++) {
  782. for (cpu = 0; cpu < cpus->nr; cpu++) {
  783. process_stat(counter, cpus->map[cpu],
  784. thread_map__pid(threads, thread), tstamp,
  785. perf_counts(counter->counts, cpu, thread));
  786. }
  787. }
  788. }
  789. static void python_process_stat_interval(u64 tstamp)
  790. {
  791. PyObject *handler, *t;
  792. static const char handler_name[] = "stat__interval";
  793. int n = 0;
  794. t = PyTuple_New(MAX_FIELDS);
  795. if (!t)
  796. Py_FatalError("couldn't create Python tuple");
  797. handler = get_handler(handler_name);
  798. if (!handler) {
  799. pr_debug("can't find python handler %s\n", handler_name);
  800. return;
  801. }
  802. tuple_set_u64(t, n++, tstamp);
  803. if (_PyTuple_Resize(&t, n) == -1)
  804. Py_FatalError("error resizing Python tuple");
  805. call_object(handler, t, handler_name);
  806. Py_DECREF(t);
  807. }
  808. static int run_start_sub(void)
  809. {
  810. main_module = PyImport_AddModule("__main__");
  811. if (main_module == NULL)
  812. return -1;
  813. Py_INCREF(main_module);
  814. main_dict = PyModule_GetDict(main_module);
  815. if (main_dict == NULL)
  816. goto error;
  817. Py_INCREF(main_dict);
  818. try_call_object("trace_begin", NULL);
  819. return 0;
  820. error:
  821. Py_XDECREF(main_dict);
  822. Py_XDECREF(main_module);
  823. return -1;
  824. }
  825. #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
  826. tables->handler_name = get_handler(#table_name); \
  827. if (tables->handler_name) \
  828. tables->dbe.export_ ## name = python_export_ ## name; \
  829. } while (0)
  830. #define SET_TABLE_HANDLER(name) \
  831. SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
  832. static void set_table_handlers(struct tables *tables)
  833. {
  834. const char *perf_db_export_mode = "perf_db_export_mode";
  835. const char *perf_db_export_calls = "perf_db_export_calls";
  836. const char *perf_db_export_callchains = "perf_db_export_callchains";
  837. PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
  838. bool export_calls = false;
  839. bool export_callchains = false;
  840. int ret;
  841. memset(tables, 0, sizeof(struct tables));
  842. if (db_export__init(&tables->dbe))
  843. Py_FatalError("failed to initialize export");
  844. db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
  845. if (!db_export_mode)
  846. return;
  847. ret = PyObject_IsTrue(db_export_mode);
  848. if (ret == -1)
  849. handler_call_die(perf_db_export_mode);
  850. if (!ret)
  851. return;
  852. /* handle export calls */
  853. tables->dbe.crp = NULL;
  854. db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
  855. if (db_export_calls) {
  856. ret = PyObject_IsTrue(db_export_calls);
  857. if (ret == -1)
  858. handler_call_die(perf_db_export_calls);
  859. export_calls = !!ret;
  860. }
  861. if (export_calls) {
  862. tables->dbe.crp =
  863. call_return_processor__new(python_process_call_return,
  864. &tables->dbe);
  865. if (!tables->dbe.crp)
  866. Py_FatalError("failed to create calls processor");
  867. }
  868. /* handle export callchains */
  869. tables->dbe.cpr = NULL;
  870. db_export_callchains = PyDict_GetItemString(main_dict,
  871. perf_db_export_callchains);
  872. if (db_export_callchains) {
  873. ret = PyObject_IsTrue(db_export_callchains);
  874. if (ret == -1)
  875. handler_call_die(perf_db_export_callchains);
  876. export_callchains = !!ret;
  877. }
  878. if (export_callchains) {
  879. /*
  880. * Attempt to use the call path root from the call return
  881. * processor, if the call return processor is in use. Otherwise,
  882. * we allocate a new call path root. This prevents exporting
  883. * duplicate call path ids when both are in use simultaniously.
  884. */
  885. if (tables->dbe.crp)
  886. tables->dbe.cpr = tables->dbe.crp->cpr;
  887. else
  888. tables->dbe.cpr = call_path_root__new();
  889. if (!tables->dbe.cpr)
  890. Py_FatalError("failed to create call path root");
  891. }
  892. tables->db_export_mode = true;
  893. /*
  894. * Reserve per symbol space for symbol->db_id via symbol__priv()
  895. */
  896. symbol_conf.priv_size = sizeof(u64);
  897. SET_TABLE_HANDLER(evsel);
  898. SET_TABLE_HANDLER(machine);
  899. SET_TABLE_HANDLER(thread);
  900. SET_TABLE_HANDLER(comm);
  901. SET_TABLE_HANDLER(comm_thread);
  902. SET_TABLE_HANDLER(dso);
  903. SET_TABLE_HANDLER(symbol);
  904. SET_TABLE_HANDLER(branch_type);
  905. SET_TABLE_HANDLER(sample);
  906. SET_TABLE_HANDLER(call_path);
  907. SET_TABLE_HANDLER(call_return);
  908. }
  909. /*
  910. * Start trace script
  911. */
  912. static int python_start_script(const char *script, int argc, const char **argv)
  913. {
  914. struct tables *tables = &tables_global;
  915. const char **command_line;
  916. char buf[PATH_MAX];
  917. int i, err = 0;
  918. FILE *fp;
  919. command_line = malloc((argc + 1) * sizeof(const char *));
  920. command_line[0] = script;
  921. for (i = 1; i < argc + 1; i++)
  922. command_line[i] = argv[i - 1];
  923. Py_Initialize();
  924. initperf_trace_context();
  925. PySys_SetArgv(argc + 1, (char **)command_line);
  926. fp = fopen(script, "r");
  927. if (!fp) {
  928. sprintf(buf, "Can't open python script \"%s\"", script);
  929. perror(buf);
  930. err = -1;
  931. goto error;
  932. }
  933. err = PyRun_SimpleFile(fp, script);
  934. if (err) {
  935. fprintf(stderr, "Error running python script %s\n", script);
  936. goto error;
  937. }
  938. err = run_start_sub();
  939. if (err) {
  940. fprintf(stderr, "Error starting python script %s\n", script);
  941. goto error;
  942. }
  943. set_table_handlers(tables);
  944. if (tables->db_export_mode) {
  945. err = db_export__branch_types(&tables->dbe);
  946. if (err)
  947. goto error;
  948. }
  949. free(command_line);
  950. return err;
  951. error:
  952. Py_Finalize();
  953. free(command_line);
  954. return err;
  955. }
  956. static int python_flush_script(void)
  957. {
  958. struct tables *tables = &tables_global;
  959. return db_export__flush(&tables->dbe);
  960. }
  961. /*
  962. * Stop trace script
  963. */
  964. static int python_stop_script(void)
  965. {
  966. struct tables *tables = &tables_global;
  967. try_call_object("trace_end", NULL);
  968. db_export__exit(&tables->dbe);
  969. Py_XDECREF(main_dict);
  970. Py_XDECREF(main_module);
  971. Py_Finalize();
  972. return 0;
  973. }
  974. static int python_generate_script(struct pevent *pevent, const char *outfile)
  975. {
  976. struct event_format *event = NULL;
  977. struct format_field *f;
  978. char fname[PATH_MAX];
  979. int not_first, count;
  980. FILE *ofp;
  981. sprintf(fname, "%s.py", outfile);
  982. ofp = fopen(fname, "w");
  983. if (ofp == NULL) {
  984. fprintf(stderr, "couldn't open %s\n", fname);
  985. return -1;
  986. }
  987. fprintf(ofp, "# perf script event handlers, "
  988. "generated by perf script -g python\n");
  989. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  990. " License version 2\n\n");
  991. fprintf(ofp, "# The common_* event handler fields are the most useful "
  992. "fields common to\n");
  993. fprintf(ofp, "# all events. They don't necessarily correspond to "
  994. "the 'common_*' fields\n");
  995. fprintf(ofp, "# in the format files. Those fields not available as "
  996. "handler params can\n");
  997. fprintf(ofp, "# be retrieved using Python functions of the form "
  998. "common_*(context).\n");
  999. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  1000. "of available functions.\n\n");
  1001. fprintf(ofp, "import os\n");
  1002. fprintf(ofp, "import sys\n\n");
  1003. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  1004. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  1005. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  1006. fprintf(ofp, "from Core import *\n\n\n");
  1007. fprintf(ofp, "def trace_begin():\n");
  1008. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  1009. fprintf(ofp, "def trace_end():\n");
  1010. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  1011. while ((event = trace_find_next_event(pevent, event))) {
  1012. fprintf(ofp, "def %s__%s(", event->system, event->name);
  1013. fprintf(ofp, "event_name, ");
  1014. fprintf(ofp, "context, ");
  1015. fprintf(ofp, "common_cpu,\n");
  1016. fprintf(ofp, "\tcommon_secs, ");
  1017. fprintf(ofp, "common_nsecs, ");
  1018. fprintf(ofp, "common_pid, ");
  1019. fprintf(ofp, "common_comm,\n\t");
  1020. fprintf(ofp, "common_callchain, ");
  1021. not_first = 0;
  1022. count = 0;
  1023. for (f = event->format.fields; f; f = f->next) {
  1024. if (not_first++)
  1025. fprintf(ofp, ", ");
  1026. if (++count % 5 == 0)
  1027. fprintf(ofp, "\n\t");
  1028. fprintf(ofp, "%s", f->name);
  1029. }
  1030. fprintf(ofp, "):\n");
  1031. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  1032. "common_secs, common_nsecs,\n\t\t\t"
  1033. "common_pid, common_comm)\n\n");
  1034. fprintf(ofp, "\t\tprint \"");
  1035. not_first = 0;
  1036. count = 0;
  1037. for (f = event->format.fields; f; f = f->next) {
  1038. if (not_first++)
  1039. fprintf(ofp, ", ");
  1040. if (count && count % 3 == 0) {
  1041. fprintf(ofp, "\" \\\n\t\t\"");
  1042. }
  1043. count++;
  1044. fprintf(ofp, "%s=", f->name);
  1045. if (f->flags & FIELD_IS_STRING ||
  1046. f->flags & FIELD_IS_FLAG ||
  1047. f->flags & FIELD_IS_ARRAY ||
  1048. f->flags & FIELD_IS_SYMBOLIC)
  1049. fprintf(ofp, "%%s");
  1050. else if (f->flags & FIELD_IS_SIGNED)
  1051. fprintf(ofp, "%%d");
  1052. else
  1053. fprintf(ofp, "%%u");
  1054. }
  1055. fprintf(ofp, "\" %% \\\n\t\t(");
  1056. not_first = 0;
  1057. count = 0;
  1058. for (f = event->format.fields; f; f = f->next) {
  1059. if (not_first++)
  1060. fprintf(ofp, ", ");
  1061. if (++count % 5 == 0)
  1062. fprintf(ofp, "\n\t\t");
  1063. if (f->flags & FIELD_IS_FLAG) {
  1064. if ((count - 1) % 5 != 0) {
  1065. fprintf(ofp, "\n\t\t");
  1066. count = 4;
  1067. }
  1068. fprintf(ofp, "flag_str(\"");
  1069. fprintf(ofp, "%s__%s\", ", event->system,
  1070. event->name);
  1071. fprintf(ofp, "\"%s\", %s)", f->name,
  1072. f->name);
  1073. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  1074. if ((count - 1) % 5 != 0) {
  1075. fprintf(ofp, "\n\t\t");
  1076. count = 4;
  1077. }
  1078. fprintf(ofp, "symbol_str(\"");
  1079. fprintf(ofp, "%s__%s\", ", event->system,
  1080. event->name);
  1081. fprintf(ofp, "\"%s\", %s)", f->name,
  1082. f->name);
  1083. } else
  1084. fprintf(ofp, "%s", f->name);
  1085. }
  1086. fprintf(ofp, ")\n\n");
  1087. fprintf(ofp, "\t\tfor node in common_callchain:");
  1088. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  1089. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  1090. fprintf(ofp, "\n\t\t\telse:");
  1091. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  1092. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  1093. }
  1094. fprintf(ofp, "def trace_unhandled(event_name, context, "
  1095. "event_fields_dict):\n");
  1096. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  1097. "for k,v in sorted(event_fields_dict.items())])\n\n");
  1098. fprintf(ofp, "def print_header("
  1099. "event_name, cpu, secs, nsecs, pid, comm):\n"
  1100. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  1101. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  1102. fclose(ofp);
  1103. fprintf(stderr, "generated Python script: %s\n", fname);
  1104. return 0;
  1105. }
  1106. struct scripting_ops python_scripting_ops = {
  1107. .name = "Python",
  1108. .start_script = python_start_script,
  1109. .flush_script = python_flush_script,
  1110. .stop_script = python_stop_script,
  1111. .process_event = python_process_event,
  1112. .process_stat = python_process_stat,
  1113. .process_stat_interval = python_process_stat_interval,
  1114. .generate_script = python_generate_script,
  1115. };