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"
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_vector buffer
;
284 buffer
.resize (insn
->size
);
285 read_memory (insn
->pc
, buffer
.data (), insn
->size
);
287 CATCH (except
, RETURN_MASK_ALL
)
289 GDB_PY_HANDLE_EXCEPTION (except
);
293 object
= PyBytes_FromStringAndSize ((const char *) buffer
.data (),
300 return PyMemoryView_FromObject (object
);
302 return PyBuffer_FromObject (object
, 0, Py_END_OF_BUFFER
);
307 /* Implementation of RecordInstruction.decoded [str] for btrace.
308 Returns the instruction as human readable string. */
311 recpy_bt_insn_decoded (PyObject
*self
, void *closure
)
313 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
321 gdb_print_insn (target_gdbarch (), insn
->pc
, &strfile
, NULL
);
323 CATCH (except
, RETURN_MASK_ALL
)
325 gdbpy_convert_exception (except
);
331 return PyBytes_FromString (strfile
.string ().c_str ());
334 /* Implementation of RecordFunctionSegment.level [int] for btrace.
335 Returns the call level. */
338 recpy_bt_func_level (PyObject
*self
, void *closure
)
340 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
346 tinfo
= ((recpy_element_object
*) self
)->thread
;
347 return PyInt_FromLong (tinfo
->btrace
.level
+ func
->level
);
350 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
351 Returns the symbol associated with this function call. */
354 recpy_bt_func_symbol (PyObject
*self
, void *closure
)
356 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
361 if (func
->sym
== NULL
)
364 return symbol_to_symbol_object (func
->sym
);
367 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
368 Returns the list of instructions that belong to this function call. */
371 recpy_bt_func_instructions (PyObject
*self
, void *closure
)
373 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
379 len
= func
->insn
.size ();
381 /* Gaps count as one instruction. */
385 return btpy_list_new (((recpy_element_object
*) self
)->thread
,
386 func
->insn_offset
, func
->insn_offset
+ len
, 1,
390 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
391 btrace. Returns the caller / returnee of this function. */
394 recpy_bt_func_up (PyObject
*self
, void *closure
)
396 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
404 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
405 RECORD_METHOD_BTRACE
, func
->up
);
408 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
409 btrace. Returns a previous segment of this function. */
412 recpy_bt_func_prev (PyObject
*self
, void *closure
)
414 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
422 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
423 RECORD_METHOD_BTRACE
, func
->prev
);
426 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
427 btrace. Returns a following segment of this function. */
430 recpy_bt_func_next (PyObject
*self
, void *closure
)
432 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
440 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
441 RECORD_METHOD_BTRACE
, func
->next
);
444 /* Implementation of BtraceList.__len__ (self) -> int. */
447 btpy_list_length (PyObject
*self
)
449 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
450 const Py_ssize_t distance
= obj
->last
- obj
->first
;
451 const Py_ssize_t result
= distance
/ obj
->step
;
453 if ((distance
% obj
->step
) == 0)
460 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
461 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
464 btpy_list_item (PyObject
*self
, Py_ssize_t index
)
466 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
469 if (index
< 0 || index
>= btpy_list_length (self
))
470 return PyErr_Format (PyExc_IndexError
, _("Index out of range: %zd."),
473 number
= obj
->first
+ (obj
->step
* index
);
475 if (obj
->element_type
== &recpy_insn_type
)
476 return recpy_insn_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
478 return recpy_func_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
481 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
484 btpy_list_slice (PyObject
*self
, PyObject
*value
)
486 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
487 const Py_ssize_t length
= btpy_list_length (self
);
488 Py_ssize_t start
, stop
, step
, slicelength
;
490 if (PyInt_Check (value
))
492 Py_ssize_t index
= PyInt_AsSsize_t (value
);
494 /* Emulate Python behavior for negative indices. */
498 return btpy_list_item (self
, index
);
501 if (!PySlice_Check (value
))
502 return PyErr_Format (PyExc_TypeError
, _("Index must be int or slice."));
504 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value
), length
, &start
, &stop
,
505 &step
, &slicelength
))
508 return btpy_list_new (obj
->thread
, obj
->first
+ obj
->step
* start
,
509 obj
->first
+ obj
->step
* stop
, obj
->step
* step
,
513 /* Helper function that returns the position of an element in a BtraceList
514 or -1 if the element is not in the list. */
517 btpy_list_position (PyObject
*self
, PyObject
*value
)
519 const btpy_list_object
* const list_obj
= (btpy_list_object
*) self
;
520 const recpy_element_object
* const obj
= (const recpy_element_object
*) value
;
521 Py_ssize_t index
= obj
->number
;
523 if (list_obj
->element_type
!= Py_TYPE (value
))
526 if (list_obj
->thread
!= obj
->thread
)
529 if (index
< list_obj
->first
|| index
> list_obj
->last
)
532 index
-= list_obj
->first
;
534 if (index
% list_obj
->step
!= 0)
537 return index
/ list_obj
->step
;
540 /* Implementation of "in" operator for BtraceLists. */
543 btpy_list_contains (PyObject
*self
, PyObject
*value
)
545 if (btpy_list_position (self
, value
) < 0)
551 /* Implementation of BtraceLists.index (self, value) -> int. */
554 btpy_list_index (PyObject
*self
, PyObject
*value
)
556 const LONGEST index
= btpy_list_position (self
, value
);
559 return PyErr_Format (PyExc_ValueError
, _("Not in list."));
561 return gdb_py_long_from_longest (index
);
564 /* Implementation of BtraceList.count (self, value) -> int. */
567 btpy_list_count (PyObject
*self
, PyObject
*value
)
569 /* We know that if an element is in the list, it is so exactly one time,
570 enabling us to reuse the "is element of" check. */
571 return PyInt_FromLong (btpy_list_contains (self
, value
));
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 (except
, RETURN_MASK_ALL
)
803 GDB_PY_HANDLE_EXCEPTION (except
);
810 /* BtraceList methods. */
812 struct PyMethodDef btpy_list_methods
[] =
814 { "count", btpy_list_count
, METH_O
, "count number of occurences"},
815 { "index", btpy_list_index
, METH_O
, "index of entry"},
819 /* BtraceList sequence methods. */
821 static PySequenceMethods btpy_list_sequence_methods
=
826 /* BtraceList mapping methods. Necessary for slicing. */
828 static PyMappingMethods btpy_list_mapping_methods
=
833 /* Sets up the btrace record API. */
836 gdbpy_initialize_btrace (void)
838 btpy_list_type
.tp_new
= PyType_GenericNew
;
839 btpy_list_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
840 btpy_list_type
.tp_basicsize
= sizeof (btpy_list_object
);
841 btpy_list_type
.tp_name
= "gdb.BtraceObjectList";
842 btpy_list_type
.tp_doc
= "GDB btrace list object";
843 btpy_list_type
.tp_methods
= btpy_list_methods
;
844 btpy_list_type
.tp_as_sequence
= &btpy_list_sequence_methods
;
845 btpy_list_type
.tp_as_mapping
= &btpy_list_mapping_methods
;
846 btpy_list_type
.tp_richcompare
= btpy_list_richcompare
;
848 btpy_list_sequence_methods
.sq_item
= btpy_list_item
;
849 btpy_list_sequence_methods
.sq_length
= btpy_list_length
;
850 btpy_list_sequence_methods
.sq_contains
= btpy_list_contains
;
852 btpy_list_mapping_methods
.mp_subscript
= btpy_list_slice
;
854 return PyType_Ready (&btpy_list_type
);