1 /* Python interface to register, and register group information.
3 Copyright (C) 2020 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/>. */
22 #include "arch-utils.h"
24 #include "reggroups.h"
25 #include "python-internal.h"
27 /* Structure for iterator over register descriptors. */
31 /* The register group that the user is iterating over. This will never
33 struct reggroup
*reggroup
;
35 /* The next register number to lookup. Starts at 0 and counts up. */
38 /* Pointer back to the architecture we're finding registers for. */
39 struct gdbarch
*gdbarch
;
40 } register_descriptor_iterator_object
;
42 extern PyTypeObject register_descriptor_iterator_object_type
43 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_iterator_object");
45 /* A register descriptor. */
49 /* The register this is a descriptor for. */
52 /* The architecture this is a register for. */
53 struct gdbarch
*gdbarch
;
54 } register_descriptor_object
;
56 extern PyTypeObject register_descriptor_object_type
57 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_object");
59 /* Create an return a new gdb.RegisterDescriptor object. */
61 gdbpy_new_register_descriptor (struct gdbarch
*gdbarch
,
64 /* Create a new object and fill in its details. */
65 register_descriptor_object
*reg
66 = PyObject_New (register_descriptor_object
,
67 ®ister_descriptor_object_type
);
71 reg
->gdbarch
= gdbarch
;
72 return (PyObject
*) reg
;
75 /* Convert the register descriptor to a string. */
78 gdbpy_register_descriptor_to_string (PyObject
*self
)
80 register_descriptor_object
*reg
81 = (register_descriptor_object
*) self
;
82 struct gdbarch
*gdbarch
= reg
->gdbarch
;
83 int regnum
= reg
->regnum
;
85 const char *name
= gdbarch_register_name (gdbarch
, regnum
);
86 return PyString_FromString (name
);
89 /* Implement gdb.RegisterDescriptor.name attribute get function. Return a
90 string that is the name of this register. Due to checking when register
91 descriptors are created the name will never by the empty string. */
94 gdbpy_register_descriptor_name (PyObject
*self
, void *closure
)
96 return gdbpy_register_descriptor_to_string (self
);
99 /* Create and return a new gdb.RegisterDescriptorIterator object which
100 will iterate over all registers in GROUP_NAME for GDBARCH. If
101 GROUP_NAME is either NULL or the empty string then the ALL_REGGROUP is
102 used, otherwise lookup the register group matching GROUP_NAME and use
105 This function can return NULL if GROUP_NAME isn't found. */
108 gdbpy_new_register_descriptor_iterator (struct gdbarch
*gdbarch
,
109 const char *group_name
)
111 struct reggroup
*grp
= NULL
;
113 /* Lookup the requested register group, or find the default. */
114 if (group_name
== NULL
|| *group_name
== '\0')
118 grp
= reggroup_find (gdbarch
, group_name
);
121 PyErr_SetString (PyExc_ValueError
,
122 _("Unknown register group name."));
126 /* Create a new iterator object initialised for this architecture and
127 fill in all of the details. */
128 register_descriptor_iterator_object
*iter
129 = PyObject_New (register_descriptor_iterator_object
,
130 ®ister_descriptor_iterator_object_type
);
134 iter
->gdbarch
= gdbarch
;
135 gdb_assert (grp
!= NULL
);
136 iter
->reggroup
= grp
;
138 return (PyObject
*) iter
;
141 /* Return a reference to the gdb.RegisterDescriptorIterator object. */
144 gdbpy_register_descriptor_iter (PyObject
*self
)
150 /* Return the next register name. */
153 gdbpy_register_descriptor_iter_next (PyObject
*self
)
155 register_descriptor_iterator_object
*iter_obj
156 = (register_descriptor_iterator_object
*) self
;
157 struct gdbarch
*gdbarch
= iter_obj
->gdbarch
;
161 if (iter_obj
->regnum
>= gdbarch_num_cooked_regs (gdbarch
))
163 PyErr_SetString (PyExc_StopIteration
, _("No more registers"));
167 const char *name
= nullptr;
168 int regnum
= iter_obj
->regnum
;
169 if (gdbarch_register_reggroup_p (gdbarch
, regnum
,
171 name
= gdbarch_register_name (gdbarch
, regnum
);
174 if (name
!= nullptr && *name
!= '\0')
175 return gdbpy_new_register_descriptor (gdbarch
, regnum
);
180 /* Initializes the new Python classes from this file in the gdb module. */
183 gdbpy_initialize_registers ()
185 register_descriptor_object_type
.tp_new
= PyType_GenericNew
;
186 if (PyType_Ready (®ister_descriptor_object_type
) < 0)
188 if (gdb_pymodule_addobject
189 (gdb_module
, "RegisterDescriptor",
190 (PyObject
*) ®ister_descriptor_object_type
) < 0)
193 register_descriptor_iterator_object_type
.tp_new
= PyType_GenericNew
;
194 if (PyType_Ready (®ister_descriptor_iterator_object_type
) < 0)
196 return (gdb_pymodule_addobject
197 (gdb_module
, "RegisterDescriptorIterator",
198 (PyObject
*) ®ister_descriptor_iterator_object_type
));
201 PyTypeObject register_descriptor_iterator_object_type
= {
202 PyVarObject_HEAD_INIT (NULL
, 0)
203 "gdb.RegisterDescriptorIterator", /*tp_name*/
204 sizeof (register_descriptor_iterator_object
), /*tp_basicsize*/
213 0, /*tp_as_sequence*/
221 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_ITER
, /*tp_flags*/
222 "GDB architecture register descriptor iterator object", /*tp_doc */
225 0, /*tp_richcompare */
226 0, /*tp_weaklistoffset */
227 gdbpy_register_descriptor_iter
, /*tp_iter */
228 gdbpy_register_descriptor_iter_next
, /*tp_iternext */
232 static gdb_PyGetSetDef gdbpy_register_descriptor_getset
[] = {
233 { "name", gdbpy_register_descriptor_name
, NULL
,
234 "The name of this register.", NULL
},
235 { NULL
} /* Sentinel */
238 PyTypeObject register_descriptor_object_type
= {
239 PyVarObject_HEAD_INIT (NULL
, 0)
240 "gdb.RegisterDescriptor", /*tp_name*/
241 sizeof (register_descriptor_object
), /*tp_basicsize*/
250 0, /*tp_as_sequence*/
254 gdbpy_register_descriptor_to_string
, /*tp_str*/
258 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
259 "GDB architecture register descriptor object", /*tp_doc */
262 0, /*tp_richcompare */
263 0, /*tp_weaklistoffset */
268 gdbpy_register_descriptor_getset
/*tp_getset */