llvm-project/lldb/source/Plugins
Raphael Isemann cef1e07cc6 [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.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D104886
2021-11-12 14:19:15 +01:00
..
ABI [lldb][NFC] Inclusive Language: Replace master with main 2021-11-01 12:25:41 -05:00
Architecture [lldb] Remove ConstString from ABI, Architecture and Disassembler plugin names 2021-10-22 10:29:19 +02:00
Disassembler [lldb] Remove ConstString from ABI, Architecture and Disassembler plugin names 2021-10-22 10:29:19 +02:00
DynamicLoader [lldb] Remove ConstString from Platform plugin names 2021-10-26 10:04:35 +02:00
ExpressionParser Revert "[lldb] Disable minimal import mode for RecordDecls that back FieldDecls" 2021-11-10 10:38:01 -08:00
Instruction Remove ConstString from DynamicLoader, JITLoader and Instruction plugin names 2021-10-25 10:32:35 +02:00
InstrumentationRuntime [lldb] Remove ConstString from SymbolVendor, Trace, TraceExporter, UnwindAssembly, MemoryHistory and InstrumentationRuntime plugin names 2021-10-29 12:08:57 +02:00
JITLoader Remove ConstString from DynamicLoader, JITLoader and Instruction plugin names 2021-10-25 10:32:35 +02:00
Language [formatters] Add a libstdcpp formatter for forward_list and refactor list formatter 2021-11-09 21:33:08 -08:00
LanguageRuntime [lldb] Improve 'lang objc tagged-pointer info' command 2021-11-05 13:19:00 -07: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 [LLDB][Breakpad] Make lldb understand INLINE and INLINE_ORIGIN records in breakpad. 2021-11-10 11:20:32 -08:00
OperatingSystem [lldb] Remove ConstString from GetPluginNameStatic of some plugins 2021-10-21 12:58:45 +02:00
Platform [lldb][AArch64] Add UnwindPlan for Linux sigreturn 2021-11-11 11:32:06 +00:00
Process [lldb/Plugins] Refactor ScriptedThread register context creation 2021-11-11 00:13:58 +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 2021-11-12 14:19:15 +01:00
StructuredData [lldb] Remove ConstString from Process, ScriptInterpreter and StructuredData plugin names 2021-10-28 10:15:03 +02:00
SymbolFile [lldb][NFC] Remove commented out code in SymbolFileDWARF 2021-11-11 12:45:38 +01: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 [lldb] Remove ConstString from SymbolVendor, Trace, TraceExporter, UnwindAssembly, MemoryHistory and InstrumentationRuntime plugin names 2021-10-29 12:08:57 +02:00
TypeSystem [lldb][NFC] StringRef-ify name param in CreateClassTemplateDecl 2021-11-04 15:28:02 +01: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