1 /* Python interface to record targets.
3 Copyright 2016-2017 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-record.h"
22 #include "py-record-btrace.h"
23 #include "py-record-full.h"
26 /* Python Record type. */
28 static PyTypeObject recpy_record_type
= {
29 PyVarObject_HEAD_INIT (NULL
, 0)
32 /* Python RecordGap type. */
34 PyTypeObject recpy_gap_type
= {
35 PyVarObject_HEAD_INIT (NULL
, 0)
38 /* Python RecordGap object. */
47 const char *reason_string
;
53 /* Implementation of record.method. */
56 recpy_method (PyObject
*self
, void* closure
)
58 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
60 if (obj
->method
== RECORD_METHOD_FULL
)
61 return recpy_full_method (self
, closure
);
63 if (obj
->method
== RECORD_METHOD_BTRACE
)
64 return recpy_bt_method (self
, closure
);
66 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
69 /* Implementation of record.format. */
72 recpy_format (PyObject
*self
, void* closure
)
74 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
76 if (obj
->method
== RECORD_METHOD_FULL
)
77 return recpy_full_format (self
, closure
);
79 if (obj
->method
== RECORD_METHOD_BTRACE
)
80 return recpy_bt_format (self
, closure
);
82 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
85 /* Implementation of record.goto (instruction) -> None. */
88 recpy_goto (PyObject
*self
, PyObject
*value
)
90 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
92 if (obj
->method
== RECORD_METHOD_BTRACE
)
93 return recpy_bt_goto (self
, value
);
95 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
98 /* Implementation of record.replay_position [instruction] */
101 recpy_replay_position (PyObject
*self
, void *closure
)
103 const recpy_record_object
* const obj
= (recpy_record_object
*) self
;
105 if (obj
->method
== RECORD_METHOD_BTRACE
)
106 return recpy_bt_replay_position (self
, closure
);
108 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
111 /* Implementation of record.instruction_history [list]. */
114 recpy_instruction_history (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_instruction_history (self
, closure
);
121 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
124 /* Implementation of record.function_call_history [list]. */
127 recpy_function_call_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_function_call_history (self
, closure
);
134 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
137 /* Implementation of record.begin [instruction]. */
140 recpy_begin (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_begin (self
, closure
);
147 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
150 /* Implementation of record.end [instruction]. */
153 recpy_end (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_end (self
, closure
);
160 return PyErr_Format (PyExc_NotImplementedError
, _("Not implemented."));
163 /* Create a new gdb.RecordGap object. */
166 recpy_gap_new (int reason_code
, const char *reason_string
, Py_ssize_t number
)
168 recpy_gap_object
* const obj
= PyObject_New (recpy_gap_object
,
174 obj
->reason_code
= reason_code
;
175 obj
->reason_string
= reason_string
;
176 obj
->number
= number
;
178 return (PyObject
*) obj
;
181 /* Implementation of RecordGap.number [int]. */
184 recpy_gap_number (PyObject
*self
, void *closure
)
186 const recpy_gap_object
* const obj
= (const recpy_gap_object
*) self
;
188 return PyInt_FromSsize_t (obj
->number
);
191 /* Implementation of RecordGap.error_code [int]. */
194 recpy_gap_reason_code (PyObject
*self
, void *closure
)
196 const recpy_gap_object
* const obj
= (const recpy_gap_object
*) self
;
198 return PyInt_FromLong (obj
->reason_code
);
201 /* Implementation of RecordGap.error_string [str]. */
204 recpy_gap_reason_string (PyObject
*self
, void *closure
)
206 const recpy_gap_object
* const obj
= (const recpy_gap_object
*) self
;
208 return PyString_FromString (obj
->reason_string
);
211 /* Record method list. */
213 static PyMethodDef recpy_record_methods
[] = {
214 { "goto", recpy_goto
, METH_VARARGS
,
215 "goto (instruction|function_call) -> None.\n\
216 Rewind to given location."},
220 /* Record member list. */
222 static gdb_PyGetSetDef recpy_record_getset
[] = {
223 { "method", recpy_method
, NULL
, "Current recording method.", NULL
},
224 { "format", recpy_format
, NULL
, "Current recording format.", NULL
},
225 { "replay_position", recpy_replay_position
, NULL
, "Current replay position.",
227 { "instruction_history", recpy_instruction_history
, NULL
,
228 "List of instructions in current recording.", NULL
},
229 { "function_call_history", recpy_function_call_history
, NULL
,
230 "List of function calls in current recording.", NULL
},
231 { "begin", recpy_begin
, NULL
,
232 "First instruction in current recording.", NULL
},
233 { "end", recpy_end
, NULL
,
234 "One past the last instruction in current recording. This is typically \
235 the current instruction and is used for e.g. record.goto (record.end).", NULL
},
239 /* RecordGap member list. */
241 static gdb_PyGetSetDef recpy_gap_getset
[] = {
242 { "number", recpy_gap_number
, NULL
, "element number", NULL
},
243 { "reason_code", recpy_gap_reason_code
, NULL
, "reason code", NULL
},
244 { "reason_string", recpy_gap_reason_string
, NULL
, "reason string", NULL
},
248 /* Sets up the record API in the gdb module. */
251 gdbpy_initialize_record (void)
253 recpy_record_type
.tp_new
= PyType_GenericNew
;
254 recpy_record_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
255 recpy_record_type
.tp_basicsize
= sizeof (recpy_record_object
);
256 recpy_record_type
.tp_name
= "gdb.Record";
257 recpy_record_type
.tp_doc
= "GDB record object";
258 recpy_record_type
.tp_methods
= recpy_record_methods
;
259 recpy_record_type
.tp_getset
= recpy_record_getset
;
261 recpy_gap_type
.tp_new
= PyType_GenericNew
;
262 recpy_gap_type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
263 recpy_gap_type
.tp_basicsize
= sizeof (recpy_gap_object
);
264 recpy_gap_type
.tp_name
= "gdb.RecordGap";
265 recpy_gap_type
.tp_doc
= "GDB recorded gap object";
266 recpy_gap_type
.tp_getset
= recpy_gap_getset
;
268 if (PyType_Ready (&recpy_record_type
) < 0
269 || PyType_Ready (&recpy_gap_type
) < 0)
275 /* Implementation of gdb.start_recording (method) -> gdb.Record. */
278 gdbpy_start_recording (PyObject
*self
, PyObject
*args
)
280 const char *method
= NULL
;
281 const char *format
= NULL
;
282 PyObject
*ret
= NULL
;
284 if (!PyArg_ParseTuple (args
, "|ss", &method
, &format
))
289 record_start (method
, format
, 0);
290 ret
= gdbpy_current_recording (self
, args
);
292 CATCH (except
, RETURN_MASK_ALL
)
294 gdbpy_convert_exception (except
);
301 /* Implementation of gdb.current_recording (self) -> gdb.Record. */
304 gdbpy_current_recording (PyObject
*self
, PyObject
*args
)
306 recpy_record_object
*ret
= NULL
;
308 if (find_record_target () == NULL
)
311 ret
= PyObject_New (recpy_record_object
, &recpy_record_type
);
312 ret
->ptid
= inferior_ptid
;
313 ret
->method
= target_record_method (inferior_ptid
);
315 return (PyObject
*) ret
;
318 /* Implementation of gdb.stop_recording (self) -> None. */
321 gdbpy_stop_recording (PyObject
*self
, PyObject
*args
)
323 PyObject
*ret
= NULL
;
331 CATCH (except
, RETURN_MASK_ALL
)
333 gdbpy_convert_exception (except
);