]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-record-btrace.c
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / python / py-record-btrace.c
CommitLineData
75c0bdf4
TW
1/* Python interface to btrace instruction history.
2
4a94e368 3 Copyright 2016-2022 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
20#include "defs.h"
21#include "gdbcore.h"
22#include "gdbcmd.h"
23#include "gdbthread.h"
24#include "btrace.h"
ae20e79a 25#include "py-record.h"
75c0bdf4 26#include "py-record-btrace.h"
4a4495d6 27#include "record-btrace.h"
75c0bdf4 28#include "disasm.h"
0d12e84c 29#include "gdbarch.h"
75c0bdf4
TW
30
31#if defined (IS_PY3K)
32
33#define BTPY_PYSLICE(x) (x)
34
35#else
36
37#define BTPY_PYSLICE(x) ((PySliceObject *) x)
38
39#endif
40
75c0bdf4
TW
41/* Python object for btrace record lists. */
42
f99b5177 43struct btpy_list_object {
75c0bdf4
TW
44 PyObject_HEAD
45
46 /* The thread this list belongs to. */
00431a78 47 thread_info *thread;
75c0bdf4
TW
48
49 /* The first index being part of this list. */
50 Py_ssize_t first;
51
52 /* The last index begin part of this list. */
53 Py_ssize_t last;
54
55 /* Stride size. */
56 Py_ssize_t step;
57
0ed5da75 58 /* Either &BTPY_CALL_TYPE or &RECPY_INSN_TYPE. */
75c0bdf4 59 PyTypeObject* element_type;
f99b5177 60};
75c0bdf4 61
75c0bdf4
TW
62/* Python type for btrace lists. */
63
64static PyTypeObject btpy_list_type = {
65 PyVarObject_HEAD_INIT (NULL, 0)
66};
67
0ed5da75
TW
68/* Returns either a btrace_insn for the given Python gdb.RecordInstruction
69 object or sets an appropriate Python exception and returns NULL. */
75c0bdf4 70
0ed5da75
TW
71static const btrace_insn *
72btrace_insn_from_recpy_insn (const PyObject * const pyobject)
75c0bdf4 73{
0ed5da75
TW
74 const btrace_insn *insn;
75 const recpy_element_object *obj;
76 thread_info *tinfo;
77 btrace_insn_iterator iter;
75c0bdf4 78
0ed5da75
TW
79 if (Py_TYPE (pyobject) != &recpy_insn_type)
80 {
81 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction"));
82 return NULL;
83 }
75c0bdf4 84
0ed5da75 85 obj = (const recpy_element_object *) pyobject;
00431a78 86 tinfo = obj->thread;
75c0bdf4 87
0ed5da75
TW
88 if (tinfo == NULL || btrace_is_empty (tinfo))
89 {
90 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
91 return NULL;
92 }
93
94 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
95 {
96 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
97 return NULL;
98 }
99
100 insn = btrace_insn_get (&iter);
101 if (insn == NULL)
102 {
103 PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction."));
104 return NULL;
105 }
106
107 return insn;
75c0bdf4
TW
108}
109
14f819c8
TW
110/* Returns either a btrace_function for the given Python
111 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
112 returns NULL. */
113
114static const btrace_function *
115btrace_func_from_recpy_func (const PyObject * const pyobject)
116{
117 const btrace_function *func;
118 const recpy_element_object *obj;
119 thread_info *tinfo;
120 btrace_call_iterator iter;
121
122 if (Py_TYPE (pyobject) != &recpy_func_type)
123 {
124 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment"));
125 return NULL;
126 }
127
128 obj = (const recpy_element_object *) pyobject;
00431a78 129 tinfo = obj->thread;
14f819c8
TW
130
131 if (tinfo == NULL || btrace_is_empty (tinfo))
132 {
133 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
134 return NULL;
135 }
136
137 if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0)
138 {
139 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
140 return NULL;
141 }
142
143 func = btrace_call_get (&iter);
144 if (func == NULL)
145 {
146 PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment."));
147 return NULL;
148 }
149
150 return func;
151}
152
913aeadd 153/* Looks at the recorded item with the number NUMBER and create a
0ed5da75 154 gdb.RecordInstruction or gdb.RecordGap object for it accordingly. */
75c0bdf4
TW
155
156static PyObject *
00431a78 157btpy_insn_or_gap_new (thread_info *tinfo, Py_ssize_t number)
75c0bdf4 158{
913aeadd
TW
159 btrace_insn_iterator iter;
160 int err_code;
161
162 btrace_find_insn_by_number (&iter, &tinfo->btrace, number);
163 err_code = btrace_insn_get_error (&iter);
164
165 if (err_code != 0)
166 {
167 const btrace_config *config;
168 const char *err_string;
169
170 config = btrace_conf (&tinfo->btrace);
171 err_string = btrace_decode_error (config->format, err_code);
172
173 return recpy_gap_new (err_code, err_string, number);
174 }
175
00431a78 176 return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number);
75c0bdf4
TW
177}
178
75c0bdf4
TW
179/* Create a new gdb.BtraceList object. */
180
181static PyObject *
00431a78 182btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last, Py_ssize_t step,
75c0bdf4
TW
183 PyTypeObject *element_type)
184{
185 btpy_list_object * const obj = PyObject_New (btpy_list_object,
186 &btpy_list_type);
187
188 if (obj == NULL)
189 return NULL;
190
00431a78 191 obj->thread = thread;
75c0bdf4
TW
192 obj->first = first;
193 obj->last = last;
194 obj->step = step;
195 obj->element_type = element_type;
196
197 return (PyObject *) obj;
198}
199
0ed5da75
TW
200/* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
201 Returns the SAL associated with this instruction. */
75c0bdf4 202
0ed5da75
TW
203PyObject *
204recpy_bt_insn_sal (PyObject *self, void *closure)
75c0bdf4 205{
0ed5da75 206 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
207 PyObject *result = NULL;
208
75c0bdf4 209 if (insn == NULL)
0ed5da75 210 return NULL;
75c0bdf4 211
a70b8144 212 try
75c0bdf4
TW
213 {
214 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
215 }
230d2906 216 catch (const gdb_exception &except)
75c0bdf4
TW
217 {
218 GDB_PY_HANDLE_EXCEPTION (except);
219 }
75c0bdf4
TW
220
221 return result;
222}
223
0ed5da75
TW
224/* Implementation of RecordInstruction.pc [int] for btrace.
225 Returns the instruction address. */
75c0bdf4 226
0ed5da75
TW
227PyObject *
228recpy_bt_insn_pc (PyObject *self, void *closure)
75c0bdf4 229{
0ed5da75 230 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 231
75c0bdf4 232 if (insn == NULL)
0ed5da75 233 return NULL;
75c0bdf4 234
d1cab987 235 return gdb_py_object_from_ulongest (insn->pc).release ();
75c0bdf4
TW
236}
237
0ed5da75
TW
238/* Implementation of RecordInstruction.size [int] for btrace.
239 Returns the instruction size. */
75c0bdf4 240
0ed5da75
TW
241PyObject *
242recpy_bt_insn_size (PyObject *self, void *closure)
75c0bdf4 243{
0ed5da75 244 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 245
75c0bdf4 246 if (insn == NULL)
0ed5da75 247 return NULL;
75c0bdf4 248
47f0e2ff 249 return gdb_py_object_from_longest (insn->size).release ();
75c0bdf4
TW
250}
251
0ed5da75 252/* Implementation of RecordInstruction.is_speculative [bool] for btrace.
75c0bdf4
TW
253 Returns if this instruction was executed speculatively. */
254
0ed5da75
TW
255PyObject *
256recpy_bt_insn_is_speculative (PyObject *self, void *closure)
75c0bdf4 257{
0ed5da75 258 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 259
75c0bdf4 260 if (insn == NULL)
0ed5da75 261 return NULL;
75c0bdf4
TW
262
263 if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
264 Py_RETURN_TRUE;
265 else
266 Py_RETURN_FALSE;
267}
268
0ed5da75 269/* Implementation of RecordInstruction.data [buffer] for btrace.
75c0bdf4
TW
270 Returns raw instruction data. */
271
0ed5da75
TW
272PyObject *
273recpy_bt_insn_data (PyObject *self, void *closure)
75c0bdf4 274{
0ed5da75 275 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
075c55e0 276 gdb::byte_vector buffer;
75c0bdf4
TW
277 PyObject *object;
278
75c0bdf4 279 if (insn == NULL)
0ed5da75 280 return NULL;
75c0bdf4 281
a70b8144 282 try
75c0bdf4 283 {
075c55e0
TT
284 buffer.resize (insn->size);
285 read_memory (insn->pc, buffer.data (), insn->size);
75c0bdf4 286 }
230d2906 287 catch (const gdb_exception &except)
75c0bdf4 288 {
75c0bdf4
TW
289 GDB_PY_HANDLE_EXCEPTION (except);
290 }
75c0bdf4 291
075c55e0
TT
292 object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
293 insn->size);
75c0bdf4
TW
294
295 if (object == NULL)
296 return NULL;
297
cee59b3f 298#ifdef IS_PY3K
75c0bdf4 299 return PyMemoryView_FromObject (object);
cee59b3f
TW
300#else
301 return PyBuffer_FromObject (object, 0, Py_END_OF_BUFFER);
302#endif
303
75c0bdf4
TW
304}
305
0ed5da75
TW
306/* Implementation of RecordInstruction.decoded [str] for btrace.
307 Returns the instruction as human readable string. */
75c0bdf4 308
0ed5da75
TW
309PyObject *
310recpy_bt_insn_decoded (PyObject *self, void *closure)
75c0bdf4 311{
0ed5da75 312 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
313 string_file strfile;
314
75c0bdf4 315 if (insn == NULL)
0ed5da75 316 return NULL;
75c0bdf4 317
a70b8144 318 try
75c0bdf4
TW
319 {
320 gdb_print_insn (target_gdbarch (), insn->pc, &strfile, NULL);
321 }
230d2906 322 catch (const gdb_exception &except)
75c0bdf4
TW
323 {
324 gdbpy_convert_exception (except);
325 return NULL;
326 }
75c0bdf4
TW
327
328
329 return PyBytes_FromString (strfile.string ().c_str ());
330}
331
14f819c8
TW
332/* Implementation of RecordFunctionSegment.level [int] for btrace.
333 Returns the call level. */
75c0bdf4 334
14f819c8
TW
335PyObject *
336recpy_bt_func_level (PyObject *self, void *closure)
75c0bdf4 337{
14f819c8
TW
338 const btrace_function * const func = btrace_func_from_recpy_func (self);
339 thread_info *tinfo;
75c0bdf4 340
75c0bdf4 341 if (func == NULL)
14f819c8 342 return NULL;
75c0bdf4 343
00431a78 344 tinfo = ((recpy_element_object *) self)->thread;
47f0e2ff
TT
345 return gdb_py_object_from_longest (tinfo->btrace.level
346 + func->level).release ();
75c0bdf4
TW
347}
348
14f819c8
TW
349/* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
350 Returns the symbol associated with this function call. */
75c0bdf4 351
14f819c8
TW
352PyObject *
353recpy_bt_func_symbol (PyObject *self, void *closure)
75c0bdf4 354{
14f819c8 355 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 356
75c0bdf4 357 if (func == NULL)
14f819c8 358 return NULL;
75c0bdf4
TW
359
360 if (func->sym == NULL)
361 Py_RETURN_NONE;
362
363 return symbol_to_symbol_object (func->sym);
364}
365
14f819c8
TW
366/* Implementation of RecordFunctionSegment.instructions [list] for btrace.
367 Returns the list of instructions that belong to this function call. */
75c0bdf4 368
14f819c8
TW
369PyObject *
370recpy_bt_func_instructions (PyObject *self, void *closure)
75c0bdf4 371{
14f819c8 372 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4
TW
373 unsigned int len;
374
75c0bdf4 375 if (func == NULL)
14f819c8 376 return NULL;
75c0bdf4 377
0860c437 378 len = func->insn.size ();
75c0bdf4
TW
379
380 /* Gaps count as one instruction. */
381 if (len == 0)
382 len = 1;
383
00431a78 384 return btpy_list_new (((recpy_element_object *) self)->thread,
14f819c8
TW
385 func->insn_offset, func->insn_offset + len, 1,
386 &recpy_insn_type);
75c0bdf4
TW
387}
388
14f819c8
TW
389/* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
390 btrace. Returns the caller / returnee of this function. */
75c0bdf4 391
14f819c8
TW
392PyObject *
393recpy_bt_func_up (PyObject *self, void *closure)
75c0bdf4 394{
14f819c8 395 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 396
75c0bdf4 397 if (func == NULL)
14f819c8 398 return NULL;
75c0bdf4 399
42bfe59e 400 if (func->up == 0)
75c0bdf4
TW
401 Py_RETURN_NONE;
402
00431a78 403 return recpy_func_new (((recpy_element_object *) self)->thread,
42bfe59e 404 RECORD_METHOD_BTRACE, func->up);
75c0bdf4
TW
405}
406
14f819c8
TW
407/* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
408 btrace. Returns a previous segment of this function. */
75c0bdf4 409
14f819c8
TW
410PyObject *
411recpy_bt_func_prev (PyObject *self, void *closure)
75c0bdf4 412{
14f819c8 413 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 414
75c0bdf4 415 if (func == NULL)
14f819c8 416 return NULL;
75c0bdf4 417
4aeb0dfc 418 if (func->prev == 0)
75c0bdf4
TW
419 Py_RETURN_NONE;
420
00431a78 421 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 422 RECORD_METHOD_BTRACE, func->prev);
75c0bdf4
TW
423}
424
14f819c8
TW
425/* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
426 btrace. Returns a following segment of this function. */
75c0bdf4 427
14f819c8
TW
428PyObject *
429recpy_bt_func_next (PyObject *self, void *closure)
75c0bdf4 430{
14f819c8 431 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 432
75c0bdf4 433 if (func == NULL)
14f819c8 434 return NULL;
75c0bdf4 435
4aeb0dfc 436 if (func->next == 0)
75c0bdf4
TW
437 Py_RETURN_NONE;
438
00431a78 439 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 440 RECORD_METHOD_BTRACE, func->next);
75c0bdf4
TW
441}
442
443/* Implementation of BtraceList.__len__ (self) -> int. */
444
445static Py_ssize_t
446btpy_list_length (PyObject *self)
447{
448 const btpy_list_object * const obj = (btpy_list_object *) self;
449 const Py_ssize_t distance = obj->last - obj->first;
450 const Py_ssize_t result = distance / obj->step;
451
452 if ((distance % obj->step) == 0)
453 return result;
454
455 return result + 1;
456}
457
458/* Implementation of
459 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
460 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
461
462static PyObject *
463btpy_list_item (PyObject *self, Py_ssize_t index)
464{
465 const btpy_list_object * const obj = (btpy_list_object *) self;
0ed5da75 466 Py_ssize_t number;
75c0bdf4
TW
467
468 if (index < 0 || index >= btpy_list_length (self))
469 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
470 index);
471
0ed5da75
TW
472 number = obj->first + (obj->step * index);
473
474 if (obj->element_type == &recpy_insn_type)
00431a78 475 return recpy_insn_new (obj->thread, RECORD_METHOD_BTRACE, number);
0ed5da75 476 else
00431a78 477 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
75c0bdf4
TW
478}
479
480/* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
481
482static PyObject *
483btpy_list_slice (PyObject *self, PyObject *value)
484{
485 const btpy_list_object * const obj = (btpy_list_object *) self;
486 const Py_ssize_t length = btpy_list_length (self);
487 Py_ssize_t start, stop, step, slicelength;
488
489 if (PyInt_Check (value))
490 {
491 Py_ssize_t index = PyInt_AsSsize_t (value);
492
493 /* Emulate Python behavior for negative indices. */
494 if (index < 0)
495 index += length;
496
497 return btpy_list_item (self, index);
498 }
499
500 if (!PySlice_Check (value))
501 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
502
503 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start, &stop,
504 &step, &slicelength))
505 return NULL;
506
00431a78 507 return btpy_list_new (obj->thread, obj->first + obj->step * start,
75c0bdf4
TW
508 obj->first + obj->step * stop, obj->step * step,
509 obj->element_type);
510}
511
512/* Helper function that returns the position of an element in a BtraceList
513 or -1 if the element is not in the list. */
514
515static LONGEST
516btpy_list_position (PyObject *self, PyObject *value)
517{
518 const btpy_list_object * const list_obj = (btpy_list_object *) self;
0ed5da75 519 const recpy_element_object * const obj = (const recpy_element_object *) value;
75c0bdf4
TW
520 Py_ssize_t index = obj->number;
521
522 if (list_obj->element_type != Py_TYPE (value))
523 return -1;
524
00431a78 525 if (list_obj->thread != obj->thread)
75c0bdf4
TW
526 return -1;
527
528 if (index < list_obj->first || index > list_obj->last)
529 return -1;
530
531 index -= list_obj->first;
532
533 if (index % list_obj->step != 0)
534 return -1;
535
536 return index / list_obj->step;
537}
538
539/* Implementation of "in" operator for BtraceLists. */
540
541static int
542btpy_list_contains (PyObject *self, PyObject *value)
543{
544 if (btpy_list_position (self, value) < 0)
545 return 0;
546
547 return 1;
548}
549
550/* Implementation of BtraceLists.index (self, value) -> int. */
551
552static PyObject *
553btpy_list_index (PyObject *self, PyObject *value)
554{
555 const LONGEST index = btpy_list_position (self, value);
556
557 if (index < 0)
558 return PyErr_Format (PyExc_ValueError, _("Not in list."));
559
4bde49dc 560 return gdb_py_object_from_longest (index).release ();
75c0bdf4
TW
561}
562
563/* Implementation of BtraceList.count (self, value) -> int. */
564
565static PyObject *
566btpy_list_count (PyObject *self, PyObject *value)
567{
568 /* We know that if an element is in the list, it is so exactly one time,
569 enabling us to reuse the "is element of" check. */
47f0e2ff
TT
570 return gdb_py_object_from_longest (btpy_list_contains (self,
571 value)).release ();
75c0bdf4
TW
572}
573
574/* Python rich compare function to allow for equality and inequality checks
575 in Python. */
576
577static PyObject *
578btpy_list_richcompare (PyObject *self, PyObject *other, int op)
579{
580 const btpy_list_object * const obj1 = (btpy_list_object *) self;
581 const btpy_list_object * const obj2 = (btpy_list_object *) other;
582
583 if (Py_TYPE (self) != Py_TYPE (other))
584 {
585 Py_INCREF (Py_NotImplemented);
586 return Py_NotImplemented;
587 }
588
589 switch (op)
590 {
591 case Py_EQ:
00431a78 592 if (obj1->thread == obj2->thread
75c0bdf4
TW
593 && obj1->element_type == obj2->element_type
594 && obj1->first == obj2->first
595 && obj1->last == obj2->last
596 && obj1->step == obj2->step)
597 Py_RETURN_TRUE;
598 else
599 Py_RETURN_FALSE;
600
601 case Py_NE:
00431a78 602 if (obj1->thread != obj2->thread
75c0bdf4
TW
603 || obj1->element_type != obj2->element_type
604 || obj1->first != obj2->first
605 || obj1->last != obj2->last
606 || obj1->step != obj2->step)
607 Py_RETURN_TRUE;
608 else
609 Py_RETURN_FALSE;
610
611 default:
612 break;
613 }
614
615 Py_INCREF (Py_NotImplemented);
616 return Py_NotImplemented;
617}
618
619/* Implementation of
620 BtraceRecord.method [str]. */
621
622PyObject *
623recpy_bt_method (PyObject *self, void *closure)
624{
625 return PyString_FromString ("btrace");
626}
627
628/* Implementation of
629 BtraceRecord.format [str]. */
630
631PyObject *
632recpy_bt_format (PyObject *self, void *closure)
633{
ae20e79a 634 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 635 const struct thread_info * const tinfo = record->thread;
75c0bdf4
TW
636 const struct btrace_config * config;
637
638 if (tinfo == NULL)
639 Py_RETURN_NONE;
640
641 config = btrace_conf (&tinfo->btrace);
642
643 if (config == NULL)
644 Py_RETURN_NONE;
645
646 return PyString_FromString (btrace_format_short_string (config->format));
647}
648
649/* Implementation of
650 BtraceRecord.replay_position [BtraceInstruction]. */
651
652PyObject *
653recpy_bt_replay_position (PyObject *self, void *closure)
654{
ae20e79a 655 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 656 thread_info * tinfo = record->thread;
75c0bdf4
TW
657
658 if (tinfo == NULL)
659 Py_RETURN_NONE;
660
661 if (tinfo->btrace.replay == NULL)
662 Py_RETURN_NONE;
663
913aeadd
TW
664 return btpy_insn_or_gap_new (tinfo,
665 btrace_insn_number (tinfo->btrace.replay));
75c0bdf4
TW
666}
667
668/* Implementation of
669 BtraceRecord.begin [BtraceInstruction]. */
670
671PyObject *
672recpy_bt_begin (PyObject *self, void *closure)
673{
ae20e79a 674 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 675 thread_info *const tinfo = record->thread;
75c0bdf4
TW
676 struct btrace_insn_iterator iterator;
677
678 if (tinfo == NULL)
679 Py_RETURN_NONE;
680
4a4495d6 681 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
682
683 if (btrace_is_empty (tinfo))
684 Py_RETURN_NONE;
685
686 btrace_insn_begin (&iterator, &tinfo->btrace);
913aeadd 687 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
75c0bdf4
TW
688}
689
690/* Implementation of
691 BtraceRecord.end [BtraceInstruction]. */
692
693PyObject *
694recpy_bt_end (PyObject *self, void *closure)
695{
ae20e79a 696 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 697 thread_info *const tinfo = record->thread;
75c0bdf4
TW
698 struct btrace_insn_iterator iterator;
699
700 if (tinfo == NULL)
701 Py_RETURN_NONE;
702
4a4495d6 703 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
704
705 if (btrace_is_empty (tinfo))
706 Py_RETURN_NONE;
707
708 btrace_insn_end (&iterator, &tinfo->btrace);
913aeadd 709 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
75c0bdf4
TW
710}
711
712/* Implementation of
713 BtraceRecord.instruction_history [list]. */
714
715PyObject *
716recpy_bt_instruction_history (PyObject *self, void *closure)
717{
ae20e79a 718 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 719 thread_info *const tinfo = record->thread;
75c0bdf4
TW
720 struct btrace_insn_iterator iterator;
721 unsigned long first = 0;
722 unsigned long last = 0;
723
724 if (tinfo == NULL)
725 Py_RETURN_NONE;
726
4a4495d6 727 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
728
729 if (btrace_is_empty (tinfo))
730 Py_RETURN_NONE;
731
732 btrace_insn_begin (&iterator, &tinfo->btrace);
733 first = btrace_insn_number (&iterator);
734
735 btrace_insn_end (&iterator, &tinfo->btrace);
736 last = btrace_insn_number (&iterator);
737
00431a78 738 return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type);
75c0bdf4
TW
739}
740
741/* Implementation of
742 BtraceRecord.function_call_history [list]. */
743
744PyObject *
745recpy_bt_function_call_history (PyObject *self, void *closure)
746{
ae20e79a 747 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 748 thread_info *const tinfo = record->thread;
8d0050ea
TW
749 struct btrace_call_iterator iterator;
750 unsigned long first = 0;
751 unsigned long last = 0;
75c0bdf4 752
8d0050ea
TW
753 if (tinfo == NULL)
754 Py_RETURN_NONE;
75c0bdf4 755
4a4495d6 756 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4 757
8d0050ea
TW
758 if (btrace_is_empty (tinfo))
759 Py_RETURN_NONE;
75c0bdf4 760
8d0050ea
TW
761 btrace_call_begin (&iterator, &tinfo->btrace);
762 first = btrace_call_number (&iterator);
75c0bdf4 763
8d0050ea
TW
764 btrace_call_end (&iterator, &tinfo->btrace);
765 last = btrace_call_number (&iterator);
75c0bdf4 766
00431a78 767 return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
75c0bdf4
TW
768}
769
770/* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
771
772PyObject *
773recpy_bt_goto (PyObject *self, PyObject *args)
774{
ae20e79a 775 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 776 thread_info *const tinfo = record->thread;
0ed5da75 777 const recpy_element_object *obj;
96b1ad86 778 PyObject *parse_obj;
75c0bdf4
TW
779
780 if (tinfo == NULL || btrace_is_empty (tinfo))
781 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
782
96b1ad86 783 if (!PyArg_ParseTuple (args, "O", &parse_obj))
75c0bdf4
TW
784 return NULL;
785
96b1ad86 786 if (Py_TYPE (parse_obj) != &recpy_insn_type)
75c0bdf4 787 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
96b1ad86 788 obj = (const recpy_element_object *) parse_obj;
75c0bdf4 789
a70b8144 790 try
75c0bdf4
TW
791 {
792 struct btrace_insn_iterator iter;
793
794 btrace_insn_end (&iter, &tinfo->btrace);
795
796 if (btrace_insn_number (&iter) == obj->number)
797 target_goto_record_end ();
798 else
799 target_goto_record (obj->number);
800 }
230d2906 801 catch (const gdb_exception &except)
75c0bdf4
TW
802 {
803 GDB_PY_HANDLE_EXCEPTION (except);
804 }
75c0bdf4
TW
805
806 Py_RETURN_NONE;
807}
808
75c0bdf4
TW
809/* BtraceList methods. */
810
6bd434d6 811static PyMethodDef btpy_list_methods[] =
75c0bdf4 812{
40c94099 813 { "count", btpy_list_count, METH_O, "count number of occurrences"},
75c0bdf4
TW
814 { "index", btpy_list_index, METH_O, "index of entry"},
815 {NULL}
816};
817
818/* BtraceList sequence methods. */
819
820static PySequenceMethods btpy_list_sequence_methods =
821{
822 NULL
823};
824
825/* BtraceList mapping methods. Necessary for slicing. */
826
827static PyMappingMethods btpy_list_mapping_methods =
828{
829 NULL
830};
831
832/* Sets up the btrace record API. */
833
834int
835gdbpy_initialize_btrace (void)
836{
75c0bdf4
TW
837 btpy_list_type.tp_new = PyType_GenericNew;
838 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
839 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
840 btpy_list_type.tp_name = "gdb.BtraceObjectList";
841 btpy_list_type.tp_doc = "GDB btrace list object";
842 btpy_list_type.tp_methods = btpy_list_methods;
843 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
844 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
845 btpy_list_type.tp_richcompare = btpy_list_richcompare;
846
847 btpy_list_sequence_methods.sq_item = btpy_list_item;
848 btpy_list_sequence_methods.sq_length = btpy_list_length;
849 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
850
851 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
852
14f819c8 853 return PyType_Ready (&btpy_list_type);
75c0bdf4 854}