Skip to content

Commit c0a8723

Browse files
authored
[LLDB] Show exit code on Windows if process can't launch (#141290)
When running a process that would exit before LLDB could stop the target, it would try to interpret (and subsequently format) the exit code as a Win32 error. However, processes on Windows won't return Win32 errors in that case. They will often return an [NTSTATUS](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55). One common case for this to occur is when a DLL is missing. In that case, the process will start successfully, but it will exit with `STATUS_DLL_NOT_FOUND`. LLDB would previously return "unknown error", because it tried to [`FormatMessage`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage) `0xC0000135` which doesn't work, so it fell back to "unknown error". This PR changes the error to be the string "Process prematurely exited with {0:x}" and doesn't try to format the exit code. One could `FormatMessage` an `NTSTATUS` by passing `FORMAT_MESSAGE_FROM_HMODULE` and a handle to `ntdll.dll`, however, I don't think we can get the required format arguments (e.g. the missing DLL name - `%hs`).
1 parent 317f3bd commit c0a8723

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

lldb/source/Plugins/Process/Windows/Common/ProcessDebugger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ void ProcessDebugger::OnExitProcess(uint32_t exit_code) {
483483
// of the error otherwise WaitForDebuggerConnection() will be blocked.
484484
// An example of this issue is when a process fails to load a dependent DLL.
485485
if (m_session_data && !m_session_data->m_initial_stop_received) {
486-
Status error(exit_code, eErrorTypeWin32);
486+
Status error = Status::FromErrorStringWithFormatv(
487+
"Process prematurely exited with {0:x}", exit_code);
487488
OnDebuggerError(error, 0);
488489
}
489490
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
C_SOURCES := main.c
2+
DYLIB_C_SOURCES := dummy_dll.c
3+
DYLIB_NAME := dummy_dll
4+
5+
include Makefile.rules
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class MissingDllTestCase(TestBase):
8+
@skipUnlessWindows
9+
def test(self):
10+
"""
11+
Test that lldb reports the application's exit code (STATUS_DLL_NOT_FOUND),
12+
rather than trying to treat it as a Win32 error number.
13+
"""
14+
15+
self.build()
16+
exe = self.getBuildArtifact("a.out")
17+
dll = self.getBuildArtifact("dummy_dll.dll")
18+
self.assertTrue(remove_file(dll))
19+
target = self.dbg.CreateTarget(exe)
20+
self.assertTrue(target, VALID_TARGET)
21+
22+
launch_info = lldb.SBLaunchInfo(None)
23+
launch_info.SetWorkingDirectory(self.get_process_working_directory())
24+
25+
error = lldb.SBError()
26+
target.Launch(launch_info, error)
27+
self.assertFailure(error, "Process prematurely exited with 0xc0000135")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__declspec(dllexport) void SomeFunction(void) {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__declspec(dllimport) void SomeFunction(void);
2+
3+
int main(void) {
4+
SomeFunction();
5+
return 0;
6+
}

0 commit comments

Comments
 (0)