Index: lldb/trunk/include/lldb/Target/CPPLanguageRuntime.h =================================================================== --- lldb/trunk/include/lldb/Target/CPPLanguageRuntime.h +++ lldb/trunk/include/lldb/Target/CPPLanguageRuntime.h @@ -1,90 +0,0 @@ -//===-- CPPLanguageRuntime.h -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_CPPLanguageRuntime_h_ -#define liblldb_CPPLanguageRuntime_h_ - -#include -#include "lldb/Core/PluginInterface.h" -#include "lldb/Target/LanguageRuntime.h" -#include "lldb/lldb-private.h" - -namespace lldb_private { - -class CPPLanguageRuntime : public LanguageRuntime { -public: - enum class LibCppStdFunctionCallableCase { - Lambda = 0, - CallableObject, - FreeOrMemberFunction, - Invalid - }; - - struct LibCppStdFunctionCallableInfo { - Symbol callable_symbol; - Address callable_address; - LineEntry callable_line_entry; - lldb::addr_t member__f_pointer_value = 0u; - LibCppStdFunctionCallableCase callable_case = - LibCppStdFunctionCallableCase::Invalid; - }; - - LibCppStdFunctionCallableInfo - FindLibCppStdFunctionCallableInfo(lldb::ValueObjectSP &valobj_sp); - - ~CPPLanguageRuntime() override; - - static char ID; - - bool isA(const void *ClassID) const override { - return ClassID == &ID || LanguageRuntime::isA(ClassID); - } - - static bool classof(const LanguageRuntime *runtime) { - return runtime->isA(&ID); - } - - lldb::LanguageType GetLanguageType() const override { - return lldb::eLanguageTypeC_plus_plus; - } - - static CPPLanguageRuntime *Get(Process &process) { - return llvm::cast_or_null( - process.GetLanguageRuntime(lldb::eLanguageTypeC_plus_plus)); - } - - bool GetObjectDescription(Stream &str, ValueObject &object) override; - - bool GetObjectDescription(Stream &str, Value &value, - ExecutionContextScope *exe_scope) override; - - /// Obtain a ThreadPlan to get us into C++ constructs such as std::function. - /// - /// \param[in] thread - /// Curent thrad of execution. - /// - /// \param[in] stop_others - /// True if other threads should pause during execution. - /// - /// \return - /// A ThreadPlan Shared pointer - lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread, - bool stop_others) override; - - bool IsWhitelistedRuntimeValue(ConstString name) override; -protected: - // Classes that inherit from CPPLanguageRuntime can see and modify these - CPPLanguageRuntime(Process *process); - -private: - DISALLOW_COPY_AND_ASSIGN(CPPLanguageRuntime); -}; - -} // namespace lldb_private - -#endif // liblldb_CPPLanguageRuntime_h_ Index: lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt =================================================================== --- lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt +++ lldb/trunk/source/Plugins/ExpressionParser/Clang/CMakeLists.txt @@ -44,6 +44,7 @@ lldbTarget lldbUtility lldbPluginCPlusPlusLanguage + lldbPluginCPPRuntime LINK_COMPONENTS Core ExecutionEngine Index: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp =================================================================== --- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp +++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp @@ -32,7 +32,6 @@ #include "lldb/Symbol/TypeList.h" #include "lldb/Symbol/Variable.h" #include "lldb/Symbol/VariableList.h" -#include "lldb/Target/CPPLanguageRuntime.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" @@ -53,6 +52,7 @@ #include "clang/AST/RecursiveASTVisitor.h" #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" +#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Plugins/Language/CPlusPlus/CMakeLists.txt =================================================================== --- lldb/trunk/source/Plugins/Language/CPlusPlus/CMakeLists.txt +++ lldb/trunk/source/Plugins/Language/CPlusPlus/CMakeLists.txt @@ -28,6 +28,7 @@ lldbTarget lldbUtility lldbPluginClangCommon + lldbPluginCPPRuntime LINK_COMPONENTS Support Index: lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp +++ lldb/trunk/source/Plugins/Language/CPlusPlus/LibCxx.cpp @@ -18,7 +18,6 @@ #include "lldb/DataFormatters/TypeSummary.h" #include "lldb/DataFormatters/VectorIterator.h" #include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Target/CPPLanguageRuntime.h" #include "lldb/Target/ProcessStructReader.h" #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" @@ -27,6 +26,8 @@ #include "lldb/Utility/Status.h" #include "lldb/Utility/Stream.h" +#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" + using namespace lldb; using namespace lldb_private; using namespace lldb_private::formatters; Index: lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CMakeLists.txt =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CMakeLists.txt +++ lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CMakeLists.txt @@ -1,2 +1,11 @@ +add_lldb_library(lldbPluginCPPRuntime PLUGIN + CPPLanguageRuntime.cpp + + LINK_LIBS + lldbCore + lldbSymbol + lldbTarget +) + add_subdirectory(ItaniumABI) #add_subdirectory(MicrosoftABI) Index: lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h +++ lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h @@ -0,0 +1,90 @@ +//===-- CPPLanguageRuntime.h +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_CPPLanguageRuntime_h_ +#define liblldb_CPPLanguageRuntime_h_ + +#include +#include "lldb/Core/PluginInterface.h" +#include "lldb/Target/LanguageRuntime.h" +#include "lldb/lldb-private.h" + +namespace lldb_private { + +class CPPLanguageRuntime : public LanguageRuntime { +public: + enum class LibCppStdFunctionCallableCase { + Lambda = 0, + CallableObject, + FreeOrMemberFunction, + Invalid + }; + + struct LibCppStdFunctionCallableInfo { + Symbol callable_symbol; + Address callable_address; + LineEntry callable_line_entry; + lldb::addr_t member__f_pointer_value = 0u; + LibCppStdFunctionCallableCase callable_case = + LibCppStdFunctionCallableCase::Invalid; + }; + + LibCppStdFunctionCallableInfo + FindLibCppStdFunctionCallableInfo(lldb::ValueObjectSP &valobj_sp); + + ~CPPLanguageRuntime() override; + + static char ID; + + bool isA(const void *ClassID) const override { + return ClassID == &ID || LanguageRuntime::isA(ClassID); + } + + static bool classof(const LanguageRuntime *runtime) { + return runtime->isA(&ID); + } + + lldb::LanguageType GetLanguageType() const override { + return lldb::eLanguageTypeC_plus_plus; + } + + static CPPLanguageRuntime *Get(Process &process) { + return llvm::cast_or_null( + process.GetLanguageRuntime(lldb::eLanguageTypeC_plus_plus)); + } + + bool GetObjectDescription(Stream &str, ValueObject &object) override; + + bool GetObjectDescription(Stream &str, Value &value, + ExecutionContextScope *exe_scope) override; + + /// Obtain a ThreadPlan to get us into C++ constructs such as std::function. + /// + /// \param[in] thread + /// Curent thrad of execution. + /// + /// \param[in] stop_others + /// True if other threads should pause during execution. + /// + /// \return + /// A ThreadPlan Shared pointer + lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread, + bool stop_others) override; + + bool IsWhitelistedRuntimeValue(ConstString name) override; +protected: + // Classes that inherit from CPPLanguageRuntime can see and modify these + CPPLanguageRuntime(Process *process); + +private: + DISALLOW_COPY_AND_ASSIGN(CPPLanguageRuntime); +}; + +} // namespace lldb_private + +#endif // liblldb_CPPLanguageRuntime_h_ Index: lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp @@ -0,0 +1,353 @@ +//===-- CPPLanguageRuntime.cpp +// +// 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 + +#include + +#include "CPPLanguageRuntime.h" + +#include "llvm/ADT/StringRef.h" + +#include "lldb/Symbol/Block.h" +#include "lldb/Symbol/Variable.h" +#include "lldb/Symbol/VariableList.h" + +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/UniqueCStringMap.h" +#include "lldb/Symbol/ClangASTContext.h" +#include "lldb/Target/ABI.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/SectionLoadList.h" +#include "lldb/Target/StackFrame.h" +#include "lldb/Target/ThreadPlanRunToAddress.h" +#include "lldb/Target/ThreadPlanStepInRange.h" + +using namespace lldb; +using namespace lldb_private; + +static ConstString g_this = ConstString("this"); + +char CPPLanguageRuntime::ID = 0; + +// Destructor +CPPLanguageRuntime::~CPPLanguageRuntime() {} + +CPPLanguageRuntime::CPPLanguageRuntime(Process *process) + : LanguageRuntime(process) {} + +bool CPPLanguageRuntime::IsWhitelistedRuntimeValue(ConstString name) { + return name == g_this; +} + +bool CPPLanguageRuntime::GetObjectDescription(Stream &str, + ValueObject &object) { + // C++ has no generic way to do this. + return false; +} + +bool CPPLanguageRuntime::GetObjectDescription( + Stream &str, Value &value, ExecutionContextScope *exe_scope) { + // C++ has no generic way to do this. + return false; +} + +CPPLanguageRuntime::LibCppStdFunctionCallableInfo +CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo( + lldb::ValueObjectSP &valobj_sp) { + LibCppStdFunctionCallableInfo optional_info; + + if (!valobj_sp) + return optional_info; + + // Member __f_ has type __base*, the contents of which will hold: + // 1) a vtable entry which may hold type information needed to discover the + // lambda being called + // 2) possibly hold a pointer to the callable object + // e.g. + // + // (lldb) frame var -R f_display + // (std::__1::function) f_display = { + // __buf_ = { + // … + // } + // __f_ = 0x00007ffeefbffa00 + // } + // (lldb) memory read -fA 0x00007ffeefbffa00 + // 0x7ffeefbffa00: ... `vtable for std::__1::__function::__funcGetChildMemberWithName(ConstString("__f_"), true)); + + if (member__f_) { + ValueObjectSP sub_member__f_( + member__f_->GetChildMemberWithName(ConstString("__f_"), true)); + + if (sub_member__f_) + member__f_ = sub_member__f_; + } + + lldb::addr_t member__f_pointer_value = member__f_->GetValueAsUnsigned(0); + + optional_info.member__f_pointer_value = member__f_pointer_value; + + ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef()); + Process *process = exe_ctx.GetProcessPtr(); + + if (process == nullptr) + return optional_info; + + uint32_t address_size = process->GetAddressByteSize(); + Status status; + + // First item pointed to by __f_ should be the pointer to the vtable for + // a __base object. + lldb::addr_t vtable_address = + process->ReadPointerFromMemory(member__f_pointer_value, status); + + if (status.Fail()) + return optional_info; + + lldb::addr_t address_after_vtable = member__f_pointer_value + address_size; + // As commened above we may not have a function pointer but if we do we will + // need it. + lldb::addr_t possible_function_address = + process->ReadPointerFromMemory(address_after_vtable, status); + + if (status.Fail()) + return optional_info; + + Target &target = process->GetTarget(); + + if (target.GetSectionLoadList().IsEmpty()) + return optional_info; + + Address vtable_addr_resolved; + SymbolContext sc; + Symbol *symbol; + + if (!target.GetSectionLoadList().ResolveLoadAddress(vtable_address, + vtable_addr_resolved)) + return optional_info; + + target.GetImages().ResolveSymbolContextForAddress( + vtable_addr_resolved, eSymbolContextEverything, sc); + symbol = sc.symbol; + + if (symbol == nullptr) + return optional_info; + + llvm::StringRef vtable_name(symbol->GetName().GetCString()); + bool found_expected_start_string = + vtable_name.startswith("vtable for std::__1::__function::__func<"); + + if (!found_expected_start_string) + return optional_info; + + // Given case 1 or 3 we have a vtable name, we are want to extract the first + // template parameter + // + // ... __func ... + // ^^^^^^^^^ + // + // We do this by find the first < and , and extracting in between. + // + // This covers the case of the lambda known at compile time. + size_t first_open_angle_bracket = vtable_name.find('<') + 1; + size_t first_comma = vtable_name.find(','); + + llvm::StringRef first_template_parameter = + vtable_name.slice(first_open_angle_bracket, first_comma); + + Address function_address_resolved; + + // Setup for cases 2, 4 and 5 we have a pointer to a function after the + // vtable. We will use a process of elimination to drop through each case + // and obtain the data we need. + if (target.GetSectionLoadList().ResolveLoadAddress( + possible_function_address, function_address_resolved)) { + target.GetImages().ResolveSymbolContextForAddress( + function_address_resolved, eSymbolContextEverything, sc); + symbol = sc.symbol; + } + + auto get_name = [&first_template_parameter, &symbol]() { + // Given case 1: + // + // main::$_0 + // + // we want to append ::operator()() + if (first_template_parameter.contains("$_")) + return llvm::Regex::escape(first_template_parameter.str()) + + R"(::operator\(\)\(.*\))"; + + if (symbol != nullptr && + symbol->GetName().GetStringRef().contains("__invoke")) { + + llvm::StringRef symbol_name = symbol->GetName().GetStringRef(); + size_t pos2 = symbol_name.find_last_of(':'); + + // Given case 2: + // + // main::$_1::__invoke(...) + // + // We want to slice off __invoke(...) and append operator()() + std::string lambda_operator = + llvm::Regex::escape(symbol_name.slice(0, pos2 + 1).str()) + + R"(operator\(\)\(.*\))"; + + return lambda_operator; + } + + // Case 3 + return first_template_parameter.str() + R"(::operator\(\)\(.*\))"; + ; + }; + + std::string func_to_match = get_name(); + + SymbolContextList scl; + + target.GetImages().FindSymbolsMatchingRegExAndType( + RegularExpression{R"(^)" + func_to_match}, eSymbolTypeAny, scl, true); + + // Case 1,2 or 3 + if (scl.GetSize() >= 1) { + SymbolContext sc2 = scl[0]; + + AddressRange range; + sc2.GetAddressRange(eSymbolContextEverything, 0, false, range); + + Address address = range.GetBaseAddress(); + + Address addr; + if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target), + addr)) { + LineEntry line_entry; + addr.CalculateSymbolContextLineEntry(line_entry); + + if (first_template_parameter.contains("$_") || + (symbol != nullptr && + symbol->GetName().GetStringRef().contains("__invoke"))) { + // Case 1 and 2 + optional_info.callable_case = LibCppStdFunctionCallableCase::Lambda; + } else { + // Case 3 + optional_info.callable_case = + LibCppStdFunctionCallableCase::CallableObject; + } + + optional_info.callable_symbol = *symbol; + optional_info.callable_line_entry = line_entry; + optional_info.callable_address = addr; + return optional_info; + } + } + + // Case 4 or 5 + if (symbol && !symbol->GetName().GetStringRef().startswith("vtable for")) { + optional_info.callable_case = + LibCppStdFunctionCallableCase::FreeOrMemberFunction; + optional_info.callable_address = function_address_resolved; + optional_info.callable_symbol = *symbol; + + return optional_info; + } + + return optional_info; +} + +lldb::ThreadPlanSP +CPPLanguageRuntime::GetStepThroughTrampolinePlan(Thread &thread, + bool stop_others) { + ThreadPlanSP ret_plan_sp; + + lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); + + TargetSP target_sp(thread.CalculateTarget()); + + if (target_sp->GetSectionLoadList().IsEmpty()) + return ret_plan_sp; + + Address pc_addr_resolved; + SymbolContext sc; + Symbol *symbol; + + if (!target_sp->GetSectionLoadList().ResolveLoadAddress(curr_pc, + pc_addr_resolved)) + return ret_plan_sp; + + target_sp->GetImages().ResolveSymbolContextForAddress( + pc_addr_resolved, eSymbolContextEverything, sc); + symbol = sc.symbol; + + if (symbol == nullptr) + return ret_plan_sp; + + llvm::StringRef function_name(symbol->GetName().GetCString()); + + // Handling the case where we are attempting to step into std::function. + // The behavior will be that we will attempt to obtain the wrapped + // callable via FindLibCppStdFunctionCallableInfo() and if we find it we + // will return a ThreadPlanRunToAddress to the callable. Therefore we will + // step into the wrapped callable. + // + bool found_expected_start_string = + function_name.startswith("std::__1::function<"); + + if (!found_expected_start_string) + return ret_plan_sp; + + AddressRange range_of_curr_func; + sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func); + + StackFrameSP frame = thread.GetStackFrameAtIndex(0); + + if (frame) { + ValueObjectSP value_sp = frame->FindVariable(g_this); + + CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info = + FindLibCppStdFunctionCallableInfo(value_sp); + + if (callable_info.callable_case != LibCppStdFunctionCallableCase::Invalid && + value_sp->GetValueIsValid()) { + // We found the std::function wrapped callable and we have its address. + // We now create a ThreadPlan to run to the callable. + ret_plan_sp = std::make_shared( + thread, callable_info.callable_address, stop_others); + return ret_plan_sp; + } else { + // We are in std::function but we could not obtain the callable. + // We create a ThreadPlan to keep stepping through using the address range + // of the current function. + ret_plan_sp = std::make_shared( + thread, range_of_curr_func, sc, eOnlyThisThread, eLazyBoolYes, + eLazyBoolYes); + return ret_plan_sp; + } + } + + return ret_plan_sp; +} Index: lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/CMakeLists.txt =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/CMakeLists.txt +++ lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/CMakeLists.txt @@ -7,4 +7,5 @@ lldbInterpreter lldbSymbol lldbTarget + lldbPluginCPPRuntime ) Index: lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h +++ lldb/trunk/source/Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h @@ -16,10 +16,11 @@ #include "lldb/Breakpoint/BreakpointResolver.h" #include "lldb/Core/Value.h" #include "lldb/Symbol/Type.h" -#include "lldb/Target/CPPLanguageRuntime.h" #include "lldb/Target/LanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" + namespace lldb_private { class ItaniumABILanguageRuntime : public lldb_private::CPPLanguageRuntime { Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp @@ -24,7 +24,6 @@ #include "lldb/Expression/FunctionCaller.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Symbol/ObjectFile.h" -#include "lldb/Target/CPPLanguageRuntime.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/Process.h" #include "lldb/Target/RegisterContext.h" @@ -39,6 +38,7 @@ #include "Plugins/Process/Utility/HistoryThread.h" #include "Plugins/Language/ObjC/NSString.h" +#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" #include Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/CMakeLists.txt =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/CMakeLists.txt +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/CMakeLists.txt @@ -19,6 +19,7 @@ lldbTarget lldbUtility lldbPluginExpressionParserClang + lldbPluginCPPRuntime LINK_COMPONENTS Support ) Index: lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.h +++ lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.h @@ -19,10 +19,11 @@ #include "llvm/ADT/StringRef.h" #include "lldb/Core/Module.h" #include "lldb/Expression/LLVMUserExpression.h" -#include "lldb/Target/CPPLanguageRuntime.h" #include "lldb/Target/LanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" + namespace lldb_private { namespace lldb_renderscript { Index: lldb/trunk/source/Target/CMakeLists.txt =================================================================== --- lldb/trunk/source/Target/CMakeLists.txt +++ lldb/trunk/source/Target/CMakeLists.txt @@ -1,6 +1,5 @@ add_lldb_library(lldbTarget ABI.cpp - CPPLanguageRuntime.cpp ExecutionContext.cpp JITLoader.cpp JITLoaderList.cpp Index: lldb/trunk/source/Target/CPPLanguageRuntime.cpp =================================================================== --- lldb/trunk/source/Target/CPPLanguageRuntime.cpp +++ lldb/trunk/source/Target/CPPLanguageRuntime.cpp @@ -1,354 +0,0 @@ -//===-- CPPLanguageRuntime.cpp -// -// 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 "lldb/Target/CPPLanguageRuntime.h" -#include "lldb/Target/ObjCLanguageRuntime.h" - -#include - -#include - -#include "llvm/ADT/StringRef.h" - -#include "lldb/Symbol/Block.h" -#include "lldb/Symbol/Variable.h" -#include "lldb/Symbol/VariableList.h" - -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/UniqueCStringMap.h" -#include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Target/ABI.h" -#include "lldb/Target/ExecutionContext.h" -#include "lldb/Target/RegisterContext.h" -#include "lldb/Target/SectionLoadList.h" -#include "lldb/Target/StackFrame.h" -#include "lldb/Target/ThreadPlanRunToAddress.h" -#include "lldb/Target/ThreadPlanStepInRange.h" - -using namespace lldb; -using namespace lldb_private; - -static ConstString g_this = ConstString("this"); - -char CPPLanguageRuntime::ID = 0; - -// Destructor -CPPLanguageRuntime::~CPPLanguageRuntime() {} - -CPPLanguageRuntime::CPPLanguageRuntime(Process *process) - : LanguageRuntime(process) {} - -bool CPPLanguageRuntime::IsWhitelistedRuntimeValue(ConstString name) { - return name == g_this; -} - -bool CPPLanguageRuntime::GetObjectDescription(Stream &str, - ValueObject &object) { - // C++ has no generic way to do this. - return false; -} - -bool CPPLanguageRuntime::GetObjectDescription( - Stream &str, Value &value, ExecutionContextScope *exe_scope) { - // C++ has no generic way to do this. - return false; -} - -CPPLanguageRuntime::LibCppStdFunctionCallableInfo -CPPLanguageRuntime::FindLibCppStdFunctionCallableInfo( - lldb::ValueObjectSP &valobj_sp) { - LibCppStdFunctionCallableInfo optional_info; - - if (!valobj_sp) - return optional_info; - - // Member __f_ has type __base*, the contents of which will hold: - // 1) a vtable entry which may hold type information needed to discover the - // lambda being called - // 2) possibly hold a pointer to the callable object - // e.g. - // - // (lldb) frame var -R f_display - // (std::__1::function) f_display = { - // __buf_ = { - // … - // } - // __f_ = 0x00007ffeefbffa00 - // } - // (lldb) memory read -fA 0x00007ffeefbffa00 - // 0x7ffeefbffa00: ... `vtable for std::__1::__function::__funcGetChildMemberWithName(ConstString("__f_"), true)); - - if (member__f_) { - ValueObjectSP sub_member__f_( - member__f_->GetChildMemberWithName(ConstString("__f_"), true)); - - if (sub_member__f_) - member__f_ = sub_member__f_; - } - - lldb::addr_t member__f_pointer_value = member__f_->GetValueAsUnsigned(0); - - optional_info.member__f_pointer_value = member__f_pointer_value; - - ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef()); - Process *process = exe_ctx.GetProcessPtr(); - - if (process == nullptr) - return optional_info; - - uint32_t address_size = process->GetAddressByteSize(); - Status status; - - // First item pointed to by __f_ should be the pointer to the vtable for - // a __base object. - lldb::addr_t vtable_address = - process->ReadPointerFromMemory(member__f_pointer_value, status); - - if (status.Fail()) - return optional_info; - - lldb::addr_t address_after_vtable = member__f_pointer_value + address_size; - // As commened above we may not have a function pointer but if we do we will - // need it. - lldb::addr_t possible_function_address = - process->ReadPointerFromMemory(address_after_vtable, status); - - if (status.Fail()) - return optional_info; - - Target &target = process->GetTarget(); - - if (target.GetSectionLoadList().IsEmpty()) - return optional_info; - - Address vtable_addr_resolved; - SymbolContext sc; - Symbol *symbol; - - if (!target.GetSectionLoadList().ResolveLoadAddress(vtable_address, - vtable_addr_resolved)) - return optional_info; - - target.GetImages().ResolveSymbolContextForAddress( - vtable_addr_resolved, eSymbolContextEverything, sc); - symbol = sc.symbol; - - if (symbol == nullptr) - return optional_info; - - llvm::StringRef vtable_name(symbol->GetName().GetCString()); - bool found_expected_start_string = - vtable_name.startswith("vtable for std::__1::__function::__func<"); - - if (!found_expected_start_string) - return optional_info; - - // Given case 1 or 3 we have a vtable name, we are want to extract the first - // template parameter - // - // ... __func ... - // ^^^^^^^^^ - // - // We do this by find the first < and , and extracting in between. - // - // This covers the case of the lambda known at compile time. - size_t first_open_angle_bracket = vtable_name.find('<') + 1; - size_t first_comma = vtable_name.find(','); - - llvm::StringRef first_template_parameter = - vtable_name.slice(first_open_angle_bracket, first_comma); - - Address function_address_resolved; - - // Setup for cases 2, 4 and 5 we have a pointer to a function after the - // vtable. We will use a process of elimination to drop through each case - // and obtain the data we need. - if (target.GetSectionLoadList().ResolveLoadAddress( - possible_function_address, function_address_resolved)) { - target.GetImages().ResolveSymbolContextForAddress( - function_address_resolved, eSymbolContextEverything, sc); - symbol = sc.symbol; - } - - auto get_name = [&first_template_parameter, &symbol]() { - // Given case 1: - // - // main::$_0 - // - // we want to append ::operator()() - if (first_template_parameter.contains("$_")) - return llvm::Regex::escape(first_template_parameter.str()) + - R"(::operator\(\)\(.*\))"; - - if (symbol != nullptr && - symbol->GetName().GetStringRef().contains("__invoke")) { - - llvm::StringRef symbol_name = symbol->GetName().GetStringRef(); - size_t pos2 = symbol_name.find_last_of(':'); - - // Given case 2: - // - // main::$_1::__invoke(...) - // - // We want to slice off __invoke(...) and append operator()() - std::string lambda_operator = - llvm::Regex::escape(symbol_name.slice(0, pos2 + 1).str()) + - R"(operator\(\)\(.*\))"; - - return lambda_operator; - } - - // Case 3 - return first_template_parameter.str() + R"(::operator\(\)\(.*\))"; - ; - }; - - std::string func_to_match = get_name(); - - SymbolContextList scl; - - target.GetImages().FindSymbolsMatchingRegExAndType( - RegularExpression{R"(^)" + func_to_match}, eSymbolTypeAny, scl, true); - - // Case 1,2 or 3 - if (scl.GetSize() >= 1) { - SymbolContext sc2 = scl[0]; - - AddressRange range; - sc2.GetAddressRange(eSymbolContextEverything, 0, false, range); - - Address address = range.GetBaseAddress(); - - Address addr; - if (target.ResolveLoadAddress(address.GetCallableLoadAddress(&target), - addr)) { - LineEntry line_entry; - addr.CalculateSymbolContextLineEntry(line_entry); - - if (first_template_parameter.contains("$_") || - (symbol != nullptr && - symbol->GetName().GetStringRef().contains("__invoke"))) { - // Case 1 and 2 - optional_info.callable_case = LibCppStdFunctionCallableCase::Lambda; - } else { - // Case 3 - optional_info.callable_case = - LibCppStdFunctionCallableCase::CallableObject; - } - - optional_info.callable_symbol = *symbol; - optional_info.callable_line_entry = line_entry; - optional_info.callable_address = addr; - return optional_info; - } - } - - // Case 4 or 5 - if (symbol && !symbol->GetName().GetStringRef().startswith("vtable for")) { - optional_info.callable_case = - LibCppStdFunctionCallableCase::FreeOrMemberFunction; - optional_info.callable_address = function_address_resolved; - optional_info.callable_symbol = *symbol; - - return optional_info; - } - - return optional_info; -} - -lldb::ThreadPlanSP -CPPLanguageRuntime::GetStepThroughTrampolinePlan(Thread &thread, - bool stop_others) { - ThreadPlanSP ret_plan_sp; - - lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC(); - - TargetSP target_sp(thread.CalculateTarget()); - - if (target_sp->GetSectionLoadList().IsEmpty()) - return ret_plan_sp; - - Address pc_addr_resolved; - SymbolContext sc; - Symbol *symbol; - - if (!target_sp->GetSectionLoadList().ResolveLoadAddress(curr_pc, - pc_addr_resolved)) - return ret_plan_sp; - - target_sp->GetImages().ResolveSymbolContextForAddress( - pc_addr_resolved, eSymbolContextEverything, sc); - symbol = sc.symbol; - - if (symbol == nullptr) - return ret_plan_sp; - - llvm::StringRef function_name(symbol->GetName().GetCString()); - - // Handling the case where we are attempting to step into std::function. - // The behavior will be that we will attempt to obtain the wrapped - // callable via FindLibCppStdFunctionCallableInfo() and if we find it we - // will return a ThreadPlanRunToAddress to the callable. Therefore we will - // step into the wrapped callable. - // - bool found_expected_start_string = - function_name.startswith("std::__1::function<"); - - if (!found_expected_start_string) - return ret_plan_sp; - - AddressRange range_of_curr_func; - sc.GetAddressRange(eSymbolContextEverything, 0, false, range_of_curr_func); - - StackFrameSP frame = thread.GetStackFrameAtIndex(0); - - if (frame) { - ValueObjectSP value_sp = frame->FindVariable(g_this); - - CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info = - FindLibCppStdFunctionCallableInfo(value_sp); - - if (callable_info.callable_case != LibCppStdFunctionCallableCase::Invalid && - value_sp->GetValueIsValid()) { - // We found the std::function wrapped callable and we have its address. - // We now create a ThreadPlan to run to the callable. - ret_plan_sp = std::make_shared( - thread, callable_info.callable_address, stop_others); - return ret_plan_sp; - } else { - // We are in std::function but we could not obtain the callable. - // We create a ThreadPlan to keep stepping through using the address range - // of the current function. - ret_plan_sp = std::make_shared( - thread, range_of_curr_func, sc, eOnlyThisThread, eLazyBoolYes, - eLazyBoolYes); - return ret_plan_sp; - } - } - - return ret_plan_sp; -}