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(llvm::formatv("[{0}]", func_idx).str());
30 }
31
32 ~ValueObjectVTableChild() override = default;
33
34 llvm::Expected<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 parent_addr = process_sp->FixCodeAddress(parent_addr);
77
78 // Each `vtable_entry_addr` points to the function pointer.
79 addr_t vtable_entry_addr = parent_addr + m_func_idx * m_addr_size;
80 addr_t vfunc_ptr =
81 process_sp->ReadPointerFromMemory(vtable_entry_addr, m_error);
82 if (m_error.Fail()) {
84 "failed to read virtual function entry 0x%16.16" PRIx64,
85 vtable_entry_addr);
86 return false;
87 }
88
89 vfunc_ptr = process_sp->FixCodeAddress(vfunc_ptr);
90
91 // Set our value to be the load address of the function pointer in memory
92 // and our type to be the function pointer type.
94 m_value.GetScalar() = vtable_entry_addr;
95
96 // See if our resolved address points to a function in the debug info. If
97 // it does, then we can report the type as a function prototype for this
98 // function.
99 Function *function = nullptr;
100 Address resolved_vfunc_ptr_address;
101 target_sp->ResolveLoadAddress(vfunc_ptr, resolved_vfunc_ptr_address);
102 if (resolved_vfunc_ptr_address.IsValid())
103 function = resolved_vfunc_ptr_address.CalculateSymbolContextFunction();
104 if (function) {
105 m_value.SetCompilerType(function->GetCompilerType().GetPointerType());
106 } else {
107 // Set our value's compiler type to a generic function protoype so that
108 // it displays as a hex function pointer for the value and the summary
109 // will display the address description.
110
111 // Get the original type that this vtable is based off of so we can get
112 // the language from it correctly.
113 ValueObject *val = parent->GetParent();
114 auto type_system = target_sp->GetScratchTypeSystemForLanguage(
116 if (type_system) {
117 m_value.SetCompilerType(
118 (*type_system)->CreateGenericFunctionPrototype().GetPointerType());
119 } else {
120 consumeError(type_system.takeError());
121 }
122 }
123
124 // Now read our value into m_data so that our we can use the default
125 // summary provider for C++ for function pointers which will get the
126 // address description for our function pointer.
127 if (m_error.Success()) {
128 const bool thread_and_frame_only_if_stopped = true;
129 ExecutionContext exe_ctx(
130 GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
131 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
132 }
133 SetValueDidChange(true);
134 SetValueIsValid(true);
135 return true;
136 };
137
139 return m_value.GetCompilerType();
140 };
141
142 const uint32_t m_func_idx;
143 const uint64_t m_addr_size;
144
145private:
146 // For ValueObject only
150};
151
153 return (new ValueObjectVTable(parent))->GetSP();
154}
155
160
161llvm::Expected<uint64_t> ValueObjectVTable::GetByteSize() {
162 if (m_vtable_symbol)
163 return m_vtable_symbol->GetByteSize();
164 return llvm::createStringError("no symbol for vtable");
165}
166
167llvm::Expected<uint32_t> ValueObjectVTable::CalculateNumChildren(uint32_t max) {
168 if (UpdateValueIfNeeded(false))
169 return m_num_vtable_entries <= max ? m_num_vtable_entries : max;
170 return 0;
171}
172
174
176 if (m_vtable_symbol)
177 return m_vtable_symbol->GetName();
178 return ConstString();
179}
180
182
184 if (m_vtable_symbol)
185 return m_vtable_symbol->GetDisplayName();
186 return ConstString();
187}
188
190
194
196 m_error.Clear();
197 m_flags.m_children_count_valid = false;
198 SetValueIsValid(false);
200 ValueObject *parent = GetParent();
201 if (!parent) {
202 m_error = Status::FromErrorString("no parent object");
203 return false;
204 }
205
206 ProcessSP process_sp = GetProcessSP();
207 if (!process_sp) {
208 m_error = Status::FromErrorString("no process");
209 return false;
210 }
211
212 const LanguageType language = parent->GetObjectRuntimeLanguage();
213 LanguageRuntime *language_runtime = process_sp->GetLanguageRuntime(language);
214
215 if (language_runtime == nullptr) {
217 "no language runtime support for the language \"%s\"",
219 return false;
220 }
221
222 // Get the vtable information from the language runtime.
223 llvm::Expected<LanguageRuntime::VTableInfo> vtable_info_or_err =
224 language_runtime->GetVTableInfo(*parent, /*check_type=*/true);
225 if (!vtable_info_or_err) {
226 m_error = Status::FromError(vtable_info_or_err.takeError());
227 return false;
228 }
229
230 TargetSP target_sp = GetTargetSP();
231 const addr_t vtable_start_addr =
232 vtable_info_or_err->addr.GetLoadAddress(target_sp.get());
233
234 m_vtable_symbol = vtable_info_or_err->symbol;
235 if (!m_vtable_symbol) {
237 "no vtable symbol found containing 0x%" PRIx64, vtable_start_addr);
238 return false;
239 }
240
241 // Now that we know it's a vtable, we update the object's state.
243
244 // Calculate the number of entries
245 if (!m_vtable_symbol->GetByteSizeIsValid()) {
247 "vtable symbol \"%s\" doesn't have a valid size",
248 m_vtable_symbol->GetMangled().GetDemangledName().GetCString());
249 return false;
250 }
251
252 m_addr_size = process_sp->GetAddressByteSize();
253 const addr_t vtable_end_addr =
254 m_vtable_symbol->GetLoadAddress(target_sp.get()) +
255 m_vtable_symbol->GetByteSize();
256 m_num_vtable_entries = (vtable_end_addr - vtable_start_addr) / m_addr_size;
257
259 m_value.GetScalar() = parent->GetAddressOf().address;
260 auto type_system_or_err =
261 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC_plus_plus);
262 if (type_system_or_err) {
263 m_value.SetCompilerType(
264 (*type_system_or_err)->GetBasicTypeFromAST(eBasicTypeUnsignedLong));
265 } else {
266 consumeError(type_system_or_err.takeError());
267 }
268 SetValueDidChange(true);
269 SetValueIsValid(true);
270 return true;
271}
272
274
ValueObjectVTableChild(ValueObject &parent, uint32_t func_idx, uint64_t addr_size)
llvm::Expected< uint64_t > GetByteSize() override
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
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:859
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
Generic representation of a type in a programming language.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
A uniqued constant string class.
Definition ConstString.h:40
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A class that describes a function.
Definition Function.h:392
CompilerType GetCompilerType()
Definition Function.cpp:576
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)
Returns the internal LLDB name for the specified language.
Definition Language.cpp:305
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
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
uint32_t m_num_vtable_entries
Cache the number of vtable children when we update the value.
ConstString GetDisplayTypeName() override
CompilerType GetCompilerTypeImpl() override
llvm::Expected< uint64_t > GetByteSize() override
void SetValueIsValid(bool valid)
struct lldb_private::ValueObject::Bitflags m_flags
ValueObject(ExecutionContextScope *exe_scope, ValueObjectManager &manager, AddressType child_ptr_or_ref_addr_type=eAddressTypeLoad)
Use this constructor to create a "root variable object".
Status m_error
An error object that can describe any errors that occur when updating values.
lldb::ProcessSP GetProcessSP() const
DataExtractor m_data
A data extractor that can be used to extract the value.
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
void SetValueDidChange(bool value_changed)
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()
void SetName(llvm::StringRef name)
Change the name of the current ValueObject.
bool UpdateValueIfNeeded(bool update_format=true)
lldb::TargetSP GetTargetSP() const
virtual ValueObject * GetParent()
virtual void SetFormat(lldb::Format format)
const ExecutionContextRef & GetExecutionContextRef() const
virtual AddrAndType GetAddressOf(bool scalar_is_load_address=true)
@ LoadAddress
A load address value.
Definition Value.h:49
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
@ eBasicTypeUnsignedLong
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
LanguageType
Programming language type.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Process > ProcessSP
@ eValueTypeVTableEntry
function pointer in virtual function table
@ eValueTypeVTable
virtual function table
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP