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 (const gdb_exception
&except
)
217 GDB_PY_HANDLE_EXCEPTION (except
);
223 /* Implementation of RecordInstruction.pc [int] for btrace.
224 Returns the instruction address. */
227 recpy_bt_insn_pc (PyObject
*self
, void *closure
)
229 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
234 return gdb_py_long_from_ulongest (insn
->pc
);
237 /* Implementation of RecordInstruction.size [int] for btrace.
238 Returns the instruction size. */
241 recpy_bt_insn_size (PyObject
*self
, void *closure
)
243 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
248 return PyInt_FromLong (insn
->size
);
251 /* Implementation of RecordInstruction.is_speculative [bool] for btrace.
252 Returns if this instruction was executed speculatively. */
255 recpy_bt_insn_is_speculative (PyObject
*self
, void *closure
)
257 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
262 if (insn
->flags
& BTRACE_INSN_FLAG_SPECULATIVE
)
268 /* Implementation of RecordInstruction.data [buffer] for btrace.
269 Returns raw instruction data. */
272 recpy_bt_insn_data (PyObject
*self
, void *closure
)
274 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
275 gdb::byte_vector buffer
;
283 buffer
.resize (insn
->size
);
284 read_memory (insn
->pc
, buffer
.data (), insn
->size
);
286 catch (const gdb_exception
&except
)
288 GDB_PY_HANDLE_EXCEPTION (except
);
291 object
= PyBytes_FromStringAndSize ((const char *) buffer
.data (),
298 return PyMemoryView_FromObject (object
);
300 return PyBuffer_FromObject (object
, 0, Py_END_OF_BUFFER
);
305 /* Implementation of RecordInstruction.decoded [str] for btrace.
306 Returns the instruction as human readable string. */
309 recpy_bt_insn_decoded (PyObject
*self
, void *closure
)
311 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
319 gdb_print_insn (target_gdbarch (), insn
->pc
, &strfile
, NULL
);
321 catch (const gdb_exception
&except
)
323 gdbpy_convert_exception (except
);
328 return PyBytes_FromString (strfile
.string ().c_str ());
331 /* Implementation of RecordFunctionSegment.level [int] for btrace.
332 Returns the call level. */
335 recpy_bt_func_level (PyObject
*self
, void *closure
)
337 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
343 tinfo
= ((recpy_element_object
*) self
)->thread
;
344 return PyInt_FromLong (tinfo
->btrace
.level
+ func
->level
);
347 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
348 Returns the symbol associated with this function call. */
351 recpy_bt_func_symbol (PyObject
*self
, void *closure
)
353 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
358 if (func
->sym
== NULL
)
361 return symbol_to_symbol_object (func
->sym
);
364 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
365 Returns the list of instructions that belong to this function call. */
368 recpy_bt_func_instructions (PyObject
*self
, void *closure
)
370 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
376 len
= func
->insn
.size ();
378 /* Gaps count as one instruction. */
382 return btpy_list_new (((recpy_element_object
*) self
)->thread
,
383 func
->insn_offset
, func
->insn_offset
+ len
, 1,
387 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
388 btrace. Returns the caller / returnee of this function. */
391 recpy_bt_func_up (PyObject
*self
, void *closure
)
393 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
401 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
402 RECORD_METHOD_BTRACE
, func
->up
);
405 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
406 btrace. Returns a previous segment of this function. */
409 recpy_bt_func_prev (PyObject
*self
, void *closure
)
411 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
419 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
420 RECORD_METHOD_BTRACE
, func
->prev
);
423 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
424 btrace. Returns a following segment of this function. */
427 recpy_bt_func_next (PyObject
*self
, void *closure
)
429 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
437 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
438 RECORD_METHOD_BTRACE
, func
->next
);
441 /* Implementation of BtraceList.__len__ (self) -> int. */
444 btpy_list_length (PyObject
*self
)
446 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
447 const Py_ssize_t distance
= obj
->last
- obj
->first
;
448 const Py_ssize_t result
= distance
/ obj
->step
;
450 if ((distance
% obj
->step
) == 0)
457 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
458 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
461 btpy_list_item (PyObject
*self
, Py_ssize_t index
)
463 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
466 if (index
< 0 || index
>= btpy_list_length (self
))
467 return PyErr_Format (PyExc_IndexError
, _("Index out of range: %zd."),
470 number
= obj
->first
+ (obj
->step
* index
);
472 if (obj
->element_type
== &recpy_insn_type
)
473 return recpy_insn_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
475 return recpy_func_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
478 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
481 btpy_list_slice (PyObject
*self
, PyObject
*value
)
483 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
484 const Py_ssize_t length
= btpy_list_length (self
);
485 Py_ssize_t start
, stop
, step
, slicelength
;
487 if (PyInt_Check (value
))
489 Py_ssize_t index
= PyInt_AsSsize_t (value
);
491 /* Emulate Python behavior for negative indices. */
495 return btpy_list_item (self
, index
);
498 if (!PySlice_Check (value
))
499 return PyErr_Format (PyExc_TypeError
, _("Index must be int or slice."));
501 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value
), length
, &start
, &stop
,
502 &step
, &slicelength
))
505 return btpy_list_new (obj
->thread
, obj
->first
+ obj
->step
* start
,
506 obj
->first
+ obj
->step
* stop
, obj
->step
* step
,
510 /* Helper function that returns the position of an element in a BtraceList
511 or -1 if the element is not in the list. */
514 btpy_list_position (PyObject
*self
, PyObject
*value
)
516 const btpy_list_object
* const list_obj
= (btpy_list_object
*) self
;
517 const recpy_element_object
* const obj
= (const recpy_element_object
*) value
;
518 Py_ssize_t index
= obj
->number
;
520 if (list_obj
->element_type
!= Py_TYPE (value
))
523 if (list_obj
->thread
!= obj
->thread
)
526 if (index
< list_obj
->first
|| index
> list_obj
->last
)
529 index
-= list_obj
->first
;
531 if (index
% list_obj
->step
!= 0)
534 return index
/ list_obj
->step
;
537 /* Implementation of "in" operator for BtraceLists. */
540 btpy_list_contains (PyObject
*self
, PyObject
*value
)
542 if (btpy_list_position (self
, value
) < 0)
548 /* Implementation of BtraceLists.index (self, value) -> int. */
551 btpy_list_index (PyObject
*self
, PyObject
*value
)
553 const LONGEST index
= btpy_list_position (self
, value
);
556 return PyErr_Format (PyExc_ValueError
, _("Not in list."));
558 return gdb_py_long_from_longest (index
);
561 /* Implementation of BtraceList.count (self, value) -> int. */
564 btpy_list_count (PyObject
*self
, PyObject
*value
)
566 /* We know that if an element is in the list, it is so exactly one time,
567 enabling us to reuse the "is element of" check. */
568 return PyInt_FromLong (btpy_list_contains (self
, value
));
571 /* Python rich compare function to allow for equality and inequality checks
575 btpy_list_richcompare (PyObject
*self
, PyObject
*other
, int op
)
577 const btpy_list_object
* const obj1
= (btpy_list_object
*) self
;
578 const btpy_list_object
* const obj2
= (btpy_list_object
*) other
;
580 if (Py_TYPE (self
) != Py_TYPE (other
))
582 Py_INCREF (Py_NotImplemented
);
583 return Py_NotImplemented
;
589 if (obj1
->thread
== obj2
->thread
590 && obj1
->element_type
== obj2
->element_type
591 && obj1
->first
== obj2
->first
592 && obj1
->last
== obj2
->last
593 && obj1
->step
== obj2
->step
)
599 if (obj1
->thread
!= obj2
->thread
600 || obj1
->element_type
!= obj2
->element_type
601 || obj1
->first
!= obj2
->first
602 || obj1
->last
!= obj2
->last
603 || obj1
->step
!= obj2
->step
)
612 Py_INCREF (Py_NotImplemented
);
613 return Py_NotImplemented
;
617 BtraceRecord.method [str]. */
620 recpy_bt_method (PyObject
*self
, void *closure
)
622 return PyString_FromString ("btrace");
626 BtraceRecord.format [str]. */
629 recpy_bt_format (PyObject
*self
, void *closure
)
631 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
632 const struct thread_info
* const tinfo
= record
->thread
;
633 const struct btrace_config
* config
;
638 config
= btrace_conf (&tinfo
->btrace
);
643 return PyString_FromString (btrace_format_short_string (config
->format
));
647 BtraceRecord.replay_position [BtraceInstruction]. */
650 recpy_bt_replay_position (PyObject
*self
, void *closure
)
652 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
653 thread_info
* tinfo
= record
->thread
;
658 if (tinfo
->btrace
.replay
== NULL
)
661 return btpy_insn_or_gap_new (tinfo
,
662 btrace_insn_number (tinfo
->btrace
.replay
));
666 BtraceRecord.begin [BtraceInstruction]. */
669 recpy_bt_begin (PyObject
*self
, void *closure
)
671 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
672 thread_info
*const tinfo
= record
->thread
;
673 struct btrace_insn_iterator iterator
;
678 btrace_fetch (tinfo
, record_btrace_get_cpu ());
680 if (btrace_is_empty (tinfo
))
683 btrace_insn_begin (&iterator
, &tinfo
->btrace
);
684 return btpy_insn_or_gap_new (tinfo
, btrace_insn_number (&iterator
));
688 BtraceRecord.end [BtraceInstruction]. */
691 recpy_bt_end (PyObject
*self
, void *closure
)
693 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
694 thread_info
*const tinfo
= record
->thread
;
695 struct btrace_insn_iterator iterator
;
700 btrace_fetch (tinfo
, record_btrace_get_cpu ());
702 if (btrace_is_empty (tinfo
))
705 btrace_insn_end (&iterator
, &tinfo
->btrace
);
706 return btpy_insn_or_gap_new (tinfo
, btrace_insn_number (&iterator
));
710 BtraceRecord.instruction_history [list]. */
713 recpy_bt_instruction_history (PyObject
*self
, void *closure
)
715 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
716 thread_info
*const tinfo
= record
->thread
;
717 struct btrace_insn_iterator iterator
;
718 unsigned long first
= 0;
719 unsigned long last
= 0;
724 btrace_fetch (tinfo
, record_btrace_get_cpu ());
726 if (btrace_is_empty (tinfo
))
729 btrace_insn_begin (&iterator
, &tinfo
->btrace
);
730 first
= btrace_insn_number (&iterator
);
732 btrace_insn_end (&iterator
, &tinfo
->btrace
);
733 last
= btrace_insn_number (&iterator
);
735 return btpy_list_new (tinfo
, first
, last
, 1, &recpy_insn_type
);
739 BtraceRecord.function_call_history [list]. */
742 recpy_bt_function_call_history (PyObject
*self
, void *closure
)
744 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
745 thread_info
*const tinfo
= record
->thread
;
746 struct btrace_call_iterator iterator
;
747 unsigned long first
= 0;
748 unsigned long last
= 0;
753 btrace_fetch (tinfo
, record_btrace_get_cpu ());
755 if (btrace_is_empty (tinfo
))
758 btrace_call_begin (&iterator
, &tinfo
->btrace
);
759 first
= btrace_call_number (&iterator
);
761 btrace_call_end (&iterator
, &tinfo
->btrace
);
762 last
= btrace_call_number (&iterator
);
764 return btpy_list_new (tinfo
, first
, last
, 1, &recpy_func_type
);
767 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
770 recpy_bt_goto (PyObject
*self
, PyObject
*args
)
772 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
773 thread_info
*const tinfo
= record
->thread
;
774 const recpy_element_object
*obj
;
777 if (tinfo
== NULL
|| btrace_is_empty (tinfo
))
778 return PyErr_Format (gdbpy_gdb_error
, _("Empty branch trace."));
780 if (!PyArg_ParseTuple (args
, "O", &parse_obj
))
783 if (Py_TYPE (parse_obj
) != &recpy_insn_type
)
784 return PyErr_Format (PyExc_TypeError
, _("Argument must be instruction."));
785 obj
= (const recpy_element_object
*) parse_obj
;
789 struct btrace_insn_iterator iter
;
791 btrace_insn_end (&iter
, &tinfo
->btrace
);
793 if (btrace_insn_number (&iter
) == obj
->number
)
794 target_goto_record_end ();
796 target_goto_record (obj
->number
);
798 catch (const gdb_exception
&except
)
800 GDB_PY_HANDLE_EXCEPTION (except
);
806 /* BtraceList methods. */
808 struct PyMethodDef btpy_list_methods
[] =
810 { "count", btpy_list_count
, METH_O
, "count number of occurences"},
811 { "index", btpy_list_index
, METH_O
, "index of entry"},
815 /* BtraceList sequence methods. */
817 static PySequenceMethods btpy_list_sequence_methods
=
822 /* BtraceList mapping methods. Necessary for slicing. */
824 static PyMappingMethods btpy_list_mapping_methods
=
829 /* Sets up the btrace record API. */
832 gdbpy_initialize_btrace (void)
834 btpy_list_type
.tp_new
= PyType_GenericNew
;
835 btpy_list_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
836 btpy_list_type
.tp_basicsize
= sizeof (btpy_list_object
);
837 btpy_list_type
.tp_name
= "gdb.BtraceObjectList";
838 btpy_list_type
.tp_doc
= "GDB btrace list object";
839 btpy_list_type
.tp_methods
= btpy_list_methods
;
840 btpy_list_type
.tp_as_sequence
= &btpy_list_sequence_methods
;
841 btpy_list_type
.tp_as_mapping
= &btpy_list_mapping_methods
;
842 btpy_list_type
.tp_richcompare
= btpy_list_richcompare
;
844 btpy_list_sequence_methods
.sq_item
= btpy_list_item
;
845 btpy_list_sequence_methods
.sq_length
= btpy_list_length
;
846 btpy_list_sequence_methods
.sq_contains
= btpy_list_contains
;
848 btpy_list_mapping_methods
.mp_subscript
= btpy_list_slice
;
850 return PyType_Ready (&btpy_list_type
);