[binutils-gdb] gdb: delay python initialisation until gdbpy_finish_initialization

Andrew Burgess aburgess@sourceware.org
Wed Apr 28 10:27:57 GMT 2021


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8e3685bf250d9ecda5058912d6624e77d7a2b07e

commit 8e3685bf250d9ecda5058912d6624e77d7a2b07e
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Thu Apr 22 17:11:25 2021 +0100

    gdb: delay python initialisation until gdbpy_finish_initialization
    
    Delay Python initialisation until gdbpy_finish_initialization.
    
    This is mostly about splitting the existing gdbpy_initialize_*
    functions in two, all the calls to register_objfile_data_with_cleanup,
    gdbarch_data_register_post_init, etc are moved into new _initialize_*
    functions, but everything else is left in the gdbpy_initialize_*
    functions.
    
    Then the call to do_start_initialization (in python/python.c) is moved
    from the _initialize_python function into gdbpy_finish_initialization.
    
    There should be no user visible changes after this commit.
    
    gdb/ChangeLog:
    
            * python/py-arch.c (_initialize_py_arch): New function.
            (gdbpy_initialize_arch): Move code to _initialize_py_arch.
            * python/py-block.c (_initialize_py_block): New function.
            (gdbpy_initialize_blocks): Move code to _initialize_py_block.
            * python/py-inferior.c (_initialize_py_inferior): New function.
            (gdbpy_initialize_inferior): Move code to _initialize_py_inferior.
            * python/py-objfile.c (_initialize_py_objfile): New function.
            (gdbpy_initialize_objfile): Move code to _initialize_py_objfile.
            * python/py-progspace.c (_initialize_py_progspace): New function.
            (gdbpy_initialize_pspace): Move code to _initialize_py_progspace.
            * python/py-registers.c (_initialize_py_registers): New function.
            (gdbpy_initialize_registers): Move code to
            _initialize_py_registers.
            * python/py-symbol.c (_initialize_py_symbol): New function.
            (gdbpy_initialize_symbols): Move code to _initialize_py_symbol.
            * python/py-symtab.c (_initialize_py_symtab): New function.
            (gdbpy_initialize_symtabs): Move code to _initialize_py_symtab.
            * python/py-type.c (_initialize_py_type): New function.
            (gdbpy_initialize_types): Move code to _initialize_py_type.
            * python/py-unwind.c (_initialize_py_unwind): New function.
            (gdbpy_initialize_unwind): Move code to _initialize_py_unwind.
            * python/python.c (_initialize_python): Move call to
            do_start_initialization to gdbpy_finish_initialization.
            (gdbpy_finish_initialization): Add call to
            do_start_initialization.

Diff:
---
 gdb/ChangeLog             | 28 ++++++++++++++++++++++++++++
 gdb/python/py-arch.c      |  8 +++++++-
 gdb/python/py-block.c     | 17 +++++++++++------
 gdb/python/py-inferior.c  | 11 ++++++++---
 gdb/python/py-objfile.c   |  9 +++++++--
 gdb/python/py-progspace.c |  9 +++++++--
 gdb/python/py-registers.c | 11 ++++++++---
 gdb/python/py-symbol.c    | 15 ++++++++++-----
 gdb/python/py-symtab.c    | 23 ++++++++++++++---------
 gdb/python/py-type.c      | 11 ++++++++---
 gdb/python/py-unwind.c    | 23 ++++++++++++++---------
 gdb/python/python.c       |  8 +++-----
 12 files changed, 125 insertions(+), 48 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index c417e81b893..c3156a8d073 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,31 @@
+2021-04-28  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* python/py-arch.c (_initialize_py_arch): New function.
+	(gdbpy_initialize_arch): Move code to _initialize_py_arch.
+	* python/py-block.c (_initialize_py_block): New function.
+	(gdbpy_initialize_blocks): Move code to _initialize_py_block.
+	* python/py-inferior.c (_initialize_py_inferior): New function.
+	(gdbpy_initialize_inferior): Move code to _initialize_py_inferior.
+	* python/py-objfile.c (_initialize_py_objfile): New function.
+	(gdbpy_initialize_objfile): Move code to _initialize_py_objfile.
+	* python/py-progspace.c (_initialize_py_progspace): New function.
+	(gdbpy_initialize_pspace): Move code to _initialize_py_progspace.
+	* python/py-registers.c (_initialize_py_registers): New function.
+	(gdbpy_initialize_registers): Move code to
+	_initialize_py_registers.
+	* python/py-symbol.c (_initialize_py_symbol): New function.
+	(gdbpy_initialize_symbols): Move code to _initialize_py_symbol.
+	* python/py-symtab.c (_initialize_py_symtab): New function.
+	(gdbpy_initialize_symtabs): Move code to _initialize_py_symtab.
+	* python/py-type.c (_initialize_py_type): New function.
+	(gdbpy_initialize_types): Move code to _initialize_py_type.
+	* python/py-unwind.c (_initialize_py_unwind): New function.
+	(gdbpy_initialize_unwind): Move code to _initialize_py_unwind.
+	* python/python.c (_initialize_python): Move call to
+	do_start_initialization to gdbpy_finish_initialization.
+	(gdbpy_finish_initialization): Add call to
+	do_start_initialization.
+
 2021-04-28  Andrew Burgess  <andrew.burgess@embecosm.com>
 
 	* extension.c (struct scoped_default_signal): New struct.
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index d1960b4906e..66f2d28b94a 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -271,12 +271,18 @@ archpy_register_groups (PyObject *self, PyObject *args)
   return gdbpy_new_reggroup_iterator (gdbarch);
 }
 
