trace-event-python.c 34 KB

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