public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] Add extension_language_ops::shutdown
Date: Fri, 23 Feb 2024 14:11:38 -0700	[thread overview]
Message-ID: <20240223-final-cleanups-v1-2-84d5271e9979@adacore.com> (raw)
In-Reply-To: <20240223-final-cleanups-v1-0-84d5271e9979@adacore.com>

Right now, Python is shut down via a final cleanup.  However, it seems
to me that it is better for extension languages to be shut down
explicitly, after all the ordinary final cleanups are run.  The main
reason for this is that a subsequent patch adds another case like
finalize_values; and rather than add a series of workarounds for
Python shutdown, it seemed better to let these be done via final
cleanups, and then have Python shutdown itself be the special case.
---
 gdb/extension-priv.h |  4 ++++
 gdb/extension.c      | 12 ++++++++++++
 gdb/extension.h      |  3 +++
 gdb/guile/guile.c    |  1 +
 gdb/python/python.c  |  6 +++---
 gdb/top.c            |  2 ++
 6 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/gdb/extension-priv.h b/gdb/extension-priv.h
index b709494927d..cb00cb6ff7b 100644
--- a/gdb/extension-priv.h
+++ b/gdb/extension-priv.h
@@ -119,6 +119,10 @@ struct extension_language_ops
      This method is required.  */
   int (*initialized) (const struct extension_language_defn *);
 
+  /* Called just before GDB exits.  This shuts down the extension
+     language.  This can be NULL.  */
+  void (*shutdown) (const struct extension_language_defn *);
+
   /* Process a sequence of commands embedded in GDB's own scripting language.
      E.g.,
      python
diff --git a/gdb/extension.c b/gdb/extension.c
index 42e05199d2c..9f403500497 100644
--- a/gdb/extension.c
+++ b/gdb/extension.c
@@ -341,6 +341,18 @@ ext_lang_initialization (void)
     }
 }
 
+/* See extension.h.  */
+
+void
+ext_lang_shutdown ()
+{
+  for (const struct extension_language_defn *extlang : extension_languages)
+    {
+      if (extlang->ops != nullptr && extlang->ops->shutdown != nullptr)
+	extlang->ops->shutdown (extlang);
+    }
+}
+
 /* Invoke the appropriate extension_language_ops.eval_from_control_command
    method to perform CMD, which is a list of commands in an extension language.
 
diff --git a/gdb/extension.h b/gdb/extension.h
index 0514d7930a2..5260bcbde00 100644
--- a/gdb/extension.h
+++ b/gdb/extension.h
@@ -282,6 +282,9 @@ extern bool ext_lang_auto_load_enabled (const struct extension_language_defn *);
 
 extern void ext_lang_initialization (void);
 
+/* Shut down all extension languages.  */
+extern void ext_lang_shutdown ();
+
 extern void eval_ext_lang_from_control_command (struct command_line *cmd);
 
 extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *);
diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c
index 30d1c72fceb..f0db709e4fe 100644
--- a/gdb/guile/guile.c
+++ b/gdb/guile/guile.c
@@ -115,6 +115,7 @@ static const struct extension_language_ops guile_extension_ops =
 {
   gdbscm_initialize,
   gdbscm_initialized,
+  nullptr,
 
   gdbscm_eval_from_control_command,
 
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 971fc850dbb..dde8afc3c65 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -110,6 +110,7 @@ static objfile_script_sourcer_func gdbpy_source_objfile_script;
 static objfile_script_executor_func gdbpy_execute_objfile_script;
 static void gdbpy_initialize (const struct extension_language_defn *);
 static int gdbpy_initialized (const struct extension_language_defn *);
+static void finalize_python (const struct extension_language_defn *);
 static void gdbpy_eval_from_control_command
   (const struct extension_language_defn *, struct command_line *cmd);
 static void gdbpy_start_type_printers (const struct extension_language_defn *,
@@ -147,6 +148,7 @@ static const struct extension_language_ops python_extension_ops =
 {
   gdbpy_initialize,
   gdbpy_initialized,
+  finalize_python,
 
   gdbpy_eval_from_control_command,
 
@@ -2070,7 +2072,7 @@ static struct cmd_list_element *user_show_python_list;
    interpreter.  This lets Python's 'atexit' work.  */
 
 static void
-finalize_python ()
+finalize_python (const struct extension_language_defn *ignore)
 {
   struct active_ext_lang_state *previous_active;
 
@@ -2310,8 +2312,6 @@ do_start_initialization ()
   /* Release the GIL while gdb runs.  */
   PyEval_SaveThread ();
 
-  add_final_cleanup (finalize_python);
-
   /* Only set this when initialization has succeeded.  */
   gdb_python_initialized = 1;
   return true;
diff --git a/gdb/top.c b/gdb/top.c
index 5114713baa4..67d6670cd9c 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1819,6 +1819,8 @@ quit_force (int *exit_arg, int from_tty)
       exception_print (gdb_stderr, ex);
     }
 
+  ext_lang_shutdown ();
+
   exit (exit_code);
 }
 

-- 
2.43.0


  parent reply	other threads:[~2024-02-23 21:11 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-23 21:11 [PATCH 0/5] Restore DAP 'quit' request Tom Tromey
2024-02-23 21:11 ` [PATCH 1/5] Rewrite final cleanups Tom Tromey
2024-02-25 22:30   ` Lancelot SIX
2024-02-26 18:53     ` Tom Tromey
2024-02-27 14:03       ` Lancelot SIX
2024-02-27 17:27         ` Tom Tromey
2024-02-27 17:36           ` Tom Tromey
2024-03-03 16:50           ` Lancelot SIX
2024-02-23 21:11 ` Tom Tromey [this message]
2024-02-23 21:11 ` [PATCH 3/5] Change finalize_values into a final cleanup Tom Tromey
2024-02-23 21:11 ` [PATCH 4/5] Add final cleanup for runnables Tom Tromey
2024-02-23 21:11 ` [PATCH 5/5] Explicitly quit gdb from DAP server thread Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240223-final-cleanups-v1-2-84d5271e9979@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).