trace-event-python.c 34 KB

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