1 /* Python interface to register, and register group information.
3 Copyright (C) 2020-2022 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"
26 #include "user-regs.h"
27 #include <unordered_map>
29 /* Token to access per-gdbarch data related to register descriptors. */
30 static struct gdbarch_data *gdbpy_register_object_data = NULL;
32 /* Structure for iterator over register descriptors. */
33 struct register_descriptor_iterator_object {
36 /* The register group that the user is iterating over. This will never
38 struct reggroup *reggroup;
40 /* The next register number to lookup. Starts at 0 and counts up. */
43 /* Pointer back to the architecture we're finding registers for. */
44 struct gdbarch *gdbarch;
47 extern PyTypeObject register_descriptor_iterator_object_type
48 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_iterator_object");
50 /* A register descriptor. */
51 struct register_descriptor_object {
54 /* The register this is a descriptor for. */
57 /* The architecture this is a register for. */
58 struct gdbarch *gdbarch;
61 extern PyTypeObject register_descriptor_object_type
62 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_object");
64 /* Structure for iterator over register groups. */
65 struct reggroup_iterator_object {
68 /* The last register group returned. Initially this will be NULL. */
69 struct reggroup *reggroup;
71 /* Pointer back to the architecture we're finding registers for. */
72 struct gdbarch *gdbarch;
75 extern PyTypeObject reggroup_iterator_object_type
76 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_iterator_object");
78 /* A register group object. */
79 struct reggroup_object {
82 /* The register group being described. */
83 struct reggroup *reggroup;
86 extern PyTypeObject reggroup_object_type
87 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_object");
89 /* Associates a vector of gdb.RegisterDescriptor objects with GDBARCH as
90 gdbarch_data via the gdbarch post init registration mechanism
91 (gdbarch_data_register_post_init). */
94 gdbpy_register_object_data_init (struct gdbarch *gdbarch)
96 return new std::vector<gdbpy_ref<>>;
99 /* Return a gdb.RegisterGroup object wrapping REGGROUP. The register
100 group objects are cached, and the same Python object will always be
101 returned for the same REGGROUP pointer. */
104 gdbpy_get_reggroup (struct reggroup *reggroup)
106 /* Map from GDB's internal reggroup objects to the Python representation.
107 GDB's reggroups are global, and are never deleted, so using a map like
109 static std::unordered_map<struct reggroup *,gdbpy_ref<>>
110 gdbpy_reggroup_object_map;
112 /* If there is not already a suitable Python object in the map then
113 create a new one, and add it to the map. */
114 if (gdbpy_reggroup_object_map[reggroup] == nullptr)
116 /* Create a new object and fill in its details. */
117 gdbpy_ref<reggroup_object> group
118 (PyObject_New (reggroup_object, ®group_object_type));
121 group->reggroup = reggroup;
122 gdbpy_reggroup_object_map[reggroup]
123 = gdbpy_ref<> ((PyObject *) group.release ());
126 /* Fetch the Python object wrapping REGGROUP from the map, increasing
127 the reference count is handled by the gdbpy_ref class. */
128 return gdbpy_reggroup_object_map[reggroup];
131 /* Convert a gdb.RegisterGroup to a string, it just returns the name of
132 the register group. */
135 gdbpy_reggroup_to_string (PyObject *self)
137 reggroup_object *group = (reggroup_object *) self;
138 struct reggroup *reggroup = group->reggroup;
140 const char *name = reggroup_name (reggroup);
141 return PyString_FromString (name);
144 /* Implement gdb.RegisterGroup.name (self) -> String.
145 Return a string that is the name of this register group. */
148 gdbpy_reggroup_name (PyObject *self, void *closure)
150 return gdbpy_reggroup_to_string (self);
153 /* Return a gdb.RegisterDescriptor object for REGNUM from GDBARCH. For
154 each REGNUM (in GDBARCH) only one descriptor is ever created, which is
155 then cached on the GDBARCH. */
158 gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
162 = *(std::vector<gdbpy_ref<>> *) gdbarch_data (gdbarch,
163 gdbpy_register_object_data);
165 /* Ensure that we have enough entries in the vector. */
166 if (vec.size () <= regnum)
167 vec.resize ((regnum + 1), nullptr);
169 /* If we don't already have a descriptor for REGNUM in GDBARCH then
171 if (vec[regnum] == nullptr)
173 gdbpy_ref <register_descriptor_object> reg
174 (PyObject_New (register_descriptor_object,
175 ®ister_descriptor_object_type));
178 reg->regnum = regnum;
179 reg->gdbarch = gdbarch;
180 vec[regnum] = gdbpy_ref<> ((PyObject *) reg.release ());
183 /* Grab the register descriptor from the vector, the reference count is
184 automatically incremented thanks to gdbpy_ref. */
188 /* Convert the register descriptor to a string. */
191 gdbpy_register_descriptor_to_string (PyObject *self)
193 register_descriptor_object *reg
194 = (register_descriptor_object *) self;
195 struct gdbarch *gdbarch = reg->gdbarch;
196 int regnum = reg->regnum;
198 const char *name = gdbarch_register_name (gdbarch, regnum);
199 return PyString_FromString (name);
202 /* Implement gdb.RegisterDescriptor.name attribute get function. Return a
203 string that is the name of this register. Due to checking when register
204 descriptors are created the name will never by the empty string. */
207 gdbpy_register_descriptor_name (PyObject *self, void *closure)
209 return gdbpy_register_descriptor_to_string (self);
212 /* Return a reference to the gdb.RegisterGroupsIterator object. */
215 gdbpy_reggroup_iter (PyObject *self)
221 /* Return the next gdb.RegisterGroup object from the iterator. */
224 gdbpy_reggroup_iter_next (PyObject *self)
226 reggroup_iterator_object *iter_obj
227 = (reggroup_iterator_object *) self;
228 struct gdbarch *gdbarch = iter_obj->gdbarch;
230 struct reggroup *next_group = reggroup_next (gdbarch, iter_obj->reggroup);
231 if (next_group == NULL)
233 PyErr_SetString (PyExc_StopIteration, _("No more groups"));
237 iter_obj->reggroup = next_group;
238 return gdbpy_get_reggroup (iter_obj->reggroup).release ();
241 /* Return a new gdb.RegisterGroupsIterator over all the register groups in
245 gdbpy_new_reggroup_iterator (struct gdbarch *gdbarch)
247 gdb_assert (gdbarch != nullptr);
249 /* Create a new object and fill in its internal state. */
250 reggroup_iterator_object *iter
251 = PyObject_New (reggroup_iterator_object,
252 ®group_iterator_object_type);
255 iter->reggroup = NULL;
256 iter->gdbarch = gdbarch;
257 return (PyObject *) iter;
260 /* Create and return a new gdb.RegisterDescriptorIterator object which
261 will iterate over all registers in GROUP_NAME for GDBARCH. If
262 GROUP_NAME is either NULL or the empty string then the ALL_REGGROUP is
263 used, otherwise lookup the register group matching GROUP_NAME and use
266 This function can return NULL if GROUP_NAME isn't found. */
269 gdbpy_new_register_descriptor_iterator (struct gdbarch *gdbarch,
270 const char *group_name)
272 struct reggroup *grp = NULL;
274 /* Lookup the requested register group, or find the default. */
275 if (group_name == NULL || *group_name == '\0')
279 grp = reggroup_find (gdbarch, group_name);
282 PyErr_SetString (PyExc_ValueError,
283 _("Unknown register group name."));
287 /* Create a new iterator object initialised for this architecture and
288 fill in all of the details. */
289 register_descriptor_iterator_object *iter
290 = PyObject_New (register_descriptor_iterator_object,
291 ®ister_descriptor_iterator_object_type);
295 iter->gdbarch = gdbarch;
296 gdb_assert (grp != NULL);
297 iter->reggroup = grp;
299 return (PyObject *) iter;
302 /* Return a reference to the gdb.RegisterDescriptorIterator object. */
305 gdbpy_register_descriptor_iter (PyObject *self)
311 /* Return the next register name. */
314 gdbpy_register_descriptor_iter_next (PyObject *self)
316 register_descriptor_iterator_object *iter_obj
317 = (register_descriptor_iterator_object *) self;
318 struct gdbarch *gdbarch = iter_obj->gdbarch;
322 if (iter_obj->regnum >= gdbarch_num_cooked_regs (gdbarch))
324 PyErr_SetString (PyExc_StopIteration, _("No more registers"));
328 const char *name = nullptr;
329 int regnum = iter_obj->regnum;
330 if (gdbarch_register_reggroup_p (gdbarch, regnum,
332 name = gdbarch_register_name (gdbarch, regnum);
335 if (name != nullptr && *name != '\0')
336 return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
343 gdb.RegisterDescriptorIterator.find (self, name) -> gdb.RegisterDescriptor
345 Look up a descriptor for register with NAME. If no matching register is
346 found then return None. */
349 register_descriptor_iter_find (PyObject *self, PyObject *args, PyObject *kw)
351 static const char *keywords[] = { "name", NULL };
352 const char *register_name = NULL;
354 register_descriptor_iterator_object *iter_obj
355 = (register_descriptor_iterator_object *) self;
356 struct gdbarch *gdbarch = iter_obj->gdbarch;
358 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords,
362 if (register_name != NULL && *register_name != '\0')
364 int regnum = user_reg_map_name_to_regnum (gdbarch, register_name,
365 strlen (register_name));
367 return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
373 /* See python-internal.h. */
376 gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id,
379 gdb_assert (pyo_reg_id != NULL);
381 /* The register could be a string, its name. */
382 if (gdbpy_is_string (pyo_reg_id))
384 gdb::unique_xmalloc_ptr<char> reg_name (gdbpy_obj_to_string (pyo_reg_id));
386 if (reg_name != NULL)
388 *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (),
389 strlen (reg_name.get ()));
390 return *reg_num >= 0;
393 /* The register could be its internal GDB register number. */
394 else if (PyInt_Check (pyo_reg_id))
397 if (gdb_py_int_as_long (pyo_reg_id, &value) && (int) value == value)
399 if (user_reg_map_regnum_to_name (gdbarch, value) != NULL)
401 *reg_num = (int) value;
406 /* The register could be a gdb.RegisterDescriptor object. */
407 else if (PyObject_IsInstance (pyo_reg_id,
408 (PyObject *) ®ister_descriptor_object_type))
410 register_descriptor_object *reg
411 = (register_descriptor_object *) pyo_reg_id;
412 if (reg->gdbarch == gdbarch)
414 *reg_num = reg->regnum;
418 PyErr_SetString (PyExc_ValueError,
419 _("Invalid Architecture in RegisterDescriptor"));
422 gdb_assert (PyErr_Occurred ());
426 void _initialize_py_registers ();
428 _initialize_py_registers ()
430 gdbpy_register_object_data
431 = gdbarch_data_register_post_init (gdbpy_register_object_data_init);
434 /* Initializes the new Python classes from this file in the gdb module. */
437 gdbpy_initialize_registers ()
439 register_descriptor_object_type.tp_new = PyType_GenericNew;
440 if (PyType_Ready (®ister_descriptor_object_type) < 0)
442 if (gdb_pymodule_addobject
443 (gdb_module, "RegisterDescriptor",
444 (PyObject *) ®ister_descriptor_object_type) < 0)
447 reggroup_iterator_object_type.tp_new = PyType_GenericNew;
448 if (PyType_Ready (®group_iterator_object_type) < 0)
450 if (gdb_pymodule_addobject
451 (gdb_module, "RegisterGroupsIterator",
452 (PyObject *) ®group_iterator_object_type) < 0)
455 reggroup_object_type.tp_new = PyType_GenericNew;
456 if (PyType_Ready (®group_object_type) < 0)
458 if (gdb_pymodule_addobject
459 (gdb_module, "RegisterGroup",
460 (PyObject *) ®group_object_type) < 0)
463 register_descriptor_iterator_object_type.tp_new = PyType_GenericNew;
464 if (PyType_Ready (®ister_descriptor_iterator_object_type) < 0)
466 return (gdb_pymodule_addobject
467 (gdb_module, "RegisterDescriptorIterator",
468 (PyObject *) ®ister_descriptor_iterator_object_type));
471 static PyMethodDef register_descriptor_iterator_object_methods [] = {
472 { "find", (PyCFunction) register_descriptor_iter_find,
473 METH_VARARGS | METH_KEYWORDS,
474 "registers (name) -> gdb.RegisterDescriptor.\n\
475 Return a register descriptor for the register NAME, or None if no register\n\
476 with that name exists in this iterator." },
477 {NULL} /* Sentinel */
480 PyTypeObject register_descriptor_iterator_object_type = {
481 PyVarObject_HEAD_INIT (NULL, 0)
482 "gdb.RegisterDescriptorIterator", /*tp_name*/
483 sizeof (register_descriptor_iterator_object), /*tp_basicsize*/
492 0, /*tp_as_sequence*/
500 Py_TPFLAGS_DEFAULT, /*tp_flags*/
501 "GDB architecture register descriptor iterator object", /*tp_doc */
504 0, /*tp_richcompare */
505 0, /*tp_weaklistoffset */
506 gdbpy_register_descriptor_iter, /*tp_iter */
507 gdbpy_register_descriptor_iter_next, /*tp_iternext */
508 register_descriptor_iterator_object_methods /*tp_methods */
511 static gdb_PyGetSetDef gdbpy_register_descriptor_getset[] = {
512 { "name", gdbpy_register_descriptor_name, NULL,
513 "The name of this register.", NULL },
514 { NULL } /* Sentinel */
517 PyTypeObject register_descriptor_object_type = {
518 PyVarObject_HEAD_INIT (NULL, 0)
519 "gdb.RegisterDescriptor", /*tp_name*/
520 sizeof (register_descriptor_object), /*tp_basicsize*/
529 0, /*tp_as_sequence*/
533 gdbpy_register_descriptor_to_string, /*tp_str*/
537 Py_TPFLAGS_DEFAULT, /*tp_flags*/
538 "GDB architecture register descriptor object", /*tp_doc */
541 0, /*tp_richcompare */
542 0, /*tp_weaklistoffset */
547 gdbpy_register_descriptor_getset /*tp_getset */
550 PyTypeObject reggroup_iterator_object_type = {
551 PyVarObject_HEAD_INIT (NULL, 0)
552 "gdb.RegisterGroupsIterator", /*tp_name*/
553 sizeof (reggroup_iterator_object), /*tp_basicsize*/
562 0, /*tp_as_sequence*/
570 Py_TPFLAGS_DEFAULT, /*tp_flags*/
571 "GDB register groups iterator object", /*tp_doc */
574 0, /*tp_richcompare */
575 0, /*tp_weaklistoffset */
576 gdbpy_reggroup_iter, /*tp_iter */
577 gdbpy_reggroup_iter_next, /*tp_iternext */
581 static gdb_PyGetSetDef gdbpy_reggroup_getset[] = {
582 { "name", gdbpy_reggroup_name, NULL,
583 "The name of this register group.", NULL },
584 { NULL } /* Sentinel */
587 PyTypeObject reggroup_object_type = {
588 PyVarObject_HEAD_INIT (NULL, 0)
589 "gdb.RegisterGroup", /*tp_name*/
590 sizeof (reggroup_object), /*tp_basicsize*/
599 0, /*tp_as_sequence*/
603 gdbpy_reggroup_to_string, /*tp_str*/
607 Py_TPFLAGS_DEFAULT, /*tp_flags*/
608 "GDB register group object", /*tp_doc */
611 0, /*tp_richcompare */
612 0, /*tp_weaklistoffset */
617 gdbpy_reggroup_getset /*tp_getset */