1 /* Python interface to btrace instruction history.
3 Copyright 2016-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbthread.h"
25 #include "py-record.h"
26 #include "py-record-btrace.h"
27 #include "record-btrace.h"
33 #define BTPY_PYSLICE(x) (x)
37 #define BTPY_PYSLICE(x) ((PySliceObject *) x)
41 /* Python object for btrace record lists. */
46 /* The thread this list belongs to. */
49 /* The first index being part of this list. */
52 /* The last index begin part of this list. */
58 /* Either &BTPY_CALL_TYPE or &RECPY_INSN_TYPE. */
59 PyTypeObject* element_type;
62 /* Python type for btrace lists. */
64 static PyTypeObject btpy_list_type = {
65 PyVarObject_HEAD_INIT (NULL, 0)
68 /* Returns either a btrace_insn for the given Python gdb.RecordInstruction
69 object or sets an appropriate Python exception and returns NULL. */
71 static const btrace_insn *
72 btrace_insn_from_recpy_insn (const PyObject * const pyobject)
74 const btrace_insn *insn;
75 const recpy_element_object *obj;
77 btrace_insn_iterator iter;
79 if (Py_TYPE (pyobject) != &recpy_insn_type)
81 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction"));
85 obj = (const recpy_element_object *) pyobject;
88 if (tinfo == NULL || btrace_is_empty (tinfo))
90 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
94 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
96 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
100 insn = btrace_insn_get (&iter);
103 PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction."));
110 /* Returns either a btrace_function for the given Python
111 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
114 static const btrace_function *
115 btrace_func_from_recpy_func (const PyObject * const pyobject)
117 const btrace_function *func;
118 const recpy_element_object *obj;
120 btrace_call_iterator iter;
122 if (Py_TYPE (pyobject) != &recpy_func_type)
124 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment"));
128 obj = (const recpy_element_object *) pyobject;
131 if (tinfo == NULL || btrace_is_empty (tinfo))
133 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
137 if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0)
139 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
143 func = btrace_call_get (&iter);
146 PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment."));
153 /* Looks at the recorded item with the number NUMBER and create a
154 gdb.RecordInstruction or gdb.RecordGap object for it accordingly. */
157 btpy_insn_or_gap_new (thread_info *tinfo, Py_ssize_t number)
159 btrace_insn_iterator iter;
162 btrace_find_insn_by_number (&iter, &tinfo->btrace, number);
163 err_code = btrace_insn_get_error (&iter);
167 const btrace_config *config;
168 const char *err_string;
170 config = btrace_conf (&tinfo->btrace);
171 err_string = btrace_decode_error (config->format, err_code);
173 return recpy_gap_new (err_code, err_string, number);
176 return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number);
179 /* Create a new gdb.BtraceList object. */
182 btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last, Py_ssize_t step,
183 PyTypeObject *element_type)
185 btpy_list_object * const obj = PyObject_New (btpy_list_object,
191 obj->thread = thread;
195 obj->element_type = element_type;
197 return (PyObject *) obj;
200 /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
201 Returns the SAL associated with this instruction. */
204 recpy_bt_insn_sal (PyObject *self, void *closure)
206 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
207 PyObject *result = NULL;
214 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
216 catch (const gdb_exception &except)
218 GDB_PY_HANDLE_EXCEPTION (except);
224 /* Implementation of RecordInstruction.pc [int] for btrace.
225 Returns the instruction address. */
228 recpy_bt_insn_pc (PyObject *self, void *closure)
230 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
235 return gdb_py_long_from_ulongest (insn->pc);
238 /* Implementation of RecordInstruction.size [int] for btrace.
239 Returns the instruction size. */
242 recpy_bt_insn_size (PyObject *self, void *closure)
244 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
249 return PyInt_FromLong (insn->size);
252 /* Implementation of RecordInstruction.is_speculative [bool] for btrace.
253 Returns if this instruction was executed speculatively. */
256 recpy_bt_insn_is_speculative (PyObject *self, void *closure)
258 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
263 if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
269 /* Implementation of RecordInstruction.data [buffer] for btrace.
270 Returns raw instruction data. */
273 recpy_bt_insn_data (PyObject *self, void *closure)
275 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
276 gdb::byte_vector buffer;
284 buffer.resize (insn->size);
285 read_memory (insn->pc, buffer.data (), insn->size);
287 catch (const gdb_exception &except)
289 GDB_PY_HANDLE_EXCEPTION (except);
292 object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
299 return PyMemoryView_FromObject (object);
301 return PyBuffer_FromObject (object, 0, Py_END_OF_BUFFER);
306 /* Implementation of RecordInstruction.decoded [str] for btrace.
307 Returns the instruction as human readable string. */
310 recpy_bt_insn_decoded (PyObject *self, void *closure)
312 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
320 gdb_print_insn (target_gdbarch (), insn->pc, &strfile, NULL);
322 catch (const gdb_exception &except)
324 gdbpy_convert_exception (except);
329 return PyBytes_FromString (strfile.string ().c_str ());
332 /* Implementation of RecordFunctionSegment.level [int] for btrace.
333 Returns the call level. */
336 recpy_bt_func_level (PyObject *self, void *closure)
338 const btrace_function * const func = btrace_func_from_recpy_func (self);
344 tinfo = ((recpy_element_object *) self)->thread;
345 return PyInt_FromLong (tinfo->btrace.level + func->level);
348 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
349 Returns the symbol associated with this function call. */
352 recpy_bt_func_symbol (PyObject *self, void *closure)
354 const btrace_function * const func = btrace_func_from_recpy_func (self);
359 if (func->sym == NULL)
362 return symbol_to_symbol_object (func->sym);
365 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
366 Returns the list of instructions that belong to this function call. */
369 recpy_bt_func_instructions (PyObject *self, void *closure)
371 const btrace_function * const func = btrace_func_from_recpy_func (self);
377 len = func->insn.size ();
379 /* Gaps count as one instruction. */
383 return btpy_list_new (((recpy_element_object *) self)->thread,
384 func->insn_offset, func->insn_offset + len, 1,
388 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
389 btrace. Returns the caller / returnee of this function. */
392 recpy_bt_func_up (PyObject *self, void *closure)
394 const btrace_function * const func = btrace_func_from_recpy_func (self);
402 return recpy_func_new (((recpy_element_object *) self)->thread,
403 RECORD_METHOD_BTRACE, func->up);
406 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
407 btrace. Returns a previous segment of this function. */
410 recpy_bt_func_prev (PyObject *self, void *closure)
412 const btrace_function * const func = btrace_func_from_recpy_func (self);
420 return recpy_func_new (((recpy_element_object *) self)->thread,
421 RECORD_METHOD_BTRACE, func->prev);
424 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
425 btrace. Returns a following segment of this function. */
428 recpy_bt_func_next (PyObject *self, void *closure)
430 const btrace_function * const func = btrace_func_from_recpy_func (self);
438 return recpy_func_new (((recpy_element_object *) self)->thread,
439 RECORD_METHOD_BTRACE, func->next);
442 /* Implementation of BtraceList.__len__ (self) -> int. */
445 btpy_list_length (PyObject *self)
447 const btpy_list_object * const obj = (btpy_list_object *) self;
448 const Py_ssize_t distance = obj->last - obj->first;
449 const Py_ssize_t result = distance / obj->step;
451 if ((distance % obj->step) == 0)
458 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
459 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
462 btpy_list_item (PyObject *self, Py_ssize_t index)
464 const btpy_list_object * const obj = (btpy_list_object *) self;
467 if (index < 0 || index >= btpy_list_length (self))
468 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
471 number = obj->first + (obj->step * index);
473 if (obj->element_type == &recpy_insn_type)
474 return recpy_insn_new (obj->thread, RECORD_METHOD_BTRACE, number);
476 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
479 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
482 btpy_list_slice (PyObject *self, PyObject *value)
484 const btpy_list_object * const obj = (btpy_list_object *) self;
485 const Py_ssize_t length = btpy_list_length (self);
486 Py_ssize_t start, stop, step, slicelength;
488 if (PyInt_Check (value))
490 Py_ssize_t index = PyInt_AsSsize_t (value);
492 /* Emulate Python behavior for negative indices. */
496 return btpy_list_item (self, index);
499 if (!PySlice_Check (value))
500 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
502 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start, &stop,
503 &step, &slicelength))
506 return btpy_list_new (obj->thread, obj->first + obj->step * start,
507 obj->first + obj->step * stop, obj->step * step,
511 /* Helper function that returns the position of an element in a BtraceList
512 or -1 if the element is not in the list. */
515 btpy_list_position (PyObject *self, PyObject *value)
517 const btpy_list_object * const list_obj = (btpy_list_object *) self;
518 const recpy_element_object * const obj = (const recpy_element_object *) value;
519 Py_ssize_t index = obj->number;
521 if (list_obj->element_type != Py_TYPE (value))
524 if (list_obj->thread != obj->thread)
527 if (index < list_obj->first || index > list_obj->last)
530 index -= list_obj->first;
532 if (index % list_obj->step != 0)
535 return index / list_obj->step;
538 /* Implementation of "in" operator for BtraceLists. */
541 btpy_list_contains (PyObject *self, PyObject *value)
543 if (btpy_list_position (self, value) < 0)
549 /* Implementation of BtraceLists.index (self, value) -> int. */
552 btpy_list_index (PyObject *self, PyObject *value)
554 const LONGEST index = btpy_list_position (self, value);
557 return PyErr_Format (PyExc_ValueError, _("Not in list."));
559 return gdb_py_long_from_longest (index);
562 /* Implementation of BtraceList.count (self, value) -> int. */
565 btpy_list_count (PyObject *self, PyObject *value)
567 /* We know that if an element is in the list, it is so exactly one time,
568 enabling us to reuse the "is element of" check. */
569 return PyInt_FromLong (btpy_list_contains (self, value));
572 /* Python rich compare function to allow for equality and inequality checks
576 btpy_list_richcompare (PyObject *self, PyObject *other, int op)
578 const btpy_list_object * const obj1 = (btpy_list_object *) self;
579 const btpy_list_object * const obj2 = (btpy_list_object *) other;
581 if (Py_TYPE (self) != Py_TYPE (other))
583 Py_INCREF (Py_NotImplemented);
584 return Py_NotImplemented;
590 if (obj1->thread == obj2->thread
591 && obj1->element_type == obj2->element_type
592 && obj1->first == obj2->first
593 && obj1->last == obj2->last
594 && obj1->step == obj2->step)
600 if (obj1->thread != obj2->thread
601 || obj1->element_type != obj2->element_type
602 || obj1->first != obj2->first
603 || obj1->last != obj2->last
604 || obj1->step != obj2->step)
613 Py_INCREF (Py_NotImplemented);
614 return Py_NotImplemented;
618 BtraceRecord.method [str]. */
621 recpy_bt_method (PyObject *self, void *closure)
623 return PyString_FromString ("btrace");
627 BtraceRecord.format [str]. */
630 recpy_bt_format (PyObject *self, void *closure)
632 const recpy_record_object * const record = (recpy_record_object *) self;
633 const struct thread_info * const tinfo = record->thread;
634 const struct btrace_config * config;
639 config = btrace_conf (&tinfo->btrace);
644 return PyString_FromString (btrace_format_short_string (config->format));
648 BtraceRecord.replay_position [BtraceInstruction]. */
651 recpy_bt_replay_position (PyObject *self, void *closure)
653 const recpy_record_object * const record = (recpy_record_object *) self;
654 thread_info * tinfo = record->thread;
659 if (tinfo->btrace.replay == NULL)
662 return btpy_insn_or_gap_new (tinfo,
663 btrace_insn_number (tinfo->btrace.replay));
667 BtraceRecord.begin [BtraceInstruction]. */
670 recpy_bt_begin (PyObject *self, void *closure)
672 const recpy_record_object * const record = (recpy_record_object *) self;
673 thread_info *const tinfo = record->thread;
674 struct btrace_insn_iterator iterator;
679 btrace_fetch (tinfo, record_btrace_get_cpu ());
681 if (btrace_is_empty (tinfo))
684 btrace_insn_begin (&iterator, &tinfo->btrace);
685 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
689 BtraceRecord.end [BtraceInstruction]. */
692 recpy_bt_end (PyObject *self, void *closure)
694 const recpy_record_object * const record = (recpy_record_object *) self;
695 thread_info *const tinfo = record->thread;
696 struct btrace_insn_iterator iterator;
701 btrace_fetch (tinfo, record_btrace_get_cpu ());
703 if (btrace_is_empty (tinfo))
706 btrace_insn_end (&iterator, &tinfo->btrace);
707 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
711 BtraceRecord.instruction_history [list]. */
714 recpy_bt_instruction_history (PyObject *self, void *closure)
716 const recpy_record_object * const record = (recpy_record_object *) self;
717 thread_info *const tinfo = record->thread;
718 struct btrace_insn_iterator iterator;
719 unsigned long first = 0;
720 unsigned long last = 0;
725 btrace_fetch (tinfo, record_btrace_get_cpu ());
727 if (btrace_is_empty (tinfo))
730 btrace_insn_begin (&iterator, &tinfo->btrace);
731 first = btrace_insn_number (&iterator);
733 btrace_insn_end (&iterator, &tinfo->btrace);
734 last = btrace_insn_number (&iterator);
736 return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type);
740 BtraceRecord.function_call_history [list]. */
743 recpy_bt_function_call_history (PyObject *self, void *closure)
745 const recpy_record_object * const record = (recpy_record_object *) self;
746 thread_info *const tinfo = record->thread;
747 struct btrace_call_iterator iterator;
748 unsigned long first = 0;
749 unsigned long last = 0;
754 btrace_fetch (tinfo, record_btrace_get_cpu ());
756 if (btrace_is_empty (tinfo))
759 btrace_call_begin (&iterator, &tinfo->btrace);
760 first = btrace_call_number (&iterator);
762 btrace_call_end (&iterator, &tinfo->btrace);
763 last = btrace_call_number (&iterator);
765 return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
768 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
771 recpy_bt_goto (PyObject *self, PyObject *args)
773 const recpy_record_object * const record = (recpy_record_object *) self;
774 thread_info *const tinfo = record->thread;
775 const recpy_element_object *obj;
778 if (tinfo == NULL || btrace_is_empty (tinfo))
779 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
781 if (!PyArg_ParseTuple (args, "O", &parse_obj))
784 if (Py_TYPE (parse_obj) != &recpy_insn_type)
785 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
786 obj = (const recpy_element_object *) parse_obj;
790 struct btrace_insn_iterator iter;
792 btrace_insn_end (&iter, &tinfo->btrace);
794 if (btrace_insn_number (&iter) == obj->number)
795 target_goto_record_end ();
797 target_goto_record (obj->number);
799 catch (const gdb_exception &except)
801 GDB_PY_HANDLE_EXCEPTION (except);
807 /* BtraceList methods. */
809 struct PyMethodDef btpy_list_methods[] =
811 { "count", btpy_list_count, METH_O, "count number of occurences"},
812 { "index", btpy_list_index, METH_O, "index of entry"},
816 /* BtraceList sequence methods. */
818 static PySequenceMethods btpy_list_sequence_methods =
823 /* BtraceList mapping methods. Necessary for slicing. */
825 static PyMappingMethods btpy_list_mapping_methods =
830 /* Sets up the btrace record API. */
833 gdbpy_initialize_btrace (void)
835 btpy_list_type.tp_new = PyType_GenericNew;
836 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
837 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
838 btpy_list_type.tp_name = "gdb.BtraceObjectList";
839 btpy_list_type.tp_doc = "GDB btrace list object";
840 btpy_list_type.tp_methods = btpy_list_methods;
841 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
842 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
843 btpy_list_type.tp_richcompare = btpy_list_richcompare;
845 btpy_list_sequence_methods.sq_item = btpy_list_item;
846 btpy_list_sequence_methods.sq_length = btpy_list_length;
847 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
849 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
851 return PyType_Ready (&btpy_list_type);