trace-event-python.c 30 KB

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