binutils-gdb/gdb/python
Tom de Vries 9d3785a8ac [gdb/python] Fix abort on Py_Initialize
I tried out making python initialization fail by passing an incorrect
PYTHONHOME with python 3.6, and got:
...
$ PYTHONHOME=foo gdb -q
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000ffff89269c80 (most recent call first):

Fatal signal: Aborted
  ...
Aborted (core dumped)
$
...

This is as per spec: when Py_Initialize () fails, a fatal error is raised
using Py_FatalError.

This can be worked around using:
...
$ PYTHONHOME=foo gdb -q -eiex "set python ignore-environment on"
(gdb)
...
but it would be better if gdb didn't abort.

I found an article [1] describing two solutions:
- try out Py_Initialize in a separate process, and
- catch the abort using a signal handler.

This patch implements the latter solution.

Obviously we cannot call into python anymore after the abort, so we avoid
calling Py_IsInitialized (), and instead use a new variable py_isinitialized.

This gets us instead:
...
$ PYTHONHOME=foo gdb -q
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000fffecfd49c80 (most recent call first):
Python not initialized
$
...

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>

PR python/32379
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32379

[1] https://stackoverflow.com/questions/7688374/how-to-i-catch-and-handle-a-fatal-error-when-py-initialize-fails
2024-11-22 19:34:24 +01:00
..
lib/gdb gdb: fix missing operator % on xmethod matcher output 2024-11-11 08:49:13 -05:00
py-all-events.def Add gdb.events.tui_enabled 2024-11-01 11:06:47 -06:00
py-arch.c gdb/python: missing PyObject_IsTrue error check in py-arch.c 2024-11-14 19:34:43 +00:00
py-auto-load.c
py-block.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-bpevent.c
py-breakpoint.c gdb/python: fix reference leak in gdb.BreakpointLocation.thread_groups 2024-11-20 09:33:17 +00:00
py-cmd.c [gdb/python] Eliminate GDB_PY_SET_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-connection.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-continueevent.c
py-dap.c Don't allow new-ui to start the TUI 2024-05-17 09:39:41 -06:00
py-disasm.c gdb/python: remove some additional PyObject_IsTrue calls 2024-11-14 19:34:43 +00:00
py-event-types.def Add gdb.events.tui_enabled 2024-11-01 11:06:47 -06:00
py-event.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-event.h Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-events.h
py-evtregistry.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-evts.c
py-exitedevent.c
py-finishbreakpoint.c [gdb/python] Eliminate GDB_PY_SET_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-frame.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-framefilter.c Add line-number styling 2024-09-30 13:23:35 -06:00
py-function.c Call gdbpy_fix_doc_string_indentation for function help 2024-11-11 07:44:27 -07:00
py-gdb-readline.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-inferior.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-infevents.c
py-infthread.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-instruction.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-instruction.h
py-lazy-string.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-linetable.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-membuf.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-mi.c Require a command argument in gdb.execute_mi 2024-10-18 11:50:27 -06:00
py-micmd.c gdb/python: missing PyObject_IsTrue error check in micmdpy_set_installed 2024-11-14 19:34:44 +00:00
py-newobjfileevent.c
py-objfile.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-param.c [gdb/python] Eliminate GDB_PY_SET_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-prettyprint.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-progspace.c gdb/python: implement Python find_exec_by_build_id hook 2024-11-10 10:18:23 +00:00
py-record-btrace.c btrace: Add support for interrupt events. 2024-09-24 14:22:28 +02:00
py-record-btrace.h python: Add clear() to gdb.Record. 2024-08-14 11:20:56 +02:00
py-record-full.c
py-record-full.h
py-record.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-record.h python: Introduce gdb.RecordAuxiliary class. 2024-08-14 11:20:56 +02:00
py-ref.h
py-registers.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-signalevent.c
py-stopevent.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-stopevent.h
py-symbol.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-symtab.c Automatically add types to Python modules 2024-09-23 13:44:59 -06:00
py-threadevent.c
py-tui.c Add gdb.events.tui_enabled 2024-11-01 11:06:47 -06:00
py-type.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-uiout.h Add line-number styling 2024-09-30 13:23:35 -06:00
py-unwind.c [gdb/python] Eliminate GDB_PY_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
py-utils.c [gdb/python] Make sure python sys.exit makes gdb exit 2024-09-25 19:29:57 +02:00
py-value.c gdb/python: remove some additional PyObject_IsTrue calls 2024-11-14 19:34:43 +00:00
py-varobj.c
py-xmethods.c
python-config.py
python-internal.h [gdb/python] Eliminate GDB_PY_SET_HANDLE_EXCEPTION 2024-09-24 13:06:32 +02:00
python.c [gdb/python] Fix abort on Py_Initialize 2024-11-22 19:34:24 +01:00
python.h