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"
26 #include "gdbthread.h"
28 /* Python Record type. */
30 static PyTypeObject recpy_record_type = {
31 PyVarObject_HEAD_INIT (NULL, 0)
34 /* Python RecordInstruction type. */
36 PyTypeObject recpy_insn_type = {
37 PyVarObject_HEAD_INIT (NULL, 0)
40 /* Python RecordFunctionSegment type. */
42 PyTypeObject recpy_func_type = {
43 PyVarObject_HEAD_INIT (NULL, 0)
46 /* Python RecordGap type. */
48 PyTypeObject recpy_gap_type = {
49 PyVarObject_HEAD_INIT (NULL, 0)
52 /* Python RecordGap object. */
61 const char *reason_string;
67 /* Implementation of record.method. */
70 recpy_method (PyObject *self, void* closure)
72 const recpy_record_object * const obj = (recpy_record_object *) self;
74 if (obj->method == RECORD_METHOD_FULL)
75 return recpy_full_method (self, closure);
77 if (obj->method == RECORD_METHOD_BTRACE)
78 return recpy_bt_method (self, closure);
80 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
83 /* Implementation of record.format. */
86 recpy_format (PyObject *self, void* closure)
88 const recpy_record_object * const obj = (recpy_record_object *) self;
90 if (obj->method == RECORD_METHOD_FULL)
91 return recpy_full_format (self, closure);
93 if (obj->method == RECORD_METHOD_BTRACE)
94 return recpy_bt_format (self, closure);
96 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
99 /* Implementation of record.goto (instruction) -> None. */
102 recpy_goto (PyObject *self, PyObject *value)
104 const recpy_record_object * const obj = (recpy_record_object *) self;
106 if (obj->method == RECORD_METHOD_BTRACE)
107 return recpy_bt_goto (self, value);
109 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
112 /* Implementation of record.replay_position [instruction] */
115 recpy_replay_position (PyObject *self, void *closure)
117 const recpy_record_object * const obj = (recpy_record_object *) self;
119 if (obj->method == RECORD_METHOD_BTRACE)
120 return recpy_bt_replay_position (self, closure);
122 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
125 /* Implementation of record.instruction_history [list]. */
128 recpy_instruction_history (PyObject *self, void* closure)
130 const recpy_record_object * const obj = (recpy_record_object *) self;
132 if (obj->method == RECORD_METHOD_BTRACE)
133 return recpy_bt_instruction_history (self, closure);
135 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
138 /* Implementation of record.function_call_history [list]. */
141 recpy_function_call_history (PyObject *self, void* closure)
143 const recpy_record_object * const obj = (recpy_record_object *) self;
145 if (obj->method == RECORD_METHOD_BTRACE)
146 return recpy_bt_function_call_history (self, closure);
148 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
151 /* Implementation of record.begin [instruction]. */
154 recpy_begin (PyObject *self, void* closure)
156 const recpy_record_object * const obj = (recpy_record_object *) self;
158 if (obj->method == RECORD_METHOD_BTRACE)
159 return recpy_bt_begin (self, closure);
161 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
164 /* Implementation of record.end [instruction]. */
167 recpy_end (PyObject *self, void* closure)
169 const recpy_record_object * const obj = (recpy_record_object *) self;
171 if (obj->method == RECORD_METHOD_BTRACE)
172 return recpy_bt_end (self, closure);
174 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
177 /* Create a new gdb.RecordInstruction object. */
180 recpy_insn_new (thread_info *thread, enum record_method method, Py_ssize_t number)
182 recpy_element_object * const obj = PyObject_New (recpy_element_object,
188 obj->thread = thread;
189 obj->method = method;
190 obj->number = number;
192 return (PyObject *) obj;
195 /* Implementation of RecordInstruction.sal [gdb.Symtab_and_line]. */
198 recpy_insn_sal (PyObject *self, void *closure)
200 const recpy_element_object * const obj = (recpy_element_object *) self;
202 if (obj->method == RECORD_METHOD_BTRACE)
203 return recpy_bt_insn_sal (self, closure);
205 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
208 /* Implementation of RecordInstruction.pc [int]. */
211 recpy_insn_pc (PyObject *self, void *closure)
213 const recpy_element_object * const obj = (recpy_element_object *) self;
215 if (obj->method == RECORD_METHOD_BTRACE)
216 return recpy_bt_insn_pc (self, closure);
218 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
221 /* Implementation of RecordInstruction.data [buffer]. */
224 recpy_insn_data (PyObject *self, void *closure)
226 const recpy_element_object * const obj = (recpy_element_object *) self;
228 if (obj->method == RECORD_METHOD_BTRACE)
229 return recpy_bt_insn_data (self, closure);
231 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
234 /* Implementation of RecordInstruction.decoded [str]. */
237 recpy_insn_decoded (PyObject *self, void *closure)
239 const recpy_element_object * const obj = (recpy_element_object *) self;
241 if (obj->method == RECORD_METHOD_BTRACE)
242 return recpy_bt_insn_decoded (self, closure);
244 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
247 /* Implementation of RecordInstruction.size [int]. */
250 recpy_insn_size (PyObject *self, void *closure)
252 const recpy_element_object * const obj = (recpy_element_object *) self;
254 if (obj->method == RECORD_METHOD_BTRACE)
255 return recpy_bt_insn_size (self, closure);
257 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
260 /* Implementation of RecordInstruction.is_speculative [bool]. */
263 recpy_insn_is_speculative (PyObject *self, void *closure)
265 const recpy_element_object * const obj = (recpy_element_object *) self;
267 if (obj->method == RECORD_METHOD_BTRACE)
268 return recpy_bt_insn_is_speculative (self, closure);
270 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
273 /* Create a new gdb.RecordFunctionSegment object. */
276 recpy_func_new (thread_info *thread, enum record_method method, Py_ssize_t number)
278 recpy_element_object * const obj = PyObject_New (recpy_element_object,
284 obj->thread = thread;
285 obj->method = method;
286 obj->number = number;
288 return (PyObject *) obj;
291 /* Implementation of RecordFunctionSegment.level [int]. */
294 recpy_func_level (PyObject *self, void *closure)
296 const recpy_element_object * const obj = (recpy_element_object *) self;
298 if (obj->method == RECORD_METHOD_BTRACE)
299 return recpy_bt_func_level (self, closure);
301 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
304 /* Implementation of RecordFunctionSegment.symbol [gdb.Symbol]. */
307 recpy_func_symbol (PyObject *self, void *closure)
309 const recpy_element_object * const obj = (recpy_element_object *) self;
311 if (obj->method == RECORD_METHOD_BTRACE)
312 return recpy_bt_func_symbol (self, closure);
314 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
317 /* Implementation of RecordFunctionSegment.instructions [list]. */
320 recpy_func_instructions (PyObject *self, void *closure)
322 const recpy_element_object * const obj = (recpy_element_object *) self;
324 if (obj->method == RECORD_METHOD_BTRACE)
325 return recpy_bt_func_instructions (self, closure);
327 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
330 /* Implementation of RecordFunctionSegment.up [RecordFunctionSegment]. */
333 recpy_func_up (PyObject *self, void *closure)
335 const recpy_element_object * const obj = (recpy_element_object *) self;
337 if (obj->method == RECORD_METHOD_BTRACE)
338 return recpy_bt_func_up (self, closure);
340 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
343 /* Implementation of RecordFunctionSegment.prev [RecordFunctionSegment]. */
346 recpy_func_prev (PyObject *self, void *closure)
348 const recpy_element_object * const obj = (recpy_element_object *) self;
350 if (obj->method == RECORD_METHOD_BTRACE)
351 return recpy_bt_func_prev (self, closure);
353 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
356 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment]. */
359 recpy_func_next (PyObject *self, void *closure)
361 const recpy_element_object * const obj = (recpy_element_object *) self;
363 if (obj->method == RECORD_METHOD_BTRACE)
364 return recpy_bt_func_next (self, closure);
366 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented."));
369 /* Implementation of RecordInstruction.number [int] and
370 RecordFunctionSegment.number [int]. */
373 recpy_element_number (PyObject *self, void* closure)
375 const recpy_element_object * const obj = (recpy_element_object *) self;
377 return PyInt_FromSsize_t (obj->number);
380 /* Implementation of RecordInstruction.__hash__ [int] and
381 RecordFunctionSegment.__hash__ [int]. */
384 recpy_element_hash (PyObject *self)
386 const recpy_element_object * const obj = (recpy_element_object *) self;
391 /* Implementation of operator == and != of RecordInstruction and
392 RecordFunctionSegment. */
395 recpy_element_richcompare (PyObject *self, PyObject *other, int op)
397 const recpy_element_object * const obj1 = (recpy_element_object *) self;
398 const recpy_element_object * const obj2 = (recpy_element_object *) other;
400 if (Py_TYPE (self) != Py_TYPE (other))
402 Py_INCREF (Py_NotImplemented);
403 return Py_NotImplemented;
409 if (obj1->thread == obj2->thread
410 && obj1->method == obj2->method
411 && obj1->number == obj2->number)
417 if (obj1->thread != obj2->thread
418 || obj1->method != obj2->method
419 || obj1->number != obj2->number)
428 Py_INCREF (Py_NotImplemented);
429 return Py_NotImplemented;
432 /* Create a new gdb.RecordGap object. */
435 recpy_gap_new (int reason_code, const char *reason_string, Py_ssize_t number)
437 recpy_gap_object * const obj = PyObject_New (recpy_gap_object,
443 obj->reason_code = reason_code;
444 obj->reason_string = reason_string;
445 obj->number = number;
447 return (PyObject *) obj;
450 /* Implementation of RecordGap.number [int]. */
453 recpy_gap_number (PyObject *self, void *closure)
455 const recpy_gap_object * const obj = (const recpy_gap_object *) self;
457 return PyInt_FromSsize_t (obj->number);
460 /* Implementation of RecordGap.error_code [int]. */
463 recpy_gap_reason_code (PyObject *self, void *closure)
465 const recpy_gap_object * const obj = (const recpy_gap_object *) self;
467 return PyInt_FromLong (obj->reason_code);
470 /* Implementation of RecordGap.error_string [str]. */
473 recpy_gap_reason_string (PyObject *self, void *closure)
475 const recpy_gap_object * const obj = (const recpy_gap_object *) self;
477 return PyString_FromString (obj->reason_string);
480 /* Record method list. */
482 static PyMethodDef recpy_record_methods[] = {
483 { "goto", recpy_goto, METH_VARARGS,
484 "goto (instruction|function_call) -> None.\n\
485 Rewind to given location."},
489 /* Record member list. */
491 static gdb_PyGetSetDef recpy_record_getset[] = {
492 { "method", recpy_method, NULL, "Current recording method.", NULL },
493 { "format", recpy_format, NULL, "Current recording format.", NULL },
494 { "replay_position", recpy_replay_position, NULL, "Current replay position.",
496 { "instruction_history", recpy_instruction_history, NULL,
497 "List of instructions in current recording.", NULL },
498 { "function_call_history", recpy_function_call_history, NULL,
499 "List of function calls in current recording.", NULL },
500 { "begin", recpy_begin, NULL,
501 "First instruction in current recording.", NULL },
502 { "end", recpy_end, NULL,
503 "One past the last instruction in current recording. This is typically \
504 the current instruction and is used for e.g. record.goto (record.end).", NULL },
508 /* RecordInstruction member list. */
510 static gdb_PyGetSetDef recpy_insn_getset[] = {
511 { "number", recpy_element_number, NULL, "instruction number", NULL},
512 { "sal", recpy_insn_sal, NULL, "associated symbol and line", NULL},
513 { "pc", recpy_insn_pc, NULL, "instruction address", NULL},
514 { "data", recpy_insn_data, NULL, "raw instruction data", NULL},
515 { "decoded", recpy_insn_decoded, NULL, "decoded instruction", NULL},
516 { "size", recpy_insn_size, NULL, "instruction size in byte", NULL},
517 { "is_speculative", recpy_insn_is_speculative, NULL, "if the instruction was \
518 executed speculatively", NULL},
522 /* RecordFunctionSegment member list. */
524 static gdb_PyGetSetDef recpy_func_getset[] = {
525 { "number", recpy_element_number, NULL, "function segment number", NULL},
526 { "level", recpy_func_level, NULL, "call stack level", NULL},
527 { "symbol", recpy_func_symbol, NULL, "associated line and symbol", NULL},
528 { "instructions", recpy_func_instructions, NULL, "list of instructions in \
529 this function segment", NULL},
530 { "up", recpy_func_up, NULL, "caller or returned-to function segment", NULL},
531 { "prev", recpy_func_prev, NULL, "previous segment of this function", NULL},
532 { "next", recpy_func_next, NULL, "next segment of this function", NULL},
536 /* RecordGap member list. */
538 static gdb_PyGetSetDef recpy_gap_getset[] = {
539 { "number", recpy_gap_number, NULL, "element number", NULL},
540 { "reason_code", recpy_gap_reason_code, NULL, "reason code", NULL},
541 { "reason_string", recpy_gap_reason_string, NULL, "reason string", NULL},
545 /* Sets up the record API in the gdb module. */
548 gdbpy_initialize_record (void)
550 recpy_record_type.tp_new = PyType_GenericNew;
551 recpy_record_type.tp_flags = Py_TPFLAGS_DEFAULT;
552 recpy_record_type.tp_basicsize = sizeof (recpy_record_object);
553 recpy_record_type.tp_name = "gdb.Record";
554 recpy_record_type.tp_doc = "GDB record object";
555 recpy_record_type.tp_methods = recpy_record_methods;
556 recpy_record_type.tp_getset = recpy_record_getset;
558 recpy_insn_type.tp_new = PyType_GenericNew;
559 recpy_insn_type.tp_flags = Py_TPFLAGS_DEFAULT;
560 recpy_insn_type.tp_basicsize = sizeof (recpy_element_object);
561 recpy_insn_type.tp_name = "gdb.RecordInstruction";
562 recpy_insn_type.tp_doc = "GDB recorded instruction object";
563 recpy_insn_type.tp_getset = recpy_insn_getset;
564 recpy_insn_type.tp_richcompare = recpy_element_richcompare;
565 recpy_insn_type.tp_hash = recpy_element_hash;
566 recpy_insn_type.tp_base = &py_insn_type;
568 recpy_func_type.tp_new = PyType_GenericNew;
569 recpy_func_type.tp_flags = Py_TPFLAGS_DEFAULT;
570 recpy_func_type.tp_basicsize = sizeof (recpy_element_object);
571 recpy_func_type.tp_name = "gdb.RecordFunctionSegment";
572 recpy_func_type.tp_doc = "GDB record function segment object";
573 recpy_func_type.tp_getset = recpy_func_getset;
574 recpy_func_type.tp_richcompare = recpy_element_richcompare;
575 recpy_func_type.tp_hash = recpy_element_hash;
577 recpy_gap_type.tp_new = PyType_GenericNew;
578 recpy_gap_type.tp_flags = Py_TPFLAGS_DEFAULT;
579 recpy_gap_type.tp_basicsize = sizeof (recpy_gap_object);
580 recpy_gap_type.tp_name = "gdb.RecordGap";
581 recpy_gap_type.tp_doc = "GDB recorded gap object";
582 recpy_gap_type.tp_getset = recpy_gap_getset;
584 if (PyType_Ready (&recpy_record_type) < 0
585 || PyType_Ready (&recpy_insn_type) < 0
586 || PyType_Ready (&recpy_func_type) < 0
587 || PyType_Ready (&recpy_gap_type) < 0)
593 /* Implementation of gdb.start_recording (method) -> gdb.Record. */
596 gdbpy_start_recording (PyObject *self, PyObject *args)
598 const char *method = NULL;
599 const char *format = NULL;
600 PyObject *ret = NULL;
602 if (!PyArg_ParseTuple (args, "|ss", &method, &format))
607 record_start (method, format, 0);
608 ret = gdbpy_current_recording (self, args);
610 CATCH (except, RETURN_MASK_ALL)
612 gdbpy_convert_exception (except);
619 /* Implementation of gdb.current_recording (self) -> gdb.Record. */
622 gdbpy_current_recording (PyObject *self, PyObject *args)
624 recpy_record_object *ret = NULL;
626 if (find_record_target () == NULL)
629 ret = PyObject_New (recpy_record_object, &recpy_record_type);
630 ret->thread = inferior_thread ();
631 ret->method = target_record_method (ret->thread->ptid);
633 return (PyObject *) ret;
636 /* Implementation of gdb.stop_recording (self) -> None. */
639 gdbpy_stop_recording (PyObject *self, PyObject *args)
641 PyObject *ret = NULL;
649 CATCH (except, RETURN_MASK_ALL)
651 gdbpy_convert_exception (except);