LLDB mainline
ValueObjectVTable.cpp
Go to the documentation of this file.
1//===-- ValueObjectVTable.cpp ---------------------------------------------===//
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#include "lldb/Core/Module.h"
15#include "lldb/lldb-defines.h"
17#include "lldb/lldb-forward.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
24public:
25 ValueObjectVTableChild(ValueObject &parent, uint32_t func_idx,
26 uint64_t addr_size)
27 : ValueObject(parent), m_func_idx(func_idx), m_addr_size(addr_size) {
29 SetName(ConstString(llvm::formatv("[{0}]", func_idx).str()));
30 }
31
32 ~ValueObjectVTableChild() override = default;
33
34 std::optional<uint64_t> GetByteSize() override { return m_addr_size; };
35
36 llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override {
37 return 0;
38 };
39
40 ValueType GetValueType() const override { return eValueTypeVTableEntry; };
41
42 bool IsInScope() override {
43 if (ValueObject *parent = GetParent())
44 return parent->IsInScope();
45 return false;
46 };
47
48protected:
49 bool UpdateValue() override {
50 SetValueIsValid(false);
51 m_value.Clear();
52 ValueObject *parent = GetParent();
53 if (!parent) {
54 m_error = Status::FromErrorString("owning vtable object not valid");
55 return false;
56 }
57
58 addr_t parent_addr = parent->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
59 if (parent_addr == LLDB_INVALID_ADDRESS) {
60 m_error = Status::FromErrorString("invalid vtable address");
61 return false;
62 }
63
64 ProcessSP process_sp = GetProcessSP();
65 if (!process_sp) {
66 m_error = Status::FromErrorString("no process");
67 return false;
68 }
69
70 TargetSP target_sp = GetTargetSP();
71 if (!target_sp) {
72 m_error = Status::FromErrorString("no target");
73 return false;
74 }
75
76 // Each `vtable_entry_addr` points to the function pointer.
77 addr_t vtable_entry_addr = parent_addr + m_func_idx * m_addr_size;
78 addr_t vfunc_ptr =
79 process_sp->ReadPointerFromMemory(vtable_entry_addr, m_error);
80 if (m_error.Fail()) {
82 "failed to read virtual function entry 0x%16.16" PRIx64,
83 vtable_entry_addr);
84 return false;
85 }
86
87 // Set our value to be the load address of the function pointer in memory
88 // and our type to be the function pointer type.
89 m_value.SetValueType(Value::ValueType::LoadAddress);
90 m_value.GetScalar() = vtable_entry_addr;
91
92 // See if our resolved address points to a function in the debug info. If
93 // it does, then we can report the type as a function prototype for this
94 // function.
95 Function *function = nullptr;
96 Address resolved_vfunc_ptr_address;
97 target_sp->ResolveLoadAddress(vfunc_ptr, resolved_vfunc_ptr_address);
98 if (resolved_vfunc_ptr_address.IsValid())
99 function = resolved_vfunc_ptr_address.CalculateSymbolContextFunction();
100 if (function) {
102 } else {
103 // Set our value's compiler type to a generic function protoype so that
104 // it displays as a hex function pointer for the value and the summary
105 // will display the address description.
106
107 // Get the original type that this vtable is based off of so we can get
108 // the language from it correctly.
109 ValueObject *val = parent->GetParent();
110 auto type_system = target_sp->GetScratchTypeSystemForLanguage(
112 if (type_system) {
114 (*type_system)->CreateGenericFunctionPrototype().GetPointerType());
115 } else {
116 consumeError(type_system.takeError());
117 }
118 }
119
120 // Now read our value into m_data so that our we can use the default
121 // summary provider for C++ for function pointers which will get the
122 // address description for our function pointer.
123 if (m_error.Success()) {
124 const bool thread_and_frame_only_if_stopped = true;
125 ExecutionContext exe_ctx(
126 GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
127 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
128 }
129 SetValueDidChange(true);
130 SetValueIsValid(true);
131 return true;
132 };
133
135 return m_value.GetCompilerType();
136 };
137
138 const uint32_t m_func_idx;
139 const uint64_t m_addr_size;
140
141private:
142 // For ValueObject only
146};
147
149 return (new ValueObjectVTable(parent))->GetSP();
150}
151
153 : ValueObject(parent) {
155}
156
157std::optional<uint64_t> ValueObjectVTable::GetByteSize() {
158 if (m_vtable_symbol)
160 return std::nullopt;
161}
162
163llvm::Expected<uint32_t> ValueObjectVTable::CalculateNumChildren(uint32_t max) {
164 if (UpdateValueIfNeeded(false))
165 return m_num_vtable_entries <= max ? m_num_vtable_entries : max;
166 return 0;
167}
168
170
172 if (m_vtable_symbol)
173 return m_vtable_symbol->GetName();
174 return ConstString();
175}
176
178
180 if (m_vtable_symbol)
182 return ConstString();
183}
184
186
188 return new ValueObjectVTableChild(*this, idx, m_addr_size);
189}
190
192 m_error.Clear();
194 SetValueIsValid(false);
196 ValueObject *parent = GetParent();
197 if (!parent) {
198 m_error = Status::FromErrorString("no parent object");
199 return false;
200 }
201
202 ProcessSP process_sp = GetProcessSP();
203 if (!process_sp) {
204 m_error = Status::FromErrorString("no process");
205 return false;
206 }
207
208 const LanguageType language = parent->GetObjectRuntimeLanguage();
209 LanguageRuntime *language_runtime = process_sp->GetLanguageRuntime(language);
210
211 if (language_runtime == nullptr) {
213 "no language runtime support for the language \"%s\"",
215 return false;
216 }
217
218 // Get the vtable information from the language runtime.
219 llvm::Expected<LanguageRuntime::VTableInfo> vtable_info_or_err =
220 language_runtime->GetVTableInfo(*parent, /*check_type=*/true);
221 if (!vtable_info_or_err) {
222 m_error = Status::FromError(vtable_info_or_err.takeError());
223 return false;
224 }
225
226 TargetSP target_sp = GetTargetSP();
227 const addr_t vtable_start_addr =
228 vtable_info_or_err->addr.GetLoadAddress(target_sp.get());
229
230 m_vtable_symbol = vtable_info_or_err->symbol;
231 if (!m_vtable_symbol) {
233 "no vtable symbol found containing 0x%" PRIx64, vtable_start_addr);
234 return false;
235 }
236
237 // Now that we know it's a vtable, we update the object's state.
239
240 // Calculate the number of entries
243 "vtable symbol \"%s\" doesn't have a valid size",
245 return false;
246 }
247
248 m_addr_size = process_sp->GetAddressByteSize();
249 const addr_t vtable_end_addr =
250 m_vtable_symbol->GetLoadAddress(target_sp.get()) +
252 m_num_vtable_entries = (vtable_end_addr - vtable_start_addr) / m_addr_size;
253
255 m_value.GetScalar() = parent->GetAddressOf();
256 auto type_system_or_err =
257 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC_plus_plus);
258 if (type_system_or_err) {
260 (*type_system_or_err)->GetBasicTypeFromAST(eBasicTypeUnsignedLong));
261 } else {
262 consumeError(type_system_or_err.takeError());
263 }
264 SetValueDidChange(true);
265 SetValueIsValid(true);
266 return true;
267}
268
270
ValueObjectVTableChild(ValueObject &parent, uint32_t func_idx, uint64_t addr_size)
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
ValueObjectVTableChild(const ValueObjectVTableChild &)=delete
~ValueObjectVTableChild() override=default
CompilerType GetCompilerTypeImpl() override
std::optional< uint64_t > GetByteSize() override
const ValueObjectVTableChild & operator=(const ValueObjectVTableChild &)=delete
ValueType GetValueType() const override
A section + offset based address class.
Definition: Address.h:62
Function * CalculateSymbolContextFunction() const
Definition: Address.cpp:872
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A class that describes a function.
Definition: Function.h:399
CompilerType GetCompilerType()
Definition: Function.cpp:587
virtual llvm::Expected< VTableInfo > GetVTableInfo(ValueObject &in_value, bool check_type)
Get the vtable information for a given value.
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:266
ConstString GetDemangledName() const
Demangled name get accessor.
Definition: Mangled.cpp:270
void Clear()
Clear the object state.
Definition: Status.cpp:215
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: Status.cpp:106
static Status FromErrorString(const char *str)
Definition: Status.h:138
bool Fail() const
Test for error condition.
Definition: Status.cpp:270
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition: Status.cpp:137
bool Success() const
Test for success condition.
Definition: Status.cpp:280
lldb::addr_t GetLoadAddress(Target *target) const
Definition: Symbol.cpp:541
bool GetByteSizeIsValid() const
Definition: Symbol.h:208
Mangled & GetMangled()
Definition: Symbol.h:146
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:468
ConstString GetName() const
Definition: Symbol.cpp:548
ConstString GetDisplayName() const
Definition: Symbol.cpp:169
A class that represents a virtual function table for a C++ class.
const Symbol * m_vtable_symbol
The symbol for the C++ virtual function table.
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
uint32_t m_addr_size
Cache the address size in bytes to avoid checking with the process to many times.
ConstString GetQualifiedTypeName() override
ValueObject * CreateChildAtIndex(size_t idx) override
Should only be called by ValueObject::GetChildAtIndex().
static lldb::ValueObjectSP Create(ValueObject &parent)
lldb::ValueType GetValueType() const override
ValueObjectVTable(ValueObject &parent)
std::optional< uint64_t > GetByteSize() override
ConstString GetTypeName() override
uint32_t m_num_vtable_entries
Cache the number of vtable children when we update the value.
ConstString GetDisplayTypeName() override
CompilerType GetCompilerTypeImpl() override
void SetValueIsValid(bool valid)
Definition: ValueObject.h:1059
virtual bool IsInScope()
Definition: ValueObject.h:420
struct lldb_private::ValueObject::Bitflags m_flags
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:930
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:338
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:926
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:1055
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
lldb::LanguageType GetObjectRuntimeLanguage()
Definition: ValueObject.h:373
virtual lldb::addr_t GetAddressOf(bool scalar_is_load_address=true, AddressType *address_type=nullptr)
bool UpdateValueIfNeeded(bool update_format=true)
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:574
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
virtual ValueObject * GetParent()
Definition: ValueObject.h:828
virtual void SetFormat(lldb::Format format)
Definition: ValueObject.h:776
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
const Scalar & GetScalar() const
Definition: Value.h:112
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:315
@ LoadAddress
A load address value.
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
void SetValueType(ValueType value_type)
Definition: Value.h:89
const CompilerType & GetCompilerType()
Definition: Value.cpp:239
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
@ eBasicTypeUnsignedLong
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
LanguageType
Programming language type.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:448
@ eValueTypeVTableEntry
function pointer in virtual function table
@ eValueTypeVTable
virtual function table