Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lldb/linux] Handle main thread exits
This patch handles the situation where the main thread exits (through the SYS_exit syscall). In this case, the process as a whole continues running until all of the other threads exit, or one of them issues an exit_group syscall. The patch consists of two changes: - a moderate redesign of the handling of thread exit (WIFEXITED) events. Previously, we were removing (forgetting) a thread once we received the WIFEXITED (or WIFSIGNALED) event. This was problematic for the main thread, since the main thread WIFEXITED event (which is better thought of as a process-wide event) gets reported only after the entire process exits. This resulted in deadlocks, where we were waiting for the process to stop (because we still considered the main thread "live"). This patch changes the logic such that the main thread is removed as soon as its PTRACE_EVENT_EXIT (the pre-exit) event is received. At this point we can consider the thread gone (for most purposes). As a corrolary, I needed to add special logic to catch process-wide exit events in the cases where we don't have the main thread around. - The second part of the patch is the removal of the assumptions that the main thread is always available. This generally meant replacing the uses of GetThreadByID(process_id) with GetCurrentThread() in various process-wide operations (such as memory reads). Differential Revision: https://reviews.llvm.org/D122716
- Loading branch information
Showing
4 changed files
with
122 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
lldb/test/API/functionalities/thread/main_thread_exit/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ENABLE_THREADS := YES | ||
CXX_SOURCES := main.cpp | ||
include Makefile.rules |
31 changes: 31 additions & 0 deletions
31
lldb/test/API/functionalities/thread/main_thread_exit/TestMainThreadExit.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Test handling of the situation where the main thread exits but the other threads | ||
in the process keep running. | ||
""" | ||
|
||
import lldb | ||
from lldbsuite.test.decorators import * | ||
from lldbsuite.test.lldbtest import * | ||
import lldbsuite.test.lldbutil as lldbutil | ||
|
||
|
||
class ThreadExitTestCase(TestBase): | ||
|
||
mydir = TestBase.compute_mydir(__file__) | ||
NO_DEBUG_INFO_TESTCASE = True | ||
|
||
# Needs os-specific implementation in the inferior | ||
@skipIf(oslist=no_match(["linux"])) | ||
def test(self): | ||
self.build() | ||
lldbutil.run_to_source_breakpoint(self, "// break here", | ||
lldb.SBFileSpec("main.cpp")) | ||
|
||
# There should be one (non-main) thread left | ||
self.assertEquals(self.process().GetNumThreads(), 1) | ||
|
||
# Ensure we can evaluate_expressions in this state | ||
self.expect_expr("call_me()", result_value="12345") | ||
|
||
self.runCmd("continue") | ||
self.assertEquals(self.process().GetExitStatus(), 47) |
23 changes: 23 additions & 0 deletions
23
lldb/test/API/functionalities/thread/main_thread_exit/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <thread> | ||
|
||
#ifdef __linux__ | ||
#include <sys/syscall.h> | ||
#include <unistd.h> | ||
|
||
void exit_thread(int result) { syscall(SYS_exit, result); } | ||
#else | ||
#error Needs OS-specific implementation | ||
#endif | ||
|
||
int call_me() { return 12345; } | ||
|
||
void thread() { | ||
std::this_thread::sleep_for( | ||
std::chrono::seconds(10)); // Let the main thread exit. | ||
exit_thread(42); // break here | ||
} | ||
|
||
int main() { | ||
std::thread(thread).detach(); | ||
exit_thread(47); | ||
} |