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