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