1 //===-- ValueObjectConstResult.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Core/ValueObjectConstResult.h"
11 
12 #include "lldb/Core/DataExtractor.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ValueObjectChild.h"
15 #include "lldb/Core/ValueObjectConstResultChild.h"
16 #include "lldb/Core/ValueObjectDynamicValue.h"
17 #include "lldb/Core/ValueObjectList.h"
18 
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/Symbol/Variable.h"
24 
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
33                                              ByteOrder byte_order,
34                                              uint32_t addr_byte_size,
35                                              lldb::addr_t address) {
36   return (new ValueObjectConstResult(exe_scope, byte_order, addr_byte_size,
37                                      address))
38       ->GetSP();
39 }
40 
41 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
42                                                ByteOrder byte_order,
43                                                uint32_t addr_byte_size,
44                                                lldb::addr_t address)
45     : ValueObject(exe_scope), m_type_name(), m_byte_size(0),
46       m_impl(this, address) {
47   SetIsConstant();
48   SetValueIsValid(true);
49   m_data.SetByteOrder(byte_order);
50   m_data.SetAddressByteSize(addr_byte_size);
51   SetAddressTypeOfChildren(eAddressTypeLoad);
52 }
53 
54 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
55                                              const CompilerType &compiler_type,
56                                              const ConstString &name,
57                                              const DataExtractor &data,
58                                              lldb::addr_t address) {
59   return (new ValueObjectConstResult(exe_scope, compiler_type, name, data,
60                                      address))
61       ->GetSP();
62 }
63 
64 ValueObjectConstResult::ValueObjectConstResult(
65     ExecutionContextScope *exe_scope, const CompilerType &compiler_type,
66     const ConstString &name, const DataExtractor &data, lldb::addr_t address)
67     : ValueObject(exe_scope), m_type_name(), m_byte_size(0),
68       m_impl(this, address) {
69   m_data = data;
70 
71   if (!m_data.GetSharedDataBuffer()) {
72     DataBufferSP shared_data_buffer(
73         new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
74     m_data.SetData(shared_data_buffer);
75   }
76 
77   m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
78   m_value.SetValueType(Value::eValueTypeHostAddress);
79   m_value.SetCompilerType(compiler_type);
80   m_name = name;
81   SetIsConstant();
82   SetValueIsValid(true);
83   SetAddressTypeOfChildren(eAddressTypeLoad);
84 }
85 
86 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
87                                              const CompilerType &compiler_type,
88                                              const ConstString &name,
89                                              const lldb::DataBufferSP &data_sp,
90                                              lldb::ByteOrder data_byte_order,
91                                              uint32_t data_addr_size,
92                                              lldb::addr_t address) {
93   return (new ValueObjectConstResult(exe_scope, compiler_type, name, data_sp,
94                                      data_byte_order, data_addr_size, address))
95       ->GetSP();
96 }
97 
98 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
99                                              Value &value,
100                                              const ConstString &name,
101                                              Module *module) {
102   return (new ValueObjectConstResult(exe_scope, value, name, module))->GetSP();
103 }
104 
105 ValueObjectConstResult::ValueObjectConstResult(
106     ExecutionContextScope *exe_scope, const CompilerType &compiler_type,
107     const ConstString &name, const lldb::DataBufferSP &data_sp,
108     lldb::ByteOrder data_byte_order, uint32_t data_addr_size,
109     lldb::addr_t address)
110     : ValueObject(exe_scope), m_type_name(), m_byte_size(0),
111       m_impl(this, address) {
112   m_data.SetByteOrder(data_byte_order);
113   m_data.SetAddressByteSize(data_addr_size);
114   m_data.SetData(data_sp);
115   m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
116   m_value.SetValueType(Value::eValueTypeHostAddress);
117   // m_value.SetContext(Value::eContextTypeClangType, compiler_type);
118   m_value.SetCompilerType(compiler_type);
119   m_name = name;
120   SetIsConstant();
121   SetValueIsValid(true);
122   SetAddressTypeOfChildren(eAddressTypeLoad);
123 }
124 
125 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
126                                              const CompilerType &compiler_type,
127                                              const ConstString &name,
128                                              lldb::addr_t address,
129                                              AddressType address_type,
130                                              uint32_t addr_byte_size) {
131   return (new ValueObjectConstResult(exe_scope, compiler_type, name, address,
132                                      address_type, addr_byte_size))
133       ->GetSP();
134 }
135 
136 ValueObjectConstResult::ValueObjectConstResult(
137     ExecutionContextScope *exe_scope, const CompilerType &compiler_type,
138     const ConstString &name, lldb::addr_t address, AddressType address_type,
139     uint32_t addr_byte_size)
140     : ValueObject(exe_scope), m_type_name(), m_byte_size(0),
141       m_impl(this, address) {
142   m_value.GetScalar() = address;
143   m_data.SetAddressByteSize(addr_byte_size);
144   m_value.GetScalar().GetData(m_data, addr_byte_size);
145   // m_value.SetValueType(Value::eValueTypeHostAddress);
146   switch (address_type) {
147   case eAddressTypeInvalid:
148     m_value.SetValueType(Value::eValueTypeScalar);
149     break;
150   case eAddressTypeFile:
151     m_value.SetValueType(Value::eValueTypeFileAddress);
152     break;
153   case eAddressTypeLoad:
154     m_value.SetValueType(Value::eValueTypeLoadAddress);
155     break;
156   case eAddressTypeHost:
157     m_value.SetValueType(Value::eValueTypeHostAddress);
158     break;
159   }
160   //    m_value.SetContext(Value::eContextTypeClangType, compiler_type);
161   m_value.SetCompilerType(compiler_type);
162   m_name = name;
163   SetIsConstant();
164   SetValueIsValid(true);
165   SetAddressTypeOfChildren(eAddressTypeLoad);
166 }
167 
168 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
169                                              const Error &error) {
170   return (new ValueObjectConstResult(exe_scope, error))->GetSP();
171 }
172 
173 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
174                                                const Error &error)
175     : ValueObject(exe_scope), m_type_name(), m_byte_size(0), m_impl(this) {
176   m_error = error;
177   SetIsConstant();
178 }
179 
180 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
181                                                const Value &value,
182                                                const ConstString &name,
183                                                Module *module)
184     : ValueObject(exe_scope), m_type_name(), m_byte_size(0), m_impl(this) {
185   m_value = value;
186   m_name = name;
187   ExecutionContext exe_ctx;
188   exe_scope->CalculateExecutionContext(exe_ctx);
189   m_error = m_value.GetValueAsData(&exe_ctx, m_data, 0, module);
190 }
191 
192 ValueObjectConstResult::~ValueObjectConstResult() {}
193 
194 CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
195   return m_value.GetCompilerType();
196 }
197 
198 lldb::ValueType ValueObjectConstResult::GetValueType() const {
199   return eValueTypeConstResult;
200 }
201 
202 uint64_t ValueObjectConstResult::GetByteSize() {
203   ExecutionContext exe_ctx(GetExecutionContextRef());
204 
205   if (m_byte_size == 0)
206     SetByteSize(
207         GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()));
208   return m_byte_size;
209 }
210 
211 void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
212 
213 size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
214   auto children_count = GetCompilerType().GetNumChildren(true);
215   return children_count <= max ? children_count : max;
216 }
217 
218 ConstString ValueObjectConstResult::GetTypeName() {
219   if (m_type_name.IsEmpty())
220     m_type_name = GetCompilerType().GetConstTypeName();
221   return m_type_name;
222 }
223 
224 ConstString ValueObjectConstResult::GetDisplayTypeName() {
225   return GetCompilerType().GetDisplayTypeName();
226 }
227 
228 bool ValueObjectConstResult::UpdateValue() {
229   // Const value is always valid
230   SetValueIsValid(true);
231   return true;
232 }
233 
234 bool ValueObjectConstResult::IsInScope() {
235   // A const result value is always in scope since it serializes all
236   // information needed to contain the constant value.
237   return true;
238 }
239 
240 lldb::ValueObjectSP ValueObjectConstResult::Dereference(Error &error) {
241   return m_impl.Dereference(error);
242 }
243 
244 lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
245     uint32_t offset, const CompilerType &type, bool can_create,
246     ConstString name_const_str) {
247   return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
248                                           name_const_str);
249 }
250 
251 lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Error &error) {
252   return m_impl.AddressOf(error);
253 }
254 
255 lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
256                                                   AddressType *address_type) {
257   return m_impl.GetAddressOf(scalar_is_load_address, address_type);
258 }
259 
260 ValueObject *ValueObjectConstResult::CreateChildAtIndex(
261     size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
262   return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
263                                    synthetic_index);
264 }
265 
266 size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
267                                               uint32_t item_idx,
268                                               uint32_t item_count) {
269   return m_impl.GetPointeeData(data, item_idx, item_count);
270 }
271 
272 lldb::ValueObjectSP
273 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
274   // Always recalculate dynamic values for const results as the memory that
275   // they might point to might have changed at any time.
276   if (use_dynamic != eNoDynamicValues) {
277     if (!IsDynamic()) {
278       ExecutionContext exe_ctx(GetExecutionContextRef());
279       Process *process = exe_ctx.GetProcessPtr();
280       if (process && process->IsPossibleDynamicValue(*this))
281         m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
282     }
283     if (m_dynamic_value)
284       return m_dynamic_value->GetSP();
285   }
286   return ValueObjectSP();
287 }
288 
289 lldb::ValueObjectSP
290 ValueObjectConstResult::Cast(const CompilerType &compiler_type) {
291   return m_impl.Cast(compiler_type);
292 }
293 
294 lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
295   if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
296     return m_preferred_display_language;
297   return GetCompilerTypeImpl().GetMinimumLanguage();
298 }
299