Blame SOURCES/gdb-python-gil.patch

689258
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
689258
From: Fedora GDB patches <invalid@email.com>
689258
Date: Fri, 27 Oct 2017 21:07:50 +0200
689258
Subject: gdb-python-gil.patch
689258
689258
;; Fix Python GIL with gdb.execute("continue") (Phil Muldoon, BZ 1116957).
689258
;;=push
689258
689258
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
689258
--- a/gdb/doc/python.texi
689258
+++ b/gdb/doc/python.texi
689258
@@ -232,6 +232,14 @@ returned as a string.  The default is @code{False}, in which case the
689258
 return value is @code{None}.  If @var{to_string} is @code{True}, the
689258
 @value{GDBN} virtual terminal will be temporarily set to unlimited width
689258
 and height, and its pagination will be disabled; @pxref{Screen Size}.
689258
+
689258
+The @var{release_gil} flag specifies whether @value{GDBN} ought to
689258
+release the Python GIL before executing the command.  This is useful
689258
+in multi-threaded Python programs where by default the Python
689258
+interpreter will acquire the GIL and lock other threads from
689258
+executing.  After the command has completed executing in @value{GDBN}
689258
+the Python GIL is reacquired. This flag must be a boolean value.  If
689258
+omitted, it defaults to @code{False}.
689258
 @end defun
689258
 
689258
 @findex gdb.breakpoints
689258
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
689258
--- a/gdb/python/python-internal.h
689258
+++ b/gdb/python/python-internal.h
689258
@@ -148,6 +148,8 @@ typedef int Py_ssize_t;
689258
 #define PyGILState_Release(ARG) ((void)(ARG))
689258
 #define PyEval_InitThreads()
689258
 #define PyThreadState_Swap(ARG) ((void)(ARG))
689258
+#define PyEval_SaveThread() ((void)(ARG))
689258
+#define PyEval_RestoreThread(ARG) ((void)(ARG))
689258
 #define PyEval_ReleaseLock()
689258
 #endif
689258
 
689258
diff --git a/gdb/python/python.c b/gdb/python/python.c
689258
--- a/gdb/python/python.c
689258
+++ b/gdb/python/python.c
689258
@@ -556,12 +556,16 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
689258
 {
689258
   const char *arg;
689258
   PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
689258
-  int from_tty, to_string;
689258
-  static const char *keywords[] = { "command", "from_tty", "to_string", NULL };
689258
+  int from_tty, to_string, release_gil;
689258
+  static const char *keywords[] = {"command", "from_tty", "to_string", "release_gil", NULL };
689258
+  PyObject *release_gil_obj = NULL;
689258
+  /* Initialize it just to avoid a GCC false warning.  */
689258
+  PyThreadState *state = NULL;
689258
 
689258
-  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
689258
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!O!", keywords, &arg,
689258
 					&PyBool_Type, &from_tty_obj,
689258
-					&PyBool_Type, &to_string_obj))
689258
+					&PyBool_Type, &to_string_obj,
689258
+					&PyBool_Type, &release_gil_obj))
689258
     return NULL;
689258
 
689258
   from_tty = 0;
689258
@@ -582,6 +586,15 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
689258
       to_string = cmp;
689258
     }
689258
 
689258
+  release_gil = 0;
689258
+  if (release_gil_obj)
689258
+    {
689258
+      int cmp = PyObject_IsTrue (release_gil_obj);
689258
+      if (cmp < 0)
689258
+	return NULL;
689258
+      release_gil = cmp;
689258
+    }
689258
+
689258
   std::string to_string_res;
689258
 
689258
   TRY
689258
@@ -602,6 +615,13 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
689258
 
689258
       counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
689258
 
689258
+      /* In the case of long running GDB commands, allow the user to
689258
+	 release the Python GIL acquired by Python.  Restore the GIL
689258
+	 after the command has completed before handing back to
689258
+	 Python.  */
689258
+      if (release_gil)
689258
+	state = PyEval_SaveThread();
689258
+
689258
       scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
689258
 
689258
       scoped_restore save_uiout = make_scoped_restore (&current_uiout);
689258
@@ -617,10 +637,22 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
689258
 							    from_tty);
689258
       else
689258
 	execute_control_commands (lines.get (), from_tty);
689258
+
689258
+      /* Reacquire the GIL if it was released earlier.  */
689258
+      if (release_gil)
689258
+	PyEval_RestoreThread (state);
689258
     }
689258
   CATCH (except, RETURN_MASK_ALL)
689258
     {
689258
-      GDB_PY_HANDLE_EXCEPTION (except);
689258
+      if (except.reason < 0)
689258
+	{
689258
+	  /* Reacquire the GIL if it was released earlier.  */
689258
+	  if (release_gil)
689258
+	    PyEval_RestoreThread (state);
689258
+
689258
+	  gdbpy_convert_exception (except);
689258
+	  return NULL;
689258
+	}
689258
     }
