Blame SOURCES/gdb-python-gil.patch

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