trace-event-python.c 15 KB

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