trace-event-python.c 34 KB

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