manual copyright year range of various GDB files to add 2023
[binutils-gdb.git] / gdb / python / py-record.c
blob1e40f2cded0ea0425133810c728fbdabfae750ff
1 /* Python interface to record targets.
3 Copyright 2016-2023 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/>. */
20 #include "defs.h"
21 #include "py-instruction.h"
22 #include "py-record.h"
23 #include "py-record-btrace.h"
24 #include "py-record-full.h"
25 #include "target.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 static PyTypeObject recpy_gap_type = {
49 PyVarObject_HEAD_INIT (NULL, 0)
52 /* Python RecordGap object. */
53 struct recpy_gap_object
55 PyObject_HEAD
57 /* Reason code. */
58 int reason_code;
60 /* Reason message. */
61 const char *reason_string;
63 /* Element number. */
64 Py_ssize_t number;
67 /* Implementation of record.method. */
69 static PyObject *
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. */
85 static PyObject *
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. */
101 static PyObject *
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] */
114 static PyObject *
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]. */
127 static PyObject *
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]. */
140 static PyObject *
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]. */
153 static PyObject *
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]. */
166 static PyObject *
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. */
179 PyObject *
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,
183 &recpy_insn_type);
185 if (obj == NULL)
186 return NULL;
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]. */
197 static PyObject *
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]. */
210 static PyObject *
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]. */
223 static PyObject *
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]. */
236 static PyObject *
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]. */
249 static PyObject *
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]. */
262 static PyObject *
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. */
275 PyObject *
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,
279 &recpy_func_type);
281 if (obj == NULL)
282 return NULL;
284 obj->thread = thread;
285 obj->method = method;
286 obj->number = number;
288 return (PyObject *) obj;
291 /* Implementation of RecordFunctionSegment.level [int]. */
293 static PyObject *
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]. */
306 static PyObject *
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]. */
319 static PyObject *
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]. */
332 static PyObject *
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]. */
345 static PyObject *
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]. */
358 static PyObject *
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]. */
372 static PyObject *
373 recpy_element_number (PyObject *self, void* closure)
375 const recpy_element_object * const obj = (recpy_element_object *) self;
377 return gdb_py_object_from_longest (obj->number).release ();
380 /* Implementation of RecordInstruction.__hash__ [int] and
381 RecordFunctionSegment.__hash__ [int]. */
383 static Py_hash_t
384 recpy_element_hash (PyObject *self)
386 const recpy_element_object * const obj = (recpy_element_object *) self;
388 return obj->number;
391 /* Implementation of operator == and != of RecordInstruction and
392 RecordFunctionSegment. */
394 static PyObject *
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;
406 switch (op)
408 case Py_EQ:
409 if (obj1->thread == obj2->thread
410 && obj1->method == obj2->method
411 && obj1->number == obj2->number)
412 Py_RETURN_TRUE;
413 else
414 Py_RETURN_FALSE;
416 case Py_NE:
417 if (obj1->thread != obj2->thread
418 || obj1->method != obj2->method
419 || obj1->number != obj2->number)
420 Py_RETURN_TRUE;
421 else
422 Py_RETURN_FALSE;
424 default:
425 break;
428 Py_INCREF (Py_NotImplemented);
429 return Py_NotImplemented;
432 /* Create a new gdb.RecordGap object. */
434 PyObject *
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,
438 &recpy_gap_type);
440 if (obj == NULL)
441 return NULL;
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]. */
452 static PyObject *
453 recpy_gap_number (PyObject *self, void *closure)
455 const recpy_gap_object * const obj = (const recpy_gap_object *) self;
457 return gdb_py_object_from_longest (obj->number).release ();
460 /* Implementation of RecordGap.error_code [int]. */
462 static PyObject *
463 recpy_gap_reason_code (PyObject *self, void *closure)
465 const recpy_gap_object * const obj = (const recpy_gap_object *) self;
467 return gdb_py_object_from_longest (obj->reason_code).release ();
470 /* Implementation of RecordGap.error_string [str]. */
472 static PyObject *
473 recpy_gap_reason_string (PyObject *self, void *closure)
475 const recpy_gap_object * const obj = (const recpy_gap_object *) self;
477 return PyUnicode_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."},
486 { NULL }
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.",
495 NULL },
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 },
505 { 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},
519 { 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},
533 { 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},
542 { 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_get_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)
588 return -1;
589 else
590 return 0;
593 /* Implementation of gdb.start_recording (method) -> gdb.Record. */
595 PyObject *
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))
603 return NULL;
607 record_start (method, format, 0);
608 ret = gdbpy_current_recording (self, args);
610 catch (const gdb_exception &except)
612 gdbpy_convert_exception (except);
615 return ret;
618 /* Implementation of gdb.current_recording (self) -> gdb.Record. */
620 PyObject *
621 gdbpy_current_recording (PyObject *self, PyObject *args)
623 recpy_record_object *ret = NULL;
625 if (find_record_target () == NULL)
626 Py_RETURN_NONE;
628 ret = PyObject_New (recpy_record_object, &recpy_record_type);
629 ret->thread = inferior_thread ();
630 ret->method = target_record_method (ret->thread->ptid);
632 return (PyObject *) ret;
635 /* Implementation of gdb.stop_recording (self) -> None. */
637 PyObject *
638 gdbpy_stop_recording (PyObject *self, PyObject *args)
642 record_stop (0);
644 catch (const gdb_exception &except)
646 GDB_PY_HANDLE_EXCEPTION (except);
649 Py_RETURN_NONE;