1e463ffe0fe74be45e8b0de37bf59dc60e6d8934
[deliverable/binutils-gdb.git] / gdb / python / py-record-btrace.c
1 /* Python interface to btrace instruction history.
2
3 Copyright 2016-2017 Free Software Foundation, Inc.
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"
25 #include "py-record.h"
26 #include "py-record-btrace.h"
27 #include "disasm.h"
28
29 #if defined (IS_PY3K)
30
31 #define BTPY_PYSLICE(x) (x)
32
33 #else
34
35 #define BTPY_PYSLICE(x) ((PySliceObject *) x)
36
37 #endif
38
39 #define BTPY_REQUIRE_VALID_INSN(obj, iter) \
40 do { \
41 struct thread_info *tinfo = find_thread_ptid (obj->ptid); \
42 if (tinfo == NULL || btrace_is_empty (tinfo)) \
43 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace.")); \
44 if (0 == btrace_find_insn_by_number (&iter, &tinfo->btrace, \
45 obj->number)) \
46 return PyErr_Format (gdbpy_gdb_error, _("No such instruction.")); \
47 } while (0)
48
49 #define BTPY_REQUIRE_VALID_CALL(obj, iter) \
50 do { \
51 struct thread_info *tinfo = find_thread_ptid (obj->ptid); \
52 if (tinfo == NULL || btrace_is_empty (tinfo)) \
53 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace.")); \
54 if (0 == btrace_find_call_by_number (&iter, &tinfo->btrace, \
55 obj->number)) \
56 return PyErr_Format (gdbpy_gdb_error, _("No such call segment."));\
57 } while (0)
58
59 /* This can either be a btrace instruction or a function call segment,
60 depending on the chosen type. */
61
62 typedef struct {
63 PyObject_HEAD
64
65 /* The thread this object belongs to. */
66 ptid_t ptid;
67
68 /* Instruction number or function call segment number, depending on the type
69 of this object. */
70 Py_ssize_t number;
71 } btpy_object;
72
73 /* Python object for btrace record lists. */
74
75 typedef struct {
76 PyObject_HEAD
77
78 /* The thread this list belongs to. */
79 ptid_t ptid;
80
81 /* The first index being part of this list. */
82 Py_ssize_t first;
83
84 /* The last index begin part of this list. */
85 Py_ssize_t last;
86
87 /* Stride size. */
88 Py_ssize_t step;
89
90 /* Either &BTPY_CALL_TYPE or &BTPY_INSN_TYPE. */
91 PyTypeObject* element_type;
92 } btpy_list_object;
93
94 /* Python type for btrace instructions. */
95
96 static PyTypeObject btpy_insn_type = {
97 PyVarObject_HEAD_INIT (NULL, 0)
98 };
99
100 /* Python type for btrace function-calls. */
101
102 static PyTypeObject btpy_call_type = {
103 PyVarObject_HEAD_INIT (NULL, 0)
104 };
105
106 /* Python type for btrace lists. */
107
108 static PyTypeObject btpy_list_type = {
109 PyVarObject_HEAD_INIT (NULL, 0)
110 };
111
112 /* Create a new gdb.BtraceInstruction or gdb.BtraceFunctionCall object,
113 depending on TYPE. */
114
115 static PyObject *
116 btpy_new (ptid_t ptid, Py_ssize_t number, PyTypeObject* type)
117 {
118 btpy_object * const obj = PyObject_New (btpy_object, type);
119
120 if (obj == NULL)
121 return NULL;
122
123 obj->ptid = ptid;
124 obj->number = number;
125
126 return (PyObject *) obj;
127 }
128
129 /* Looks at the recorded item with the number NUMBER and create a
130 gdb.BtraceInstruction or gdb.RecordGap object for it accordingly. */
131
132 static PyObject *
133 btpy_insn_or_gap_new (const thread_info *tinfo, Py_ssize_t number)
134 {
135 btrace_insn_iterator iter;
136 int err_code;
137
138 btrace_find_insn_by_number (&iter, &tinfo->btrace, number);
139 err_code = btrace_insn_get_error (&iter);
140
141 if (err_code != 0)
142 {
143 const btrace_config *config;
144 const char *err_string;
145
146 config = btrace_conf (&tinfo->btrace);
147 err_string = btrace_decode_error (config->format, err_code);
148
149 return recpy_gap_new (err_code, err_string, number);
150 }
151
152 return btpy_new (tinfo->ptid, number, &btpy_insn_type);
153 }
154
155 /* Create a new gdb.BtraceFunctionCall object. */
156
157 static PyObject *
158 btpy_call_new (ptid_t ptid, Py_ssize_t number)
159 {
160 return btpy_new (ptid, number, &btpy_call_type);
161 }
162
163 /* Create a new gdb.BtraceList object. */
164
165 static PyObject *
166 btpy_list_new (ptid_t ptid, Py_ssize_t first, Py_ssize_t last, Py_ssize_t step,
167 PyTypeObject *element_type)
168 {
169 btpy_list_object * const obj = PyObject_New (btpy_list_object,
170 &btpy_list_type);
171
172 if (obj == NULL)
173 return NULL;
174
175 obj->ptid = ptid;
176 obj->first = first;
177 obj->last = last;
178 obj->step = step;
179 obj->element_type = element_type;
180
181 return (PyObject *) obj;
182 }
183
184 /* Implementation of BtraceInstruction.number [int] and
185 BtraceFunctionCall.number [int]. */
186
187 static PyObject *
188 btpy_number (PyObject *self, void *closure)
189 {
190 const btpy_object * const obj = (btpy_object *) self;
191
192 return PyInt_FromSsize_t (obj->number);
193 }
194
195 /* Implementation of BtraceInstruction.__hash__ () -> int and
196 BtraceFunctionCall.__hash__ () -> int. */
197
198 static Py_hash_t
199 btpy_hash (PyObject *self)
200 {
201 const btpy_object * const obj = (btpy_object *) self;
202
203 return obj->number;
204 }
205
206 /* Implementation of BtraceInstruction.sal [gdb.Symtab_and_line].
207 Return the SAL associated with this instruction. */
208
209 static PyObject *
210 btpy_insn_sal (PyObject *self, void *closure)
211 {
212 const btpy_object * const obj = (btpy_object *) self;
213 const struct btrace_insn *insn;
214 struct btrace_insn_iterator iter;
215 PyObject *result = NULL;
216
217 BTPY_REQUIRE_VALID_INSN (obj, iter);
218
219 insn = btrace_insn_get (&iter);
220 if (insn == NULL)
221 return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
222
223 TRY
224 {
225 result = symtab_and_line_to_sal_object (find_pc_line (insn->pc, 0));
226 }
227 CATCH (except, RETURN_MASK_ALL)
228 {
229 GDB_PY_HANDLE_EXCEPTION (except);
230 }
231 END_CATCH
232
233 return result;
234 }
235
236 /* Implementation of BtraceInstruction.pc [int]. Returns
237 the instruction address. */
238
239 static PyObject *
240 btpy_insn_pc (PyObject *self, void *closure)
241 {
242 const btpy_object * const obj = (btpy_object *) self;
243 const struct btrace_insn *insn;
244 struct btrace_insn_iterator iter;
245
246 BTPY_REQUIRE_VALID_INSN (obj, iter);
247
248 insn = btrace_insn_get (&iter);
249 if (insn == NULL)
250 return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
251
252 return gdb_py_long_from_ulongest (insn->pc);
253 }
254
255 /* Implementation of BtraceInstruction.size [int]. Returns
256 the instruction size. */
257
258 static PyObject *
259 btpy_insn_size (PyObject *self, void *closure)
260 {
261 const btpy_object * const obj = (btpy_object *) self;
262 const struct btrace_insn *insn;
263 struct btrace_insn_iterator iter;
264
265 BTPY_REQUIRE_VALID_INSN (obj, iter);
266
267 insn = btrace_insn_get (&iter);
268 if (insn == NULL)
269 return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
270
271 return PyInt_FromLong (insn->size);
272 }
273
274 /* Implementation of BtraceInstruction.is_speculative [bool].
275 Returns if this instruction was executed speculatively. */
276
277 static PyObject *
278 btpy_insn_is_speculative (PyObject *self, void *closure)
279 {
280 const btpy_object * const obj = (btpy_object *) self;
281 const struct btrace_insn *insn;
282 struct btrace_insn_iterator iter;
283
284 BTPY_REQUIRE_VALID_INSN (obj, iter);
285
286 insn = btrace_insn_get (&iter);
287 if (insn == NULL)
288 return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
289
290 if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
291 Py_RETURN_TRUE;
292 else
293 Py_RETURN_FALSE;
294 }
295
296 /* Implementation of BtraceInstruction.data [buffer].
297 Returns raw instruction data. */
298
299 static PyObject *
300 btpy_insn_data (PyObject *self, void *closure)
301 {
302 const btpy_object * const obj = (btpy_object *) self;
303 const struct btrace_insn *insn;
304 struct btrace_insn_iterator iter;
305 gdb_byte *buffer = NULL;
306 PyObject *object;
307
308 BTPY_REQUIRE_VALID_INSN (obj, iter);
309
310 insn = btrace_insn_get (&iter);
311 if (insn == NULL)
312 return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
313
314 TRY
315 {
316 buffer = (gdb_byte *) xmalloc (insn->size);
317 read_memory (insn->pc, buffer, insn->size);
318 }
319 CATCH (except, RETURN_MASK_ALL)
320 {
321 xfree (buffer);
322 GDB_PY_HANDLE_EXCEPTION (except);
323 }
324 END_CATCH
325
326 object = PyBytes_FromStringAndSize ((const char*) buffer, insn->size);
327 xfree (buffer);
328
329 if (object == NULL)
330 return NULL;
331
332 #ifdef IS_PY3K
333 return PyMemoryView_FromObject (object);
334 #else
335 return PyBuffer_FromObject (object, 0, Py_END_OF_BUFFER);
336 #endif
337
338 }
339
340 /* Implementation of BtraceInstruction.decode [str]. Returns
341 the instruction as human readable string. */
342
343 static PyObject *
344 btpy_insn_decode (PyObject *self, void *closure)
345 {
346 const btpy_object * const obj = (btpy_object *) self;
347 const struct btrace_insn *insn;
348 struct btrace_insn_iterator iter;
349 string_file strfile;
350
351 BTPY_REQUIRE_VALID_INSN (obj, iter);
352
353 insn = btrace_insn_get (&iter);
354 if (insn == NULL)
355 return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));
356
357 TRY
358 {
359 gdb_print_insn (target_gdbarch (), insn->pc, &strfile, NULL);
360 }
361 CATCH (except, RETURN_MASK_ALL)
362 {
363 gdbpy_convert_exception (except);
364 return NULL;
365 }
366 END_CATCH
367
368
369 return PyBytes_FromString (strfile.string ().c_str ());
370 }
371
372 /* Implementation of BtraceFunctionCall.level [int]. Returns the
373 call level. */
374
375 static PyObject *
376 btpy_call_level (PyObject *self, void *closure)
377 {
378 const btpy_object * const obj = (btpy_object *) self;
379 const struct btrace_function *func;
380 struct btrace_call_iterator iter;
381
382 BTPY_REQUIRE_VALID_CALL (obj, iter);
383
384 func = btrace_call_get (&iter);
385 if (func == NULL)
386 Py_RETURN_NONE;
387
388 return PyInt_FromLong (iter.btinfo->level + func->level);
389 }
390
391 /* Implementation of BtraceFunctionCall.symbol [gdb.Symbol]. Returns
392 the symbol associated with this function call. */
393
394 static PyObject *
395 btpy_call_symbol (PyObject *self, void *closure)
396 {
397 const btpy_object * const obj = (btpy_object *) self;
398 const struct btrace_function *func;
399 struct btrace_call_iterator iter;
400
401 BTPY_REQUIRE_VALID_CALL (obj, iter);
402
403 func = btrace_call_get (&iter);
404 if (func == NULL)
405 Py_RETURN_NONE;
406
407 if (func->sym == NULL)
408 Py_RETURN_NONE;
409
410 return symbol_to_symbol_object (func->sym);
411 }
412
413 /* Implementation of BtraceFunctionCall.instructions [list].
414 Return the list of instructions that belong to this function call. */
415
416 static PyObject *
417 btpy_call_instructions (PyObject *self, void *closure)
418 {
419 const btpy_object * const obj = (btpy_object *) self;
420 const struct btrace_function *func;
421 struct btrace_call_iterator iter;
422 unsigned int len;
423
424 BTPY_REQUIRE_VALID_CALL (obj, iter);
425
426 func = btrace_call_get (&iter);
427 if (func == NULL)
428 Py_RETURN_NONE;
429
430 len = VEC_length (btrace_insn_s, func->insn);
431
432 /* Gaps count as one instruction. */
433 if (len == 0)
434 len = 1;
435
436 return btpy_list_new (obj->ptid, func->insn_offset, func->insn_offset + len,
437 1, &btpy_insn_type);
438 }
439
440 /* Implementation of BtraceFunctionCall.up [gdb.BtraceRecordCall].
441 Return the caller / returnee of this function. */
442
443 static PyObject *
444 btpy_call_up (PyObject *self, void *closure)
445 {
446 const btpy_object * const obj = (btpy_object *) self;
447 const struct btrace_function *func;
448 struct btrace_call_iterator iter;
449
450 BTPY_REQUIRE_VALID_CALL (obj, iter);
451
452 func = btrace_call_get (&iter);
453 if (func == NULL)
454 Py_RETURN_NONE;
455
456 if (func->up == NULL)
457 Py_RETURN_NONE;
458
459 return btpy_call_new (obj->ptid, func->up->number);
460 }
461
462 /* Implementation of BtraceFunctionCall.prev_sibling [BtraceFunctionCall].
463 Return a previous segment of this function. */
464
465 static PyObject *
466 btpy_call_prev_sibling (PyObject *self, void *closure)
467 {
468 const btpy_object * const obj = (btpy_object *) self;
469 const struct btrace_function *func;
470 struct btrace_call_iterator iter;
471
472 BTPY_REQUIRE_VALID_CALL (obj, iter);
473
474 func = btrace_call_get (&iter);
475 if (func == NULL)
476 Py_RETURN_NONE;
477
478 if (func->segment.prev == NULL)
479 Py_RETURN_NONE;
480
481 return btpy_call_new (obj->ptid, func->segment.prev->number);
482 }
483
484 /* Implementation of BtraceFunctionCall.next_sibling [BtraceFunctionCall].
485 Return a following segment of this function. */
486
487 static PyObject *
488 btpy_call_next_sibling (PyObject *self, void *closure)
489 {
490 const btpy_object * const obj = (btpy_object *) self;
491 const struct btrace_function *func;
492 struct btrace_call_iterator iter;
493
494 BTPY_REQUIRE_VALID_CALL (obj, iter);
495
496 func = btrace_call_get (&iter);
497 if (func == NULL)
498 Py_RETURN_NONE;
499
500 if (func->segment.next == NULL)
501 Py_RETURN_NONE;
502
503 return btpy_call_new (obj->ptid, func->segment.next->number);
504 }
505
506 /* Python rich compare function to allow for equality and inequality checks
507 in Python. */
508
509 static PyObject *
510 btpy_richcompare (PyObject *self, PyObject *other, int op)
511 {
512 const btpy_object * const obj1 = (btpy_object *) self;
513 const btpy_object * const obj2 = (btpy_object *) other;
514
515 if (Py_TYPE (self) != Py_TYPE (other))
516 {
517 Py_INCREF (Py_NotImplemented);
518 return Py_NotImplemented;
519 }
520
521 switch (op)
522 {
523 case Py_EQ:
524 if (ptid_equal (obj1->ptid, obj2->ptid) && obj1->number == obj2->number)
525 Py_RETURN_TRUE;
526 else
527 Py_RETURN_FALSE;
528
529 case Py_NE:
530 if (!ptid_equal (obj1->ptid, obj2->ptid) || obj1->number != obj2->number)
531 Py_RETURN_TRUE;
532 else
533 Py_RETURN_FALSE;
534
535 default:
536 break;
537 }
538
539 Py_INCREF (Py_NotImplemented);
540 return Py_NotImplemented;
541 }
542
543 /* Implementation of BtraceList.__len__ (self) -> int. */
544
545 static Py_ssize_t
546 btpy_list_length (PyObject *self)
547 {
548 const btpy_list_object * const obj = (btpy_list_object *) self;
549 const Py_ssize_t distance = obj->last - obj->first;
550 const Py_ssize_t result = distance / obj->step;
551
552 if ((distance % obj->step) == 0)
553 return result;
554
555 return result + 1;
556 }
557
558 /* Implementation of
559 BtraceList.__getitem__ (self, key) -> BtraceInstruction and
560 BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
561
562 static PyObject *
563 btpy_list_item (PyObject *self, Py_ssize_t index)
564 {
565 const btpy_list_object * const obj = (btpy_list_object *) self;
566 struct thread_info * const tinfo = find_thread_ptid (obj->ptid);
567
568 if (index < 0 || index >= btpy_list_length (self))
569 return PyErr_Format (PyExc_IndexError, _("Index out of range: %zd."),
570 index);
571
572 return btpy_new (obj->ptid, obj->first + (obj->step * index),
573 obj->element_type);
574 }
575
576 /* Implementation of BtraceList.__getitem__ (self, slice) -> BtraceList. */
577
578 static PyObject *
579 btpy_list_slice (PyObject *self, PyObject *value)
580 {
581 const btpy_list_object * const obj = (btpy_list_object *) self;
582 const Py_ssize_t length = btpy_list_length (self);
583 Py_ssize_t start, stop, step, slicelength;
584
585 if (PyInt_Check (value))
586 {
587 Py_ssize_t index = PyInt_AsSsize_t (value);
588
589 /* Emulate Python behavior for negative indices. */
590 if (index < 0)
591 index += length;
592
593 return btpy_list_item (self, index);
594 }
595
596 if (!PySlice_Check (value))
597 return PyErr_Format (PyExc_TypeError, _("Index must be int or slice."));
598
599 if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start, &stop,
600 &step, &slicelength))
601 return NULL;
602
603 return btpy_list_new (obj->ptid, obj->first + obj->step * start,
604 obj->first + obj->step * stop, obj->step * step,
605 obj->element_type);
606 }
607
608 /* Helper function that returns the position of an element in a BtraceList
609 or -1 if the element is not in the list. */
610
611 static LONGEST
612 btpy_list_position (PyObject *self, PyObject *value)
613 {
614 const btpy_list_object * const list_obj = (btpy_list_object *) self;
615 const btpy_object * const obj = (btpy_object *) value;
616 Py_ssize_t index = obj->number;
617
618 if (list_obj->element_type != Py_TYPE (value))
619 return -1;
620
621 if (!ptid_equal (list_obj->ptid, obj->ptid))
622 return -1;
623
624 if (index < list_obj->first || index > list_obj->last)
625 return -1;
626
627 index -= list_obj->first;
628
629 if (index % list_obj->step != 0)
630 return -1;
631
632 return index / list_obj->step;
633 }
634
635 /* Implementation of "in" operator for BtraceLists. */
636
637 static int
638 btpy_list_contains (PyObject *self, PyObject *value)
639 {
640 if (btpy_list_position (self, value) < 0)
641 return 0;
642
643 return 1;
644 }
645
646 /* Implementation of BtraceLists.index (self, value) -> int. */
647
648 static PyObject *
649 btpy_list_index (PyObject *self, PyObject *value)
650 {
651 const LONGEST index = btpy_list_position (self, value);
652
653 if (index < 0)
654 return PyErr_Format (PyExc_ValueError, _("Not in list."));
655
656 return gdb_py_long_from_longest (index);
657 }
658
659 /* Implementation of BtraceList.count (self, value) -> int. */
660
661 static PyObject *
662 btpy_list_count (PyObject *self, PyObject *value)
663 {
664 /* We know that if an element is in the list, it is so exactly one time,
665 enabling us to reuse the "is element of" check. */
666 return PyInt_FromLong (btpy_list_contains (self, value));
667 }
668
669 /* Python rich compare function to allow for equality and inequality checks
670 in Python. */
671
672 static PyObject *
673 btpy_list_richcompare (PyObject *self, PyObject *other, int op)
674 {
675 const btpy_list_object * const obj1 = (btpy_list_object *) self;
676 const btpy_list_object * const obj2 = (btpy_list_object *) other;
677
678 if (Py_TYPE (self) != Py_TYPE (other))
679 {
680 Py_INCREF (Py_NotImplemented);
681 return Py_NotImplemented;
682 }
683
684 switch (op)
685 {
686 case Py_EQ:
687 if (ptid_equal (obj1->ptid, obj2->ptid)
688 && obj1->element_type == obj2->element_type
689 && obj1->first == obj2->first
690 && obj1->last == obj2->last
691 && obj1->step == obj2->step)
692 Py_RETURN_TRUE;
693 else
694 Py_RETURN_FALSE;
695
696 case Py_NE:
697 if (!ptid_equal (obj1->ptid, obj2->ptid)
698 || obj1->element_type != obj2->element_type
699 || obj1->first != obj2->first
700 || obj1->last != obj2->last
701 || obj1->step != obj2->step)
702 Py_RETURN_TRUE;
703 else
704 Py_RETURN_FALSE;
705
706 default:
707 break;
708 }
709
710 Py_INCREF (Py_NotImplemented);
711 return Py_NotImplemented;
712 }
713
714 /* Implementation of
715 BtraceRecord.method [str]. */
716
717 PyObject *
718 recpy_bt_method (PyObject *self, void *closure)
719 {
720 return PyString_FromString ("btrace");
721 }
722
723 /* Implementation of
724 BtraceRecord.format [str]. */
725
726 PyObject *
727 recpy_bt_format (PyObject *self, void *closure)
728 {
729 const recpy_record_object * const record = (recpy_record_object *) self;
730 const struct thread_info * const tinfo = find_thread_ptid (record->ptid);
731 const struct btrace_config * config;
732
733 if (tinfo == NULL)
734 Py_RETURN_NONE;
735
736 config = btrace_conf (&tinfo->btrace);
737
738 if (config == NULL)
739 Py_RETURN_NONE;
740
741 return PyString_FromString (btrace_format_short_string (config->format));
742 }
743
744 /* Implementation of
745 BtraceRecord.replay_position [BtraceInstruction]. */
746
747 PyObject *
748 recpy_bt_replay_position (PyObject *self, void *closure)
749 {
750 const recpy_record_object * const record = (recpy_record_object *) self;
751 const struct thread_info * const tinfo = find_thread_ptid (record->ptid);
752
753 if (tinfo == NULL)
754 Py_RETURN_NONE;
755
756 if (tinfo->btrace.replay == NULL)
757 Py_RETURN_NONE;
758
759 return btpy_insn_or_gap_new (tinfo,
760 btrace_insn_number (tinfo->btrace.replay));
761 }
762
763 /* Implementation of
764 BtraceRecord.begin [BtraceInstruction]. */
765
766 PyObject *
767 recpy_bt_begin (PyObject *self, void *closure)
768 {
769 const recpy_record_object * const record = (recpy_record_object *) self;
770 struct thread_info * const tinfo = find_thread_ptid (record->ptid);
771 struct btrace_insn_iterator iterator;
772
773 if (tinfo == NULL)
774 Py_RETURN_NONE;
775
776 btrace_fetch (tinfo);
777
778 if (btrace_is_empty (tinfo))
779 Py_RETURN_NONE;
780
781 btrace_insn_begin (&iterator, &tinfo->btrace);
782 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
783 }
784
785 /* Implementation of
786 BtraceRecord.end [BtraceInstruction]. */
787
788 PyObject *
789 recpy_bt_end (PyObject *self, void *closure)
790 {
791 const recpy_record_object * const record = (recpy_record_object *) self;
792 struct thread_info * const tinfo = find_thread_ptid (record->ptid);
793 struct btrace_insn_iterator iterator;
794
795 if (tinfo == NULL)
796 Py_RETURN_NONE;
797
798 btrace_fetch (tinfo);
799
800 if (btrace_is_empty (tinfo))
801 Py_RETURN_NONE;
802
803 btrace_insn_end (&iterator, &tinfo->btrace);
804 return btpy_insn_or_gap_new (tinfo, btrace_insn_number (&iterator));
805 }
806
807 /* Implementation of
808 BtraceRecord.instruction_history [list]. */
809
810 PyObject *
811 recpy_bt_instruction_history (PyObject *self, void *closure)
812 {
813 const recpy_record_object * const record = (recpy_record_object *) self;
814 struct thread_info * const tinfo = find_thread_ptid (record->ptid);
815 struct btrace_insn_iterator iterator;
816 unsigned long first = 0;
817 unsigned long last = 0;
818
819 if (tinfo == NULL)
820 Py_RETURN_NONE;
821
822 btrace_fetch (tinfo);
823
824 if (btrace_is_empty (tinfo))
825 Py_RETURN_NONE;
826
827 btrace_insn_begin (&iterator, &tinfo->btrace);
828 first = btrace_insn_number (&iterator);
829
830 btrace_insn_end (&iterator, &tinfo->btrace);
831 last = btrace_insn_number (&iterator);
832
833 return btpy_list_new (record->ptid, first, last, 1, &btpy_insn_type);
834 }
835
836 /* Implementation of
837 BtraceRecord.function_call_history [list]. */
838
839 PyObject *
840 recpy_bt_function_call_history (PyObject *self, void *closure)
841 {
842 const recpy_record_object * const record = (recpy_record_object *) self;
843 struct thread_info * const tinfo = find_thread_ptid (record->ptid);
844 struct btrace_call_iterator iterator;
845 unsigned long first = 0;
846 unsigned long last = 0;
847
848 if (tinfo == NULL)
849 Py_RETURN_NONE;
850
851 btrace_fetch (tinfo);
852
853 if (btrace_is_empty (tinfo))
854 Py_RETURN_NONE;
855
856 btrace_call_begin (&iterator, &tinfo->btrace);
857 first = btrace_call_number (&iterator);
858
859 btrace_call_end (&iterator, &tinfo->btrace);
860 last = btrace_call_number (&iterator);
861
862 return btpy_list_new (record->ptid, first, last, 1, &btpy_call_type);
863 }
864
865 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None. */
866
867 PyObject *
868 recpy_bt_goto (PyObject *self, PyObject *args)
869 {
870 const recpy_record_object * const record = (recpy_record_object *) self;
871 struct thread_info * const tinfo = find_thread_ptid (record->ptid);
872 const btpy_object *obj;
873
874 if (tinfo == NULL || btrace_is_empty (tinfo))
875 return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
876
877 if (!PyArg_ParseTuple (args, "O", &obj))
878 return NULL;
879
880 if (Py_TYPE (obj) != &btpy_insn_type)
881 return PyErr_Format (PyExc_TypeError, _("Argument must be instruction."));
882
883 TRY
884 {
885 struct btrace_insn_iterator iter;
886
887 btrace_insn_end (&iter, &tinfo->btrace);
888
889 if (btrace_insn_number (&iter) == obj->number)
890 target_goto_record_end ();
891 else
892 target_goto_record (obj->number);
893 }
894 CATCH (except, RETURN_MASK_ALL)
895 {
896 GDB_PY_HANDLE_EXCEPTION (except);
897 }
898 END_CATCH
899
900 Py_RETURN_NONE;
901 }
902
903 /* BtraceInstruction members. */
904
905 struct gdb_PyGetSetDef btpy_insn_getset[] =
906 {
907 { "number", btpy_number, NULL, "instruction number", NULL},
908 { "sal", btpy_insn_sal, NULL, "associated symbol and line", NULL},
909 { "pc", btpy_insn_pc, NULL, "instruction address", NULL},
910 { "data", btpy_insn_data, NULL, "raw instruction data", NULL},
911 { "decoded", btpy_insn_decode, NULL, "decoded instruction or error message \
912 if the instruction is a gap", NULL},
913 { "size", btpy_insn_size, NULL, "instruction size in byte", NULL},
914 { "is_speculative", btpy_insn_is_speculative, NULL, "if the instruction was \
915 executed speculatively", NULL},
916 {NULL}
917 };
918
919 /* BtraceFunctionCall members. */
920
921 static gdb_PyGetSetDef btpy_call_getset[] =
922 {
923 { "number", btpy_number, NULL, "function call number", NULL},
924 { "level", btpy_call_level, NULL, "call stack level", NULL},
925 { "symbol", btpy_call_symbol, NULL, "associated line and symbol", NULL},
926 { "instructions", btpy_call_instructions, NULL, "list of instructions in \
927 this function segment", NULL},
928 { "up", btpy_call_up, NULL, "caller or returned-to function segment", NULL},
929 { "prev_sibling", btpy_call_prev_sibling, NULL, "previous segment of this \
930 function", NULL},
931 { "next_sibling", btpy_call_next_sibling, NULL, "next segment of this \
932 function", NULL},
933 {NULL}
934 };
935
936 /* BtraceList methods. */
937
938 struct PyMethodDef btpy_list_methods[] =
939 {
940 { "count", btpy_list_count, METH_O, "count number of occurences"},
941 { "index", btpy_list_index, METH_O, "index of entry"},
942 {NULL}
943 };
944
945 /* BtraceList sequence methods. */
946
947 static PySequenceMethods btpy_list_sequence_methods =
948 {
949 NULL
950 };
951
952 /* BtraceList mapping methods. Necessary for slicing. */
953
954 static PyMappingMethods btpy_list_mapping_methods =
955 {
956 NULL
957 };
958
959 /* Sets up the btrace record API. */
960
961 int
962 gdbpy_initialize_btrace (void)
963 {
964 btpy_insn_type.tp_new = PyType_GenericNew;
965 btpy_insn_type.tp_flags = Py_TPFLAGS_DEFAULT;
966 btpy_insn_type.tp_basicsize = sizeof (btpy_object);
967 btpy_insn_type.tp_name = "gdb.BtraceInstruction";
968 btpy_insn_type.tp_doc = "GDB btrace instruction object";
969 btpy_insn_type.tp_getset = btpy_insn_getset;
970 btpy_insn_type.tp_richcompare = btpy_richcompare;
971 btpy_insn_type.tp_hash = btpy_hash;
972
973 btpy_call_type.tp_new = PyType_GenericNew;
974 btpy_call_type.tp_flags = Py_TPFLAGS_DEFAULT;
975 btpy_call_type.tp_basicsize = sizeof (btpy_object);
976 btpy_call_type.tp_name = "gdb.BtraceFunctionCall";
977 btpy_call_type.tp_doc = "GDB btrace call object";
978 btpy_call_type.tp_getset = btpy_call_getset;
979 btpy_call_type.tp_richcompare = btpy_richcompare;
980 btpy_call_type.tp_hash = btpy_hash;
981
982 btpy_list_type.tp_new = PyType_GenericNew;
983 btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
984 btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
985 btpy_list_type.tp_name = "gdb.BtraceObjectList";
986 btpy_list_type.tp_doc = "GDB btrace list object";
987 btpy_list_type.tp_methods = btpy_list_methods;
988 btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
989 btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
990 btpy_list_type.tp_richcompare = btpy_list_richcompare;
991
992 btpy_list_sequence_methods.sq_item = btpy_list_item;
993 btpy_list_sequence_methods.sq_length = btpy_list_length;
994 btpy_list_sequence_methods.sq_contains = btpy_list_contains;
995
996 btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
997
998 if (PyType_Ready (&btpy_insn_type) < 0
999 || PyType_Ready (&btpy_call_type) < 0
1000 || PyType_Ready (&btpy_list_type) < 0)
1001 return -1;
1002 else
1003 return 0;
1004 }
This page took 0.04942 seconds and 3 git commands to generate.