ValueObjectVariable.cpp revision 917c000e77fcf657099f59085d6436d179a39ea4
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/ValueObjectList.h"
19#include "lldb/Core/Value.h"
20
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/SymbolContext.h"
23#include "lldb/Symbol/Type.h"
24#include "lldb/Symbol/Variable.h"
25
26#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
29#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31
32
33using namespace lldb_private;
34
35lldb::ValueObjectSP
36ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
37{
38    return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
39}
40
41ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
42    ValueObject(exe_scope),
43    m_variable_sp(var_sp)
44{
45    // Do not attempt to construct one of these objects with no variable!
46    assert (m_variable_sp.get() != NULL);
47    m_name = var_sp->GetName();
48}
49
50ValueObjectVariable::~ValueObjectVariable()
51{
52}
53
54lldb::clang_type_t
55ValueObjectVariable::GetClangType ()
56{
57    Type *var_type = m_variable_sp->GetType();
58    if (var_type)
59        return var_type->GetClangForwardType();
60    return NULL;
61}
62
63ConstString
64ValueObjectVariable::GetTypeName()
65{
66    Type * var_type = m_variable_sp->GetType();
67    if (var_type)
68        return var_type->GetName();
69    ConstString empty_type_name;
70    return empty_type_name;
71}
72
73uint32_t
74ValueObjectVariable::CalculateNumChildren()
75{
76    Type *var_type = m_variable_sp->GetType();
77    if (var_type)
78        return var_type->GetNumChildren(true);
79    return 0;
80}
81
82clang::ASTContext *
83ValueObjectVariable::GetClangAST ()
84{
85    return m_variable_sp->GetType()->GetClangAST();
86}
87
88size_t
89ValueObjectVariable::GetByteSize()
90{
91    Type *type = m_variable_sp->GetType();
92    if (type)
93        return type->GetByteSize();
94    return 0;
95}
96
97lldb::ValueType
98ValueObjectVariable::GetValueType() const
99{
100    if (m_variable_sp)
101        return m_variable_sp->GetScope();
102    return lldb::eValueTypeInvalid;
103}
104
105bool
106ValueObjectVariable::UpdateValue ()
107{
108    SetValueIsValid (false);
109    m_error.Clear();
110
111    Variable *variable = m_variable_sp.get();
112    DWARFExpression &expr = variable->LocationExpression();
113
114    if (variable->GetLocationIsConstantValueData())
115    {
116        // expr doesn't contain DWARF bytes, it contains the constant variable
117        // value bytes themselves...
118        if (expr.GetExpressionData(m_data))
119            m_value.SetContext(Value::eContextTypeVariable, variable);
120        else
121            m_error.SetErrorString ("empty constant data");
122    }
123    else
124    {
125        lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
126        ExecutionContext exe_ctx (GetExecutionContextScope());
127
128        if (exe_ctx.target)
129        {
130            m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder());
131            m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize());
132        }
133
134        if (expr.IsLocationList())
135        {
136            SymbolContext sc;
137            variable->CalculateSymbolContext (&sc);
138            if (sc.function)
139                loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.target);
140        }
141        Value old_value(m_value);
142        if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
143        {
144            m_value.SetContext(Value::eContextTypeVariable, variable);
145
146            Value::ValueType value_type = m_value.GetValueType();
147
148            switch (value_type)
149            {
150            default:
151                assert(!"Unhandled expression result value kind...");
152                break;
153
154            case Value::eValueTypeScalar:
155                // The variable value is in the Scalar value inside the m_value.
156                // We can point our m_data right to it.
157                m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0);
158                break;
159
160            case Value::eValueTypeFileAddress:
161            case Value::eValueTypeLoadAddress:
162            case Value::eValueTypeHostAddress:
163                // The DWARF expression result was an address in the inferior
164                // process. If this variable is an aggregate type, we just need
165                // the address as the main value as all child variable objects
166                // will rely upon this location and add an offset and then read
167                // their own values as needed. If this variable is a simple
168                // type, we read all data for it into m_data.
169                // Make sure this type has a value before we try and read it
170
171                // If we have a file address, convert it to a load address if we can.
172                if (value_type == Value::eValueTypeFileAddress && exe_ctx.process && exe_ctx.process->IsAlive())
173                {
174                    lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
175                    if (file_addr != LLDB_INVALID_ADDRESS)
176                    {
177                        SymbolContext var_sc;
178                        variable->CalculateSymbolContext(&var_sc);
179                        if (var_sc.module_sp)
180                        {
181                            ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
182                            if (objfile)
183                            {
184                                Address so_addr(file_addr, objfile->GetSectionList());
185                                lldb::addr_t load_addr = so_addr.GetLoadAddress (exe_ctx.target);
186                                if (load_addr != LLDB_INVALID_ADDRESS)
187                                {
188                                    m_value.SetValueType(Value::eValueTypeLoadAddress);
189                                    m_value.GetScalar() = load_addr;
190                                }
191                            }
192                        }
193                    }
194                }
195
196                if (ClangASTContext::IsAggregateType (GetClangType()))
197                {
198                    // this value object represents an aggregate type whose
199                    // children have values, but this object does not. So we
200                    // say we are changed if our location has changed.
201                    SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
202                }
203                else
204                {
205                    // Copy the Value and set the context to use our Variable
206                    // so it can extract read its value into m_data appropriately
207                    Value value(m_value);
208                    value.SetContext(Value::eContextTypeVariable, variable);
209                    m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0);
210                }
211                break;
212            }
213
214            SetValueIsValid (m_error.Success());
215        }
216    }
217    return m_error.Success();
218}
219
220
221
222bool
223ValueObjectVariable::IsInScope ()
224{
225    ExecutionContextScope *exe_scope = GetExecutionContextScope();
226    if (!exe_scope)
227        return true;
228
229    StackFrame *frame = exe_scope->CalculateStackFrame();
230    if (!frame)
231        return true;
232
233    return m_variable_sp->IsInScope (frame);
234}
235
236