]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-record-btrace.c
gas: introduce .errif and .warnif
[thirdparty/binutils-gdb.git] / gdb / python / py-record-btrace.c
CommitLineData
75c0bdf4
TW
1/* Python interface to btrace instruction history.
2
d01e8234 3 Copyright 2016-2025 Free Software Foundation, Inc.
75c0bdf4
TW
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
75c0bdf4 20#include "gdbcore.h"
5b9707eb 21#include "cli/cli-cmds.h"
75c0bdf4
TW
22#include "gdbthread.h"
23#include "btrace.h"
ae20e79a 24#include "py-record.h"
75c0bdf4 25#include "py-record-btrace.h"
4a4495d6 26#include "record-btrace.h"
75c0bdf4 27#include "disasm.h"
0d12e84c 28#include "gdbarch.h"
75c0bdf4 29
75c0bdf4
TW
30/* Python object for btrace record lists. */
31
f99b5177 32struct btpy_list_object {
75c0bdf4
TW
33 PyObject_HEAD
34
35 /* The thread this list belongs to. */
00431a78 36 thread_info *thread;
75c0bdf4
TW
37
38 /* The first index being part of this list. */
39 Py_ssize_t first;
40
41 /* The last index begin part of this list. */
42 Py_ssize_t last;
43
44 /* Stride size. */
45 Py_ssize_t step;
46
bea4f6fa
FW
47 /* Either &recpy_func_type, &recpy_insn_type, &recpy_aux_type or
48 &recpy_gap_type. */
75c0bdf4 49 PyTypeObject* element_type;
f99b5177 50};
75c0bdf4 51
75c0bdf4
TW
52/* Python type for btrace lists. */
53
54static PyTypeObject btpy_list_type = {
55 PyVarObject_HEAD_INIT (NULL, 0)
56};
57
0ed5da75
TW
58/* Returns either a btrace_insn for the given Python gdb.RecordInstruction
59 object or sets an appropriate Python exception and returns NULL. */
75c0bdf4 60
0ed5da75
TW
61static const btrace_insn *
62btrace_insn_from_recpy_insn (const PyObject * const pyobject)
75c0bdf4 63{
0ed5da75
TW
64 const btrace_insn *insn;
65 const recpy_element_object *obj;
66 thread_info *tinfo;
67 btrace_insn_iterator iter;
75c0bdf4 68
0ed5da75
TW
69 if (Py_TYPE (pyobject) != &recpy_insn_type)
70 {
71 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction"));
72 return NULL;
73 }
75c0bdf4 74
0ed5da75 75 obj = (const recpy_element_object *) pyobject;
00431a78 76 tinfo = obj->thread;
75c0bdf4 77
0ed5da75
TW
78 if (tinfo == NULL || btrace_is_empty (tinfo))
79 {
80 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
81 return NULL;
82 }
83
84 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
85 {
86 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
87 return NULL;
88 }
89
90 insn = btrace_insn_get (&iter);
91 if (insn == NULL)
92 {
93 PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction."));
94 return NULL;
95 }
96
97 return insn;
75c0bdf4
TW
98}
99
14f819c8
TW
100/* Returns either a btrace_function for the given Python
101 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
102 returns NULL. */
103
104static const btrace_function *
105btrace_func_from_recpy_func (const PyObject * const pyobject)
106{
107 const btrace_function *func;
108 const recpy_element_object *obj;
109 thread_info *tinfo;
110 btrace_call_iterator iter;
111
112 if (Py_TYPE (pyobject) != &recpy_func_type)
113 {
114 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment"));
115 return NULL;
116 }
117
118 obj = (const recpy_element_object *) pyobject;
00431a78 119 tinfo = obj->thread;
14f819c8
TW
120
121 if (tinfo == NULL || btrace_is_empty (tinfo))
122 {
123 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
124 return NULL;
125 }
126
127 if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0)
128 {
129 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
130 return NULL;
131 }
132
133 func = btrace_call_get (&iter);
134 if (func == NULL)
135 {
136 PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment."));
137 return NULL;
138 }
139
140 return func;
141}
142
913aeadd 143/* Looks at the recorded item with the number NUMBER and create a
bea4f6fa
FW
144 gdb.RecordInstruction, gdb.RecordGap or gdb.RecordAuxiliary object
145 for it accordingly. */
75c0bdf4
TW
146
147static PyObject *
bea4f6fa 148btpy_item_new (thread_info *tinfo, Py_ssize_t number)
75c0bdf4 149{
913aeadd
TW
150 btrace_insn_iterator iter;
151 int err_code;
152
bea4f6fa
FW
153 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, number) == 0)
154 {
155 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
156 return nullptr;
157 }
158
913aeadd
TW
159 err_code = btrace_insn_get_error (&iter);
160
161 if (err_code != 0)
162 {
163 const btrace_config *config;
164 const char *err_string;
165
166 config = btrace_conf (&tinfo->btrace);
167 err_string = btrace_decode_error (config->format, err_code);
168
169 return recpy_gap_new (err_code, err_string, number);
170 }
171
bea4f6fa
FW
172 const struct btrace_insn *insn = btrace_insn_get (&iter);
173 gdb_assert (insn != nullptr);
174
175 if (insn->iclass == BTRACE_INSN_AUX)
176 return recpy_aux_new (tinfo, RECORD_METHOD_BTRACE, number);
177
00431a78 178 return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number);
75c0bdf4
TW
179}
180
75c0bdf4
TW
181/* Create a new gdb.BtraceList object. */
182
183static PyObject *
e9683acf
FW
184btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last,
185 Py_ssize_t step, PyTypeObject *element_type)
75c0bdf4
TW
186{
187 btpy_list_object * const obj = PyObject_New (btpy_list_object,
188 &btpy_list_type);
189
190 if (obj == NULL)
191 return NULL;
192
00431a78 193 obj->thread = thread;
75c0bdf4
TW
194 obj->first = first;
195 obj->last = last;
196 obj->step = step;
197 obj->element_type = element_type;
198
199 return (PyObject *) obj;
200}
201
0ed5da75
TW
202/* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
203 Returns the SAL associated with this instruction. */
75c0bdf4 204
0ed5da75
TW
205PyObject *
206recpy_bt_insn_sal (PyObject *self, void *closure)
75c0bdf4 207{
0ed5da75 208 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
209 PyObject *result = NULL;
210
75c0bdf4 211 if (insn == NULL)
0ed5da75 212 return NULL;
75c0bdf4 213
a70b8144 214 try
75c0bdf4
TW
215 {
216 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
217 }
230d2906 218 catch (const gdb_exception &except)
75c0bdf4 219 {
1ccb6f10 220 return gdbpy_handle_gdb_exception (nullptr, except);
75c0bdf4 221 }
75c0bdf4
TW
222
223 return result;
224}
225
0ed5da75
TW
226/* Implementation of RecordInstruction.pc [int] for btrace.
227 Returns the instruction address. */
75c0bdf4 228
0ed5da75
TW
229PyObject *
230recpy_bt_insn_pc (PyObject *self, void *closure)
75c0bdf4 231{
0ed5da75 232 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 233
75c0bdf4 234 if (insn == NULL)
0ed5da75 235 return NULL;
75c0bdf4 236
d1cab987 237 return gdb_py_object_from_ulongest (insn->pc).release ();
75c0bdf4
TW
238}
239
0ed5da75
TW
240/* Implementation of RecordInstruction.size [int] for btrace.
241 Returns the instruction size. */
75c0bdf4 242
0ed5da75
TW
243PyObject *
244recpy_bt_insn_size (PyObject *self, void *closure)
75c0bdf4 245{
0ed5da75 246 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 247
75c0bdf4 248 if (insn == NULL)
0ed5da75 249 return NULL;
75c0bdf4 250
47f0e2ff 251 return gdb_py_object_from_longest (insn->size).release ();
75c0bdf4
TW
252}
253
0ed5da75 254/* Implementation of RecordInstruction.is_speculative [bool] for btrace.
75c0bdf4
TW
255 Returns if this instruction was executed speculatively. */
256
0ed5da75
TW
257PyObject *
258recpy_bt_insn_is_speculative (PyObject *self, void *closure)
75c0bdf4 259{
0ed5da75 260 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 261
75c0bdf4 262 if (insn == NULL)
0ed5da75 263 return NULL;
75c0bdf4
TW
264
265 if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
266 Py_RETURN_TRUE;
267 else
268 Py_RETURN_FALSE;
269}
270
0ed5da75 271/* Implementation of RecordInstruction.data [buffer] for btrace.
75c0bdf4
TW
272 Returns raw instruction data. */
273
0ed5da75
TW
274PyObject *
275recpy_bt_insn_data (PyObject *self, void *closure)
75c0bdf4 276{
0ed5da75 277 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
075c55e0 278 gdb::byte_vector buffer;
75c0bdf4
TW
279 PyObject *object;
280
75c0bdf4 281 if (insn == NULL)
0ed5da75 282 return NULL;
75c0bdf4 283
a70b8144 284 try
75c0bdf4 285 {
075c55e0
TT
286 buffer.resize (insn->size);
287 read_memory (insn->pc, buffer.data (), insn->size);
75c0bdf4 288 }
230d2906 289 catch (const gdb_exception &except)
75c0bdf4 290 {
1ccb6f10 291 return gdbpy_handle_gdb_exception (nullptr, except);
75c0bdf4 292 }
75c0bdf4 293
075c55e0
TT
294 object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
295 insn->size);
75c0bdf4
TW
296
297 if (object == NULL)
298 return NULL;
299
300 return PyMemoryView_FromObject (object);
301}
302
0ed5da75
TW
303/* Implementation of RecordInstruction.decoded [str] for btrace.
304 Returns the instruction as human readable string. */
75c0bdf4 305
0ed5da75
TW
306PyObject *
307recpy_bt_insn_decoded (PyObject *self, void *closure)
75c0bdf4 308{
0ed5da75 309 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
310 string_file strfile;
311
75c0bdf4 312 if (insn == NULL)
0ed5da75 313 return NULL;
75c0bdf4 314
a70b8144 315 try
75c0bdf4 316 {
99d9c3b9 317 gdb_print_insn (current_inferior ()->arch (), insn->pc, &strfile, NULL);
75c0bdf4 318 }
230d2906 319 catch (const gdb_exception &except)
75c0bdf4 320 {
1ccb6f10 321 return gdbpy_handle_gdb_exception (nullptr, except);
75c0bdf4 322 }
75c0bdf4 323
75c0bdf4
TW
324 return PyBytes_FromString (strfile.string ().c_str ());
325}
326
14f819c8
TW
327/* Implementation of RecordFunctionSegment.level [int] for btrace.
328 Returns the call level. */
75c0bdf4 329
14f819c8
TW
330PyObject *
331recpy_bt_func_level (PyObject *self, void *closure)
75c0bdf4 332{
14f819c8
TW
333 const btrace_function * const func = btrace_func_from_recpy_func (self);
334 thread_info *tinfo;
75c0bdf4 335
75c0bdf4 336 if (func == NULL)
14f819c8 337 return NULL;
75c0bdf4 338
00431a78 339 tinfo = ((recpy_element_object *) self)->thread;
47f0e2ff
TT
340 return gdb_py_object_from_longest (tinfo->btrace.level
341 + func->level).release ();
75c0bdf4
TW
342}
343
14f819c8
TW
344/* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
345 Returns the symbol associated with this function call. */
75c0bdf4 346
14f819c8
TW
347PyObject *
348recpy_bt_func_symbol (PyObject *self, void *closure)
75c0bdf4 349{
14f819c8 350 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 351
75c0bdf4 352 if (func == NULL)
14f819c8 353 return NULL;
75c0bdf4
TW
354
355 if (func->sym == NULL)
356 Py_RETURN_NONE;
357
358 return symbol_to_symbol_object (func->sym);
359}
360
14f819c8
TW
361/* Implementation of RecordFunctionSegment.instructions [list] for btrace.
362 Returns the list of instructions that belong to this function call. */
75c0bdf4 363
14f819c8
TW
364PyObject *
365recpy_bt_func_instructions (PyObject *self, void *closure)
75c0bdf4 366{
14f819c8 367 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4
TW
368 unsigned int len;
369
75c0bdf4 370 if (func == NULL)
14f819c8 371 return NULL;
75c0bdf4 372
0860c437 373 len = func->insn.size ();
75c0bdf4
TW
374
375 /* Gaps count as one instruction. */
376 if (len == 0)
377 len = 1;
378
00431a78 379 return btpy_list_new (((recpy_element_object *) self)->thread,
14f819c8
TW
380 func->insn_offset, func->insn_offset + len, 1,
381 &recpy_insn_type);
75c0bdf4
TW
382}
383
14f819c8
TW
384/* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
385 btrace. Returns the caller / returnee of this function. */
75c0bdf4 386
14f819c8
TW
387PyObject *
388recpy_bt_func_up (PyObject *self, void *closure)
75c0bdf4 389{
14f819c8 390 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 391
75c0bdf4 392 if (func == NULL)
14f819c8 393 return NULL;
75c0bdf4 394
42bfe59e 395 if (func->up == 0)
75c0bdf4
TW
396 Py_RETURN_NONE;
397
00431a78 398 return recpy_func_new (((recpy_element_object *) self)->thread,
42bfe59e 399 RECORD_METHOD_BTRACE, func->up);
75c0bdf4
TW
400}
401
14f819c8
TW
402/* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
403 btrace. Returns a previous segment of this function. */
75c0bdf4 404
14f819c8
TW
405PyObject *
406recpy_bt_func_prev (PyObject *self, void *closure)
75c0bdf4 407{
14f819c8 408 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 409
75c0bdf4 410 if (func == NULL)
14f819c8 411 return NULL;
75c0bdf4 412
4aeb0dfc 413 if (func->prev == 0)
75c0bdf4
TW
414 Py_RETURN_NONE;
415
00431a78 416 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 417 RECORD_METHOD_BTRACE, func->prev);
75c0bdf4
TW
418}
419
14f819c8
TW
420/* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
421 btrace. Returns a following segment of this function. */
75c0bdf4 422
14f819c8
TW
423PyObject *
424recpy_bt_func_next (PyObject *self, void *closure)
75c0bdf4 425{
14f819c8 426 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 427
75c0bdf4 428 if (func == NULL)
14f819c8 429 return NULL;
75c0bdf4 430
4aeb0dfc 431 if (func->next == 0)
75c0bdf4
TW
432 Py_RETURN_NONE;
433
00431a78 434 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 435 RECORD_METHOD_BTRACE, func->next);
75c0bdf4
TW
436}
437
bea4f6fa
FW
438/* Implementation of Auxiliary.data [str] for btrace. */
439
440PyObject *
441recpy_bt_aux_data (PyObject *self, void *closure)
442{
443 const btrace_insn *insn;
444 const recpy_element_object *obj;
445 thread_info *tinfo;
446 btrace_insn_iterator iter;
447
448 if (Py_TYPE (self) != &recpy_aux_type)
449 {
450 PyErr_Format (gdbpy_gdb_error, _("Must be a gdb.Auxiliary."));
451 return nullptr;
452 }
453
454 obj = (const recpy_element_object *) self;
455 tinfo = obj->thread;
456
457 if (tinfo == nullptr || btrace_is_empty (tinfo))
458 {
459 PyErr_Format (gdbpy_gdb_error, _("No such auxiliary object."));
460 return nullptr;
461 }
462
463 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
464 {
465 PyErr_Format (gdbpy_gdb_error, _("No such auxiliary object."));
466 return nullptr;
467 }
468
469 insn = btrace_insn_get (&iter);
470 if (insn == nullptr || insn->iclass != BTRACE_INSN_AUX)
471 {
472 PyErr_Format (gdbpy_gdb_error, _("Not a valid auxiliary object."));
473 return nullptr;
474 }
475
476 return PyUnicode_FromString
477 (iter.btinfo->aux_data.at (insn->aux_data_index).c_str ());
478}
479
75c0bdf4
TW
480/* Implementation of BtraceList.__len__ (self) -> int. */
481
482static Py_ssize_t
483btpy_list_length (PyObject *self)
484{
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;
488
489 if ((distance % obj->step) == 0)
490 return result;
491
492 return result + 1;
493}
494
495/* Implementation of
bea4f6fa
FW
496 BtraceList.__getitem__ (self, key) -> BtraceInstruction,
497 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall,
498 BtraceList.__getitem__ (self, key) -> BtraceAuxiliary. */
75c0bdf4
TW
499
500static PyObject *
501btpy_list_item (PyObject *self, Py_ssize_t index)
502{
503 const btpy_list_object * const obj = (btpy_list_object *) self;
0ed5da75 504 Py_ssize_t number;
75c0bdf4
TW
505
506 if (index < 0 || index >= btpy_list_length (self))
507 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
508 index);
509
0ed5da75
TW
510 number = obj->first + (obj->step * index);
511
bea4f6fa 512 if (obj->element_type == &recpy_func_type)
00431a78 513 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
bea4f6fa
FW
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."));
75c0bdf4
TW
519}
520
521/* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
522
523static PyObject *
524btpy_list_slice (PyObject *self, PyObject *value)
525{
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;
529
5aee4587 530 if (PyLong_Check (value))
75c0bdf4 531 {
5aee4587 532 Py_ssize_t index = PyLong_AsSsize_t (value);
75c0bdf4
TW
533
534 /* Emulate Python behavior for negative indices. */
535 if (index < 0)
536 index += length;
537
538 return btpy_list_item (self, index);
539 }
540
541 if (!PySlice_Check (value))
542 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
543
edae3fd6 544 if (0 != PySlice_GetIndicesEx (value, length, &start, &stop,
75c0bdf4
TW
545 &step, &slicelength))
546 return NULL;
547
00431a78 548 return btpy_list_new (obj->thread, obj->first + obj->step * start,
75c0bdf4
TW
549 obj->first + obj->step * stop, obj->step * step,
550 obj->element_type);
551}
552
553/* Helper function that returns the position of an element in a BtraceList
554 or -1 if the element is not in the list. */
555
556static LONGEST
557btpy_list_position (PyObject *self, PyObject *value)
558{
559 const btpy_list_object * const list_obj = (btpy_list_object *) self;
0ed5da75 560 const recpy_element_object * const obj = (const recpy_element_object *) value;
75c0bdf4
TW
561 Py_ssize_t index = obj->number;
562
563 if (list_obj->element_type != Py_TYPE (value))
564 return -1;
565
00431a78 566 if (list_obj->thread != obj->thread)
75c0bdf4
TW
567 return -1;
568
569 if (index < list_obj->first || index > list_obj->last)
570 return -1;
571
572 index -= list_obj->first;
573
574 if (index % list_obj->step != 0)
575 return -1;
576
577 return index / list_obj->step;
578}
579
580/* Implementation of "in" operator for BtraceLists. */
581
582static int
583btpy_list_contains (PyObject *self, PyObject *value)
584{
585 if (btpy_list_position (self, value) < 0)
586 return 0;
587
588 return 1;
589}
590
591/* Implementation of BtraceLists.index (self, value) -> int. */
592
593static PyObject *
594btpy_list_index (PyObject *self, PyObject *value)
595{
596 const LONGEST index = btpy_list_position (self, value);
597
598 if (index < 0)
599 return PyErr_Format (PyExc_ValueError, _("Not in list."));
600
4bde49dc 601 return gdb_py_object_from_longest (index).release ();
75c0bdf4
TW
602}
603
604/* Implementation of BtraceList.count (self, value) -> int. */
605
606static PyObject *
607btpy_list_count (PyObject *self, PyObject *value)
608{
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. */
47f0e2ff
TT
611 return gdb_py_object_from_longest (btpy_list_contains (self,
612 value)).release ();
75c0bdf4
TW
613}
614
615/* Python rich compare function to allow for equality and inequality checks
616 in Python. */
617
618static PyObject *
619btpy_list_richcompare (PyObject *self, PyObject *other, int op)
620{
621 const btpy_list_object * const obj1 = (btpy_list_object *) self;
622 const btpy_list_object * const obj2 = (btpy_list_object *) other;
623
624 if (Py_TYPE (self) != Py_TYPE (other))
625 {
626 Py_INCREF (Py_NotImplemented);
627 return Py_NotImplemented;
628 }
629
630 switch (op)
631 {
632 case Py_EQ:
00431a78 633 if (obj1->thread == obj2->thread
75c0bdf4
TW
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;
641
642 case Py_NE:
00431a78 643 if (obj1->thread != obj2->thread
75c0bdf4
TW
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;
651
652 default:
653 break;
654 }
655
656 Py_INCREF (Py_NotImplemented);
657 return Py_NotImplemented;
658}
659
660/* Implementation of
661 BtraceRecord.method [str]. */
662
663PyObject *
664recpy_bt_method (PyObject *self, void *closure)
665{
5aee4587 666 return PyUnicode_FromString ("btrace");
75c0bdf4
TW
667}
668
669/* Implementation of
670 BtraceRecord.format [str]. */
671
672PyObject *
673recpy_bt_format (PyObject *self, void *closure)
674{
ae20e79a 675 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 676 const struct thread_info * const tinfo = record->thread;
75c0bdf4
TW
677 const struct btrace_config * config;
678
679 if (tinfo == NULL)
680 Py_RETURN_NONE;
681
682 config = btrace_conf (&tinfo->btrace);
683
684 if (config == NULL)
685 Py_RETURN_NONE;
686
5aee4587 687 return PyUnicode_FromString (btrace_format_short_string (config->format));
75c0bdf4
TW
688}
689
690/* Implementation of
691 BtraceRecord.replay_position [BtraceInstruction]. */
692
693PyObject *
694recpy_bt_replay_position (PyObject *self, void *closure)
695{
ae20e79a 696 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 697 thread_info * tinfo = record->thread;
75c0bdf4
TW
698
699 if (tinfo == NULL)
700 Py_RETURN_NONE;
701
702 if (tinfo->btrace.replay == NULL)
703 Py_RETURN_NONE;
704
bea4f6fa 705 return btpy_item_new (tinfo, btrace_insn_number (tinfo->btrace.replay));
75c0bdf4
TW
706}
707
708/* Implementation of
709 BtraceRecord.begin [BtraceInstruction]. */
710
711PyObject *
712recpy_bt_begin (PyObject *self, void *closure)
713{
ae20e79a 714 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 715 thread_info *const tinfo = record->thread;
75c0bdf4
TW
716 struct btrace_insn_iterator iterator;
717
718 if (tinfo == NULL)
719 Py_RETURN_NONE;
720
4a4495d6 721 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
722
723 if (btrace_is_empty (tinfo))
724 Py_RETURN_NONE;
725
726 btrace_insn_begin (&iterator, &tinfo->btrace);
bea4f6fa 727 return btpy_item_new (tinfo, btrace_insn_number (&iterator));
75c0bdf4
TW
728}
729
730/* Implementation of
731 BtraceRecord.end [BtraceInstruction]. */
732
733PyObject *
734recpy_bt_end (PyObject *self, void *closure)
735{
ae20e79a 736 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 737 thread_info *const tinfo = record->thread;
75c0bdf4
TW
738 struct btrace_insn_iterator iterator;
739
740 if (tinfo == NULL)
741 Py_RETURN_NONE;
742
4a4495d6 743 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
744
745 if (btrace_is_empty (tinfo))
746 Py_RETURN_NONE;
747
748 btrace_insn_end (&iterator, &tinfo->btrace);
bea4f6fa 749 return btpy_item_new (tinfo, btrace_insn_number (&iterator));
75c0bdf4
TW
750}
751
752/* Implementation of
753 BtraceRecord.instruction_history [list]. */
754
755PyObject *
756recpy_bt_instruction_history (PyObject *self, void *closure)
757{
ae20e79a 758 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 759 thread_info *const tinfo = record->thread;
75c0bdf4
TW
760 struct btrace_insn_iterator iterator;
761 unsigned long first = 0;
762 unsigned long last = 0;
763
764 if (tinfo == NULL)
765 Py_RETURN_NONE;
766
4a4495d6 767 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
768
769 if (btrace_is_empty (tinfo))
770 Py_RETURN_NONE;
771
772 btrace_insn_begin (&iterator, &tinfo->btrace);
773 first = btrace_insn_number (&iterator);
774
775 btrace_insn_end (&iterator, &tinfo->btrace);
776 last = btrace_insn_number (&iterator);
777
00431a78 778 return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type);
75c0bdf4
TW
779}
780
781/* Implementation of
782 BtraceRecord.function_call_history [list]. */
783
784PyObject *
785recpy_bt_function_call_history (PyObject *self, void *closure)
786{
ae20e79a 787 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 788 thread_info *const tinfo = record->thread;
8d0050ea
TW
789 struct btrace_call_iterator iterator;
790 unsigned long first = 0;
791 unsigned long last = 0;
75c0bdf4 792
8d0050ea
TW
793 if (tinfo == NULL)
794 Py_RETURN_NONE;
75c0bdf4 795
4a4495d6 796 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4 797
8d0050ea
TW
798 if (btrace_is_empty (tinfo))
799 Py_RETURN_NONE;
75c0bdf4 800
8d0050ea
TW
801 btrace_call_begin (&iterator, &tinfo->btrace);
802 first = btrace_call_number (&iterator);
75c0bdf4 803
8d0050ea
TW
804 btrace_call_end (&iterator, &tinfo->btrace);
805 last = btrace_call_number (&iterator);
75c0bdf4 806
00431a78 807 return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
75c0bdf4
TW
808}
809
6be9971c
FW
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. */
812static std::optional<std::string>
cdd65168 813recpy_call_filter (const uint64_t payload, std::optional<uint64_t> ip,
6be9971c
FW
814 const void *ptw_filter)
815{
816 std::optional<std::string> result;
817
818 gdb_assert (ptw_filter != nullptr);
819 if ((PyObject *) ptw_filter == Py_None)
820 return result;
821
822 gdbpy_enter enter_py;
823
824 gdbpy_ref<> py_payload = gdb_py_object_from_ulongest (payload);
825
826 gdbpy_ref<> py_ip;
cdd65168 827 if (!ip.has_value ())
6be9971c
FW
828 py_ip = gdbpy_ref<>::new_reference (Py_None);
829 else
cdd65168 830 py_ip = gdb_py_object_from_ulongest (*ip);
6be9971c
FW
831
832 gdbpy_ref<> py_result (PyObject_CallFunctionObjArgs ((PyObject *) ptw_filter,
833 py_payload.get (),
834 py_ip.get (),
835 nullptr));
836
837 if (py_result == nullptr)
838 {
839 gdbpy_print_stack ();
840 gdbpy_error (_("Couldn't call the ptwrite filter."));
841 }
842
843 /* Py_None is valid and results in no output. */
844 if (py_result == Py_None)
845 {
846 result = "";
847 return result;
848 }
849
850 gdb::unique_xmalloc_ptr<char> user_string
851 = gdbpy_obj_to_string (py_result.get ());
852
853 if (user_string == nullptr)
854 {
855 gdbpy_print_stack ();
856 gdbpy_error (_("The ptwrite filter didn't return a string."));
857 }
858 else
859 result = user_string.get ();
860
861 return result;
862}
863
864/* Helper function returning the current ptwrite filter. */
865
866static PyObject *
867get_ptwrite_filter ()
868{
869 gdbpy_ref<> module (PyImport_ImportModule ("gdb.ptwrite"));
870
871 if (PyErr_Occurred ())
872 {
873 gdbpy_print_stack ();
874 gdbpy_error (_("Couldn't import gdb.ptwrite."));
875 }
876
877 /* We need to keep the reference count. */
878 gdbpy_ref<> ptw_filter (gdbpy_call_method (module.get (), "get_filter"));
879
880 if (PyErr_Occurred ())
881 {
882 gdbpy_print_stack ();
883 gdbpy_error (_("Couldn't get the ptwrite filter."));
884 }
885
886 return ptw_filter.get();
887}
888
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. */
891
892void
893gdbpy_load_ptwrite_filter (const struct extension_language_defn *extlang,
894 struct btrace_thread_info *btinfo)
895{
896 gdb_assert (btinfo != nullptr);
897
898 gdbpy_enter enter_py;
899
900 btinfo->ptw_context = get_ptwrite_filter ();
901
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) */
909
910 btinfo->ptw_callback_fun = &recpy_call_filter;
911}
912
75c0bdf4
TW
913/* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
914
915PyObject *
916recpy_bt_goto (PyObject *self, PyObject *args)
917{
ae20e79a 918 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 919 thread_info *const tinfo = record->thread;
0ed5da75 920 const recpy_element_object *obj;
96b1ad86 921 PyObject *parse_obj;
75c0bdf4
TW
922
923 if (tinfo == NULL || btrace_is_empty (tinfo))
924 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
925
96b1ad86 926 if (!PyArg_ParseTuple (args, "O", &parse_obj))
75c0bdf4
TW
927 return NULL;
928
96b1ad86 929 if (Py_TYPE (parse_obj) != &recpy_insn_type)
75c0bdf4 930 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
96b1ad86 931 obj = (const recpy_element_object *) parse_obj;
75c0bdf4 932
a70b8144 933 try
75c0bdf4
TW
934 {
935 struct btrace_insn_iterator iter;
936
937 btrace_insn_end (&iter, &tinfo->btrace);
938
939 if (btrace_insn_number (&iter) == obj->number)
940 target_goto_record_end ();
941 else
942 target_goto_record (obj->number);
943 }
230d2906 944 catch (const gdb_exception &except)
75c0bdf4 945 {
1ccb6f10 946 return gdbpy_handle_gdb_exception (nullptr, except);
75c0bdf4 947 }
75c0bdf4
TW
948
949 Py_RETURN_NONE;
950}
951
8958aefd
FW
952/* Implementation of BtraceRecord.clear (self) -> None. */
953
954PyObject *
955recpy_bt_clear (PyObject *self, PyObject *args)
956{
957 const recpy_record_object * const record = (recpy_record_object *) self;
958 thread_info *const tinfo = record->thread;
959
960 btrace_clear (tinfo);
961
962 Py_RETURN_NONE;
963}
964
75c0bdf4
TW
965/* BtraceList methods. */
966
6bd434d6 967static PyMethodDef btpy_list_methods[] =
75c0bdf4 968{
40c94099 969 { "count", btpy_list_count, METH_O, "count number of occurrences"},
75c0bdf4
TW
970 { "index", btpy_list_index, METH_O, "index of entry"},
971 {NULL}
972};
973
974/* BtraceList sequence methods. */
975
976static PySequenceMethods btpy_list_sequence_methods =
977{
978 NULL
979};
980
981/* BtraceList mapping methods. Necessary for slicing. */
982
983static PyMappingMethods btpy_list_mapping_methods =
984{
985 NULL
986};
987
988/* Sets up the btrace record API. */
989
3965bff5 990static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
75c0bdf4
TW
991gdbpy_initialize_btrace (void)
992{
75c0bdf4
TW
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;
1002
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;
1006
1007 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
1008
336bb2a1 1009 return gdbpy_type_ready (&btpy_list_type);
75c0bdf4 1010}
3965bff5
AB
1011
1012GDBPY_INITIALIZE_FILE (gdbpy_initialize_btrace);