trace-event-python.c 33 KB

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