llvm-project/lldb/source/Plugins
Jonas Devlieghere 049ae93097 [lldb] Fix that the embedded Python REPL crashes if it receives SIGINT
When LLDB receives a SIGINT while running the embedded Python REPL it
currently just crashes in ScriptInterpreterPythonImpl::Interrupt with an
error such as the one below:

  Fatal Python error: PyThreadState_Get: the function must be called
  with the GIL held, but the GIL is released (the current Python thread
  state is NULL)

The faulty code that causes this error is this part of
ScriptInterpreterPythonImpl::Interrupt:

  PyThreadState *state = PyThreadState_GET();
  if (!state)
    state = GetThreadState();
  if (state) {
    long tid = state->thread_id;
    PyThreadState_Swap(state);
    int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);

The obvious fix I tried is to just acquire the GIL before this code is
running which fixes the crash but the KeyboardInterrupt we want to raise
immediately is actually just queued and would only be raised once the
next line of input has been parsed (which e.g. won't interrupt Python
code that is currently waiting on a timer or IO from what I can see).
Also none of the functions we call here is marked as safe to be called
from a signal handler from what I can see, so we might still end up
crashing here with some bad timing.

Python 3.2 introduced PyErr_SetInterrupt to solve this and the function
takes care of all the details and avoids doing anything that isn't safe
to do inside a signal handler. The only thing we need to do is to
manually setup our own fake SIGINT handler that behaves the same way as
the standalone Python REPL signal handler (which raises a
KeyboardInterrupt).

From what I understand the old code used to work with Python 2 so I kept
the old code around until we officially drop support for Python 2.

There is a small gap here with Python 3.0->3.1 where we might still be
crashing, but those versions have reached their EOL more than a decade
ago so I think we don't need to bother about them.

Differential revision: https://reviews.llvm.org/D104886
2022-01-13 15:27:38 -08:00
..
ABI Set a default number of address bits on Darwin arm64 systems 2021-12-09 22:53:01 -08:00
Architecture [lldb] Remove ConstString from ABI, Architecture and Disassembler plugin names 2021-10-22 10:29:19 +02:00
Disassembler Remove redundant string initialization (NFC) 2021-12-26 09:39:26 -08:00
DynamicLoader Fix bugprone argument comments. 2022-01-09 12:21:02 -08:00
ExpressionParser [CodeCompletion] Signature help for braced constructor calls 2022-01-03 20:14:59 +01:00
Instruction Remove redundant string initialization (NFC) 2021-12-26 09:39:26 -08:00
InstrumentationRuntime Remove redundant string initialization (NFC) 2021-12-26 09:39:26 -08:00
JITLoader Remove ConstString from DynamicLoader, JITLoader and Instruction plugin names 2021-10-25 10:32:35 +02:00
Language [lldb] Remove ProcessStructReader from NSStringSummaryProvider (NFC) 2022-01-09 12:09:02 -08:00
LanguageRuntime Use true/false instead of 1/0 (NFC) 2022-01-09 12:21:06 -08:00
MemoryHistory [lldb] Remove ConstString from SymbolVendor, Trace, TraceExporter, UnwindAssembly, MemoryHistory and InstrumentationRuntime plugin names 2021-10-29 12:08:57 +02:00
ObjectContainer [lldb] Remove ConstString from GetPluginNameStatic of some plugins 2021-10-21 12:58:45 +02:00
ObjectFile Use true/false instead of 1/0 (NFC) 2022-01-09 12:21:06 -08:00
OperatingSystem [lldb] Remove ConstString from GetPluginNameStatic of some plugins 2021-10-21 12:58:45 +02:00
Platform [lldb/qemu] Implement GetMmapArgumentList 2022-01-11 14:08:03 +01:00
Process [lldb] [llgs] Implement qXfer:siginfo:read 2022-01-13 11:24:36 +01:00
REPL [lldb] Remove ConstString from TypeSystem and REPL plugin names 2021-11-02 16:13:52 +01:00
ScriptInterpreter [lldb] Fix that the embedded Python REPL crashes if it receives SIGINT 2022-01-13 15:27:38 -08:00
StructuredData [lldb] Remove ConstString from Process, ScriptInterpreter and StructuredData plugin names 2021-10-28 10:15:03 +02:00
SymbolFile Revert "Revert "[LLDB][NativePDB] Add support for inlined functions"" 2022-01-13 14:00:24 -08:00
SymbolVendor [lldb] Unbreak the macOS build after dfd499a61c 2021-11-02 09:47:44 +01:00
SystemRuntime Remove ConstString from Language, LanguageRuntime, SystemRuntime and SymbolFile plugin names 2021-10-27 08:25:44 +02:00
Trace [lldb] Remove ConstString from SymbolVendor, Trace, TraceExporter, UnwindAssembly, MemoryHistory and InstrumentationRuntime plugin names 2021-10-29 12:08:57 +02:00
TraceExporter Use Optional::getValueOr (NFC) 2021-12-24 20:57:40 -08:00
TypeSystem [LLDB][Clang] add AccessSpecDecl for methods and fields in RecordType 2022-01-04 13:50:24 -08:00
UnwindAssembly [lldb] Remove ConstString from SymbolVendor, Trace, TraceExporter, UnwindAssembly, MemoryHistory and InstrumentationRuntime plugin names 2021-10-29 12:08:57 +02:00
CMakeLists.txt [lldb] Add a C language REPL to test LLDB's REPL infrastructure 2021-09-23 19:31:02 +02:00
Plugins.def.in Re-land "[lldb/CMake] Auto-generate the Initialize and Terminate calls for plugin" 2020-02-18 19:16:07 -08:00