1 //===-- ValueObjectVariable.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/ValueObjectVariable.h"
12 
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project includes
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/RegisterValue.h"
19 #include "lldb/Core/ValueObjectList.h"
20 #include "lldb/Core/Value.h"
21 
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/SymbolContextScope.h"
26 #include "lldb/Symbol/Type.h"
27 #include "lldb/Symbol/Variable.h"
28 
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/RegisterContext.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
34 
35 
36 using namespace lldb_private;
37 
38 lldb::ValueObjectSP
39 ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
40 {
41     return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
42 }
43 
44 ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
45     ValueObject(exe_scope),
46     m_variable_sp(var_sp)
47 {
48     // Do not attempt to construct one of these objects with no variable!
49     assert (m_variable_sp.get() != NULL);
50     m_name = var_sp->GetName();
51 }
52 
53 ValueObjectVariable::~ValueObjectVariable()
54 {
55 }
56 
57 lldb::clang_type_t
58 ValueObjectVariable::GetClangTypeImpl ()
59 {
60     Type *var_type = m_variable_sp->GetType();
61     if (var_type)
62         return var_type->GetClangForwardType();
63     return NULL;
64 }
65 
66 ConstString
67 ValueObjectVariable::GetTypeName()
68 {
69     Type * var_type = m_variable_sp->GetType();
70     if (var_type)
71         return var_type->GetName();
72     return ConstString();
73 }
74 
75 ConstString
76 ValueObjectVariable::GetQualifiedTypeName()
77 {
78     Type * var_type = m_variable_sp->GetType();
79     if (var_type)
80         return var_type->GetQualifiedName();
81     return ConstString();
82 }
83 
84 size_t
85 ValueObjectVariable::CalculateNumChildren()
86 {
87     ClangASTType type(GetClangAST(),
88                       GetClangType());
89 
90     if (!type.IsValid())
91         return 0;
92 
93     const bool omit_empty_base_classes = true;
94     return ClangASTContext::GetNumChildren(type.GetASTContext(), type.GetOpaqueQualType(), omit_empty_base_classes);
95 }
96 
97 clang::ASTContext *
98 ValueObjectVariable::GetClangASTImpl ()
99 {
100     Type *var_type = m_variable_sp->GetType();
101     if (var_type)
102         return var_type->GetClangAST();
103     return 0;
104 }
105 
106 uint64_t
107 ValueObjectVariable::GetByteSize()
108 {
109     ClangASTType type(GetClangAST(), GetClangType());
110 
111     if (!type.IsValid())
112         return 0;
113 
114     return type.GetClangTypeByteSize();
115 }
116 
117 lldb::ValueType
118 ValueObjectVariable::GetValueType() const
119 {
120     if (m_variable_sp)
121         return m_variable_sp->GetScope();
122     return lldb::eValueTypeInvalid;
123 }
124 
125 bool
126 ValueObjectVariable::UpdateValue ()
127 {
128     SetValueIsValid (false);
129     m_error.Clear();
130 
131     Variable *variable = m_variable_sp.get();
132     DWARFExpression &expr = variable->LocationExpression();
133 
134     if (variable->GetLocationIsConstantValueData())
135     {
136         // expr doesn't contain DWARF bytes, it contains the constant variable
137         // value bytes themselves...
138         if (expr.GetExpressionData(m_data))
139             m_value.SetContext(Value::eContextTypeVariable, variable);
140         else
141             m_error.SetErrorString ("empty constant data");
142         // constant bytes can't be edited - sorry
143         m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
144     }
145     else
146     {
147         lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
148         ExecutionContext exe_ctx (GetExecutionContextRef());
149 
150         Target *target = exe_ctx.GetTargetPtr();
151         if (target)
152         {
153             m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
154             m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
155         }
156 
157         if (expr.IsLocationList())
158         {
159             SymbolContext sc;
160             variable->CalculateSymbolContext (&sc);
161             if (sc.function)
162                 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
163         }
164         Value old_value(m_value);
165         if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
166         {
167             m_resolved_value = m_value;
168             m_value.SetContext(Value::eContextTypeVariable, variable);
169 
170             Value::ValueType value_type = m_value.GetValueType();
171 
172             switch (value_type)
173             {
174                 case Value::eValueTypeFileAddress:
175                     SetAddressTypeOfChildren(eAddressTypeFile);
176                     break;
177                 case Value::eValueTypeHostAddress:
178                     SetAddressTypeOfChildren(eAddressTypeHost);
179                     break;
180                 case Value::eValueTypeLoadAddress:
181                 case Value::eValueTypeScalar:
182                 case Value::eValueTypeVector:
183                     SetAddressTypeOfChildren(eAddressTypeLoad);
184                     break;
185             }
186 
187             switch (value_type)
188             {
189             default:
190                 assert(!"Unhandled expression result value kind...");
191                 break;
192 
193             case Value::eValueTypeScalar:
194                 // The variable value is in the Scalar value inside the m_value.
195                 // We can point our m_data right to it.
196                 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
197                 break;
198 
199             case Value::eValueTypeFileAddress:
200             case Value::eValueTypeLoadAddress:
201             case Value::eValueTypeHostAddress:
202                 // The DWARF expression result was an address in the inferior
203                 // process. If this variable is an aggregate type, we just need
204                 // the address as the main value as all child variable objects
205                 // will rely upon this location and add an offset and then read
206                 // their own values as needed. If this variable is a simple
207                 // type, we read all data for it into m_data.
208                 // Make sure this type has a value before we try and read it
209 
210                 // If we have a file address, convert it to a load address if we can.
211                 Process *process = exe_ctx.GetProcessPtr();
212                 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
213                 {
214                     lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
215                     if (file_addr != LLDB_INVALID_ADDRESS)
216                     {
217                         SymbolContext var_sc;
218                         variable->CalculateSymbolContext(&var_sc);
219                         if (var_sc.module_sp)
220                         {
221                             ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
222                             if (objfile)
223                             {
224                                 Address so_addr(file_addr, objfile->GetSectionList());
225                                 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
226                                 if (load_addr != LLDB_INVALID_ADDRESS)
227                                 {
228                                     m_value.SetValueType(Value::eValueTypeLoadAddress);
229                                     m_value.GetScalar() = load_addr;
230                                 }
231                             }
232                         }
233                     }
234                 }
235 
236                 if (ClangASTContext::IsAggregateType (GetClangType()))
237                 {
238                     // this value object represents an aggregate type whose
239                     // children have values, but this object does not. So we
240                     // say we are changed if our location has changed.
241                     SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
242                 }
243                 else
244                 {
245                     // Copy the Value and set the context to use our Variable
246                     // so it can extract read its value into m_data appropriately
247                     Value value(m_value);
248                     value.SetContext(Value::eContextTypeVariable, variable);
249                     m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
250                 }
251                 break;
252             }
253 
254             SetValueIsValid (m_error.Success());
255         }
256         else
257         {
258             // could not find location, won't allow editing
259             m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
260         }
261     }
262     return m_error.Success();
263 }
264 
265 
266 
267 bool
268 ValueObjectVariable::IsInScope ()
269 {
270     const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
271     if (exe_ctx_ref.HasFrameRef())
272     {
273         ExecutionContext exe_ctx (exe_ctx_ref);
274         StackFrame *frame = exe_ctx.GetFramePtr();
275         if (frame)
276         {
277             return m_variable_sp->IsInScope (frame);
278         }
279         else
280         {
281             // This ValueObject had a frame at one time, but now we
282             // can't locate it, so return false since we probably aren't
283             // in scope.
284             return false;
285         }
286     }
287     // We have a variable that wasn't tied to a frame, which
288     // means it is a global and is always in scope.
289     return true;
290 
291 }
292 
293 lldb::ModuleSP
294 ValueObjectVariable::GetModule()
295 {
296     if (m_variable_sp)
297     {
298         SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
299         if (sc_scope)
300         {
301             return sc_scope->CalculateSymbolContextModule();
302         }
303     }
304     return lldb::ModuleSP();
305 }
306 
307 SymbolContextScope *
308 ValueObjectVariable::GetSymbolContextScope()
309 {
310     if (m_variable_sp)
311         return m_variable_sp->GetSymbolContextScope();
312     return NULL;
313 }
314 
315 bool
316 ValueObjectVariable::GetDeclaration (Declaration &decl)
317 {
318     if (m_variable_sp)
319     {
320         decl = m_variable_sp->GetDeclaration();
321         return true;
322     }
323     return false;
324 }
325 
326 const char *
327 ValueObjectVariable::GetLocationAsCString ()
328 {
329     return GetLocationAsCStringImpl(m_resolved_value,
330                                     m_data);
331 }
332 
333 bool
334 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
335 {
336     if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
337     {
338         RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
339         ExecutionContext exe_ctx(GetExecutionContextRef());
340         RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
341         RegisterValue reg_value;
342         if (!reg_info || !reg_ctx)
343         {
344             error.SetErrorString("unable to retrieve register info");
345             return false;
346         }
347         error = reg_value.SetValueFromCString(reg_info, value_str);
348         if (error.Fail())
349             return false;
350         if (reg_ctx->WriteRegister (reg_info, reg_value))
351         {
352             SetNeedsUpdate();
353             return true;
354         }
355         else
356         {
357             error.SetErrorString("unable to write back to register");
358             return false;
359         }
360     }
361     else
362         return ValueObject::SetValueFromCString(value_str, error);
363 }
364 
365 bool
366 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
367 {
368     if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
369     {
370         RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
371         ExecutionContext exe_ctx(GetExecutionContextRef());
372         RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
373         RegisterValue reg_value;
374         if (!reg_info || !reg_ctx)
375         {
376             error.SetErrorString("unable to retrieve register info");
377             return false;
378         }
379         error = reg_value.SetValueFromData(reg_info, data, 0, false);
380         if (error.Fail())
381             return false;
382         if (reg_ctx->WriteRegister (reg_info, reg_value))
383         {
384             SetNeedsUpdate();
385             return true;
386         }
387         else
388         {
389             error.SetErrorString("unable to write back to register");
390             return false;
391         }
392     }
393     else
394         return ValueObject::SetData(data, error);
395 }
396