mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-15 04:31:49 +08:00
6f8b04077b
New in v2: - Define PyMem_RawMalloc as PyMem_Malloc for Python < 3.4 and use PyMem_RawMalloc in the code. Since Python 3.4, the callback installed in PyOS_ReadlineFunctionPointer should return a value allocated with PyMem_RawMalloc instead of PyMem_Malloc. The reason is that PyMem_Malloc must be called with the Python Global Interpreter Lock (GIL) held, which is not the case in the context where this function is called. PyMem_RawMalloc was introduced for cases like this. In Python 3.6, it looks like they added an assert to verify that PyMem_Malloc was not called without the GIL. The consequence is that typing anything in the python-interactive mode of gdb crashes the process. The same behavior was observed with the official package on Arch Linux as well as with a manual Python build on Ubuntu 14.04. This is what is shown with a debug build of Python 3.6 (the error with a non-debug build is far less clear): (gdb) pi >>> print(1) Fatal Python error: Python memory allocator called without holding the GIL Current thread 0x00007f1459af8780 (most recent call first): [1] 21326 abort ./gdb and the backtrace: #0 0x00007ffff618bc37 in raise () from /lib/x86_64-linux-gnu/libc.so.6 #1 0x00007ffff618f028 in abort () from /lib/x86_64-linux-gnu/libc.so.6 #2 0x00007ffff6b104d6 in Py_FatalError (msg=msg@entry=0x7ffff6ba15b8 "Python memory allocator called without holding the GIL") at Python/pylifecycle.c:1457 #3 0x00007ffff6a37a68 in _PyMem_DebugCheckGIL () at Objects/obmalloc.c:1972 #4 0x00007ffff6a3804e in _PyMem_DebugFree (ctx=0x7ffff6e65290 <_PyMem_Debug+48>, ptr=0x24f8830) at Objects/obmalloc.c:1994 #5 0x00007ffff6a38e1d in PyMem_Free (ptr=<optimized out>) at Objects/obmalloc.c:442 #6 0x00007ffff6b866c6 in _PyFaulthandler_Fini () at ./Modules/faulthandler.c:1369 #7 0x00007ffff6b104bd in Py_FatalError (msg=msg@entry=0x7ffff6ba15b8 "Python memory allocator called without holding the GIL") at Python/pylifecycle.c:1431 #8 0x00007ffff6a37a68 in _PyMem_DebugCheckGIL () at Objects/obmalloc.c:1972 #9 0x00007ffff6a37aa3 in _PyMem_DebugMalloc (ctx=0x7ffff6e65290 <_PyMem_Debug+48>, nbytes=5) at Objects/obmalloc.c:1980 #10 0x00007ffff6a38d91 in PyMem_Malloc (size=<optimized out>) at Objects/obmalloc.c:418 #11 0x000000000064dbe2 in gdbpy_readline_wrapper (sys_stdin=0x7ffff6514640 <_IO_2_1_stdin_>, sys_stdout=0x7ffff6514400 <_IO_2_1_stdout_>, prompt=0x7ffff4d4f7d0 ">>> ") at /home/emaisin/src/binutils-gdb/gdb/python/py-gdb-readline.c:75 The documentation is very clear about it [1] and it was also mentioned in the "What's New In Python 3.4" page [2]. [1] https://docs.python.org/3/c-api/veryhigh.html#c.PyOS_ReadlineFunctionPointer [2] https://docs.python.org/3/whatsnew/3.4.html#changes-in-the-c-api gdb/ChangeLog: * python/python-internal.h (PyMem_RawMalloc): Define for Python < 3.4. * python/py-gdb-readline.c (gdbpy_readline_wrapper): Use PyMem_RawMalloc instead of PyMem_Malloc.
114 lines
3.2 KiB
C
114 lines
3.2 KiB
C
/* Readline support for Python.
|
|
|
|
Copyright (C) 2012-2017 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#include "defs.h"
|
|
#include "python-internal.h"
|
|
#include "top.h"
|
|
#include "cli/cli-utils.h"
|
|
|
|
/* Readline function suitable for PyOS_ReadlineFunctionPointer, which
|
|
is used for Python's interactive parser and raw_input. In both
|
|
cases, sys_stdin and sys_stdout are always stdin and stdout
|
|
respectively, as far as I can tell; they are ignored and
|
|
command_line_input is used instead. */
|
|
|
|
static char *
|
|
gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
|
|
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4
|
|
const char *prompt)
|
|
#else
|
|
char *prompt)
|
|
#endif
|
|
{
|
|
int n;
|
|
char *p = NULL, *q;
|
|
|
|
TRY
|
|
{
|
|
p = command_line_input (prompt, 0, "python");
|
|
}
|
|
/* Handle errors by raising Python exceptions. */
|
|
CATCH (except, RETURN_MASK_ALL)
|
|
{
|
|
/* Detect user interrupt (Ctrl-C). */
|
|
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);
|
|
gdbpy_convert_exception (except);
|
|
PyEval_SaveThread ();
|
|
return NULL;
|
|
}
|
|
END_CATCH
|
|
|
|
/* Detect EOF (Ctrl-D). */
|
|
if (p == NULL)
|
|
{
|
|
q = (char *) PyMem_RawMalloc (1);
|
|
if (q != NULL)
|
|
q[0] = '\0';
|
|
return q;
|
|
}
|
|
|
|
n = strlen (p);
|
|
|
|
/* Copy the line to Python and return. */
|
|
q = (char *) PyMem_RawMalloc (n + 2);
|
|
if (q != NULL)
|
|
{
|
|
strncpy (q, p, n);
|
|
q[n] = '\n';
|
|
q[n + 1] = '\0';
|
|
}
|
|
return q;
|
|
}
|
|
|
|
/* Initialize Python readline support. */
|
|
|
|
void
|
|
gdbpy_initialize_gdb_readline (void)
|
|
{
|
|
/* Python's readline module conflicts with GDB's use of readline
|
|
since readline is not reentrant. Ideally, a reentrant wrapper to
|
|
GDB's readline should be implemented to replace Python's readline
|
|
and prevent conflicts. For now, this file implements a
|
|
sys.meta_path finder that simply fails to import the readline
|
|
module. */
|
|
if (PyRun_SimpleString ("\
|
|
import sys\n\
|
|
\n\
|
|
class GdbRemoveReadlineFinder:\n\
|
|
def find_module(self, fullname, path=None):\n\
|
|
if fullname == 'readline' and path is None:\n\
|
|
return self\n\
|
|
return None\n\
|
|
\n\
|
|
def load_module(self, fullname):\n\
|
|
raise ImportError('readline module disabled under GDB')\n\
|
|
\n\
|
|
sys.meta_path.append(GdbRemoveReadlineFinder())\n\
|
|
") == 0)
|
|
PyOS_ReadlineFunctionPointer = gdbpy_readline_wrapper;
|
|
}
|
|
|