+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
 gdbpy_initialize_arch (void)
 {
-  arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
   arch_object_type.tp_new = PyType_GenericNew;
   if (PyType_Ready (&arch_object_type) < 0)
     return -1;
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index d257545b8aa..244ff9a6bab 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -427,6 +427,17 @@ del_objfile_blocks (struct objfile *objfile, void *datum)
     }
 }
 
+void _initialize_py_block ();
+void
+_initialize_py_block ()
+{
+  /* Register an objfile "free" callback so we can properly
+     invalidate blocks when an object file is about to be
+     deleted.  */
+  blpy_objfile_data_key
+    = register_objfile_data_with_cleanup (NULL, del_objfile_blocks);
+}
+
 int
 gdbpy_initialize_blocks (void)
 {
@@ -438,12 +449,6 @@ gdbpy_initialize_blocks (void)
   if (PyType_Ready (&block_syms_iterator_object_type) < 0)
     return -1;
 
-  /* Register an objfile "free" callback so we can properly
-     invalidate blocks when an object file is about to be
-     deleted.  */
-  blpy_objfile_data_key
-    = register_objfile_data_with_cleanup (NULL, del_objfile_blocks);
-
   if (gdb_pymodule_addobject (gdb_module, "Block",
 			      (PyObject *) &block_object_type) < 0)
     return -1;
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index febd2a73ece..94c2c2329e9 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -892,6 +892,14 @@ gdbpy_selected_inferior (PyObject *self, PyObject *args)
 	  inferior_to_inferior_object (current_inferior ()).release ());
 }
 
+void _initialize_py_inferior ();
+void
+_initialize_py_inferior ()
+{
+  infpy_inf_data_key =
+    register_inferior_data_with_cleanup (NULL, py_free_inferior);
+}
+
 int
 gdbpy_initialize_inferior (void)
 {
@@ -902,9 +910,6 @@ gdbpy_initialize_inferior (void)
 			      (PyObject *) &inferior_object_type) < 0)
     return -1;
 
-  infpy_inf_data_key =
-    register_inferior_data_with_cleanup (NULL, py_free_inferior);
-
   gdb::observers::new_thread.attach (add_thread_object, "py-inferior");
   gdb::observers::thread_exit.attach (delete_thread_object, "py-inferior");
   gdb::observers::normal_stop.attach (python_on_normal_stop, "py-inferior");
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 8fb73820bb5..626f2d3e18c 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -693,12 +693,17 @@ objfile_to_objfile_object (struct objfile *objfile)
   return gdbpy_ref<>::new_reference (result);
 }
 
-int
-gdbpy_initialize_objfile (void)
+void _initialize_py_objfile ();
+void
+_initialize_py_objfile ()
 {
   objfpy_objfile_data_key
     = register_objfile_data_with_cleanup (NULL, py_free_objfile);
+}
 
+int
+gdbpy_initialize_objfile (void)
+{
   if (PyType_Ready (&objfile_object_type) < 0)
     return -1;
 
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index 011c6cc6c36..d8df9c31d80 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -504,12 +504,17 @@ pspace_to_pspace_object (struct program_space *pspace)
   return gdbpy_ref<>::new_reference (result);
 }
 
-int
-gdbpy_initialize_pspace (void)
+void _initialize_py_progspace ();
+void
+_initialize_py_progspace ()
 {
   pspy_pspace_data_key
     = register_program_space_data_with_cleanup (NULL, py_free_pspace);
+}
 
+int
+gdbpy_initialize_pspace (void)
+{
   if (PyType_Ready (&pspace_object_type) < 0)
     return -1;
 
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index eb59a499f33..04e554f48e1 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -423,14 +423,19 @@ 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
 gdbpy_initialize_registers ()
 {
-  gdbpy_register_object_data
-    = gdbarch_data_register_post_init (gdbpy_register_object_data_init);
-
   register_descriptor_object_type.tp_new = PyType_GenericNew;
   if (PyType_Ready (&register_descriptor_object_type) < 0)
     return -1;
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index ead26d5d441..8953ee097cc 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -619,17 +619,22 @@ del_objfile_symbols (struct objfile *objfile, void *datum)
     }
 }
 
