1 /* Python interface to instruction objects. 2 3 Copyright 2017-2023 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 "py-instruction.h" 22 23 /* Python type object for the abstract gdb.Instruction class. This class 24 contains getters for four elements: "pc" (int), "data" (buffer), "decode" 25 (str) and "size" (int) that must be overridden by sub classes. */ 26 27 PyTypeObject py_insn_type = { 28 PyVarObject_HEAD_INIT (NULL, 0) 29 }; 30 31 /* Python instruction object. */ 32 33 struct py_insn_obj { 34 PyObject_HEAD 35 }; 36 37 /* Getter function for gdb.Instruction attributes. */ 38 39 static PyObject * 40 py_insn_getter (PyObject *self, void *closure) 41 { 42 return PyErr_Format (PyExc_NotImplementedError, _("Not implemented.")); 43 } 44 45 /* Instruction members. */ 46 47 static gdb_PyGetSetDef py_insn_getset[] = 48 { 49 { "pc", py_insn_getter, NULL, "instruction address", NULL}, 50 { "data", py_insn_getter, NULL, "instruction memory", NULL}, 51 { "decoded", py_insn_getter, NULL, "decoded instruction", NULL}, 52 { "size", py_insn_getter, NULL, "instruction size in bytes", NULL}, 53 {NULL} 54 }; 55 56 /* See py-instruction.h. */ 57 58 PyTypeObject * 59 py_insn_get_insn_type () 60 { 61 if (py_insn_type.tp_new == nullptr) 62 { 63 py_insn_type.tp_new = PyType_GenericNew; 64 py_insn_type.tp_flags = Py_TPFLAGS_DEFAULT; 65 py_insn_type.tp_basicsize = sizeof (py_insn_obj); 66 py_insn_type.tp_name = "gdb.Instruction"; 67 py_insn_type.tp_doc = "GDB instruction object"; 68 py_insn_type.tp_getset = py_insn_getset; 69 70 if (PyType_Ready (&py_insn_type) < 0) 71 { 72 /* Reset the tp_new field so any subsequent calls to this 73 function will retry to make the type ready. */ 74 py_insn_type.tp_new = nullptr; 75 return nullptr; 76 } 77 } 78 79 return &py_insn_type; 80 } 81 82 /* Sets up the gdb.Instruction type. */ 83 84 int 85 gdbpy_initialize_instruction (void) 86 { 87 if (py_insn_get_insn_type () == nullptr) 88 return -1; 89 return 0; 90 } 91