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() const 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 uint64_t 173 Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr) 174 { 175 uint64_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 byte_size = ClangASTType(ast_context, m_context).GetClangTypeByteSize(); 194 } 195 break; 196 197 case eContextTypeRegisterInfo: // RegisterInfo * 198 if (GetRegisterInfo()) 199 byte_size = GetRegisterInfo()->byte_size; 200 else if (error_ptr) 201 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *."); 202 break; 203 204 case eContextTypeLLDBType: // Type * 205 if (GetType()) 206 byte_size = GetType()->GetByteSize(); 207 else if (error_ptr) 208 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *."); 209 break; 210 211 case eContextTypeVariable: // Variable * 212 if (GetVariable()) 213 { 214 if (GetVariable()->GetType()) 215 byte_size = GetVariable()->GetType()->GetByteSize(); 216 else if (error_ptr) 217 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *."); 218 } 219 else if (error_ptr) 220 error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *."); 221 break; 222 } 223 224 if (error_ptr) 225 { 226 if (byte_size == 0) 227 { 228 if (error_ptr->Success()) 229 error_ptr->SetErrorString("Unable to determine byte size."); 230 } 231 else 232 { 233 error_ptr->Clear(); 234 } 235 } 236 return byte_size; 237 } 238 239 clang_type_t 240 Value::GetClangType () 241 { 242 switch (m_context_type) 243 { 244 case eContextTypeInvalid: 245 break; 246 247 case eContextTypeClangType: 248 return m_context; 249 250 case eContextTypeRegisterInfo: 251 break; // TODO: Eventually convert into a clang type? 252 253 case eContextTypeLLDBType: 254 if (GetType()) 255 return GetType()->GetClangForwardType(); 256 break; 257 258 case eContextTypeVariable: 259 if (GetVariable()) 260 return GetVariable()->GetType()->GetClangForwardType(); 261 break; 262 } 263 264 return NULL; 265 } 266 267 lldb::Format 268 Value::GetValueDefaultFormat () 269 { 270 switch (m_context_type) 271 { 272 case eContextTypeInvalid: 273 break; 274 275 case eContextTypeClangType: 276 return ClangASTType::GetFormat (m_context); 277 278 case eContextTypeRegisterInfo: 279 if (GetRegisterInfo()) 280 return GetRegisterInfo()->format; 281 break; 282 283 case eContextTypeLLDBType: 284 if (GetType()) 285 return GetType()->GetFormat(); 286 break; 287 288 case eContextTypeVariable: 289 if (GetVariable()) 290 return GetVariable()->GetType()->GetFormat(); 291 break; 292 293 } 294 295 // Return a good default in case we can't figure anything out 296 return eFormatHex; 297 } 298 299 bool 300 Value::GetData (DataExtractor &data) 301 { 302 switch (m_value_type) 303 { 304 default: 305 break; 306 307 case eValueTypeScalar: 308 if (m_value.GetData (data)) 309 return true; 310 break; 311 312 case eValueTypeLoadAddress: 313 case eValueTypeFileAddress: 314 case eValueTypeHostAddress: 315 if (m_data_buffer.GetByteSize()) 316 { 317 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder()); 318 return true; 319 } 320 break; 321 } 322 323 return false; 324 325 } 326 327 Error 328 Value::GetValueAsData (ExecutionContext *exe_ctx, 329 clang::ASTContext *ast_context, 330 DataExtractor &data, 331 uint32_t data_offset, 332 Module *module) 333 { 334 data.Clear(); 335 336 Error error; 337 lldb::addr_t address = LLDB_INVALID_ADDRESS; 338 AddressType address_type = eAddressTypeFile; 339 Address file_so_addr; 340 switch (m_value_type) 341 { 342 case eValueTypeVector: 343 if (m_context_type == eContextTypeClangType && ast_context) 344 { 345 ClangASTType ptr_type (ast_context, ClangASTContext::GetVoidPtrType(ast_context, false)); 346 uint64_t ptr_byte_size = ptr_type.GetClangTypeByteSize(); 347 data.SetAddressByteSize (ptr_byte_size); 348 } 349 else 350 data.SetAddressByteSize(sizeof(void *)); 351 data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order); 352 break; 353 354 case eValueTypeScalar: 355 data.SetByteOrder (lldb::endian::InlHostByteOrder()); 356 if (m_context_type == eContextTypeClangType && ast_context) 357 { 358 ClangASTType ptr_type (ast_context, ClangASTContext::GetVoidPtrType(ast_context, false)); 359 uint64_t ptr_byte_size = ptr_type.GetClangTypeByteSize(); 360 data.SetAddressByteSize (ptr_byte_size); 361 } 362 else 363 data.SetAddressByteSize(sizeof(void *)); 364 if (m_value.GetData (data)) 365 return error; // Success; 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 (ast_context, &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 memcpy (dst, (uint8_t*)NULL + address, byte_size); 586 } 587 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile)) 588 { 589 if (file_so_addr.IsValid()) 590 { 591 // We have a file address that we were able to translate into a 592 // section offset address so we might be able to read this from 593 // the object files if we don't have a live process. Lets always 594 // try and read from the process if we have one though since we 595 // want to read the actual value by setting "prefer_file_cache" 596 // to false. 597 const bool prefer_file_cache = false; 598 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size) 599 { 600 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address); 601 } 602 } 603 else 604 { 605 // The execution context might have a NULL process, but it 606 // might have a valid process in the exe_ctx->target, so use 607 // the ExecutionContext::GetProcess accessor to ensure we 608 // get the process if there is one. 609 Process *process = exe_ctx->GetProcessPtr(); 610 611 if (process) 612 { 613 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error); 614 if (bytes_read != byte_size) 615 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)", 616 (uint64_t)address, 617 (uint32_t)bytes_read, 618 (uint32_t)byte_size); 619 } 620 else 621 { 622 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address); 623 } 624 } 625 } 626 else 627 { 628 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type); 629 } 630 } 631 else 632 { 633 error.SetErrorStringWithFormat ("out of memory"); 634 } 635 636 return error; 637 } 638 639 Scalar & 640 Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) 641 { 642 void *opaque_clang_qual_type = GetClangType(); 643 if (opaque_clang_qual_type) 644 { 645 switch (m_value_type) 646 { 647 case eValueTypeScalar: // raw scalar value 648 break; 649 650 default: 651 case eValueTypeFileAddress: 652 case eValueTypeLoadAddress: // load address value 653 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb) 654 { 655 DataExtractor data; 656 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); 657 Error error (GetValueAsData (exe_ctx, ast_context, data, 0, NULL)); 658 if (error.Success()) 659 { 660 Scalar scalar; 661 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar)) 662 { 663 m_value = scalar; 664 m_value_type = eValueTypeScalar; 665 } 666 else 667 { 668 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 669 { 670 m_value.Clear(); 671 m_value_type = eValueTypeScalar; 672 } 673 } 674 } 675 else 676 { 677 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 678 { 679 m_value.Clear(); 680 m_value_type = eValueTypeScalar; 681 } 682 } 683 } 684 break; 685 } 686 } 687 return m_value; 688 } 689 690 Variable * 691 Value::GetVariable() 692 { 693 if (m_context_type == eContextTypeVariable) 694 return static_cast<Variable *> (m_context); 695 return NULL; 696 } 697 698 const char * 699 Value::GetValueTypeAsCString (ValueType value_type) 700 { 701 switch (value_type) 702 { 703 case eValueTypeScalar: return "scalar"; 704 case eValueTypeVector: return "vector"; 705 case eValueTypeFileAddress: return "file address"; 706 case eValueTypeLoadAddress: return "load address"; 707 case eValueTypeHostAddress: return "host address"; 708 }; 709 return "???"; 710 } 711 712 const char * 713 Value::GetContextTypeAsCString (ContextType context_type) 714 { 715 switch (context_type) 716 { 717 case eContextTypeInvalid: return "invalid"; 718 case eContextTypeClangType: return "clang::Type *"; 719 case eContextTypeRegisterInfo: return "RegisterInfo *"; 720 case eContextTypeLLDBType: return "Type *"; 721 case eContextTypeVariable: return "Variable *"; 722 }; 723 return "???"; 724 } 725 726 ValueList::ValueList (const ValueList &rhs) 727 { 728 m_values = rhs.m_values; 729 } 730 731 const ValueList & 732 ValueList::operator= (const ValueList &rhs) 733 { 734 m_values = rhs.m_values; 735 return *this; 736 } 737 738 void 739 ValueList::PushValue (const Value &value) 740 { 741 m_values.push_back (value); 742 } 743 744 size_t 745 ValueList::GetSize() 746 { 747 return m_values.size(); 748 } 749 750 Value * 751 ValueList::GetValueAtIndex (size_t idx) 752 { 753 if (idx < GetSize()) 754 { 755 return &(m_values[idx]); 756 } 757 else 758 return NULL; 759 } 760 761 void 762 ValueList::Clear () 763 { 764 m_values.clear(); 765 } 766