trace-event-python.c 15 KB

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