1 /* Python interface to btrace instruction history.
3 Copyright 2016-2018 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"
32 #define BTPY_PYSLICE(x) (x)
36 #define BTPY_PYSLICE(x) ((PySliceObject *) x)
40 /* Python object for btrace record lists. */
45 /* The thread this list belongs to. */
48 /* The first index being part of this list. */
51 /* The last index begin part of this list. */
57 /* Either &BTPY_CALL_TYPE or &RECPY_INSN_TYPE. */
58 PyTypeObject* element_type;
61 /* Python type for btrace lists. */
63 static PyTypeObject btpy_list_type = {
64 PyVarObject_HEAD_INIT (NULL, 0)
67 /* Returns either a btrace_insn for the given Python gdb.RecordInstruction
68 object or sets an appropriate Python exception and returns NULL. */
70 static const btrace_insn *
71 btrace_insn_from_recpy_insn (const PyObject * const pyobject)
73 const btrace_insn *insn;
74 const recpy_element_object *obj;
76 btrace_insn_iterator iter;
78 if (Py_TYPE (pyobject) != &recpy_insn_type)
80 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction"));
84 obj = (const recpy_element_object *) pyobject;
87 if (tinfo == NULL || btrace_is_empty (tinfo))
89 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
93 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
95 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
99 insn = btrace_insn_get (&iter);
102 PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction."));
109 /* Returns either a btrace_function for the given Python
110 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
113 static const btrace_function *
114 btrace_func_from_recpy_func (const PyObject * const pyobject)
116 const btrace_function *func;
117 const recpy_element_object *obj;
119 btrace_call_iterator iter;
121 if (Py_TYPE (pyobject) != &recpy_func_type)
123 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment"));
127 obj = (const recpy_element_object *) pyobject;
130 if (tinfo == NULL || btrace_is_empty (tinfo))
132 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
136 if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0)
138 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
142 func = btrace_call_get (&iter);
145 PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment."));
152 /* Looks at the recorded item with the number NUMBER and create a
153 gdb.RecordInstruction or gdb.RecordGap object for it accordingly. */
156 btpy_insn_or_gap_new (thread_info *tinfo, Py_ssize_t number)
158 btrace_insn_iterator iter;
161 btrace_find_insn_by_number (&iter, &tinfo->btrace, number);
162 err_code = btrace_insn_get_error (&iter);
166 const btrace_config *config;
167 const char *err_string;
169 config = btrace_conf (&tinfo->btrace);
170 err_string = btrace_decode_error (config->format, err_code);
172 return recpy_gap_new (err_code, err_string, number);
175 return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number);
178 /* Create a new gdb.BtraceList object. */
181 btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last, Py_ssize_t step,
182 PyTypeObject *element_type)
184 btpy_list_object * const obj = PyObject_New (btpy_list_object,
190 obj->thread = thread;
194 obj->element_type = element_type;
196 return (PyObject *) obj;
199 /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
200 Returns the SAL associated with this instruction. */
203 recpy_bt_insn_sal (PyObject *self, void *closure)
205 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
206 PyObject *result = NULL;
213 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
215 CATCH (except, RETURN_MASK_ALL)
217 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 *buffer = NULL;
284 buffer = (gdb_byte *) xmalloc (insn->size);
285 read_memory (insn->pc, buffer, insn->size);
287 CATCH (except, RETURN_MASK_ALL)
290 GDB_PY_HANDLE_EXCEPTION (except);
294 object = PyBytes_FromStringAndSize ((const char*) buffer, insn->size);
301 return PyMemoryView_FromObject (object);
303 return PyBuffer_FromObject (object, 0, Py_END_OF_BUFFER);
308 /* Implementation of RecordInstruction.decoded [str] for btrace.
309 Returns the instruction as human readable string. */
312 recpy_bt_insn_decoded (PyObject *self, void *closure)
314 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
322 gdb_print_insn (target_gdbarch (), insn->pc, &strfile, NULL);
324 CATCH (except, RETURN_MASK_ALL)
326 gdbpy_convert_exception (except);
332 return PyBytes_FromString (strfile.string ().c_str ());
335 /* Implementation of RecordFunctionSegment.level [int] for btrace.
336 Returns the call level. */
339 recpy_bt_func_level (PyObject *self, void *closure)
341 const btrace_function * const func = btrace_func_from_recpy_func (self);
347 tinfo = ((recpy_element_object *) self)->thread;
348 return PyInt_FromLong (tinfo->btrace.level + func->level);
351 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
352 Returns the symbol associated with this function call. */
355 recpy_bt_func_symbol (PyObject *self, void *closure)
357 const btrace_function * const func = btrace_func_from_recpy_func (self);
362 if (func->sym == NULL)
365 return symbol_to_symbol_object (func->sym);
368 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
369 Returns the list of instructions that belong to this function call. */
372 recpy_bt_func_instructions (PyObject *self, void *closure)
374 const btrace_function * const func = btrace_func_from_recpy_func (self);
380 len = func->insn.size ();
382 /* Gaps count as one instruction. */
386 return btpy_list_new (((recpy_element_object *) self)->thread,
387 func->insn_offset, func->insn_offset + len, 1,
391 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
392 btrace. Returns the caller / returnee of this function. */
395 recpy_bt_func_up (PyObject *self, void *closure)
397 const btrace_function * const func = btrace_func_from_recpy_func (self);
405 return recpy_func_new (((recpy_element_object *) self)->thread,
406 RECORD_METHOD_BTRACE, func->up);
409 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
410 btrace. Returns a previous segment of this function. */
413 recpy_bt_func_prev (PyObject *self, void *closure)
415 const btrace_function * const func = btrace_func_from_recpy_func (self);
423 return recpy_func_new (((recpy_element_object *) self)->thread,
424 RECORD_METHOD_BTRACE, func->prev);
427 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
428 btrace. Returns a following segment of this function. */
431 recpy_bt_func_next (PyObject *self, void *closure)
433 const btrace_function * const func = btrace_func_from_recpy_func (self);
441 return recpy_func_new (((recpy_element_object *) self)->thread,
442 RECORD_METHOD_BTRACE, func->next);
445 /* Implementation of BtraceList.__len__ (self) -> int. */
448 btpy_list_length (PyObject *self)
450 const btpy_list_object * const obj = (btpy_list_object *) self;
451 const Py_ssize_t distance = obj->last - obj->first;
452 const Py_ssize_t result = distance / obj->step;
454 if ((distance % obj->step) == 0)
461 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
462 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
465 btpy_list_item (PyObject *self, Py_ssize_t index)
467 const btpy_list_object * const obj = (btpy_list_object *) self;
470 if (index < 0 || index >= btpy_list_length (self))
471 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
474 number = obj->first + (obj->step * index);
476 if (obj->element_type == &recpy_insn_type)
477 return recpy_insn_new (obj->thread, RECORD_METHOD_BTRACE, number);
479 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
482 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
485 btpy_list_slice (PyObject *self, PyObject *value)
487 const btpy_list_object * const obj = (btpy_list_object *) self;
488 const Py_ssize_t length = btpy_list_length (self);
489 Py_ssize_t start, stop, step, slicelength;
491 if (PyInt_Check (value))
493 Py_ssize_t index = PyInt_AsSsize_t (value);
495 /* Emulate Python behavior for negative indices. */
499 return btpy_list_item (self, index);
502 if (!PySlice_Check (value))
503 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
505 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start, &stop,
506 &step, &slicelength))
509 return btpy_list_new (obj->thread, obj->first + obj->step * start,
510 obj->first + obj->step * stop, obj->step * step,
514 /* Helper function that returns the position of an element in a BtraceList
515 or -1 if the element is not in the list. */
518 btpy_list_position (PyObject *self, PyObject *value)
520 const btpy_list_object * const list_obj = (btpy_list_object *) self;
521 const recpy_element_object * const obj = (const recpy_element_object *) value;
522 Py_ssize_t index = obj->number;
524 if (list_obj->element_type != Py_TYPE (value))
527 if (list_obj->thread != obj->thread)
530 if (index < list_obj->first || index > list_obj->last)
533 index -= list_obj->first;
535 if (index % list_obj->step != 0)
538 return index / list_obj->step;
541 /* Implementation of "in" operator for BtraceLists. */
544 btpy_list_contains (PyObject *self, PyObject *value)
546 if (btpy_list_position (self, value) < 0)
552 /* Implementation of BtraceLists.index (self, value) -> int. */
555 btpy_list_index (PyObject *self, PyObject *value)
557 const LONGEST index = btpy_list_position (self, value);
560 return PyErr_Format (PyExc_ValueError, _("Not in list."));
562 return gdb_py_long_from_longest (index);
565 /* Implementation of BtraceList.count (self, value) -> int. */
568 btpy_list_count (PyObject *self, PyObject *value)
570 /* We know that if an element is in the list, it is so exactly one time,
571 enabling us to reuse the "is element of" check. */
572 return PyInt_FromLong (btpy_list_contains (self, value));
575 /* Python rich compare function to allow for equality and inequality checks
579 btpy_list_richcompare (PyObject *self, PyObject *other, int op)
581 const btpy_list_object * const obj1 = (btpy_list_object *) self;
582 const btpy_list_object * const obj2 = (btpy_list_object *) other;
584 if (Py_TYPE (self) != Py_TYPE (other))
586 Py_INCREF (Py_NotImplemented);
587 return Py_NotImplemented;
593 if (obj1->thread == obj2->thread
594 && obj1->element_type == obj2->element_type
595 && obj1->first == obj2->first
596 && obj1->last == obj2->last
597 && obj1->step == obj2->step)
603 if (obj1->thread != obj2->thread
604 || obj1->element_type != obj2->element_type
605 || obj1->first != obj2->first
606 || obj1->last != obj2->last
607 || obj1->step != obj2->step)
616 Py_INCREF (Py_NotImplemented);
617 return Py_NotImplemented;
621 BtraceRecord.method [str]. */
624 recpy_bt_method (PyObject *self, void *closure)
626 return PyString_FromString ("btrace");
630 BtraceRecord.format [str]. */
633 recpy_bt_format (PyObject *self, void *closure)
635 const recpy_record_object * const record = (recpy_record_object *) self;
636 const struct thread_info * const tinfo = record->thread;
637 const struct btrace_config * config;
642 config = btrace_conf (&tinfo->btrace);
647 return PyString_FromString (btrace_format_short_string (config->format));
651 BtraceRecord.replay_position [BtraceInstruction]. */
654 recpy_bt_replay_position (PyObject *self, void *closure)
656 const recpy_record_object * const record = (recpy_record_object *) self;
657 thread_info * tinfo = record->thread;
662 if (tinfo->btrace.replay == NULL)
665 return btpy_insn_or_gap_new (tinfo,
666 btrace_insn_number (tinfo->btrace.replay));
670 BtraceRecord.begin [BtraceInstruction]. */
673 recpy_bt_begin (PyObject *self, void *closure)
675 const recpy_record_object * const record = (recpy_record_object *) self;
676 thread_info *const tinfo = record->thread;
677 struct btrace_insn_iterator iterator;
682 btrace_fetch (tinfo, record_btrace_get_cpu ());
684 if (btrace_is_empty (tinfo))
687 btrace_insn_begin (&iterator, &tinfo->btrace);
688 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
692 BtraceRecord.end [BtraceInstruction]. */
695 recpy_bt_end (PyObject *self, void *closure)
697 const recpy_record_object * const record = (recpy_record_object *) self;
698 thread_info *const tinfo = record->thread;
699 struct btrace_insn_iterator iterator;
704 btrace_fetch (tinfo, record_btrace_get_cpu ());
706 if (btrace_is_empty (tinfo))
709 btrace_insn_end (&iterator, &tinfo->btrace);
710 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
714 BtraceRecord.instruction_history [list]. */
717 recpy_bt_instruction_history (PyObject *self, void *closure)
719 const recpy_record_object * const record = (recpy_record_object *) self;
720 thread_info *const tinfo = record->thread;
721 struct btrace_insn_iterator iterator;
722 unsigned long first = 0;
723 unsigned long last = 0;
728 btrace_fetch (tinfo, record_btrace_get_cpu ());
730 if (btrace_is_empty (tinfo))
733 btrace_insn_begin (&iterator, &tinfo->btrace);
734 first = btrace_insn_number (&iterator);
736 btrace_insn_end (&iterator, &tinfo->btrace);
737 last = btrace_insn_number (&iterator);
739 return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type);
743 BtraceRecord.function_call_history [list]. */
746 recpy_bt_function_call_history (PyObject *self, void *closure)
748 const recpy_record_object * const record = (recpy_record_object *) self;
749 thread_info *const tinfo = record->thread;
750 struct btrace_call_iterator iterator;
751 unsigned long first = 0;
752 unsigned long last = 0;
757 btrace_fetch (tinfo, record_btrace_get_cpu ());
759 if (btrace_is_empty (tinfo))
762 btrace_call_begin (&iterator, &tinfo->btrace);
763 first = btrace_call_number (&iterator);
765 btrace_call_end (&iterator, &tinfo->btrace);
766 last = btrace_call_number (&iterator);
768 return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
771 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
774 recpy_bt_goto (PyObject *self, PyObject *args)
776 const recpy_record_object * const record = (recpy_record_object *) self;
777 thread_info *const tinfo = record->thread;
778 const recpy_element_object *obj;
781 if (tinfo == NULL || btrace_is_empty (tinfo))
782 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
784 if (!PyArg_ParseTuple (args, "O", &parse_obj))
787 if (Py_TYPE (parse_obj) != &recpy_insn_type)
788 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
789 obj = (const recpy_element_object *) parse_obj;
793 struct btrace_insn_iterator iter;
795 btrace_insn_end (&iter, &tinfo->btrace);
797 if (btrace_insn_number (&iter) == obj->number)
798 target_goto_record_end ();
800 target_goto_record (obj->number);
802 CATCH (except, RETURN_MASK_ALL)
804 GDB_PY_HANDLE_EXCEPTION (except);
811 /* BtraceList methods. */
813 struct PyMethodDef btpy_list_methods[] =
815 { "count", btpy_list_count, METH_O, "count number of occurences"},
816 { "index", btpy_list_index, METH_O, "index of entry"},
820 /* BtraceList sequence methods. */
822 static PySequenceMethods btpy_list_sequence_methods =
827 /* BtraceList mapping methods. Necessary for slicing. */
829 static PyMappingMethods btpy_list_mapping_methods =
834 /* Sets up the btrace record API. */
837 gdbpy_initialize_btrace (void)
839 btpy_list_type.tp_new = PyType_GenericNew;
840 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
841 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
842 btpy_list_type.tp_name = "gdb.BtraceObjectList";
843 btpy_list_type.tp_doc = "GDB btrace list object";
844 btpy_list_type.tp_methods = btpy_list_methods;
845 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
846 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
847 btpy_list_type.tp_richcompare = btpy_list_richcompare;
849 btpy_list_sequence_methods.sq_item = btpy_list_item;
850 btpy_list_sequence_methods.sq_length = btpy_list_length;
851 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
853 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
855 return PyType_Ready (&btpy_list_type);