Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Value.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 | #include "lldb/Core/Value.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/DataExtractor.h" |
| 17 | #include "lldb/Core/DataBufferHeap.h" |
| 18 | #include "lldb/Core/Module.h" |
| 19 | #include "lldb/Core/Stream.h" |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/ClangASTType.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/ClangASTContext.h" |
| 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 | #include "lldb/Target/ExecutionContext.h" |
| 27 | #include "lldb/Target/Process.h" |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | Value::Value() : |
| 34 | m_value(), |
| 35 | m_value_type(eValueTypeScalar), |
| 36 | m_context(NULL), |
| 37 | m_context_type(eContextTypeInvalid) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | Value::Value(const Scalar& scalar) : |
| 42 | m_value(scalar), |
| 43 | m_value_type(eValueTypeScalar), |
| 44 | m_context(NULL), |
| 45 | m_context_type(eContextTypeInvalid) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | Value::Value(int v) : |
| 50 | m_value(v), |
| 51 | m_value_type(eValueTypeScalar), |
| 52 | m_context(NULL), |
| 53 | m_context_type(eContextTypeInvalid) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | Value::Value(unsigned int v) : |
| 58 | m_value(v), |
| 59 | m_value_type(eValueTypeScalar), |
| 60 | m_context(NULL), |
| 61 | m_context_type(eContextTypeInvalid) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | Value::Value(long v) : |
| 66 | m_value(v), |
| 67 | m_value_type(eValueTypeScalar), |
| 68 | m_context(NULL), |
| 69 | m_context_type(eContextTypeInvalid) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | Value::Value(unsigned long v) : |
| 74 | m_value(v), |
| 75 | m_value_type(eValueTypeScalar), |
| 76 | m_context(NULL), |
| 77 | m_context_type(eContextTypeInvalid) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | Value::Value(long long v) : |
| 82 | m_value(v), |
| 83 | m_value_type(eValueTypeScalar), |
| 84 | m_context(NULL), |
| 85 | m_context_type(eContextTypeInvalid) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | Value::Value(unsigned long long v) : |
| 90 | m_value(v), |
| 91 | m_value_type(eValueTypeScalar), |
| 92 | m_context(NULL), |
| 93 | m_context_type(eContextTypeInvalid) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | Value::Value(float v) : |
| 98 | m_value(v), |
| 99 | m_value_type(eValueTypeScalar), |
| 100 | m_context(NULL), |
| 101 | m_context_type(eContextTypeInvalid) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | Value::Value(double v) : |
| 106 | m_value(v), |
| 107 | m_value_type(eValueTypeScalar), |
| 108 | m_context(NULL), |
| 109 | m_context_type(eContextTypeInvalid) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | Value::Value(long double v) : |
| 114 | m_value(v), |
| 115 | m_value_type(eValueTypeScalar), |
| 116 | m_context(NULL), |
| 117 | m_context_type(eContextTypeInvalid) |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | Value::Value(const uint8_t *bytes, int len) : |
| 122 | m_value(), |
| 123 | m_value_type(eValueTypeHostAddress), |
| 124 | m_context(NULL), |
| 125 | m_context_type(eContextTypeInvalid) |
| 126 | { |
| 127 | m_data_buffer.CopyData(bytes, len); |
| 128 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
| 129 | } |
| 130 | |
| 131 | Value::Value(const Value &v) : |
| 132 | m_value(v.m_value), |
| 133 | m_value_type(v.m_value_type), |
| 134 | m_context(v.m_context), |
| 135 | m_context_type(v.m_context_type) |
| 136 | { |
| 137 | if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes()) |
| 138 | { |
| 139 | m_data_buffer.CopyData(v.m_data_buffer.GetBytes(), |
| 140 | v.m_data_buffer.GetByteSize()); |
| 141 | |
| 142 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
| 143 | } |
| 144 | } |
| 145 | |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 146 | Value & |
| 147 | Value::operator=(const Value &rhs) |
| 148 | { |
| 149 | if (this != &rhs) |
| 150 | { |
| 151 | m_value = rhs.m_value; |
| 152 | m_value_type = rhs.m_value_type; |
| 153 | m_context = rhs.m_context; |
| 154 | m_context_type = rhs.m_context_type; |
| 155 | if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes()) |
| 156 | { |
| 157 | m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(), |
| 158 | rhs.m_data_buffer.GetByteSize()); |
| 159 | |
| 160 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
| 161 | } |
| 162 | } |
| 163 | return *this; |
| 164 | } |
| 165 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | Value * |
| 167 | Value::CreateProxy() |
| 168 | { |
| 169 | if (m_context_type == eContextTypeValue) |
| 170 | return ((Value*)m_context)->CreateProxy (); |
| 171 | |
| 172 | Value *ret = new Value; |
| 173 | ret->SetContext(eContextTypeValue, this); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | Value * |
| 178 | Value::GetProxyTarget() |
| 179 | { |
| 180 | if (m_context_type == eContextTypeValue) |
| 181 | return (Value*)m_context; |
| 182 | else |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | void |
| 187 | Value::Dump (Stream* strm) |
| 188 | { |
| 189 | if (m_context_type == eContextTypeValue) |
| 190 | { |
| 191 | ((Value*)m_context)->Dump (strm); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | m_value.GetValue (strm, true); |
| 196 | strm->Printf(", value_type = %s, context = %p, context_type = %s", |
| 197 | Value::GetValueTypeAsCString(m_value_type), |
| 198 | m_context, |
| 199 | Value::GetContextTypeAsCString(m_context_type)); |
| 200 | } |
| 201 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | Value::ValueType |
| 203 | Value::GetValueType() const |
| 204 | { |
| 205 | if (m_context_type == eContextTypeValue) |
| 206 | return ((Value*)m_context)->GetValueType (); |
| 207 | |
| 208 | return m_value_type; |
| 209 | } |
| 210 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame^] | 211 | AddressType |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | Value::GetValueAddressType () const |
| 213 | { |
| 214 | if (m_context_type == eContextTypeValue) |
| 215 | return ((Value*)m_context)->GetValueAddressType (); |
| 216 | |
| 217 | switch (m_value_type) |
| 218 | { |
| 219 | default: |
| 220 | case eValueTypeScalar: |
| 221 | break; |
| 222 | case eValueTypeLoadAddress: return eAddressTypeLoad; |
| 223 | case eValueTypeFileAddress: return eAddressTypeFile; |
| 224 | case eValueTypeHostAddress: return eAddressTypeHost; |
| 225 | } |
| 226 | return eAddressTypeInvalid; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | Value::ContextType |
| 231 | Value::GetContextType() const |
| 232 | { |
| 233 | if (m_context_type == eContextTypeValue) |
| 234 | return ((Value*)m_context)->GetContextType (); |
| 235 | |
| 236 | return m_context_type; |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | Value::SetValueType (Value::ValueType value_type) |
| 241 | { |
| 242 | if (m_context_type == eContextTypeValue) |
| 243 | { |
| 244 | ((Value*)m_context)->SetValueType(value_type); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | m_value_type = value_type; |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | Value::ClearContext () |
| 253 | { |
| 254 | if (m_context_type == eContextTypeValue) |
| 255 | { |
| 256 | ((Value*)m_context)->ClearContext(); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | m_context = NULL; |
| 261 | m_context_type = eContextTypeInvalid; |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | Value::SetContext (Value::ContextType context_type, void *p) |
| 266 | { |
| 267 | if (m_context_type == eContextTypeValue) |
| 268 | { |
| 269 | ((Value*)m_context)->SetContext(context_type, p); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | m_context_type = context_type; |
| 274 | m_context = p; |
| 275 | } |
| 276 | |
| 277 | RegisterInfo * |
| 278 | Value::GetRegisterInfo() |
| 279 | { |
| 280 | if (m_context_type == eContextTypeValue) |
| 281 | return ((Value*)m_context)->GetRegisterInfo(); |
| 282 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 283 | if (m_context_type == eContextTypeRegisterInfo) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | return static_cast<RegisterInfo *> (m_context); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | Type * |
| 289 | Value::GetType() |
| 290 | { |
| 291 | if (m_context_type == eContextTypeValue) |
| 292 | return ((Value*)m_context)->GetType(); |
| 293 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 294 | if (m_context_type == eContextTypeLLDBType) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | return static_cast<Type *> (m_context); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | Scalar & |
| 300 | Value::GetScalar() |
| 301 | { |
| 302 | if (m_context_type == eContextTypeValue) |
| 303 | return ((Value*)m_context)->GetScalar(); |
| 304 | |
| 305 | return m_value; |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | Value::ResizeData(int len) |
| 310 | { |
| 311 | if (m_context_type == eContextTypeValue) |
| 312 | { |
| 313 | ((Value*)m_context)->ResizeData(len); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | m_value_type = eValueTypeHostAddress; |
| 318 | m_data_buffer.SetByteSize(len); |
| 319 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
| 320 | } |
| 321 | |
| 322 | bool |
| 323 | Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) |
| 324 | { |
| 325 | if (m_context_type == eContextTypeValue) |
| 326 | return ((Value*)m_context)->ValueOf(exe_ctx, ast_context); |
| 327 | |
| 328 | switch (m_context_type) |
| 329 | { |
| 330 | default: |
| 331 | case eContextTypeInvalid: |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 332 | case eContextTypeClangType: // clang::Type * |
| 333 | case eContextTypeRegisterInfo: // RegisterInfo * |
| 334 | case eContextTypeLLDBType: // Type * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 335 | break; |
| 336 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 337 | case eContextTypeVariable: // Variable * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 338 | ResolveValue(exe_ctx, ast_context); |
| 339 | return true; |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | size_t |
| 345 | Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr) |
| 346 | { |
| 347 | if (m_context_type == eContextTypeValue) |
| 348 | return ((Value*)m_context)->GetValueByteSize(ast_context, error_ptr); |
| 349 | |
| 350 | size_t byte_size = 0; |
| 351 | |
| 352 | switch (m_context_type) |
| 353 | { |
| 354 | default: |
| 355 | case eContextTypeInvalid: |
| 356 | // If we have no context, there is no way to know how much memory to read |
| 357 | if (error_ptr) |
| 358 | error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read."); |
| 359 | break; |
| 360 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 361 | case eContextTypeClangType: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 362 | if (ast_context == NULL) |
| 363 | { |
| 364 | if (error_ptr) |
| 365 | error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *."); |
| 366 | } |
| 367 | else |
| 368 | { |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 369 | uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | byte_size = (bit_width + 7 ) / 8; |
| 371 | } |
| 372 | break; |
| 373 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 374 | case eContextTypeRegisterInfo: // RegisterInfo * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | if (GetRegisterInfo()) |
| 376 | byte_size = GetRegisterInfo()->byte_size; |
| 377 | else if (error_ptr) |
| 378 | error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *."); |
| 379 | |
| 380 | break; |
| 381 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 382 | case eContextTypeLLDBType: // Type * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | if (GetType()) |
| 384 | byte_size = GetType()->GetByteSize(); |
| 385 | else if (error_ptr) |
| 386 | error_ptr->SetErrorString ("Can't determine byte size with NULL Type *."); |
| 387 | break; |
| 388 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 389 | case eContextTypeVariable: // Variable * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | if (GetVariable()) |
| 391 | byte_size = GetVariable()->GetType()->GetByteSize(); |
| 392 | else if (error_ptr) |
| 393 | error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *."); |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | if (error_ptr) |
| 398 | { |
| 399 | if (byte_size == 0) |
| 400 | { |
| 401 | if (error_ptr->Success()) |
| 402 | error_ptr->SetErrorString("Unable to determine byte size."); |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | error_ptr->Clear(); |
| 407 | } |
| 408 | } |
| 409 | return byte_size; |
| 410 | } |
| 411 | |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 412 | clang_type_t |
Greg Clayton | 1be10fc | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 413 | Value::GetClangType () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 414 | { |
| 415 | if (m_context_type == eContextTypeValue) |
Greg Clayton | 1be10fc | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 416 | return ((Value*)m_context)->GetClangType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | |
| 418 | switch (m_context_type) |
| 419 | { |
| 420 | default: |
| 421 | case eContextTypeInvalid: |
| 422 | break; |
| 423 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 424 | case eContextTypeClangType: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | return m_context; |
| 426 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 427 | case eContextTypeRegisterInfo: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | break; // TODO: Eventually convert into a clang type? |
| 429 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 430 | case eContextTypeLLDBType: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 431 | if (GetType()) |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 432 | return GetType()->GetClangForwardType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 433 | break; |
| 434 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 435 | case eContextTypeVariable: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 436 | if (GetVariable()) |
Greg Clayton | f4ecaa5 | 2011-02-16 23:00:21 +0000 | [diff] [blame] | 437 | return GetVariable()->GetType()->GetClangForwardType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | lldb::Format |
| 445 | Value::GetValueDefaultFormat () |
| 446 | { |
| 447 | if (m_context_type == eContextTypeValue) |
| 448 | return ((Value*)m_context)->GetValueDefaultFormat(); |
| 449 | |
| 450 | switch (m_context_type) |
| 451 | { |
| 452 | default: |
| 453 | case eContextTypeInvalid: |
| 454 | break; |
| 455 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 456 | case eContextTypeClangType: |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 457 | return ClangASTType::GetFormat (m_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 458 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 459 | case eContextTypeRegisterInfo: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | if (GetRegisterInfo()) |
| 461 | return GetRegisterInfo()->format; |
| 462 | break; |
| 463 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 464 | case eContextTypeLLDBType: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 465 | if (GetType()) |
| 466 | return GetType()->GetFormat(); |
| 467 | break; |
| 468 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 469 | case eContextTypeVariable: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 470 | if (GetVariable()) |
| 471 | return GetVariable()->GetType()->GetFormat(); |
| 472 | break; |
| 473 | |
| 474 | } |
| 475 | |
| 476 | // Return a good default in case we can't figure anything out |
| 477 | return eFormatHex; |
| 478 | } |
| 479 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 480 | bool |
| 481 | Value::GetData (DataExtractor &data) |
| 482 | { |
| 483 | switch (m_value_type) |
| 484 | { |
| 485 | default: |
| 486 | break; |
| 487 | |
| 488 | case eValueTypeScalar: |
| 489 | if (m_value.GetData (data)) |
| 490 | return true; |
| 491 | break; |
| 492 | |
| 493 | case eValueTypeLoadAddress: |
| 494 | case eValueTypeFileAddress: |
| 495 | case eValueTypeHostAddress: |
| 496 | if (m_data_buffer.GetByteSize()) |
| 497 | { |
| 498 | data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder()); |
| 499 | return true; |
| 500 | } |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | return false; |
| 505 | |
| 506 | } |
| 507 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 508 | Error |
| 509 | Value::GetValueAsData (ExecutionContext *exe_ctx, clang::ASTContext *ast_context, DataExtractor &data, uint32_t data_offset) |
| 510 | { |
| 511 | if (m_context_type == eContextTypeValue) |
| 512 | return ((Value*)m_context)->GetValueAsData(exe_ctx, ast_context, data, data_offset); |
| 513 | |
| 514 | data.Clear(); |
| 515 | |
| 516 | Error error; |
| 517 | lldb::addr_t address = LLDB_INVALID_ADDRESS; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame^] | 518 | AddressType address_type = eAddressTypeFile; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 519 | switch (m_value_type) |
| 520 | { |
| 521 | default: |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 522 | error.SetErrorStringWithFormat("invalid value type %i", m_value_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 523 | break; |
| 524 | |
| 525 | case eValueTypeScalar: |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 526 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
Jim Ingham | f6ea93f | 2011-02-24 21:23:14 +0000 | [diff] [blame] | 527 | if (m_context_type == eContextTypeClangType && ast_context) |
| 528 | { |
| 529 | uint32_t ptr_bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, |
| 530 | ClangASTContext::GetVoidPtrType(ast_context, false)); |
| 531 | uint32_t ptr_byte_size = (ptr_bit_width + 7) / 8; |
| 532 | data.SetAddressByteSize (ptr_byte_size); |
| 533 | } |
| 534 | else |
| 535 | data.SetAddressByteSize(sizeof(void *)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 536 | if (m_value.GetData (data)) |
| 537 | return error; // Success; |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 538 | error.SetErrorStringWithFormat("extracting data from value failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 539 | break; |
| 540 | |
| 541 | case eValueTypeLoadAddress: |
| 542 | if (exe_ctx == NULL) |
| 543 | { |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 544 | error.SetErrorString ("can't read memory (no execution context)"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 545 | } |
| 546 | else if (exe_ctx->process == NULL) |
| 547 | { |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 548 | error.SetErrorString ("can't read memory (invalid process)"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | } |
| 550 | else |
| 551 | { |
| 552 | address = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 553 | address_type = eAddressTypeLoad; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 554 | data.SetByteOrder(exe_ctx->process->GetTarget().GetArchitecture().GetByteOrder()); |
| 555 | data.SetAddressByteSize(exe_ctx->process->GetTarget().GetArchitecture().GetAddressByteSize()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 556 | } |
| 557 | break; |
| 558 | |
| 559 | case eValueTypeFileAddress: |
| 560 | { |
| 561 | // The only thing we can currently lock down to a module so that |
| 562 | // we can resolve a file address, is a variable. |
| 563 | Variable *variable = GetVariable(); |
| 564 | |
| 565 | if (GetVariable()) |
| 566 | { |
| 567 | lldb::addr_t file_addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 568 | if (file_addr != LLDB_INVALID_ADDRESS) |
| 569 | { |
| 570 | SymbolContext var_sc; |
| 571 | variable->CalculateSymbolContext(&var_sc); |
| 572 | if (var_sc.module_sp) |
| 573 | { |
| 574 | ObjectFile *objfile = var_sc.module_sp->GetObjectFile(); |
| 575 | if (objfile) |
| 576 | { |
| 577 | Address so_addr(file_addr, objfile->GetSectionList()); |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 578 | address = so_addr.GetLoadAddress (exe_ctx->target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 579 | if (address != LLDB_INVALID_ADDRESS) |
Greg Clayton | ad3843c | 2010-08-18 18:28:52 +0000 | [diff] [blame] | 580 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | address_type = eAddressTypeLoad; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 582 | data.SetByteOrder(exe_ctx->target->GetArchitecture().GetByteOrder()); |
| 583 | data.SetAddressByteSize(exe_ctx->target->GetArchitecture().GetAddressByteSize()); |
Greg Clayton | ad3843c | 2010-08-18 18:28:52 +0000 | [diff] [blame] | 584 | } |
| 585 | else |
| 586 | { |
| 587 | data.SetByteOrder(objfile->GetByteOrder()); |
| 588 | data.SetAddressByteSize(objfile->GetAddressByteSize()); |
| 589 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | } |
| 591 | if (address_type == eAddressTypeFile) |
| 592 | error.SetErrorStringWithFormat ("%s is not loaded.\n", var_sc.module_sp->GetFileSpec().GetFilename().AsCString()); |
| 593 | } |
| 594 | else |
| 595 | { |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 596 | error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s'", file_addr, variable->GetName().AsCString("")); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | error.SetErrorString ("Invalid file address."); |
| 602 | } |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | // Can't convert a file address to anything valid without more |
| 607 | // context (which Module it came from) |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 608 | error.SetErrorString ("can't read memory from file address without more context"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | break; |
| 612 | |
| 613 | case eValueTypeHostAddress: |
| 614 | address = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 615 | data.SetByteOrder(lldb::endian::InlHostByteOrder()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 616 | data.SetAddressByteSize(sizeof(void *)); |
| 617 | address_type = eAddressTypeHost; |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | // Bail if we encountered any errors |
| 622 | if (error.Fail()) |
| 623 | return error; |
| 624 | |
| 625 | if (address == LLDB_INVALID_ADDRESS) |
| 626 | { |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 627 | error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 628 | return error; |
| 629 | } |
| 630 | |
| 631 | // If we got here, we need to read the value from memory |
| 632 | uint32_t byte_size = GetValueByteSize (ast_context, &error); |
| 633 | |
| 634 | // Bail if we encountered any errors getting the byte size |
| 635 | if (error.Fail()) |
| 636 | return error; |
| 637 | |
| 638 | // Make sure we have enough room within "data", and if we don't make |
| 639 | // something large enough that does |
| 640 | if (!data.ValidOffsetForDataOfSize (data_offset, byte_size)) |
| 641 | { |
| 642 | DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0')); |
| 643 | data.SetData(data_sp); |
| 644 | } |
| 645 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 646 | uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 647 | if (dst != NULL) |
| 648 | { |
| 649 | if (address_type == eAddressTypeHost) |
| 650 | { |
| 651 | // The address is an address in this process, so just copy it |
| 652 | memcpy (dst, (uint8_t*)NULL + address, byte_size); |
| 653 | } |
| 654 | else if (address_type == eAddressTypeLoad) |
| 655 | { |
| 656 | if (exe_ctx->process->ReadMemory(address, dst, byte_size, error) != byte_size) |
| 657 | { |
| 658 | if (error.Success()) |
| 659 | error.SetErrorStringWithFormat("read %u bytes of memory from 0x%llx failed", (uint64_t)address, byte_size); |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 660 | else |
| 661 | error.SetErrorStringWithFormat("read memory from 0x%llx failed", (uint64_t)address); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | else |
| 665 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame^] | 666 | error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | else |
| 670 | { |
Greg Clayton | 7c8a966 | 2010-11-02 01:50:16 +0000 | [diff] [blame] | 671 | error.SetErrorStringWithFormat ("out of memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | return error; |
| 675 | } |
| 676 | |
| 677 | Scalar & |
| 678 | Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) |
| 679 | { |
| 680 | Scalar scalar; |
| 681 | if (m_context_type == eContextTypeValue) |
| 682 | { |
| 683 | // Resolve the proxy |
| 684 | |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 685 | Value * rhs = (Value*)m_context; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 687 | m_value = rhs->m_value; |
| 688 | m_value_type = rhs->m_value_type; |
| 689 | m_context = rhs->m_context; |
| 690 | m_context_type = rhs->m_context_type; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 691 | |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 692 | if ((uintptr_t)rhs->m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs->m_data_buffer.GetBytes()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 693 | { |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 694 | m_data_buffer.CopyData(rhs->m_data_buffer.GetBytes(), |
| 695 | rhs->m_data_buffer.GetByteSize()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 696 | |
| 697 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
| 698 | } |
| 699 | } |
| 700 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 701 | if (m_context_type == eContextTypeClangType) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 702 | { |
Greg Clayton | 1be10fc | 2010-09-29 01:12:09 +0000 | [diff] [blame] | 703 | void *opaque_clang_qual_type = GetClangType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 704 | switch (m_value_type) |
| 705 | { |
| 706 | case eValueTypeScalar: // raw scalar value |
| 707 | break; |
| 708 | |
| 709 | default: |
| 710 | case eValueTypeFileAddress: |
| 711 | m_value.Clear(); |
| 712 | break; |
| 713 | |
| 714 | case eValueTypeLoadAddress: // load address value |
| 715 | case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb) |
| 716 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame^] | 717 | AddressType address_type = m_value_type == eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 718 | lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 719 | DataExtractor data; |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 720 | if (ClangASTType::ReadFromMemory (ast_context, opaque_clang_qual_type, exe_ctx, addr, address_type, data)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 721 | { |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 722 | if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 723 | { |
| 724 | m_value = scalar; |
| 725 | m_value_type = eValueTypeScalar; |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) |
| 730 | { |
| 731 | m_value.Clear(); |
| 732 | m_value_type = eValueTypeScalar; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) |
| 739 | { |
| 740 | m_value.Clear(); |
| 741 | m_value_type = eValueTypeScalar; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | |
| 749 | } |
| 750 | return m_value; |
| 751 | } |
| 752 | |
| 753 | Variable * |
| 754 | Value::GetVariable() |
| 755 | { |
| 756 | if (m_context_type == eContextTypeValue) |
| 757 | return ((Value*)m_context)->GetVariable(); |
| 758 | |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 759 | if (m_context_type == eContextTypeVariable) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | return static_cast<Variable *> (m_context); |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | |
| 766 | const char * |
| 767 | Value::GetValueTypeAsCString (ValueType value_type) |
| 768 | { |
| 769 | switch (value_type) |
| 770 | { |
| 771 | case eValueTypeScalar: return "scalar"; |
| 772 | case eValueTypeFileAddress: return "file address"; |
| 773 | case eValueTypeLoadAddress: return "load address"; |
| 774 | case eValueTypeHostAddress: return "host address"; |
| 775 | }; |
| 776 | return "???"; |
| 777 | } |
| 778 | |
| 779 | const char * |
| 780 | Value::GetContextTypeAsCString (ContextType context_type) |
| 781 | { |
| 782 | switch (context_type) |
| 783 | { |
| 784 | case eContextTypeInvalid: return "invalid"; |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 785 | case eContextTypeClangType: return "clang::Type *"; |
| 786 | case eContextTypeRegisterInfo: return "RegisterInfo *"; |
| 787 | case eContextTypeLLDBType: return "Type *"; |
| 788 | case eContextTypeVariable: return "Variable *"; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 789 | case eContextTypeValue: return "Value"; // TODO: Sean, more description here? |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 790 | }; |
| 791 | return "???"; |
| 792 | } |
| 793 | |
| 794 | ValueList::ValueList (const ValueList &rhs) |
| 795 | { |
| 796 | m_values = rhs.m_values; |
| 797 | } |
| 798 | |
| 799 | const ValueList & |
| 800 | ValueList::operator= (const ValueList &rhs) |
| 801 | { |
| 802 | m_values = rhs.m_values; |
| 803 | return *this; |
| 804 | } |
| 805 | |
| 806 | void |
| 807 | ValueList::PushValue (const Value &value) |
| 808 | { |
| 809 | m_values.push_back (value); |
| 810 | } |
| 811 | |
| 812 | size_t |
| 813 | ValueList::GetSize() |
| 814 | { |
| 815 | return m_values.size(); |
| 816 | } |
| 817 | |
| 818 | Value * |
| 819 | ValueList::GetValueAtIndex (size_t idx) |
| 820 | { |
| 821 | if (idx < GetSize()) |
| 822 | { |
| 823 | return &(m_values[idx]); |
| 824 | } |
| 825 | else |
| 826 | return NULL; |
| 827 | } |
| 828 | |
| 829 | void |
| 830 | ValueList::Clear () |
| 831 | { |
| 832 | m_values.clear(); |
| 833 | } |