llvm-project/lldb/source/Plugins/Process/Windows/Common/MSVCRTCFrameRecognizer.cpp
Charles Zablit 681f1a5ee9
[lldb][windows] print stop reason if MSVC's runtime check fails (#185473)
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.

rdar://172103284
2026-03-13 15:36:51 +00:00

81 lines
2.7 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "MSVCRTCFrameRecognizer.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrameRecognizer.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/ValueObject/ValueObject.h"
using namespace lldb;
using namespace lldb_private;
namespace lldb_private {
void RegisterMSVCRTCFrameRecognizer(ProcessWindows &process) {
process.GetTarget().GetFrameRecognizerManager().AddRecognizer(
std::make_shared<MSVCRTCFrameRecognizer>(), ConstString(),
{ConstString("failwithmessage")}, Mangled::ePreferDemangled,
/*first_instruction_only=*/false);
}
lldb::RecognizedStackFrameSP
MSVCRTCFrameRecognizer::RecognizeFrame(lldb::StackFrameSP frame_sp) {
// failwithmessage calls __debugbreak() which lands at frame 0.
if (frame_sp->GetFrameIndex() != 0)
return RecognizedStackFrameSP();
// Only fire on EXCEPTION_BREAKPOINT (0x80000003), not on other exceptions
// that might incidentally have failwithmessage somewhere in the call stack.
auto *pw =
static_cast<ProcessWindows *>(frame_sp->GetThread()->GetProcess().get());
auto exc_code = pw->GetActiveExceptionCode();
if (!exc_code || *exc_code != EXCEPTION_BREAKPOINT)
return RecognizedStackFrameSP();
const char *fn_name = frame_sp->GetFunctionName();
if (!fn_name)
return RecognizedStackFrameSP();
if (!llvm::StringRef(fn_name).contains("failwithmessage"))
return RecognizedStackFrameSP();
VariableListSP vars = frame_sp->GetInScopeVariableList(false);
if (!vars)
return RecognizedStackFrameSP();
for (size_t i = 0; i < vars->GetSize(); ++i) {
VariableSP var = vars->GetVariableAtIndex(i);
if (!var || var->GetName() != ConstString("msg"))
continue;
ValueObjectSP val =
frame_sp->GetValueObjectForFrameVariable(var, eNoDynamicValues);
if (!val)
break;
uint64_t ptr = val->GetValueAsUnsigned(0);
if (!ptr)
break;
std::string msg;
Status err;
frame_sp->GetThread()->GetProcess()->ReadCStringFromMemory(ptr, msg, err);
if (err.Success() && !msg.empty())
return lldb::RecognizedStackFrameSP(
new MSVCRTCRecognizedFrame("Run-time check failure: " + msg));
break;
}
return RecognizedStackFrameSP();
}
} // namespace lldb_private