1 /* Python interface to btrace instruction history.
3 Copyright 2016-2024 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"
31 /* Python object for btrace record lists. */
33 struct btpy_list_object
{
36 /* The thread this list belongs to. */
39 /* The first index being part of this list. */
42 /* The last index begin part of this list. */
48 /* Either &BTPY_CALL_TYPE or &RECPY_INSN_TYPE. */
49 PyTypeObject
* element_type
;
52 /* Python type for btrace lists. */
54 static PyTypeObject btpy_list_type
= {
55 PyVarObject_HEAD_INIT (NULL
, 0)
58 /* Returns either a btrace_insn for the given Python gdb.RecordInstruction
59 object or sets an appropriate Python exception and returns NULL. */
61 static const btrace_insn
*
62 btrace_insn_from_recpy_insn (const PyObject
* const pyobject
)
64 const btrace_insn
*insn
;
65 const recpy_element_object
*obj
;
67 btrace_insn_iterator iter
;
69 if (Py_TYPE (pyobject
) != &recpy_insn_type
)
71 PyErr_Format (gdbpy_gdb_error
, _("Must be gdb.RecordInstruction"));
75 obj
= (const recpy_element_object
*) pyobject
;
78 if (tinfo
== NULL
|| btrace_is_empty (tinfo
))
80 PyErr_Format (gdbpy_gdb_error
, _("No such instruction."));
84 if (btrace_find_insn_by_number (&iter
, &tinfo
->btrace
, obj
->number
) == 0)
86 PyErr_Format (gdbpy_gdb_error
, _("No such instruction."));
90 insn
= btrace_insn_get (&iter
);
93 PyErr_Format (gdbpy_gdb_error
, _("Not a valid instruction."));
100 /* Returns either a btrace_function for the given Python
101 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
104 static const btrace_function
*
105 btrace_func_from_recpy_func (const PyObject
* const pyobject
)
107 const btrace_function
*func
;
108 const recpy_element_object
*obj
;
110 btrace_call_iterator iter
;
112 if (Py_TYPE (pyobject
) != &recpy_func_type
)
114 PyErr_Format (gdbpy_gdb_error
, _("Must be gdb.RecordFunctionSegment"));
118 obj
= (const recpy_element_object
*) pyobject
;
121 if (tinfo
== NULL
|| btrace_is_empty (tinfo
))
123 PyErr_Format (gdbpy_gdb_error
, _("No such function segment."));
127 if (btrace_find_call_by_number (&iter
, &tinfo
->btrace
, obj
->number
) == 0)
129 PyErr_Format (gdbpy_gdb_error
, _("No such function segment."));
133 func
= btrace_call_get (&iter
);
136 PyErr_Format (gdbpy_gdb_error
, _("Not a valid function segment."));
143 /* Looks at the recorded item with the number NUMBER and create a
144 gdb.RecordInstruction or gdb.RecordGap object for it accordingly. */
147 btpy_insn_or_gap_new (thread_info
*tinfo
, Py_ssize_t number
)
149 btrace_insn_iterator iter
;
152 btrace_find_insn_by_number (&iter
, &tinfo
->btrace
, number
);
153 err_code
= btrace_insn_get_error (&iter
);
157 const btrace_config
*config
;
158 const char *err_string
;
160 config
= btrace_conf (&tinfo
->btrace
);
161 err_string
= btrace_decode_error (config
->format
, err_code
);
163 return recpy_gap_new (err_code
, err_string
, number
);
166 return recpy_insn_new (tinfo
, RECORD_METHOD_BTRACE
, number
);
169 /* Create a new gdb.BtraceList object. */
172 btpy_list_new (thread_info
*thread
, Py_ssize_t first
, Py_ssize_t last
,
173 Py_ssize_t step
, PyTypeObject
*element_type
)
175 btpy_list_object
* const obj
= PyObject_New (btpy_list_object
,
181 obj
->thread
= thread
;
185 obj
->element_type
= element_type
;
187 return (PyObject
*) obj
;
190 /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
191 Returns the SAL associated with this instruction. */
194 recpy_bt_insn_sal (PyObject
*self
, void *closure
)
196 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
197 PyObject
*result
= NULL
;
204 result
= symtab_and_line_to_sal_object (find_pc_line (insn
->pc
, 0));
206 catch (const gdb_exception
&except
)
208 GDB_PY_HANDLE_EXCEPTION (except
);
214 /* Implementation of RecordInstruction.pc [int] for btrace.
215 Returns the instruction address. */
218 recpy_bt_insn_pc (PyObject
*self
, void *closure
)
220 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
225 return gdb_py_object_from_ulongest (insn
->pc
).release ();
228 /* Implementation of RecordInstruction.size [int] for btrace.
229 Returns the instruction size. */
232 recpy_bt_insn_size (PyObject
*self
, void *closure
)
234 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
239 return gdb_py_object_from_longest (insn
->size
).release ();
242 /* Implementation of RecordInstruction.is_speculative [bool] for btrace.
243 Returns if this instruction was executed speculatively. */
246 recpy_bt_insn_is_speculative (PyObject
*self
, void *closure
)
248 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
253 if (insn
->flags
& BTRACE_INSN_FLAG_SPECULATIVE
)
259 /* Implementation of RecordInstruction.data [buffer] for btrace.
260 Returns raw instruction data. */
263 recpy_bt_insn_data (PyObject
*self
, void *closure
)
265 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
266 gdb::byte_vector buffer
;
274 buffer
.resize (insn
->size
);
275 read_memory (insn
->pc
, buffer
.data (), insn
->size
);
277 catch (const gdb_exception
&except
)
279 GDB_PY_HANDLE_EXCEPTION (except
);
282 object
= PyBytes_FromStringAndSize ((const char *) buffer
.data (),
288 return PyMemoryView_FromObject (object
);
291 /* Implementation of RecordInstruction.decoded [str] for btrace.
292 Returns the instruction as human readable string. */
295 recpy_bt_insn_decoded (PyObject
*self
, void *closure
)
297 const btrace_insn
* const insn
= btrace_insn_from_recpy_insn (self
);
305 gdb_print_insn (current_inferior ()->arch (), insn
->pc
, &strfile
, NULL
);
307 catch (const gdb_exception
&except
)
309 gdbpy_convert_exception (except
);
313 return PyBytes_FromString (strfile
.string ().c_str ());
316 /* Implementation of RecordFunctionSegment.level [int] for btrace.
317 Returns the call level. */
320 recpy_bt_func_level (PyObject
*self
, void *closure
)
322 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
328 tinfo
= ((recpy_element_object
*) self
)->thread
;
329 return gdb_py_object_from_longest (tinfo
->btrace
.level
330 + func
->level
).release ();
333 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
334 Returns the symbol associated with this function call. */
337 recpy_bt_func_symbol (PyObject
*self
, void *closure
)
339 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
344 if (func
->sym
== NULL
)
347 return symbol_to_symbol_object (func
->sym
);
350 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
351 Returns the list of instructions that belong to this function call. */
354 recpy_bt_func_instructions (PyObject
*self
, void *closure
)
356 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
362 len
= func
->insn
.size ();
364 /* Gaps count as one instruction. */
368 return btpy_list_new (((recpy_element_object
*) self
)->thread
,
369 func
->insn_offset
, func
->insn_offset
+ len
, 1,
373 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
374 btrace. Returns the caller / returnee of this function. */
377 recpy_bt_func_up (PyObject
*self
, void *closure
)
379 const btrace_function
* const func
= btrace_func_from_recpy_func (self
);
387 return recpy_func_new (((recpy_element_object
*) self
)->thread
,
388 RECORD_METHOD_BTRACE
, func
->up
);
391 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
392 btrace. Returns a previous segment of this function. */
395 recpy_bt_func_prev (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
->prev
);
409 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
410 btrace. Returns a following segment of this function. */
413 recpy_bt_func_next (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
->next
);
427 /* Implementation of BtraceList.__len__ (self) -> int. */
430 btpy_list_length (PyObject
*self
)
432 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
433 const Py_ssize_t distance
= obj
->last
- obj
->first
;
434 const Py_ssize_t result
= distance
/ obj
->step
;
436 if ((distance
% obj
->step
) == 0)
443 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
444 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
447 btpy_list_item (PyObject
*self
, Py_ssize_t index
)
449 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
452 if (index
< 0 || index
>= btpy_list_length (self
))
453 return PyErr_Format (PyExc_IndexError
, _("Index out of range: %zd."),
456 number
= obj
->first
+ (obj
->step
* index
);
458 if (obj
->element_type
== &recpy_insn_type
)
459 return recpy_insn_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
461 return recpy_func_new (obj
->thread
, RECORD_METHOD_BTRACE
, number
);
464 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
467 btpy_list_slice (PyObject
*self
, PyObject
*value
)
469 const btpy_list_object
* const obj
= (btpy_list_object
*) self
;
470 const Py_ssize_t length
= btpy_list_length (self
);
471 Py_ssize_t start
, stop
, step
, slicelength
;
473 if (PyLong_Check (value
))
475 Py_ssize_t index
= PyLong_AsSsize_t (value
);
477 /* Emulate Python behavior for negative indices. */
481 return btpy_list_item (self
, index
);
484 if (!PySlice_Check (value
))
485 return PyErr_Format (PyExc_TypeError
, _("Index must be int or slice."));
487 if (0 != PySlice_GetIndicesEx (value
, length
, &start
, &stop
,
488 &step
, &slicelength
))
491 return btpy_list_new (obj
->thread
, obj
->first
+ obj
->step
* start
,
492 obj
->first
+ obj
->step
* stop
, obj
->step
* step
,
496 /* Helper function that returns the position of an element in a BtraceList
497 or -1 if the element is not in the list. */
500 btpy_list_position (PyObject
*self
, PyObject
*value
)
502 const btpy_list_object
* const list_obj
= (btpy_list_object
*) self
;
503 const recpy_element_object
* const obj
= (const recpy_element_object
*) value
;
504 Py_ssize_t index
= obj
->number
;
506 if (list_obj
->element_type
!= Py_TYPE (value
))
509 if (list_obj
->thread
!= obj
->thread
)
512 if (index
< list_obj
->first
|| index
> list_obj
->last
)
515 index
-= list_obj
->first
;
517 if (index
% list_obj
->step
!= 0)
520 return index
/ list_obj
->step
;
523 /* Implementation of "in" operator for BtraceLists. */
526 btpy_list_contains (PyObject
*self
, PyObject
*value
)
528 if (btpy_list_position (self
, value
) < 0)
534 /* Implementation of BtraceLists.index (self, value) -> int. */
537 btpy_list_index (PyObject
*self
, PyObject
*value
)
539 const LONGEST index
= btpy_list_position (self
, value
);
542 return PyErr_Format (PyExc_ValueError
, _("Not in list."));
544 return gdb_py_object_from_longest (index
).release ();
547 /* Implementation of BtraceList.count (self, value) -> int. */
550 btpy_list_count (PyObject
*self
, PyObject
*value
)
552 /* We know that if an element is in the list, it is so exactly one time,
553 enabling us to reuse the "is element of" check. */
554 return gdb_py_object_from_longest (btpy_list_contains (self
,
558 /* Python rich compare function to allow for equality and inequality checks
562 btpy_list_richcompare (PyObject
*self
, PyObject
*other
, int op
)
564 const btpy_list_object
* const obj1
= (btpy_list_object
*) self
;
565 const btpy_list_object
* const obj2
= (btpy_list_object
*) other
;
567 if (Py_TYPE (self
) != Py_TYPE (other
))
569 Py_INCREF (Py_NotImplemented
);
570 return Py_NotImplemented
;
576 if (obj1
->thread
== obj2
->thread
577 && obj1
->element_type
== obj2
->element_type
578 && obj1
->first
== obj2
->first
579 && obj1
->last
== obj2
->last
580 && obj1
->step
== obj2
->step
)
586 if (obj1
->thread
!= obj2
->thread
587 || obj1
->element_type
!= obj2
->element_type
588 || obj1
->first
!= obj2
->first
589 || obj1
->last
!= obj2
->last
590 || obj1
->step
!= obj2
->step
)
599 Py_INCREF (Py_NotImplemented
);
600 return Py_NotImplemented
;
604 BtraceRecord.method [str]. */
607 recpy_bt_method (PyObject
*self
, void *closure
)
609 return PyUnicode_FromString ("btrace");
613 BtraceRecord.format [str]. */
616 recpy_bt_format (PyObject
*self
, void *closure
)
618 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
619 const struct thread_info
* const tinfo
= record
->thread
;
620 const struct btrace_config
* config
;
625 config
= btrace_conf (&tinfo
->btrace
);
630 return PyUnicode_FromString (btrace_format_short_string (config
->format
));
634 BtraceRecord.replay_position [BtraceInstruction]. */
637 recpy_bt_replay_position (PyObject
*self
, void *closure
)
639 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
640 thread_info
* tinfo
= record
->thread
;
645 if (tinfo
->btrace
.replay
== NULL
)
648 return btpy_insn_or_gap_new (tinfo
,
649 btrace_insn_number (tinfo
->btrace
.replay
));
653 BtraceRecord.begin [BtraceInstruction]. */
656 recpy_bt_begin (PyObject
*self
, void *closure
)
658 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
659 thread_info
*const tinfo
= record
->thread
;
660 struct btrace_insn_iterator iterator
;
665 btrace_fetch (tinfo
, record_btrace_get_cpu ());
667 if (btrace_is_empty (tinfo
))
670 btrace_insn_begin (&iterator
, &tinfo
->btrace
);
671 return btpy_insn_or_gap_new (tinfo
, btrace_insn_number (&iterator
));
675 BtraceRecord.end [BtraceInstruction]. */
678 recpy_bt_end (PyObject
*self
, void *closure
)
680 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
681 thread_info
*const tinfo
= record
->thread
;
682 struct btrace_insn_iterator iterator
;
687 btrace_fetch (tinfo
, record_btrace_get_cpu ());
689 if (btrace_is_empty (tinfo
))
692 btrace_insn_end (&iterator
, &tinfo
->btrace
);
693 return btpy_insn_or_gap_new (tinfo
, btrace_insn_number (&iterator
));
697 BtraceRecord.instruction_history [list]. */
700 recpy_bt_instruction_history (PyObject
*self
, void *closure
)
702 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
703 thread_info
*const tinfo
= record
->thread
;
704 struct btrace_insn_iterator iterator
;
705 unsigned long first
= 0;
706 unsigned long last
= 0;
711 btrace_fetch (tinfo
, record_btrace_get_cpu ());
713 if (btrace_is_empty (tinfo
))
716 btrace_insn_begin (&iterator
, &tinfo
->btrace
);
717 first
= btrace_insn_number (&iterator
);
719 btrace_insn_end (&iterator
, &tinfo
->btrace
);
720 last
= btrace_insn_number (&iterator
);
722 return btpy_list_new (tinfo
, first
, last
, 1, &recpy_insn_type
);
726 BtraceRecord.function_call_history [list]. */
729 recpy_bt_function_call_history (PyObject
*self
, void *closure
)
731 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
732 thread_info
*const tinfo
= record
->thread
;
733 struct btrace_call_iterator iterator
;
734 unsigned long first
= 0;
735 unsigned long last
= 0;
740 btrace_fetch (tinfo
, record_btrace_get_cpu ());
742 if (btrace_is_empty (tinfo
))
745 btrace_call_begin (&iterator
, &tinfo
->btrace
);
746 first
= btrace_call_number (&iterator
);
748 btrace_call_end (&iterator
, &tinfo
->btrace
);
749 last
= btrace_call_number (&iterator
);
751 return btpy_list_new (tinfo
, first
, last
, 1, &recpy_func_type
);
754 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
757 recpy_bt_goto (PyObject
*self
, PyObject
*args
)
759 const recpy_record_object
* const record
= (recpy_record_object
*) self
;
760 thread_info
*const tinfo
= record
->thread
;
761 const recpy_element_object
*obj
;
764 if (tinfo
== NULL
|| btrace_is_empty (tinfo
))
765 return PyErr_Format (gdbpy_gdb_error
, _("Empty branch trace."));
767 if (!PyArg_ParseTuple (args
, "O", &parse_obj
))
770 if (Py_TYPE (parse_obj
) != &recpy_insn_type
)
771 return PyErr_Format (PyExc_TypeError
, _("Argument must be instruction."));
772 obj
= (const recpy_element_object
*) parse_obj
;
776 struct btrace_insn_iterator iter
;
778 btrace_insn_end (&iter
, &tinfo
->btrace
);
780 if (btrace_insn_number (&iter
) == obj
->number
)
781 target_goto_record_end ();
783 target_goto_record (obj
->number
);
785 catch (const gdb_exception
&except
)
787 GDB_PY_HANDLE_EXCEPTION (except
);
793 /* BtraceList methods. */
795 static PyMethodDef btpy_list_methods
[] =
797 { "count", btpy_list_count
, METH_O
, "count number of occurrences"},
798 { "index", btpy_list_index
, METH_O
, "index of entry"},
802 /* BtraceList sequence methods. */
804 static PySequenceMethods btpy_list_sequence_methods
=
809 /* BtraceList mapping methods. Necessary for slicing. */
811 static PyMappingMethods btpy_list_mapping_methods
=
816 /* Sets up the btrace record API. */
818 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
819 gdbpy_initialize_btrace (void)
821 btpy_list_type
.tp_new
= PyType_GenericNew
;
822 btpy_list_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
823 btpy_list_type
.tp_basicsize
= sizeof (btpy_list_object
);
824 btpy_list_type
.tp_name
= "gdb.BtraceObjectList";
825 btpy_list_type
.tp_doc
= "GDB btrace list object";
826 btpy_list_type
.tp_methods
= btpy_list_methods
;
827 btpy_list_type
.tp_as_sequence
= &btpy_list_sequence_methods
;
828 btpy_list_type
.tp_as_mapping
= &btpy_list_mapping_methods
;
829 btpy_list_type
.tp_richcompare
= btpy_list_richcompare
;
831 btpy_list_sequence_methods
.sq_item
= btpy_list_item
;
832 btpy_list_sequence_methods
.sq_length
= btpy_list_length
;
833 btpy_list_sequence_methods
.sq_contains
= btpy_list_contains
;
835 btpy_list_mapping_methods
.mp_subscript
= btpy_list_slice
;
837 return PyType_Ready (&btpy_list_type
);
840 GDBPY_INITIALIZE_FILE (gdbpy_initialize_btrace
);