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 default: 343 error.SetErrorStringWithFormat("invalid value type %i", m_value_type); 344 break; 345 346 case eValueTypeScalar: 347 data.SetByteOrder (lldb::endian::InlHostByteOrder()); 348 if (m_context_type == eContextTypeClangType && ast_context) 349 { 350 ClangASTType ptr_type (ast_context, ClangASTContext::GetVoidPtrType(ast_context, false)); 351 uint64_t ptr_byte_size = ptr_type.GetClangTypeByteSize(); 352 data.SetAddressByteSize (ptr_byte_size); 353 } 354 else 355 data.SetAddressByteSize(sizeof(void *)); 356 if (m_value.GetData (data)) 357 return error; // Success; 358 error.SetErrorStringWithFormat("extracting data from value failed"); 359 break; 360 361 case eValueTypeLoadAddress: 362 if (exe_ctx == NULL) 363 { 364 error.SetErrorString ("can't read load address (no execution context)"); 365 } 366 else 367 { 368 Process *process = exe_ctx->GetProcessPtr(); 369 if (process == NULL || !process->IsAlive()) 370 { 371 Target *target = exe_ctx->GetTargetPtr(); 372 if (target) 373 { 374 // Allow expressions to run and evaluate things when the target 375 // has memory sections loaded. This allows you to use "target modules load" 376 // to load your executable and any shared libraries, then execute 377 // commands where you can look at types in data sections. 378 const SectionLoadList &target_sections = target->GetSectionLoadList(); 379 if (!target_sections.IsEmpty()) 380 { 381 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 382 if (target_sections.ResolveLoadAddress(address, file_so_addr)) 383 { 384 address_type = eAddressTypeLoad; 385 data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 386 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 387 } 388 else 389 address = LLDB_INVALID_ADDRESS; 390 } 391 // else 392 // { 393 // ModuleSP exe_module_sp (target->GetExecutableModule()); 394 // if (exe_module_sp) 395 // { 396 // address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 397 // if (address != LLDB_INVALID_ADDRESS) 398 // { 399 // if (exe_module_sp->ResolveFileAddress(address, file_so_addr)) 400 // { 401 // data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 402 // data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 403 // address_type = eAddressTypeFile; 404 // } 405 // else 406 // { 407 // address = LLDB_INVALID_ADDRESS; 408 // } 409 // } 410 // } 411 // } 412 } 413 else 414 { 415 error.SetErrorString ("can't read load address (invalid process)"); 416 } 417 } 418 else 419 { 420 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 421 address_type = eAddressTypeLoad; 422 data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder()); 423 data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize()); 424 } 425 } 426 break; 427 428 case eValueTypeFileAddress: 429 if (exe_ctx == NULL) 430 { 431 error.SetErrorString ("can't read file address (no execution context)"); 432 } 433 else if (exe_ctx->GetTargetPtr() == NULL) 434 { 435 error.SetErrorString ("can't read file address (invalid target)"); 436 } 437 else 438 { 439 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 440 if (address == LLDB_INVALID_ADDRESS) 441 { 442 error.SetErrorString ("invalid file address"); 443 } 444 else 445 { 446 if (module == NULL) 447 { 448 // The only thing we can currently lock down to a module so that 449 // we can resolve a file address, is a variable. 450 Variable *variable = GetVariable(); 451 if (variable) 452 { 453 SymbolContext var_sc; 454 variable->CalculateSymbolContext(&var_sc); 455 module = var_sc.module_sp.get(); 456 } 457 } 458 459 if (module) 460 { 461 bool resolved = false; 462 ObjectFile *objfile = module->GetObjectFile(); 463 if (objfile) 464 { 465 Address so_addr(address, objfile->GetSectionList()); 466 addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr()); 467 bool process_launched_and_stopped = exe_ctx->GetProcessPtr() 468 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */) 469 : false; 470 // Don't use the load address if the process has exited. 471 if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped) 472 { 473 resolved = true; 474 address = load_address; 475 address_type = eAddressTypeLoad; 476 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder()); 477 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize()); 478 } 479 else 480 { 481 if (so_addr.IsSectionOffset()) 482 { 483 resolved = true; 484 file_so_addr = so_addr; 485 data.SetByteOrder(objfile->GetByteOrder()); 486 data.SetAddressByteSize(objfile->GetAddressByteSize()); 487 } 488 } 489 } 490 if (!resolved) 491 { 492 Variable *variable = GetVariable(); 493 494 if (module) 495 { 496 if (variable) 497 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s", 498 address, 499 variable->GetName().AsCString(""), 500 module->GetFileSpec().GetPath().c_str()); 501 else 502 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s", 503 address, 504 module->GetFileSpec().GetPath().c_str()); 505 } 506 else 507 { 508 if (variable) 509 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'", 510 address, 511 variable->GetName().AsCString("")); 512 else 513 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address); 514 } 515 } 516 } 517 else 518 { 519 // Can't convert a file address to anything valid without more 520 // context (which Module it came from) 521 error.SetErrorString ("can't read memory from file address without more context"); 522 } 523 } 524 } 525 break; 526 527 case eValueTypeHostAddress: 528 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 529 address_type = eAddressTypeHost; 530 if (exe_ctx) 531 { 532 Target *target = exe_ctx->GetTargetPtr(); 533 if (target) 534 { 535 data.SetByteOrder(target->GetArchitecture().GetByteOrder()); 536 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); 537 break; 538 } 539 } 540 // fallback to host settings 541 data.SetByteOrder(lldb::endian::InlHostByteOrder()); 542 data.SetAddressByteSize(sizeof(void *)); 543 break; 544 } 545 546 // Bail if we encountered any errors 547 if (error.Fail()) 548 return error; 549 550 if (address == LLDB_INVALID_ADDRESS) 551 { 552 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load"); 553 return error; 554 } 555 556 // If we got here, we need to read the value from memory 557 size_t byte_size = GetValueByteSize (ast_context, &error); 558 559 // Bail if we encountered any errors getting the byte size 560 if (error.Fail()) 561 return error; 562 563 // Make sure we have enough room within "data", and if we don't make 564 // something large enough that does 565 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size)) 566 { 567 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0')); 568 data.SetData(data_sp); 569 } 570 571 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size)); 572 if (dst != NULL) 573 { 574 if (address_type == eAddressTypeHost) 575 { 576 // The address is an address in this process, so just copy it 577 memcpy (dst, (uint8_t*)NULL + address, byte_size); 578 } 579 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile)) 580 { 581 if (file_so_addr.IsValid()) 582 { 583 // We have a file address that we were able to translate into a 584 // section offset address so we might be able to read this from 585 // the object files if we don't have a live process. Lets always 586 // try and read from the process if we have one though since we 587 // want to read the actual value by setting "prefer_file_cache" 588 // to false. 589 const bool prefer_file_cache = false; 590 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size) 591 { 592 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address); 593 } 594 } 595 else 596 { 597 // The execution context might have a NULL process, but it 598 // might have a valid process in the exe_ctx->target, so use 599 // the ExecutionContext::GetProcess accessor to ensure we 600 // get the process if there is one. 601 Process *process = exe_ctx->GetProcessPtr(); 602 603 if (process) 604 { 605 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error); 606 if (bytes_read != byte_size) 607 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)", 608 (uint64_t)address, 609 (uint32_t)bytes_read, 610 (uint32_t)byte_size); 611 } 612 else 613 { 614 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address); 615 } 616 } 617 } 618 else 619 { 620 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type); 621 } 622 } 623 else 624 { 625 error.SetErrorStringWithFormat ("out of memory"); 626 } 627 628 return error; 629 } 630 631 Scalar & 632 Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) 633 { 634 void *opaque_clang_qual_type = GetClangType(); 635 if (opaque_clang_qual_type) 636 { 637 switch (m_value_type) 638 { 639 case eValueTypeScalar: // raw scalar value 640 break; 641 642 default: 643 case eValueTypeFileAddress: 644 case eValueTypeLoadAddress: // load address value 645 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb) 646 { 647 DataExtractor data; 648 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); 649 Error error (GetValueAsData (exe_ctx, ast_context, data, 0, NULL)); 650 if (error.Success()) 651 { 652 Scalar scalar; 653 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar)) 654 { 655 m_value = scalar; 656 m_value_type = eValueTypeScalar; 657 } 658 else 659 { 660 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 661 { 662 m_value.Clear(); 663 m_value_type = eValueTypeScalar; 664 } 665 } 666 } 667 else 668 { 669 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 670 { 671 m_value.Clear(); 672 m_value_type = eValueTypeScalar; 673 } 674 } 675 } 676 break; 677 } 678 } 679 return m_value; 680 } 681 682 Variable * 683 Value::GetVariable() 684 { 685 if (m_context_type == eContextTypeVariable) 686 return static_cast<Variable *> (m_context); 687 return NULL; 688 } 689 690 const char * 691 Value::GetValueTypeAsCString (ValueType value_type) 692 { 693 switch (value_type) 694 { 695 case eValueTypeScalar: return "scalar"; 696 case eValueTypeVector: return "vector"; 697 case eValueTypeFileAddress: return "file address"; 698 case eValueTypeLoadAddress: return "load address"; 699 case eValueTypeHostAddress: return "host address"; 700 }; 701 return "???"; 702 } 703 704 const char * 705 Value::GetContextTypeAsCString (ContextType context_type) 706 { 707 switch (context_type) 708 { 709 case eContextTypeInvalid: return "invalid"; 710 case eContextTypeClangType: return "clang::Type *"; 711 case eContextTypeRegisterInfo: return "RegisterInfo *"; 712 case eContextTypeLLDBType: return "Type *"; 713 case eContextTypeVariable: return "Variable *"; 714 }; 715 return "???"; 716 } 717 718 ValueList::ValueList (const ValueList &rhs) 719 { 720 m_values = rhs.m_values; 721 } 722 723 const ValueList & 724 ValueList::operator= (const ValueList &rhs) 725 { 726 m_values = rhs.m_values; 727 return *this; 728 } 729 730 void 731 ValueList::PushValue (const Value &value) 732 { 733 m_values.push_back (value); 734 } 735 736 size_t 737 ValueList::GetSize() 738 { 739 return m_values.size(); 740 } 741 742 Value * 743 ValueList::GetValueAtIndex (size_t idx) 744 { 745 if (idx < GetSize()) 746 { 747 return &(m_values[idx]); 748 } 749 else 750 return NULL; 751 } 752 753 void 754 ValueList::Clear () 755 { 756 m_values.clear(); 757 } 758