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(size_t 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 case eContextTypeInvalid: 160 case eContextTypeClangType: // clang::Type * 161 case eContextTypeRegisterInfo: // RegisterInfo * 162 case eContextTypeLLDBType: // Type * 163 break; 164 165 case eContextTypeVariable: // Variable * 166 ResolveValue(exe_ctx, ast_context); 167 return true; 168 } 169 return false; 170 } 171 172 size_t 173 Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr) 174 { 175 size_t byte_size = 0; 176 177 switch (m_context_type) 178 { 179 case eContextTypeInvalid: 180 // If we have no context, there is no way to know how much memory to read 181 if (error_ptr) 182 error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read."); 183 break; 184 185 case eContextTypeClangType: 186 if (ast_context == NULL) 187 { 188 if (error_ptr) 189 error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *."); 190 } 191 else 192 { 193 uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context); 194 byte_size = (bit_width + 7 ) / 8; 195 } 196 break; 197 198 case eContextTypeRegisterInfo: // RegisterInfo * 199 if (GetRegisterInfo()) 200 byte_size = GetRegisterInfo()->byte_size; 201 else if (error_ptr) 202 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *."); 203 break; 204 205 case eContextTypeLLDBType: // Type * 206 if (GetType()) 207 byte_size = GetType()->GetByteSize(); 208 else if (error_ptr) 209 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *."); 210 break; 211 212 case eContextTypeVariable: // Variable * 213 if (GetVariable()) 214 { 215 if (GetVariable()->GetType()) 216 byte_size = GetVariable()->GetType()->GetByteSize(); 217 else if (error_ptr) 218 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *."); 219 } 220 else if (error_ptr) 221 error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *."); 222 break; 223 } 224 225 if (error_ptr) 226 { 227 if (byte_size == 0) 228 { 229 if (error_ptr->Success()) 230 error_ptr->SetErrorString("Unable to determine byte size."); 231 } 232 else 233 { 234 error_ptr->Clear(); 235 } 236 } 237 return byte_size; 238 } 239 240 clang_type_t 241 Value::GetClangType () 242 { 243 switch (m_context_type) 244 { 245 case eContextTypeInvalid: 246 break; 247 248 case eContextTypeClangType: 249 return m_context; 250 251 case eContextTypeRegisterInfo: 252 break; // TODO: Eventually convert into a clang type? 253 254 case eContextTypeLLDBType: 255 if (GetType()) 256 return GetType()->GetClangForwardType(); 257 break; 258 259 case eContextTypeVariable: 260 if (GetVariable()) 261 return GetVariable()->GetType()->GetClangForwardType(); 262 break; 263 } 264 265 return NULL; 266 } 267 268 lldb::Format 269 Value::GetValueDefaultFormat () 270 { 271 switch (m_context_type) 272 { 273 case eContextTypeInvalid: 274 break; 275 276 case eContextTypeClangType: 277 return ClangASTType::GetFormat (m_context); 278 279 case eContextTypeRegisterInfo: 280 if (GetRegisterInfo()) 281 return GetRegisterInfo()->format; 282 break; 283 284 case eContextTypeLLDBType: 285 if (GetType()) 286 return GetType()->GetFormat(); 287 break; 288 289 case eContextTypeVariable: 290 if (GetVariable()) 291 return GetVariable()->GetType()->GetFormat(); 292 break; 293 294 } 295 296 // Return a good default in case we can't figure anything out 297 return eFormatHex; 298 } 299 300 bool 301 Value::GetData (DataExtractor &data) 302 { 303 switch (m_value_type) 304 { 305 default: 306 break; 307 308 case eValueTypeScalar: 309 if (m_value.GetData (data)) 310 return true; 311 break; 312 313 case eValueTypeLoadAddress: 314 case eValueTypeFileAddress: 315 case eValueTypeHostAddress: 316 if (m_data_buffer.GetByteSize()) 317 { 318 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder()); 319 return true; 320 } 321 break; 322 } 323 324 return false; 325 326 } 327 328 Error 329 Value::GetValueAsData (ExecutionContext *exe_ctx, 330 clang::ASTContext *ast_context, 331 DataExtractor &data, 332 uint32_t data_offset, 333 Module *module) 334 { 335 data.Clear(); 336 337 Error error; 338 lldb::addr_t address = LLDB_INVALID_ADDRESS; 339 AddressType address_type = eAddressTypeFile; 340 Address file_so_addr; 341 switch (m_value_type) 342 { 343 default: 344 error.SetErrorStringWithFormat("invalid value type %i", m_value_type); 345 break; 346 347 case eValueTypeScalar: 348 data.SetByteOrder (lldb::endian::InlHostByteOrder()); 349 if (m_context_type == eContextTypeClangType && ast_context) 350 { 351 uint32_t ptr_bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, 352 ClangASTContext::GetVoidPtrType(ast_context, false)); 353 uint32_t ptr_byte_size = (ptr_bit_width + 7) / 8; 354 data.SetAddressByteSize (ptr_byte_size); 355 } 356 else 357 data.SetAddressByteSize(sizeof(void *)); 358 if (m_value.GetData (data)) 359 return error; // Success; 360 error.SetErrorStringWithFormat("extracting data from value failed"); 361 break; 362 363 case eValueTypeLoadAddress: 364 if (exe_ctx == NULL) 365 { 366 error.SetErrorString ("can't read load address (no execution context)"); 367 } 368 else 369 { 370 Process *process = exe_ctx->GetProcessPtr(); 371 if (process == NULL || !process->IsAlive()) 372 { 373 Target *target = exe_ctx->GetTargetPtr(); 374 if (target) 375 { 376 // Allow expressions to run and evaluate things when the target 377 // has memory sections loaded. This allows you to use "target modules load" 378 // to load your executable and any shared libraries, then execute 379 // commands where you can look at types in data sections. 380 const SectionLoadList &target_sections = target->GetSectionLoadList(); 381 if (!target_sections.IsEmpty()) 382 { 383 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 384 if (target_sections.ResolveLoadAddress(address, file_so_addr)) 385 { 386 address_type = eAddressTypeLoad; 387 data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 388 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 389 } 390 else 391 address = LLDB_INVALID_ADDRESS; 392 } 393 // else 394 // { 395 // ModuleSP exe_module_sp (target->GetExecutableModule()); 396 // if (exe_module_sp) 397 // { 398 // address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 399 // if (address != LLDB_INVALID_ADDRESS) 400 // { 401 // if (exe_module_sp->ResolveFileAddress(address, file_so_addr)) 402 // { 403 // data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 404 // data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 405 // address_type = eAddressTypeFile; 406 // } 407 // else 408 // { 409 // address = LLDB_INVALID_ADDRESS; 410 // } 411 // } 412 // } 413 // } 414 } 415 else 416 { 417 error.SetErrorString ("can't read load address (invalid process)"); 418 } 419 } 420 else 421 { 422 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 423 address_type = eAddressTypeLoad; 424 data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder()); 425 data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize()); 426 } 427 } 428 break; 429 430 case eValueTypeFileAddress: 431 if (exe_ctx == NULL) 432 { 433 error.SetErrorString ("can't read file address (no execution context)"); 434 } 435 else if (exe_ctx->GetTargetPtr() == NULL) 436 { 437 error.SetErrorString ("can't read file address (invalid target)"); 438 } 439 else 440 { 441 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 442 if (address == LLDB_INVALID_ADDRESS) 443 { 444 error.SetErrorString ("invalid file address"); 445 } 446 else 447 { 448 if (module == NULL) 449 { 450 // The only thing we can currently lock down to a module so that 451 // we can resolve a file address, is a variable. 452 Variable *variable = GetVariable(); 453 if (variable) 454 { 455 SymbolContext var_sc; 456 variable->CalculateSymbolContext(&var_sc); 457 module = var_sc.module_sp.get(); 458 } 459 } 460 461 if (module) 462 { 463 bool resolved = false; 464 ObjectFile *objfile = module->GetObjectFile(); 465 if (objfile) 466 { 467 Address so_addr(address, objfile->GetSectionList()); 468 addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr()); 469 bool process_launched_and_stopped = exe_ctx->GetProcessPtr() 470 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */) 471 : false; 472 // Don't use the load address if the process has exited. 473 if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped) 474 { 475 resolved = true; 476 address = load_address; 477 address_type = eAddressTypeLoad; 478 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder()); 479 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize()); 480 } 481 else 482 { 483 if (so_addr.IsSectionOffset()) 484 { 485 resolved = true; 486 file_so_addr = so_addr; 487 data.SetByteOrder(objfile->GetByteOrder()); 488 data.SetAddressByteSize(objfile->GetAddressByteSize()); 489 } 490 } 491 } 492 if (!resolved) 493 { 494 Variable *variable = GetVariable(); 495 496 if (module) 497 { 498 if (variable) 499 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s%s%s", 500 address, 501 variable->GetName().AsCString(""), 502 module->GetFileSpec().GetDirectory().GetCString(), 503 module->GetFileSpec().GetDirectory() ? "/" : "", 504 module->GetFileSpec().GetFilename().GetCString()); 505 else 506 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s%s%s", 507 address, 508 module->GetFileSpec().GetDirectory().GetCString(), 509 module->GetFileSpec().GetDirectory() ? "/" : "", 510 module->GetFileSpec().GetFilename().GetCString()); 511 } 512 else 513 { 514 if (variable) 515 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'", 516 address, 517 variable->GetName().AsCString("")); 518 else 519 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address); 520 } 521 } 522 } 523 else 524 { 525 // Can't convert a file address to anything valid without more 526 // context (which Module it came from) 527 error.SetErrorString ("can't read memory from file address without more context"); 528 } 529 } 530 } 531 break; 532 533 case eValueTypeHostAddress: 534 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 535 address_type = eAddressTypeHost; 536 if (exe_ctx) 537 { 538 Target *target = exe_ctx->GetTargetPtr(); 539 if (target) 540 { 541 data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 542 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 543 break; 544 } 545 } 546 // fallback to host settings 547 data.SetByteOrder(lldb::endian::InlHostByteOrder()); 548 data.SetAddressByteSize(sizeof(void *)); 549 break; 550 } 551 552 // Bail if we encountered any errors 553 if (error.Fail()) 554 return error; 555 556 if (address == LLDB_INVALID_ADDRESS) 557 { 558 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load"); 559 return error; 560 } 561 562 // If we got here, we need to read the value from memory 563 size_t byte_size = GetValueByteSize (ast_context, &error); 564 565 // Bail if we encountered any errors getting the byte size 566 if (error.Fail()) 567 return error; 568 569 // Make sure we have enough room within "data", and if we don't make 570 // something large enough that does 571 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size)) 572 { 573 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0')); 574 data.SetData(data_sp); 575 } 576 577 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size)); 578 if (dst != NULL) 579 { 580 if (address_type == eAddressTypeHost) 581 { 582 // The address is an address in this process, so just copy it 583 memcpy (dst, (uint8_t*)NULL + address, byte_size); 584 } 585 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile)) 586 { 587 if (file_so_addr.IsValid()) 588 { 589 // We have a file address that we were able to translate into a 590 // section offset address so we might be able to read this from 591 // the object files if we don't have a live process. Lets always 592 // try and read from the process if we have one though since we 593 // want to read the actual value by setting "prefer_file_cache" 594 // to false. 595 const bool prefer_file_cache = false; 596 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size) 597 { 598 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address); 599 } 600 } 601 else 602 { 603 // The execution context might have a NULL process, but it 604 // might have a valid process in the exe_ctx->target, so use 605 // the ExecutionContext::GetProcess accessor to ensure we 606 // get the process if there is one. 607 Process *process = exe_ctx->GetProcessPtr(); 608 609 if (process) 610 { 611 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error); 612 if (bytes_read != byte_size) 613 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)", 614 (uint64_t)address, 615 (uint32_t)bytes_read, 616 (uint32_t)byte_size); 617 } 618 else 619 { 620 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address); 621 } 622 } 623 } 624 else 625 { 626 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type); 627 } 628 } 629 else 630 { 631 error.SetErrorStringWithFormat ("out of memory"); 632 } 633 634 return error; 635 } 636 637 Scalar & 638 Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) 639 { 640 void *opaque_clang_qual_type = GetClangType(); 641 if (opaque_clang_qual_type) 642 { 643 switch (m_value_type) 644 { 645 case eValueTypeScalar: // raw scalar value 646 break; 647 648 default: 649 case eValueTypeFileAddress: 650 case eValueTypeLoadAddress: // load address value 651 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb) 652 { 653 DataExtractor data; 654 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); 655 Error error (GetValueAsData (exe_ctx, ast_context, data, 0, NULL)); 656 if (error.Success()) 657 { 658 Scalar scalar; 659 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar)) 660 { 661 m_value = scalar; 662 m_value_type = eValueTypeScalar; 663 } 664 else 665 { 666 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 667 { 668 m_value.Clear(); 669 m_value_type = eValueTypeScalar; 670 } 671 } 672 } 673 else 674 { 675 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 676 { 677 m_value.Clear(); 678 m_value_type = eValueTypeScalar; 679 } 680 } 681 } 682 break; 683 } 684 } 685 return m_value; 686 } 687 688 Variable * 689 Value::GetVariable() 690 { 691 if (m_context_type == eContextTypeVariable) 692 return static_cast<Variable *> (m_context); 693 return NULL; 694 } 695 696 const char * 697 Value::GetValueTypeAsCString (ValueType value_type) 698 { 699 switch (value_type) 700 { 701 case eValueTypeScalar: return "scalar"; 702 case eValueTypeVector: return "vector"; 703 case eValueTypeFileAddress: return "file address"; 704 case eValueTypeLoadAddress: return "load address"; 705 case eValueTypeHostAddress: return "host address"; 706 }; 707 return "???"; 708 } 709 710 const char * 711 Value::GetContextTypeAsCString (ContextType context_type) 712 { 713 switch (context_type) 714 { 715 case eContextTypeInvalid: return "invalid"; 716 case eContextTypeClangType: return "clang::Type *"; 717 case eContextTypeRegisterInfo: return "RegisterInfo *"; 718 case eContextTypeLLDBType: return "Type *"; 719 case eContextTypeVariable: return "Variable *"; 720 }; 721 return "???"; 722 } 723 724 ValueList::ValueList (const ValueList &rhs) 725 { 726 m_values = rhs.m_values; 727 } 728 729 const ValueList & 730 ValueList::operator= (const ValueList &rhs) 731 { 732 m_values = rhs.m_values; 733 return *this; 734 } 735 736 void 737 ValueList::PushValue (const Value &value) 738 { 739 m_values.push_back (value); 740 } 741 742 size_t 743 ValueList::GetSize() 744 { 745 return m_values.size(); 746 } 747 748 Value * 749 ValueList::GetValueAtIndex (size_t idx) 750 { 751 if (idx < GetSize()) 752 { 753 return &(m_values[idx]); 754 } 755 else 756 return NULL; 757 } 758 759 void 760 ValueList::Clear () 761 { 762 m_values.clear(); 763 } 764