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