[3/7] gdb: delay python initialisation until gdbpy_finish_initialization

Message ID 222490f5330bee284b46010719040df190b7631a.1619125261.git.andrew.burgess@embecosm.com
State New
Headers show
Series
  • New options to control how Python is initialized
Related show

Commit Message

Andrew Burgess April 22, 2021, 9:02 p.m.
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.
---
 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(-)

-- 
2.25.4

Comments

Tom Tromey April 23, 2021, 2:26 p.m. | #1
>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:


Andrew> Delay Python initialisation until gdbpy_finish_initialization.
Andrew> This is mostly about splitting the existing gdbpy_initialize_*
Andrew> functions in two, all the calls to register_objfile_data_with_cleanup,
Andrew> gdbarch_data_register_post_init, etc are moved into new _initialize_*
Andrew> functions, but everything else is left in the gdbpy_initialize_*
Andrew> functions.

Andrew> Then the call to do_start_initialization (in python/python.c) is moved
Andrew> from the _initialize_python function into gdbpy_finish_initialization.

Looks good, thank you.

Tom

Patch

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 a3d5952a10b..3b6c99f3f58 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -891,6 +891,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)
 {
@@ -901,9 +909,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);
   gdb::observers::thread_exit.attach (delete_thread_object);
   gdb::observers::normal_stop.attach (python_on_normal_stop);
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 bf707078841..3741fd26d1f 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -1417,14 +1417,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 f0574eba610..625b8902f5d 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,14 +627,21 @@  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);
 
   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))