1 /* Python interface to register, and register group information.
3 Copyright (C) 2020-2024 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/>. */
21 #include "arch-utils.h"
22 #include "reggroups.h"
23 #include "python-internal.h"
24 #include "user-regs.h"
25 #include <unordered_map>
27 /* Per-gdbarch data type. */
28 typedef std::vector
<gdbpy_ref
<>> gdbpy_register_type
;
30 /* Token to access per-gdbarch data related to register descriptors. */
31 static const registry
<gdbarch
>::key
<gdbpy_register_type
>
32 gdbpy_register_object_data
;
34 /* Structure for iterator over register descriptors. */
35 struct register_descriptor_iterator_object
{
38 /* The register group that the user is iterating over. This will never
40 const struct reggroup
*reggroup
;
42 /* The next register number to lookup. Starts at 0 and counts up. */
45 /* Pointer back to the architecture we're finding registers for. */
46 struct gdbarch
*gdbarch
;
49 extern PyTypeObject register_descriptor_iterator_object_type
50 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_iterator_object");
52 /* A register descriptor. */
53 struct register_descriptor_object
{
56 /* The register this is a descriptor for. */
59 /* The architecture this is a register for. */
60 struct gdbarch
*gdbarch
;
63 extern PyTypeObject register_descriptor_object_type
64 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_object");
66 /* Structure for iterator over register groups. */
67 struct reggroup_iterator_object
{
70 /* The index into GROUPS for the next group to return. */
71 std::vector
<const reggroup
*>::size_type index
;
73 /* Pointer back to the architecture we're finding registers for. */
74 struct gdbarch
*gdbarch
;
77 extern PyTypeObject reggroup_iterator_object_type
78 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_iterator_object");
80 /* A register group object. */
81 struct reggroup_object
{
84 /* The register group being described. */
85 const struct reggroup
*reggroup
;
88 extern PyTypeObject reggroup_object_type
89 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_object");
91 /* Return a gdb.RegisterGroup object wrapping REGGROUP. The register
92 group objects are cached, and the same Python object will always be
93 returned for the same REGGROUP pointer. */
96 gdbpy_get_reggroup (const reggroup
*reggroup
)
98 /* Map from GDB's internal reggroup objects to the Python representation.
99 GDB's reggroups are global, and are never deleted, so using a map like
101 static std::unordered_map
<const struct reggroup
*,gdbpy_ref
<>>
102 gdbpy_reggroup_object_map
;
104 /* If there is not already a suitable Python object in the map then
105 create a new one, and add it to the map. */
106 if (gdbpy_reggroup_object_map
[reggroup
] == nullptr)
108 /* Create a new object and fill in its details. */
109 gdbpy_ref
<reggroup_object
> group
110 (PyObject_New (reggroup_object
, ®group_object_type
));
113 group
->reggroup
= reggroup
;
114 gdbpy_reggroup_object_map
[reggroup
]
115 = gdbpy_ref
<> ((PyObject
*) group
.release ());
118 /* Fetch the Python object wrapping REGGROUP from the map, increasing
119 the reference count is handled by the gdbpy_ref class. */
120 return gdbpy_reggroup_object_map
[reggroup
];
123 /* Convert a gdb.RegisterGroup to a string, it just returns the name of
124 the register group. */
127 gdbpy_reggroup_to_string (PyObject
*self
)
129 reggroup_object
*group
= (reggroup_object
*) self
;
130 const reggroup
*reggroup
= group
->reggroup
;
132 return PyUnicode_FromString (reggroup
->name ());
135 /* Implement gdb.RegisterGroup.name (self) -> String.
136 Return a string that is the name of this register group. */
139 gdbpy_reggroup_name (PyObject
*self
, void *closure
)
141 return gdbpy_reggroup_to_string (self
);
144 /* Return a gdb.RegisterDescriptor object for REGNUM from GDBARCH. For
145 each REGNUM (in GDBARCH) only one descriptor is ever created, which is
146 then cached on the GDBARCH. */
149 gdbpy_get_register_descriptor (struct gdbarch
*gdbarch
,
152 gdbpy_register_type
*vecp
= gdbpy_register_object_data
.get (gdbarch
);
154 vecp
= gdbpy_register_object_data
.emplace (gdbarch
);
155 gdbpy_register_type
&vec
= *vecp
;
157 /* Ensure that we have enough entries in the vector. */
158 if (vec
.size () <= regnum
)
159 vec
.resize ((regnum
+ 1), nullptr);
161 /* If we don't already have a descriptor for REGNUM in GDBARCH then
163 if (vec
[regnum
] == nullptr)
165 gdbpy_ref
<register_descriptor_object
> reg
166 (PyObject_New (register_descriptor_object
,
167 ®ister_descriptor_object_type
));
170 reg
->regnum
= regnum
;
171 reg
->gdbarch
= gdbarch
;
172 vec
[regnum
] = gdbpy_ref
<> ((PyObject
*) reg
.release ());
175 /* Grab the register descriptor from the vector, the reference count is
176 automatically incremented thanks to gdbpy_ref. */
180 /* Convert the register descriptor to a string. */
183 gdbpy_register_descriptor_to_string (PyObject
*self
)
185 register_descriptor_object
*reg
186 = (register_descriptor_object
*) self
;
187 struct gdbarch
*gdbarch
= reg
->gdbarch
;
188 int regnum
= reg
->regnum
;
190 const char *name
= gdbarch_register_name (gdbarch
, regnum
);
191 return PyUnicode_FromString (name
);
194 /* Implement gdb.RegisterDescriptor.name attribute get function. Return a
195 string that is the name of this register. Due to checking when register
196 descriptors are created the name will never by the empty string. */
199 gdbpy_register_descriptor_name (PyObject
*self
, void *closure
)
201 return gdbpy_register_descriptor_to_string (self
);
204 /* Return a reference to the gdb.RegisterGroupsIterator object. */
207 gdbpy_reggroup_iter (PyObject
*self
)
213 /* Return the next gdb.RegisterGroup object from the iterator. */
216 gdbpy_reggroup_iter_next (PyObject
*self
)
218 reggroup_iterator_object
*iter_obj
219 = (reggroup_iterator_object
*) self
;
221 const std::vector
<const reggroup
*> &groups
222 = gdbarch_reggroups (iter_obj
->gdbarch
);
223 if (iter_obj
->index
>= groups
.size ())
225 PyErr_SetString (PyExc_StopIteration
, _("No more groups"));
229 const reggroup
*group
= groups
[iter_obj
->index
];
231 return gdbpy_get_reggroup (group
).release ();
234 /* Return a new gdb.RegisterGroupsIterator over all the register groups in
238 gdbpy_new_reggroup_iterator (struct gdbarch
*gdbarch
)
240 gdb_assert (gdbarch
!= nullptr);
242 /* Create a new object and fill in its internal state. */
243 reggroup_iterator_object
*iter
244 = PyObject_New (reggroup_iterator_object
,
245 ®group_iterator_object_type
);
249 iter
->gdbarch
= gdbarch
;
250 return (PyObject
*) iter
;
253 /* Create and return a new gdb.RegisterDescriptorIterator object which
254 will iterate over all registers in GROUP_NAME for GDBARCH. If
255 GROUP_NAME is either NULL or the empty string then the ALL_REGGROUP is
256 used, otherwise lookup the register group matching GROUP_NAME and use
259 This function can return NULL if GROUP_NAME isn't found. */
262 gdbpy_new_register_descriptor_iterator (struct gdbarch
*gdbarch
,
263 const char *group_name
)
265 const reggroup
*grp
= NULL
;
267 /* Lookup the requested register group, or find the default. */
268 if (group_name
== NULL
|| *group_name
== '\0')
272 grp
= reggroup_find (gdbarch
, group_name
);
275 PyErr_SetString (PyExc_ValueError
,
276 _("Unknown register group name."));
280 /* Create a new iterator object initialised for this architecture and
281 fill in all of the details. */
282 register_descriptor_iterator_object
*iter
283 = PyObject_New (register_descriptor_iterator_object
,
284 ®ister_descriptor_iterator_object_type
);
288 iter
->gdbarch
= gdbarch
;
289 gdb_assert (grp
!= NULL
);
290 iter
->reggroup
= grp
;
292 return (PyObject
*) iter
;
295 /* Return a reference to the gdb.RegisterDescriptorIterator object. */
298 gdbpy_register_descriptor_iter (PyObject
*self
)
304 /* Return the next register name. */
307 gdbpy_register_descriptor_iter_next (PyObject
*self
)
309 register_descriptor_iterator_object
*iter_obj
310 = (register_descriptor_iterator_object
*) self
;
311 struct gdbarch
*gdbarch
= iter_obj
->gdbarch
;
315 if (iter_obj
->regnum
>= gdbarch_num_cooked_regs (gdbarch
))
317 PyErr_SetString (PyExc_StopIteration
, _("No more registers"));
321 const char *name
= nullptr;
322 int regnum
= iter_obj
->regnum
;
323 if (gdbarch_register_reggroup_p (gdbarch
, regnum
,
325 name
= gdbarch_register_name (gdbarch
, regnum
);
328 if (name
!= nullptr && *name
!= '\0')
329 return gdbpy_get_register_descriptor (gdbarch
, regnum
).release ();
336 gdb.RegisterDescriptorIterator.find (self, name) -> gdb.RegisterDescriptor
338 Look up a descriptor for register with NAME. If no matching register is
339 found then return None. */
342 register_descriptor_iter_find (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
344 static const char *keywords
[] = { "name", NULL
};
345 const char *register_name
= NULL
;
347 register_descriptor_iterator_object
*iter_obj
348 = (register_descriptor_iterator_object
*) self
;
349 struct gdbarch
*gdbarch
= iter_obj
->gdbarch
;
351 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "s", keywords
,
355 if (register_name
!= NULL
&& *register_name
!= '\0')
357 int regnum
= user_reg_map_name_to_regnum (gdbarch
, register_name
,
358 strlen (register_name
));
360 return gdbpy_get_register_descriptor (gdbarch
, regnum
).release ();
366 /* See python-internal.h. */
369 gdbpy_parse_register_id (struct gdbarch
*gdbarch
, PyObject
*pyo_reg_id
,
372 gdb_assert (pyo_reg_id
!= NULL
);
374 /* The register could be a string, its name. */
375 if (gdbpy_is_string (pyo_reg_id
))
377 gdb::unique_xmalloc_ptr
<char> reg_name (gdbpy_obj_to_string (pyo_reg_id
));
379 if (reg_name
!= NULL
)
381 *reg_num
= user_reg_map_name_to_regnum (gdbarch
, reg_name
.get (),
382 strlen (reg_name
.get ()));
385 PyErr_SetString (PyExc_ValueError
, "Bad register");
388 /* The register could be its internal GDB register number. */
389 else if (PyLong_Check (pyo_reg_id
))
392 if (gdb_py_int_as_long (pyo_reg_id
, &value
) == 0)
394 /* Nothing -- error. */
396 else if ((int) value
== value
397 && user_reg_map_regnum_to_name (gdbarch
, value
) != NULL
)
399 *reg_num
= (int) value
;
403 PyErr_SetString (PyExc_ValueError
, "Bad register");
405 /* The register could be a gdb.RegisterDescriptor object. */
406 else if (PyObject_IsInstance (pyo_reg_id
,
407 (PyObject
*) ®ister_descriptor_object_type
))
409 register_descriptor_object
*reg
410 = (register_descriptor_object
*) pyo_reg_id
;
411 if (reg
->gdbarch
== gdbarch
)
413 *reg_num
= reg
->regnum
;
417 PyErr_SetString (PyExc_ValueError
,
418 _("Invalid Architecture in RegisterDescriptor"));
421 PyErr_SetString (PyExc_TypeError
, _("Invalid type for register"));
423 gdb_assert (PyErr_Occurred ());
427 /* Initializes the new Python classes from this file in the gdb module. */
429 static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
430 gdbpy_initialize_registers ()
432 register_descriptor_object_type
.tp_new
= PyType_GenericNew
;
433 if (gdbpy_type_ready (®ister_descriptor_object_type
) < 0)
436 reggroup_iterator_object_type
.tp_new
= PyType_GenericNew
;
437 if (gdbpy_type_ready (®group_iterator_object_type
) < 0)
440 reggroup_object_type
.tp_new
= PyType_GenericNew
;
441 if (gdbpy_type_ready (®group_object_type
) < 0)
444 register_descriptor_iterator_object_type
.tp_new
= PyType_GenericNew
;
445 if (gdbpy_type_ready (®ister_descriptor_iterator_object_type
) < 0)
451 GDBPY_INITIALIZE_FILE (gdbpy_initialize_registers
);
455 static PyMethodDef register_descriptor_iterator_object_methods
[] = {
456 { "find", (PyCFunction
) register_descriptor_iter_find
,
457 METH_VARARGS
| METH_KEYWORDS
,
458 "registers (name) -> gdb.RegisterDescriptor.\n\
459 Return a register descriptor for the register NAME, or None if no register\n\
460 with that name exists in this iterator." },
461 {NULL
} /* Sentinel */
464 PyTypeObject register_descriptor_iterator_object_type
= {
465 PyVarObject_HEAD_INIT (NULL
, 0)
466 "gdb.RegisterDescriptorIterator", /*tp_name*/
467 sizeof (register_descriptor_iterator_object
), /*tp_basicsize*/
476 0, /*tp_as_sequence*/
484 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
485 "GDB architecture register descriptor iterator object", /*tp_doc */
488 0, /*tp_richcompare */
489 0, /*tp_weaklistoffset */
490 gdbpy_register_descriptor_iter
, /*tp_iter */
491 gdbpy_register_descriptor_iter_next
, /*tp_iternext */
492 register_descriptor_iterator_object_methods
/*tp_methods */
495 static gdb_PyGetSetDef gdbpy_register_descriptor_getset
[] = {
496 { "name", gdbpy_register_descriptor_name
, NULL
,
497 "The name of this register.", NULL
},
498 { NULL
} /* Sentinel */
501 PyTypeObject register_descriptor_object_type
= {
502 PyVarObject_HEAD_INIT (NULL
, 0)
503 "gdb.RegisterDescriptor", /*tp_name*/
504 sizeof (register_descriptor_object
), /*tp_basicsize*/
513 0, /*tp_as_sequence*/
517 gdbpy_register_descriptor_to_string
, /*tp_str*/
521 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
522 "GDB architecture register descriptor object", /*tp_doc */
525 0, /*tp_richcompare */
526 0, /*tp_weaklistoffset */
531 gdbpy_register_descriptor_getset
/*tp_getset */
534 PyTypeObject reggroup_iterator_object_type
= {
535 PyVarObject_HEAD_INIT (NULL
, 0)
536 "gdb.RegisterGroupsIterator", /*tp_name*/
537 sizeof (reggroup_iterator_object
), /*tp_basicsize*/
546 0, /*tp_as_sequence*/
554 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
555 "GDB register groups iterator object", /*tp_doc */
558 0, /*tp_richcompare */
559 0, /*tp_weaklistoffset */
560 gdbpy_reggroup_iter
, /*tp_iter */
561 gdbpy_reggroup_iter_next
, /*tp_iternext */
565 static gdb_PyGetSetDef gdbpy_reggroup_getset
[] = {
566 { "name", gdbpy_reggroup_name
, NULL
,
567 "The name of this register group.", NULL
},
568 { NULL
} /* Sentinel */
571 PyTypeObject reggroup_object_type
= {
572 PyVarObject_HEAD_INIT (NULL
, 0)
573 "gdb.RegisterGroup", /*tp_name*/
574 sizeof (reggroup_object
), /*tp_basicsize*/
583 0, /*tp_as_sequence*/
587 gdbpy_reggroup_to_string
, /*tp_str*/
591 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
592 "GDB register group object", /*tp_doc */
595 0, /*tp_richcompare */
596 0, /*tp_weaklistoffset */
601 gdbpy_reggroup_getset
/*tp_getset */