Blame SOURCES/gdb-python-gil.patch

26bbde
Index: gdb-7.9.50.20150520/gdb/doc/python.texi
26bbde
===================================================================
26bbde
--- gdb-7.9.50.20150520.orig/gdb/doc/python.texi	2015-05-31 17:57:12.431593983 +0200
26bbde
+++ gdb-7.9.50.20150520/gdb/doc/python.texi	2015-05-31 17:57:14.654607708 +0200
26bbde
@@ -229,6 +229,14 @@ returned as a string.  The default is @c
26bbde
 return value is @code{None}.  If @var{to_string} is @code{True}, the
26bbde
 @value{GDBN} virtual terminal will be temporarily set to unlimited width
26bbde
 and height, and its pagination will be disabled; @pxref{Screen Size}.
26bbde
+
26bbde
+The @var{release_gil} flag specifies whether @value{GDBN} ought to
26bbde
+release the Python GIL before executing the command.  This is useful
26bbde
+in multi-threaded Python programs where by default the Python
26bbde
+interpreter will acquire the GIL and lock other threads from
26bbde
+executing.  After the command has completed executing in @value{GDBN}
26bbde
+the Python GIL is reacquired. This flag must be a boolean value.  If
26bbde
+omitted, it defaults to @code{False}.
26bbde
 @end defun
26bbde
 
26bbde
 @findex gdb.breakpoints
26bbde
Index: gdb-7.9.50.20150520/gdb/python/python-internal.h
26bbde
===================================================================
26bbde
--- gdb-7.9.50.20150520.orig/gdb/python/python-internal.h	2015-05-31 17:57:12.432593989 +0200
26bbde
+++ gdb-7.9.50.20150520/gdb/python/python-internal.h	2015-05-31 17:57:14.654607708 +0200
26bbde
@@ -142,6 +142,8 @@ typedef int Py_ssize_t;
26bbde
 #define PyGILState_Release(ARG) ((void)(ARG))
26bbde
 #define PyEval_InitThreads()
26bbde
 #define PyThreadState_Swap(ARG) ((void)(ARG))
26bbde
+#define PyEval_SaveThread() ((void)(ARG))
26bbde
+#define PyEval_RestoreThread(ARG) ((void)(ARG))
26bbde
 #define PyEval_ReleaseLock()
26bbde
 #endif
26bbde
 
26bbde
Index: gdb-7.9.50.20150520/gdb/python/python.c
26bbde
===================================================================
26bbde
--- gdb-7.9.50.20150520.orig/gdb/python/python.c	2015-05-31 17:57:14.656607720 +0200
26bbde
+++ gdb-7.9.50.20150520/gdb/python/python.c	2015-05-31 18:02:08.891424227 +0200
26bbde
@@ -627,13 +627,18 @@ execute_gdb_command (PyObject *self, PyO
26bbde
 {
26bbde
   const char *arg;
26bbde
   PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
26bbde
-  int from_tty, to_string;
26bbde
-  static char *keywords[] = {"command", "from_tty", "to_string", NULL };
26bbde
+  PyObject *release_gil_obj = NULL;
26bbde
+  int from_tty, to_string, release_gil;
26bbde
+  static char *keywords[] = {"command", "from_tty", "to_string",
26bbde
+			     "release_gil", NULL };
26bbde
   char *result = NULL;
26bbde
+  /* Initialize it just to avoid a GCC false warning.  */
26bbde
+  PyThreadState *state = NULL;
26bbde
 
26bbde
-  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
26bbde
+  if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!O!", keywords, &arg,
26bbde
 				     &PyBool_Type, &from_tty_obj,
26bbde
-				     &PyBool_Type, &to_string_obj))
26bbde
+				     &PyBool_Type, &to_string_obj,
26bbde
+				     &PyBool_Type, &release_gil_obj))
26bbde
     return NULL;
26bbde
 
26bbde
   from_tty = 0;
26bbde
@@ -654,12 +659,28 @@ execute_gdb_command (PyObject *self, PyO
26bbde
       to_string = cmp;
26bbde
     }
26bbde
 
26bbde
+  release_gil = 0;
26bbde
+  if (release_gil_obj)
26bbde
+    {
26bbde
+      int cmp = PyObject_IsTrue (release_gil_obj);
26bbde
+      if (cmp < 0)
26bbde
+	return NULL;
26bbde
+      release_gil = cmp;
26bbde
+    }
26bbde
+
26bbde
   TRY
26bbde
     {
26bbde
       /* Copy the argument text in case the command modifies it.  */
26bbde
       char *copy = xstrdup (arg);
26bbde
       struct cleanup *cleanup = make_cleanup (xfree, copy);
26bbde
 
26bbde
+      /* In the case of long running GDB commands, allow the user to
26bbde
+	 release the Python GIL acquired by Python.  Restore the GIL
26bbde
+	 after the command has completed before handing back to
26bbde
+	 Python.  */
26bbde
+      if (release_gil)
26bbde
+	state = PyEval_SaveThread();
26bbde
+
26bbde
       make_cleanup_restore_integer (&interpreter_async);
26bbde
       interpreter_async = 0;
26bbde
 
26bbde
@@ -672,11 +693,23 @@ execute_gdb_command (PyObject *self, PyO
26bbde
 	  execute_command (copy, from_tty);
26bbde
 	}
26bbde
 
26bbde
+      /* Reacquire the GIL if it was released earlier.  */
26bbde
+      if (release_gil)
26bbde
+	PyEval_RestoreThread (state);
26bbde
+
26bbde
       do_cleanups (cleanup);
26bbde
     }
26bbde
   CATCH (except, RETURN_MASK_ALL)
