From cb275538dbddfbb3c2c372a665ac48e6f617ea33 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 1 Jun 2022 15:31:15 -0600 Subject: Use registry in gdbarch gdbarch implements its own registry-like approach. This patch changes it to instead use registry.h. It's a rather large patch but largely uninteresting -- it's mostly a straightforward conversion from the old approach to the new one. The main benefit of this change is that it introduces type safety to the gdbarch registry. It also removes a bunch of code. One possible drawback is that, previously, the gdbarch registry differentiated between pre- and post-initialization setup. This doesn't seem very important to me, though. --- gdb/python/py-arch.c | 24 +++++++++++------------- gdb/python/py-registers.c | 31 +++++++++---------------------- gdb/python/py-unwind.c | 18 +++++------------- 3 files changed, 25 insertions(+), 48 deletions(-) (limited to 'gdb/python') diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index 594e4ed..cf09785 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -28,7 +28,8 @@ struct arch_object { struct gdbarch *gdbarch; }; -static struct gdbarch_data *arch_object_data = NULL; +static const registry::key> + arch_object_data; /* Require a valid Architecture. */ #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \ @@ -48,7 +49,7 @@ extern PyTypeObject arch_object_type /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch post init registration mechanism (gdbarch_data_register_post_init). */ -static void * +static PyObject * arch_object_data_init (struct gdbarch *gdbarch) { arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type); @@ -58,7 +59,7 @@ arch_object_data_init (struct gdbarch *gdbarch) arch_obj->gdbarch = gdbarch; - return (void *) arch_obj; + return (PyObject *) arch_obj; } /* Returns the struct gdbarch value corresponding to the given Python @@ -88,10 +89,14 @@ gdbpy_is_architecture (PyObject *obj) PyObject * gdbarch_to_arch_object (struct gdbarch *gdbarch) { - PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data); + PyObject *new_ref = arch_object_data.get (gdbarch); + if (new_ref == nullptr) + { + new_ref = arch_object_data_init (gdbarch); + arch_object_data.set (gdbarch, new_ref); + } - /* new_ref could be NULL if registration of arch_object with GDBARCH failed - in arch_object_data_init. */ + /* new_ref could be NULL if creation failed. */ Py_XINCREF (new_ref); return new_ref; @@ -337,13 +342,6 @@ gdbpy_all_architecture_names (PyObject *self, PyObject *args) return list.release (); } -void _initialize_py_arch (); -void -_initialize_py_arch () -{ - arch_object_data = gdbarch_data_register_post_init (arch_object_data_init); -} - /* Initializes the Architecture class in the gdb module. */ int diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index bbb322f..f22575a 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -25,8 +25,12 @@ #include "user-regs.h" #include +/* Per-gdbarch data type. */ +typedef std::vector> gdbpy_register_type; + /* Token to access per-gdbarch data related to register descriptors. */ -static struct gdbarch_data *gdbpy_register_object_data = NULL; +static const registry::key + gdbpy_register_object_data; /* Structure for iterator over register descriptors. */ struct register_descriptor_iterator_object { @@ -85,16 +89,6 @@ struct reggroup_object { extern PyTypeObject reggroup_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_object"); -/* Associates a vector of gdb.RegisterDescriptor objects with GDBARCH as - gdbarch_data via the gdbarch post init registration mechanism - (gdbarch_data_register_post_init). */ - -static void * -gdbpy_register_object_data_init (struct gdbarch *gdbarch) -{ - return new std::vector>; -} - /* Return a gdb.RegisterGroup object wrapping REGGROUP. The register group objects are cached, and the same Python object will always be returned for the same REGGROUP pointer. */ @@ -156,9 +150,10 @@ static gdbpy_ref<> gdbpy_get_register_descriptor (struct gdbarch *gdbarch, int regnum) { - auto &vec - = *(std::vector> *) gdbarch_data (gdbarch, - gdbpy_register_object_data); + gdbpy_register_type *vecp = gdbpy_register_object_data.get (gdbarch); + if (vecp == nullptr) + vecp = gdbpy_register_object_data.emplace (gdbarch); + gdbpy_register_type &vec = *vecp; /* Ensure that we have enough entries in the vector. */ if (vec.size () <= regnum) @@ -422,14 +417,6 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id, return false; } -void _initialize_py_registers (); -void -_initialize_py_registers () -{ - gdbpy_register_object_data - = gdbarch_data_register_post_init (gdbpy_register_object_data_init); -} - /* Initializes the new Python classes from this file in the gdb module. */ int diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index b2fd140..fb94661 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -117,8 +117,6 @@ extern PyTypeObject pending_frame_object_type extern PyTypeObject unwind_info_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("unwind_info_object"); -static struct gdbarch_data *pyuw_gdbarch_data; - /* Convert gdb.Value instance to inferior's pointer. Return 1 on success, 0 on failure. */ @@ -657,14 +655,10 @@ pyuw_dealloc_cache (struct frame_info *this_frame, void *cache) struct pyuw_gdbarch_data_type { /* Has the unwinder shim been prepended? */ - int unwinder_registered; + int unwinder_registered = 0; }; -static void * -pyuw_gdbarch_data_init (struct gdbarch *gdbarch) -{ - return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct pyuw_gdbarch_data_type); -} +static const registry::key pyuw_gdbarch_data; /* New inferior architecture callback: register the Python unwinders intermediary. */ @@ -672,9 +666,9 @@ pyuw_gdbarch_data_init (struct gdbarch *gdbarch) static void pyuw_on_new_gdbarch (struct gdbarch *newarch) { - struct pyuw_gdbarch_data_type *data - = (struct pyuw_gdbarch_data_type *) gdbarch_data (newarch, - pyuw_gdbarch_data); + struct pyuw_gdbarch_data_type *data = pyuw_gdbarch_data.get (newarch); + if (data == nullptr) + data= pyuw_gdbarch_data.emplace (newarch); if (!data->unwinder_registered) { @@ -706,8 +700,6 @@ _initialize_py_unwind () NULL, show_pyuw_debug, &setdebuglist, &showdebuglist); - pyuw_gdbarch_data - = gdbarch_data_register_post_init (pyuw_gdbarch_data_init); } /* Initialize unwind machinery. */ -- cgit v1.1