binutils-gdb/gdb/python
Simon Marchi 6f8b04077b Fix python-interactive with Python 3.6
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.
2017-01-20 20:39:08 -05:00
..
lib/gdb update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-arch.c Use gdbpy_ref in archpy_disassemble 2017-01-10 19:13:59 -07:00
py-auto-load.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-block.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-bpevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-breakpoint.c Use gdbpy_ref in gdbpy_breakpoint_cond_says_stop 2017-01-10 19:14:00 -07:00
py-cmd.c Use gdbpy_ref in py-cmd.c 2017-01-10 19:14:01 -07:00
py-continueevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-event.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-event.h Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-events.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-evtregistry.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-evts.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-exitedevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-finishbreakpoint.c Use gdbpy_ref in bpfinishpy_out_of_scope 2017-01-10 19:14:01 -07:00
py-frame.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-framefilter.c Use gdbpy_ref rather than make_cleanup_py_decref 2017-01-10 19:14:07 -07:00
py-function.c Use gdbpy_enter in fnpy_call 2017-01-10 19:13:53 -07:00
py-gdb-readline.c Fix python-interactive with Python 3.6 2017-01-20 20:39:08 -05:00
py-inferior.c Use gdbpy_ref in py-inferior.c 2017-01-10 19:14:03 -07:00
py-infevents.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-infthread.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-lazy-string.c Change type of encoding argument to gdbpy_extract_lazy_string 2017-01-10 19:13:55 -07:00
py-linetable.c Use gdbpy_ref in py-linetable.c 2017-01-10 19:13:34 -07:00
py-newobjfileevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-objfile.c Use class to manage BFD reference counts 2017-01-10 19:14:10 -07:00
py-param.c Use gdbpy_ref in py-param.c 2017-01-10 19:14:04 -07:00
py-prettyprint.c Use gdbpy_ref in py-prettyprint.c 2017-01-10 19:13:58 -07:00
py-progspace.c Use gdbpy_enter in py-progspace.c 2017-01-10 19:13:42 -07:00
py-ref.h Add gdb_ref_ptr.h 2017-01-10 19:14:09 -07:00
py-signalevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-stopevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-stopevent.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-symbol.c Use gdbpy_ref in gdbpy_lookup_symbol 2017-01-10 19:13:38 -07:00
py-symtab.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
py-threadevent.c Change event code to use gdbpy_ref 2017-01-10 19:13:29 -07:00
py-type.c Add constructor and destructor to demangle_parse_info 2017-01-10 19:14:15 -07:00
py-unwind.c Use gdbpy_ref in pyuw_object_attribute_to_pointer 2017-01-10 19:14:05 -07:00
py-utils.c Remove make_cleanup_py_decref and make_cleanup_py_xdecref 2017-01-10 19:14:08 -07:00
py-value.c Add scoped_value_mark 2017-01-10 19:14:12 -07:00
py-varobj.c Use gdbpy_enter_varobj in py-varobj.c 2017-01-10 19:13:51 -07:00
py-xmethods.c Use gdbpy_enter in py-xmethod.c 2017-01-10 19:13:50 -07:00
python-config.py
python-internal.h Fix python-interactive with Python 3.6 2017-01-20 20:39:08 -05:00
python.c Remove cleanups from execute_gdb_command 2017-01-10 19:14:14 -07:00
python.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00