trace-event-python.c 30 KB

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