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/Stream.h" 20 #include "lldb/Symbol/ClangASTType.h" 21 #include "lldb/Symbol/ClangASTContext.h" 22 #include "lldb/Symbol/ObjectFile.h" 23 #include "lldb/Symbol/SymbolContext.h" 24 #include "lldb/Symbol/Type.h" 25 #include "lldb/Symbol/Variable.h" 26 #include "lldb/Target/ExecutionContext.h" 27 #include "lldb/Target/Process.h" 28 #include "lldb/Target/Target.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 Value::Value() : 34 m_value (), 35 m_value_type (eValueTypeScalar), 36 m_context (NULL), 37 m_context_type (eContextTypeInvalid), 38 m_data_buffer () 39 { 40 } 41 42 Value::Value(const Scalar& scalar) : 43 m_value (scalar), 44 m_value_type (eValueTypeScalar), 45 m_context (NULL), 46 m_context_type (eContextTypeInvalid), 47 m_data_buffer () 48 { 49 } 50 51 52 Value::Value(const uint8_t *bytes, int len) : 53 m_value (), 54 m_value_type (eValueTypeHostAddress), 55 m_context (NULL), 56 m_context_type (eContextTypeInvalid), 57 m_data_buffer () 58 { 59 m_data_buffer.CopyData(bytes, len); 60 m_value = (uintptr_t)m_data_buffer.GetBytes(); 61 } 62 63 Value::Value(const Value &v) : 64 m_value(v.m_value), 65 m_value_type(v.m_value_type), 66 m_context(v.m_context), 67 m_context_type(v.m_context_type) 68 { 69 if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes()) 70 { 71 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(), 72 v.m_data_buffer.GetByteSize()); 73 74 m_value = (uintptr_t)m_data_buffer.GetBytes(); 75 } 76 } 77 78 Value & 79 Value::operator=(const Value &rhs) 80 { 81 if (this != &rhs) 82 { 83 m_value = rhs.m_value; 84 m_value_type = rhs.m_value_type; 85 m_context = rhs.m_context; 86 m_context_type = rhs.m_context_type; 87 if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes()) 88 { 89 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(), 90 rhs.m_data_buffer.GetByteSize()); 91 92 m_value = (uintptr_t)m_data_buffer.GetBytes(); 93 } 94 } 95 return *this; 96 } 97 98 void 99 Value::Dump (Stream* strm) 100 { 101 m_value.GetValue (strm, true); 102 strm->Printf(", value_type = %s, context = %p, context_type = %s", 103 Value::GetValueTypeAsCString(m_value_type), 104 m_context, 105 Value::GetContextTypeAsCString(m_context_type)); 106 } 107 108 Value::ValueType 109 Value::GetValueType() const 110 { 111 return m_value_type; 112 } 113 114 AddressType 115 Value::GetValueAddressType () const 116 { 117 switch (m_value_type) 118 { 119 default: 120 case eValueTypeScalar: 121 break; 122 case eValueTypeLoadAddress: return eAddressTypeLoad; 123 case eValueTypeFileAddress: return eAddressTypeFile; 124 case eValueTypeHostAddress: return eAddressTypeHost; 125 } 126 return eAddressTypeInvalid; 127 } 128 129 RegisterInfo * 130 Value::GetRegisterInfo() 131 { 132 if (m_context_type == eContextTypeRegisterInfo) 133 return static_cast<RegisterInfo *> (m_context); 134 return NULL; 135 } 136 137 Type * 138 Value::GetType() 139 { 140 if (m_context_type == eContextTypeLLDBType) 141 return static_cast<Type *> (m_context); 142 return NULL; 143 } 144 145 void 146 Value::ResizeData(int len) 147 { 148 m_value_type = eValueTypeHostAddress; 149 m_data_buffer.SetByteSize(len); 150 m_value = (uintptr_t)m_data_buffer.GetBytes(); 151 } 152 153 bool 154 Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) 155 { 156 switch (m_context_type) 157 { 158 default: 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 default: 180 case eContextTypeInvalid: 181 // If we have no context, there is no way to know how much memory to read 182 if (error_ptr) 183 error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read."); 184 break; 185 186 case eContextTypeClangType: 187 if (ast_context == NULL) 188 { 189 if (error_ptr) 190 error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *."); 191 } 192 else 193 { 194 uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context); 195 byte_size = (bit_width + 7 ) / 8; 196 } 197 break; 198 199 case eContextTypeRegisterInfo: // RegisterInfo * 200 if (GetRegisterInfo()) 201 byte_size = GetRegisterInfo()->byte_size; 202 else if (error_ptr) 203 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *."); 204 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 if (load_address != LLDB_INVALID_ADDRESS) 429 { 430 resolved = true; 431 address = load_address; 432 address_type = eAddressTypeLoad; 433 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder()); 434 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize()); 435 } 436 else 437 { 438 if (so_addr.IsSectionOffset()) 439 { 440 resolved = true; 441 file_so_addr = so_addr; 442 data.SetByteOrder(objfile->GetByteOrder()); 443 data.SetAddressByteSize(objfile->GetAddressByteSize()); 444 } 445 } 446 } 447 if (!resolved) 448 { 449 Variable *variable = GetVariable(); 450 451 if (module) 452 { 453 if (variable) 454 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s' in %s%s%s", 455 address, 456 variable->GetName().AsCString(""), 457 module->GetFileSpec().GetDirectory().GetCString(), 458 module->GetFileSpec().GetDirectory() ? "/" : "", 459 module->GetFileSpec().GetFilename().GetCString()); 460 else 461 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx in %s%s%s", 462 address, 463 module->GetFileSpec().GetDirectory().GetCString(), 464 module->GetFileSpec().GetDirectory() ? "/" : "", 465 module->GetFileSpec().GetFilename().GetCString()); 466 } 467 else 468 { 469 if (variable) 470 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s'", 471 address, 472 variable->GetName().AsCString("")); 473 else 474 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx", address); 475 } 476 } 477 } 478 else 479 { 480 // Can't convert a file address to anything valid without more 481 // context (which Module it came from) 482 error.SetErrorString ("can't read memory from file address without more context"); 483 } 484 } 485 } 486 break; 487 488 case eValueTypeHostAddress: 489 address = m_value.ULongLong(LLDB_INVALID_ADDRESS); 490 data.SetByteOrder(lldb::endian::InlHostByteOrder()); 491 data.SetAddressByteSize(sizeof(void *)); 492 address_type = eAddressTypeHost; 493 break; 494 } 495 496 // Bail if we encountered any errors 497 if (error.Fail()) 498 return error; 499 500 if (address == LLDB_INVALID_ADDRESS) 501 { 502 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load"); 503 return error; 504 } 505 506 // If we got here, we need to read the value from memory 507 uint32_t byte_size = GetValueByteSize (ast_context, &error); 508 509 // Bail if we encountered any errors getting the byte size 510 if (error.Fail()) 511 return error; 512 513 // Make sure we have enough room within "data", and if we don't make 514 // something large enough that does 515 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size)) 516 { 517 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0')); 518 data.SetData(data_sp); 519 } 520 521 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size)); 522 if (dst != NULL) 523 { 524 if (address_type == eAddressTypeHost) 525 { 526 // The address is an address in this process, so just copy it 527 memcpy (dst, (uint8_t*)NULL + address, byte_size); 528 } 529 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile)) 530 { 531 if (file_so_addr.IsValid()) 532 { 533 // We have a file address that we were able to translate into a 534 // section offset address so we might be able to read this from 535 // the object files if we don't have a live process. Lets always 536 // try and read from the process if we have one though since we 537 // want to read the actual value by setting "prefer_file_cache" 538 // to false. 539 const bool prefer_file_cache = false; 540 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size) 541 { 542 error.SetErrorStringWithFormat("read memory from 0x%llx failed", (uint64_t)address); 543 } 544 } 545 else 546 { 547 // The execution context might have a NULL process, but it 548 // might have a valid process in the exe_ctx->target, so use 549 // the ExecutionContext::GetProcess accessor to ensure we 550 // get the process if there is one. 551 Process *process = exe_ctx->GetProcessPtr(); 552 553 if (process) 554 { 555 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error); 556 if (bytes_read != byte_size) 557 error.SetErrorStringWithFormat("read memory from 0x%llx failed (%u of %u bytes read)", 558 (uint64_t)address, 559 (uint32_t)bytes_read, 560 (uint32_t)byte_size); 561 } 562 else 563 { 564 error.SetErrorStringWithFormat("read memory from 0x%llx failed (invalid process)", (uint64_t)address); 565 } 566 } 567 } 568 else 569 { 570 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type); 571 } 572 } 573 else 574 { 575 error.SetErrorStringWithFormat ("out of memory"); 576 } 577 578 return error; 579 } 580 581 Scalar & 582 Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context) 583 { 584 void *opaque_clang_qual_type = GetClangType(); 585 if (opaque_clang_qual_type) 586 { 587 switch (m_value_type) 588 { 589 case eValueTypeScalar: // raw scalar value 590 break; 591 592 default: 593 case eValueTypeFileAddress: 594 m_value.Clear(); 595 break; 596 597 case eValueTypeLoadAddress: // load address value 598 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb) 599 { 600 AddressType address_type = m_value_type == eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost; 601 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); 602 DataExtractor data; 603 if (ClangASTType::ReadFromMemory (ast_context, opaque_clang_qual_type, exe_ctx, addr, address_type, data)) 604 { 605 Scalar scalar; 606 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar)) 607 { 608 m_value = scalar; 609 m_value_type = eValueTypeScalar; 610 } 611 else 612 { 613 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 614 { 615 m_value.Clear(); 616 m_value_type = eValueTypeScalar; 617 } 618 } 619 } 620 else 621 { 622 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) 623 { 624 m_value.Clear(); 625 m_value_type = eValueTypeScalar; 626 } 627 } 628 } 629 break; 630 } 631 } 632 return m_value; 633 } 634 635 Variable * 636 Value::GetVariable() 637 { 638 if (m_context_type == eContextTypeVariable) 639 return static_cast<Variable *> (m_context); 640 return NULL; 641 } 642 643 const char * 644 Value::GetValueTypeAsCString (ValueType value_type) 645 { 646 switch (value_type) 647 { 648 case eValueTypeScalar: return "scalar"; 649 case eValueTypeFileAddress: return "file address"; 650 case eValueTypeLoadAddress: return "load address"; 651 case eValueTypeHostAddress: return "host address"; 652 }; 653 return "???"; 654 } 655 656 const char * 657 Value::GetContextTypeAsCString (ContextType context_type) 658 { 659 switch (context_type) 660 { 661 case eContextTypeInvalid: return "invalid"; 662 case eContextTypeClangType: return "clang::Type *"; 663 case eContextTypeRegisterInfo: return "RegisterInfo *"; 664 case eContextTypeLLDBType: return "Type *"; 665 case eContextTypeVariable: return "Variable *"; 666 }; 667 return "???"; 668 } 669 670 ValueList::ValueList (const ValueList &rhs) 671 { 672 m_values = rhs.m_values; 673 } 674 675 const ValueList & 676 ValueList::operator= (const ValueList &rhs) 677 { 678 m_values = rhs.m_values; 679 return *this; 680 } 681 682 void 683 ValueList::PushValue (const Value &value) 684 { 685 m_values.push_back (value); 686 } 687 688 size_t 689 ValueList::GetSize() 690 { 691 return m_values.size(); 692 } 693 694 Value * 695 ValueList::GetValueAtIndex (size_t idx) 696 { 697 if (idx < GetSize()) 698 { 699 return &(m_values[idx]); 700 } 701 else 702 return NULL; 703 } 704 705 void 706 ValueList::Clear () 707 { 708 m_values.clear(); 709 } 710