trace-event-python.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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, *dict = NULL;
  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. handler = PyDict_GetItemString(main_dict, handler_name);
  195. if (handler && !PyCallable_Check(handler))
  196. handler = NULL;
  197. if (!handler) {
  198. dict = PyDict_New();
  199. if (!dict)
  200. Py_FatalError("couldn't create Python dict");
  201. }
  202. s = nsecs / NSECS_PER_SEC;
  203. ns = nsecs - s * NSECS_PER_SEC;
  204. scripting_context->event_data = data;
  205. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  206. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  207. PyTuple_SetItem(t, n++, context);
  208. if (handler) {
  209. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  210. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  211. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  212. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  213. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  214. } else {
  215. PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
  216. PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
  217. PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
  218. PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
  219. PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
  220. }
  221. for (field = event->format.fields; field; field = field->next) {
  222. if (field->flags & FIELD_IS_STRING) {
  223. int offset;
  224. if (field->flags & FIELD_IS_DYNAMIC) {
  225. offset = *(int *)(data + field->offset);
  226. offset &= 0xffff;
  227. } else
  228. offset = field->offset;
  229. obj = PyString_FromString((char *)data + offset);
  230. } else { /* FIELD_IS_NUMERIC */
  231. val = read_size(data + field->offset, field->size);
  232. if (field->flags & FIELD_IS_SIGNED) {
  233. if ((long long)val >= LONG_MIN &&
  234. (long long)val <= LONG_MAX)
  235. obj = PyInt_FromLong(val);
  236. else
  237. obj = PyLong_FromLongLong(val);
  238. } else {
  239. if (val <= LONG_MAX)
  240. obj = PyInt_FromLong(val);
  241. else
  242. obj = PyLong_FromUnsignedLongLong(val);
  243. }
  244. }
  245. if (handler)
  246. PyTuple_SetItem(t, n++, obj);
  247. else
  248. PyDict_SetItemString(dict, field->name, obj);
  249. }
  250. if (!handler)
  251. PyTuple_SetItem(t, n++, dict);
  252. if (_PyTuple_Resize(&t, n) == -1)
  253. Py_FatalError("error resizing Python tuple");
  254. if (handler) {
  255. retval = PyObject_CallObject(handler, t);
  256. if (retval == NULL)
  257. handler_call_die(handler_name);
  258. } else {
  259. handler = PyDict_GetItemString(main_dict, "trace_unhandled");
  260. if (handler && PyCallable_Check(handler)) {
  261. retval = PyObject_CallObject(handler, t);
  262. if (retval == NULL)
  263. handler_call_die("trace_unhandled");
  264. }
  265. Py_DECREF(dict);
  266. }
  267. Py_DECREF(t);
  268. }
  269. static int run_start_sub(void)
  270. {
  271. PyObject *handler, *retval;
  272. int err = 0;
  273. main_module = PyImport_AddModule("__main__");
  274. if (main_module == NULL)
  275. return -1;
  276. Py_INCREF(main_module);
  277. main_dict = PyModule_GetDict(main_module);
  278. if (main_dict == NULL) {
  279. err = -1;
  280. goto error;
  281. }
  282. Py_INCREF(main_dict);
  283. handler = PyDict_GetItemString(main_dict, "trace_begin");
  284. if (handler == NULL || !PyCallable_Check(handler))
  285. goto out;
  286. retval = PyObject_CallObject(handler, NULL);
  287. if (retval == NULL)
  288. handler_call_die("trace_begin");
  289. Py_DECREF(retval);
  290. return err;
  291. error:
  292. Py_XDECREF(main_dict);
  293. Py_XDECREF(main_module);
  294. out:
  295. return err;
  296. }
  297. /*
  298. * Start trace script
  299. */
  300. static int python_start_script(const char *script, int argc, const char **argv)
  301. {
  302. const char **command_line;
  303. char buf[PATH_MAX];
  304. int i, err = 0;
  305. FILE *fp;
  306. command_line = malloc((argc + 1) * sizeof(const char *));
  307. command_line[0] = script;
  308. for (i = 1; i < argc + 1; i++)
  309. command_line[i] = argv[i - 1];
  310. Py_Initialize();
  311. initperf_trace_context();
  312. PySys_SetArgv(argc + 1, (char **)command_line);
  313. fp = fopen(script, "r");
  314. if (!fp) {
  315. sprintf(buf, "Can't open python script \"%s\"", script);
  316. perror(buf);
  317. err = -1;
  318. goto error;
  319. }
  320. err = PyRun_SimpleFile(fp, script);
  321. if (err) {
  322. fprintf(stderr, "Error running python script %s\n", script);
  323. goto error;
  324. }
  325. err = run_start_sub();
  326. if (err) {
  327. fprintf(stderr, "Error starting python script %s\n", script);
  328. goto error;
  329. }
  330. free(command_line);
  331. return err;
  332. error:
  333. Py_Finalize();
  334. free(command_line);
  335. return err;
  336. }
  337. /*
  338. * Stop trace script
  339. */
  340. static int python_stop_script(void)
  341. {
  342. PyObject *handler, *retval;
  343. int err = 0;
  344. handler = PyDict_GetItemString(main_dict, "trace_end");
  345. if (handler == NULL || !PyCallable_Check(handler))
  346. goto out;
  347. retval = PyObject_CallObject(handler, NULL);
  348. if (retval == NULL)
  349. handler_call_die("trace_end");
  350. else
  351. Py_DECREF(retval);
  352. out:
  353. Py_XDECREF(main_dict);
  354. Py_XDECREF(main_module);
  355. Py_Finalize();
  356. return err;
  357. }
  358. static int python_generate_script(const char *outfile)
  359. {
  360. struct event *event = NULL;
  361. struct format_field *f;
  362. char fname[PATH_MAX];
  363. int not_first, count;
  364. FILE *ofp;
  365. sprintf(fname, "%s.py", outfile);
  366. ofp = fopen(fname, "w");
  367. if (ofp == NULL) {
  368. fprintf(stderr, "couldn't open %s\n", fname);
  369. return -1;
  370. }
  371. fprintf(ofp, "# perf script event handlers, "
  372. "generated by perf script -g python\n");
  373. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  374. " License version 2\n\n");
  375. fprintf(ofp, "# The common_* event handler fields are the most useful "
  376. "fields common to\n");
  377. fprintf(ofp, "# all events. They don't necessarily correspond to "
  378. "the 'common_*' fields\n");
  379. fprintf(ofp, "# in the format files. Those fields not available as "
  380. "handler params can\n");
  381. fprintf(ofp, "# be retrieved using Python functions of the form "
  382. "common_*(context).\n");
  383. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  384. "of available functions.\n\n");
  385. fprintf(ofp, "import os\n");
  386. fprintf(ofp, "import sys\n\n");
  387. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  388. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  389. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  390. fprintf(ofp, "from Core import *\n\n\n");
  391. fprintf(ofp, "def trace_begin():\n");
  392. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  393. fprintf(ofp, "def trace_end():\n");
  394. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  395. while ((event = trace_find_next_event(event))) {
  396. fprintf(ofp, "def %s__%s(", event->system, event->name);
  397. fprintf(ofp, "event_name, ");
  398. fprintf(ofp, "context, ");
  399. fprintf(ofp, "common_cpu,\n");
  400. fprintf(ofp, "\tcommon_secs, ");
  401. fprintf(ofp, "common_nsecs, ");
  402. fprintf(ofp, "common_pid, ");
  403. fprintf(ofp, "common_comm,\n\t");
  404. not_first = 0;
  405. count = 0;
  406. for (f = event->format.fields; f; f = f->next) {
  407. if (not_first++)
  408. fprintf(ofp, ", ");
  409. if (++count % 5 == 0)
  410. fprintf(ofp, "\n\t");
  411. fprintf(ofp, "%s", f->name);
  412. }
  413. fprintf(ofp, "):\n");
  414. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  415. "common_secs, common_nsecs,\n\t\t\t"
  416. "common_pid, common_comm)\n\n");
  417. fprintf(ofp, "\t\tprint \"");
  418. not_first = 0;
  419. count = 0;
  420. for (f = event->format.fields; f; f = f->next) {
  421. if (not_first++)
  422. fprintf(ofp, ", ");
  423. if (count && count % 3 == 0) {
  424. fprintf(ofp, "\" \\\n\t\t\"");
  425. }
  426. count++;
  427. fprintf(ofp, "%s=", f->name);
  428. if (f->flags & FIELD_IS_STRING ||
  429. f->flags & FIELD_IS_FLAG ||
  430. f->flags & FIELD_IS_SYMBOLIC)
  431. fprintf(ofp, "%%s");
  432. else if (f->flags & FIELD_IS_SIGNED)
  433. fprintf(ofp, "%%d");
  434. else
  435. fprintf(ofp, "%%u");
  436. }
  437. fprintf(ofp, "\\n\" %% \\\n\t\t(");
  438. not_first = 0;
  439. count = 0;
  440. for (f = event->format.fields; f; f = f->next) {
  441. if (not_first++)
  442. fprintf(ofp, ", ");
  443. if (++count % 5 == 0)
  444. fprintf(ofp, "\n\t\t");
  445. if (f->flags & FIELD_IS_FLAG) {
  446. if ((count - 1) % 5 != 0) {
  447. fprintf(ofp, "\n\t\t");
  448. count = 4;
  449. }
  450. fprintf(ofp, "flag_str(\"");
  451. fprintf(ofp, "%s__%s\", ", event->system,
  452. event->name);
  453. fprintf(ofp, "\"%s\", %s)", f->name,
  454. f->name);
  455. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  456. if ((count - 1) % 5 != 0) {
  457. fprintf(ofp, "\n\t\t");
  458. count = 4;
  459. }
  460. fprintf(ofp, "symbol_str(\"");
  461. fprintf(ofp, "%s__%s\", ", event->system,
  462. event->name);
  463. fprintf(ofp, "\"%s\", %s)", f->name,
  464. f->name);
  465. } else
  466. fprintf(ofp, "%s", f->name);
  467. }
  468. fprintf(ofp, "),\n\n");
  469. }
  470. fprintf(ofp, "def trace_unhandled(event_name, context, "
  471. "event_fields_dict):\n");
  472. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  473. "for k,v in sorted(event_fields_dict.items())])\n\n");
  474. fprintf(ofp, "def print_header("
  475. "event_name, cpu, secs, nsecs, pid, comm):\n"
  476. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  477. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  478. fclose(ofp);
  479. fprintf(stderr, "generated Python script: %s\n", fname);
  480. return 0;
  481. }
  482. struct scripting_ops python_scripting_ops = {
  483. .name = "Python",
  484. .start_script = python_start_script,
  485. .stop_script = python_stop_script,
  486. .process_event = python_process_event,
  487. .generate_script = python_generate_script,
  488. };