[Lldb-commits] [lldb] [lldb][windows] print stop reason if MSVC's runtime check fails (PR #185473)
via lldb-commits
lldb-commits at lists.llvm.org
Mon Mar 9 10:46:45 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Charles Zablit (charles-zablit)
<details>
<summary>Changes</summary>
This patch extracts the `msg` value of the `failwithmessage` error and uses it as the stop reason if the MSVC Runtime fails while debugging.
# Before
```
lldb.exe C:\Users\charleszablit\Developer\testing\uninit.exe -b -o 'r'
(lldb) target create "C:\\Users\\charleszablit\\Developer\\testing\\uninit.exe"
Current executable set to 'C:\Users\charleszablit\Developer\testing\uninit.exe' (x86_64).
(lldb) r
Process 9400 launched: 'C:\Users\charleszablit\Developer\testing\uninit.exe' (x86_64)
Process 9400 stopped
* thread #<!-- -->1, stop reason = Exception 0x80000003 encountered at address 0x7ff96516c96a
frame #<!-- -->0: 0x00007ff77efe20ba uninit.exe`failwithmessage(retaddr=0x00007ff77efe150f, crttype=1, errnum=3, msg="The variable 'x' is being used without being initialized.") at error.cpp:210
```
# After
```
lldb.exe C:\Users\charleszablit\Developer\testing\uninit.exe -b -o 'r'
(lldb) target create "C:\\Users\\charleszablit\\Developer\\testing\\uninit.exe"
Current executable set to 'C:\Users\charleszablit\Developer\testing\uninit.exe' (x86_64).
(lldb) r
Process 9400 launched: 'C:\Users\charleszablit\Developer\testing\uninit.exe' (x86_64)
Process 9400 stopped
* thread #<!-- -->1, stop reason = Run-time check failure: The variable 'x' is being used without being initialized.
frame #<!-- -->0: 0x00007ff77efe20ba uninit.exe`failwithmessage(retaddr=0x00007ff77efe150f, crttype=1, errnum=3, msg="The variable 'x' is being used without being initialized.") at error.cpp:210
```
fix https://github.com/llvm/llvm-project/issues/184990.
---
Full diff: https://github.com/llvm/llvm-project/pull/185473.diff
2 Files Affected:
- (modified) lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp (+50)
- (added) lldb/test/Shell/Process/MSVCRTCException.cpp (+16)
``````````diff
diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
index c61a41cd00444..16bbb636fad0c 100644
--- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
+++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp
@@ -27,6 +27,7 @@
#include "lldb/Host/windows/ConnectionConPTYWindows.h"
#include "lldb/Host/windows/HostThreadWindows.h"
#include "lldb/Symbol/ObjectFile.h"
+#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/DynamicLoader.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Target/StopInfo.h"
@@ -34,6 +35,7 @@
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/State.h"
+#include "lldb/ValueObject/ValueObject.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Format.h"
@@ -79,6 +81,49 @@ std::string GetProcessExecutableName(DWORD pid) {
}
return file_name;
}
+
+std::optional<std::string> GetMSVCRTCFailureDescription(Thread &thread) {
+ const uint32_t kMaxFrames = 8;
+
+ for (uint32_t i = 0; i < kMaxFrames; ++i) {
+ StackFrameSP frame = thread.GetStackFrameAtIndex(i);
+ if (!frame)
+ break;
+
+ SymbolContext sc = frame->GetSymbolContext(eSymbolContextSymbol);
+ if (!sc.symbol)
+ continue;
+
+ const char *fn_name = frame->GetFunctionName();
+ if (!fn_name)
+ continue;
+ llvm::StringRef name(fn_name);
+
+ if (!name.contains("failwithmessage"))
+ continue;
+ VariableListSP vars = frame->GetInScopeVariableList(false);
+ if (!vars)
+ continue;
+ for (size_t j = 0; j < vars->GetSize(); ++j) {
+ VariableSP var = vars->GetVariableAtIndex(j);
+ if (!var || var->GetName() != ConstString("msg"))
+ continue;
+ ValueObjectSP val =
+ frame->GetValueObjectForFrameVariable(var, eNoDynamicValues);
+ if (!val)
+ break;
+ uint64_t ptr = val->GetValueAsUnsigned(0);
+ if (!ptr)
+ break;
+ std::string msg;
+ Status err;
+ thread.GetProcess()->ReadCStringFromMemory(ptr, msg, err);
+ if (err.Success() && !msg.empty())
+ return "Run-time check failure: " + msg;
+ }
+ }
+ return std::nullopt;
+}
} // anonymous namespace
namespace lldb_private {
@@ -488,6 +533,11 @@ void ProcessWindows::RefreshStateAfterStop() {
}
stop_thread->SetStopInfo(stop_info);
return;
+ } else if (auto rtc_desc = GetMSVCRTCFailureDescription(*stop_thread)) {
+ stop_info = StopInfo::CreateStopReasonWithException(*stop_thread,
+ rtc_desc->c_str());
+ stop_thread->SetStopInfo(stop_info);
+ return;
} else {
// The thread hit a hard-coded breakpoint like an `int 3` or
// `__debugbreak()`.
diff --git a/lldb/test/Shell/Process/MSVCRTCException.cpp b/lldb/test/Shell/Process/MSVCRTCException.cpp
new file mode 100644
index 0000000000000..3238aaa24c8d2
--- /dev/null
+++ b/lldb/test/Shell/Process/MSVCRTCException.cpp
@@ -0,0 +1,16 @@
+// clang-format off
+
+// Test that lldb prints MSVC's runtime checks exceptions as stop reasons.
+
+// REQUIRES: msvc
+
+// RUN: %msvc_cl /nologo /Od /Zi /MDd /RTC1 -o %t.exe %s
+// RUN: %lldb -f %t.exe -b -o 'r' 2>&1 | FileCheck %s
+// CHECK: thread #1, stop reason = Run-time check failure: The variable 'x' is being used without being initialized.
+
+#include <iostream>
+
+int main() {
+ int x;
+ printf("%d\n", x);
+}
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/185473
More information about the lldb-commits
mailing list