trace-event-python.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 <ctype.h>
  26. #include <errno.h>
  27. #include "../../perf.h"
  28. #include "../util.h"
  29. #include "../trace-event.h"
  30. PyMODINIT_FUNC initperf_trace_context(void);
  31. #define FTRACE_MAX_EVENT \
  32. ((1 << (sizeof(unsigned short) * 8)) - 1)
  33. struct event *events[FTRACE_MAX_EVENT];
  34. #define MAX_FIELDS 64
  35. #define N_COMMON_FIELDS 7
  36. extern struct scripting_context *scripting_context;
  37. static char *cur_field_name;
  38. static int zero_flag_atom;
  39. static PyObject *main_module, *main_dict;
  40. static void handler_call_die(const char *handler_name)
  41. {
  42. PyErr_Print();
  43. Py_FatalError("problem in Python trace event handler");
  44. }
  45. static void define_value(enum print_arg_type field_type,
  46. const char *ev_name,
  47. const char *field_name,
  48. const char *field_value,
  49. const char *field_str)
  50. {
  51. const char *handler_name = "define_flag_value";
  52. PyObject *handler, *t, *retval;
  53. unsigned long long value;
  54. unsigned n = 0;
  55. if (field_type == PRINT_SYMBOL)
  56. handler_name = "define_symbolic_value";
  57. t = PyTuple_New(MAX_FIELDS);
  58. if (!t)
  59. Py_FatalError("couldn't create Python tuple");
  60. value = eval_flag(field_value);
  61. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  62. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  63. PyTuple_SetItem(t, n++, PyInt_FromLong(value));
  64. PyTuple_SetItem(t, n++, PyString_FromString(field_str));
  65. if (_PyTuple_Resize(&t, n) == -1)
  66. Py_FatalError("error resizing Python tuple");
  67. handler = PyDict_GetItemString(main_dict, handler_name);
  68. if (handler && PyCallable_Check(handler)) {
  69. retval = PyObject_CallObject(handler, t);
  70. if (retval == NULL)
  71. handler_call_die(handler_name);
  72. }
  73. Py_DECREF(t);
  74. }
  75. static void define_values(enum print_arg_type field_type,
  76. struct print_flag_sym *field,
  77. const char *ev_name,
  78. const char *field_name)
  79. {
  80. define_value(field_type, ev_name, field_name, field->value,
  81. field->str);
  82. if (field->next)
  83. define_values(field_type, field->next, ev_name, field_name);
  84. }
  85. static void define_field(enum print_arg_type field_type,
  86. const char *ev_name,
  87. const char *field_name,
  88. const char *delim)
  89. {
  90. const char *handler_name = "define_flag_field";
  91. PyObject *handler, *t, *retval;
  92. unsigned n = 0;
  93. if (field_type == PRINT_SYMBOL)
  94. handler_name = "define_symbolic_field";
  95. t = PyTuple_New(MAX_FIELDS);
  96. if (!t)
  97. Py_FatalError("couldn't create Python tuple");
  98. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  99. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  100. if (field_type == PRINT_FLAGS)
  101. PyTuple_SetItem(t, n++, PyString_FromString(delim));
  102. if (_PyTuple_Resize(&t, n) == -1)
  103. Py_FatalError("error resizing Python tuple");
  104. handler = PyDict_GetItemString(main_dict, handler_name);
  105. if (handler && PyCallable_Check(handler)) {
  106. retval = PyObject_CallObject(handler, t);
  107. if (retval == NULL)
  108. handler_call_die(handler_name);
  109. }
  110. Py_DECREF(t);
  111. }
  112. static void define_event_symbols(struct event *event,
  113. const char *ev_name,
  114. struct print_arg *args)
  115. {
  116. switch (args->type) {
  117. case PRINT_NULL:
  118. break;
  119. case PRINT_ATOM:
  120. define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
  121. args->atom.atom);
  122. zero_flag_atom = 0;
  123. break;
  124. case PRINT_FIELD:
  125. if (cur_field_name)
  126. free(cur_field_name);
  127. cur_field_name = strdup(args->field.name);
  128. break;
  129. case PRINT_FLAGS:
  130. define_event_symbols(event, ev_name, args->flags.field);
  131. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  132. args->flags.delim);
  133. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  134. cur_field_name);
  135. break;
  136. case PRINT_SYMBOL:
  137. define_event_symbols(event, ev_name, args->symbol.field);
  138. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  139. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  140. cur_field_name);
  141. break;
  142. case PRINT_STRING:
  143. break;
  144. case PRINT_TYPE:
  145. define_event_symbols(event, ev_name, args->typecast.item);
  146. break;
  147. case PRINT_OP:
  148. if (strcmp(args->op.op, ":") == 0)
  149. zero_flag_atom = 1;
  150. define_event_symbols(event, ev_name, args->op.left);
  151. define_event_symbols(event, ev_name, args->op.right);
  152. break;
  153. default:
  154. /* we should warn... */
  155. return;
  156. }
  157. if (args->next)
  158. define_event_symbols(event, ev_name, args->next);
  159. }
  160. static inline struct event *find_cache_event(int type)
  161. {
  162. static char ev_name[256];
  163. struct event *event;
  164. if (events[type])
  165. return events[type];
  166. events[type] = event = trace_find_event(type);
  167. if (!event)
  168. return NULL;
  169. sprintf(ev_name, "%s__%s", event->system, event->name);
  170. define_event_symbols(event, ev_name, event->print_fmt.args);
  171. return event;
  172. }
  173. static void python_process_event(int cpu, void *data,
  174. int size __unused,
  175. unsigned long long nsecs, char *comm)
  176. {
  177. PyObject *handler, *retval, *context, *t;
  178. static char handler_name[256];
  179. struct format_field *field;
  180. unsigned long long val;
  181. unsigned long s, ns;
  182. struct event *event;
  183. unsigned n = 0;
  184. int type;
  185. int pid;
  186. t = PyTuple_New(MAX_FIELDS);
  187. if (!t)
  188. Py_FatalError("couldn't create Python tuple");
  189. type = trace_parse_common_type(data);
  190. event = find_cache_event(type);
  191. if (!event)
  192. die("ug! no event found for type %d", type);
  193. pid = trace_parse_common_pid(data);
  194. sprintf(handler_name, "%s__%s", event->system, event->name);
  195. s = nsecs / NSECS_PER_SEC;
  196. ns = nsecs - s * NSECS_PER_SEC;
  197. scripting_context->event_data = data;
  198. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  199. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  200. PyTuple_SetItem(t, n++,
  201. PyCObject_FromVoidPtr(scripting_context, NULL));
  202. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  203. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  204. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  205. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  206. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  207. for (field = event->format.fields; field; field = field->next) {
  208. if (field->flags & FIELD_IS_STRING) {
  209. int offset;
  210. if (field->flags & FIELD_IS_DYNAMIC) {
  211. offset = *(int *)(data + field->offset);
  212. offset &= 0xffff;
  213. } else
  214. offset = field->offset;
  215. PyTuple_SetItem(t, n++,
  216. PyString_FromString((char *)data + offset));
  217. } else { /* FIELD_IS_NUMERIC */
  218. val = read_size(data + field->offset, field->size);
  219. if (field->flags & FIELD_IS_SIGNED) {
  220. PyTuple_SetItem(t, n++, PyInt_FromLong(val));
  221. } else {
  222. PyTuple_SetItem(t, n++, PyInt_FromLong(val));
  223. }
  224. }
  225. }
  226. if (_PyTuple_Resize(&t, n) == -1)
  227. Py_FatalError("error resizing Python tuple");
  228. handler = PyDict_GetItemString(main_dict, handler_name);
  229. if (handler && PyCallable_Check(handler)) {
  230. retval = PyObject_CallObject(handler, t);
  231. if (retval == NULL)
  232. handler_call_die(handler_name);
  233. } else {
  234. handler = PyDict_GetItemString(main_dict, "trace_unhandled");
  235. if (handler && PyCallable_Check(handler)) {
  236. if (_PyTuple_Resize(&t, N_COMMON_FIELDS) == -1)
  237. Py_FatalError("error resizing Python tuple");
  238. retval = PyObject_CallObject(handler, t);
  239. if (retval == NULL)
  240. handler_call_die("trace_unhandled");
  241. }
  242. }
  243. Py_DECREF(t);
  244. }
  245. static int run_start_sub(void)
  246. {
  247. PyObject *handler, *retval;
  248. int err = 0;
  249. main_module = PyImport_AddModule("__main__");
  250. if (main_module == NULL)
  251. return -1;
  252. Py_INCREF(main_module);
  253. main_dict = PyModule_GetDict(main_module);
  254. if (main_dict == NULL) {
  255. err = -1;
  256. goto error;
  257. }
  258. Py_INCREF(main_dict);
  259. handler = PyDict_GetItemString(main_dict, "trace_begin");
  260. if (handler == NULL || !PyCallable_Check(handler))
  261. goto out;
  262. retval = PyObject_CallObject(handler, NULL);
  263. if (retval == NULL)
  264. handler_call_die("trace_begin");
  265. Py_DECREF(retval);
  266. return err;
  267. error:
  268. Py_XDECREF(main_dict);
  269. Py_XDECREF(main_module);
  270. out:
  271. return err;
  272. }
  273. /*
  274. * Start trace script
  275. */
  276. static int python_start_script(const char *script, int argc, const char **argv)
  277. {
  278. const char **command_line;
  279. char buf[PATH_MAX];
  280. int i, err = 0;
  281. FILE *fp;
  282. command_line = malloc((argc + 1) * sizeof(const char *));
  283. command_line[0] = script;
  284. for (i = 1; i < argc + 1; i++)
  285. command_line[i] = argv[i - 1];
  286. Py_Initialize();
  287. initperf_trace_context();
  288. PySys_SetArgv(argc + 1, (char **)command_line);
  289. fp = fopen(script, "r");
  290. if (!fp) {
  291. sprintf(buf, "Can't open python script \"%s\"", script);
  292. perror(buf);
  293. err = -1;
  294. goto error;
  295. }
  296. err = PyRun_SimpleFile(fp, script);
  297. if (err) {
  298. fprintf(stderr, "Error running python script %s\n", script);
  299. goto error;
  300. }
  301. err = run_start_sub();
  302. if (err) {
  303. fprintf(stderr, "Error starting python script %s\n", script);
  304. goto error;
  305. }
  306. free(command_line);
  307. fprintf(stderr, "perf trace started with Python script %s\n\n",
  308. script);
  309. return err;
  310. error:
  311. Py_Finalize();
  312. free(command_line);
  313. return err;
  314. }
  315. /*
  316. * Stop trace script
  317. */
  318. static int python_stop_script(void)
  319. {
  320. PyObject *handler, *retval;
  321. int err = 0;
  322. handler = PyDict_GetItemString(main_dict, "trace_end");
  323. if (handler == NULL || !PyCallable_Check(handler))
  324. goto out;
  325. retval = PyObject_CallObject(handler, NULL);
  326. if (retval == NULL)
  327. handler_call_die("trace_end");
  328. else
  329. Py_DECREF(retval);
  330. out:
  331. Py_XDECREF(main_dict);
  332. Py_XDECREF(main_module);
  333. Py_Finalize();
  334. fprintf(stderr, "\nperf trace Python script stopped\n");
  335. return err;
  336. }
  337. static int python_generate_script(const char *outfile)
  338. {
  339. struct event *event = NULL;
  340. struct format_field *f;
  341. char fname[PATH_MAX];
  342. int not_first, count;
  343. FILE *ofp;
  344. sprintf(fname, "%s.py", outfile);
  345. ofp = fopen(fname, "w");
  346. if (ofp == NULL) {
  347. fprintf(stderr, "couldn't open %s\n", fname);
  348. return -1;
  349. }
  350. fprintf(ofp, "# perf trace event handlers, "
  351. "generated by perf trace -g python\n");
  352. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  353. " License version 2\n\n");
  354. fprintf(ofp, "# The common_* event handler fields are the most useful "
  355. "fields common to\n");
  356. fprintf(ofp, "# all events. They don't necessarily correspond to "
  357. "the 'common_*' fields\n");
  358. fprintf(ofp, "# in the format files. Those fields not available as "
  359. "handler params can\n");
  360. fprintf(ofp, "# be retrieved using Python functions of the form "
  361. "common_*(context).\n");
  362. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  363. "of available functions.\n\n");
  364. fprintf(ofp, "import os\n");
  365. fprintf(ofp, "import sys\n\n");
  366. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  367. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  368. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  369. fprintf(ofp, "from Core import *\n\n\n");
  370. fprintf(ofp, "def trace_begin():\n");
  371. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  372. fprintf(ofp, "def trace_end():\n");
  373. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  374. while ((event = trace_find_next_event(event))) {
  375. fprintf(ofp, "def %s__%s(", event->system, event->name);
  376. fprintf(ofp, "event_name, ");
  377. fprintf(ofp, "context, ");
  378. fprintf(ofp, "common_cpu,\n");
  379. fprintf(ofp, "\tcommon_secs, ");
  380. fprintf(ofp, "common_nsecs, ");
  381. fprintf(ofp, "common_pid, ");
  382. fprintf(ofp, "common_comm,\n\t");
  383. not_first = 0;
  384. count = 0;
  385. for (f = event->format.fields; f; f = f->next) {
  386. if (not_first++)
  387. fprintf(ofp, ", ");
  388. if (++count % 5 == 0)
  389. fprintf(ofp, "\n\t");
  390. fprintf(ofp, "%s", f->name);
  391. }
  392. fprintf(ofp, "):\n");
  393. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  394. "common_secs, common_nsecs,\n\t\t\t"
  395. "common_pid, common_comm)\n\n");
  396. fprintf(ofp, "\t\tprint \"");
  397. not_first = 0;
  398. count = 0;
  399. for (f = event->format.fields; f; f = f->next) {
  400. if (not_first++)
  401. fprintf(ofp, ", ");
  402. if (count && count % 3 == 0) {
  403. fprintf(ofp, "\" \\\n\t\t\"");
  404. }
  405. count++;
  406. fprintf(ofp, "%s=", f->name);
  407. if (f->flags & FIELD_IS_STRING ||
  408. f->flags & FIELD_IS_FLAG ||
  409. f->flags & FIELD_IS_SYMBOLIC)
  410. fprintf(ofp, "%%s");
  411. else if (f->flags & FIELD_IS_SIGNED)
  412. fprintf(ofp, "%%d");
  413. else
  414. fprintf(ofp, "%%u");
  415. }
  416. fprintf(ofp, "\\n\" %% \\\n\t\t(");
  417. not_first = 0;
  418. count = 0;
  419. for (f = event->format.fields; f; f = f->next) {
  420. if (not_first++)
  421. fprintf(ofp, ", ");
  422. if (++count % 5 == 0)
  423. fprintf(ofp, "\n\t\t");
  424. if (f->flags & FIELD_IS_FLAG) {
  425. if ((count - 1) % 5 != 0) {
  426. fprintf(ofp, "\n\t\t");
  427. count = 4;
  428. }
  429. fprintf(ofp, "flag_str(\"");
  430. fprintf(ofp, "%s__%s\", ", event->system,
  431. event->name);
  432. fprintf(ofp, "\"%s\", %s)", f->name,
  433. f->name);
  434. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  435. if ((count - 1) % 5 != 0) {
  436. fprintf(ofp, "\n\t\t");
  437. count = 4;
  438. }
  439. fprintf(ofp, "symbol_str(\"");
  440. fprintf(ofp, "%s__%s\", ", event->system,
  441. event->name);
  442. fprintf(ofp, "\"%s\", %s)", f->name,
  443. f->name);
  444. } else
  445. fprintf(ofp, "%s", f->name);
  446. }
  447. fprintf(ofp, "),\n\n");
  448. }
  449. fprintf(ofp, "def trace_unhandled(event_name, context, "
  450. "common_cpu, common_secs, common_nsecs,\n\t\t"
  451. "common_pid, common_comm):\n");
  452. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  453. "common_secs, common_nsecs,\n\t\tcommon_pid, "
  454. "common_comm)\n\n");
  455. fprintf(ofp, "def print_header("
  456. "event_name, cpu, secs, nsecs, pid, comm):\n"
  457. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  458. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  459. fclose(ofp);
  460. fprintf(stderr, "generated Python script: %s\n", fname);
  461. return 0;
  462. }
  463. struct scripting_ops python_scripting_ops = {
  464. .name = "Python",
  465. .start_script = python_start_script,
  466. .stop_script = python_stop_script,
  467. .process_event = python_process_event,
  468. .generate_script = python_generate_script,
  469. };