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