trace-event-python.c 21 KB

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