arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / python / py-registers.c
blob229dd62a826aac2beb2bf65770e6bd0c1ba73a87
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/>. */
20 #include "gdbarch.h"
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 {
36 PyObject_HEAD
38 /* The register group that the user is iterating over. This will never
39 be NULL. */
40 const struct reggroup *reggroup;
42 /* The next register number to lookup. Starts at 0 and counts up. */
43 int regnum;
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 {
54 PyObject_HEAD
56 /* The register this is a descriptor for. */
57 int regnum;
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 {
68 PyObject_HEAD
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 {
82 PyObject_HEAD
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. */
95 static gdbpy_ref<>
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
100 this is safe. */
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, &reggroup_object_type));
111 if (group == NULL)
112 return NULL;
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. */
126 static PyObject *
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. */
138 static PyObject *
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. */
148 static gdbpy_ref<>
149 gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
150 int regnum)
152 gdbpy_register_type *vecp = gdbpy_register_object_data.get (gdbarch);
153 if (vecp == nullptr)
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
162 create one now. */
163 if (vec[regnum] == nullptr)
165 gdbpy_ref <register_descriptor_object> reg
166 (PyObject_New (register_descriptor_object,
167 &register_descriptor_object_type));
168 if (reg == NULL)
169 return NULL;
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. */
177 return vec[regnum];
180 /* Convert the register descriptor to a string. */
182 static PyObject *
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. */
198 static PyObject *
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. */
206 static PyObject *
207 gdbpy_reggroup_iter (PyObject *self)
209 Py_INCREF (self);
210 return self;
213 /* Return the next gdb.RegisterGroup object from the iterator. */
215 static PyObject *
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"));
226 return NULL;
229 const reggroup *group = groups[iter_obj->index];
230 iter_obj->index++;
231 return gdbpy_get_reggroup (group).release ();
234 /* Return a new gdb.RegisterGroupsIterator over all the register groups in
235 GDBARCH. */
237 PyObject *
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 &reggroup_iterator_object_type);
246 if (iter == NULL)
247 return NULL;
248 iter->index = 0;
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
257 that.
259 This function can return NULL if GROUP_NAME isn't found. */
261 PyObject *
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')
269 grp = all_reggroup;
270 else
272 grp = reggroup_find (gdbarch, group_name);
273 if (grp == NULL)
275 PyErr_SetString (PyExc_ValueError,
276 _("Unknown register group name."));
277 return NULL;
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 &register_descriptor_iterator_object_type);
285 if (iter == NULL)
286 return NULL;
287 iter->regnum = 0;
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. */
297 static PyObject *
298 gdbpy_register_descriptor_iter (PyObject *self)
300 Py_INCREF (self);
301 return self;
304 /* Return the next register name. */
306 static PyObject *
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"));
318 return NULL;
321 const char *name = nullptr;
322 int regnum = iter_obj->regnum;
323 if (gdbarch_register_reggroup_p (gdbarch, regnum,
324 iter_obj->reggroup))
325 name = gdbarch_register_name (gdbarch, regnum);
326 iter_obj->regnum++;
328 if (name != nullptr && *name != '\0')
329 return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
331 while (true);
334 /* Implement:
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. */
341 static PyObject *
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,
352 &register_name))
353 return NULL;
355 if (register_name != NULL && *register_name != '\0')
357 int regnum = user_reg_map_name_to_regnum (gdbarch, register_name,
358 strlen (register_name));
359 if (regnum >= 0)
360 return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
363 Py_RETURN_NONE;
366 /* See python-internal.h. */
368 bool
369 gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id,
370 int *reg_num)
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 ()));
383 if (*reg_num >= 0)
384 return true;
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))
391 long value;
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;
400 return true;
402 else
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 *) &register_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;
414 return true;
416 else
417 PyErr_SetString (PyExc_ValueError,
418 _("Invalid Architecture in RegisterDescriptor"));
420 else
421 PyErr_SetString (PyExc_TypeError, _("Invalid type for register"));
423 gdb_assert (PyErr_Occurred ());
424 return false;
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 (&register_descriptor_object_type) < 0)
434 return -1;
436 reggroup_iterator_object_type.tp_new = PyType_GenericNew;
437 if (gdbpy_type_ready (&reggroup_iterator_object_type) < 0)
438 return -1;
440 reggroup_object_type.tp_new = PyType_GenericNew;
441 if (gdbpy_type_ready (&reggroup_object_type) < 0)
442 return -1;
444 register_descriptor_iterator_object_type.tp_new = PyType_GenericNew;
445 if (gdbpy_type_ready (&register_descriptor_iterator_object_type) < 0)
446 return -1;
448 return 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*/
468 0, /*tp_itemsize*/
469 0, /*tp_dealloc*/
470 0, /*tp_print*/
471 0, /*tp_getattr*/
472 0, /*tp_setattr*/
473 0, /*tp_compare*/
474 0, /*tp_repr*/
475 0, /*tp_as_number*/
476 0, /*tp_as_sequence*/
477 0, /*tp_as_mapping*/
478 0, /*tp_hash */
479 0, /*tp_call*/
480 0, /*tp_str*/
481 0, /*tp_getattro*/
482 0, /*tp_setattro*/
483 0, /*tp_as_buffer*/
484 Py_TPFLAGS_DEFAULT, /*tp_flags*/
485 "GDB architecture register descriptor iterator object", /*tp_doc */
486 0, /*tp_traverse */
487 0, /*tp_clear */
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*/
505 0, /*tp_itemsize*/
506 0, /*tp_dealloc*/
507 0, /*tp_print*/
508 0, /*tp_getattr*/
509 0, /*tp_setattr*/
510 0, /*tp_compare*/
511 0, /*tp_repr*/
512 0, /*tp_as_number*/
513 0, /*tp_as_sequence*/
514 0, /*tp_as_mapping*/
515 0, /*tp_hash */
516 0, /*tp_call*/
517 gdbpy_register_descriptor_to_string, /*tp_str*/
518 0, /*tp_getattro*/
519 0, /*tp_setattro*/
520 0, /*tp_as_buffer*/
521 Py_TPFLAGS_DEFAULT, /*tp_flags*/
522 "GDB architecture register descriptor object", /*tp_doc */
523 0, /*tp_traverse */
524 0, /*tp_clear */
525 0, /*tp_richcompare */
526 0, /*tp_weaklistoffset */
527 0, /*tp_iter */
528 0, /*tp_iternext */
529 0, /*tp_methods */
530 0, /*tp_members */
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*/
538 0, /*tp_itemsize*/
539 0, /*tp_dealloc*/
540 0, /*tp_print*/
541 0, /*tp_getattr*/
542 0, /*tp_setattr*/
543 0, /*tp_compare*/
544 0, /*tp_repr*/
545 0, /*tp_as_number*/
546 0, /*tp_as_sequence*/
547 0, /*tp_as_mapping*/
548 0, /*tp_hash */
549 0, /*tp_call*/
550 0, /*tp_str*/
551 0, /*tp_getattro*/
552 0, /*tp_setattro*/
553 0, /*tp_as_buffer*/
554 Py_TPFLAGS_DEFAULT, /*tp_flags*/
555 "GDB register groups iterator object", /*tp_doc */
556 0, /*tp_traverse */
557 0, /*tp_clear */
558 0, /*tp_richcompare */
559 0, /*tp_weaklistoffset */
560 gdbpy_reggroup_iter, /*tp_iter */
561 gdbpy_reggroup_iter_next, /*tp_iternext */
562 0 /*tp_methods */
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*/
575 0, /*tp_itemsize*/
576 0, /*tp_dealloc*/
577 0, /*tp_print*/
578 0, /*tp_getattr*/
579 0, /*tp_setattr*/
580 0, /*tp_compare*/
581 0, /*tp_repr*/
582 0, /*tp_as_number*/
583 0, /*tp_as_sequence*/
584 0, /*tp_as_mapping*/
585 0, /*tp_hash */
586 0, /*tp_call*/
587 gdbpy_reggroup_to_string, /*tp_str*/
588 0, /*tp_getattro*/
589 0, /*tp_setattro*/
590 0, /*tp_as_buffer*/
591 Py_TPFLAGS_DEFAULT, /*tp_flags*/
592 "GDB register group object", /*tp_doc */
593 0, /*tp_traverse */
594 0, /*tp_clear */
595 0, /*tp_richcompare */
596 0, /*tp_weaklistoffset */
597 0, /*tp_iter */
598 0, /*tp_iternext */
599 0, /*tp_methods */
600 0, /*tp_members */
601 gdbpy_reggroup_getset /*tp_getset */