LLDB mainline
MSVCRTCFrameRecognizer.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "lldb/Target/Process.h"
14#include "lldb/Target/Target.h"
15#include "lldb/Target/Thread.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22namespace lldb_private {
23
26 std::make_shared<MSVCRTCFrameRecognizer>(), ConstString(""),
27 {ConstString("failwithmessage")}, Mangled::ePreferDemangled,
28 /*first_instruction_only=*/false);
29}
30
33 // failwithmessage calls __debugbreak() which lands at frame 0.
34 if (frame_sp->GetFrameIndex() != 0)
36 // Only fire on EXCEPTION_BREAKPOINT (0x80000003), not on other exceptions
37 // that might incidentally have failwithmessage somewhere in the call stack.
38 auto *pw =
39 static_cast<ProcessWindows *>(frame_sp->GetThread()->GetProcess().get());
40 auto exc_code = pw->GetActiveExceptionCode();
41 if (!exc_code || *exc_code != EXCEPTION_BREAKPOINT)
43
44 const char *fn_name = frame_sp->GetFunctionName();
45 if (!fn_name)
47 if (!llvm::StringRef(fn_name).contains("failwithmessage"))
49
50 VariableListSP vars = frame_sp->GetInScopeVariableList(false);
51 if (!vars)
53
54 for (size_t i = 0; i < vars->GetSize(); ++i) {
55 VariableSP var = vars->GetVariableAtIndex(i);
56 if (!var || var->GetName() != ConstString("msg"))
57 continue;
58
59 ValueObjectSP val =
60 frame_sp->GetValueObjectForFrameVariable(var, eNoDynamicValues);
61 if (!val)
62 break;
63
64 uint64_t ptr = val->GetValueAsUnsigned(0);
65 if (!ptr)
66 break;
67
68 std::string msg;
69 Status err;
70 frame_sp->GetThread()->GetProcess()->ReadCStringFromMemory(ptr, msg, err);
71 if (err.Success() && !msg.empty())
73 new MSVCRTCRecognizedFrame("Run-time check failure: " + msg));
74 break;
75 }
76
78}
79
80} // namespace lldb_private
A uniqued constant string class.
Definition ConstString.h:40
lldb::RecognizedStackFrameSP RecognizeFrame(lldb::StackFrameSP frame_sp) override
Recognized stack frame for an MSVC _RTC failure.
std::optional< DWORD > GetActiveExceptionCode() const
Returns the exception code of the active (current) debug exception, or std::nullopt if there is no ac...
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1250
void AddRecognizer(lldb::StackFrameRecognizerSP recognizer, ConstString module, llvm::ArrayRef< ConstString > symbols, Mangled::NamePreference symbol_mangling, bool first_instruction_only=true)
Add a new recognizer that triggers on a given symbol name.
An error handling class.
Definition Status.h:118
bool Success() const
Test for success condition.
Definition Status.cpp:303
StackFrameRecognizerManager & GetFrameRecognizerManager()
Definition Target.h:1702
A class that represents a running process on the host machine.
void RegisterMSVCRTCFrameRecognizer(ProcessWindows &process)
Registers the MSVC run-time check failure frame recognizer with the target.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::VariableList > VariableListSP
std::shared_ptr< lldb_private::Variable > VariableSP