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 CompilerType
58 ValueObjectVariable::GetCompilerTypeImpl ()
59 {
60     Type *var_type = m_variable_sp->GetType();
61     if (var_type)
62         return var_type->GetForwardCompilerType ();
63     return CompilerType();
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::GetDisplayTypeName()
77 {
78     Type * var_type = m_variable_sp->GetType();
79     if (var_type)
80         return var_type->GetForwardCompilerType ().GetDisplayTypeName();
81     return ConstString();
82 }
83 
84 ConstString
85 ValueObjectVariable::GetQualifiedTypeName()
86 {
87     Type * var_type = m_variable_sp->GetType();
88     if (var_type)
89         return var_type->GetQualifiedName();
90     return ConstString();
91 }
92 
93 size_t
94 ValueObjectVariable::CalculateNumChildren(uint32_t max)
95 {
96     CompilerType type(GetCompilerType());
97 
98     if (!type.IsValid())
99         return 0;
100 
101     const bool omit_empty_base_classes = true;
102     auto child_count = type.GetNumChildren(omit_empty_base_classes);
103     return child_count <= max ? child_count : max;
104 }
105 
106 uint64_t
107 ValueObjectVariable::GetByteSize()
108 {
109     ExecutionContext exe_ctx(GetExecutionContextRef());
110 
111     CompilerType type(GetCompilerType());
112 
113     if (!type.IsValid())
114         return 0;
115 
116     return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
117 }
118 
119 lldb::ValueType
120 ValueObjectVariable::GetValueType() const
121 {
122     if (m_variable_sp)
123         return m_variable_sp->GetScope();
124     return lldb::eValueTypeInvalid;
125 }
126 
127 bool
128 ValueObjectVariable::UpdateValue ()
129 {
130     SetValueIsValid (false);
131     m_error.Clear();
132 
133     Variable *variable = m_variable_sp.get();
134     DWARFExpression &expr = variable->LocationExpression();
135 
136     if (variable->GetLocationIsConstantValueData())
137     {
138         // expr doesn't contain DWARF bytes, it contains the constant variable
139         // value bytes themselves...
140         if (expr.GetExpressionData(m_data))
141             m_value.SetContext(Value::eContextTypeVariable, variable);
142         else
143             m_error.SetErrorString ("empty constant data");
144         // constant bytes can't be edited - sorry
145         m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
146     }
147     else
148     {
149         lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
150         ExecutionContext exe_ctx (GetExecutionContextRef());
151 
152         Target *target = exe_ctx.GetTargetPtr();
153         if (target)
154         {
155             m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
156             m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
157         }
158 
159         if (expr.IsLocationList())
160         {
161             SymbolContext sc;
162             variable->CalculateSymbolContext (&sc);
163             if (sc.function)
164                 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
165         }
166         Value old_value(m_value);
167         if (expr.Evaluate (&exe_ctx,
168                            nullptr,
169                            nullptr,
170                            nullptr,
171                            loclist_base_load_addr,
172                            nullptr,
173                            nullptr,
174                            m_value,
175                            &m_error))
176         {
177             m_resolved_value = m_value;
178             m_value.SetContext(Value::eContextTypeVariable, variable);
179 
180             CompilerType compiler_type = GetCompilerType();
181             if (compiler_type.IsValid())
182                 m_value.SetCompilerType(compiler_type);
183 
184             Value::ValueType value_type = m_value.GetValueType();
185 
186             Process *process = exe_ctx.GetProcessPtr();
187             const bool process_is_alive = process && process->IsAlive();
188             const uint32_t type_info = compiler_type.GetTypeInfo();
189             const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
190 
191             switch (value_type)
192             {
193                 case Value::eValueTypeFileAddress:
194                     // If this type is a pointer, then its children will be considered load addresses
195                     // if the pointer or reference is dereferenced, but only if the process is alive.
196                     //
197                     // There could be global variables like in the following code:
198                     // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
199                     // Foo g_foo1;
200                     // Foo g_foo2;
201                     // LinkedListNode g_second_node = { &g_foo2, NULL };
202                     // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
203                     //
204                     // When we aren't running, we should be able to look at these variables using
205                     // the "target variable" command. Children of the "g_first_node" always will
206                     // be of the same address type as the parent. But children of the "next" member of
207                     // LinkedListNode will become load addresses if we have a live process, or remain
208                     // what a file address if it what a file address.
209                     if (process_is_alive && is_pointer_or_ref)
210                         SetAddressTypeOfChildren(eAddressTypeLoad);
211                     else
212                         SetAddressTypeOfChildren(eAddressTypeFile);
213                     break;
214                 case Value::eValueTypeHostAddress:
215                     // Same as above for load addresses, except children of pointer or refs are always
216                     // load addresses. Host addresses are used to store freeze dried variables. If this
217                     // type is a struct, the entire struct contents will be copied into the heap of the
218                     // LLDB process, but we do not currrently follow any pointers.
219                     if (is_pointer_or_ref)
220                         SetAddressTypeOfChildren(eAddressTypeLoad);
221                     else
222                         SetAddressTypeOfChildren(eAddressTypeHost);
223                     break;
224                 case Value::eValueTypeLoadAddress:
225                 case Value::eValueTypeScalar:
226                 case Value::eValueTypeVector:
227                     SetAddressTypeOfChildren(eAddressTypeLoad);
228                     break;
229             }
230 
231             switch (value_type)
232             {
233             case Value::eValueTypeVector:
234                     // fall through
235             case Value::eValueTypeScalar:
236                 // The variable value is in the Scalar value inside the m_value.
237                 // We can point our m_data right to it.
238                 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
239                 break;
240 
241             case Value::eValueTypeFileAddress:
242             case Value::eValueTypeLoadAddress:
243             case Value::eValueTypeHostAddress:
244                 // The DWARF expression result was an address in the inferior
245                 // process. If this variable is an aggregate type, we just need
246                 // the address as the main value as all child variable objects
247                 // will rely upon this location and add an offset and then read
248                 // their own values as needed. If this variable is a simple
249                 // type, we read all data for it into m_data.
250                 // Make sure this type has a value before we try and read it
251 
252                 // If we have a file address, convert it to a load address if we can.
253                 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
254                 {
255                     lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
256                     if (file_addr != LLDB_INVALID_ADDRESS)
257                     {
258                         SymbolContext var_sc;
259                         variable->CalculateSymbolContext(&var_sc);
260                         if (var_sc.module_sp)
261                         {
262                             ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
263                             if (objfile)
264                             {
265                                 Address so_addr(file_addr, objfile->GetSectionList());
266                                 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
267                                 if (load_addr != LLDB_INVALID_ADDRESS)
268                                 {
269                                     m_value.SetValueType(Value::eValueTypeLoadAddress);
270                                     m_value.GetScalar() = load_addr;
271                                 }
272                             }
273                         }
274                     }
275                 }
276 
277                 if (!CanProvideValue())
278                 {
279                     // this value object represents an aggregate type whose
280                     // children have values, but this object does not. So we
281                     // say we are changed if our location has changed.
282                     SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
283                 }
284                 else
285                 {
286                     // Copy the Value and set the context to use our Variable
287                     // so it can extract read its value into m_data appropriately
288                     Value value(m_value);
289                     value.SetContext(Value::eContextTypeVariable, variable);
290                     m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
291 
292                     SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
293                 }
294                 break;
295             }
296 
297             SetValueIsValid (m_error.Success());
298         }
299         else
300         {
301             // could not find location, won't allow editing
302             m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
303         }
304     }
305     return m_error.Success();
306 }
307 
308 
309 
310 bool
311 ValueObjectVariable::IsInScope ()
312 {
313     const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
314     if (exe_ctx_ref.HasFrameRef())
315     {
316         ExecutionContext exe_ctx (exe_ctx_ref);
317         StackFrame *frame = exe_ctx.GetFramePtr();
318         if (frame)
319         {
320             return m_variable_sp->IsInScope (frame);
321         }
322         else
323         {
324             // This ValueObject had a frame at one time, but now we
325             // can't locate it, so return false since we probably aren't
326             // in scope.
327             return false;
328         }
329     }
330     // We have a variable that wasn't tied to a frame, which
331     // means it is a global and is always in scope.
332     return true;
333 
334 }
335 
336 lldb::ModuleSP
337 ValueObjectVariable::GetModule()
338 {
339     if (m_variable_sp)
340     {
341         SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
342         if (sc_scope)
343         {
344             return sc_scope->CalculateSymbolContextModule();
345         }
346     }
347     return lldb::ModuleSP();
348 }
349 
350 SymbolContextScope *
351 ValueObjectVariable::GetSymbolContextScope()
352 {
353     if (m_variable_sp)
354         return m_variable_sp->GetSymbolContextScope();
355     return NULL;
356 }
357 
358 bool
359 ValueObjectVariable::GetDeclaration (Declaration &decl)
360 {
361     if (m_variable_sp)
362     {
363         decl = m_variable_sp->GetDeclaration();
364         return true;
365     }
366     return false;
367 }
368 
369 const char *
370 ValueObjectVariable::GetLocationAsCString ()
371 {
372     if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
373         return GetLocationAsCStringImpl(m_resolved_value,
374                                         m_data);
375     else
376         return ValueObject::GetLocationAsCString();
377 }
378 
379 bool
380 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
381 {
382     if (!UpdateValueIfNeeded())
383     {
384         error.SetErrorString("unable to update value before writing");
385         return false;
386     }
387 
388     if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
389     {
390         RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
391         ExecutionContext exe_ctx(GetExecutionContextRef());
392         RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
393         RegisterValue reg_value;
394         if (!reg_info || !reg_ctx)
395         {
396             error.SetErrorString("unable to retrieve register info");
397             return false;
398         }
399         error = reg_value.SetValueFromCString(reg_info, value_str);
400         if (error.Fail())
401             return false;
402         if (reg_ctx->WriteRegister (reg_info, reg_value))
403         {
404             SetNeedsUpdate();
405             return true;
406         }
407         else
408         {
409             error.SetErrorString("unable to write back to register");
410             return false;
411         }
412     }
413     else
414         return ValueObject::SetValueFromCString(value_str, error);
415 }
416 
417 bool
418 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
419 {
420     if (!UpdateValueIfNeeded())
421     {
422         error.SetErrorString("unable to update value before writing");
423         return false;
424     }
425 
426     if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
427     {
428         RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
429         ExecutionContext exe_ctx(GetExecutionContextRef());
430         RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
431         RegisterValue reg_value;
432         if (!reg_info || !reg_ctx)
433         {
434             error.SetErrorString("unable to retrieve register info");
435             return false;
436         }
437         error = reg_value.SetValueFromData(reg_info, data, 0, true);
438         if (error.Fail())
439             return false;
440         if (reg_ctx->WriteRegister (reg_info, reg_value))
441         {
442             SetNeedsUpdate();
443             return true;
444         }
445         else
446         {
447             error.SetErrorString("unable to write back to register");
448             return false;
449         }
450     }
451     else
452         return ValueObject::SetData(data, error);
453 }
454