Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace lldb_private; |
| 34 | |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 35 | lldb::ValueObjectSP |
| 36 | ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) |
| 37 | { |
| 38 | return (new ValueObjectVariable (exe_scope, var_sp))->GetSP(); |
| 39 | } |
| 40 | |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 41 | ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) : |
| 42 | ValueObject(exe_scope), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
| 50 | ValueObjectVariable::~ValueObjectVariable() |
| 51 | { |
| 52 | } |
| 53 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 54 | lldb::clang_type_t |
Greg Clayton | 1be10fc | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 55 | ValueObjectVariable::GetClangType () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | { |
| 57 | Type *var_type = m_variable_sp->GetType(); |
| 58 | if (var_type) |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 59 | return var_type->GetClangForwardType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | ConstString |
| 64 | ValueObjectVariable::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 | |
| 73 | uint32_t |
| 74 | ValueObjectVariable::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 | |
| 82 | clang::ASTContext * |
| 83 | ValueObjectVariable::GetClangAST () |
| 84 | { |
| 85 | return m_variable_sp->GetType()->GetClangAST(); |
| 86 | } |
| 87 | |
| 88 | size_t |
| 89 | ValueObjectVariable::GetByteSize() |
| 90 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 91 | Type *type = m_variable_sp->GetType(); |
| 92 | if (type) |
| 93 | return type->GetByteSize(); |
| 94 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | lldb::ValueType |
| 98 | ValueObjectVariable::GetValueType() const |
| 99 | { |
| 100 | if (m_variable_sp) |
| 101 | return m_variable_sp->GetScope(); |
| 102 | return lldb::eValueTypeInvalid; |
| 103 | } |
| 104 | |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 105 | bool |
| 106 | ValueObjectVariable::UpdateValue () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | { |
| 108 | SetValueIsValid (false); |
| 109 | m_error.Clear(); |
| 110 | |
| 111 | Variable *variable = m_variable_sp.get(); |
| 112 | DWARFExpression &expr = variable->LocationExpression(); |
Greg Clayton | f5fb427 | 2010-09-18 04:00:06 +0000 | [diff] [blame] | 113 | |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 114 | if (variable->GetLocationIsConstantValueData()) |
Greg Clayton | f5fb427 | 2010-09-18 04:00:06 +0000 | [diff] [blame] | 115 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 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"); |
Greg Clayton | f5fb427 | 2010-09-18 04:00:06 +0000 | [diff] [blame] | 122 | } |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 123 | else |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 124 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 125 | lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS; |
| 126 | ExecutionContext exe_ctx (GetExecutionContextScope()); |
| 127 | |
| 128 | if (exe_ctx.target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 130 | m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder()); |
| 131 | m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize()); |
| 132 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 146 | Value::ValueType value_type = m_value.GetValueType(); |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 147 | |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 148 | switch (value_type) |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 149 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 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) |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 173 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 174 | lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 175 | if (file_addr != LLDB_INVALID_ADDRESS) |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 176 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 177 | SymbolContext var_sc; |
| 178 | variable->CalculateSymbolContext(&var_sc); |
| 179 | if (var_sc.module_sp) |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 180 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 181 | ObjectFile *objfile = var_sc.module_sp->GetObjectFile(); |
| 182 | if (objfile) |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 183 | { |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 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 | } |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 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; |
Greg Clayton | a134cc1 | 2010-09-13 02:37:44 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Greg Clayton | 007d5be | 2011-05-30 00:49:24 +0000 | [diff] [blame^] | 214 | SetValueIsValid (m_error.Success()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | } |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 217 | return m_error.Success(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | bool |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 223 | ValueObjectVariable::IsInScope () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | { |
Jim Ingham | 6035b67 | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | return m_variable_sp->IsInScope (frame); |
| 234 | } |
| 235 | |