13#include "clang/AST/RecordLayout.h"
14#include "llvm/Demangle/MicrosoftDemangle.h"
15#include "llvm/Demangle/MicrosoftDemangleNodes.h"
25 "::`RTTI Complete Object Locator'";
29 name.consume_front(
"const ");
42 if (!suffix.consume_front(
"{for `") || !suffix.consume_back(
"'}"))
45 suffix.split(components,
"'s `");
56 std::optional<int64_t> offset_to_top =
63 if (!
m_process->GetTarget().ResolveLoadAddress(dynamic_addr, dynamic_address))
89 LLDB_LOG(log,
"{0:x16}: '{1}' is not a valid vtable symbol",
94 bool any_found =
false;
103 "static-type = '{0}' has dynamic type: uid={1:x}, type-name='{2}'",
104 in_value.
GetTypeName(), type_sp->GetID(), type_sp->GetName());
118 llvm::SmallVector<llvm::StringRef, 2> vtable_components;
119 class_type_or_name =
GetTypeInfo(in_value, vtable_info, vtable_components);
120 if (!class_type_or_name) {
121 LLDB_LOG(log,
"{0:x16}: failed to get type info",
128 LLDB_LOG(log,
"{0:x16}: failed to get type info (no compiler type)",
137 LLDB_LOG(log,
"{0:x16}: {1} is not a complete type",
142 LLDB_LOG(log,
"{0:x16}: {1} is not a complete type",
149 LLDB_LOG(log,
"{0:x16}: in type is the same as the dynamic type",
155 std::scoped_lock<std::mutex> g(
m_mutex);
161 std::scoped_lock<std::mutex> g(
m_mutex);
164 return offset_to_top_it->second;
178 llvm::StringRef strategy =
"RTTI";
187 "{0:x16}: [{1}] '{2}' has offset {3} in '{4}'",
194 std::scoped_lock<std::mutex> g(
m_mutex);
210 const uint32_t addr_byte_size =
m_process->GetAddressByteSize();
212 vtable_load_addr - addr_byte_size;
214 if (complete_object_locator_load_addr >= vtable_load_addr)
219 llvm::Expected<lldb::addr_t> rtti_locator_base_load_addr =
220 m_process->ReadPointerFromMemory(complete_object_locator_load_addr);
221 if (!rtti_locator_base_load_addr ||
224 "Failed to read address of RTTI complete object locator at "
226 complete_object_locator_load_addr);
231 Address rtti_locator_base_addr;
232 if (!
m_process->GetTarget().ResolveLoadAddress(*rtti_locator_base_load_addr,
233 rtti_locator_base_addr)) {
235 "Failed to resolve load address of RTTI complete object "
236 "locator at {1:x16}: {0}",
237 *rtti_locator_base_load_addr);
243 if (symbol ==
nullptr)
249 LLDB_LOG(log,
"Found RTTI complete object locator: {0:x16}",
250 *rtti_locator_base_load_addr);
257 constexpr uint32_t int_size = 4;
261 Address offset_from_top_addr = rtti_locator_base_addr;
262 offset_from_top_addr.
Slide(int_size);
266 offset_from_top_addr, int_size, INT64_MIN,
error);
267 if (offset_from_top == INT64_MIN ||
error.Fail()) {
269 "Failed to read 'offset from top' from RTTI complete object "
270 "locator at {1:x16}: {0}",
275 return offset_from_top;
280static std::optional<size_t>
282 const clang::CXXRecordDecl *haystack,
283 const clang::CXXRecordDecl *target_base,
284 const llvm::ArrayRef<llvm::StringRef> checkpoints) {
286 const clang::CXXRecordDecl *decl;
289 std::optional<size_t> offset;
291 llvm::ArrayRef<llvm::StringRef> checkpoints;
293 bool found_target =
false;
298 std::deque<BaseEntry> bases;
299 bases.emplace_back(BaseEntry{
306 std::string base_name;
307 for (
const clang::CXXBaseSpecifier &base : haystack->vbases()) {
308 const clang::CXXRecordDecl *base_decl = llvm::cast<clang::CXXRecordDecl>(
309 base.getType()->castAs<clang::RecordType>()->getDecl());
312 if (!checkpoints.empty()) {
313 base_name = base_decl->getDeclName().getAsString();
314 if (base_name == checkpoints.back())
316 }
else if (base_decl == target_base) {
322 bases.emplace_back(BaseEntry{
329 while (!bases.empty()) {
330 BaseEntry current = bases.front();
332 (!current.found_target || !current.checkpoints.empty()) &&
333 "if we found the target we should only need to check the checkpoints");
335 const clang::ASTRecordLayout &record_layout =
336 ast.getASTRecordLayout(current.decl);
338 for (
const clang::CXXBaseSpecifier &base : current.decl->bases()) {
339 if (base.isVirtual())
342 const clang::CXXRecordDecl *base_decl = llvm::cast<clang::CXXRecordDecl>(
343 base.getType()->castAs<clang::RecordType>()->getDecl());
344 bool found_target = base_decl == target_base || current.found_target;
345 std::optional<size_t> offset = current.offset;
347 *offset += record_layout.getBaseClassOffset(base_decl).getQuantity();
349 if (!current.checkpoints.empty()) {
350 base_name = base_decl->getDeclName().getAsString();
351 if (base_name == current.checkpoints.back()) {
353 if (found_target && current.checkpoints.size() == 1)
358 bases.emplace_back(BaseEntry{
361 current.checkpoints.drop_back(),
366 }
else if (found_target) {
370 bases.emplace_back(BaseEntry{
386 llvm::ArrayRef<llvm::StringRef> vtable_components) {
391 clang::CXXRecordDecl *in_decl =
393 clang::CXXRecordDecl *target_decl =
395 if (!in_decl || !target_decl) {
396 LLDB_LOG(log,
"{0:x16}: types are not record types (in={1}, target={2})",
404 LLDB_LOG(log,
"{0:x16}: '{1}' and '{2}' are from different type systems",
406 in_decl->getDeclName().getAsString(),
407 target_decl->getDeclName().getAsString());
412 ts->getASTContext(), target_decl, in_decl, vtable_components);
414 LLDB_LOG(log,
"{0:x16}: failed to find offset of '{1}' in '{2}'",
416 in_decl->getDeclName().getAsString(),
417 target_decl->getDeclName().getAsString());
429 bool has_nested_template = class_name.contains(
">>");
430 bool has_typename_list = class_name.contains(
", ");
432 if (!has_nested_template && !has_typename_list) {
443 llvm::MSDemangleFlags flags = llvm::MSDemangleFlags(
444 llvm::MSDF_CompactTemplateLists | llvm::MSDF_SplitTemplateClosers |
445 llvm::MSDF_NoTagSpecifier);
447 llvm::microsoftDemangle(mangled_name,
nullptr,
nullptr, flags);
450 llvm::scope_exit guard([&] { std::free(demangled); });
457 if (!has_typename_list)
462 flags = llvm::MSDemangleFlags(flags & ~llvm::MSDF_CompactTemplateLists);
463 std::free(demangled);
464 demangled = llvm::microsoftDemangle(mangled_name,
nullptr,
nullptr, flags);
470 type_sp =
LookupTypeByName(llvm::StringRef(demangled_ref), preferred_module,
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOG_ERROR(log, error,...)
static std::optional< size_t > GetOffsetOfBaseFromAST(clang::ASTContext &ast, const clang::CXXRecordDecl *haystack, const clang::CXXRecordDecl *target_base, const llvm::ArrayRef< llvm::StringRef > checkpoints)
Try to find the offset of target_base inside haystack while visiting all checkpoints.
static bool SplitVTableComponents(llvm::StringRef demangled, llvm::SmallVectorImpl< llvm::StringRef > &components)
static const llvm::StringRef s_rtti_complete_object_locator_indicator
static const llvm::StringRef s_vtable_demangled_indicator
static llvm::StringRef StripDemangledVTableSuffix(llvm::StringRef demangled)
A section + offset based address class.
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
void SetRawAddress(lldb::addr_t addr)
bool Slide(int64_t offset)
lldb::addr_t GetOffset() const
Get the section relative offset value.
bool IsSectionOffset() const
Check if an address is section offset.
Symbol * CalculateSymbolContextSymbol() const
CommonABIRuntime(Process *process)
void SetDynamicTypeInfo(const lldb_private::Address &vtable_addr, const TypeAndOrName &type_info)
lldb::TypeSP LookupTypeByName(llvm::StringRef type_name, lldb::ModuleSP preferred_module, bool &any_found) const
Find a type by its name, preferably in preferred_module.
TypeAndOrName GetDynamicTypeInfo(const lldb_private::Address &vtable_addr)
Generic representation of a type in a programming language.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
ConstString GetTypeName(bool BaseOnly=false) const
CompilerType GetPointeeType() const
If this type is a pointer type, return the type that the pointer points to, else return an invalid ty...
bool GetCompleteType() const
Type Completion.
bool IsPointerOrReferenceType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
A class that handles mangled names.
ConstString GetMangledName() const
Mangled name get accessor.
ConstString GetDemangledName() const
Demangled name get accessor.
bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, const LanguageRuntime::VTableInfo &vtable_info, TypeAndOrName &class_type_or_name, Address &dynamic_address) override
std::pair< lldb::TypeSP, ConstString > LookupTypeByMangled(Mangled &mangled, lldb::ModuleSP preferred_module, bool &any_found)
std::optional< int64_t > GetOffsetToTopFromAST(const CompilerType &in_type, ValueObject &in_value, const CompilerType &dyn_type, const LanguageRuntime::VTableInfo &vtable_info, llvm::ArrayRef< llvm::StringRef > vtable_components)
OffsetToTopCache m_offset_to_top_map
std::optional< int64_t > GetOffsetToTopFromRTTI(const LanguageRuntime::VTableInfo &vtable_info, const CompilerType &dynamic_type)
std::optional< int64_t > GetOffsetToTop(ValueObject &in_value, TypeAndOrName &class_type_or_name, const LanguageRuntime::VTableInfo &vtable_info)
bool IsVTableSymbol(Mangled &mangled) const override
MicrosoftABIRuntime(Process *process)
TypeAndOrName GetTypeInfo(ValueObject &in_value, const LanguageRuntime::VTableInfo &vtable_info, llvm::SmallVectorImpl< llvm::StringRef > &vtable_components)
A plug-in interface definition class for debugging a process.
lldb::ModuleSP CalculateSymbolContextModule() override
ConstString GetName() const
int64_t ReadSignedIntegerFromMemory(const Address &addr, size_t integer_byte_size, int64_t fail_value, Status &error, bool force_live_memory=false)
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
void SetName(ConstString type_name)
CompilerType GetCompilerType() const
void SetTypeSP(lldb::TypeSP type_sp)
A TypeSystem implementation based on Clang.
static bool AreTypesSame(CompilerType type1, CompilerType type2, bool ignore_qualifiers=false)
static clang::CXXRecordDecl * GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type)
virtual ConstString GetTypeName()
CompilerType GetCompilerType()
AddrAndType GetPointerValue()
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Type > TypeSP
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
std::shared_ptr< lldb_private::Module > ModuleSP
Symbol * symbol
Address of the vtable's virtual function table.