[2/2] gdb/python: avoid use of _PyOS_ReadlineTState

Message ID 20231207110240.1853760-2-ahajkova@redhat.com
State New
Headers
Series [1/2] gdb: move gdbpy_gil into python-internal.h |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Alexandra Petlanova Hajkova Dec. 7, 2023, 11:02 a.m. UTC
  From: Andrew Burgess <aburgess@redhat.com>

In python/py-gdb-readline.c we make use of _PyOS_ReadlineTState,
however, this variable is no longer public in Python 3.13, and so GDB
no longer builds.

We are making use of _PyOS_ReadlineTState in order to re-acquire the
Python Global Interpreter Lock (GIL).  The _PyOS_ReadlineTState
variable is set in Python's outer readline code prior to calling the
user (GDB) supplied readline callback function, which for us is
gdbpy_readline_wrapper.  The gdbpy_readline_wrapper function is called
without the GIL held.

Instead of using _PyOS_ReadlineTState, I propose that we switch to
calling PyGILState_Ensure() and PyGILState_Release().  These functions
will acquire the GIL based on the current thread.  I think this should
be sufficient; I can't imagine why we'd be running
gdbpy_readline_wrapper on one thread on behalf of a different Python
thread.... that would be unexpected I think.
---
 gdb/python/py-gdb-readline.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
  

Comments

Tom Tromey Dec. 8, 2023, 6:04 p.m. UTC | #1
>>>>> "Alexandra" == Alexandra Hájková <ahajkova@redhat.com> writes:

Alexandra> Instead of using _PyOS_ReadlineTState, I propose that we switch to
Alexandra> calling PyGILState_Ensure() and PyGILState_Release().  These functions
Alexandra> will acquire the GIL based on the current thread.  I think this should
Alexandra> be sufficient; I can't imagine why we'd be running
Alexandra> gdbpy_readline_wrapper on one thread on behalf of a different Python
Alexandra> thread.... that would be unexpected I think.

Makes sense to me.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  

Patch

diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c
index 124cec8055d..699d396dd19 100644
--- a/gdb/python/py-gdb-readline.c
+++ b/gdb/python/py-gdb-readline.c
@@ -56,13 +56,11 @@  gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
       if (except.reason == RETURN_QUIT)
 	return NULL;
 
-      /* The thread state is nulled during gdbpy_readline_wrapper,
-	 with the original value saved in the following undocumented
-	 variable (see Python's Parser/myreadline.c and
-	 Modules/readline.c).  */
-      PyEval_RestoreThread (_PyOS_ReadlineTState);
+
+      /* This readline callback is called without the GIL held.  */
+      gdbpy_gil gil;
+
       gdbpy_convert_exception (except);
-      PyEval_SaveThread ();
       return NULL;
     }