Blame SOURCES/gdb-python-gil.patch

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