trace-event-python.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * trace-event-python. Feed trace events to an embedded Python interpreter.
  3. *
  4. * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <Python.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include "../../perf.h"
  27. #include "../evsel.h"
  28. #include "../util.h"
  29. #include "../event.h"
  30. #include "../thread.h"
  31. #include "../trace-event.h"
  32. #include "../machine.h"
  33. PyMODINIT_FUNC initperf_trace_context(void);
  34. #define FTRACE_MAX_EVENT \
  35. ((1 << (sizeof(unsigned short) * 8)) - 1)
  36. struct event_format *events[FTRACE_MAX_EVENT];
  37. #define MAX_FIELDS 64
  38. #define N_COMMON_FIELDS 7
  39. extern struct scripting_context *scripting_context;
  40. static char *cur_field_name;
  41. static int zero_flag_atom;
  42. static PyObject *main_module, *main_dict;
  43. static void handler_call_die(const char *handler_name) NORETURN;
  44. static void handler_call_die(const char *handler_name)
  45. {
  46. PyErr_Print();
  47. Py_FatalError("problem in Python trace event handler");
  48. // Py_FatalError does not return
  49. // but we have to make the compiler happy
  50. abort();
  51. }
  52. /*
  53. * Insert val into into the dictionary and decrement the reference counter.
  54. * This is necessary for dictionaries since PyDict_SetItemString() does not
  55. * steal a reference, as opposed to PyTuple_SetItem().
  56. */
  57. static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
  58. {
  59. PyDict_SetItemString(dict, key, val);
  60. Py_DECREF(val);
  61. }
  62. static void define_value(enum print_arg_type field_type,
  63. const char *ev_name,
  64. const char *field_name,
  65. const char *field_value,
  66. const char *field_str)
  67. {
  68. const char *handler_name = "define_flag_value";
  69. PyObject *handler, *t, *retval;
  70. unsigned long long value;
  71. unsigned n = 0;
  72. if (field_type == PRINT_SYMBOL)
  73. handler_name = "define_symbolic_value";
  74. t = PyTuple_New(4);
  75. if (!t)
  76. Py_FatalError("couldn't create Python tuple");
  77. value = eval_flag(field_value);
  78. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  79. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  80. PyTuple_SetItem(t, n++, PyInt_FromLong(value));
  81. PyTuple_SetItem(t, n++, PyString_FromString(field_str));
  82. handler = PyDict_GetItemString(main_dict, handler_name);
  83. if (handler && PyCallable_Check(handler)) {
  84. retval = PyObject_CallObject(handler, t);
  85. if (retval == NULL)
  86. handler_call_die(handler_name);
  87. Py_DECREF(retval);
  88. }
  89. Py_DECREF(t);
  90. }
  91. static void define_values(enum print_arg_type field_type,
  92. struct print_flag_sym *field,
  93. const char *ev_name,
  94. const char *field_name)
  95. {
  96. define_value(field_type, ev_name, field_name, field->value,
  97. field->str);
  98. if (field->next)
  99. define_values(field_type, field->next, ev_name, field_name);
  100. }
  101. static void define_field(enum print_arg_type field_type,
  102. const char *ev_name,
  103. const char *field_name,
  104. const char *delim)
  105. {
  106. const char *handler_name = "define_flag_field";
  107. PyObject *handler, *t, *retval;
  108. unsigned n = 0;
  109. if (field_type == PRINT_SYMBOL)
  110. handler_name = "define_symbolic_field";
  111. if (field_type == PRINT_FLAGS)
  112. t = PyTuple_New(3);
  113. else
  114. t = PyTuple_New(2);
  115. if (!t)
  116. Py_FatalError("couldn't create Python tuple");
  117. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  118. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  119. if (field_type == PRINT_FLAGS)
  120. PyTuple_SetItem(t, n++, PyString_FromString(delim));
  121. handler = PyDict_GetItemString(main_dict, handler_name);
  122. if (handler && PyCallable_Check(handler)) {
  123. retval = PyObject_CallObject(handler, t);
  124. if (retval == NULL)
  125. handler_call_die(handler_name);
  126. Py_DECREF(retval);
  127. }
  128. Py_DECREF(t);
  129. }
  130. static void define_event_symbols(struct event_format *event,
  131. const char *ev_name,
  132. struct print_arg *args)
  133. {
  134. switch (args->type) {
  135. case PRINT_NULL:
  136. break;
  137. case PRINT_ATOM:
  138. define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
  139. args->atom.atom);
  140. zero_flag_atom = 0;
  141. break;
  142. case PRINT_FIELD:
  143. free(cur_field_name);
  144. cur_field_name = strdup(args->field.name);
  145. break;
  146. case PRINT_FLAGS:
  147. define_event_symbols(event, ev_name, args->flags.field);
  148. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  149. args->flags.delim);
  150. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  151. cur_field_name);
  152. break;
  153. case PRINT_SYMBOL:
  154. define_event_symbols(event, ev_name, args->symbol.field);
  155. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  156. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  157. cur_field_name);
  158. break;
  159. case PRINT_HEX:
  160. define_event_symbols(event, ev_name, args->hex.field);
  161. define_event_symbols(event, ev_name, args->hex.size);
  162. break;
  163. case PRINT_STRING:
  164. break;
  165. case PRINT_TYPE:
  166. define_event_symbols(event, ev_name, args->typecast.item);
  167. break;
  168. case PRINT_OP:
  169. if (strcmp(args->op.op, ":") == 0)
  170. zero_flag_atom = 1;
  171. define_event_symbols(event, ev_name, args->op.left);
  172. define_event_symbols(event, ev_name, args->op.right);
  173. break;
  174. default:
  175. /* gcc warns for these? */
  176. case PRINT_BSTRING:
  177. case PRINT_DYNAMIC_ARRAY:
  178. case PRINT_FUNC:
  179. case PRINT_BITMASK:
  180. /* we should warn... */
  181. return;
  182. }
  183. if (args->next)
  184. define_event_symbols(event, ev_name, args->next);
  185. }
  186. static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
  187. {
  188. static char ev_name[256];
  189. struct event_format *event;
  190. int type = evsel->attr.config;
  191. /*
  192. * XXX: Do we really need to cache this since now we have evsel->tp_format
  193. * cached already? Need to re-read this "cache" routine that as well calls
  194. * define_event_symbols() :-\
  195. */
  196. if (events[type])
  197. return events[type];
  198. events[type] = event = evsel->tp_format;
  199. if (!event)
  200. return NULL;
  201. sprintf(ev_name, "%s__%s", event->system, event->name);
  202. define_event_symbols(event, ev_name, event->print_fmt.args);
  203. return event;
  204. }
  205. static PyObject *get_field_numeric_entry(struct event_format *event,
  206. struct format_field *field, void *data)
  207. {
  208. bool is_array = field->flags & FIELD_IS_ARRAY;
  209. PyObject *obj, *list = NULL;
  210. unsigned long long val;
  211. unsigned int item_size, n_items, i;
  212. if (is_array) {
  213. list = PyList_New(field->arraylen);
  214. item_size = field->size / field->arraylen;
  215. n_items = field->arraylen;
  216. } else {
  217. item_size = field->size;
  218. n_items = 1;
  219. }
  220. for (i = 0; i < n_items; i++) {
  221. val = read_size(event, data + field->offset + i * item_size,
  222. item_size);
  223. if (field->flags & FIELD_IS_SIGNED) {
  224. if ((long long)val >= LONG_MIN &&
  225. (long long)val <= LONG_MAX)
  226. obj = PyInt_FromLong(val);
  227. else
  228. obj = PyLong_FromLongLong(val);
  229. } else {
  230. if (val <= LONG_MAX)
  231. obj = PyInt_FromLong(val);
  232. else
  233. obj = PyLong_FromUnsignedLongLong(val);
  234. }
  235. if (is_array)
  236. PyList_SET_ITEM(list, i, obj);
  237. }
  238. if (is_array)
  239. obj = list;
  240. return obj;
  241. }
  242. static PyObject *python_process_callchain(struct perf_sample *sample,
  243. struct perf_evsel *evsel,
  244. struct addr_location *al)
  245. {
  246. PyObject *pylist;
  247. pylist = PyList_New(0);
  248. if (!pylist)
  249. Py_FatalError("couldn't create Python list");
  250. if (!symbol_conf.use_callchain || !sample->callchain)
  251. goto exit;
  252. if (machine__resolve_callchain(al->machine, evsel, al->thread,
  253. sample, NULL, NULL,
  254. PERF_MAX_STACK_DEPTH) != 0) {
  255. pr_err("Failed to resolve callchain. Skipping\n");
  256. goto exit;
  257. }
  258. callchain_cursor_commit(&callchain_cursor);
  259. while (1) {
  260. PyObject *pyelem;
  261. struct callchain_cursor_node *node;
  262. node = callchain_cursor_current(&callchain_cursor);
  263. if (!node)
  264. break;
  265. pyelem = PyDict_New();
  266. if (!pyelem)
  267. Py_FatalError("couldn't create Python dictionary");
  268. pydict_set_item_string_decref(pyelem, "ip",
  269. PyLong_FromUnsignedLongLong(node->ip));
  270. if (node->sym) {
  271. PyObject *pysym = PyDict_New();
  272. if (!pysym)
  273. Py_FatalError("couldn't create Python dictionary");
  274. pydict_set_item_string_decref(pysym, "start",
  275. PyLong_FromUnsignedLongLong(node->sym->start));
  276. pydict_set_item_string_decref(pysym, "end",
  277. PyLong_FromUnsignedLongLong(node->sym->end));
  278. pydict_set_item_string_decref(pysym, "binding",
  279. PyInt_FromLong(node->sym->binding));
  280. pydict_set_item_string_decref(pysym, "name",
  281. PyString_FromStringAndSize(node->sym->name,
  282. node->sym->namelen));
  283. pydict_set_item_string_decref(pyelem, "sym", pysym);
  284. }
  285. if (node->map) {
  286. struct map *map = node->map;
  287. const char *dsoname = "[unknown]";
  288. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  289. if (symbol_conf.show_kernel_path && map->dso->long_name)
  290. dsoname = map->dso->long_name;
  291. else if (map->dso->name)
  292. dsoname = map->dso->name;
  293. }
  294. pydict_set_item_string_decref(pyelem, "dso",
  295. PyString_FromString(dsoname));
  296. }
  297. callchain_cursor_advance(&callchain_cursor);
  298. PyList_Append(pylist, pyelem);
  299. Py_DECREF(pyelem);
  300. }
  301. exit:
  302. return pylist;
  303. }
  304. static void python_process_tracepoint(struct perf_sample *sample,
  305. struct perf_evsel *evsel,
  306. struct thread *thread,
  307. struct addr_location *al)
  308. {
  309. PyObject *handler, *retval, *context, *t, *obj, *callchain;
  310. PyObject *dict = NULL;
  311. static char handler_name[256];
  312. struct format_field *field;
  313. unsigned long s, ns;
  314. struct event_format *event;
  315. unsigned n = 0;
  316. int pid;
  317. int cpu = sample->cpu;
  318. void *data = sample->raw_data;
  319. unsigned long long nsecs = sample->time;
  320. const char *comm = thread__comm_str(thread);
  321. t = PyTuple_New(MAX_FIELDS);
  322. if (!t)
  323. Py_FatalError("couldn't create Python tuple");
  324. event = find_cache_event(evsel);
  325. if (!event)
  326. die("ug! no event found for type %d", (int)evsel->attr.config);
  327. pid = raw_field_value(event, "common_pid", data);
  328. sprintf(handler_name, "%s__%s", event->system, event->name);
  329. handler = PyDict_GetItemString(main_dict, handler_name);
  330. if (handler && !PyCallable_Check(handler))
  331. handler = NULL;
  332. if (!handler) {
  333. dict = PyDict_New();
  334. if (!dict)
  335. Py_FatalError("couldn't create Python dict");
  336. }
  337. s = nsecs / NSECS_PER_SEC;
  338. ns = nsecs - s * NSECS_PER_SEC;
  339. scripting_context->event_data = data;
  340. scripting_context->pevent = evsel->tp_format->pevent;
  341. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  342. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  343. PyTuple_SetItem(t, n++, context);
  344. /* ip unwinding */
  345. callchain = python_process_callchain(sample, evsel, al);
  346. if (handler) {
  347. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  348. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  349. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  350. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  351. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  352. PyTuple_SetItem(t, n++, callchain);
  353. } else {
  354. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  355. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  356. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  357. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  358. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  359. pydict_set_item_string_decref(dict, "common_callchain", callchain);
  360. }
  361. for (field = event->format.fields; field; field = field->next) {
  362. if (field->flags & FIELD_IS_STRING) {
  363. int offset;
  364. if (field->flags & FIELD_IS_DYNAMIC) {
  365. offset = *(int *)(data + field->offset);
  366. offset &= 0xffff;
  367. } else
  368. offset = field->offset;
  369. obj = PyString_FromString((char *)data + offset);
  370. } else { /* FIELD_IS_NUMERIC */
  371. obj = get_field_numeric_entry(event, field, data);
  372. }
  373. if (handler)
  374. PyTuple_SetItem(t, n++, obj);
  375. else
  376. pydict_set_item_string_decref(dict, field->name, obj);
  377. }
  378. if (!handler)
  379. PyTuple_SetItem(t, n++, dict);
  380. if (_PyTuple_Resize(&t, n) == -1)
  381. Py_FatalError("error resizing Python tuple");
  382. if (handler) {
  383. retval = PyObject_CallObject(handler, t);
  384. if (retval == NULL)
  385. handler_call_die(handler_name);
  386. Py_DECREF(retval);
  387. } else {
  388. handler = PyDict_GetItemString(main_dict, "trace_unhandled");
  389. if (handler && PyCallable_Check(handler)) {
  390. retval = PyObject_CallObject(handler, t);
  391. if (retval == NULL)
  392. handler_call_die("trace_unhandled");
  393. Py_DECREF(retval);
  394. }
  395. Py_DECREF(dict);
  396. }
  397. Py_DECREF(t);
  398. }
  399. static void python_process_general_event(struct perf_sample *sample,
  400. struct perf_evsel *evsel,
  401. struct thread *thread,
  402. struct addr_location *al)
  403. {
  404. PyObject *handler, *retval, *t, *dict, *callchain, *dict_sample;
  405. static char handler_name[64];
  406. unsigned n = 0;
  407. /*
  408. * Use the MAX_FIELDS to make the function expandable, though
  409. * currently there is only one item for the tuple.
  410. */
  411. t = PyTuple_New(MAX_FIELDS);
  412. if (!t)
  413. Py_FatalError("couldn't create Python tuple");
  414. dict = PyDict_New();
  415. if (!dict)
  416. Py_FatalError("couldn't create Python dictionary");
  417. dict_sample = PyDict_New();
  418. if (!dict_sample)
  419. Py_FatalError("couldn't create Python dictionary");
  420. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  421. handler = PyDict_GetItemString(main_dict, handler_name);
  422. if (!handler || !PyCallable_Check(handler))
  423. goto exit;
  424. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  425. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  426. (const char *)&evsel->attr, sizeof(evsel->attr)));
  427. pydict_set_item_string_decref(dict_sample, "pid",
  428. PyInt_FromLong(sample->pid));
  429. pydict_set_item_string_decref(dict_sample, "tid",
  430. PyInt_FromLong(sample->tid));
  431. pydict_set_item_string_decref(dict_sample, "cpu",
  432. PyInt_FromLong(sample->cpu));
  433. pydict_set_item_string_decref(dict_sample, "ip",
  434. PyLong_FromUnsignedLongLong(sample->ip));
  435. pydict_set_item_string_decref(dict_sample, "time",
  436. PyLong_FromUnsignedLongLong(sample->time));
  437. pydict_set_item_string_decref(dict_sample, "period",
  438. PyLong_FromUnsignedLongLong(sample->period));
  439. pydict_set_item_string_decref(dict, "sample", dict_sample);
  440. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  441. (const char *)sample->raw_data, sample->raw_size));
  442. pydict_set_item_string_decref(dict, "comm",
  443. PyString_FromString(thread__comm_str(thread)));
  444. if (al->map) {
  445. pydict_set_item_string_decref(dict, "dso",
  446. PyString_FromString(al->map->dso->name));
  447. }
  448. if (al->sym) {
  449. pydict_set_item_string_decref(dict, "symbol",
  450. PyString_FromString(al->sym->name));
  451. }
  452. /* ip unwinding */
  453. callchain = python_process_callchain(sample, evsel, al);
  454. pydict_set_item_string_decref(dict, "callchain", callchain);
  455. PyTuple_SetItem(t, n++, dict);
  456. if (_PyTuple_Resize(&t, n) == -1)
  457. Py_FatalError("error resizing Python tuple");
  458. retval = PyObject_CallObject(handler, t);
  459. if (retval == NULL)
  460. handler_call_die(handler_name);
  461. Py_DECREF(retval);
  462. exit:
  463. Py_DECREF(dict);
  464. Py_DECREF(t);
  465. }
  466. static void python_process_event(union perf_event *event __maybe_unused,
  467. struct perf_sample *sample,
  468. struct perf_evsel *evsel,
  469. struct thread *thread,
  470. struct addr_location *al)
  471. {
  472. switch (evsel->attr.type) {
  473. case PERF_TYPE_TRACEPOINT:
  474. python_process_tracepoint(sample, evsel, thread, al);
  475. break;
  476. /* Reserve for future process_hw/sw/raw APIs */
  477. default:
  478. python_process_general_event(sample, evsel, thread, al);
  479. }
  480. }
  481. static int run_start_sub(void)
  482. {
  483. PyObject *handler, *retval;
  484. int err = 0;
  485. main_module = PyImport_AddModule("__main__");
  486. if (main_module == NULL)
  487. return -1;
  488. Py_INCREF(main_module);
  489. main_dict = PyModule_GetDict(main_module);
  490. if (main_dict == NULL) {
  491. err = -1;
  492. goto error;
  493. }
  494. Py_INCREF(main_dict);
  495. handler = PyDict_GetItemString(main_dict, "trace_begin");
  496. if (handler == NULL || !PyCallable_Check(handler))
  497. goto out;
  498. retval = PyObject_CallObject(handler, NULL);
  499. if (retval == NULL)
  500. handler_call_die("trace_begin");
  501. Py_DECREF(retval);
  502. return err;
  503. error:
  504. Py_XDECREF(main_dict);
  505. Py_XDECREF(main_module);
  506. out:
  507. return err;
  508. }
  509. /*
  510. * Start trace script
  511. */
  512. static int python_start_script(const char *script, int argc, const char **argv)
  513. {
  514. const char **command_line;
  515. char buf[PATH_MAX];
  516. int i, err = 0;
  517. FILE *fp;
  518. command_line = malloc((argc + 1) * sizeof(const char *));
  519. command_line[0] = script;
  520. for (i = 1; i < argc + 1; i++)
  521. command_line[i] = argv[i - 1];
  522. Py_Initialize();
  523. initperf_trace_context();
  524. PySys_SetArgv(argc + 1, (char **)command_line);
  525. fp = fopen(script, "r");
  526. if (!fp) {
  527. sprintf(buf, "Can't open python script \"%s\"", script);
  528. perror(buf);
  529. err = -1;
  530. goto error;
  531. }
  532. err = PyRun_SimpleFile(fp, script);
  533. if (err) {
  534. fprintf(stderr, "Error running python script %s\n", script);
  535. goto error;
  536. }
  537. err = run_start_sub();
  538. if (err) {
  539. fprintf(stderr, "Error starting python script %s\n", script);
  540. goto error;
  541. }
  542. free(command_line);
  543. return err;
  544. error:
  545. Py_Finalize();
  546. free(command_line);
  547. return err;
  548. }
  549. /*
  550. * Stop trace script
  551. */
  552. static int python_stop_script(void)
  553. {
  554. PyObject *handler, *retval;
  555. int err = 0;
  556. handler = PyDict_GetItemString(main_dict, "trace_end");
  557. if (handler == NULL || !PyCallable_Check(handler))
  558. goto out;
  559. retval = PyObject_CallObject(handler, NULL);
  560. if (retval == NULL)
  561. handler_call_die("trace_end");
  562. Py_DECREF(retval);
  563. out:
  564. Py_XDECREF(main_dict);
  565. Py_XDECREF(main_module);
  566. Py_Finalize();
  567. return err;
  568. }
  569. static int python_generate_script(struct pevent *pevent, const char *outfile)
  570. {
  571. struct event_format *event = NULL;
  572. struct format_field *f;
  573. char fname[PATH_MAX];
  574. int not_first, count;
  575. FILE *ofp;
  576. sprintf(fname, "%s.py", outfile);
  577. ofp = fopen(fname, "w");
  578. if (ofp == NULL) {
  579. fprintf(stderr, "couldn't open %s\n", fname);
  580. return -1;
  581. }
  582. fprintf(ofp, "# perf script event handlers, "
  583. "generated by perf script -g python\n");
  584. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  585. " License version 2\n\n");
  586. fprintf(ofp, "# The common_* event handler fields are the most useful "
  587. "fields common to\n");
  588. fprintf(ofp, "# all events. They don't necessarily correspond to "
  589. "the 'common_*' fields\n");
  590. fprintf(ofp, "# in the format files. Those fields not available as "
  591. "handler params can\n");
  592. fprintf(ofp, "# be retrieved using Python functions of the form "
  593. "common_*(context).\n");
  594. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  595. "of available functions.\n\n");
  596. fprintf(ofp, "import os\n");
  597. fprintf(ofp, "import sys\n\n");
  598. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  599. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  600. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  601. fprintf(ofp, "from Core import *\n\n\n");
  602. fprintf(ofp, "def trace_begin():\n");
  603. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  604. fprintf(ofp, "def trace_end():\n");
  605. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  606. while ((event = trace_find_next_event(pevent, event))) {
  607. fprintf(ofp, "def %s__%s(", event->system, event->name);
  608. fprintf(ofp, "event_name, ");
  609. fprintf(ofp, "context, ");
  610. fprintf(ofp, "common_cpu,\n");
  611. fprintf(ofp, "\tcommon_secs, ");
  612. fprintf(ofp, "common_nsecs, ");
  613. fprintf(ofp, "common_pid, ");
  614. fprintf(ofp, "common_comm,\n\t");
  615. fprintf(ofp, "common_callchain, ");
  616. not_first = 0;
  617. count = 0;
  618. for (f = event->format.fields; f; f = f->next) {
  619. if (not_first++)
  620. fprintf(ofp, ", ");
  621. if (++count % 5 == 0)
  622. fprintf(ofp, "\n\t");
  623. fprintf(ofp, "%s", f->name);
  624. }
  625. fprintf(ofp, "):\n");
  626. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  627. "common_secs, common_nsecs,\n\t\t\t"
  628. "common_pid, common_comm)\n\n");
  629. fprintf(ofp, "\t\tprint \"");
  630. not_first = 0;
  631. count = 0;
  632. for (f = event->format.fields; f; f = f->next) {
  633. if (not_first++)
  634. fprintf(ofp, ", ");
  635. if (count && count % 3 == 0) {
  636. fprintf(ofp, "\" \\\n\t\t\"");
  637. }
  638. count++;
  639. fprintf(ofp, "%s=", f->name);
  640. if (f->flags & FIELD_IS_STRING ||
  641. f->flags & FIELD_IS_FLAG ||
  642. f->flags & FIELD_IS_ARRAY ||
  643. f->flags & FIELD_IS_SYMBOLIC)
  644. fprintf(ofp, "%%s");
  645. else if (f->flags & FIELD_IS_SIGNED)
  646. fprintf(ofp, "%%d");
  647. else
  648. fprintf(ofp, "%%u");
  649. }
  650. fprintf(ofp, "\" %% \\\n\t\t(");
  651. not_first = 0;
  652. count = 0;
  653. for (f = event->format.fields; f; f = f->next) {
  654. if (not_first++)
  655. fprintf(ofp, ", ");
  656. if (++count % 5 == 0)
  657. fprintf(ofp, "\n\t\t");
  658. if (f->flags & FIELD_IS_FLAG) {
  659. if ((count - 1) % 5 != 0) {
  660. fprintf(ofp, "\n\t\t");
  661. count = 4;
  662. }
  663. fprintf(ofp, "flag_str(\"");
  664. fprintf(ofp, "%s__%s\", ", event->system,
  665. event->name);
  666. fprintf(ofp, "\"%s\", %s)", f->name,
  667. f->name);
  668. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  669. if ((count - 1) % 5 != 0) {
  670. fprintf(ofp, "\n\t\t");
  671. count = 4;
  672. }
  673. fprintf(ofp, "symbol_str(\"");
  674. fprintf(ofp, "%s__%s\", ", event->system,
  675. event->name);
  676. fprintf(ofp, "\"%s\", %s)", f->name,
  677. f->name);
  678. } else
  679. fprintf(ofp, "%s", f->name);
  680. }
  681. fprintf(ofp, ")\n\n");
  682. fprintf(ofp, "\t\tfor node in common_callchain:");
  683. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  684. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  685. fprintf(ofp, "\n\t\t\telse:");
  686. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  687. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  688. }
  689. fprintf(ofp, "def trace_unhandled(event_name, context, "
  690. "event_fields_dict):\n");
  691. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  692. "for k,v in sorted(event_fields_dict.items())])\n\n");
  693. fprintf(ofp, "def print_header("
  694. "event_name, cpu, secs, nsecs, pid, comm):\n"
  695. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  696. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  697. fclose(ofp);
  698. fprintf(stderr, "generated Python script: %s\n", fname);
  699. return 0;
  700. }
  701. struct scripting_ops python_scripting_ops = {
  702. .name = "Python",
  703. .start_script = python_start_script,
  704. .stop_script = python_stop_script,
  705. .process_event = python_process_event,
  706. .generate_script = python_generate_script,
  707. };