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         m_value = m_parent->GetValue();
182         m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
183         return m_error.Success();
184     }
185 
186     Value old_value(m_value);
187 
188     lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
189 
190     bool has_changed_type = false;
191 
192     if (!m_type_sp)
193     {
194         m_type_sp = dynamic_type_sp;
195         has_changed_type = true;
196     }
197     else if (dynamic_type_sp != m_type_sp)
198     {
199         // We are another type, we need to tear down our children...
200         m_type_sp = dynamic_type_sp;
201         SetValueDidChange (true);
202         has_changed_type = true;
203     }
204 
205     if (has_changed_type)
206         ClearDynamicTypeInformation ();
207 
208     if (!m_address.IsValid() || m_address != dynamic_address)
209     {
210         if (m_address.IsValid())
211             SetValueDidChange (true);
212 
213         // We've moved, so we should be fine...
214         m_address = dynamic_address;
215         lldb::TargetSP target_sp (GetTargetSP());
216         lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
217         m_value.GetScalar() = load_address;
218     }
219 
220     // The type will always be the type of the dynamic object.  If our parent's type was a pointer,
221     // then our type should be a pointer to the type of the dynamic object.  If a reference, then the original type
222     // should be okay...
223     lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType();
224     lldb::clang_type_t corrected_type = orig_type;
225     if (m_parent->IsPointerType())
226         corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type);
227     else if (m_parent->IsPointerOrReferenceType())
228         corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type);
229 
230     m_value.SetContext (Value::eContextTypeClangType, corrected_type);
231 
232     // Our address is the location of the dynamic type stored in memory.  It isn't a load address,
233     // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
234     m_value.SetValueType(Value::eValueTypeScalar);
235 
236     if (has_changed_type && log)
237         log->Printf("[%s %p] has a new dynamic type %s",
238                     GetName().GetCString(),
239                     this,
240                     GetTypeName().GetCString());
241 
242     if (m_address.IsValid() && m_type_sp)
243     {
244         // The variable value is in the Scalar value inside the m_value.
245         // We can point our m_data right to it.
246         m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
247         if (m_error.Success())
248         {
249             if (ClangASTContext::IsAggregateType (GetClangType()))
250             {
251                 // this value object represents an aggregate type whose
252                 // children have values, but this object does not. So we
253                 // say we are changed if our location has changed.
254                 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
255             }
256 
257             SetValueIsValid (true);
258             return true;
259         }
260     }
261 
262     // We get here if we've failed above...
263     SetValueIsValid (false);
264     return false;
265 }
266 
267 
268 
269 bool
270 ValueObjectDynamicValue::IsInScope ()
271 {
272     return m_parent->IsInScope();
273 }
274 
275 bool
276 ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
277 {
278     if (!UpdateValueIfNeeded(false))
279     {
280         error.SetErrorString("unable to read value");
281         return false;
282     }
283 
284     uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
285     uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
286 
287     if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
288     {
289         error.SetErrorString("unable to read value");
290         return false;
291     }
292 
293     // if we are at an offset from our parent, in order to set ourselves correctly we would need
294     // to change the new value so that it refers to the correct dynamic type. we choose not to deal
295     // with that - if anything more than a value overwrite is required, you should be using the
296     // expression parser instead of the value editing facility
297     if (my_value != parent_value)
298     {
299         // but NULL'ing out a value should always be allowed
300         if (strcmp(value_str,"0"))
301         {
302             error.SetErrorString("unable to modify dynamic value, use 'expression' command");
303             return false;
304         }
305     }
306 
307     bool ret_val = m_parent->SetValueFromCString(value_str,error);
308     SetNeedsUpdate();
309     return ret_val;
310 }
311