-int
-gdbpy_initialize_symbols (void)
+void _initialize_py_symbol ();
+void
+_initialize_py_symbol ()
 {
-  if (PyType_Ready (&symbol_object_type) < 0)
-    return -1;
-
   /* Register an objfile "free" callback so we can properly
      invalidate symbol when an object file that is about to be
      deleted.  */
   sympy_objfile_data_key
     = register_objfile_data_with_cleanup (NULL, del_objfile_symbols);
+}
+
+int
+gdbpy_initialize_symbols (void)
+{
+  if (PyType_Ready (&symbol_object_type) < 0)
+    return -1;
 
   if (PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_UNDEF", LOC_UNDEF) < 0
       || PyModule_AddIntConstant (gdb_module, "SYMBOL_LOC_CONST",
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index f0bf4ef5911..e9013731c4b 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -511,6 +511,20 @@ del_objfile_sal (struct objfile *objfile, void *datum)
     }
 }
 
+void _initialize_py_symtab ();
+void
+_initialize_py_symtab ()
+{
+  /* Register an objfile "free" callback so we can properly
+     invalidate symbol tables, and symbol table and line data
+     structures when an object file that is about to be
+     deleted.  */
+  stpy_objfile_data_key
+    = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
+  salpy_objfile_data_key
+    = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
+}
+
 int
 gdbpy_initialize_symtabs (void)
 {
@@ -522,15 +536,6 @@ gdbpy_initialize_symtabs (void)
   if (PyType_Ready (&sal_object_type) < 0)
     return -1;
 
-  /* Register an objfile "free" callback so we can properly
-     invalidate symbol tables, and symbol table and line data
-     structures when an object file that is about to be
-     deleted.  */
-  stpy_objfile_data_key
-    = register_objfile_data_with_cleanup (NULL, del_objfile_symtab);
-  salpy_objfile_data_key
-    = register_objfile_data_with_cleanup (NULL, del_objfile_sal);
-
   if (gdb_pymodule_addobject (gdb_module, "Symtab",
 			      (PyObject *) &symtab_object_type) < 0)
     return -1;
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 148e4a6aa3a..4f5f42529c2 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -1424,14 +1424,19 @@ gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
   return type_to_type_object (type);
 }
 
+void _initialize_py_type ();
+void
+_initialize_py_type ()
+{
+  typy_objfile_data_key
+    = register_objfile_data_with_cleanup (save_objfile_types, NULL);
+}
+
 int
 gdbpy_initialize_types (void)
 {
   int i;
 
-  typy_objfile_data_key
-    = register_objfile_data_with_cleanup (save_objfile_types, NULL);
-
   if (PyType_Ready (&type_object_type) < 0)
     return -1;
   if (PyType_Ready (&field_object_type) < 0)
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 5dc8d33f0dc..4b25c485b8c 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -614,12 +614,10 @@ pyuw_on_new_gdbarch (struct gdbarch *newarch)
     }
 }
 
-/* Initialize unwind machinery.  */
-
-int
-gdbpy_initialize_unwind (void)
+void _initialize_py_unwind ();
+void
+_initialize_py_unwind ()
 {
-  int rc;
   add_setshow_zuinteger_cmd
       ("py-unwind", class_maintenance, &pyuw_debug,
 	_("Set Python unwinder debugging."),
@@ -629,15 +627,22 @@ gdbpy_initialize_unwind (void)
 	NULL,
 	&setdebuglist, &showdebuglist);
   pyuw_gdbarch_data
-      = gdbarch_data_register_post_init (pyuw_gdbarch_data_init);
+    = gdbarch_data_register_post_init (pyuw_gdbarch_data_init);
+}
+
+/* Initialize unwind machinery.  */
+
+int
+gdbpy_initialize_unwind (void)
+{
   gdb::observers::architecture_changed.attach (pyuw_on_new_gdbarch,
 					       "py-unwind");
 
   if (PyType_Ready (&pending_frame_object_type) < 0)
     return -1;
-  rc = gdb_pymodule_addobject (gdb_module, "PendingFrame",
-      (PyObject *) &pending_frame_object_type);
-  if (rc)
+  int rc = gdb_pymodule_addobject (gdb_module, "PendingFrame",
+				   (PyObject *) &pending_frame_object_type);
+  if (rc != 0)
     return rc;
 
   if (PyType_Ready (&unwind_info_object_type) < 0)
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9eed258c181..520508043ba 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1881,11 +1881,6 @@ message == an error message without a stack will be printed."),
 			NULL, NULL,
 			&user_set_python_list,
 			&user_show_python_list);
-
-#ifdef HAVE_PYTHON
-  if (!do_start_initialization () && PyErr_Occurred ())
-    gdbpy_print_stack ();
-#endif /* HAVE_PYTHON */
 }
 
 #ifdef HAVE_PYTHON
@@ -1962,6 +1957,9 @@ do_finish_initialization (const struct extension_language_defn *extlang)
 static void
 gdbpy_finish_initialization (const struct extension_language_defn *extlang)
 {
+  if (!do_start_initialization () && PyErr_Occurred ())
+    gdbpy_print_stack ();
+
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
   if (!do_finish_initialization (extlang))


More information about the Gdb-cvs mailing list