1 //===-- ValueObjectDynamicValue.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 
11 #include "lldb/Core/ValueObjectDynamicValue.h"
12 
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project includes
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/ValueObjectList.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Core/ValueObject.h"
22 
23 #include "lldb/Symbol/ClangASTType.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Symbol/Type.h"
27 #include "lldb/Symbol/Variable.h"
28 
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/LanguageRuntime.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/RegisterContext.h"
33 #include "lldb/Target/Target.h"
34 #include "lldb/Target/Thread.h"
35 
36 using namespace lldb_private;
37 
38 ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) :
39     ValueObject(parent),
40     m_address (),
41     m_type_sp(),
42     m_use_dynamic (use_dynamic)
43 {
44     m_last_format_mgr_dynamic = use_dynamic;
45     SetName (parent.GetName());
46 }
47 
48 ValueObjectDynamicValue::~ValueObjectDynamicValue()
49 {
50     m_owning_valobj_sp.reset();
51 }
52 
53 lldb::clang_type_t
54 ValueObjectDynamicValue::GetClangTypeImpl ()
55 {
56     if (m_type_sp)
57         return m_value.GetClangType();
58     else
59         return m_parent->GetClangType();
60 }
61 
62 ConstString
63 ValueObjectDynamicValue::GetTypeName()
64 {
65     const bool success = UpdateValueIfNeeded(false);
66     if (success && m_type_sp)
67         return ClangASTType::GetConstTypeName (GetClangAST(), GetClangType());
68     else
69         return m_parent->GetTypeName();
70 }
71 
72 uint32_t
73 ValueObjectDynamicValue::CalculateNumChildren()
74 {
75     const bool success = UpdateValueIfNeeded(false);
76     if (success && m_type_sp)
77         return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true);
78     else
79         return m_parent->GetNumChildren();
80 }
81 
82 clang::ASTContext *
83 ValueObjectDynamicValue::GetClangASTImpl ()
84 {
85     const bool success = UpdateValueIfNeeded(false);
86     if (success && m_type_sp)
87         return m_type_sp->GetClangAST();
88     else
89         return m_parent->GetClangAST ();
90 }
91 
92 size_t
93 ValueObjectDynamicValue::GetByteSize()
94 {
95     const bool success = UpdateValueIfNeeded(false);
96     if (success && m_type_sp)
97         return m_value.GetValueByteSize(GetClangAST(), NULL);
98     else
99         return m_parent->GetByteSize();
100 }
101 
102 lldb::ValueType
103 ValueObjectDynamicValue::GetValueType() const
104 {
105     return m_parent->GetValueType();
106 }
107 
108 bool
109 ValueObjectDynamicValue::UpdateValue ()
110 {
111     SetValueIsValid (false);
112     m_error.Clear();
113 
114     if (!m_parent->UpdateValueIfNeeded(false))
115     {
116         // The dynamic value failed to get an error, pass the error along
117         if (m_error.Success() && m_parent->GetError().Fail())
118             m_error = m_parent->GetError();
119         return false;
120     }
121 
122     // Setting our type_sp to NULL will route everything back through our
123     // parent which is equivalent to not using dynamic values.
124     if (m_use_dynamic == lldb::eNoDynamicValues)
125     {
126         m_type_sp.reset();
127         return true;
128     }
129 
130     ExecutionContext exe_ctx (GetExecutionContextRef());
131     Target *target = exe_ctx.GetTargetPtr();
132     if (target)
133     {
134         m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
135         m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
136     }
137 
138     // First make sure our Type and/or Address haven't changed:
139     Process *process = exe_ctx.GetProcessPtr();
140     if (!process)
141         return false;
142 
143     TypeAndOrName class_type_or_name;
144     Address dynamic_address;
145     bool found_dynamic_type = false;
146 
147     lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
148     if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
149     {
150         LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
151         if (runtime)
152             found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
153     }
154     else
155     {
156         LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
157         if (cpp_runtime)
158             found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
159 
160         if (!found_dynamic_type)
161         {
162             LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
163             if (objc_runtime)
164                 found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
165         }
166     }
167 
168     lldb::TypeSP dynamic_type_sp = class_type_or_name.GetTypeSP();
169 
170     // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
171     // don't...
172 
173     m_update_point.SetUpdated();
174 
175     // If we don't have a dynamic type, then make ourselves just a echo of our parent.
176     // Or we could return false, and make ourselves an echo of our parent?
177     if (!found_dynamic_type)
178     {
179         if (m_type_sp)
180             SetValueDidChange(true);
181         ClearDynamicTypeInformation();
182         m_type_sp.reset();
183         m_value = m_parent->GetValue();
184         m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
185         return m_error.Success();
186     }
187 
188     Value old_value(m_value);
189 
190     lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
191 
192     bool has_changed_type = false;
193 
194     if (!m_type_sp)
195     {
196         m_type_sp = dynamic_type_sp;
197         has_changed_type = true;
198     }
199     else if (dynamic_type_sp != m_type_sp)
200     {
201         // We are another type, we need to tear down our children...
202         m_type_sp = dynamic_type_sp;
203         SetValueDidChange (true);
204         has_changed_type = true;
205     }
206 
207     if (has_changed_type)
208         ClearDynamicTypeInformation ();
209 
210     if (!m_address.IsValid() || m_address != dynamic_address)
211     {
212         if (m_address.IsValid())
213             SetValueDidChange (true);
214 
215         // We've moved, so we should be fine...
216         m_address = dynamic_address;
217         lldb::TargetSP target_sp (GetTargetSP());
218         lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
219         m_value.GetScalar() = load_address;
220     }
221 
222     // The type will always be the type of the dynamic object.  If our parent's type was a pointer,
223     // then our type should be a pointer to the type of the dynamic object.  If a reference, then the original type
224     // should be okay...
225     lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType();
226     lldb::clang_type_t corrected_type = orig_type;
227     if (m_parent->IsPointerType())
228         corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type);
229     else if (m_parent->IsPointerOrReferenceType())
230         corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type);
231 
232     m_value.SetContext (Value::eContextTypeClangType, corrected_type);
233 
234     // Our address is the location of the dynamic type stored in memory.  It isn't a load address,
235     // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
236     m_value.SetValueType(Value::eValueTypeScalar);
237 
238     if (has_changed_type && log)
239         log->Printf("[%s %p] has a new dynamic type %s",
240                     GetName().GetCString(),
241                     this,
242                     GetTypeName().GetCString());
243 
244     if (m_address.IsValid() && m_type_sp)
245     {
246         // The variable value is in the Scalar value inside the m_value.
247         // We can point our m_data right to it.
248         m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
249         if (m_error.Success())
250         {
251             if (ClangASTContext::IsAggregateType (GetClangType()))
252             {
253                 // this value object represents an aggregate type whose
254                 // children have values, but this object does not. So we
255                 // say we are changed if our location has changed.
256                 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
257             }
258 
259             SetValueIsValid (true);
260             return true;
261         }
262     }
263 
264     // We get here if we've failed above...
265     SetValueIsValid (false);
266     return false;
267 }
268 
269 
270 
271 bool
272 ValueObjectDynamicValue::IsInScope ()
273 {
274     return m_parent->IsInScope();
275 }
276 
277 bool
278 ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
279 {
280     if (!UpdateValueIfNeeded(false))
281     {
282         error.SetErrorString("unable to read value");
283         return false;
284     }
285 
286     uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
287     uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
288 
289     if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
290     {
291         error.SetErrorString("unable to read value");
292         return false;
293     }
294 
295     // if we are at an offset from our parent, in order to set ourselves correctly we would need
296     // to change the new value so that it refers to the correct dynamic type. we choose not to deal
297     // with that - if anything more than a value overwrite is required, you should be using the
298     // expression parser instead of the value editing facility
299     if (my_value != parent_value)
300     {
301         // but NULL'ing out a value should always be allowed
302         if (strcmp(value_str,"0"))
303         {
304             error.SetErrorString("unable to modify dynamic value, use 'expression' command");
305             return false;
306         }
307     }
308 
309     bool ret_val = m_parent->SetValueFromCString(value_str,error);
310     SetNeedsUpdate();
311     return ret_val;
312 }
313