trace-event-python.c 35 KB

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