trace-event-python.c 35 KB

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