Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@redhat.com>
To: gdb-patches@sourceware.org
Subject: Re: RFC: fix atexit.register
Date: Mon, 10 Sep 2012 19:40:00 -0000	[thread overview]
Message-ID: <874nn5d5ur.fsf@fleche.redhat.com> (raw)
In-Reply-To: <87d31znom9.fsf@fleche.redhat.com> (Tom Tromey's message of "Thu,	06 Sep 2012 09:43:26 -0600")

>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:

Tom> Regression tested on x86-64 F16.

I found out something scary after posting this: if gdb crashes during
exit, the entire test suite will pass.

This patch fixes the bug and also doesn't SEGV.

I'm looking into writing a test case for a clean exit.

Tom

	* NEWS: Update.
	* python/python.c (finalize_python): New function.
	(_initialize_python): Make a final cleanup.

	* gdb.python/python.exp: Test atexit.register.

diff --git a/gdb/NEWS b/gdb/NEWS
index dba6937..933d6f1 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -7,6 +7,8 @@
 
   ** Vectors can be created with gdb.Type.vector.
 
+  ** Python's atexit.register now works in GDB.
+
 * New Python-based convenience functions:
 
   ** $_memeq(buf1, buf2, length)
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 482f53e..59ba5b7 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1264,6 +1264,24 @@ user_show_python (char *args, int from_tty)
 
 /* Initialize the Python code.  */
 
+/* This is installed as a final cleanup and cleans up the
+   interpreter.  This lets Python's 'atexit' work.  */
+
+static void
+finalize_python (void *ignore)
+{
+  /* We don't use ensure_python_env here because if we ever ran the
+     cleanup, gdb would crash -- because the cleanup calls into the
+     Python interpreter, which we are about to destroy.  It seems
+     clearer to make the needed calls explicitly here than to create a
+     cleanup and then mysteriously discard it.  */
+  PyGILState_Ensure ();
+  python_gdbarch = target_gdbarch;
+  python_language = current_language;
+
+  Py_Finalize ();
+}
+
 /* Provide a prototype to silence -Wmissing-prototypes.  */
 extern initialize_file_ftype _initialize_python;
 
@@ -1439,6 +1457,7 @@ message == an error message without a stack will be printed."),
   PyThreadState_Swap (NULL);
   PyEval_ReleaseLock ();
 
+  make_final_cleanup (finalize_python, NULL);
 #endif /* HAVE_PYTHON */
 }
 
diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp
index 9683b1c..177f94e 100644
--- a/gdb/testsuite/gdb.python/python.exp
+++ b/gdb/testsuite/gdb.python/python.exp
@@ -141,6 +141,24 @@ gdb_test "python print a" ".*aliases -- Aliases of other commands.*" "verify hel
 gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
 gdb_test "python print nothread == None" "True" "Ensure that no threads are returned"
 
+gdb_py_test_multiple "register atexit function" \
+    "python" "" \
+    "import atexit" "" \
+    "def printit(arg):" "" \
+    "  print arg" "" \
+    "atexit.register(printit, 'good bye world')" "" \
+    "end" ""
+
+send_gdb "quit\n"
+gdb_expect {
+    -re "good bye world" {
+	pass "atexit handling"
+    }
+    default {
+	fail "atexit handling"
+    }
+}
+
 # Start with a fresh gdb.
 clean_restart ${testfile}
 


  parent reply	other threads:[~2012-09-10 19:40 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-06 15:43 Tom Tromey
2012-09-06 16:13 ` Eli Zaretskii
2012-09-10 19:40 ` Tom Tromey [this message]
2012-09-10 19:52   ` Eli Zaretskii
2012-09-20 20:46   ` Tom Tromey
2012-09-21  7:05     ` Regression for build --without-python [Re: RFC: fix atexit.register] Jan Kratochvil
2012-11-29 19:13   ` [commit] Fix !HAVE_THREAD Python build error (Re: RFC: fix atexit.register) Ulrich Weigand

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=874nn5d5ur.fsf@fleche.redhat.com \
    --to=tromey@redhat.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