trace-event-python.c 21 KB

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