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