trace-event-python.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 <stdbool.h>
  26. #include <errno.h>
  27. #include <linux/bitmap.h>
  28. #include "../../perf.h"
  29. #include "../debug.h"
  30. #include "../callchain.h"
  31. #include "../evsel.h"
  32. #include "../util.h"
  33. #include "../event.h"
  34. #include "../thread.h"
  35. #include "../comm.h"
  36. #include "../machine.h"
  37. #include "../db-export.h"
  38. #include "../thread-stack.h"
  39. #include "../trace-event.h"
  40. #include "../machine.h"
  41. PyMODINIT_FUNC initperf_trace_context(void);
  42. #define TRACE_EVENT_TYPE_MAX \
  43. ((1 << (sizeof(unsigned short) * 8)) - 1)
  44. static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
  45. #define MAX_FIELDS 64
  46. #define N_COMMON_FIELDS 7
  47. extern struct scripting_context *scripting_context;
  48. static char *cur_field_name;
  49. static int zero_flag_atom;
  50. static PyObject *main_module, *main_dict;
  51. struct tables {
  52. struct db_export dbe;
  53. PyObject *evsel_handler;
  54. PyObject *machine_handler;
  55. PyObject *thread_handler;
  56. PyObject *comm_handler;
  57. PyObject *comm_thread_handler;
  58. PyObject *dso_handler;
  59. PyObject *symbol_handler;
  60. PyObject *branch_type_handler;
  61. PyObject *sample_handler;
  62. PyObject *call_path_handler;
  63. PyObject *call_return_handler;
  64. bool db_export_mode;
  65. };
  66. static struct tables tables_global;
  67. static void handler_call_die(const char *handler_name) NORETURN;
  68. static void handler_call_die(const char *handler_name)
  69. {
  70. PyErr_Print();
  71. Py_FatalError("problem in Python trace event handler");
  72. // Py_FatalError does not return
  73. // but we have to make the compiler happy
  74. abort();
  75. }
  76. /*
  77. * Insert val into into the dictionary and decrement the reference counter.
  78. * This is necessary for dictionaries since PyDict_SetItemString() does not
  79. * steal a reference, as opposed to PyTuple_SetItem().
  80. */
  81. static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
  82. {
  83. PyDict_SetItemString(dict, key, val);
  84. Py_DECREF(val);
  85. }
  86. static PyObject *get_handler(const char *handler_name)
  87. {
  88. PyObject *handler;
  89. handler = PyDict_GetItemString(main_dict, handler_name);
  90. if (handler && !PyCallable_Check(handler))
  91. return NULL;
  92. return handler;
  93. }
  94. static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
  95. {
  96. PyObject *retval;
  97. retval = PyObject_CallObject(handler, args);
  98. if (retval == NULL)
  99. handler_call_die(die_msg);
  100. Py_DECREF(retval);
  101. }
  102. static void try_call_object(const char *handler_name, PyObject *args)
  103. {
  104. PyObject *handler;
  105. handler = get_handler(handler_name);
  106. if (handler)
  107. call_object(handler, args, handler_name);
  108. }
  109. static void define_value(enum print_arg_type field_type,
  110. const char *ev_name,
  111. const char *field_name,
  112. const char *field_value,
  113. const char *field_str)
  114. {
  115. const char *handler_name = "define_flag_value";
  116. PyObject *t;
  117. unsigned long long value;
  118. unsigned n = 0;
  119. if (field_type == PRINT_SYMBOL)
  120. handler_name = "define_symbolic_value";
  121. t = PyTuple_New(4);
  122. if (!t)
  123. Py_FatalError("couldn't create Python tuple");
  124. value = eval_flag(field_value);
  125. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  126. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  127. PyTuple_SetItem(t, n++, PyInt_FromLong(value));
  128. PyTuple_SetItem(t, n++, PyString_FromString(field_str));
  129. try_call_object(handler_name, t);
  130. Py_DECREF(t);
  131. }
  132. static void define_values(enum print_arg_type field_type,
  133. struct print_flag_sym *field,
  134. const char *ev_name,
  135. const char *field_name)
  136. {
  137. define_value(field_type, ev_name, field_name, field->value,
  138. field->str);
  139. if (field->next)
  140. define_values(field_type, field->next, ev_name, field_name);
  141. }
  142. static void define_field(enum print_arg_type field_type,
  143. const char *ev_name,
  144. const char *field_name,
  145. const char *delim)
  146. {
  147. const char *handler_name = "define_flag_field";
  148. PyObject *t;
  149. unsigned n = 0;
  150. if (field_type == PRINT_SYMBOL)
  151. handler_name = "define_symbolic_field";
  152. if (field_type == PRINT_FLAGS)
  153. t = PyTuple_New(3);
  154. else
  155. t = PyTuple_New(2);
  156. if (!t)
  157. Py_FatalError("couldn't create Python tuple");
  158. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  159. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  160. if (field_type == PRINT_FLAGS)
  161. PyTuple_SetItem(t, n++, PyString_FromString(delim));
  162. try_call_object(handler_name, t);
  163. Py_DECREF(t);
  164. }
  165. static void define_event_symbols(struct event_format *event,
  166. const char *ev_name,
  167. struct print_arg *args)
  168. {
  169. switch (args->type) {
  170. case PRINT_NULL:
  171. break;
  172. case PRINT_ATOM:
  173. define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
  174. args->atom.atom);
  175. zero_flag_atom = 0;
  176. break;
  177. case PRINT_FIELD:
  178. free(cur_field_name);
  179. cur_field_name = strdup(args->field.name);
  180. break;
  181. case PRINT_FLAGS:
  182. define_event_symbols(event, ev_name, args->flags.field);
  183. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  184. args->flags.delim);
  185. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  186. cur_field_name);
  187. break;
  188. case PRINT_SYMBOL:
  189. define_event_symbols(event, ev_name, args->symbol.field);
  190. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  191. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  192. cur_field_name);
  193. break;
  194. case PRINT_HEX:
  195. define_event_symbols(event, ev_name, args->hex.field);
  196. define_event_symbols(event, ev_name, args->hex.size);
  197. break;
  198. case PRINT_INT_ARRAY:
  199. define_event_symbols(event, ev_name, args->int_array.field);
  200. define_event_symbols(event, ev_name, args->int_array.count);
  201. define_event_symbols(event, ev_name, args->int_array.el_size);
  202. break;
  203. case PRINT_STRING:
  204. break;
  205. case PRINT_TYPE:
  206. define_event_symbols(event, ev_name, args->typecast.item);
  207. break;
  208. case PRINT_OP:
  209. if (strcmp(args->op.op, ":") == 0)
  210. zero_flag_atom = 1;
  211. define_event_symbols(event, ev_name, args->op.left);
  212. define_event_symbols(event, ev_name, args->op.right);
  213. break;
  214. default:
  215. /* gcc warns for these? */
  216. case PRINT_BSTRING:
  217. case PRINT_DYNAMIC_ARRAY:
  218. case PRINT_DYNAMIC_ARRAY_LEN:
  219. case PRINT_FUNC:
  220. case PRINT_BITMASK:
  221. /* we should warn... */
  222. return;
  223. }
  224. if (args->next)
  225. define_event_symbols(event, ev_name, args->next);
  226. }
  227. static PyObject *get_field_numeric_entry(struct event_format *event,
  228. struct format_field *field, void *data)
  229. {
  230. bool is_array = field->flags & FIELD_IS_ARRAY;
  231. PyObject *obj, *list = NULL;
  232. unsigned long long val;
  233. unsigned int item_size, n_items, i;
  234. if (is_array) {
  235. list = PyList_New(field->arraylen);
  236. item_size = field->size / field->arraylen;
  237. n_items = field->arraylen;
  238. } else {
  239. item_size = field->size;
  240. n_items = 1;
  241. }
  242. for (i = 0; i < n_items; i++) {
  243. val = read_size(event, data + field->offset + i * item_size,
  244. item_size);
  245. if (field->flags & FIELD_IS_SIGNED) {
  246. if ((long long)val >= LONG_MIN &&
  247. (long long)val <= LONG_MAX)
  248. obj = PyInt_FromLong(val);
  249. else
  250. obj = PyLong_FromLongLong(val);
  251. } else {
  252. if (val <= LONG_MAX)
  253. obj = PyInt_FromLong(val);
  254. else
  255. obj = PyLong_FromUnsignedLongLong(val);
  256. }
  257. if (is_array)
  258. PyList_SET_ITEM(list, i, obj);
  259. }
  260. if (is_array)
  261. obj = list;
  262. return obj;
  263. }
  264. static PyObject *python_process_callchain(struct perf_sample *sample,
  265. struct perf_evsel *evsel,
  266. struct addr_location *al)
  267. {
  268. PyObject *pylist;
  269. pylist = PyList_New(0);
  270. if (!pylist)
  271. Py_FatalError("couldn't create Python list");
  272. if (!symbol_conf.use_callchain || !sample->callchain)
  273. goto exit;
  274. if (thread__resolve_callchain(al->thread, evsel,
  275. sample, NULL, NULL,
  276. scripting_max_stack) != 0) {
  277. pr_err("Failed to resolve callchain. Skipping\n");
  278. goto exit;
  279. }
  280. callchain_cursor_commit(&callchain_cursor);
  281. while (1) {
  282. PyObject *pyelem;
  283. struct callchain_cursor_node *node;
  284. node = callchain_cursor_current(&callchain_cursor);
  285. if (!node)
  286. break;
  287. pyelem = PyDict_New();
  288. if (!pyelem)
  289. Py_FatalError("couldn't create Python dictionary");
  290. pydict_set_item_string_decref(pyelem, "ip",
  291. PyLong_FromUnsignedLongLong(node->ip));
  292. if (node->sym) {
  293. PyObject *pysym = PyDict_New();
  294. if (!pysym)
  295. Py_FatalError("couldn't create Python dictionary");
  296. pydict_set_item_string_decref(pysym, "start",
  297. PyLong_FromUnsignedLongLong(node->sym->start));
  298. pydict_set_item_string_decref(pysym, "end",
  299. PyLong_FromUnsignedLongLong(node->sym->end));
  300. pydict_set_item_string_decref(pysym, "binding",
  301. PyInt_FromLong(node->sym->binding));
  302. pydict_set_item_string_decref(pysym, "name",
  303. PyString_FromStringAndSize(node->sym->name,
  304. node->sym->namelen));
  305. pydict_set_item_string_decref(pyelem, "sym", pysym);
  306. }
  307. if (node->map) {
  308. struct map *map = node->map;
  309. const char *dsoname = "[unknown]";
  310. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  311. if (symbol_conf.show_kernel_path && map->dso->long_name)
  312. dsoname = map->dso->long_name;
  313. else if (map->dso->name)
  314. dsoname = map->dso->name;
  315. }
  316. pydict_set_item_string_decref(pyelem, "dso",
  317. PyString_FromString(dsoname));
  318. }
  319. callchain_cursor_advance(&callchain_cursor);
  320. PyList_Append(pylist, pyelem);
  321. Py_DECREF(pyelem);
  322. }
  323. exit:
  324. return pylist;
  325. }
  326. static void python_process_tracepoint(struct perf_sample *sample,
  327. struct perf_evsel *evsel,
  328. struct addr_location *al)
  329. {
  330. struct event_format *event = evsel->tp_format;
  331. PyObject *handler, *context, *t, *obj, *callchain;
  332. PyObject *dict = NULL;
  333. static char handler_name[256];
  334. struct format_field *field;
  335. unsigned long s, ns;
  336. unsigned n = 0;
  337. int pid;
  338. int cpu = sample->cpu;
  339. void *data = sample->raw_data;
  340. unsigned long long nsecs = sample->time;
  341. const char *comm = thread__comm_str(al->thread);
  342. t = PyTuple_New(MAX_FIELDS);
  343. if (!t)
  344. Py_FatalError("couldn't create Python tuple");
  345. if (!event)
  346. die("ug! no event found for type %d", (int)evsel->attr.config);
  347. pid = raw_field_value(event, "common_pid", data);
  348. sprintf(handler_name, "%s__%s", event->system, event->name);
  349. if (!test_and_set_bit(event->id, events_defined))
  350. define_event_symbols(event, handler_name, event->print_fmt.args);
  351. handler = get_handler(handler_name);
  352. if (!handler) {
  353. dict = PyDict_New();
  354. if (!dict)
  355. Py_FatalError("couldn't create Python dict");
  356. }
  357. s = nsecs / NSECS_PER_SEC;
  358. ns = nsecs - s * NSECS_PER_SEC;
  359. scripting_context->event_data = data;
  360. scripting_context->pevent = evsel->tp_format->pevent;
  361. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  362. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  363. PyTuple_SetItem(t, n++, context);
  364. /* ip unwinding */
  365. callchain = python_process_callchain(sample, evsel, al);
  366. if (handler) {
  367. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  368. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  369. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  370. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  371. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  372. PyTuple_SetItem(t, n++, callchain);
  373. } else {
  374. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  375. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  376. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  377. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  378. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  379. pydict_set_item_string_decref(dict, "common_callchain", callchain);
  380. }
  381. for (field = event->format.fields; field; field = field->next) {
  382. if (field->flags & FIELD_IS_STRING) {
  383. int offset;
  384. if (field->flags & FIELD_IS_DYNAMIC) {
  385. offset = *(int *)(data + field->offset);
  386. offset &= 0xffff;
  387. } else
  388. offset = field->offset;
  389. obj = PyString_FromString((char *)data + offset);
  390. } else { /* FIELD_IS_NUMERIC */
  391. obj = get_field_numeric_entry(event, field, data);
  392. }
  393. if (handler)
  394. PyTuple_SetItem(t, n++, obj);
  395. else
  396. pydict_set_item_string_decref(dict, field->name, obj);
  397. }
  398. if (!handler)
  399. PyTuple_SetItem(t, n++, dict);
  400. if (_PyTuple_Resize(&t, n) == -1)
  401. Py_FatalError("error resizing Python tuple");
  402. if (handler) {
  403. call_object(handler, t, handler_name);
  404. } else {
  405. try_call_object("trace_unhandled", t);
  406. Py_DECREF(dict);
  407. }
  408. Py_DECREF(t);
  409. }
  410. static PyObject *tuple_new(unsigned int sz)
  411. {
  412. PyObject *t;
  413. t = PyTuple_New(sz);
  414. if (!t)
  415. Py_FatalError("couldn't create Python tuple");
  416. return t;
  417. }
  418. static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
  419. {
  420. #if BITS_PER_LONG == 64
  421. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  422. #endif
  423. #if BITS_PER_LONG == 32
  424. return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
  425. #endif
  426. }
  427. static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
  428. {
  429. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  430. }
  431. static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
  432. {
  433. return PyTuple_SetItem(t, pos, PyString_FromString(s));
  434. }
  435. static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
  436. {
  437. struct tables *tables = container_of(dbe, struct tables, dbe);
  438. PyObject *t;
  439. t = tuple_new(2);
  440. tuple_set_u64(t, 0, evsel->db_id);
  441. tuple_set_string(t, 1, perf_evsel__name(evsel));
  442. call_object(tables->evsel_handler, t, "evsel_table");
  443. Py_DECREF(t);
  444. return 0;
  445. }
  446. static int python_export_machine(struct db_export *dbe,
  447. struct machine *machine)
  448. {
  449. struct tables *tables = container_of(dbe, struct tables, dbe);
  450. PyObject *t;
  451. t = tuple_new(3);
  452. tuple_set_u64(t, 0, machine->db_id);
  453. tuple_set_s32(t, 1, machine->pid);
  454. tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
  455. call_object(tables->machine_handler, t, "machine_table");
  456. Py_DECREF(t);
  457. return 0;
  458. }
  459. static int python_export_thread(struct db_export *dbe, struct thread *thread,
  460. u64 main_thread_db_id, struct machine *machine)
  461. {
  462. struct tables *tables = container_of(dbe, struct tables, dbe);
  463. PyObject *t;
  464. t = tuple_new(5);
  465. tuple_set_u64(t, 0, thread->db_id);
  466. tuple_set_u64(t, 1, machine->db_id);
  467. tuple_set_u64(t, 2, main_thread_db_id);
  468. tuple_set_s32(t, 3, thread->pid_);
  469. tuple_set_s32(t, 4, thread->tid);
  470. call_object(tables->thread_handler, t, "thread_table");
  471. Py_DECREF(t);
  472. return 0;
  473. }
  474. static int python_export_comm(struct db_export *dbe, struct comm *comm)
  475. {
  476. struct tables *tables = container_of(dbe, struct tables, dbe);
  477. PyObject *t;
  478. t = tuple_new(2);
  479. tuple_set_u64(t, 0, comm->db_id);
  480. tuple_set_string(t, 1, comm__str(comm));
  481. call_object(tables->comm_handler, t, "comm_table");
  482. Py_DECREF(t);
  483. return 0;
  484. }
  485. static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
  486. struct comm *comm, struct thread *thread)
  487. {
  488. struct tables *tables = container_of(dbe, struct tables, dbe);
  489. PyObject *t;
  490. t = tuple_new(3);
  491. tuple_set_u64(t, 0, db_id);
  492. tuple_set_u64(t, 1, comm->db_id);
  493. tuple_set_u64(t, 2, thread->db_id);
  494. call_object(tables->comm_thread_handler, t, "comm_thread_table");
  495. Py_DECREF(t);
  496. return 0;
  497. }
  498. static int python_export_dso(struct db_export *dbe, struct dso *dso,
  499. struct machine *machine)
  500. {
  501. struct tables *tables = container_of(dbe, struct tables, dbe);
  502. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  503. PyObject *t;
  504. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  505. t = tuple_new(5);
  506. tuple_set_u64(t, 0, dso->db_id);
  507. tuple_set_u64(t, 1, machine->db_id);
  508. tuple_set_string(t, 2, dso->short_name);
  509. tuple_set_string(t, 3, dso->long_name);
  510. tuple_set_string(t, 4, sbuild_id);
  511. call_object(tables->dso_handler, t, "dso_table");
  512. Py_DECREF(t);
  513. return 0;
  514. }
  515. static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
  516. struct dso *dso)
  517. {
  518. struct tables *tables = container_of(dbe, struct tables, dbe);
  519. u64 *sym_db_id = symbol__priv(sym);
  520. PyObject *t;
  521. t = tuple_new(6);
  522. tuple_set_u64(t, 0, *sym_db_id);
  523. tuple_set_u64(t, 1, dso->db_id);
  524. tuple_set_u64(t, 2, sym->start);
  525. tuple_set_u64(t, 3, sym->end);
  526. tuple_set_s32(t, 4, sym->binding);
  527. tuple_set_string(t, 5, sym->name);
  528. call_object(tables->symbol_handler, t, "symbol_table");
  529. Py_DECREF(t);
  530. return 0;
  531. }
  532. static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
  533. const char *name)
  534. {
  535. struct tables *tables = container_of(dbe, struct tables, dbe);
  536. PyObject *t;
  537. t = tuple_new(2);
  538. tuple_set_s32(t, 0, branch_type);
  539. tuple_set_string(t, 1, name);
  540. call_object(tables->branch_type_handler, t, "branch_type_table");
  541. Py_DECREF(t);
  542. return 0;
  543. }
  544. static int python_export_sample(struct db_export *dbe,
  545. struct export_sample *es)
  546. {
  547. struct tables *tables = container_of(dbe, struct tables, dbe);
  548. PyObject *t;
  549. t = tuple_new(21);
  550. tuple_set_u64(t, 0, es->db_id);
  551. tuple_set_u64(t, 1, es->evsel->db_id);
  552. tuple_set_u64(t, 2, es->al->machine->db_id);
  553. tuple_set_u64(t, 3, es->al->thread->db_id);
  554. tuple_set_u64(t, 4, es->comm_db_id);
  555. tuple_set_u64(t, 5, es->dso_db_id);
  556. tuple_set_u64(t, 6, es->sym_db_id);
  557. tuple_set_u64(t, 7, es->offset);
  558. tuple_set_u64(t, 8, es->sample->ip);
  559. tuple_set_u64(t, 9, es->sample->time);
  560. tuple_set_s32(t, 10, es->sample->cpu);
  561. tuple_set_u64(t, 11, es->addr_dso_db_id);
  562. tuple_set_u64(t, 12, es->addr_sym_db_id);
  563. tuple_set_u64(t, 13, es->addr_offset);
  564. tuple_set_u64(t, 14, es->sample->addr);
  565. tuple_set_u64(t, 15, es->sample->period);
  566. tuple_set_u64(t, 16, es->sample->weight);
  567. tuple_set_u64(t, 17, es->sample->transaction);
  568. tuple_set_u64(t, 18, es->sample->data_src);
  569. tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
  570. tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
  571. call_object(tables->sample_handler, t, "sample_table");
  572. Py_DECREF(t);
  573. return 0;
  574. }
  575. static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
  576. {
  577. struct tables *tables = container_of(dbe, struct tables, dbe);
  578. PyObject *t;
  579. u64 parent_db_id, sym_db_id;
  580. parent_db_id = cp->parent ? cp->parent->db_id : 0;
  581. sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
  582. t = tuple_new(4);
  583. tuple_set_u64(t, 0, cp->db_id);
  584. tuple_set_u64(t, 1, parent_db_id);
  585. tuple_set_u64(t, 2, sym_db_id);
  586. tuple_set_u64(t, 3, cp->ip);
  587. call_object(tables->call_path_handler, t, "call_path_table");
  588. Py_DECREF(t);
  589. return 0;
  590. }
  591. static int python_export_call_return(struct db_export *dbe,
  592. struct call_return *cr)
  593. {
  594. struct tables *tables = container_of(dbe, struct tables, dbe);
  595. u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
  596. PyObject *t;
  597. t = tuple_new(11);
  598. tuple_set_u64(t, 0, cr->db_id);
  599. tuple_set_u64(t, 1, cr->thread->db_id);
  600. tuple_set_u64(t, 2, comm_db_id);
  601. tuple_set_u64(t, 3, cr->cp->db_id);
  602. tuple_set_u64(t, 4, cr->call_time);
  603. tuple_set_u64(t, 5, cr->return_time);
  604. tuple_set_u64(t, 6, cr->branch_count);
  605. tuple_set_u64(t, 7, cr->call_ref);
  606. tuple_set_u64(t, 8, cr->return_ref);
  607. tuple_set_u64(t, 9, cr->cp->parent->db_id);
  608. tuple_set_s32(t, 10, cr->flags);
  609. call_object(tables->call_return_handler, t, "call_return_table");
  610. Py_DECREF(t);
  611. return 0;
  612. }
  613. static int python_process_call_return(struct call_return *cr, void *data)
  614. {
  615. struct db_export *dbe = data;
  616. return db_export__call_return(dbe, cr);
  617. }
  618. static void python_process_general_event(struct perf_sample *sample,
  619. struct perf_evsel *evsel,
  620. struct addr_location *al)
  621. {
  622. PyObject *handler, *t, *dict, *callchain, *dict_sample;
  623. static char handler_name[64];
  624. unsigned n = 0;
  625. /*
  626. * Use the MAX_FIELDS to make the function expandable, though
  627. * currently there is only one item for the tuple.
  628. */
  629. t = PyTuple_New(MAX_FIELDS);
  630. if (!t)
  631. Py_FatalError("couldn't create Python tuple");
  632. dict = PyDict_New();
  633. if (!dict)
  634. Py_FatalError("couldn't create Python dictionary");
  635. dict_sample = PyDict_New();
  636. if (!dict_sample)
  637. Py_FatalError("couldn't create Python dictionary");
  638. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  639. handler = get_handler(handler_name);
  640. if (!handler)
  641. goto exit;
  642. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  643. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  644. (const char *)&evsel->attr, sizeof(evsel->attr)));
  645. pydict_set_item_string_decref(dict_sample, "pid",
  646. PyInt_FromLong(sample->pid));
  647. pydict_set_item_string_decref(dict_sample, "tid",
  648. PyInt_FromLong(sample->tid));
  649. pydict_set_item_string_decref(dict_sample, "cpu",
  650. PyInt_FromLong(sample->cpu));
  651. pydict_set_item_string_decref(dict_sample, "ip",
  652. PyLong_FromUnsignedLongLong(sample->ip));
  653. pydict_set_item_string_decref(dict_sample, "time",
  654. PyLong_FromUnsignedLongLong(sample->time));
  655. pydict_set_item_string_decref(dict_sample, "period",
  656. PyLong_FromUnsignedLongLong(sample->period));
  657. pydict_set_item_string_decref(dict, "sample", dict_sample);
  658. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  659. (const char *)sample->raw_data, sample->raw_size));
  660. pydict_set_item_string_decref(dict, "comm",
  661. PyString_FromString(thread__comm_str(al->thread)));
  662. if (al->map) {
  663. pydict_set_item_string_decref(dict, "dso",
  664. PyString_FromString(al->map->dso->name));
  665. }
  666. if (al->sym) {
  667. pydict_set_item_string_decref(dict, "symbol",
  668. PyString_FromString(al->sym->name));
  669. }
  670. /* ip unwinding */
  671. callchain = python_process_callchain(sample, evsel, al);
  672. pydict_set_item_string_decref(dict, "callchain", callchain);
  673. PyTuple_SetItem(t, n++, dict);
  674. if (_PyTuple_Resize(&t, n) == -1)
  675. Py_FatalError("error resizing Python tuple");
  676. call_object(handler, t, handler_name);
  677. exit:
  678. Py_DECREF(dict);
  679. Py_DECREF(t);
  680. }
  681. static void python_process_event(union perf_event *event,
  682. struct perf_sample *sample,
  683. struct perf_evsel *evsel,
  684. struct addr_location *al)
  685. {
  686. struct tables *tables = &tables_global;
  687. switch (evsel->attr.type) {
  688. case PERF_TYPE_TRACEPOINT:
  689. python_process_tracepoint(sample, evsel, al);
  690. break;
  691. /* Reserve for future process_hw/sw/raw APIs */
  692. default:
  693. if (tables->db_export_mode)
  694. db_export__sample(&tables->dbe, event, sample, evsel, al);
  695. else
  696. python_process_general_event(sample, evsel, al);
  697. }
  698. }
  699. static int run_start_sub(void)
  700. {
  701. main_module = PyImport_AddModule("__main__");
  702. if (main_module == NULL)
  703. return -1;
  704. Py_INCREF(main_module);
  705. main_dict = PyModule_GetDict(main_module);
  706. if (main_dict == NULL)
  707. goto error;
  708. Py_INCREF(main_dict);
  709. try_call_object("trace_begin", NULL);
  710. return 0;
  711. error:
  712. Py_XDECREF(main_dict);
  713. Py_XDECREF(main_module);
  714. return -1;
  715. }
  716. #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
  717. tables->handler_name = get_handler(#table_name); \
  718. if (tables->handler_name) \
  719. tables->dbe.export_ ## name = python_export_ ## name; \
  720. } while (0)
  721. #define SET_TABLE_HANDLER(name) \
  722. SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
  723. static void set_table_handlers(struct tables *tables)
  724. {
  725. const char *perf_db_export_mode = "perf_db_export_mode";
  726. const char *perf_db_export_calls = "perf_db_export_calls";
  727. PyObject *db_export_mode, *db_export_calls;
  728. bool export_calls = false;
  729. int ret;
  730. memset(tables, 0, sizeof(struct tables));
  731. if (db_export__init(&tables->dbe))
  732. Py_FatalError("failed to initialize export");
  733. db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
  734. if (!db_export_mode)
  735. return;
  736. ret = PyObject_IsTrue(db_export_mode);
  737. if (ret == -1)
  738. handler_call_die(perf_db_export_mode);
  739. if (!ret)
  740. return;
  741. tables->dbe.crp = NULL;
  742. db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
  743. if (db_export_calls) {
  744. ret = PyObject_IsTrue(db_export_calls);
  745. if (ret == -1)
  746. handler_call_die(perf_db_export_calls);
  747. export_calls = !!ret;
  748. }
  749. if (export_calls) {
  750. tables->dbe.crp =
  751. call_return_processor__new(python_process_call_return,
  752. &tables->dbe);
  753. if (!tables->dbe.crp)
  754. Py_FatalError("failed to create calls processor");
  755. }
  756. tables->db_export_mode = true;
  757. /*
  758. * Reserve per symbol space for symbol->db_id via symbol__priv()
  759. */
  760. symbol_conf.priv_size = sizeof(u64);
  761. SET_TABLE_HANDLER(evsel);
  762. SET_TABLE_HANDLER(machine);
  763. SET_TABLE_HANDLER(thread);
  764. SET_TABLE_HANDLER(comm);
  765. SET_TABLE_HANDLER(comm_thread);
  766. SET_TABLE_HANDLER(dso);
  767. SET_TABLE_HANDLER(symbol);
  768. SET_TABLE_HANDLER(branch_type);
  769. SET_TABLE_HANDLER(sample);
  770. SET_TABLE_HANDLER(call_path);
  771. SET_TABLE_HANDLER(call_return);
  772. }
  773. /*
  774. * Start trace script
  775. */
  776. static int python_start_script(const char *script, int argc, const char **argv)
  777. {
  778. struct tables *tables = &tables_global;
  779. const char **command_line;
  780. char buf[PATH_MAX];
  781. int i, err = 0;
  782. FILE *fp;
  783. command_line = malloc((argc + 1) * sizeof(const char *));
  784. command_line[0] = script;
  785. for (i = 1; i < argc + 1; i++)
  786. command_line[i] = argv[i - 1];
  787. Py_Initialize();
  788. initperf_trace_context();
  789. PySys_SetArgv(argc + 1, (char **)command_line);
  790. fp = fopen(script, "r");
  791. if (!fp) {
  792. sprintf(buf, "Can't open python script \"%s\"", script);
  793. perror(buf);
  794. err = -1;
  795. goto error;
  796. }
  797. err = PyRun_SimpleFile(fp, script);
  798. if (err) {
  799. fprintf(stderr, "Error running python script %s\n", script);
  800. goto error;
  801. }
  802. err = run_start_sub();
  803. if (err) {
  804. fprintf(stderr, "Error starting python script %s\n", script);
  805. goto error;
  806. }
  807. free(command_line);
  808. set_table_handlers(tables);
  809. if (tables->db_export_mode) {
  810. err = db_export__branch_types(&tables->dbe);
  811. if (err)
  812. goto error;
  813. }
  814. return err;
  815. error:
  816. Py_Finalize();
  817. free(command_line);
  818. return err;
  819. }
  820. static int python_flush_script(void)
  821. {
  822. struct tables *tables = &tables_global;
  823. return db_export__flush(&tables->dbe);
  824. }
  825. /*
  826. * Stop trace script
  827. */
  828. static int python_stop_script(void)
  829. {
  830. struct tables *tables = &tables_global;
  831. try_call_object("trace_end", NULL);
  832. db_export__exit(&tables->dbe);
  833. Py_XDECREF(main_dict);
  834. Py_XDECREF(main_module);
  835. Py_Finalize();
  836. return 0;
  837. }
  838. static int python_generate_script(struct pevent *pevent, const char *outfile)
  839. {
  840. struct event_format *event = NULL;
  841. struct format_field *f;
  842. char fname[PATH_MAX];
  843. int not_first, count;
  844. FILE *ofp;
  845. sprintf(fname, "%s.py", outfile);
  846. ofp = fopen(fname, "w");
  847. if (ofp == NULL) {
  848. fprintf(stderr, "couldn't open %s\n", fname);
  849. return -1;
  850. }
  851. fprintf(ofp, "# perf script event handlers, "
  852. "generated by perf script -g python\n");
  853. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  854. " License version 2\n\n");
  855. fprintf(ofp, "# The common_* event handler fields are the most useful "
  856. "fields common to\n");
  857. fprintf(ofp, "# all events. They don't necessarily correspond to "
  858. "the 'common_*' fields\n");
  859. fprintf(ofp, "# in the format files. Those fields not available as "
  860. "handler params can\n");
  861. fprintf(ofp, "# be retrieved using Python functions of the form "
  862. "common_*(context).\n");
  863. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  864. "of available functions.\n\n");
  865. fprintf(ofp, "import os\n");
  866. fprintf(ofp, "import sys\n\n");
  867. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  868. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  869. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  870. fprintf(ofp, "from Core import *\n\n\n");
  871. fprintf(ofp, "def trace_begin():\n");
  872. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  873. fprintf(ofp, "def trace_end():\n");
  874. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  875. while ((event = trace_find_next_event(pevent, event))) {
  876. fprintf(ofp, "def %s__%s(", event->system, event->name);
  877. fprintf(ofp, "event_name, ");
  878. fprintf(ofp, "context, ");
  879. fprintf(ofp, "common_cpu,\n");
  880. fprintf(ofp, "\tcommon_secs, ");
  881. fprintf(ofp, "common_nsecs, ");
  882. fprintf(ofp, "common_pid, ");
  883. fprintf(ofp, "common_comm,\n\t");
  884. fprintf(ofp, "common_callchain, ");
  885. not_first = 0;
  886. count = 0;
  887. for (f = event->format.fields; f; f = f->next) {
  888. if (not_first++)
  889. fprintf(ofp, ", ");
  890. if (++count % 5 == 0)
  891. fprintf(ofp, "\n\t");
  892. fprintf(ofp, "%s", f->name);
  893. }
  894. fprintf(ofp, "):\n");
  895. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  896. "common_secs, common_nsecs,\n\t\t\t"
  897. "common_pid, common_comm)\n\n");
  898. fprintf(ofp, "\t\tprint \"");
  899. not_first = 0;
  900. count = 0;
  901. for (f = event->format.fields; f; f = f->next) {
  902. if (not_first++)
  903. fprintf(ofp, ", ");
  904. if (count && count % 3 == 0) {
  905. fprintf(ofp, "\" \\\n\t\t\"");
  906. }
  907. count++;
  908. fprintf(ofp, "%s=", f->name);
  909. if (f->flags & FIELD_IS_STRING ||
  910. f->flags & FIELD_IS_FLAG ||
  911. f->flags & FIELD_IS_ARRAY ||
  912. f->flags & FIELD_IS_SYMBOLIC)
  913. fprintf(ofp, "%%s");
  914. else if (f->flags & FIELD_IS_SIGNED)
  915. fprintf(ofp, "%%d");
  916. else
  917. fprintf(ofp, "%%u");
  918. }
  919. fprintf(ofp, "\" %% \\\n\t\t(");
  920. not_first = 0;
  921. count = 0;
  922. for (f = event->format.fields; f; f = f->next) {
  923. if (not_first++)
  924. fprintf(ofp, ", ");
  925. if (++count % 5 == 0)
  926. fprintf(ofp, "\n\t\t");
  927. if (f->flags & FIELD_IS_FLAG) {
  928. if ((count - 1) % 5 != 0) {
  929. fprintf(ofp, "\n\t\t");
  930. count = 4;
  931. }
  932. fprintf(ofp, "flag_str(\"");
  933. fprintf(ofp, "%s__%s\", ", event->system,
  934. event->name);
  935. fprintf(ofp, "\"%s\", %s)", f->name,
  936. f->name);
  937. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  938. if ((count - 1) % 5 != 0) {
  939. fprintf(ofp, "\n\t\t");
  940. count = 4;
  941. }
  942. fprintf(ofp, "symbol_str(\"");
  943. fprintf(ofp, "%s__%s\", ", event->system,
  944. event->name);
  945. fprintf(ofp, "\"%s\", %s)", f->name,
  946. f->name);
  947. } else
  948. fprintf(ofp, "%s", f->name);
  949. }
  950. fprintf(ofp, ")\n\n");
  951. fprintf(ofp, "\t\tfor node in common_callchain:");
  952. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  953. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  954. fprintf(ofp, "\n\t\t\telse:");
  955. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  956. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  957. }
  958. fprintf(ofp, "def trace_unhandled(event_name, context, "
  959. "event_fields_dict):\n");
  960. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  961. "for k,v in sorted(event_fields_dict.items())])\n\n");
  962. fprintf(ofp, "def print_header("
  963. "event_name, cpu, secs, nsecs, pid, comm):\n"
  964. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  965. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  966. fclose(ofp);
  967. fprintf(stderr, "generated Python script: %s\n", fname);
  968. return 0;
  969. }
  970. struct scripting_ops python_scripting_ops = {
  971. .name = "Python",
  972. .start_script = python_start_script,
  973. .flush_script = python_flush_script,
  974. .stop_script = python_stop_script,
  975. .process_event = python_process_event,
  976. .generate_script = python_generate_script,
  977. };