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