689258
   END_CATCH
689258
 
689258
diff --git a/gdb/testsuite/gdb.python/py-gil-mthread.c b/gdb/testsuite/gdb.python/py-gil-mthread.c
689258
new file mode 100644
689258
--- /dev/null
689258
+++ b/gdb/testsuite/gdb.python/py-gil-mthread.c
689258
@@ -0,0 +1,13 @@
689258
+#include <stdio.h>
689258
+#include <unistd.h>
689258
+
689258
+int
689258
+main (void)
689258
+{
689258
+  int i;
689258
+  for (i = 0; i < 10; i++)
689258
+    {
689258
+      sleep (1); /* break-here */
689258
+      printf ("Sleeping %d\n", i);
689258
+    }
689258
+}
689258
diff --git a/gdb/testsuite/gdb.python/py-gil-mthread.exp b/gdb/testsuite/gdb.python/py-gil-mthread.exp
689258
new file mode 100644
689258
--- /dev/null
689258
+++ b/gdb/testsuite/gdb.python/py-gil-mthread.exp
689258
@@ -0,0 +1,69 @@
689258
+# Copyright (C) 2014 Free Software Foundation, Inc.
689258
+
689258
+# This program is free software; you can redistribute it and/or modify
689258
+# it under the terms of the GNU General Public License as published by
689258
+# the Free Software Foundation; either version 3 of the License, or
689258
+# (at your option) any later version.
689258
+#
689258
+# This program is distributed in the hope that it will be useful,
689258
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
689258
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
689258
+# GNU General Public License for more details.
689258
+#
689258
+# You should have received a copy of the GNU General Public License
689258
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
689258
+
689258
+standard_testfile .c .py
689258
+set executable $testfile
689258
+
689258
+if { [prepare_for_testing $testfile.exp $executable $srcfile] } {
689258
+    return -1
689258
+}
689258
+
689258
+# Skip all tests if Python scripting is not enabled.
689258
+if { [skip_python_tests] } { continue }
689258
+
689258
+if ![runto_main] {
689258
+    return -1
689258
+}
689258
+
689258
+gdb_breakpoint $srcfile:[gdb_get_line_number "break-here"] temporary
689258
+gdb_continue_to_breakpoint "break-here" ".* break-here .*"
689258
+
689258
+set test "response"
689258
+set timeout 60
689258
+set sleeping_last -1
689258
+set hello_last 0
689258
+set minimal 5
689258
+gdb_test_multiple "python exec (open ('$srcdir/$subdir/$srcfile2').read ())" $test {
689258
+    -re "Error: unable to start thread\r\n" {
689258
+	fail $test
689258
+	# Not $gdb_prompt-synced!
689258
+    }
689258
+    -re "Sleeping (\[0-9\]+)\r\n" {
689258
+	set n $expect_out(1,string)
689258
+	if { $sleeping_last + 1 != $n } {
689258
+	    fail $test
689258
+	} else {
689258
+	    set sleeping_last $n
689258
+	    if { $sleeping_last >= $minimal && $hello_last >= $minimal } {
689258
+		pass $test
689258
+	    } else {
689258
+		exp_continue
689258
+	    }
689258
+	}
689258
+    }
689258
+    -re "Hello \\( (\[0-9\]+) \\)\r\n" {
689258
+	set n $expect_out(1,string)
689258
+	if { $hello_last + 1 != $n } {
689258
+	    fail $test
689258
+	} else {
689258
+	    set hello_last $n
689258
+	    if { $sleeping_last >= $minimal && $hello_last >= $minimal } {
689258
+		pass $test
689258
+	    } else {
689258
+		exp_continue
689258
+	    }
689258
+	}
689258
+    }
689258
+}
689258
diff --git a/gdb/testsuite/gdb.python/py-gil-mthread.py b/gdb/testsuite/gdb.python/py-gil-mthread.py
689258
new file mode 100644
689258
--- /dev/null
689258
+++ b/gdb/testsuite/gdb.python/py-gil-mthread.py
689258
@@ -0,0 +1,28 @@
689258
+try:
689258
+   import thread
689258
+except:
689258
+   import _thread
689258
+import time
689258
+import gdb
689258
+
689258
+# Define a function for the thread
689258
+def print_thread_hello():
689258
+   count = 0
689258
+   while count < 10:
689258
+      time.sleep(1)
689258
+      count += 1
689258
+      print ("Hello ( %d )" % count)
689258
+
689258
+# Create a threads a continue
689258
+try:
689258
+   thread.start_new_thread (print_thread_hello, ())
689258
+   gdb.execute ("continue", release_gil=True)
689258
+except:
689258
+   try:
689258
+      _thread.start_new_thread (print_thread_hello, ())
689258
+      gdb.execute ("continue", release_gil=True)
689258
+   except:
689258
+      print ("Error: unable to start thread")
689258
+
689258
+while 1:
689258
+   pass