ValueObjectVariable.cpp revision 254729
157580Smjacob//===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
257580Smjacob//
357580Smjacob//                     The LLVM Compiler Infrastructure
457580Smjacob//
557580Smjacob// This file is distributed under the University of Illinois Open Source
657580Smjacob// License. See LICENSE.TXT for details.
757580Smjacob//
857580Smjacob//===----------------------------------------------------------------------===//
957580Smjacob
1057580Smjacob
1157580Smjacob#include "lldb/Core/ValueObjectVariable.h"
1257580Smjacob
1357580Smjacob// C Includes
1457580Smjacob// C++ Includes
1557580Smjacob// Other libraries and framework includes
1657580Smjacob// Project includes
1757580Smjacob#include "lldb/Core/Module.h"
1857580Smjacob#include "lldb/Core/RegisterValue.h"
1957580Smjacob#include "lldb/Core/ValueObjectList.h"
2057580Smjacob#include "lldb/Core/Value.h"
2157580Smjacob
2257580Smjacob#include "lldb/Symbol/Function.h"
2357580Smjacob#include "lldb/Symbol/ObjectFile.h"
2457580Smjacob#include "lldb/Symbol/SymbolContext.h"
2557580Smjacob#include "lldb/Symbol/SymbolContextScope.h"
2657580Smjacob#include "lldb/Symbol/Type.h"
2757580Smjacob#include "lldb/Symbol/Variable.h"
2857580Smjacob
2957580Smjacob#include "lldb/Target/ExecutionContext.h"
3057580Smjacob#include "lldb/Target/Process.h"
3157580Smjacob#include "lldb/Target/RegisterContext.h"
3257580Smjacob#include "lldb/Target/Target.h"
3357580Smjacob#include "lldb/Target/Thread.h"
3457580Smjacob
3557580Smjacob
36246437Smavusing namespace lldb_private;
37246437Smav
3857580Smjacoblldb::ValueObjectSP
3957580SmjacobValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
4057580Smjacob{
41246437Smav    return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
42246437Smav}
4357580Smjacob
44198934SdelphijValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
45198934Sdelphij    ValueObject(exe_scope),
4657580Smjacob    m_variable_sp(var_sp)
47198934Sdelphij{
4857580Smjacob    // Do not attempt to construct one of these objects with no variable!
4957580Smjacob    assert (m_variable_sp.get() != NULL);
5057580Smjacob    m_name = var_sp->GetName();
5157580Smjacob}
52246437Smav
5357580SmjacobValueObjectVariable::~ValueObjectVariable()
5457580Smjacob{
55246437Smav}
56246437Smav
5757580SmjacobClangASTType
58246437SmavValueObjectVariable::GetClangTypeImpl ()
59246437Smav{
6057580Smjacob    Type *var_type = m_variable_sp->GetType();
61246437Smav    if (var_type)
62246437Smav        return var_type->GetClangForwardType();
6357580Smjacob    return ClangASTType();
64246437Smav}
65246437Smav
6657580SmjacobConstString
67246437SmavValueObjectVariable::GetTypeName()
6857580Smjacob{
6957580Smjacob    Type * var_type = m_variable_sp->GetType();
70246437Smav    if (var_type)
7157580Smjacob        return var_type->GetName();
7257580Smjacob    return ConstString();
73246437Smav}
74246437Smav
7557580SmjacobConstString
76246437SmavValueObjectVariable::GetQualifiedTypeName()
77246437Smav{
7857580Smjacob    Type * var_type = m_variable_sp->GetType();
79246437Smav    if (var_type)
80246437Smav        return var_type->GetQualifiedName();
8157580Smjacob    return ConstString();
82246437Smav}
83246437Smav
8457580Smjacobsize_t
85246437SmavValueObjectVariable::CalculateNumChildren()
86246437Smav{
87246437Smav    ClangASTType type(GetClangType());
88246437Smav
8957580Smjacob    if (!type.IsValid())
9057580Smjacob        return 0;
91246437Smav
92246437Smav    const bool omit_empty_base_classes = true;
9357580Smjacob    return type.GetNumChildren(omit_empty_base_classes);
94246437Smav}
95222336Smav
96222336Smavuint64_t
97246437SmavValueObjectVariable::GetByteSize()
98246437Smav{
9957580Smjacob    ClangASTType type(GetClangType());
100246437Smav
10157580Smjacob    if (!type.IsValid())
10257580Smjacob        return 0;
103246437Smav
10457580Smjacob    return type.GetByteSize();
10557580Smjacob}
106246437Smav
10757580Smjacoblldb::ValueType
10857580SmjacobValueObjectVariable::GetValueType() const
109246437Smav{
11057580Smjacob    if (m_variable_sp)
11157580Smjacob        return m_variable_sp->GetScope();
112246437Smav    return lldb::eValueTypeInvalid;
113246437Smav}
11457580Smjacob
115246437Smavbool
116246437SmavValueObjectVariable::UpdateValue ()
11757580Smjacob{
118246437Smav    SetValueIsValid (false);
119246437Smav    m_error.Clear();
12057580Smjacob
121246437Smav    Variable *variable = m_variable_sp.get();
122246437Smav    DWARFExpression &expr = variable->LocationExpression();
123222336Smav
124246437Smav    if (variable->GetLocationIsConstantValueData())
125222338Smav    {
126222336Smav        // expr doesn't contain DWARF bytes, it contains the constant variable
127246437Smav        // value bytes themselves...
128222336Smav        if (expr.GetExpressionData(m_data))
129222336Smav            m_value.SetContext(Value::eContextTypeVariable, variable);
13057580Smjacob        else
13157580Smjacob            m_error.SetErrorString ("empty constant data");
13257580Smjacob        // constant bytes can't be edited - sorry
13357580Smjacob        m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
13457580Smjacob    }
13557580Smjacob    else
13657580Smjacob    {
13757580Smjacob        lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
138198934Sdelphij        ExecutionContext exe_ctx (GetExecutionContextRef());
13957580Smjacob
14057580Smjacob        Target *target = exe_ctx.GetTargetPtr();
14157580Smjacob        if (target)
14257580Smjacob        {
143246437Smav            m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
14457580Smjacob            m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
14557580Smjacob        }
146246437Smav
14757580Smjacob        if (expr.IsLocationList())
14857580Smjacob        {
149246437Smav            SymbolContext sc;
15057580Smjacob            variable->CalculateSymbolContext (&sc);
15157580Smjacob            if (sc.function)
152246437Smav                loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
15357580Smjacob        }
15457580Smjacob        Value old_value(m_value);
155246437Smav        if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
15657580Smjacob        {
15757580Smjacob            m_resolved_value = m_value;
158246437Smav            m_value.SetContext(Value::eContextTypeVariable, variable);
15957580Smjacob
16057580Smjacob            Value::ValueType value_type = m_value.GetValueType();
161246437Smav
16257580Smjacob            switch (value_type)
16357580Smjacob            {
164246437Smav                case Value::eValueTypeFileAddress:
16557580Smjacob                    SetAddressTypeOfChildren(eAddressTypeFile);
166246437Smav                    break;
167246437Smav                case Value::eValueTypeHostAddress:
168246437Smav                    SetAddressTypeOfChildren(eAddressTypeHost);
16957580Smjacob                    break;
170246437Smav                case Value::eValueTypeLoadAddress:
17157580Smjacob                case Value::eValueTypeScalar:
17257580Smjacob                case Value::eValueTypeVector:
17357580Smjacob                    SetAddressTypeOfChildren(eAddressTypeLoad);
17457580Smjacob                    break;
17557580Smjacob            }
17657580Smjacob
17757580Smjacob            switch (value_type)
178198934Sdelphij            {
17957580Smjacob            case Value::eValueTypeVector:
18057580Smjacob                    // fall through
18157580Smjacob            case Value::eValueTypeScalar:
18257580Smjacob                // The variable value is in the Scalar value inside the m_value.
183246437Smav                // We can point our m_data right to it.
184246437Smav                m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
185246437Smav                break;
186246437Smav
187246437Smav            case Value::eValueTypeFileAddress:
188246437Smav            case Value::eValueTypeLoadAddress:
18957580Smjacob            case Value::eValueTypeHostAddress:
19057580Smjacob                // The DWARF expression result was an address in the inferior
191                // process. If this variable is an aggregate type, we just need
192                // the address as the main value as all child variable objects
193                // will rely upon this location and add an offset and then read
194                // their own values as needed. If this variable is a simple
195                // type, we read all data for it into m_data.
196                // Make sure this type has a value before we try and read it
197
198                // If we have a file address, convert it to a load address if we can.
199                Process *process = exe_ctx.GetProcessPtr();
200                if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
201                {
202                    lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
203                    if (file_addr != LLDB_INVALID_ADDRESS)
204                    {
205                        SymbolContext var_sc;
206                        variable->CalculateSymbolContext(&var_sc);
207                        if (var_sc.module_sp)
208                        {
209                            ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
210                            if (objfile)
211                            {
212                                Address so_addr(file_addr, objfile->GetSectionList());
213                                lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
214                                if (load_addr != LLDB_INVALID_ADDRESS)
215                                {
216                                    m_value.SetValueType(Value::eValueTypeLoadAddress);
217                                    m_value.GetScalar() = load_addr;
218                                }
219                            }
220                        }
221                    }
222                }
223
224                if (GetClangType().IsAggregateType())
225                {
226                    // this value object represents an aggregate type whose
227                    // children have values, but this object does not. So we
228                    // say we are changed if our location has changed.
229                    SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
230                }
231                else
232                {
233                    // Copy the Value and set the context to use our Variable
234                    // so it can extract read its value into m_data appropriately
235                    Value value(m_value);
236                    value.SetContext(Value::eContextTypeVariable, variable);
237                    m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
238                }
239                break;
240            }
241
242            SetValueIsValid (m_error.Success());
243        }
244        else
245        {
246            // could not find location, won't allow editing
247            m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
248        }
249    }
250    return m_error.Success();
251}
252
253
254
255bool
256ValueObjectVariable::IsInScope ()
257{
258    const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
259    if (exe_ctx_ref.HasFrameRef())
260    {
261        ExecutionContext exe_ctx (exe_ctx_ref);
262        StackFrame *frame = exe_ctx.GetFramePtr();
263        if (frame)
264        {
265            return m_variable_sp->IsInScope (frame);
266        }
267        else
268        {
269            // This ValueObject had a frame at one time, but now we
270            // can't locate it, so return false since we probably aren't
271            // in scope.
272            return false;
273        }
274    }
275    // We have a variable that wasn't tied to a frame, which
276    // means it is a global and is always in scope.
277    return true;
278
279}
280
281lldb::ModuleSP
282ValueObjectVariable::GetModule()
283{
284    if (m_variable_sp)
285    {
286        SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
287        if (sc_scope)
288        {
289            return sc_scope->CalculateSymbolContextModule();
290        }
291    }
292    return lldb::ModuleSP();
293}
294
295SymbolContextScope *
296ValueObjectVariable::GetSymbolContextScope()
297{
298    if (m_variable_sp)
299        return m_variable_sp->GetSymbolContextScope();
300    return NULL;
301}
302
303bool
304ValueObjectVariable::GetDeclaration (Declaration &decl)
305{
306    if (m_variable_sp)
307    {
308        decl = m_variable_sp->GetDeclaration();
309        return true;
310    }
311    return false;
312}
313
314const char *
315ValueObjectVariable::GetLocationAsCString ()
316{
317    if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
318        return GetLocationAsCStringImpl(m_resolved_value,
319                                        m_data);
320    else
321        return ValueObject::GetLocationAsCString();
322}
323
324bool
325ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
326{
327    if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
328    {
329        RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
330        ExecutionContext exe_ctx(GetExecutionContextRef());
331        RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
332        RegisterValue reg_value;
333        if (!reg_info || !reg_ctx)
334        {
335            error.SetErrorString("unable to retrieve register info");
336            return false;
337        }
338        error = reg_value.SetValueFromCString(reg_info, value_str);
339        if (error.Fail())
340            return false;
341        if (reg_ctx->WriteRegister (reg_info, reg_value))
342        {
343            SetNeedsUpdate();
344            return true;
345        }
346        else
347        {
348            error.SetErrorString("unable to write back to register");
349            return false;
350        }
351    }
352    else
353        return ValueObject::SetValueFromCString(value_str, error);
354}
355
356bool
357ValueObjectVariable::SetData (DataExtractor &data, Error &error)
358{
359    if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
360    {
361        RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
362        ExecutionContext exe_ctx(GetExecutionContextRef());
363        RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
364        RegisterValue reg_value;
365        if (!reg_info || !reg_ctx)
366        {
367            error.SetErrorString("unable to retrieve register info");
368            return false;
369        }
370        error = reg_value.SetValueFromData(reg_info, data, 0, false);
371        if (error.Fail())
372            return false;
373        if (reg_ctx->WriteRegister (reg_info, reg_value))
374        {
375            SetNeedsUpdate();
376            return true;
377        }
378        else
379        {
380            error.SetErrorString("unable to write back to register");
381            return false;
382        }
383    }
384    else
385        return ValueObject::SetData(data, error);
386}
387