26bbde
     {
26bbde
-      GDB_PY_HANDLE_EXCEPTION (except);
26bbde
+      if (except.reason < 0)
26bbde
+	{
26bbde
+	  /* Reacquire the GIL if it was released earlier.  */
26bbde
+	  if (release_gil)
26bbde
+	    PyEval_RestoreThread (state);
26bbde
+
26bbde
+	  gdbpy_convert_exception (except);
26bbde
+	  return NULL;
26bbde
+	}
26bbde
     }
26bbde
   END_CATCH
26bbde
 
26bbde
Index: gdb-7.9.50.20150520/gdb/testsuite/gdb.python/py-gil-mthread.c
26bbde
===================================================================
26bbde
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
26bbde
+++ gdb-7.9.50.20150520/gdb/testsuite/gdb.python/py-gil-mthread.c	2015-05-31 17:57:14.656607720 +0200
26bbde
@@ -0,0 +1,12 @@
26bbde
+#include <stdio.h>
26bbde
+
26bbde
+int
26bbde
+main (void)
26bbde
+{
26bbde
+  int i;
26bbde
+  for (i = 0; i < 10; i++)
26bbde
+    {
26bbde
+      sleep (1); /* break-here */
26bbde
+      printf ("Sleeping %d\n", i);
26bbde
+    }
26bbde
+}
26bbde
Index: gdb-7.9.50.20150520/gdb/testsuite/gdb.python/py-gil-mthread.exp
26bbde
===================================================================
26bbde
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
26bbde
+++ gdb-7.9.50.20150520/gdb/testsuite/gdb.python/py-gil-mthread.exp	2015-05-31 17:57:14.657607726 +0200
26bbde
@@ -0,0 +1,69 @@
26bbde
+# Copyright (C) 2014 Free Software Foundation, Inc.
26bbde
+
26bbde
+# This program is free software; you can redistribute it and/or modify
26bbde
+# it under the terms of the GNU General Public License as published by
26bbde
+# the Free Software Foundation; either version 3 of the License, or
26bbde
+# (at your option) any later version.
26bbde
+#
26bbde
+# This program is distributed in the hope that it will be useful,
26bbde
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
26bbde
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26bbde
+# GNU General Public License for more details.
26bbde
+#
26bbde
+# You should have received a copy of the GNU General Public License
26bbde
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
26bbde
+
26bbde
+standard_testfile .c .py
26bbde
+set executable $testfile
26bbde
+
26bbde
+if { [prepare_for_testing $testfile.exp $executable $srcfile] } {
26bbde
+    return -1
26bbde
+}
26bbde
+
26bbde
+# Skip all tests if Python scripting is not enabled.
26bbde
+if { [skip_python_tests] } { continue }
26bbde
+
26bbde
+if ![runto_main] {
26bbde
+    return -1
26bbde
+}
26bbde
+
26bbde
+gdb_breakpoint $srcfile:[gdb_get_line_number "break-here"] temporary
26bbde
+gdb_continue_to_breakpoint "break-here" ".* break-here .*"
26bbde
+
26bbde
+set test "response"
26bbde
+set timeout 60
26bbde
+set sleeping_last -1
26bbde
+set hello_last 0
26bbde
+set minimal 5
26bbde
+gdb_test_multiple "python execfile('$srcdir/$subdir/$srcfile2')" $test {
26bbde
+    -re "Error: unable to start thread\r\n" {
26bbde
+	fail $test
26bbde
+	# Not $gdb_prompt-synced!
26bbde
+    }
26bbde
+    -re "Sleeping (\[0-9\]+)\r\n" {
26bbde
+	set n $expect_out(1,string)
26bbde
+	if { $sleeping_last + 1 != $n } {
26bbde
+	    fail $test
26bbde
+	} else {
26bbde
+	    set sleeping_last $n
26bbde
+	    if { $sleeping_last >= $minimal && $hello_last >= $minimal } {
26bbde
+		pass $test
26bbde
+	    } else {
26bbde
+		exp_continue
26bbde
+	    }
26bbde
+	}
26bbde
+    }
26bbde
+    -re "Hello \\( (\[0-9\]+) \\)\r\n" {
26bbde
+	set n $expect_out(1,string)
26bbde
+	if { $hello_last + 1 != $n } {
26bbde
+	    fail $test
26bbde
+	} else {
26bbde
+	    set hello_last $n
26bbde
+	    if { $sleeping_last >= $minimal && $hello_last >= $minimal } {
26bbde
+		pass $test
26bbde
+	    } else {
26bbde
+		exp_continue
26bbde
+	    }
26bbde
+	}
26bbde
+    }
26bbde
+}
26bbde
Index: gdb-7.9.50.20150520/gdb/testsuite/gdb.python/py-gil-mthread.py
26bbde
===================================================================
26bbde
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
26bbde
+++ gdb-7.9.50.20150520/gdb/testsuite/gdb.python/py-gil-mthread.py	2015-05-31 17:57:14.657607726 +0200
26bbde
@@ -0,0 +1,22 @@
26bbde
+import thread
26bbde
+import time
26bbde
+import gdb
26bbde
+
26bbde
+# Define a function for the thread
26bbde
+def print_thread_hello():
26bbde
+   count = 0
26bbde
+   while count < 10:
26bbde
+      time.sleep(1)
26bbde
+      count += 1
26bbde
+      print "Hello (", count, ")"
26bbde
+
26bbde
+# Create a threads a continue
26bbde
+try:
26bbde
+   thread.start_new_thread( print_thread_hello, ())
26bbde
+   gdb.execute ("continue", release_gil=True)
26bbde
+   
26bbde
+except:
26bbde
+   print "Error: unable to start thread"
26bbde
+
26bbde
+while 1:
26bbde
+   pass