Document fact that mininum Python version is now 2.6
[deliverable/binutils-gdb.git] / gdb / python / py-record-btrace.c
CommitLineData
75c0bdf4
TW
1/* Python interface to btrace instruction history.
2
42a4f53d 3 Copyright 2016-2019 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
TW
28#include "disasm.h"
29
30#if defined (IS_PY3K)
31
32#define BTPY_PYSLICE(x) (x)
33
34#else
35
36#define BTPY_PYSLICE(x) ((PySliceObject *) x)
37
38#endif
39
75c0bdf4
TW
40/* Python object for btrace record lists. */
41
42typedef struct {
43 PyObject_HEAD
44
45 /* The thread this list belongs to. */
00431a78 46 thread_info *thread;
75c0bdf4
TW
47
48 /* The first index being part of this list. */
49 Py_ssize_t first;
50
51 /* The last index begin part of this list. */
52 Py_ssize_t last;
53
54 /* Stride size. */
55 Py_ssize_t step;
56
0ed5da75 57 /* Either &BTPY_CALL_TYPE or &RECPY_INSN_TYPE. */
75c0bdf4
TW
58 PyTypeObject* element_type;
59} btpy_list_object;
60
75c0bdf4
TW
61/* Python type for btrace lists. */
62
63static PyTypeObject btpy_list_type = {
64 PyVarObject_HEAD_INIT (NULL, 0)
65};
66
0ed5da75
TW
67/* Returns either a btrace_insn for the given Python gdb.RecordInstruction
68 object or sets an appropriate Python exception and returns NULL. */
75c0bdf4 69
0ed5da75
TW
70static const btrace_insn *
71btrace_insn_from_recpy_insn (const PyObject * const pyobject)
75c0bdf4 72{
0ed5da75
TW
73 const btrace_insn *insn;
74 const recpy_element_object *obj;
75 thread_info *tinfo;
76 btrace_insn_iterator iter;
75c0bdf4 77
0ed5da75
TW
78 if (Py_TYPE (pyobject) != &recpy_insn_type)
79 {
80 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordInstruction"));
81 return NULL;
82 }
75c0bdf4 83
0ed5da75 84 obj = (const recpy_element_object *) pyobject;
00431a78 85 tinfo = obj->thread;
75c0bdf4 86
0ed5da75
TW
87 if (tinfo == NULL || btrace_is_empty (tinfo))
88 {
89 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
90 return NULL;
91 }
92
93 if (btrace_find_insn_by_number (&iter, &tinfo->btrace, obj->number) == 0)
94 {
95 PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
96 return NULL;
97 }
98
99 insn = btrace_insn_get (&iter);
100 if (insn == NULL)
101 {
102 PyErr_Format (gdbpy_gdb_error, _("Not a valid instruction."));
103 return NULL;
104 }
105
106 return insn;
75c0bdf4
TW
107}
108
14f819c8
TW
109/* Returns either a btrace_function for the given Python
110 gdb.RecordFunctionSegment object or sets an appropriate Python exception and
111 returns NULL. */
112
113static const btrace_function *
114btrace_func_from_recpy_func (const PyObject * const pyobject)
115{
116 const btrace_function *func;
117 const recpy_element_object *obj;
118 thread_info *tinfo;
119 btrace_call_iterator iter;
120
121 if (Py_TYPE (pyobject) != &recpy_func_type)
122 {
123 PyErr_Format (gdbpy_gdb_error, _("Must be gdb.RecordFunctionSegment"));
124 return NULL;
125 }
126
127 obj = (const recpy_element_object *) pyobject;
00431a78 128 tinfo = obj->thread;
14f819c8
TW
129
130 if (tinfo == NULL || btrace_is_empty (tinfo))
131 {
132 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
133 return NULL;
134 }
135
136 if (btrace_find_call_by_number (&iter, &tinfo->btrace, obj->number) == 0)
137 {
138 PyErr_Format (gdbpy_gdb_error, _("No such function segment."));
139 return NULL;
140 }
141
142 func = btrace_call_get (&iter);
143 if (func == NULL)
144 {
145 PyErr_Format (gdbpy_gdb_error, _("Not a valid function segment."));
146 return NULL;
147 }
148
149 return func;
150}
151
913aeadd 152/* Looks at the recorded item with the number NUMBER and create a
0ed5da75 153 gdb.RecordInstruction or gdb.RecordGap object for it accordingly. */
75c0bdf4
TW
154
155static PyObject *
00431a78 156btpy_insn_or_gap_new (thread_info *tinfo, Py_ssize_t number)
75c0bdf4 157{
913aeadd
TW
158 btrace_insn_iterator iter;
159 int err_code;
160
161 btrace_find_insn_by_number (&iter, &tinfo->btrace, number);
162 err_code = btrace_insn_get_error (&iter);
163
164 if (err_code != 0)
165 {
166 const btrace_config *config;
167 const char *err_string;
168
169 config = btrace_conf (&tinfo->btrace);
170 err_string = btrace_decode_error (config->format, err_code);
171
172 return recpy_gap_new (err_code, err_string, number);
173 }
174
00431a78 175 return recpy_insn_new (tinfo, RECORD_METHOD_BTRACE, number);
75c0bdf4
TW
176}
177
75c0bdf4
TW
178/* Create a new gdb.BtraceList object. */
179
180static PyObject *
00431a78 181btpy_list_new (thread_info *thread, Py_ssize_t first, Py_ssize_t last, Py_ssize_t step,
75c0bdf4
TW
182 PyTypeObject *element_type)
183{
184 btpy_list_object * const obj = PyObject_New (btpy_list_object,
185 &btpy_list_type);
186
187 if (obj == NULL)
188 return NULL;
189
00431a78 190 obj->thread = thread;
75c0bdf4
TW
191 obj->first = first;
192 obj->last = last;
193 obj->step = step;
194 obj->element_type = element_type;
195
196 return (PyObject *) obj;
197}
198
0ed5da75
TW
199/* Implementation of RecordInstruction.sal [gdb.Symtab_and_line] for btrace.
200 Returns the SAL associated with this instruction. */
75c0bdf4 201
0ed5da75
TW
202PyObject *
203recpy_bt_insn_sal (PyObject *self, void *closure)
75c0bdf4 204{
0ed5da75 205 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
206 PyObject *result = NULL;
207
75c0bdf4 208 if (insn == NULL)
0ed5da75 209 return NULL;
75c0bdf4
TW
210
211 TRY
212 {
213 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
214 }
215 CATCH (except, RETURN_MASK_ALL)
216 {
217 GDB_PY_HANDLE_EXCEPTION (except);
218 }
219 END_CATCH
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
TW
234
235 return gdb_py_long_from_ulongest (insn->pc);
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
TW
248
249 return PyInt_FromLong (insn->size);
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
TW
281
282 TRY
283 {
075c55e0
TT
284 buffer.resize (insn->size);
285 read_memory (insn->pc, buffer.data (), insn->size);
75c0bdf4
TW
286 }
287 CATCH (except, RETURN_MASK_ALL)
288 {
75c0bdf4
TW
289 GDB_PY_HANDLE_EXCEPTION (except);
290 }
291 END_CATCH
292
075c55e0
TT
293 object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
294 insn->size);
75c0bdf4
TW
295
296 if (object == NULL)
297 return NULL;
298
cee59b3f 299#ifdef IS_PY3K
75c0bdf4 300 return PyMemoryView_FromObject (object);
cee59b3f
TW
301#else
302 return PyBuffer_FromObject (object, 0, Py_END_OF_BUFFER);
303#endif
304
75c0bdf4
TW
305}
306
0ed5da75
TW
307/* Implementation of RecordInstruction.decoded [str] for btrace.
308 Returns the instruction as human readable string. */
75c0bdf4 309
0ed5da75
TW
310PyObject *
311recpy_bt_insn_decoded (PyObject *self, void *closure)
75c0bdf4 312{
0ed5da75 313 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
314 string_file strfile;
315
75c0bdf4 316 if (insn == NULL)
0ed5da75 317 return NULL;
75c0bdf4
TW
318
319 TRY
320 {
321 gdb_print_insn (target_gdbarch (), insn->pc, &strfile, NULL);
322 }
323 CATCH (except, RETURN_MASK_ALL)
324 {
325 gdbpy_convert_exception (except);
326 return NULL;
327 }
328 END_CATCH
329
330
331 return PyBytes_FromString (strfile.string ().c_str ());
332}
333
14f819c8
TW
334/* Implementation of RecordFunctionSegment.level [int] for btrace.
335 Returns the call level. */
75c0bdf4 336
14f819c8
TW
337PyObject *
338recpy_bt_func_level (PyObject *self, void *closure)
75c0bdf4 339{
14f819c8
TW
340 const btrace_function * const func = btrace_func_from_recpy_func (self);
341 thread_info *tinfo;
75c0bdf4 342
75c0bdf4 343 if (func == NULL)
14f819c8 344 return NULL;
75c0bdf4 345
00431a78 346 tinfo = ((recpy_element_object *) self)->thread;
14f819c8 347 return PyInt_FromLong (tinfo->btrace.level + func->level);
75c0bdf4
TW
348}
349
14f819c8
TW
350/* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
351 Returns the symbol associated with this function call. */
75c0bdf4 352
14f819c8
TW
353PyObject *
354recpy_bt_func_symbol (PyObject *self, void *closure)
75c0bdf4 355{
14f819c8 356 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 357
75c0bdf4 358 if (func == NULL)
14f819c8 359 return NULL;
75c0bdf4
TW
360
361 if (func->sym == NULL)
362 Py_RETURN_NONE;
363
364 return symbol_to_symbol_object (func->sym);
365}
366
14f819c8
TW
367/* Implementation of RecordFunctionSegment.instructions [list] for btrace.
368 Returns the list of instructions that belong to this function call. */
75c0bdf4 369
14f819c8
TW
370PyObject *
371recpy_bt_func_instructions (PyObject *self, void *closure)
75c0bdf4 372{
14f819c8 373 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4
TW
374 unsigned int len;
375
75c0bdf4 376 if (func == NULL)
14f819c8 377 return NULL;
75c0bdf4 378
0860c437 379 len = func->insn.size ();
75c0bdf4
TW
380
381 /* Gaps count as one instruction. */
382 if (len == 0)
383 len = 1;
384
00431a78 385 return btpy_list_new (((recpy_element_object *) self)->thread,
14f819c8
TW
386 func->insn_offset, func->insn_offset + len, 1,
387 &recpy_insn_type);
75c0bdf4
TW
388}
389
14f819c8
TW
390/* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
391 btrace. Returns the caller / returnee of this function. */
75c0bdf4 392
14f819c8
TW
393PyObject *
394recpy_bt_func_up (PyObject *self, void *closure)
75c0bdf4 395{
14f819c8 396 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 397
75c0bdf4 398 if (func == NULL)
14f819c8 399 return NULL;
75c0bdf4 400
42bfe59e 401 if (func->up == 0)
75c0bdf4
TW
402 Py_RETURN_NONE;
403
00431a78 404 return recpy_func_new (((recpy_element_object *) self)->thread,
42bfe59e 405 RECORD_METHOD_BTRACE, func->up);
75c0bdf4
TW
406}
407
14f819c8
TW
408/* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
409 btrace. Returns a previous segment of this function. */
75c0bdf4 410
14f819c8
TW
411PyObject *
412recpy_bt_func_prev (PyObject *self, void *closure)
75c0bdf4 413{
14f819c8 414 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 415
75c0bdf4 416 if (func == NULL)
14f819c8 417 return NULL;
75c0bdf4 418
4aeb0dfc 419 if (func->prev == 0)
75c0bdf4
TW
420 Py_RETURN_NONE;
421
00431a78 422 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 423 RECORD_METHOD_BTRACE, func->prev);
75c0bdf4
TW
424}
425
14f819c8
TW
426/* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
427 btrace. Returns a following segment of this function. */
75c0bdf4 428
14f819c8
TW
429PyObject *
430recpy_bt_func_next (PyObject *self, void *closure)
75c0bdf4 431{
14f819c8 432 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 433
75c0bdf4 434 if (func == NULL)
14f819c8 435 return NULL;
75c0bdf4 436
4aeb0dfc 437 if (func->next == 0)
75c0bdf4
TW
438 Py_RETURN_NONE;
439
00431a78 440 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 441 RECORD_METHOD_BTRACE, func->next);
75c0bdf4
TW
442}
443
444/* Implementation of BtraceList.__len__ (self) -> int. */
445
446static Py_ssize_t
447btpy_list_length (PyObject *self)
448{
449 const btpy_list_object * const obj = (btpy_list_object *) self;
450 const Py_ssize_t distance = obj->last - obj->first;
451 const Py_ssize_t result = distance / obj->step;
452
453 if ((distance % obj->step) == 0)
454 return result;
455
456 return result + 1;
457}
458
459/* Implementation of
460 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
461 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
462
463static PyObject *
464btpy_list_item (PyObject *self, Py_ssize_t index)
465{
466 const btpy_list_object * const obj = (btpy_list_object *) self;
0ed5da75 467 Py_ssize_t number;
75c0bdf4
TW
468
469 if (index < 0 || index >= btpy_list_length (self))
470 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
471 index);
472
0ed5da75
TW
473 number = obj->first + (obj->step * index);
474
475 if (obj->element_type == &recpy_insn_type)
00431a78 476 return recpy_insn_new (obj->thread, RECORD_METHOD_BTRACE, number);
0ed5da75 477 else
00431a78 478 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
75c0bdf4
TW
479}
480
481/* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
482
483static PyObject *
484btpy_list_slice (PyObject *self, PyObject *value)
485{
486 const btpy_list_object * const obj = (btpy_list_object *) self;
487 const Py_ssize_t length = btpy_list_length (self);
488 Py_ssize_t start, stop, step, slicelength;
489
490 if (PyInt_Check (value))
491 {
492 Py_ssize_t index = PyInt_AsSsize_t (value);
493
494 /* Emulate Python behavior for negative indices. */
495 if (index < 0)
496 index += length;
497
498 return btpy_list_item (self, index);
499 }
500
501 if (!PySlice_Check (value))
502 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
503
504 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start, &stop,
505 &step, &slicelength))
506 return NULL;
507
00431a78 508 return btpy_list_new (obj->thread, obj->first + obj->step * start,
75c0bdf4
TW
509 obj->first + obj->step * stop, obj->step * step,
510 obj->element_type);
511}
512
513/* Helper function that returns the position of an element in a BtraceList
514 or -1 if the element is not in the list. */
515
516static LONGEST
517btpy_list_position (PyObject *self, PyObject *value)
518{
519 const btpy_list_object * const list_obj = (btpy_list_object *) self;
0ed5da75 520 const recpy_element_object * const obj = (const recpy_element_object *) value;
75c0bdf4
TW
521 Py_ssize_t index = obj->number;
522
523 if (list_obj->element_type != Py_TYPE (value))
524 return -1;
525
00431a78 526 if (list_obj->thread != obj->thread)
75c0bdf4
TW
527 return -1;
528
529 if (index < list_obj->first || index > list_obj->last)
530 return -1;
531
532 index -= list_obj->first;
533
534 if (index % list_obj->step != 0)
535 return -1;
536
537 return index / list_obj->step;
538}
539
540/* Implementation of "in" operator for BtraceLists. */
541
542static int
543btpy_list_contains (PyObject *self, PyObject *value)
544{
545 if (btpy_list_position (self, value) < 0)
546 return 0;
547
548 return 1;
549}
550
551/* Implementation of BtraceLists.index (self, value) -> int. */
552
553static PyObject *
554btpy_list_index (PyObject *self, PyObject *value)
555{
556 const LONGEST index = btpy_list_position (self, value);
557
558 if (index < 0)
559 return PyErr_Format (PyExc_ValueError, _("Not in list."));
560
561 return gdb_py_long_from_longest (index);
562}
563
564/* Implementation of BtraceList.count (self, value) -> int. */
565
566static PyObject *
567btpy_list_count (PyObject *self, PyObject *value)
568{
569 /* We know that if an element is in the list, it is so exactly one time,
570 enabling us to reuse the "is element of" check. */
571 return PyInt_FromLong (btpy_list_contains (self, value));
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
TW
789
790 TRY
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 }
801 CATCH (except, RETURN_MASK_ALL)
802 {
803 GDB_PY_HANDLE_EXCEPTION (except);
804 }
805 END_CATCH
806
807 Py_RETURN_NONE;
808}
809
75c0bdf4
TW
810/* BtraceList methods. */
811
812struct PyMethodDef btpy_list_methods[] =
813{
814 { "count", btpy_list_count, METH_O, "count number of occurences"},
815 { "index", btpy_list_index, METH_O, "index of entry"},
816 {NULL}
817};
818
819/* BtraceList sequence methods. */
820
821static PySequenceMethods btpy_list_sequence_methods =
822{
823 NULL
824};
825
826/* BtraceList mapping methods. Necessary for slicing. */
827
828static PyMappingMethods btpy_list_mapping_methods =
829{
830 NULL
831};
832
833/* Sets up the btrace record API. */
834
835int
836gdbpy_initialize_btrace (void)
837{
75c0bdf4
TW
838 btpy_list_type.tp_new = PyType_GenericNew;
839 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
840 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
841 btpy_list_type.tp_name = "gdb.BtraceObjectList";
842 btpy_list_type.tp_doc = "GDB btrace list object";
843 btpy_list_type.tp_methods = btpy_list_methods;
844 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
845 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
846 btpy_list_type.tp_richcompare = btpy_list_richcompare;
847
848 btpy_list_sequence_methods.sq_item = btpy_list_item;
849 btpy_list_sequence_methods.sq_length = btpy_list_length;
850 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
851
852 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
853
14f819c8 854 return PyType_Ready (&btpy_list_type);
75c0bdf4 855}
This page took 0.262491 seconds and 4 git commands to generate.