Rename gdb exception types
[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 210
a70b8144 211 try
75c0bdf4
TW
212 {
213 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
214 }
230d2906 215 catch (const gdb_exception &except)
75c0bdf4
TW
216 {
217 GDB_PY_HANDLE_EXCEPTION (except);
218 }
75c0bdf4
TW
219
220 return result;
221}
222
0ed5da75
TW
223/* Implementation of RecordInstruction.pc [int] for btrace.
224 Returns the instruction address. */
75c0bdf4 225
0ed5da75
TW
226PyObject *
227recpy_bt_insn_pc (PyObject *self, void *closure)
75c0bdf4 228{
0ed5da75 229 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 230
75c0bdf4 231 if (insn == NULL)
0ed5da75 232 return NULL;
75c0bdf4
TW
233
234 return gdb_py_long_from_ulongest (insn->pc);
235}
236
0ed5da75
TW
237/* Implementation of RecordInstruction.size [int] for btrace.
238 Returns the instruction size. */
75c0bdf4 239
0ed5da75
TW
240PyObject *
241recpy_bt_insn_size (PyObject *self, void *closure)
75c0bdf4 242{
0ed5da75 243 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 244
75c0bdf4 245 if (insn == NULL)
0ed5da75 246 return NULL;
75c0bdf4
TW
247
248 return PyInt_FromLong (insn->size);
249}
250
0ed5da75 251/* Implementation of RecordInstruction.is_speculative [bool] for btrace.
75c0bdf4
TW
252 Returns if this instruction was executed speculatively. */
253
0ed5da75
TW
254PyObject *
255recpy_bt_insn_is_speculative (PyObject *self, void *closure)
75c0bdf4 256{
0ed5da75 257 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4 258
75c0bdf4 259 if (insn == NULL)
0ed5da75 260 return NULL;
75c0bdf4
TW
261
262 if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
263 Py_RETURN_TRUE;
264 else
265 Py_RETURN_FALSE;
266}
267
0ed5da75 268/* Implementation of RecordInstruction.data [buffer] for btrace.
75c0bdf4
TW
269 Returns raw instruction data. */
270
0ed5da75
TW
271PyObject *
272recpy_bt_insn_data (PyObject *self, void *closure)
75c0bdf4 273{
0ed5da75 274 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
075c55e0 275 gdb::byte_vector buffer;
75c0bdf4
TW
276 PyObject *object;
277
75c0bdf4 278 if (insn == NULL)
0ed5da75 279 return NULL;
75c0bdf4 280
a70b8144 281 try
75c0bdf4 282 {
075c55e0
TT
283 buffer.resize (insn->size);
284 read_memory (insn->pc, buffer.data (), insn->size);
75c0bdf4 285 }
230d2906 286 catch (const gdb_exception &except)
75c0bdf4 287 {
75c0bdf4
TW
288 GDB_PY_HANDLE_EXCEPTION (except);
289 }
75c0bdf4 290
075c55e0
TT
291 object = PyBytes_FromStringAndSize ((const char *) buffer.data (),
292 insn->size);
75c0bdf4
TW
293
294 if (object == NULL)
295 return NULL;
296
cee59b3f 297#ifdef IS_PY3K
75c0bdf4 298 return PyMemoryView_FromObject (object);
cee59b3f
TW
299#else
300 return PyBuffer_FromObject (object, 0, Py_END_OF_BUFFER);
301#endif
302
75c0bdf4
TW
303}
304
0ed5da75
TW
305/* Implementation of RecordInstruction.decoded [str] for btrace.
306 Returns the instruction as human readable string. */
75c0bdf4 307
0ed5da75
TW
308PyObject *
309recpy_bt_insn_decoded (PyObject *self, void *closure)
75c0bdf4 310{
0ed5da75 311 const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
75c0bdf4
TW
312 string_file strfile;
313
75c0bdf4 314 if (insn == NULL)
0ed5da75 315 return NULL;
75c0bdf4 316
a70b8144 317 try
75c0bdf4
TW
318 {
319 gdb_print_insn (target_gdbarch (), insn->pc, &strfile, NULL);
320 }
230d2906 321 catch (const gdb_exception &except)
75c0bdf4
TW
322 {
323 gdbpy_convert_exception (except);
324 return NULL;
325 }
75c0bdf4
TW
326
327
328 return PyBytes_FromString (strfile.string ().c_str ());
329}
330
14f819c8
TW
331/* Implementation of RecordFunctionSegment.level [int] for btrace.
332 Returns the call level. */
75c0bdf4 333
14f819c8
TW
334PyObject *
335recpy_bt_func_level (PyObject *self, void *closure)
75c0bdf4 336{
14f819c8
TW
337 const btrace_function * const func = btrace_func_from_recpy_func (self);
338 thread_info *tinfo;
75c0bdf4 339
75c0bdf4 340 if (func == NULL)
14f819c8 341 return NULL;
75c0bdf4 342
00431a78 343 tinfo = ((recpy_element_object *) self)->thread;
14f819c8 344 return PyInt_FromLong (tinfo->btrace.level + func->level);
75c0bdf4
TW
345}
346
14f819c8
TW
347/* Implementation of RecordFunctionSegment.symbol [gdb.Symbol] for btrace.
348 Returns the symbol associated with this function call. */
75c0bdf4 349
14f819c8
TW
350PyObject *
351recpy_bt_func_symbol (PyObject *self, void *closure)
75c0bdf4 352{
14f819c8 353 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 354
75c0bdf4 355 if (func == NULL)
14f819c8 356 return NULL;
75c0bdf4
TW
357
358 if (func->sym == NULL)
359 Py_RETURN_NONE;
360
361 return symbol_to_symbol_object (func->sym);
362}
363
14f819c8
TW
364/* Implementation of RecordFunctionSegment.instructions [list] for btrace.
365 Returns the list of instructions that belong to this function call. */
75c0bdf4 366
14f819c8
TW
367PyObject *
368recpy_bt_func_instructions (PyObject *self, void *closure)
75c0bdf4 369{
14f819c8 370 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4
TW
371 unsigned int len;
372
75c0bdf4 373 if (func == NULL)
14f819c8 374 return NULL;
75c0bdf4 375
0860c437 376 len = func->insn.size ();
75c0bdf4
TW
377
378 /* Gaps count as one instruction. */
379 if (len == 0)
380 len = 1;
381
00431a78 382 return btpy_list_new (((recpy_element_object *) self)->thread,
14f819c8
TW
383 func->insn_offset, func->insn_offset + len, 1,
384 &recpy_insn_type);
75c0bdf4
TW
385}
386
14f819c8
TW
387/* Implementation of RecordFunctionSegment.up [RecordFunctionSegment] for
388 btrace. Returns the caller / returnee of this function. */
75c0bdf4 389
14f819c8
TW
390PyObject *
391recpy_bt_func_up (PyObject *self, void *closure)
75c0bdf4 392{
14f819c8 393 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 394
75c0bdf4 395 if (func == NULL)
14f819c8 396 return NULL;
75c0bdf4 397
42bfe59e 398 if (func->up == 0)
75c0bdf4
TW
399 Py_RETURN_NONE;
400
00431a78 401 return recpy_func_new (((recpy_element_object *) self)->thread,
42bfe59e 402 RECORD_METHOD_BTRACE, func->up);
75c0bdf4
TW
403}
404
14f819c8
TW
405/* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment] for
406 btrace. Returns a previous segment of this function. */
75c0bdf4 407
14f819c8
TW
408PyObject *
409recpy_bt_func_prev (PyObject *self, void *closure)
75c0bdf4 410{
14f819c8 411 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 412
75c0bdf4 413 if (func == NULL)
14f819c8 414 return NULL;
75c0bdf4 415
4aeb0dfc 416 if (func->prev == 0)
75c0bdf4
TW
417 Py_RETURN_NONE;
418
00431a78 419 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 420 RECORD_METHOD_BTRACE, func->prev);
75c0bdf4
TW
421}
422
14f819c8
TW
423/* Implementation of RecordFunctionSegment.next [RecordFunctionSegment] for
424 btrace. Returns a following segment of this function. */
75c0bdf4 425
14f819c8
TW
426PyObject *
427recpy_bt_func_next (PyObject *self, void *closure)
75c0bdf4 428{
14f819c8 429 const btrace_function * const func = btrace_func_from_recpy_func (self);
75c0bdf4 430
75c0bdf4 431 if (func == NULL)
14f819c8 432 return NULL;
75c0bdf4 433
4aeb0dfc 434 if (func->next == 0)
75c0bdf4
TW
435 Py_RETURN_NONE;
436
00431a78 437 return recpy_func_new (((recpy_element_object *) self)->thread,
4aeb0dfc 438 RECORD_METHOD_BTRACE, func->next);
75c0bdf4
TW
439}
440
441/* Implementation of BtraceList.__len__ (self) -> int. */
442
443static Py_ssize_t
444btpy_list_length (PyObject *self)
445{
446 const btpy_list_object * const obj = (btpy_list_object *) self;
447 const Py_ssize_t distance = obj->last - obj->first;
448 const Py_ssize_t result = distance / obj->step;
449
450 if ((distance % obj->step) == 0)
451 return result;
452
453 return result + 1;
454}
455
456/* Implementation of
457 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
458 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
459
460static PyObject *
461btpy_list_item (PyObject *self, Py_ssize_t index)
462{
463 const btpy_list_object * const obj = (btpy_list_object *) self;
0ed5da75 464 Py_ssize_t number;
75c0bdf4
TW
465
466 if (index < 0 || index >= btpy_list_length (self))
467 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
468 index);
469
0ed5da75
TW
470 number = obj->first + (obj->step * index);
471
472 if (obj->element_type == &recpy_insn_type)
00431a78 473 return recpy_insn_new (obj->thread, RECORD_METHOD_BTRACE, number);
0ed5da75 474 else
00431a78 475 return recpy_func_new (obj->thread, RECORD_METHOD_BTRACE, number);
75c0bdf4
TW
476}
477
478/* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
479
480static PyObject *
481btpy_list_slice (PyObject *self, PyObject *value)
482{
483 const btpy_list_object * const obj = (btpy_list_object *) self;
484 const Py_ssize_t length = btpy_list_length (self);
485 Py_ssize_t start, stop, step, slicelength;
486
487 if (PyInt_Check (value))
488 {
489 Py_ssize_t index = PyInt_AsSsize_t (value);
490
491 /* Emulate Python behavior for negative indices. */
492 if (index < 0)
493 index += length;
494
495 return btpy_list_item (self, index);
496 }
497
498 if (!PySlice_Check (value))
499 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
500
501 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start, &stop,
502 &step, &slicelength))
503 return NULL;
504
00431a78 505 return btpy_list_new (obj->thread, obj->first + obj->step * start,
75c0bdf4
TW
506 obj->first + obj->step * stop, obj->step * step,
507 obj->element_type);
508}
509
510/* Helper function that returns the position of an element in a BtraceList
511 or -1 if the element is not in the list. */
512
513static LONGEST
514btpy_list_position (PyObject *self, PyObject *value)
515{
516 const btpy_list_object * const list_obj = (btpy_list_object *) self;
0ed5da75 517 const recpy_element_object * const obj = (const recpy_element_object *) value;
75c0bdf4
TW
518 Py_ssize_t index = obj->number;
519
520 if (list_obj->element_type != Py_TYPE (value))
521 return -1;
522
00431a78 523 if (list_obj->thread != obj->thread)
75c0bdf4
TW
524 return -1;
525
526 if (index < list_obj->first || index > list_obj->last)
527 return -1;
528
529 index -= list_obj->first;
530
531 if (index % list_obj->step != 0)
532 return -1;
533
534 return index / list_obj->step;
535}
536
537/* Implementation of "in" operator for BtraceLists. */
538
539static int
540btpy_list_contains (PyObject *self, PyObject *value)
541{
542 if (btpy_list_position (self, value) < 0)
543 return 0;
544
545 return 1;
546}
547
548/* Implementation of BtraceLists.index (self, value) -> int. */
549
550static PyObject *
551btpy_list_index (PyObject *self, PyObject *value)
552{
553 const LONGEST index = btpy_list_position (self, value);
554
555 if (index < 0)
556 return PyErr_Format (PyExc_ValueError, _("Not in list."));
557
558 return gdb_py_long_from_longest (index);
559}
560
561/* Implementation of BtraceList.count (self, value) -> int. */
562
563static PyObject *
564btpy_list_count (PyObject *self, PyObject *value)
565{
566 /* We know that if an element is in the list, it is so exactly one time,
567 enabling us to reuse the "is element of" check. */
568 return PyInt_FromLong (btpy_list_contains (self, value));
569}
570
571/* Python rich compare function to allow for equality and inequality checks
572 in Python. */
573
574static PyObject *
575btpy_list_richcompare (PyObject *self, PyObject *other, int op)
576{
577 const btpy_list_object * const obj1 = (btpy_list_object *) self;
578 const btpy_list_object * const obj2 = (btpy_list_object *) other;
579
580 if (Py_TYPE (self) != Py_TYPE (other))
581 {
582 Py_INCREF (Py_NotImplemented);
583 return Py_NotImplemented;
584 }
585
586 switch (op)
587 {
588 case Py_EQ:
00431a78 589 if (obj1->thread == obj2->thread
75c0bdf4
TW
590 && obj1->element_type == obj2->element_type
591 && obj1->first == obj2->first
592 && obj1->last == obj2->last
593 && obj1->step == obj2->step)
594 Py_RETURN_TRUE;
595 else
596 Py_RETURN_FALSE;
597
598 case Py_NE:
00431a78 599 if (obj1->thread != obj2->thread
75c0bdf4
TW
600 || obj1->element_type != obj2->element_type
601 || obj1->first != obj2->first
602 || obj1->last != obj2->last
603 || obj1->step != obj2->step)
604 Py_RETURN_TRUE;
605 else
606 Py_RETURN_FALSE;
607
608 default:
609 break;
610 }
611
612 Py_INCREF (Py_NotImplemented);
613 return Py_NotImplemented;
614}
615
616/* Implementation of
617 BtraceRecord.method [str]. */
618
619PyObject *
620recpy_bt_method (PyObject *self, void *closure)
621{
622 return PyString_FromString ("btrace");
623}
624
625/* Implementation of
626 BtraceRecord.format [str]. */
627
628PyObject *
629recpy_bt_format (PyObject *self, void *closure)
630{
ae20e79a 631 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 632 const struct thread_info * const tinfo = record->thread;
75c0bdf4
TW
633 const struct btrace_config * config;
634
635 if (tinfo == NULL)
636 Py_RETURN_NONE;
637
638 config = btrace_conf (&tinfo->btrace);
639
640 if (config == NULL)
641 Py_RETURN_NONE;
642
643 return PyString_FromString (btrace_format_short_string (config->format));
644}
645
646/* Implementation of
647 BtraceRecord.replay_position [BtraceInstruction]. */
648
649PyObject *
650recpy_bt_replay_position (PyObject *self, void *closure)
651{
ae20e79a 652 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 653 thread_info * tinfo = record->thread;
75c0bdf4
TW
654
655 if (tinfo == NULL)
656 Py_RETURN_NONE;
657
658 if (tinfo->btrace.replay == NULL)
659 Py_RETURN_NONE;
660
913aeadd
TW
661 return btpy_insn_or_gap_new (tinfo,
662 btrace_insn_number (tinfo->btrace.replay));
75c0bdf4
TW
663}
664
665/* Implementation of
666 BtraceRecord.begin [BtraceInstruction]. */
667
668PyObject *
669recpy_bt_begin (PyObject *self, void *closure)
670{
ae20e79a 671 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 672 thread_info *const tinfo = record->thread;
75c0bdf4
TW
673 struct btrace_insn_iterator iterator;
674
675 if (tinfo == NULL)
676 Py_RETURN_NONE;
677
4a4495d6 678 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
679
680 if (btrace_is_empty (tinfo))
681 Py_RETURN_NONE;
682
683 btrace_insn_begin (&iterator, &tinfo->btrace);
913aeadd 684 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
75c0bdf4
TW
685}
686
687/* Implementation of
688 BtraceRecord.end [BtraceInstruction]. */
689
690PyObject *
691recpy_bt_end (PyObject *self, void *closure)
692{
ae20e79a 693 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 694 thread_info *const tinfo = record->thread;
75c0bdf4
TW
695 struct btrace_insn_iterator iterator;
696
697 if (tinfo == NULL)
698 Py_RETURN_NONE;
699
4a4495d6 700 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
701
702 if (btrace_is_empty (tinfo))
703 Py_RETURN_NONE;
704
705 btrace_insn_end (&iterator, &tinfo->btrace);
913aeadd 706 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
75c0bdf4
TW
707}
708
709/* Implementation of
710 BtraceRecord.instruction_history [list]. */
711
712PyObject *
713recpy_bt_instruction_history (PyObject *self, void *closure)
714{
ae20e79a 715 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 716 thread_info *const tinfo = record->thread;
75c0bdf4
TW
717 struct btrace_insn_iterator iterator;
718 unsigned long first = 0;
719 unsigned long last = 0;
720
721 if (tinfo == NULL)
722 Py_RETURN_NONE;
723
4a4495d6 724 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4
TW
725
726 if (btrace_is_empty (tinfo))
727 Py_RETURN_NONE;
728
729 btrace_insn_begin (&iterator, &tinfo->btrace);
730 first = btrace_insn_number (&iterator);
731
732 btrace_insn_end (&iterator, &tinfo->btrace);
733 last = btrace_insn_number (&iterator);
734
00431a78 735 return btpy_list_new (tinfo, first, last, 1, &recpy_insn_type);
75c0bdf4
TW
736}
737
738/* Implementation of
739 BtraceRecord.function_call_history [list]. */
740
741PyObject *
742recpy_bt_function_call_history (PyObject *self, void *closure)
743{
ae20e79a 744 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 745 thread_info *const tinfo = record->thread;
8d0050ea
TW
746 struct btrace_call_iterator iterator;
747 unsigned long first = 0;
748 unsigned long last = 0;
75c0bdf4 749
8d0050ea
TW
750 if (tinfo == NULL)
751 Py_RETURN_NONE;
75c0bdf4 752
4a4495d6 753 btrace_fetch (tinfo, record_btrace_get_cpu ());
75c0bdf4 754
8d0050ea
TW
755 if (btrace_is_empty (tinfo))
756 Py_RETURN_NONE;
75c0bdf4 757
8d0050ea
TW
758 btrace_call_begin (&iterator, &tinfo->btrace);
759 first = btrace_call_number (&iterator);
75c0bdf4 760
8d0050ea
TW
761 btrace_call_end (&iterator, &tinfo->btrace);
762 last = btrace_call_number (&iterator);
75c0bdf4 763
00431a78 764 return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
75c0bdf4
TW
765}
766
767/* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
768
769PyObject *
770recpy_bt_goto (PyObject *self, PyObject *args)
771{
ae20e79a 772 const recpy_record_object * const record = (recpy_record_object *) self;
00431a78 773 thread_info *const tinfo = record->thread;
0ed5da75 774 const recpy_element_object *obj;
96b1ad86 775 PyObject *parse_obj;
75c0bdf4
TW
776
777 if (tinfo == NULL || btrace_is_empty (tinfo))
778 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
779
96b1ad86 780 if (!PyArg_ParseTuple (args, "O", &parse_obj))
75c0bdf4
TW
781 return NULL;
782
96b1ad86 783 if (Py_TYPE (parse_obj) != &recpy_insn_type)
75c0bdf4 784 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
96b1ad86 785 obj = (const recpy_element_object *) parse_obj;
75c0bdf4 786
a70b8144 787 try
75c0bdf4
TW
788 {
789 struct btrace_insn_iterator iter;
790
791 btrace_insn_end (&iter, &tinfo->btrace);
792
793 if (btrace_insn_number (&iter) == obj->number)
794 target_goto_record_end ();
795 else
796 target_goto_record (obj->number);
797 }
230d2906 798 catch (const gdb_exception &except)
75c0bdf4
TW
799 {
800 GDB_PY_HANDLE_EXCEPTION (except);
801 }
75c0bdf4
TW
802
803 Py_RETURN_NONE;
804}
805
75c0bdf4
TW
806/* BtraceList methods. */
807
808struct PyMethodDef btpy_list_methods[] =
809{
810 { "count", btpy_list_count, METH_O, "count number of occurences"},
811 { "index", btpy_list_index, METH_O, "index of entry"},
812 {NULL}
813};
814
815/* BtraceList sequence methods. */
816
817static PySequenceMethods btpy_list_sequence_methods =
818{
819 NULL
820};
821
822/* BtraceList mapping methods. Necessary for slicing. */
823
824static PyMappingMethods btpy_list_mapping_methods =
825{
826 NULL
827};
828
829/* Sets up the btrace record API. */
830
831int
832gdbpy_initialize_btrace (void)
833{
75c0bdf4
TW
834 btpy_list_type.tp_new = PyType_GenericNew;
835 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
836 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
837 btpy_list_type.tp_name = "gdb.BtraceObjectList";
838 btpy_list_type.tp_doc = "GDB btrace list object";
839 btpy_list_type.tp_methods = btpy_list_methods;
840 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
841 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
842 btpy_list_type.tp_richcompare = btpy_list_richcompare;
843
844 btpy_list_sequence_methods.sq_item = btpy_list_item;
845 btpy_list_sequence_methods.sq_length = btpy_list_length;
846 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
847
848 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
849
14f819c8 850 return PyType_Ready (&btpy_list_type);
75c0bdf4 851}
This page took 0.506006 seconds and 4 git commands to generate.