1 /* Python interface to btrace instruction history.
3 Copyright 2016-2020 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_object_from_ulongest (insn
->pc
).release ();
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 gdb_py_object_from_longest (insn
->size
).release ();
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 gdb_py_object_from_longest (tinfo
->btrace
.level
346 + func
->level
).release ();
349 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
350 Returns the symbol associated with this function call. */
353 recpy_bt_func_symbol (PyObject
*self
, void *closure
)
355 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
360 if (func
->sym
== NULL
)
363 return symbol_to_symbol_object (func
->sym
);
366 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
367 Returns the list of instructions that belong to this function call. */
370 recpy_bt_func_instructions (PyObject
*self
, void *closure
)
372 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
378 len
= func
->insn
.size ();
380 /* Gaps count as one instruction. */
384 return btpy_list_new (((recpy_element_object
*) self
)->thread
,
385 func
->insn_offset
, func
->insn_offset
+ len
, 1,
389 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
390 btrace. Returns the caller / returnee of this function. */
393 recpy_bt_func_up (PyObject
*self
, void *closure
)
395 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
403 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
404 RECORD_METHOD_BTRACE
, func
->up
);
407 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
408 btrace. Returns a previous segment of this function. */
411 recpy_bt_func_prev (PyObject
*self
, void *closure
)
413 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
421 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
422 RECORD_METHOD_BTRACE
, func
->prev
);
425 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
426 btrace. Returns a following segment of this function. */
429 recpy_bt_func_next (PyObject
*self
, void *closure
)
431 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
439 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
440 RECORD_METHOD_BTRACE
, func
->next
);
443 /* Implementation of BtraceList.__len__ (self) -> int. */
446 btpy_list_length (PyObject
*self
)
448 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
449 const Py_ssize_t distance
= obj
->last
- obj
->first
;
450 const Py_ssize_t result
= distance
/ obj
->step
;
452 if ((distance
% obj
->step
) == 0)
459 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
460 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
463 btpy_list_item (PyObject
*self
, Py_ssize_t index
)
465 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
468 if (index
< 0 || index
>= btpy_list_length (self
))
469 return PyErr_Format (PyExc_IndexError
, _("Index out of range: %zd."),
472 number
= obj
->first
+ (obj
->step
* index
);
474 if (obj
->element_type
== &recpy_insn_type
)
475 return recpy_insn_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
477 return recpy_func_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
480 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
483 btpy_list_slice (PyObject
*self
, PyObject
*value
)
485 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
486 const Py_ssize_t length
= btpy_list_length (self
);
487 Py_ssize_t start
, stop
, step
, slicelength
;
489 if (PyInt_Check (value
))
491 Py_ssize_t index
= PyInt_AsSsize_t (value
);
493 /* Emulate Python behavior for negative indices. */
497 return btpy_list_item (self
, index
);
500 if (!PySlice_Check (value
))
501 return PyErr_Format (PyExc_TypeError
, _("Index must be int or slice."));
503 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value
), length
, &start
, &stop
,
504 &step
, &slicelength
))
507 return btpy_list_new (obj
->thread
, obj
->first
+ obj
->step
* start
,
508 obj
->first
+ obj
->step
* stop
, obj
->step
* step
,
512 /* Helper function that returns the position of an element in a BtraceList
513 or -1 if the element is not in the list. */
516 btpy_list_position (PyObject
*self
, PyObject
*value
)
518 const btpy_list_object
* const list_obj
= (btpy_list_object
*) self
;
519 const recpy_element_object
* const obj
= (const recpy_element_object
*) value
;
520 Py_ssize_t index
= obj
->number
;
522 if (list_obj
->element_type
!= Py_TYPE (value
))
525 if (list_obj
->thread
!= obj
->thread
)
528 if (index
< list_obj
->first
|| index
> list_obj
->last
)
531 index
-= list_obj
->first
;
533 if (index
% list_obj
->step
!= 0)
536 return index
/ list_obj
->step
;
539 /* Implementation of "in" operator for BtraceLists. */
542 btpy_list_contains (PyObject
*self
, PyObject
*value
)
544 if (btpy_list_position (self
, value
) < 0)
550 /* Implementation of BtraceLists.index (self, value) -> int. */
553 btpy_list_index (PyObject
*self
, PyObject
*value
)
555 const LONGEST index
= btpy_list_position (self
, value
);
558 return PyErr_Format (PyExc_ValueError
, _("Not in list."));
560 return gdb_py_object_from_longest (index
).release ();
563 /* Implementation of BtraceList.count (self, value) -> int. */
566 btpy_list_count (PyObject
*self
, PyObject
*value
)
568 /* We know that if an element is in the list, it is so exactly one time,
569 enabling us to reuse the "is element of" check. */
570 return gdb_py_object_from_longest (btpy_list_contains (self
,
574 /* Python rich compare function to allow for equality and inequality checks
578 btpy_list_richcompare (PyObject
*self
, PyObject
*other
, int op
)
580 const btpy_list_object
* const obj1
= (btpy_list_object
*) self
;
581 const btpy_list_object
* const obj2
= (btpy_list_object
*) other
;
583 if (Py_TYPE (self
) != Py_TYPE (other
))
585 Py_INCREF (Py_NotImplemented
);
586 return Py_NotImplemented
;
592 if (obj1
->thread
== obj2
->thread
593 && obj1
->element_type
== obj2
->element_type
594 && obj1
->first
== obj2
->first
595 && obj1
->last
== obj2
->last
596 && obj1
->step
== obj2
->step
)
602 if (obj1
->thread
!= obj2
->thread
603 || obj1
->element_type
!= obj2
->element_type
604 || obj1
->first
!= obj2
->first
605 || obj1
->last
!= obj2
->last
606 || obj1
->step
!= obj2
->step
)
615 Py_INCREF (Py_NotImplemented
);
616 return Py_NotImplemented
;
620 BtraceRecord.method [str]. */
623 recpy_bt_method (PyObject
*self
, void *closure
)
625 return PyString_FromString ("btrace");
629 BtraceRecord.format [str]. */
632 recpy_bt_format (PyObject
*self
, void *closure
)
634 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
635 const struct thread_info
* const tinfo
= record
->thread
;
636 const struct btrace_config
* config
;
641 config
= btrace_conf (&tinfo
->btrace
);
646 return PyString_FromString (btrace_format_short_string (config
->format
));
650 BtraceRecord.replay_position [BtraceInstruction]. */
653 recpy_bt_replay_position (PyObject
*self
, void *closure
)
655 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
656 thread_info
* tinfo
= record
->thread
;
661 if (tinfo
->btrace
.replay
== NULL
)
664 return btpy_insn_or_gap_new (tinfo
,
665 btrace_insn_number (tinfo
->btrace
.replay
));
669 BtraceRecord.begin [BtraceInstruction]. */
672 recpy_bt_begin (PyObject
*self
, void *closure
)
674 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
675 thread_info
*const tinfo
= record
->thread
;
676 struct btrace_insn_iterator iterator
;
681 btrace_fetch (tinfo
, record_btrace_get_cpu ());
683 if (btrace_is_empty (tinfo
))
686 btrace_insn_begin (&iterator
, &tinfo
->btrace
);
687 return btpy_insn_or_gap_new (tinfo
, btrace_insn_number (&iterator
));
691 BtraceRecord.end [BtraceInstruction]. */
694 recpy_bt_end (PyObject
*self
, void *closure
)
696 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
697 thread_info
*const tinfo
= record
->thread
;
698 struct btrace_insn_iterator iterator
;
703 btrace_fetch (tinfo
, record_btrace_get_cpu ());
705 if (btrace_is_empty (tinfo
))
708 btrace_insn_end (&iterator
, &tinfo
->btrace
);
709 return btpy_insn_or_gap_new (tinfo
, btrace_insn_number (&iterator
));
713 BtraceRecord.instruction_history [list]. */
716 recpy_bt_instruction_history (PyObject
*self
, void *closure
)
718 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
719 thread_info
*const tinfo
= record
->thread
;
720 struct btrace_insn_iterator iterator
;
721 unsigned long first
= 0;
722 unsigned long last
= 0;
727 btrace_fetch (tinfo
, record_btrace_get_cpu ());
729 if (btrace_is_empty (tinfo
))
732 btrace_insn_begin (&iterator
, &tinfo
->btrace
);
733 first
= btrace_insn_number (&iterator
);
735 btrace_insn_end (&iterator
, &tinfo
->btrace
);
736 last
= btrace_insn_number (&iterator
);
738 return btpy_list_new (tinfo
, first
, last
, 1, &recpy_insn_type
);
742 BtraceRecord.function_call_history [list]. */
745 recpy_bt_function_call_history (PyObject
*self
, void *closure
)
747 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
748 thread_info
*const tinfo
= record
->thread
;
749 struct btrace_call_iterator iterator
;
750 unsigned long first
= 0;
751 unsigned long last
= 0;
756 btrace_fetch (tinfo
, record_btrace_get_cpu ());
758 if (btrace_is_empty (tinfo
))
761 btrace_call_begin (&iterator
, &tinfo
->btrace
);
762 first
= btrace_call_number (&iterator
);
764 btrace_call_end (&iterator
, &tinfo
->btrace
);
765 last
= btrace_call_number (&iterator
);
767 return btpy_list_new (tinfo
, first
, last
, 1, &recpy_func_type
);
770 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
773 recpy_bt_goto (PyObject
*self
, PyObject
*args
)
775 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
776 thread_info
*const tinfo
= record
->thread
;
777 const recpy_element_object
*obj
;
780 if (tinfo
== NULL
|| btrace_is_empty (tinfo
))
781 return PyErr_Format (gdbpy_gdb_error
, _("Empty branch trace."));
783 if (!PyArg_ParseTuple (args
, "O", &parse_obj
))
786 if (Py_TYPE (parse_obj
) != &recpy_insn_type
)
787 return PyErr_Format (PyExc_TypeError
, _("Argument must be instruction."));
788 obj
= (const recpy_element_object
*) parse_obj
;
792 struct btrace_insn_iterator iter
;
794 btrace_insn_end (&iter
, &tinfo
->btrace
);
796 if (btrace_insn_number (&iter
) == obj
->number
)
797 target_goto_record_end ();
799 target_goto_record (obj
->number
);
801 catch (const gdb_exception
&except
)
803 GDB_PY_HANDLE_EXCEPTION (except
);
809 /* BtraceList methods. */
811 struct PyMethodDef btpy_list_methods
[] =
813 { "count", btpy_list_count
, METH_O
, "count number of occurrences"},
814 { "index", btpy_list_index
, METH_O
, "index of entry"},
818 /* BtraceList sequence methods. */
820 static PySequenceMethods btpy_list_sequence_methods
=
825 /* BtraceList mapping methods. Necessary for slicing. */
827 static PyMappingMethods btpy_list_mapping_methods
=
832 /* Sets up the btrace record API. */
835 gdbpy_initialize_btrace (void)
837 btpy_list_type
.tp_new
= PyType_GenericNew
;
838 btpy_list_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
839 btpy_list_type
.tp_basicsize
= sizeof (btpy_list_object
);
840 btpy_list_type
.tp_name
= "gdb.BtraceObjectList";
841 btpy_list_type
.tp_doc
= "GDB btrace list object";
842 btpy_list_type
.tp_methods
= btpy_list_methods
;
843 btpy_list_type
.tp_as_sequence
= &btpy_list_sequence_methods
;
844 btpy_list_type
.tp_as_mapping
= &btpy_list_mapping_methods
;
845 btpy_list_type
.tp_richcompare
= btpy_list_richcompare
;
847 btpy_list_sequence_methods
.sq_item
= btpy_list_item
;
848 btpy_list_sequence_methods
.sq_length
= btpy_list_length
;
849 btpy_list_sequence_methods
.sq_contains
= btpy_list_contains
;
851 btpy_list_mapping_methods
.mp_subscript
= btpy_list_slice
;
853 return PyType_Ready (&btpy_list_type
);