trace-event-python.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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;
  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. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  418. handler = PyDict_GetItemString(main_dict, handler_name);
  419. if (!handler || !PyCallable_Check(handler))
  420. goto exit;
  421. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  422. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  423. (const char *)&evsel->attr, sizeof(evsel->attr)));
  424. pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize(
  425. (const char *)sample, sizeof(*sample)));
  426. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  427. (const char *)sample->raw_data, sample->raw_size));
  428. pydict_set_item_string_decref(dict, "comm",
  429. PyString_FromString(thread__comm_str(thread)));
  430. if (al->map) {
  431. pydict_set_item_string_decref(dict, "dso",
  432. PyString_FromString(al->map->dso->name));
  433. }
  434. if (al->sym) {
  435. pydict_set_item_string_decref(dict, "symbol",
  436. PyString_FromString(al->sym->name));
  437. }
  438. /* ip unwinding */
  439. callchain = python_process_callchain(sample, evsel, al);
  440. pydict_set_item_string_decref(dict, "callchain", callchain);
  441. PyTuple_SetItem(t, n++, dict);
  442. if (_PyTuple_Resize(&t, n) == -1)
  443. Py_FatalError("error resizing Python tuple");
  444. retval = PyObject_CallObject(handler, t);
  445. if (retval == NULL)
  446. handler_call_die(handler_name);
  447. Py_DECREF(retval);
  448. exit:
  449. Py_DECREF(dict);
  450. Py_DECREF(t);
  451. }
  452. static void python_process_event(union perf_event *event __maybe_unused,
  453. struct perf_sample *sample,
  454. struct perf_evsel *evsel,
  455. struct thread *thread,
  456. struct addr_location *al)
  457. {
  458. switch (evsel->attr.type) {
  459. case PERF_TYPE_TRACEPOINT:
  460. python_process_tracepoint(sample, evsel, thread, al);
  461. break;
  462. /* Reserve for future process_hw/sw/raw APIs */
  463. default:
  464. python_process_general_event(sample, evsel, thread, al);
  465. }
  466. }
  467. static int run_start_sub(void)
  468. {
  469. PyObject *handler, *retval;
  470. int err = 0;
  471. main_module = PyImport_AddModule("__main__");
  472. if (main_module == NULL)
  473. return -1;
  474. Py_INCREF(main_module);
  475. main_dict = PyModule_GetDict(main_module);
  476. if (main_dict == NULL) {
  477. err = -1;
  478. goto error;
  479. }
  480. Py_INCREF(main_dict);
  481. handler = PyDict_GetItemString(main_dict, "trace_begin");
  482. if (handler == NULL || !PyCallable_Check(handler))
  483. goto out;
  484. retval = PyObject_CallObject(handler, NULL);
  485. if (retval == NULL)
  486. handler_call_die("trace_begin");
  487. Py_DECREF(retval);
  488. return err;
  489. error:
  490. Py_XDECREF(main_dict);
  491. Py_XDECREF(main_module);
  492. out:
  493. return err;
  494. }
  495. /*
  496. * Start trace script
  497. */
  498. static int python_start_script(const char *script, int argc, const char **argv)
  499. {
  500. const char **command_line;
  501. char buf[PATH_MAX];
  502. int i, err = 0;
  503. FILE *fp;
  504. command_line = malloc((argc + 1) * sizeof(const char *));
  505. command_line[0] = script;
  506. for (i = 1; i < argc + 1; i++)
  507. command_line[i] = argv[i - 1];
  508. Py_Initialize();
  509. initperf_trace_context();
  510. PySys_SetArgv(argc + 1, (char **)command_line);
  511. fp = fopen(script, "r");
  512. if (!fp) {
  513. sprintf(buf, "Can't open python script \"%s\"", script);
  514. perror(buf);
  515. err = -1;
  516. goto error;
  517. }
  518. err = PyRun_SimpleFile(fp, script);
  519. if (err) {
  520. fprintf(stderr, "Error running python script %s\n", script);
  521. goto error;
  522. }
  523. err = run_start_sub();
  524. if (err) {
  525. fprintf(stderr, "Error starting python script %s\n", script);
  526. goto error;
  527. }
  528. free(command_line);
  529. return err;
  530. error:
  531. Py_Finalize();
  532. free(command_line);
  533. return err;
  534. }
  535. /*
  536. * Stop trace script
  537. */
  538. static int python_stop_script(void)
  539. {
  540. PyObject *handler, *retval;
  541. int err = 0;
  542. handler = PyDict_GetItemString(main_dict, "trace_end");
  543. if (handler == NULL || !PyCallable_Check(handler))
  544. goto out;
  545. retval = PyObject_CallObject(handler, NULL);
  546. if (retval == NULL)
  547. handler_call_die("trace_end");
  548. Py_DECREF(retval);
  549. out:
  550. Py_XDECREF(main_dict);
  551. Py_XDECREF(main_module);
  552. Py_Finalize();
  553. return err;
  554. }
  555. static int python_generate_script(struct pevent *pevent, const char *outfile)
  556. {
  557. struct event_format *event = NULL;
  558. struct format_field *f;
  559. char fname[PATH_MAX];
  560. int not_first, count;
  561. FILE *ofp;
  562. sprintf(fname, "%s.py", outfile);
  563. ofp = fopen(fname, "w");
  564. if (ofp == NULL) {
  565. fprintf(stderr, "couldn't open %s\n", fname);
  566. return -1;
  567. }
  568. fprintf(ofp, "# perf script event handlers, "
  569. "generated by perf script -g python\n");
  570. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  571. " License version 2\n\n");
  572. fprintf(ofp, "# The common_* event handler fields are the most useful "
  573. "fields common to\n");
  574. fprintf(ofp, "# all events. They don't necessarily correspond to "
  575. "the 'common_*' fields\n");
  576. fprintf(ofp, "# in the format files. Those fields not available as "
  577. "handler params can\n");
  578. fprintf(ofp, "# be retrieved using Python functions of the form "
  579. "common_*(context).\n");
  580. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  581. "of available functions.\n\n");
  582. fprintf(ofp, "import os\n");
  583. fprintf(ofp, "import sys\n\n");
  584. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  585. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  586. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  587. fprintf(ofp, "from Core import *\n\n\n");
  588. fprintf(ofp, "def trace_begin():\n");
  589. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  590. fprintf(ofp, "def trace_end():\n");
  591. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  592. while ((event = trace_find_next_event(pevent, event))) {
  593. fprintf(ofp, "def %s__%s(", event->system, event->name);
  594. fprintf(ofp, "event_name, ");
  595. fprintf(ofp, "context, ");
  596. fprintf(ofp, "common_cpu,\n");
  597. fprintf(ofp, "\tcommon_secs, ");
  598. fprintf(ofp, "common_nsecs, ");
  599. fprintf(ofp, "common_pid, ");
  600. fprintf(ofp, "common_comm,\n\t");
  601. fprintf(ofp, "common_callchain, ");
  602. not_first = 0;
  603. count = 0;
  604. for (f = event->format.fields; f; f = f->next) {
  605. if (not_first++)
  606. fprintf(ofp, ", ");
  607. if (++count % 5 == 0)
  608. fprintf(ofp, "\n\t");
  609. fprintf(ofp, "%s", f->name);
  610. }
  611. fprintf(ofp, "):\n");
  612. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  613. "common_secs, common_nsecs,\n\t\t\t"
  614. "common_pid, common_comm)\n\n");
  615. fprintf(ofp, "\t\tprint \"");
  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 && count % 3 == 0) {
  622. fprintf(ofp, "\" \\\n\t\t\"");
  623. }
  624. count++;
  625. fprintf(ofp, "%s=", f->name);
  626. if (f->flags & FIELD_IS_STRING ||
  627. f->flags & FIELD_IS_FLAG ||
  628. f->flags & FIELD_IS_ARRAY ||
  629. f->flags & FIELD_IS_SYMBOLIC)
  630. fprintf(ofp, "%%s");
  631. else if (f->flags & FIELD_IS_SIGNED)
  632. fprintf(ofp, "%%d");
  633. else
  634. fprintf(ofp, "%%u");
  635. }
  636. fprintf(ofp, "\" %% \\\n\t\t(");
  637. not_first = 0;
  638. count = 0;
  639. for (f = event->format.fields; f; f = f->next) {
  640. if (not_first++)
  641. fprintf(ofp, ", ");
  642. if (++count % 5 == 0)
  643. fprintf(ofp, "\n\t\t");
  644. if (f->flags & FIELD_IS_FLAG) {
  645. if ((count - 1) % 5 != 0) {
  646. fprintf(ofp, "\n\t\t");
  647. count = 4;
  648. }
  649. fprintf(ofp, "flag_str(\"");
  650. fprintf(ofp, "%s__%s\", ", event->system,
  651. event->name);
  652. fprintf(ofp, "\"%s\", %s)", f->name,
  653. f->name);
  654. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  655. if ((count - 1) % 5 != 0) {
  656. fprintf(ofp, "\n\t\t");
  657. count = 4;
  658. }
  659. fprintf(ofp, "symbol_str(\"");
  660. fprintf(ofp, "%s__%s\", ", event->system,
  661. event->name);
  662. fprintf(ofp, "\"%s\", %s)", f->name,
  663. f->name);
  664. } else
  665. fprintf(ofp, "%s", f->name);
  666. }
  667. fprintf(ofp, ")\n\n");
  668. fprintf(ofp, "\t\tfor node in common_callchain:");
  669. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  670. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  671. fprintf(ofp, "\n\t\t\telse:");
  672. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  673. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  674. }
  675. fprintf(ofp, "def trace_unhandled(event_name, context, "
  676. "event_fields_dict):\n");
  677. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  678. "for k,v in sorted(event_fields_dict.items())])\n\n");
  679. fprintf(ofp, "def print_header("
  680. "event_name, cpu, secs, nsecs, pid, comm):\n"
  681. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  682. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  683. fclose(ofp);
  684. fprintf(stderr, "generated Python script: %s\n", fname);
  685. return 0;
  686. }
  687. struct scripting_ops python_scripting_ops = {
  688. .name = "Python",
  689. .start_script = python_start_script,
  690. .stop_script = python_stop_script,
  691. .process_event = python_process_event,
  692. .generate_script = python_generate_script,
  693. };