arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / python / py-record-btrace.c
blobbdfebf1b8d2687f5e07617ba77912a806ac70164
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/>. */
20 #include "gdbcore.h"
21 #include "cli/cli-cmds.h"
22 #include "gdbthread.h"
23 #include "btrace.h"
24 #include "py-record.h"
25 #include "py-record-btrace.h"
26 #include "record-btrace.h"
27 #include "disasm.h"
28 #include "gdbarch.h"
30 /* Python object for btrace record lists. */
32 struct btpy_list_object {
33 PyObject_HEAD
35 /* The thread this list belongs to. */
36 thread_info *thread;
38 /* The first index being part of this list. */
39 Py_ssize_t first;
41 /* The last index begin part of this list. */
42 Py_ssize_t last;
44 /* Stride size. */
45 Py_ssize_t step;
47 /* Either &recpy_func_type, &recpy_insn_type, &recpy_aux_type or
48 &recpy_gap_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;
66 thread_info *tinfo;
67 btrace_insn_iterator iter;
69 if (Py_TYPE (pyobject) != &recpy_insn_type)
71 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction"));
72 return NULL;
75 obj = (const recpy_element_object *) pyobject;
76 tinfo = obj->thread;
78 if (tinfo == NULL || btrace_is_empty (tinfo))
80 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
81 return NULL;
84 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
86 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
87 return NULL;
90 insn = btrace_insn_get (&iter);
91 if (insn == NULL)
93 PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction."));
94 return NULL;
97 return insn;
100 /* Returns either a btrace_function for the given Python
101 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
102 returns NULL. */
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;
109 thread_info *tinfo;
110 btrace_call_iterator iter;
112 if (Py_TYPE (pyobject) != &recpy_func_type)
114 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment"));
115 return NULL;
118 obj = (const recpy_element_object *) pyobject;
119 tinfo = obj->thread;
121 if (tinfo == NULL || btrace_is_empty (tinfo))
123 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
124 return NULL;
127 if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0)
129 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
130 return NULL;
133 func = btrace_call_get (&iter);
134 if (func == NULL)
136 PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment."));
137 return NULL;
140 return func;
143 /* Looks at the recorded item with the number NUMBER and create a
144 gdb.RecordInstruction, gdb.RecordGap or gdb.RecordAuxiliary object
145 for it accordingly. */
147 static PyObject *
148 btpy_item_new (thread_info *tinfo, Py_ssize_t number)
150 btrace_insn_iterator iter;
151 int err_code;
153 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, number) == 0)
155 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
156 return nullptr;
159 err_code = btrace_insn_get_error (&iter);
161 if (err_code != 0)
163 const btrace_config *config;
164 const char *err_string;
166 config = btrace_conf (&tinfo->btrace);
167 err_string = btrace_decode_error (config->format, err_code);
169 return recpy_gap_new (err_code, err_string, number);
172 const struct btrace_insn *insn = btrace_insn_get (&iter);
173 gdb_assert (insn != nullptr);
175 if (insn->iclass == BTRACE_INSN_AUX)
176 return recpy_aux_new (tinfo, RECORD_METHOD_BTRACE, number);
178 return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number);
181 /* Create a new gdb.BtraceList object. */
183 static PyObject *
184 btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last,
185 Py_ssize_t step, PyTypeObject *element_type)
187 btpy_list_object * const obj = PyObject_New (btpy_list_object,
188 &btpy_list_type);
190 if (obj == NULL)
191 return NULL;
193 obj->thread = thread;
194 obj->first = first;
195 obj->last = last;
196 obj->step = step;
197 obj->element_type = element_type;
199 return (PyObject *) obj;
202 /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
203 Returns the SAL associated with this instruction. */
205 PyObject *
206 recpy_bt_insn_sal (PyObject *self, void *closure)
208 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
209 PyObject *result = NULL;
211 if (insn == NULL)
212 return NULL;
216 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
218 catch (const gdb_exception &except)
220 return gdbpy_handle_gdb_exception (nullptr, except);
223 return result;
226 /* Implementation of RecordInstruction.pc [int] for btrace.
227 Returns the instruction address. */
229 PyObject *
230 recpy_bt_insn_pc (PyObject *self, void *closure)
232 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
234 if (insn == NULL)
235 return NULL;
237 return gdb_py_object_from_ulongest (insn->pc).release ();
240 /* Implementation of RecordInstruction.size [int] for btrace.
241 Returns the instruction size. */
243 PyObject *
244 recpy_bt_insn_size (PyObject *self, void *closure)
246 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
248 if (insn == NULL)
249 return NULL;
251 return gdb_py_object_from_longest (insn->size).release ();
254 /* Implementation of RecordInstruction.is_speculative [bool] for btrace.
255 Returns if this instruction was executed speculatively. */
257 PyObject *
258 recpy_bt_insn_is_speculative (PyObject *self, void *closure)
260 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
262 if (insn == NULL)
263 return NULL;
265 if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
266 Py_RETURN_TRUE;
267 else
268 Py_RETURN_FALSE;
271 /* Implementation of RecordInstruction.data [buffer] for btrace.
272 Returns raw instruction data. */
274 PyObject *
275 recpy_bt_insn_data (PyObject *self, void *closure)
277 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
278 gdb::byte_vector buffer;
279 PyObject *object;
281 if (insn == NULL)
282 return NULL;
286 buffer.resize (insn->size);
287 read_memory (insn->pc, buffer.data (), insn->size);
289 catch (const gdb_exception &except)
291 return gdbpy_handle_gdb_exception (nullptr, except);
294 object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
295 insn->size);
297 if (object == NULL)
298 return NULL;
300 return PyMemoryView_FromObject (object);
303 /* Implementation of RecordInstruction.decoded [str] for btrace.
304 Returns the instruction as human readable string. */
306 PyObject *
307 recpy_bt_insn_decoded (PyObject *self, void *closure)
309 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
310 string_file strfile;
312 if (insn == NULL)
313 return NULL;
317 gdb_print_insn (current_inferior ()->arch (), insn->pc, &strfile, NULL);
319 catch (const gdb_exception &except)
321 return gdbpy_handle_gdb_exception (nullptr, except);
324 return PyBytes_FromString (strfile.string ().c_str ());
327 /* Implementation of RecordFunctionSegment.level [int] for btrace.
328 Returns the call level. */
330 PyObject *
331 recpy_bt_func_level (PyObject *self, void *closure)
333 const btrace_function * const func = btrace_func_from_recpy_func (self);
334 thread_info *tinfo;
336 if (func == NULL)
337 return NULL;
339 tinfo = ((recpy_element_object *) self)->thread;
340 return gdb_py_object_from_longest (tinfo->btrace.level
341 + func->level).release ();
344 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
345 Returns the symbol associated with this function call. */
347 PyObject *
348 recpy_bt_func_symbol (PyObject *self, void *closure)
350 const btrace_function * const func = btrace_func_from_recpy_func (self);
352 if (func == NULL)
353 return NULL;
355 if (func->sym == NULL)
356 Py_RETURN_NONE;
358 return symbol_to_symbol_object (func->sym);
361 /* Implementation of RecordFunctionSegment.instructions [list] for btrace.
362 Returns the list of instructions that belong to this function call. */
364 PyObject *
365 recpy_bt_func_instructions (PyObject *self, void *closure)
367 const btrace_function * const func = btrace_func_from_recpy_func (self);
368 unsigned int len;
370 if (func == NULL)
371 return NULL;
373 len = func->insn.size ();
375 /* Gaps count as one instruction. */
376 if (len == 0)
377 len = 1;
379 return btpy_list_new (((recpy_element_object *) self)->thread,
380 func->insn_offset, func->insn_offset + len, 1,
381 &recpy_insn_type);
384 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
385 btrace. Returns the caller / returnee of this function. */
387 PyObject *
388 recpy_bt_func_up (PyObject *self, void *closure)
390 const btrace_function * const func = btrace_func_from_recpy_func (self);
392 if (func == NULL)
393 return NULL;
395 if (func->up == 0)
396 Py_RETURN_NONE;
398 return recpy_func_new (((recpy_element_object *) self)->thread,
399 RECORD_METHOD_BTRACE, func->up);
402 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
403 btrace. Returns a previous segment of this function. */
405 PyObject *
406 recpy_bt_func_prev (PyObject *self, void *closure)
408 const btrace_function * const func = btrace_func_from_recpy_func (self);
410 if (func == NULL)
411 return NULL;
413 if (func->prev == 0)
414 Py_RETURN_NONE;
416 return recpy_func_new (((recpy_element_object *) self)->thread,
417 RECORD_METHOD_BTRACE, func->prev);
420 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
421 btrace. Returns a following segment of this function. */
423 PyObject *
424 recpy_bt_func_next (PyObject *self, void *closure)
426 const btrace_function * const func = btrace_func_from_recpy_func (self);
428 if (func == NULL)
429 return NULL;
431 if (func->next == 0)
432 Py_RETURN_NONE;
434 return recpy_func_new (((recpy_element_object *) self)->thread,
435 RECORD_METHOD_BTRACE, func->next);
438 /* Implementation of Auxiliary.data [str] for btrace. */
440 PyObject *
441 recpy_bt_aux_data (PyObject *self, void *closure)
443 const btrace_insn *insn;
444 const recpy_element_object *obj;
445 thread_info *tinfo;
446 btrace_insn_iterator iter;
448 if (Py_TYPE (self) != &recpy_aux_type)
450 PyErr_Format (gdbpy_gdb_error, _("Must be a gdb.Auxiliary."));
451 return nullptr;
454 obj = (const recpy_element_object *) self;
455 tinfo = obj->thread;
457 if (tinfo == nullptr || btrace_is_empty (tinfo))
459 PyErr_Format (gdbpy_gdb_error, _("No such auxiliary object."));
460 return nullptr;
463 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
465 PyErr_Format (gdbpy_gdb_error, _("No such auxiliary object."));
466 return nullptr;
469 insn = btrace_insn_get (&iter);
470 if (insn == nullptr || insn->iclass != BTRACE_INSN_AUX)
472 PyErr_Format (gdbpy_gdb_error, _("Not a valid auxiliary object."));
473 return nullptr;
476 return PyUnicode_FromString
477 (iter.btinfo->aux_data.at (insn->aux_data_index).c_str ());
480 /* Implementation of BtraceList.__len__ (self) -> int. */
482 static Py_ssize_t
483 btpy_list_length (PyObject *self)
485 const btpy_list_object * const obj = (btpy_list_object *) self;
486 const Py_ssize_t distance = obj->last - obj->first;
487 const Py_ssize_t result = distance / obj->step;
489 if ((distance % obj->step) == 0)
490 return result;
492 return result + 1;
495 /* Implementation of
496 BtraceList.__getitem__ (self, key) -> BtraceInstruction,
497 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall,
498 BtraceList.__getitem__ (self, key) -> BtraceAuxiliary. */
500 static PyObject *
501 btpy_list_item (PyObject *self, Py_ssize_t index)
503 const btpy_list_object * const obj = (btpy_list_object *) self;
504 Py_ssize_t number;
506 if (index < 0 || index >= btpy_list_length (self))
507 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
508 index);
510 number = obj->first + (obj->step * index);
512 if (obj->element_type == &recpy_func_type)
513 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
514 else if (obj->element_type == &recpy_insn_type
515 || obj->element_type == &recpy_aux_type)
516 return btpy_item_new (obj->thread, number);
517 else
518 return PyErr_Format (gdbpy_gdb_error, _("Not a valid BtraceList object."));
521 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
523 static PyObject *
524 btpy_list_slice (PyObject *self, PyObject *value)
526 const btpy_list_object * const obj = (btpy_list_object *) self;
527 const Py_ssize_t length = btpy_list_length (self);
528 Py_ssize_t start, stop, step, slicelength;
530 if (PyLong_Check (value))
532 Py_ssize_t index = PyLong_AsSsize_t (value);
534 /* Emulate Python behavior for negative indices. */
535 if (index < 0)
536 index += length;
538 return btpy_list_item (self, index);
541 if (!PySlice_Check (value))
542 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
544 if (0 != PySlice_GetIndicesEx (value, length, &start, &stop,
545 &step, &slicelength))
546 return NULL;
548 return btpy_list_new (obj->thread, obj->first + obj->step * start,
549 obj->first + obj->step * stop, obj->step * step,
550 obj->element_type);
553 /* Helper function that returns the position of an element in a BtraceList
554 or -1 if the element is not in the list. */
556 static LONGEST
557 btpy_list_position (PyObject *self, PyObject *value)
559 const btpy_list_object * const list_obj = (btpy_list_object *) self;
560 const recpy_element_object * const obj = (const recpy_element_object *) value;
561 Py_ssize_t index = obj->number;
563 if (list_obj->element_type != Py_TYPE (value))
564 return -1;
566 if (list_obj->thread != obj->thread)
567 return -1;
569 if (index < list_obj->first || index > list_obj->last)
570 return -1;
572 index -= list_obj->first;
574 if (index % list_obj->step != 0)
575 return -1;
577 return index / list_obj->step;
580 /* Implementation of "in" operator for BtraceLists. */
582 static int
583 btpy_list_contains (PyObject *self, PyObject *value)
585 if (btpy_list_position (self, value) < 0)
586 return 0;
588 return 1;
591 /* Implementation of BtraceLists.index (self, value) -> int. */
593 static PyObject *
594 btpy_list_index (PyObject *self, PyObject *value)
596 const LONGEST index = btpy_list_position (self, value);
598 if (index < 0)
599 return PyErr_Format (PyExc_ValueError, _("Not in list."));
601 return gdb_py_object_from_longest (index).release ();
604 /* Implementation of BtraceList.count (self, value) -> int. */
606 static PyObject *
607 btpy_list_count (PyObject *self, PyObject *value)
609 /* We know that if an element is in the list, it is so exactly one time,
610 enabling us to reuse the "is element of" check. */
611 return gdb_py_object_from_longest (btpy_list_contains (self,
612 value)).release ();
615 /* Python rich compare function to allow for equality and inequality checks
616 in Python. */
618 static PyObject *
619 btpy_list_richcompare (PyObject *self, PyObject *other, int op)
621 const btpy_list_object * const obj1 = (btpy_list_object *) self;
622 const btpy_list_object * const obj2 = (btpy_list_object *) other;
624 if (Py_TYPE (self) != Py_TYPE (other))
626 Py_INCREF (Py_NotImplemented);
627 return Py_NotImplemented;
630 switch (op)
632 case Py_EQ:
633 if (obj1->thread == obj2->thread
634 && obj1->element_type == obj2->element_type
635 && obj1->first == obj2->first
636 && obj1->last == obj2->last
637 && obj1->step == obj2->step)
638 Py_RETURN_TRUE;
639 else
640 Py_RETURN_FALSE;
642 case Py_NE:
643 if (obj1->thread != obj2->thread
644 || obj1->element_type != obj2->element_type
645 || obj1->first != obj2->first
646 || obj1->last != obj2->last
647 || obj1->step != obj2->step)
648 Py_RETURN_TRUE;
649 else
650 Py_RETURN_FALSE;
652 default:
653 break;
656 Py_INCREF (Py_NotImplemented);
657 return Py_NotImplemented;
660 /* Implementation of
661 BtraceRecord.method [str]. */
663 PyObject *
664 recpy_bt_method (PyObject *self, void *closure)
666 return PyUnicode_FromString ("btrace");
669 /* Implementation of
670 BtraceRecord.format [str]. */
672 PyObject *
673 recpy_bt_format (PyObject *self, void *closure)
675 const recpy_record_object * const record = (recpy_record_object *) self;
676 const struct thread_info * const tinfo = record->thread;
677 const struct btrace_config * config;
679 if (tinfo == NULL)
680 Py_RETURN_NONE;
682 config = btrace_conf (&tinfo->btrace);
684 if (config == NULL)
685 Py_RETURN_NONE;
687 return PyUnicode_FromString (btrace_format_short_string (config->format));
690 /* Implementation of
691 BtraceRecord.replay_position [BtraceInstruction]. */
693 PyObject *
694 recpy_bt_replay_position (PyObject *self, void *closure)
696 const recpy_record_object * const record = (recpy_record_object *) self;
697 thread_info * tinfo = record->thread;
699 if (tinfo == NULL)
700 Py_RETURN_NONE;
702 if (tinfo->btrace.replay == NULL)
703 Py_RETURN_NONE;
705 return btpy_item_new (tinfo, btrace_insn_number (tinfo->btrace.replay));
708 /* Implementation of
709 BtraceRecord.begin [BtraceInstruction]. */
711 PyObject *
712 recpy_bt_begin (PyObject *self, void *closure)
714 const recpy_record_object * const record = (recpy_record_object *) self;
715 thread_info *const tinfo = record->thread;
716 struct btrace_insn_iterator iterator;
718 if (tinfo == NULL)
719 Py_RETURN_NONE;
721 btrace_fetch (tinfo, record_btrace_get_cpu ());
723 if (btrace_is_empty (tinfo))
724 Py_RETURN_NONE;
726 btrace_insn_begin (&iterator, &tinfo->btrace);
727 return btpy_item_new (tinfo, btrace_insn_number (&iterator));
730 /* Implementation of
731 BtraceRecord.end [BtraceInstruction]. */
733 PyObject *
734 recpy_bt_end (PyObject *self, void *closure)
736 const recpy_record_object * const record = (recpy_record_object *) self;
737 thread_info *const tinfo = record->thread;
738 struct btrace_insn_iterator iterator;
740 if (tinfo == NULL)
741 Py_RETURN_NONE;
743 btrace_fetch (tinfo, record_btrace_get_cpu ());
745 if (btrace_is_empty (tinfo))
746 Py_RETURN_NONE;
748 btrace_insn_end (&iterator, &tinfo->btrace);
749 return btpy_item_new (tinfo, btrace_insn_number (&iterator));
752 /* Implementation of
753 BtraceRecord.instruction_history [list]. */
755 PyObject *
756 recpy_bt_instruction_history (PyObject *self, void *closure)
758 const recpy_record_object * const record = (recpy_record_object *) self;
759 thread_info *const tinfo = record->thread;
760 struct btrace_insn_iterator iterator;
761 unsigned long first = 0;
762 unsigned long last = 0;
764 if (tinfo == NULL)
765 Py_RETURN_NONE;
767 btrace_fetch (tinfo, record_btrace_get_cpu ());
769 if (btrace_is_empty (tinfo))
770 Py_RETURN_NONE;
772 btrace_insn_begin (&iterator, &tinfo->btrace);
773 first = btrace_insn_number (&iterator);
775 btrace_insn_end (&iterator, &tinfo->btrace);
776 last = btrace_insn_number (&iterator);
778 return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type);
781 /* Implementation of
782 BtraceRecord.function_call_history [list]. */
784 PyObject *
785 recpy_bt_function_call_history (PyObject *self, void *closure)
787 const recpy_record_object * const record = (recpy_record_object *) self;
788 thread_info *const tinfo = record->thread;
789 struct btrace_call_iterator iterator;
790 unsigned long first = 0;
791 unsigned long last = 0;
793 if (tinfo == NULL)
794 Py_RETURN_NONE;
796 btrace_fetch (tinfo, record_btrace_get_cpu ());
798 if (btrace_is_empty (tinfo))
799 Py_RETURN_NONE;
801 btrace_call_begin (&iterator, &tinfo->btrace);
802 first = btrace_call_number (&iterator);
804 btrace_call_end (&iterator, &tinfo->btrace);
805 last = btrace_call_number (&iterator);
807 return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
810 /* Helper function that calls PTW_FILTER with PAYLOAD and IP as arguments.
811 Returns the string that will be printed, if there is a filter to call. */
812 static std::optional<std::string>
813 recpy_call_filter (const uint64_t payload, std::optional<uint64_t> ip,
814 const void *ptw_filter)
816 std::optional<std::string> result;
818 gdb_assert (ptw_filter != nullptr);
819 if ((PyObject *) ptw_filter == Py_None)
820 return result;
822 gdbpy_enter enter_py;
824 gdbpy_ref<> py_payload = gdb_py_object_from_ulongest (payload);
826 gdbpy_ref<> py_ip;
827 if (!ip.has_value ())
828 py_ip = gdbpy_ref<>::new_reference (Py_None);
829 else
830 py_ip = gdb_py_object_from_ulongest (*ip);
832 gdbpy_ref<> py_result (PyObject_CallFunctionObjArgs ((PyObject *) ptw_filter,
833 py_payload.get (),
834 py_ip.get (),
835 nullptr));
837 if (py_result == nullptr)
839 gdbpy_print_stack ();
840 gdbpy_error (_("Couldn't call the ptwrite filter."));
843 /* Py_None is valid and results in no output. */
844 if (py_result == Py_None)
846 result = "";
847 return result;
850 gdb::unique_xmalloc_ptr<char> user_string
851 = gdbpy_obj_to_string (py_result.get ());
853 if (user_string == nullptr)
855 gdbpy_print_stack ();
856 gdbpy_error (_("The ptwrite filter didn't return a string."));
858 else
859 result = user_string.get ();
861 return result;
864 /* Helper function returning the current ptwrite filter. */
866 static PyObject *
867 get_ptwrite_filter ()
869 gdbpy_ref<> module (PyImport_ImportModule ("gdb.ptwrite"));
871 if (PyErr_Occurred ())
873 gdbpy_print_stack ();
874 gdbpy_error (_("Couldn't import gdb.ptwrite."));
877 /* We need to keep the reference count. */
878 gdbpy_ref<> ptw_filter (gdbpy_call_method (module.get (), "get_filter"));
880 if (PyErr_Occurred ())
882 gdbpy_print_stack ();
883 gdbpy_error (_("Couldn't get the ptwrite filter."));
886 return ptw_filter.get();
889 /* Used for registering any python ptwrite filter to the current thread. A
890 pointer to this function is stored in the python extension interface. */
892 void
893 gdbpy_load_ptwrite_filter (const struct extension_language_defn *extlang,
894 struct btrace_thread_info *btinfo)
896 gdb_assert (btinfo != nullptr);
898 gdbpy_enter enter_py;
900 btinfo->ptw_context = get_ptwrite_filter ();
902 #if defined (HAVE_STRUCT_PT_EVENT_VARIANT_PTWRITE)
903 if (!btinfo->target->conf.pt.ptwrite && btinfo->ptw_context != Py_None)
904 warning (_("The target doesn't support decoding ptwrite events."));
905 #else
906 if (btinfo->ptw_context != Py_None)
907 warning (_("Libipt doesn't support decoding ptwrite events."));
908 #endif /* defined (HAVE_STRUCT_PT_EVENT_VARIANT_PTWRITE) */
910 btinfo->ptw_callback_fun = &recpy_call_filter;
913 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
915 PyObject *
916 recpy_bt_goto (PyObject *self, PyObject *args)
918 const recpy_record_object * const record = (recpy_record_object *) self;
919 thread_info *const tinfo = record->thread;
920 const recpy_element_object *obj;
921 PyObject *parse_obj;
923 if (tinfo == NULL || btrace_is_empty (tinfo))
924 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
926 if (!PyArg_ParseTuple (args, "O", &parse_obj))
927 return NULL;
929 if (Py_TYPE (parse_obj) != &recpy_insn_type)
930 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
931 obj = (const recpy_element_object *) parse_obj;
935 struct btrace_insn_iterator iter;
937 btrace_insn_end (&iter, &tinfo->btrace);
939 if (btrace_insn_number (&iter) == obj->number)
940 target_goto_record_end ();
941 else
942 target_goto_record (obj->number);
944 catch (const gdb_exception &except)
946 return gdbpy_handle_gdb_exception (nullptr, except);
949 Py_RETURN_NONE;
952 /* Implementation of BtraceRecord.clear (self) -> None. */
954 PyObject *
955 recpy_bt_clear (PyObject *self, PyObject *args)
957 const recpy_record_object * const record = (recpy_record_object *) self;
958 thread_info *const tinfo = record->thread;
960 btrace_clear (tinfo);
962 Py_RETURN_NONE;
965 /* BtraceList methods. */
967 static PyMethodDef btpy_list_methods[] =
969 { "count", btpy_list_count, METH_O, "count number of occurrences"},
970 { "index", btpy_list_index, METH_O, "index of entry"},
971 {NULL}
974 /* BtraceList sequence methods. */
976 static PySequenceMethods btpy_list_sequence_methods =
978 NULL
981 /* BtraceList mapping methods. Necessary for slicing. */
983 static PyMappingMethods btpy_list_mapping_methods =
985 NULL
988 /* Sets up the btrace record API. */
990 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
991 gdbpy_initialize_btrace (void)
993 btpy_list_type.tp_new = PyType_GenericNew;
994 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
995 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
996 btpy_list_type.tp_name = "gdb.BtraceObjectList";
997 btpy_list_type.tp_doc = "GDB btrace list object";
998 btpy_list_type.tp_methods = btpy_list_methods;
999 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
1000 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
1001 btpy_list_type.tp_richcompare = btpy_list_richcompare;
1003 btpy_list_sequence_methods.sq_item = btpy_list_item;
1004 btpy_list_sequence_methods.sq_length = btpy_list_length;
1005 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
1007 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
1009 return gdbpy_type_ready (&btpy_list_type);
1012 GDBPY_INITIALIZE_FILE (gdbpy_initialize_btrace);