xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/python/py-registers.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Python interface to register, and register group information.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 2020-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos #include "defs.h"
217d62b00eSchristos #include "gdbarch.h"
227d62b00eSchristos #include "arch-utils.h"
237d62b00eSchristos #include "reggroups.h"
247d62b00eSchristos #include "python-internal.h"
257d62b00eSchristos #include "user-regs.h"
267d62b00eSchristos #include <unordered_map>
277d62b00eSchristos 
28*6881a400Schristos /* Per-gdbarch data type.  */
29*6881a400Schristos typedef std::vector<gdbpy_ref<>> gdbpy_register_type;
30*6881a400Schristos 
317d62b00eSchristos /* Token to access per-gdbarch data related to register descriptors.  */
32*6881a400Schristos static const registry<gdbarch>::key<gdbpy_register_type>
33*6881a400Schristos      gdbpy_register_object_data;
347d62b00eSchristos 
357d62b00eSchristos /* Structure for iterator over register descriptors.  */
36*6881a400Schristos struct register_descriptor_iterator_object {
377d62b00eSchristos   PyObject_HEAD
387d62b00eSchristos 
397d62b00eSchristos   /* The register group that the user is iterating over.  This will never
407d62b00eSchristos      be NULL.  */
41*6881a400Schristos   const struct reggroup *reggroup;
427d62b00eSchristos 
437d62b00eSchristos   /* The next register number to lookup.  Starts at 0 and counts up.  */
447d62b00eSchristos   int regnum;
457d62b00eSchristos 
467d62b00eSchristos   /* Pointer back to the architecture we're finding registers for.  */
477d62b00eSchristos   struct gdbarch *gdbarch;
48*6881a400Schristos };
497d62b00eSchristos 
507d62b00eSchristos extern PyTypeObject register_descriptor_iterator_object_type
517d62b00eSchristos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_iterator_object");
527d62b00eSchristos 
537d62b00eSchristos /* A register descriptor.  */
54*6881a400Schristos struct register_descriptor_object {
557d62b00eSchristos   PyObject_HEAD
567d62b00eSchristos 
577d62b00eSchristos   /* The register this is a descriptor for.  */
587d62b00eSchristos   int regnum;
597d62b00eSchristos 
607d62b00eSchristos   /* The architecture this is a register for.  */
617d62b00eSchristos   struct gdbarch *gdbarch;
62*6881a400Schristos };
637d62b00eSchristos 
647d62b00eSchristos extern PyTypeObject register_descriptor_object_type
657d62b00eSchristos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("register_descriptor_object");
667d62b00eSchristos 
677d62b00eSchristos /* Structure for iterator over register groups.  */
68*6881a400Schristos struct reggroup_iterator_object {
697d62b00eSchristos   PyObject_HEAD
707d62b00eSchristos 
71*6881a400Schristos   /* The index into GROUPS for the next group to return.  */
72*6881a400Schristos   std::vector<const reggroup *>::size_type index;
737d62b00eSchristos 
747d62b00eSchristos   /* Pointer back to the architecture we're finding registers for.  */
757d62b00eSchristos   struct gdbarch *gdbarch;
76*6881a400Schristos };
777d62b00eSchristos 
787d62b00eSchristos extern PyTypeObject reggroup_iterator_object_type
797d62b00eSchristos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_iterator_object");
807d62b00eSchristos 
817d62b00eSchristos /* A register group object.  */
82*6881a400Schristos struct reggroup_object {
837d62b00eSchristos   PyObject_HEAD
847d62b00eSchristos 
857d62b00eSchristos   /* The register group being described.  */
86*6881a400Schristos   const struct reggroup *reggroup;
87*6881a400Schristos };
887d62b00eSchristos 
897d62b00eSchristos extern PyTypeObject reggroup_object_type
907d62b00eSchristos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_object");
917d62b00eSchristos 
927d62b00eSchristos /* Return a gdb.RegisterGroup object wrapping REGGROUP.  The register
937d62b00eSchristos    group objects are cached, and the same Python object will always be
947d62b00eSchristos    returned for the same REGGROUP pointer.  */
957d62b00eSchristos 
967d62b00eSchristos static gdbpy_ref<>
97*6881a400Schristos gdbpy_get_reggroup (const reggroup *reggroup)
987d62b00eSchristos {
997d62b00eSchristos   /* Map from GDB's internal reggroup objects to the Python representation.
1007d62b00eSchristos      GDB's reggroups are global, and are never deleted, so using a map like
1017d62b00eSchristos      this is safe.  */
102*6881a400Schristos   static std::unordered_map<const struct reggroup *,gdbpy_ref<>>
1037d62b00eSchristos     gdbpy_reggroup_object_map;
1047d62b00eSchristos 
1057d62b00eSchristos   /* If there is not already a suitable Python object in the map then
1067d62b00eSchristos      create a new one, and add it to the map.  */
1077d62b00eSchristos   if (gdbpy_reggroup_object_map[reggroup] == nullptr)
1087d62b00eSchristos     {
1097d62b00eSchristos       /* Create a new object and fill in its details.  */
1107d62b00eSchristos       gdbpy_ref<reggroup_object> group
1117d62b00eSchristos 	(PyObject_New (reggroup_object, &reggroup_object_type));
1127d62b00eSchristos       if (group == NULL)
1137d62b00eSchristos 	return NULL;
1147d62b00eSchristos       group->reggroup = reggroup;
1157d62b00eSchristos       gdbpy_reggroup_object_map[reggroup]
1167d62b00eSchristos 	= gdbpy_ref<> ((PyObject *) group.release ());
1177d62b00eSchristos     }
1187d62b00eSchristos 
1197d62b00eSchristos   /* Fetch the Python object wrapping REGGROUP from the map, increasing
1207d62b00eSchristos      the reference count is handled by the gdbpy_ref class.  */
1217d62b00eSchristos   return gdbpy_reggroup_object_map[reggroup];
1227d62b00eSchristos }
1237d62b00eSchristos 
1247d62b00eSchristos /* Convert a gdb.RegisterGroup to a string, it just returns the name of
1257d62b00eSchristos    the register group.  */
1267d62b00eSchristos 
1277d62b00eSchristos static PyObject *
1287d62b00eSchristos gdbpy_reggroup_to_string (PyObject *self)
1297d62b00eSchristos {
1307d62b00eSchristos   reggroup_object *group = (reggroup_object *) self;
131*6881a400Schristos   const reggroup *reggroup = group->reggroup;
1327d62b00eSchristos 
133*6881a400Schristos   return PyUnicode_FromString (reggroup->name ());
1347d62b00eSchristos }
1357d62b00eSchristos 
1367d62b00eSchristos /* Implement gdb.RegisterGroup.name (self) -> String.
1377d62b00eSchristos    Return a string that is the name of this register group.  */
1387d62b00eSchristos 
1397d62b00eSchristos static PyObject *
1407d62b00eSchristos gdbpy_reggroup_name (PyObject *self, void *closure)
1417d62b00eSchristos {
1427d62b00eSchristos   return gdbpy_reggroup_to_string (self);
1437d62b00eSchristos }
1447d62b00eSchristos 
1457d62b00eSchristos /* Return a gdb.RegisterDescriptor object for REGNUM from GDBARCH.  For
1467d62b00eSchristos    each REGNUM (in GDBARCH) only one descriptor is ever created, which is
1477d62b00eSchristos    then cached on the GDBARCH.  */
1487d62b00eSchristos 
1497d62b00eSchristos static gdbpy_ref<>
1507d62b00eSchristos gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
1517d62b00eSchristos 			       int regnum)
1527d62b00eSchristos {
153*6881a400Schristos   gdbpy_register_type *vecp = gdbpy_register_object_data.get (gdbarch);
154*6881a400Schristos   if (vecp == nullptr)
155*6881a400Schristos     vecp = gdbpy_register_object_data.emplace (gdbarch);
156*6881a400Schristos   gdbpy_register_type &vec = *vecp;
1577d62b00eSchristos 
1587d62b00eSchristos   /* Ensure that we have enough entries in the vector.  */
1597d62b00eSchristos   if (vec.size () <= regnum)
1607d62b00eSchristos     vec.resize ((regnum + 1), nullptr);
1617d62b00eSchristos 
1627d62b00eSchristos   /* If we don't already have a descriptor for REGNUM in GDBARCH then
1637d62b00eSchristos      create one now.  */
1647d62b00eSchristos   if (vec[regnum] == nullptr)
1657d62b00eSchristos     {
1667d62b00eSchristos       gdbpy_ref <register_descriptor_object> reg
1677d62b00eSchristos 	(PyObject_New (register_descriptor_object,
1687d62b00eSchristos 		       &register_descriptor_object_type));
1697d62b00eSchristos       if (reg == NULL)
1707d62b00eSchristos 	return NULL;
1717d62b00eSchristos       reg->regnum = regnum;
1727d62b00eSchristos       reg->gdbarch = gdbarch;
1737d62b00eSchristos       vec[regnum] = gdbpy_ref<> ((PyObject *) reg.release ());
1747d62b00eSchristos     }
1757d62b00eSchristos 
1767d62b00eSchristos   /* Grab the register descriptor from the vector, the reference count is
1777d62b00eSchristos      automatically incremented thanks to gdbpy_ref.  */
1787d62b00eSchristos   return vec[regnum];
1797d62b00eSchristos }
1807d62b00eSchristos 
1817d62b00eSchristos /* Convert the register descriptor to a string.  */
1827d62b00eSchristos 
1837d62b00eSchristos static PyObject *
1847d62b00eSchristos gdbpy_register_descriptor_to_string (PyObject *self)
1857d62b00eSchristos {
1867d62b00eSchristos   register_descriptor_object *reg
1877d62b00eSchristos     = (register_descriptor_object *) self;
1887d62b00eSchristos   struct gdbarch *gdbarch = reg->gdbarch;
1897d62b00eSchristos   int regnum = reg->regnum;
1907d62b00eSchristos 
1917d62b00eSchristos   const char *name = gdbarch_register_name (gdbarch, regnum);
192*6881a400Schristos   return PyUnicode_FromString (name);
1937d62b00eSchristos }
1947d62b00eSchristos 
1957d62b00eSchristos /* Implement gdb.RegisterDescriptor.name attribute get function.  Return a
1967d62b00eSchristos    string that is the name of this register.  Due to checking when register
1977d62b00eSchristos    descriptors are created the name will never by the empty string.  */
1987d62b00eSchristos 
1997d62b00eSchristos static PyObject *
2007d62b00eSchristos gdbpy_register_descriptor_name (PyObject *self, void *closure)
2017d62b00eSchristos {
2027d62b00eSchristos   return gdbpy_register_descriptor_to_string (self);
2037d62b00eSchristos }
2047d62b00eSchristos 
2057d62b00eSchristos /* Return a reference to the gdb.RegisterGroupsIterator object.  */
2067d62b00eSchristos 
2077d62b00eSchristos static PyObject *
2087d62b00eSchristos gdbpy_reggroup_iter (PyObject *self)
2097d62b00eSchristos {
2107d62b00eSchristos   Py_INCREF (self);
2117d62b00eSchristos   return self;
2127d62b00eSchristos }
2137d62b00eSchristos 
2147d62b00eSchristos /* Return the next gdb.RegisterGroup object from the iterator.  */
2157d62b00eSchristos 
2167d62b00eSchristos static PyObject *
2177d62b00eSchristos gdbpy_reggroup_iter_next (PyObject *self)
2187d62b00eSchristos {
2197d62b00eSchristos   reggroup_iterator_object *iter_obj
2207d62b00eSchristos     = (reggroup_iterator_object *) self;
2217d62b00eSchristos 
222*6881a400Schristos   const std::vector<const reggroup *> &groups
223*6881a400Schristos     = gdbarch_reggroups (iter_obj->gdbarch);
224*6881a400Schristos   if (iter_obj->index >= groups.size ())
2257d62b00eSchristos     {
2267d62b00eSchristos       PyErr_SetString (PyExc_StopIteration, _("No more groups"));
2277d62b00eSchristos       return NULL;
2287d62b00eSchristos     }
2297d62b00eSchristos 
230*6881a400Schristos   const reggroup *group = groups[iter_obj->index];
231*6881a400Schristos   iter_obj->index++;
232*6881a400Schristos   return gdbpy_get_reggroup (group).release ();
2337d62b00eSchristos }
2347d62b00eSchristos 
2357d62b00eSchristos /* Return a new gdb.RegisterGroupsIterator over all the register groups in
2367d62b00eSchristos    GDBARCH.  */
2377d62b00eSchristos 
2387d62b00eSchristos PyObject *
2397d62b00eSchristos gdbpy_new_reggroup_iterator (struct gdbarch *gdbarch)
2407d62b00eSchristos {
2417d62b00eSchristos   gdb_assert (gdbarch != nullptr);
2427d62b00eSchristos 
2437d62b00eSchristos   /* Create a new object and fill in its internal state.  */
2447d62b00eSchristos   reggroup_iterator_object *iter
2457d62b00eSchristos     = PyObject_New (reggroup_iterator_object,
2467d62b00eSchristos 		    &reggroup_iterator_object_type);
2477d62b00eSchristos   if (iter == NULL)
2487d62b00eSchristos     return NULL;
249*6881a400Schristos   iter->index = 0;
2507d62b00eSchristos   iter->gdbarch = gdbarch;
2517d62b00eSchristos   return (PyObject *) iter;
2527d62b00eSchristos }
2537d62b00eSchristos 
2547d62b00eSchristos /* Create and return a new gdb.RegisterDescriptorIterator object which
2557d62b00eSchristos    will iterate over all registers in GROUP_NAME for GDBARCH.  If
2567d62b00eSchristos    GROUP_NAME is either NULL or the empty string then the ALL_REGGROUP is
2577d62b00eSchristos    used, otherwise lookup the register group matching GROUP_NAME and use
2587d62b00eSchristos    that.
2597d62b00eSchristos 
2607d62b00eSchristos    This function can return NULL if GROUP_NAME isn't found.  */
2617d62b00eSchristos 
2627d62b00eSchristos PyObject *
2637d62b00eSchristos gdbpy_new_register_descriptor_iterator (struct gdbarch *gdbarch,
2647d62b00eSchristos 					const char *group_name)
2657d62b00eSchristos {
266*6881a400Schristos   const reggroup *grp = NULL;
2677d62b00eSchristos 
2687d62b00eSchristos   /* Lookup the requested register group, or find the default.  */
2697d62b00eSchristos   if (group_name == NULL || *group_name == '\0')
2707d62b00eSchristos     grp = all_reggroup;
2717d62b00eSchristos   else
2727d62b00eSchristos     {
2737d62b00eSchristos       grp = reggroup_find (gdbarch, group_name);
2747d62b00eSchristos       if (grp == NULL)
2757d62b00eSchristos 	{
2767d62b00eSchristos 	  PyErr_SetString (PyExc_ValueError,
2777d62b00eSchristos 			   _("Unknown register group name."));
2787d62b00eSchristos 	  return NULL;
2797d62b00eSchristos 	}
2807d62b00eSchristos     }
2817d62b00eSchristos   /* Create a new iterator object initialised for this architecture and
2827d62b00eSchristos      fill in all of the details.  */
2837d62b00eSchristos   register_descriptor_iterator_object *iter
2847d62b00eSchristos     = PyObject_New (register_descriptor_iterator_object,
2857d62b00eSchristos 		    &register_descriptor_iterator_object_type);
2867d62b00eSchristos   if (iter == NULL)
2877d62b00eSchristos     return NULL;
2887d62b00eSchristos   iter->regnum = 0;
2897d62b00eSchristos   iter->gdbarch = gdbarch;
2907d62b00eSchristos   gdb_assert (grp != NULL);
2917d62b00eSchristos   iter->reggroup = grp;
2927d62b00eSchristos 
2937d62b00eSchristos   return (PyObject *) iter;
2947d62b00eSchristos }
2957d62b00eSchristos 
2967d62b00eSchristos /* Return a reference to the gdb.RegisterDescriptorIterator object.  */
2977d62b00eSchristos 
2987d62b00eSchristos static PyObject *
2997d62b00eSchristos gdbpy_register_descriptor_iter (PyObject *self)
3007d62b00eSchristos {
3017d62b00eSchristos   Py_INCREF (self);
3027d62b00eSchristos   return self;
3037d62b00eSchristos }
3047d62b00eSchristos 
3057d62b00eSchristos /* Return the next register name.  */
3067d62b00eSchristos 
3077d62b00eSchristos static PyObject *
3087d62b00eSchristos gdbpy_register_descriptor_iter_next (PyObject *self)
3097d62b00eSchristos {
3107d62b00eSchristos   register_descriptor_iterator_object *iter_obj
3117d62b00eSchristos     = (register_descriptor_iterator_object *) self;
3127d62b00eSchristos   struct gdbarch *gdbarch = iter_obj->gdbarch;
3137d62b00eSchristos 
3147d62b00eSchristos   do
3157d62b00eSchristos     {
3167d62b00eSchristos       if (iter_obj->regnum >= gdbarch_num_cooked_regs (gdbarch))
3177d62b00eSchristos 	{
3187d62b00eSchristos 	  PyErr_SetString (PyExc_StopIteration, _("No more registers"));
3197d62b00eSchristos 	  return NULL;
3207d62b00eSchristos 	}
3217d62b00eSchristos 
3227d62b00eSchristos       const char *name = nullptr;
3237d62b00eSchristos       int regnum = iter_obj->regnum;
3247d62b00eSchristos       if (gdbarch_register_reggroup_p (gdbarch, regnum,
3257d62b00eSchristos 				       iter_obj->reggroup))
3267d62b00eSchristos 	name = gdbarch_register_name (gdbarch, regnum);
3277d62b00eSchristos       iter_obj->regnum++;
3287d62b00eSchristos 
3297d62b00eSchristos       if (name != nullptr && *name != '\0')
3307d62b00eSchristos 	return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
3317d62b00eSchristos     }
3327d62b00eSchristos   while (true);
3337d62b00eSchristos }
3347d62b00eSchristos 
3357d62b00eSchristos /* Implement:
3367d62b00eSchristos 
3377d62b00eSchristos    gdb.RegisterDescriptorIterator.find (self, name) -> gdb.RegisterDescriptor
3387d62b00eSchristos 
3397d62b00eSchristos    Look up a descriptor for register with NAME.  If no matching register is
3407d62b00eSchristos    found then return None.  */
3417d62b00eSchristos 
3427d62b00eSchristos static PyObject *
3437d62b00eSchristos register_descriptor_iter_find (PyObject *self, PyObject *args, PyObject *kw)
3447d62b00eSchristos {
3457d62b00eSchristos   static const char *keywords[] = { "name", NULL };
3467d62b00eSchristos   const char *register_name = NULL;
3477d62b00eSchristos 
3487d62b00eSchristos   register_descriptor_iterator_object *iter_obj
3497d62b00eSchristos     = (register_descriptor_iterator_object *) self;
3507d62b00eSchristos   struct gdbarch *gdbarch = iter_obj->gdbarch;
3517d62b00eSchristos 
3527d62b00eSchristos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s", keywords,
3537d62b00eSchristos 					&register_name))
3547d62b00eSchristos     return NULL;
3557d62b00eSchristos 
3567d62b00eSchristos   if (register_name != NULL && *register_name != '\0')
3577d62b00eSchristos     {
3587d62b00eSchristos       int regnum = user_reg_map_name_to_regnum (gdbarch, register_name,
3597d62b00eSchristos 						strlen (register_name));
3607d62b00eSchristos       if (regnum >= 0)
3617d62b00eSchristos 	return gdbpy_get_register_descriptor (gdbarch, regnum).release ();
3627d62b00eSchristos     }
3637d62b00eSchristos 
3647d62b00eSchristos   Py_RETURN_NONE;
3657d62b00eSchristos }
3667d62b00eSchristos 
3677d62b00eSchristos /* See python-internal.h.  */
3687d62b00eSchristos 
3697d62b00eSchristos bool
3707d62b00eSchristos gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id,
3717d62b00eSchristos 			 int *reg_num)
3727d62b00eSchristos {
3737d62b00eSchristos   gdb_assert (pyo_reg_id != NULL);
3747d62b00eSchristos 
3757d62b00eSchristos   /* The register could be a string, its name.  */
3767d62b00eSchristos   if (gdbpy_is_string (pyo_reg_id))
3777d62b00eSchristos     {
3787d62b00eSchristos       gdb::unique_xmalloc_ptr<char> reg_name (gdbpy_obj_to_string (pyo_reg_id));
3797d62b00eSchristos 
3807d62b00eSchristos       if (reg_name != NULL)
3817d62b00eSchristos 	{
3827d62b00eSchristos 	  *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (),
3837d62b00eSchristos 						  strlen (reg_name.get ()));
384*6881a400Schristos 	  if (*reg_num >= 0)
385*6881a400Schristos 	    return true;
386*6881a400Schristos 	  PyErr_SetString (PyExc_ValueError, "Bad register");
3877d62b00eSchristos 	}
3887d62b00eSchristos     }
3897d62b00eSchristos   /* The register could be its internal GDB register number.  */
390*6881a400Schristos   else if (PyLong_Check (pyo_reg_id))
3917d62b00eSchristos     {
3927d62b00eSchristos       long value;
393*6881a400Schristos       if (gdb_py_int_as_long (pyo_reg_id, &value) == 0)
3947d62b00eSchristos 	{
395*6881a400Schristos 	  /* Nothing -- error.  */
396*6881a400Schristos 	}
397*6881a400Schristos       else if ((int) value == value
398*6881a400Schristos 	       && user_reg_map_regnum_to_name (gdbarch, value) != NULL)
3997d62b00eSchristos 	{
4007d62b00eSchristos 	  *reg_num = (int) value;
4017d62b00eSchristos 	  return true;
4027d62b00eSchristos 	}
403*6881a400Schristos       else
404*6881a400Schristos 	PyErr_SetString (PyExc_ValueError, "Bad register");
4057d62b00eSchristos     }
4067d62b00eSchristos   /* The register could be a gdb.RegisterDescriptor object.  */
4077d62b00eSchristos   else if (PyObject_IsInstance (pyo_reg_id,
4087d62b00eSchristos 			   (PyObject *) &register_descriptor_object_type))
4097d62b00eSchristos     {
4107d62b00eSchristos       register_descriptor_object *reg
4117d62b00eSchristos 	= (register_descriptor_object *) pyo_reg_id;
4127d62b00eSchristos       if (reg->gdbarch == gdbarch)
4137d62b00eSchristos 	{
4147d62b00eSchristos 	  *reg_num = reg->regnum;
4157d62b00eSchristos 	  return true;
4167d62b00eSchristos 	}
4177d62b00eSchristos       else
4187d62b00eSchristos 	PyErr_SetString (PyExc_ValueError,
4197d62b00eSchristos 			 _("Invalid Architecture in RegisterDescriptor"));
4207d62b00eSchristos     }
421*6881a400Schristos   else
422*6881a400Schristos     PyErr_SetString (PyExc_TypeError, _("Invalid type for register"));
4237d62b00eSchristos 
4247d62b00eSchristos   gdb_assert (PyErr_Occurred ());
4257d62b00eSchristos   return false;
4267d62b00eSchristos }
4277d62b00eSchristos 
4287d62b00eSchristos /* Initializes the new Python classes from this file in the gdb module.  */
4297d62b00eSchristos 
4307d62b00eSchristos int
4317d62b00eSchristos gdbpy_initialize_registers ()
4327d62b00eSchristos {
4337d62b00eSchristos   register_descriptor_object_type.tp_new = PyType_GenericNew;
4347d62b00eSchristos   if (PyType_Ready (&register_descriptor_object_type) < 0)
4357d62b00eSchristos     return -1;
4367d62b00eSchristos   if (gdb_pymodule_addobject
4377d62b00eSchristos       (gdb_module, "RegisterDescriptor",
4387d62b00eSchristos        (PyObject *) &register_descriptor_object_type) < 0)
4397d62b00eSchristos     return -1;
4407d62b00eSchristos 
4417d62b00eSchristos   reggroup_iterator_object_type.tp_new = PyType_GenericNew;
4427d62b00eSchristos   if (PyType_Ready (&reggroup_iterator_object_type) < 0)
4437d62b00eSchristos     return -1;
4447d62b00eSchristos   if (gdb_pymodule_addobject
4457d62b00eSchristos       (gdb_module, "RegisterGroupsIterator",
4467d62b00eSchristos        (PyObject *) &reggroup_iterator_object_type) < 0)
4477d62b00eSchristos     return -1;
4487d62b00eSchristos 
4497d62b00eSchristos   reggroup_object_type.tp_new = PyType_GenericNew;
4507d62b00eSchristos   if (PyType_Ready (&reggroup_object_type) < 0)
4517d62b00eSchristos     return -1;
4527d62b00eSchristos   if (gdb_pymodule_addobject
4537d62b00eSchristos       (gdb_module, "RegisterGroup",
4547d62b00eSchristos        (PyObject *) &reggroup_object_type) < 0)
4557d62b00eSchristos     return -1;
4567d62b00eSchristos 
4577d62b00eSchristos   register_descriptor_iterator_object_type.tp_new = PyType_GenericNew;
4587d62b00eSchristos   if (PyType_Ready (&register_descriptor_iterator_object_type) < 0)
4597d62b00eSchristos     return -1;
4607d62b00eSchristos   return (gdb_pymodule_addobject
4617d62b00eSchristos 	  (gdb_module, "RegisterDescriptorIterator",
4627d62b00eSchristos 	   (PyObject *) &register_descriptor_iterator_object_type));
4637d62b00eSchristos }
4647d62b00eSchristos 
4657d62b00eSchristos static PyMethodDef register_descriptor_iterator_object_methods [] = {
4667d62b00eSchristos   { "find", (PyCFunction) register_descriptor_iter_find,
4677d62b00eSchristos     METH_VARARGS | METH_KEYWORDS,
4687d62b00eSchristos     "registers (name) -> gdb.RegisterDescriptor.\n\
4697d62b00eSchristos Return a register descriptor for the register NAME, or None if no register\n\
4707d62b00eSchristos with that name exists in this iterator." },
4717d62b00eSchristos   {NULL}  /* Sentinel */
4727d62b00eSchristos };
4737d62b00eSchristos 
4747d62b00eSchristos PyTypeObject register_descriptor_iterator_object_type = {
4757d62b00eSchristos   PyVarObject_HEAD_INIT (NULL, 0)
4767d62b00eSchristos   "gdb.RegisterDescriptorIterator",	  	/*tp_name*/
4777d62b00eSchristos   sizeof (register_descriptor_iterator_object),	/*tp_basicsize*/
4787d62b00eSchristos   0,				  /*tp_itemsize*/
4797d62b00eSchristos   0,				  /*tp_dealloc*/
4807d62b00eSchristos   0,				  /*tp_print*/
4817d62b00eSchristos   0,				  /*tp_getattr*/
4827d62b00eSchristos   0,				  /*tp_setattr*/
4837d62b00eSchristos   0,				  /*tp_compare*/
4847d62b00eSchristos   0,				  /*tp_repr*/
4857d62b00eSchristos   0,				  /*tp_as_number*/
4867d62b00eSchristos   0,				  /*tp_as_sequence*/
4877d62b00eSchristos   0,				  /*tp_as_mapping*/
4887d62b00eSchristos   0,				  /*tp_hash */
4897d62b00eSchristos   0,				  /*tp_call*/
4907d62b00eSchristos   0,				  /*tp_str*/
4917d62b00eSchristos   0,				  /*tp_getattro*/
4927d62b00eSchristos   0,				  /*tp_setattro*/
4937d62b00eSchristos   0,				  /*tp_as_buffer*/
494*6881a400Schristos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
4957d62b00eSchristos   "GDB architecture register descriptor iterator object",	/*tp_doc */
4967d62b00eSchristos   0,				  /*tp_traverse */
4977d62b00eSchristos   0,				  /*tp_clear */
4987d62b00eSchristos   0,				  /*tp_richcompare */
4997d62b00eSchristos   0,				  /*tp_weaklistoffset */
5007d62b00eSchristos   gdbpy_register_descriptor_iter,	  /*tp_iter */
5017d62b00eSchristos   gdbpy_register_descriptor_iter_next,  /*tp_iternext */
5027d62b00eSchristos   register_descriptor_iterator_object_methods		/*tp_methods */
5037d62b00eSchristos };
5047d62b00eSchristos 
5057d62b00eSchristos static gdb_PyGetSetDef gdbpy_register_descriptor_getset[] = {
5067d62b00eSchristos   { "name", gdbpy_register_descriptor_name, NULL,
5077d62b00eSchristos     "The name of this register.", NULL },
5087d62b00eSchristos   { NULL }  /* Sentinel */
5097d62b00eSchristos };
5107d62b00eSchristos 
5117d62b00eSchristos PyTypeObject register_descriptor_object_type = {
5127d62b00eSchristos   PyVarObject_HEAD_INIT (NULL, 0)
5137d62b00eSchristos   "gdb.RegisterDescriptor",	  /*tp_name*/
5147d62b00eSchristos   sizeof (register_descriptor_object),	/*tp_basicsize*/
5157d62b00eSchristos   0,				  /*tp_itemsize*/
5167d62b00eSchristos   0,				  /*tp_dealloc*/
5177d62b00eSchristos   0,				  /*tp_print*/
5187d62b00eSchristos   0,				  /*tp_getattr*/
5197d62b00eSchristos   0,				  /*tp_setattr*/
5207d62b00eSchristos   0,				  /*tp_compare*/
5217d62b00eSchristos   0,				  /*tp_repr*/
5227d62b00eSchristos   0,				  /*tp_as_number*/
5237d62b00eSchristos   0,				  /*tp_as_sequence*/
5247d62b00eSchristos   0,				  /*tp_as_mapping*/
5257d62b00eSchristos   0,				  /*tp_hash */
5267d62b00eSchristos   0,				  /*tp_call*/
5277d62b00eSchristos   gdbpy_register_descriptor_to_string,			/*tp_str*/
5287d62b00eSchristos   0,				  /*tp_getattro*/
5297d62b00eSchristos   0,				  /*tp_setattro*/
5307d62b00eSchristos   0,				  /*tp_as_buffer*/
5317d62b00eSchristos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
5327d62b00eSchristos   "GDB architecture register descriptor object",	/*tp_doc */
5337d62b00eSchristos   0,				  /*tp_traverse */
5347d62b00eSchristos   0,				  /*tp_clear */
5357d62b00eSchristos   0,				  /*tp_richcompare */
5367d62b00eSchristos   0,				  /*tp_weaklistoffset */
5377d62b00eSchristos   0,				  /*tp_iter */
5387d62b00eSchristos   0,				  /*tp_iternext */
5397d62b00eSchristos   0,				  /*tp_methods */
5407d62b00eSchristos   0,				  /*tp_members */
5417d62b00eSchristos   gdbpy_register_descriptor_getset			/*tp_getset */
5427d62b00eSchristos };
5437d62b00eSchristos 
5447d62b00eSchristos PyTypeObject reggroup_iterator_object_type = {
5457d62b00eSchristos   PyVarObject_HEAD_INIT (NULL, 0)
5467d62b00eSchristos   "gdb.RegisterGroupsIterator",	  /*tp_name*/
5477d62b00eSchristos   sizeof (reggroup_iterator_object),		/*tp_basicsize*/
5487d62b00eSchristos   0,				  /*tp_itemsize*/
5497d62b00eSchristos   0,				  /*tp_dealloc*/
5507d62b00eSchristos   0,				  /*tp_print*/
5517d62b00eSchristos   0,				  /*tp_getattr*/
5527d62b00eSchristos   0,				  /*tp_setattr*/
5537d62b00eSchristos   0,				  /*tp_compare*/
5547d62b00eSchristos   0,				  /*tp_repr*/
5557d62b00eSchristos   0,				  /*tp_as_number*/
5567d62b00eSchristos   0,				  /*tp_as_sequence*/
5577d62b00eSchristos   0,				  /*tp_as_mapping*/
5587d62b00eSchristos   0,				  /*tp_hash */
5597d62b00eSchristos   0,				  /*tp_call*/
5607d62b00eSchristos   0,				  /*tp_str*/
5617d62b00eSchristos   0,				  /*tp_getattro*/
5627d62b00eSchristos   0,				  /*tp_setattro*/
5637d62b00eSchristos   0,				  /*tp_as_buffer*/
564*6881a400Schristos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
5657d62b00eSchristos   "GDB register groups iterator object",	/*tp_doc */
5667d62b00eSchristos   0,				  /*tp_traverse */
5677d62b00eSchristos   0,				  /*tp_clear */
5687d62b00eSchristos   0,				  /*tp_richcompare */
5697d62b00eSchristos   0,				  /*tp_weaklistoffset */
5707d62b00eSchristos   gdbpy_reggroup_iter,		  /*tp_iter */
5717d62b00eSchristos   gdbpy_reggroup_iter_next,	  /*tp_iternext */
5727d62b00eSchristos   0				  /*tp_methods */
5737d62b00eSchristos };
5747d62b00eSchristos 
5757d62b00eSchristos static gdb_PyGetSetDef gdbpy_reggroup_getset[] = {
5767d62b00eSchristos   { "name", gdbpy_reggroup_name, NULL,
5777d62b00eSchristos     "The name of this register group.", NULL },
5787d62b00eSchristos   { NULL }  /* Sentinel */
5797d62b00eSchristos };
5807d62b00eSchristos 
5817d62b00eSchristos PyTypeObject reggroup_object_type = {
5827d62b00eSchristos   PyVarObject_HEAD_INIT (NULL, 0)
5837d62b00eSchristos   "gdb.RegisterGroup",		  /*tp_name*/
5847d62b00eSchristos   sizeof (reggroup_object),	  /*tp_basicsize*/
5857d62b00eSchristos   0,				  /*tp_itemsize*/
5867d62b00eSchristos   0,				  /*tp_dealloc*/
5877d62b00eSchristos   0,				  /*tp_print*/
5887d62b00eSchristos   0,				  /*tp_getattr*/
5897d62b00eSchristos   0,				  /*tp_setattr*/
5907d62b00eSchristos   0,				  /*tp_compare*/
5917d62b00eSchristos   0,				  /*tp_repr*/
5927d62b00eSchristos   0,				  /*tp_as_number*/
5937d62b00eSchristos   0,				  /*tp_as_sequence*/
5947d62b00eSchristos   0,				  /*tp_as_mapping*/
5957d62b00eSchristos   0,				  /*tp_hash */
5967d62b00eSchristos   0,				  /*tp_call*/
5977d62b00eSchristos   gdbpy_reggroup_to_string,	  /*tp_str*/
5987d62b00eSchristos   0,				  /*tp_getattro*/
5997d62b00eSchristos   0,				  /*tp_setattro*/
6007d62b00eSchristos   0,				  /*tp_as_buffer*/
6017d62b00eSchristos   Py_TPFLAGS_DEFAULT,		  /*tp_flags*/
6027d62b00eSchristos   "GDB register group object",	  /*tp_doc */
6037d62b00eSchristos   0,				  /*tp_traverse */
6047d62b00eSchristos   0,				  /*tp_clear */
6057d62b00eSchristos   0,				  /*tp_richcompare */
6067d62b00eSchristos   0,				  /*tp_weaklistoffset */
6077d62b00eSchristos   0,				  /*tp_iter */
6087d62b00eSchristos   0,				  /*tp_iternext */
6097d62b00eSchristos   0,				  /*tp_methods */
6107d62b00eSchristos   0,				  /*tp_members */
6117d62b00eSchristos   gdbpy_reggroup_getset		  /*tp_getset */
6127d62b00eSchristos };
613