trace-event-python.c 34 KB

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