public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* RFC: fix atexit.register
@ 2012-09-06 15:43 Tom Tromey
  2012-09-06 16:13 ` Eli Zaretskii
  2012-09-10 19:40 ` Tom Tromey
  0 siblings, 2 replies; 7+ messages in thread
From: Tom Tromey @ 2012-09-06 15:43 UTC (permalink / raw)
  To: gdb-patches

A user on irc reported that Python's atexit.register did not work
properly in gdb.

This patch fixes the problem by installing a final cleanup that calls
Py_Finalize.

Regression tested on x86-64 F16.
New test case included.

Tom

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

	* gdb.python/python.exp: Test atexit.register.
---
 gdb/NEWS                            |    2 ++
 gdb/python/python.c                 |   14 ++++++++++++++
 gdb/testsuite/gdb.python/python.exp |   18 ++++++++++++++++++
 3 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index e625e25..9571bcf 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -10,6 +10,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 cb60877..a75a421 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1396,6 +1396,19 @@ 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)
+{
+  struct cleanup *cleanup = ensure_python_env (target_gdbarch,
+					       current_language);
+
+  Py_Finalize ();
+  do_cleanups (cleanup);
+}
+
 /* Provide a prototype to silence -Wmissing-prototypes.  */
 extern initialize_file_ftype _initialize_python;
 
@@ -1573,6 +1586,7 @@ gdb.type_printers = []\n\
   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}
 
-- 
1.7.7.6

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFC: fix atexit.register
  2012-09-06 15:43 RFC: fix atexit.register Tom Tromey
@ 2012-09-06 16:13 ` Eli Zaretskii
  2012-09-10 19:40 ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Eli Zaretskii @ 2012-09-06 16:13 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> From: Tom Tromey <tromey@redhat.com>
> Date: Thu, 06 Sep 2012 09:43:26 -0600
> 
> A user on irc reported that Python's atexit.register did not work
> properly in gdb.
> 
> This patch fixes the problem by installing a final cleanup that calls
> Py_Finalize.
> 
> Regression tested on x86-64 F16.
> New test case included.
> 
> Tom
> 
> 	* NEWS: Update.
> 	* python/python.c (finalize_python): New function.
> 	(_initialize_python): Make a final cleanup.
> 
> 	* gdb.python/python.exp: Test atexit.register.

OK for the NEWS part.

Thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFC: fix atexit.register
  2012-09-06 15:43 RFC: fix atexit.register Tom Tromey
  2012-09-06 16:13 ` Eli Zaretskii
@ 2012-09-10 19:40 ` Tom Tromey
  2012-09-10 19:52   ` Eli Zaretskii
                     ` (2 more replies)
  1 sibling, 3 replies; 7+ messages in thread
From: Tom Tromey @ 2012-09-10 19:40 UTC (permalink / raw)
  To: gdb-patches

>>>>> "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}
 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFC: fix atexit.register
  2012-09-10 19:40 ` Tom Tromey
@ 2012-09-10 19:52   ` Eli Zaretskii
  2012-09-20 20:46   ` Tom Tromey
  2012-11-29 19:13   ` [commit] Fix !HAVE_THREAD Python build error (Re: RFC: fix atexit.register) Ulrich Weigand
  2 siblings, 0 replies; 7+ messages in thread
From: Eli Zaretskii @ 2012-09-10 19:52 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> From: Tom Tromey <tromey@redhat.com>
> Date: Mon, 10 Sep 2012 13:40:12 -0600
> 
> 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)

OK for this part, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: RFC: fix atexit.register
  2012-09-10 19:40 ` Tom Tromey
  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
  2 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2012-09-20 20:46 UTC (permalink / raw)
  To: gdb-patches

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

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

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

I'm checking this in.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Regression for build --without-python  [Re: RFC: fix atexit.register]
  2012-09-20 20:46   ` Tom Tromey
@ 2012-09-21  7:05     ` Jan Kratochvil
  0 siblings, 0 replies; 7+ messages in thread
From: Jan Kratochvil @ 2012-09-21  7:05 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On Thu, 20 Sep 2012 22:45:56 +0200, Tom Tromey wrote:
> >>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:
> 
> Tom> 	* NEWS: Update.
> Tom> 	* python/python.c (finalize_python): New function.
> Tom> 	(_initialize_python): Make a final cleanup.
> 
> Tom> 	* gdb.python/python.exp: Test atexit.register.
> 
> I'm checking this in.

./python/python.c: In function ‘finalize_python’:
./python/python.c:1280:3: warning: implicit declaration of function ‘PyGILState_Ensure’ [-Wimplicit-function-declaration]
./python/python.c:1281:3: error: ‘python_gdbarch’ undeclared (first use in this function)
./python/python.c:1281:3: note: each undeclared identifier is reported only once for each function it appears in
./python/python.c:1282:3: error: ‘python_language’ undeclared (first use in this function)
./python/python.c:1284:3: warning: implicit declaration of function ‘Py_Finalize’ [-Wimplicit-function-declaration]
./python/python.c: At top level:
./python/python.c:1273:1: warning: ‘finalize_python’ defined but not used [-Wunused-function]


Regards,
Jan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [commit] Fix !HAVE_THREAD Python build error (Re: RFC: fix atexit.register)
  2012-09-10 19:40 ` Tom Tromey
  2012-09-10 19:52   ` Eli Zaretskii
  2012-09-20 20:46   ` Tom Tromey
@ 2012-11-29 19:13   ` Ulrich Weigand
  2 siblings, 0 replies; 7+ messages in thread
From: Ulrich Weigand @ 2012-11-29 19:13 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Tom Tromey wrote:

> +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 ();
> +}

If WITH_THREAD is not defined, we use this definition from python-internal.h:

#define PyGILState_Ensure() ((PyGILState_STATE) 0)

This causes build to abort for me with the error:

/home/uweigand/fsf/gdb-head/gdb/python/python.c: In function 'finalize_python':
/home/uweigand/fsf/gdb-head/gdb/python/python.c:1418: warning: statement with no effect

Fixed by the following patch, committed as obvious.

Bye,
Ulrich

ChangeLog:

	* python/python.c (finalize_python): Cast unused PyGILState_Ensure
	return value to void to avoid compiler warning.

Index: gdb/python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.101
diff -u -p -r1.101 python.c
--- gdb/python/python.c	12 Nov 2012 19:24:14 -0000	1.101
+++ gdb/python/python.c	29 Nov 2012 18:58:30 -0000
@@ -1415,7 +1415,7 @@ finalize_python (void *ignore)
      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 ();
+  (void) PyGILState_Ensure ();
   python_gdbarch = target_gdbarch ();
   python_language = current_language;
 

-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-11-29 19:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-06 15:43 RFC: fix atexit.register Tom Tromey
2012-09-06 16:13 ` Eli Zaretskii
2012-09-10 19:40 ` Tom Tromey
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

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).