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 size_t CalculateNumChildren(uint32_t max) override { return 0; };
37
38 ValueType GetValueType() const override { return eValueTypeVTableEntry; };
39
40 bool IsInScope() override {
41 if (ValueObject *parent = GetParent())
42 return parent->IsInScope();
43 return false;
44 };
45
46protected:
47 bool UpdateValue() override {
48 SetValueIsValid(false);
49 m_value.Clear();
50 ValueObject *parent = GetParent();
51 if (!parent) {
52 m_error.SetErrorString("owning vtable object not valid");
53 return false;
54 }
55
56 addr_t parent_addr = parent->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
57 if (parent_addr == LLDB_INVALID_ADDRESS) {
58 m_error.SetErrorString("invalid vtable address");
59 return false;
60 }
61
62 ProcessSP process_sp = GetProcessSP();
63 if (!process_sp) {
64 m_error.SetErrorString("no process");
65 return false;
66 }
67
68 TargetSP target_sp = GetTargetSP();
69 if (!target_sp) {
70 m_error.SetErrorString("no target");
71 return false;
72 }
73
74 // Each `vtable_entry_addr` points to the function pointer.
75 addr_t vtable_entry_addr = parent_addr + m_func_idx * m_addr_size;
76 addr_t vfunc_ptr =
77 process_sp->ReadPointerFromMemory(vtable_entry_addr, m_error);
78 if (m_error.Fail()) {
80 "failed to read virtual function entry 0x%16.16" PRIx64,
81 vtable_entry_addr);
82 return false;
83 }
84
85
86 // Set our value to be the load address of the function pointer in memory
87 // and our type to be the function pointer type.
88 m_value.SetValueType(Value::ValueType::LoadAddress);
89 m_value.GetScalar() = vtable_entry_addr;
90
91 // See if our resolved address points to a function in the debug info. If
92 // it does, then we can report the type as a function prototype for this
93 // function.
94 Function *function = nullptr;
95 Address resolved_vfunc_ptr_address;
96 target_sp->ResolveLoadAddress(vfunc_ptr, resolved_vfunc_ptr_address);
97 if (resolved_vfunc_ptr_address.IsValid())
98 function = resolved_vfunc_ptr_address.CalculateSymbolContextFunction();
99 if (function) {
101 } else {
102 // Set our value's compiler type to a generic function protoype so that
103 // it displays as a hex function pointer for the value and the summary
104 // will display the address description.
105
106 // Get the original type that this vtable is based off of so we can get
107 // the language from it correctly.
108 ValueObject *val = parent->GetParent();
109 auto type_system = target_sp->GetScratchTypeSystemForLanguage(
111 if (type_system) {
113 (*type_system)->CreateGenericFunctionPrototype().GetPointerType());
114 } else {
115 consumeError(type_system.takeError());
116 }
117 }
118
119 // Now read our value into m_data so that our we can use the default
120 // summary provider for C++ for function pointers which will get the
121 // address description for our function pointer.
122 if (m_error.Success()) {
123 const bool thread_and_frame_only_if_stopped = true;
124 ExecutionContext exe_ctx(
125 GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
126 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
127 }
128 SetValueDidChange(true);
129 SetValueIsValid(true);
130 return true;
131 };
132
134 return m_value.GetCompilerType();
135 };
136
137 const uint32_t m_func_idx;
138 const uint64_t m_addr_size;
139
140private:
141 // For ValueObject only
145};
146
148 return (new ValueObjectVTable(parent))->GetSP();
149}
150
152 : ValueObject(parent) {
154}
155
156std::optional<uint64_t> ValueObjectVTable::GetByteSize() {
157 if (m_vtable_symbol)
159 return std::nullopt;
160}
161
163 if (UpdateValueIfNeeded(false))
164 return m_num_vtable_entries <= max ? m_num_vtable_entries : max;
165 return 0;
166}
167
169
171 if (m_vtable_symbol)
172 return m_vtable_symbol->GetName();
173 return ConstString();
174}
175
177
179 if (m_vtable_symbol)
181 return ConstString();
182}
183
185
187 bool synthetic_array_member,
188 int32_t synthetic_index) {
189 if (synthetic_array_member)
190 return nullptr;
191 return new ValueObjectVTableChild(*this, idx, m_addr_size);
192}
193
195 m_error.Clear();
197 SetValueIsValid(false);
199 ValueObject *parent = GetParent();
200 if (!parent) {
201 m_error.SetErrorString("no parent object");
202 return false;
203 }
204
205 ProcessSP process_sp = GetProcessSP();
206 if (!process_sp) {
207 m_error.SetErrorString("no process");
208 return false;
209 }
210
211 const LanguageType language = parent->GetObjectRuntimeLanguage();
212 LanguageRuntime *language_runtime = process_sp->GetLanguageRuntime(language);
213
214 if (language_runtime == nullptr) {
216 "no language runtime support for the language \"%s\"",
218 return false;
219 }
220
221 // Get the vtable information from the language runtime.
222 llvm::Expected<LanguageRuntime::VTableInfo> vtable_info_or_err =
223 language_runtime->GetVTableInfo(*parent, /*check_type=*/true);
224 if (!vtable_info_or_err) {
225 m_error = vtable_info_or_err.takeError();
226 return false;
227 }
228
229 TargetSP target_sp = GetTargetSP();
230 const addr_t vtable_start_addr =
231 vtable_info_or_err->addr.GetLoadAddress(target_sp.get());
232
233 m_vtable_symbol = vtable_info_or_err->symbol;
234 if (!m_vtable_symbol) {
236 "no vtable symbol found containing 0x%" PRIx64, vtable_start_addr);
237 return false;
238 }
239
240 // Now that we know it's a vtable, we update the object's state.
242
243 // Calculate the number of entries
246 "vtable symbol \"%s\" doesn't have a valid size",
248 return false;
249 }
250
251 m_addr_size = process_sp->GetAddressByteSize();
252 const addr_t vtable_end_addr =
253 m_vtable_symbol->GetLoadAddress(target_sp.get()) +
255 m_num_vtable_entries = (vtable_end_addr - vtable_start_addr) / m_addr_size;
256
258 m_value.GetScalar() = parent->GetAddressOf();
259 auto type_system_or_err =
260 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC_plus_plus);
261 if (type_system_or_err) {
263 (*type_system_or_err)->GetBasicTypeFromAST(eBasicTypeUnsignedLong));
264 } else {
265 consumeError(type_system_or_err.takeError());
266 }
267 SetValueDidChange(true);
268 SetValueIsValid(true);
269 return true;
270}
271
273
ValueObjectVTableChild(ValueObject &parent, uint32_t func_idx, uint64_t addr_size)
size_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:871
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:214
"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:559
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:235
ConstString GetDemangledName() const
Demangled name get accessor.
Definition: Mangled.cpp:260
void Clear()
Clear the object state.
Definition: Status.cpp:167
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:247
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:233
bool Success() const
Test for success condition.
Definition: Status.cpp:279
lldb::addr_t GetLoadAddress(Target *target) const
Definition: Symbol.cpp:545
bool GetByteSizeIsValid() const
Definition: Symbol.h:208
Mangled & GetMangled()
Definition: Symbol.h:146
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:472
ConstString GetName() const
Definition: Symbol.cpp:552
ConstString GetDisplayName() const
Definition: Symbol.cpp:173
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.
uint32_t m_addr_size
Cache the address size in bytes to avoid checking with the process to many times.
ConstString GetQualifiedTypeName() override
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
ValueObject * CreateChildAtIndex(size_t idx, bool synthetic_array_member, int32_t synthetic_index) override
Should only be called by ValueObject::GetChildAtIndex().
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
void SetValueIsValid(bool valid)
Definition: ValueObject.h:969
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:844
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:840
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:965
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:544
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
virtual ValueObject * GetParent()
Definition: ValueObject.h:742
virtual void SetFormat(lldb::Format format)
Definition: ValueObject.h:694
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: SBAttachInfo.h:14
Definition: SBAddress.h:15
@ eBasicTypeUnsignedLong
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
LanguageType
Programming language type.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:381
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
@ eValueTypeVTableEntry
function pointer in virtual function table
@ eValueTypeVTable
virtual function table