1 /* Python interface to record targets.
3 Copyright 2016-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "py-instruction.h"
22 #include "py-record.h"
23 #include "py-record-btrace.h"
24 #include "py-record-full.h"
27 /* Python Record type. */
29 static PyTypeObject recpy_record_type
= {
30 PyVarObject_HEAD_INIT (NULL
, 0)
33 /* Python RecordInstruction type. */
35 PyTypeObject recpy_insn_type
= {
36 PyVarObject_HEAD_INIT (NULL
, 0)
39 /* Python RecordFunctionSegment type. */
41 PyTypeObject recpy_func_type
= {
42 PyVarObject_HEAD_INIT (NULL
, 0)
45 /* Python RecordGap type. */
47 PyTypeObject recpy_gap_type
= {
48 PyVarObject_HEAD_INIT (NULL
, 0)
51 /* Python RecordGap object. */
60 const char *reason_string
;
66 /* Implementation of record.method. */
69 recpy_method (PyObject
*self
, void* closure
)
71 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
73 if (obj
->method
== RECORD_METHOD_FULL
)
74 return recpy_full_method (self
, closure
);
76 if (obj
->method
== RECORD_METHOD_BTRACE
)
77 return recpy_bt_method (self
, closure
);
79 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
82 /* Implementation of record.format. */
85 recpy_format (PyObject
*self
, void* closure
)
87 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
89 if (obj
->method
== RECORD_METHOD_FULL
)
90 return recpy_full_format (self
, closure
);
92 if (obj
->method
== RECORD_METHOD_BTRACE
)
93 return recpy_bt_format (self
, closure
);
95 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
98 /* Implementation of record.goto (instruction) -> None. */
101 recpy_goto (PyObject
*self
, PyObject
*value
)
103 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
105 if (obj
->method
== RECORD_METHOD_BTRACE
)
106 return recpy_bt_goto (self
, value
);
108 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
111 /* Implementation of record.replay_position [instruction] */
114 recpy_replay_position (PyObject
*self
, void *closure
)
116 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
118 if (obj
->method
== RECORD_METHOD_BTRACE
)
119 return recpy_bt_replay_position (self
, closure
);
121 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
124 /* Implementation of record.instruction_history [list]. */
127 recpy_instruction_history (PyObject
*self
, void* closure
)
129 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
131 if (obj
->method
== RECORD_METHOD_BTRACE
)
132 return recpy_bt_instruction_history (self
, closure
);
134 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
137 /* Implementation of record.function_call_history [list]. */
140 recpy_function_call_history (PyObject
*self
, void* closure
)
142 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
144 if (obj
->method
== RECORD_METHOD_BTRACE
)
145 return recpy_bt_function_call_history (self
, closure
);
147 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
150 /* Implementation of record.begin [instruction]. */
153 recpy_begin (PyObject
*self
, void* closure
)
155 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
157 if (obj
->method
== RECORD_METHOD_BTRACE
)
158 return recpy_bt_begin (self
, closure
);
160 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
163 /* Implementation of record.end [instruction]. */
166 recpy_end (PyObject
*self
, void* closure
)
168 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
170 if (obj
->method
== RECORD_METHOD_BTRACE
)
171 return recpy_bt_end (self
, closure
);
173 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
176 /* Create a new gdb.RecordInstruction object. */
179 recpy_insn_new (ptid_t ptid
, enum record_method method
, Py_ssize_t number
)
181 recpy_element_object
* const obj
= PyObject_New (recpy_element_object
,
188 obj
->method
= method
;
189 obj
->number
= number
;
191 return (PyObject
*) obj
;
194 /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line]. */
197 recpy_insn_sal (PyObject
*self
, void *closure
)
199 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
201 if (obj
->method
== RECORD_METHOD_BTRACE
)
202 return recpy_bt_insn_sal (self
, closure
);
204 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
207 /* Implementation of RecordInstruction.pc [int]. */
210 recpy_insn_pc (PyObject
*self
, void *closure
)
212 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
214 if (obj
->method
== RECORD_METHOD_BTRACE
)
215 return recpy_bt_insn_pc (self
, closure
);
217 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
220 /* Implementation of RecordInstruction.data [buffer]. */
223 recpy_insn_data (PyObject
*self
, void *closure
)
225 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
227 if (obj
->method
== RECORD_METHOD_BTRACE
)
228 return recpy_bt_insn_data (self
, closure
);
230 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
233 /* Implementation of RecordInstruction.decoded [str]. */
236 recpy_insn_decoded (PyObject
*self
, void *closure
)
238 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
240 if (obj
->method
== RECORD_METHOD_BTRACE
)
241 return recpy_bt_insn_decoded (self
, closure
);
243 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
246 /* Implementation of RecordInstruction.size [int]. */
249 recpy_insn_size (PyObject
*self
, void *closure
)
251 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
253 if (obj
->method
== RECORD_METHOD_BTRACE
)
254 return recpy_bt_insn_size (self
, closure
);
256 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
259 /* Implementation of RecordInstruction.is_speculative [bool]. */
262 recpy_insn_is_speculative (PyObject
*self
, void *closure
)
264 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
266 if (obj
->method
== RECORD_METHOD_BTRACE
)
267 return recpy_bt_insn_is_speculative (self
, closure
);
269 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
272 /* Create a new gdb.RecordFunctionSegment object. */
275 recpy_func_new (ptid_t ptid
, enum record_method method
, Py_ssize_t number
)
277 recpy_element_object
* const obj
= PyObject_New (recpy_element_object
,
284 obj
->method
= method
;
285 obj
->number
= number
;
287 return (PyObject
*) obj
;
290 /* Implementation of RecordFunctionSegment.level [int]. */
293 recpy_func_level (PyObject
*self
, void *closure
)
295 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
297 if (obj
->method
== RECORD_METHOD_BTRACE
)
298 return recpy_bt_func_level (self
, closure
);
300 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
303 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol]. */
306 recpy_func_symbol (PyObject
*self
, void *closure
)
308 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
310 if (obj
->method
== RECORD_METHOD_BTRACE
)
311 return recpy_bt_func_symbol (self
, closure
);
313 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
316 /* Implementation of RecordFunctionSegment.instructions [list]. */
319 recpy_func_instructions (PyObject
*self
, void *closure
)
321 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
323 if (obj
->method
== RECORD_METHOD_BTRACE
)
324 return recpy_bt_func_instructions (self
, closure
);
326 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
329 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment]. */
332 recpy_func_up (PyObject
*self
, void *closure
)
334 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
336 if (obj
->method
== RECORD_METHOD_BTRACE
)
337 return recpy_bt_func_up (self
, closure
);
339 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
342 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment]. */
345 recpy_func_prev (PyObject
*self
, void *closure
)
347 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
349 if (obj
->method
== RECORD_METHOD_BTRACE
)
350 return recpy_bt_func_prev (self
, closure
);
352 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
355 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment]. */
358 recpy_func_next (PyObject
*self
, void *closure
)
360 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
362 if (obj
->method
== RECORD_METHOD_BTRACE
)
363 return recpy_bt_func_next (self
, closure
);
365 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
368 /* Implementation of RecordInstruction.number [int] and
369 RecordFunctionSegment.number [int]. */
372 recpy_element_number (PyObject
*self
, void* closure
)
374 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
376 return PyInt_FromSsize_t (obj
->number
);
379 /* Implementation of RecordInstruction.__hash__ [int] and
380 RecordFunctionSegment.__hash__ [int]. */
383 recpy_element_hash (PyObject
*self
)
385 const recpy_element_object
* const obj
= (recpy_element_object
*) self
;
390 /* Implementation of operator == and != of RecordInstruction and
391 RecordFunctionSegment. */
394 recpy_element_richcompare (PyObject
*self
, PyObject
*other
, int op
)
396 const recpy_element_object
* const obj1
= (recpy_element_object
*) self
;
397 const recpy_element_object
* const obj2
= (recpy_element_object
*) other
;
399 if (Py_TYPE (self
) != Py_TYPE (other
))
401 Py_INCREF (Py_NotImplemented
);
402 return Py_NotImplemented
;
408 if (ptid_equal (obj1
->ptid
, obj2
->ptid
)
409 && obj1
->method
== obj2
->method
410 && obj1
->number
== obj2
->number
)
416 if (!ptid_equal (obj1
->ptid
, obj2
->ptid
)
417 || obj1
->method
!= obj2
->method
418 || obj1
->number
!= obj2
->number
)
427 Py_INCREF (Py_NotImplemented
);
428 return Py_NotImplemented
;
431 /* Create a new gdb.RecordGap object. */
434 recpy_gap_new (int reason_code
, const char *reason_string
, Py_ssize_t number
)
436 recpy_gap_object
* const obj
= PyObject_New (recpy_gap_object
,
442 obj
->reason_code
= reason_code
;
443 obj
->reason_string
= reason_string
;
444 obj
->number
= number
;
446 return (PyObject
*) obj
;
449 /* Implementation of RecordGap.number [int]. */
452 recpy_gap_number (PyObject
*self
, void *closure
)
454 const recpy_gap_object
* const obj
= (const recpy_gap_object
*) self
;
456 return PyInt_FromSsize_t (obj
->number
);
459 /* Implementation of RecordGap.error_code [int]. */
462 recpy_gap_reason_code (PyObject
*self
, void *closure
)
464 const recpy_gap_object
* const obj
= (const recpy_gap_object
*) self
;
466 return PyInt_FromLong (obj
->reason_code
);
469 /* Implementation of RecordGap.error_string [str]. */
472 recpy_gap_reason_string (PyObject
*self
, void *closure
)
474 const recpy_gap_object
* const obj
= (const recpy_gap_object
*) self
;
476 return PyString_FromString (obj
->reason_string
);
479 /* Record method list. */
481 static PyMethodDef recpy_record_methods
[] = {
482 { "goto", recpy_goto
, METH_VARARGS
,
483 "goto (instruction|function_call) -> None.\n\
484 Rewind to given location."},
488 /* Record member list. */
490 static gdb_PyGetSetDef recpy_record_getset
[] = {
491 { "method", recpy_method
, NULL
, "Current recording method.", NULL
},
492 { "format", recpy_format
, NULL
, "Current recording format.", NULL
},
493 { "replay_position", recpy_replay_position
, NULL
, "Current replay position.",
495 { "instruction_history", recpy_instruction_history
, NULL
,
496 "List of instructions in current recording.", NULL
},
497 { "function_call_history", recpy_function_call_history
, NULL
,
498 "List of function calls in current recording.", NULL
},
499 { "begin", recpy_begin
, NULL
,
500 "First instruction in current recording.", NULL
},
501 { "end", recpy_end
, NULL
,
502 "One past the last instruction in current recording. This is typically \
503 the current instruction and is used for e.g. record.goto (record.end).", NULL
},
507 /* RecordInstruction member list. */
509 static gdb_PyGetSetDef recpy_insn_getset
[] = {
510 { "number", recpy_element_number
, NULL
, "instruction number", NULL
},
511 { "sal", recpy_insn_sal
, NULL
, "associated symbol and line", NULL
},
512 { "pc", recpy_insn_pc
, NULL
, "instruction address", NULL
},
513 { "data", recpy_insn_data
, NULL
, "raw instruction data", NULL
},
514 { "decoded", recpy_insn_decoded
, NULL
, "decoded instruction", NULL
},
515 { "size", recpy_insn_size
, NULL
, "instruction size in byte", NULL
},
516 { "is_speculative", recpy_insn_is_speculative
, NULL
, "if the instruction was \
517 executed speculatively", NULL
},
521 /* RecordFunctionSegment member list. */
523 static gdb_PyGetSetDef recpy_func_getset
[] = {
524 { "number", recpy_element_number
, NULL
, "function segment number", NULL
},
525 { "level", recpy_func_level
, NULL
, "call stack level", NULL
},
526 { "symbol", recpy_func_symbol
, NULL
, "associated line and symbol", NULL
},
527 { "instructions", recpy_func_instructions
, NULL
, "list of instructions in \
528 this function segment", NULL
},
529 { "up", recpy_func_up
, NULL
, "caller or returned-to function segment", NULL
},
530 { "prev", recpy_func_prev
, NULL
, "previous segment of this function", NULL
},
531 { "next", recpy_func_next
, NULL
, "next segment of this function", NULL
},
535 /* RecordGap member list. */
537 static gdb_PyGetSetDef recpy_gap_getset
[] = {
538 { "number", recpy_gap_number
, NULL
, "element number", NULL
},
539 { "reason_code", recpy_gap_reason_code
, NULL
, "reason code", NULL
},
540 { "reason_string", recpy_gap_reason_string
, NULL
, "reason string", NULL
},
544 /* Sets up the record API in the gdb module. */
547 gdbpy_initialize_record (void)
549 recpy_record_type
.tp_new
= PyType_GenericNew
;
550 recpy_record_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
551 recpy_record_type
.tp_basicsize
= sizeof (recpy_record_object
);
552 recpy_record_type
.tp_name
= "gdb.Record";
553 recpy_record_type
.tp_doc
= "GDB record object";
554 recpy_record_type
.tp_methods
= recpy_record_methods
;
555 recpy_record_type
.tp_getset
= recpy_record_getset
;
557 recpy_insn_type
.tp_new
= PyType_GenericNew
;
558 recpy_insn_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
559 recpy_insn_type
.tp_basicsize
= sizeof (recpy_element_object
);
560 recpy_insn_type
.tp_name
= "gdb.RecordInstruction";
561 recpy_insn_type
.tp_doc
= "GDB recorded instruction object";
562 recpy_insn_type
.tp_getset
= recpy_insn_getset
;
563 recpy_insn_type
.tp_richcompare
= recpy_element_richcompare
;
564 recpy_insn_type
.tp_hash
= recpy_element_hash
;
565 recpy_insn_type
.tp_base
= &py_insn_type
;
567 recpy_func_type
.tp_new
= PyType_GenericNew
;
568 recpy_func_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
569 recpy_func_type
.tp_basicsize
= sizeof (recpy_element_object
);
570 recpy_func_type
.tp_name
= "gdb.RecordFunctionSegment";
571 recpy_func_type
.tp_doc
= "GDB record function segment object";
572 recpy_func_type
.tp_getset
= recpy_func_getset
;
573 recpy_func_type
.tp_richcompare
= recpy_element_richcompare
;
574 recpy_func_type
.tp_hash
= recpy_element_hash
;
576 recpy_gap_type
.tp_new
= PyType_GenericNew
;
577 recpy_gap_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
578 recpy_gap_type
.tp_basicsize
= sizeof (recpy_gap_object
);
579 recpy_gap_type
.tp_name
= "gdb.RecordGap";
580 recpy_gap_type
.tp_doc
= "GDB recorded gap object";
581 recpy_gap_type
.tp_getset
= recpy_gap_getset
;
583 if (PyType_Ready (&recpy_record_type
) < 0
584 || PyType_Ready (&recpy_insn_type
) < 0
585 || PyType_Ready (&recpy_func_type
) < 0
586 || PyType_Ready (&recpy_gap_type
) < 0)
592 /* Implementation of gdb.start_recording (method) -> gdb.Record. */
595 gdbpy_start_recording (PyObject
*self
, PyObject
*args
)
597 const char *method
= NULL
;
598 const char *format
= NULL
;
599 PyObject
*ret
= NULL
;
601 if (!PyArg_ParseTuple (args
, "|ss", &method
, &format
))
606 record_start (method
, format
, 0);
607 ret
= gdbpy_current_recording (self
, args
);
609 CATCH (except
, RETURN_MASK_ALL
)
611 gdbpy_convert_exception (except
);
618 /* Implementation of gdb.current_recording (self) -> gdb.Record. */
621 gdbpy_current_recording (PyObject
*self
, PyObject
*args
)
623 recpy_record_object
*ret
= NULL
;
625 if (find_record_target () == NULL
)
628 ret
= PyObject_New (recpy_record_object
, &recpy_record_type
);
629 ret
->ptid
= inferior_ptid
;
630 ret
->method
= target_record_method (inferior_ptid
);
632 return (PyObject
*) ret
;
635 /* Implementation of gdb.stop_recording (self) -> None. */
638 gdbpy_stop_recording (PyObject
*self
, PyObject
*args
)
640 PyObject
*ret
= NULL
;
648 CATCH (except
, RETURN_MASK_ALL
)
650 gdbpy_convert_exception (except
);