Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 1 | //===-- ValueObjectMemory.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/ValueObjectMemory.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 | #include "lldb/Core/ValueObject.h" |
| 21 | |
| 22 | #include "lldb/Symbol/ObjectFile.h" |
| 23 | #include "lldb/Symbol/SymbolContext.h" |
| 24 | #include "lldb/Symbol/Type.h" |
| 25 | #include "lldb/Symbol/Variable.h" |
| 26 | |
| 27 | #include "lldb/Target/ExecutionContext.h" |
| 28 | #include "lldb/Target/Process.h" |
| 29 | #include "lldb/Target/RegisterContext.h" |
| 30 | #include "lldb/Target/Target.h" |
| 31 | #include "lldb/Target/Thread.h" |
| 32 | |
Jim Ingham | 47da810 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 33 | using namespace lldb; |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 34 | using namespace lldb_private; |
| 35 | |
Jim Ingham | 47da810 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 36 | ValueObjectSP |
| 37 | ValueObjectMemory::Create (ExecutionContextScope *exe_scope, |
| 38 | const char *name, |
| 39 | const Address &address, |
| 40 | lldb::TypeSP &type_sp) |
| 41 | { |
| 42 | return (new ValueObjectMemory (exe_scope, name, address, type_sp))->GetSP(); |
| 43 | } |
| 44 | |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 45 | ValueObjectSP |
| 46 | ValueObjectMemory::Create (ExecutionContextScope *exe_scope, |
| 47 | const char *name, |
| 48 | const Address &address, |
| 49 | const ClangASTType &ast_type) |
| 50 | { |
| 51 | return (new ValueObjectMemory (exe_scope, name, address, ast_type))->GetSP(); |
| 52 | } |
| 53 | |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 54 | ValueObjectMemory::ValueObjectMemory (ExecutionContextScope *exe_scope, |
| 55 | const char *name, |
| 56 | const Address &address, |
| 57 | lldb::TypeSP &type_sp) : |
| 58 | ValueObject(exe_scope), |
| 59 | m_address (address), |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 60 | m_type_sp(type_sp), |
| 61 | m_clang_type() |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 62 | { |
| 63 | // Do not attempt to construct one of these objects with no variable! |
| 64 | assert (m_type_sp.get() != NULL); |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 65 | SetName (ConstString(name)); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 66 | m_value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get()); |
Greg Clayton | b4d7fc0 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 67 | TargetSP target_sp (GetTargetSP()); |
| 68 | lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get()); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 69 | if (load_address != LLDB_INVALID_ADDRESS) |
| 70 | { |
| 71 | m_value.SetValueType(Value::eValueTypeLoadAddress); |
| 72 | m_value.GetScalar() = load_address; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | lldb::addr_t file_address = m_address.GetFileAddress(); |
| 77 | if (file_address != LLDB_INVALID_ADDRESS) |
| 78 | { |
| 79 | m_value.SetValueType(Value::eValueTypeFileAddress); |
| 80 | m_value.GetScalar() = file_address; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | m_value.GetScalar() = m_address.GetOffset(); |
| 85 | m_value.SetValueType (Value::eValueTypeScalar); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 90 | ValueObjectMemory::ValueObjectMemory (ExecutionContextScope *exe_scope, |
| 91 | const char *name, |
| 92 | const Address &address, |
| 93 | const ClangASTType &ast_type) : |
| 94 | ValueObject(exe_scope), |
| 95 | m_address (address), |
| 96 | m_type_sp(), |
| 97 | m_clang_type(ast_type) |
| 98 | { |
| 99 | // Do not attempt to construct one of these objects with no variable! |
| 100 | assert (m_clang_type.GetASTContext()); |
| 101 | assert (m_clang_type.GetOpaqueQualType()); |
| 102 | |
Greg Clayton | b4d7fc0 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 103 | TargetSP target_sp (GetTargetSP()); |
| 104 | |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 105 | SetName (ConstString(name)); |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 106 | m_value.SetContext(Value::eContextTypeClangType, m_clang_type.GetOpaqueQualType()); |
Greg Clayton | b4d7fc0 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 107 | lldb::addr_t load_address = m_address.GetLoadAddress (target_sp.get()); |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 108 | if (load_address != LLDB_INVALID_ADDRESS) |
| 109 | { |
| 110 | m_value.SetValueType(Value::eValueTypeLoadAddress); |
| 111 | m_value.GetScalar() = load_address; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | lldb::addr_t file_address = m_address.GetFileAddress(); |
| 116 | if (file_address != LLDB_INVALID_ADDRESS) |
| 117 | { |
| 118 | m_value.SetValueType(Value::eValueTypeFileAddress); |
| 119 | m_value.GetScalar() = file_address; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | m_value.GetScalar() = m_address.GetOffset(); |
| 124 | m_value.SetValueType (Value::eValueTypeScalar); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 129 | ValueObjectMemory::~ValueObjectMemory() |
| 130 | { |
| 131 | } |
| 132 | |
| 133 | lldb::clang_type_t |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 134 | ValueObjectMemory::GetClangTypeImpl () |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 135 | { |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 136 | if (m_type_sp) |
| 137 | return m_type_sp->GetClangForwardType(); |
| 138 | return m_clang_type.GetOpaqueQualType(); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | ConstString |
| 142 | ValueObjectMemory::GetTypeName() |
| 143 | { |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 144 | if (m_type_sp) |
| 145 | return m_type_sp->GetName(); |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 146 | return ClangASTType::GetConstTypeName (GetClangAST(), m_clang_type.GetOpaqueQualType()); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | uint32_t |
| 150 | ValueObjectMemory::CalculateNumChildren() |
| 151 | { |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 152 | if (m_type_sp) |
| 153 | return m_type_sp->GetNumChildren(true); |
| 154 | const bool omit_empty_base_classes = true; |
| 155 | return ClangASTContext::GetNumChildren (m_clang_type.GetASTContext(), |
| 156 | m_clang_type.GetOpaqueQualType(), |
| 157 | omit_empty_base_classes); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | clang::ASTContext * |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 161 | ValueObjectMemory::GetClangASTImpl () |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 162 | { |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 163 | if (m_type_sp) |
| 164 | return m_type_sp->GetClangAST(); |
| 165 | return m_clang_type.GetASTContext(); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | size_t |
| 169 | ValueObjectMemory::GetByteSize() |
| 170 | { |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 171 | if (m_type_sp) |
| 172 | return m_type_sp->GetByteSize(); |
| 173 | return (m_clang_type.GetClangTypeBitWidth () + 7) / 8; |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | lldb::ValueType |
| 177 | ValueObjectMemory::GetValueType() const |
| 178 | { |
| 179 | // RETHINK: Should this be inherited from somewhere? |
| 180 | return lldb::eValueTypeVariableGlobal; |
| 181 | } |
| 182 | |
| 183 | bool |
| 184 | ValueObjectMemory::UpdateValue () |
| 185 | { |
| 186 | SetValueIsValid (false); |
| 187 | m_error.Clear(); |
| 188 | |
Greg Clayton | b4d7fc0 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 189 | ExecutionContext exe_ctx (GetExecutionContextRef()); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 190 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 191 | Target *target = exe_ctx.GetTargetPtr(); |
| 192 | if (target) |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 193 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 194 | m_data.SetByteOrder(target->GetArchitecture().GetByteOrder()); |
| 195 | m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | Value old_value(m_value); |
| 199 | if (m_address.IsValid()) |
| 200 | { |
| 201 | Value::ValueType value_type = m_value.GetValueType(); |
| 202 | |
| 203 | switch (value_type) |
| 204 | { |
| 205 | default: |
| 206 | assert(!"Unhandled expression result value kind..."); |
| 207 | break; |
| 208 | |
| 209 | case Value::eValueTypeScalar: |
| 210 | // The variable value is in the Scalar value inside the m_value. |
| 211 | // We can point our m_data right to it. |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 212 | m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get()); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 213 | break; |
| 214 | |
| 215 | case Value::eValueTypeFileAddress: |
| 216 | case Value::eValueTypeLoadAddress: |
| 217 | case Value::eValueTypeHostAddress: |
| 218 | // The DWARF expression result was an address in the inferior |
| 219 | // process. If this variable is an aggregate type, we just need |
| 220 | // the address as the main value as all child variable objects |
| 221 | // will rely upon this location and add an offset and then read |
| 222 | // their own values as needed. If this variable is a simple |
| 223 | // type, we read all data for it into m_data. |
| 224 | // Make sure this type has a value before we try and read it |
| 225 | |
| 226 | // If we have a file address, convert it to a load address if we can. |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 227 | if (value_type == Value::eValueTypeFileAddress && exe_ctx.GetProcessPtr()) |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 228 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 229 | lldb::addr_t load_addr = m_address.GetLoadAddress(target); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 230 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 231 | { |
| 232 | m_value.SetValueType(Value::eValueTypeLoadAddress); |
| 233 | m_value.GetScalar() = load_addr; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (ClangASTContext::IsAggregateType (GetClangType())) |
| 238 | { |
| 239 | // this value object represents an aggregate type whose |
| 240 | // children have values, but this object does not. So we |
| 241 | // say we are changed if our location has changed. |
| 242 | SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar()); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | // Copy the Value and set the context to use our Variable |
| 247 | // so it can extract read its value into m_data appropriately |
| 248 | Value value(m_value); |
Greg Clayton | 57b3c6b | 2011-04-27 22:04:39 +0000 | [diff] [blame] | 249 | if (m_type_sp) |
| 250 | value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get()); |
| 251 | else |
| 252 | value.SetContext(Value::eContextTypeClangType, m_clang_type.GetOpaqueQualType()); |
| 253 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 254 | m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get()); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 255 | } |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | SetValueIsValid (m_error.Success()); |
| 260 | } |
| 261 | return m_error.Success(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | bool |
| 267 | ValueObjectMemory::IsInScope () |
| 268 | { |
| 269 | // FIXME: Maybe try to read the memory address, and if that works, then |
| 270 | // we are in scope? |
| 271 | return true; |
| 272 | } |
| 273 | |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 274 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 275 | lldb::ModuleSP |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 276 | ValueObjectMemory::GetModule() |
| 277 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 278 | return m_address.GetModule(); |
Greg Clayton | 801417e | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |