Blame SOURCES/gdb-python-gil.patch

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