trace-event-python.c 15 KB

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