1//===-- DataExtractor.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 <assert.h> 11#include <stddef.h> 12 13#include <bitset> 14#include <limits> 15#include <sstream> 16#include <string> 17#include <math.h> 18 19#include "clang/AST/ASTContext.h" 20 21#include "llvm/ADT/APFloat.h" 22#include "llvm/ADT/APInt.h" 23#include "llvm/ADT/ArrayRef.h" 24#include "llvm/ADT/SmallVector.h" 25#include "llvm/Support/MathExtras.h" 26#include "llvm/Support/MD5.h" 27 28#include "lldb/Core/DataBufferHeap.h" 29#include "lldb/Core/DataExtractor.h" 30#include "lldb/Core/DataBuffer.h" 31#include "lldb/Core/Disassembler.h" 32#include "lldb/Core/Log.h" 33#include "lldb/Core/Stream.h" 34#include "lldb/Core/StreamString.h" 35#include "lldb/Core/UUID.h" 36#include "lldb/Core/dwarf.h" 37#include "lldb/Host/Endian.h" 38#include "lldb/Symbol/ClangASTContext.h" 39#include "lldb/Target/ExecutionContext.h" 40#include "lldb/Target/ExecutionContextScope.h" 41#include "lldb/Target/SectionLoadList.h" 42#include "lldb/Target/Target.h" 43 44using namespace lldb; 45using namespace lldb_private; 46 47static inline uint16_t 48ReadInt16(const unsigned char* ptr, offset_t offset) 49{ 50 uint16_t value; 51 memcpy (&value, ptr + offset, 2); 52 return value; 53} 54 55static inline uint32_t 56ReadInt32 (const unsigned char* ptr, offset_t offset = 0) 57{ 58 uint32_t value; 59 memcpy (&value, ptr + offset, 4); 60 return value; 61} 62 63static inline uint64_t 64ReadInt64(const unsigned char* ptr, offset_t offset = 0) 65{ 66 uint64_t value; 67 memcpy (&value, ptr + offset, 8); 68 return value; 69} 70 71static inline uint16_t 72ReadInt16(const void* ptr) 73{ 74 uint16_t value; 75 memcpy (&value, ptr, 2); 76 return value; 77} 78 79static inline uint16_t 80ReadSwapInt16(const unsigned char* ptr, offset_t offset) 81{ 82 uint16_t value; 83 memcpy (&value, ptr + offset, 2); 84 return llvm::ByteSwap_16(value); 85} 86 87static inline uint32_t 88ReadSwapInt32 (const unsigned char* ptr, offset_t offset) 89{ 90 uint32_t value; 91 memcpy (&value, ptr + offset, 4); 92 return llvm::ByteSwap_32(value); 93} 94 95static inline uint64_t 96ReadSwapInt64(const unsigned char* ptr, offset_t offset) 97{ 98 uint64_t value; 99 memcpy (&value, ptr + offset, 8); 100 return llvm::ByteSwap_64(value); 101} 102 103static inline uint16_t 104ReadSwapInt16(const void* ptr) 105{ 106 uint16_t value; 107 memcpy (&value, ptr, 2); 108 return llvm::ByteSwap_16(value); 109} 110 111static inline uint32_t 112ReadSwapInt32 (const void* ptr) 113{ 114 uint32_t value; 115 memcpy (&value, ptr, 4); 116 return llvm::ByteSwap_32(value); 117} 118 119static inline uint64_t 120ReadSwapInt64(const void* ptr) 121{ 122 uint64_t value; 123 memcpy (&value, ptr, 8); 124 return llvm::ByteSwap_64(value); 125} 126 127#define NON_PRINTABLE_CHAR '.' 128//---------------------------------------------------------------------- 129// Default constructor. 130//---------------------------------------------------------------------- 131DataExtractor::DataExtractor () : 132 m_start (NULL), 133 m_end (NULL), 134 m_byte_order(endian::InlHostByteOrder()), 135 m_addr_size (sizeof(void *)), 136 m_data_sp (), 137 m_target_byte_size(1) 138{ 139} 140 141//---------------------------------------------------------------------- 142// This constructor allows us to use data that is owned by someone else. 143// The data must stay around as long as this object is valid. 144//---------------------------------------------------------------------- 145DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size/*=1*/) : 146 m_start (const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data))), 147 m_end (const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data)) + length), 148 m_byte_order(endian), 149 m_addr_size (addr_size), 150 m_data_sp (), 151 m_target_byte_size(target_byte_size) 152{ 153#ifdef LLDB_CONFIGURATION_DEBUG 154 assert (addr_size == 4 || addr_size == 8); 155#endif 156} 157 158//---------------------------------------------------------------------- 159// Make a shared pointer reference to the shared data in "data_sp" and 160// set the endian swapping setting to "swap", and the address size to 161// "addr_size". The shared data reference will ensure the data lives 162// as long as any DataExtractor objects exist that have a reference to 163// this data. 164//---------------------------------------------------------------------- 165DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size/*=1*/) : 166 m_start (NULL), 167 m_end (NULL), 168 m_byte_order(endian), 169 m_addr_size (addr_size), 170 m_data_sp (), 171 m_target_byte_size(target_byte_size) 172{ 173#ifdef LLDB_CONFIGURATION_DEBUG 174 assert (addr_size == 4 || addr_size == 8); 175#endif 176 SetData (data_sp); 177} 178 179//---------------------------------------------------------------------- 180// Initialize this object with a subset of the data bytes in "data". 181// If "data" contains shared data, then a reference to this shared 182// data will added and the shared data will stay around as long 183// as any object contains a reference to that data. The endian 184// swap and address size settings are copied from "data". 185//---------------------------------------------------------------------- 186DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length, uint32_t target_byte_size/*=1*/) : 187 m_start(NULL), 188 m_end(NULL), 189 m_byte_order(data.m_byte_order), 190 m_addr_size(data.m_addr_size), 191 m_data_sp(), 192 m_target_byte_size(target_byte_size) 193{ 194#ifdef LLDB_CONFIGURATION_DEBUG 195 assert (m_addr_size == 4 || m_addr_size == 8); 196#endif 197 if (data.ValidOffset(offset)) 198 { 199 offset_t bytes_available = data.GetByteSize() - offset; 200 if (length > bytes_available) 201 length = bytes_available; 202 SetData(data, offset, length); 203 } 204} 205 206DataExtractor::DataExtractor (const DataExtractor& rhs) : 207 m_start (rhs.m_start), 208 m_end (rhs.m_end), 209 m_byte_order (rhs.m_byte_order), 210 m_addr_size (rhs.m_addr_size), 211 m_data_sp (rhs.m_data_sp), 212 m_target_byte_size(rhs.m_target_byte_size) 213{ 214#ifdef LLDB_CONFIGURATION_DEBUG 215 assert (m_addr_size == 4 || m_addr_size == 8); 216#endif 217} 218 219//---------------------------------------------------------------------- 220// Assignment operator 221//---------------------------------------------------------------------- 222const DataExtractor& 223DataExtractor::operator= (const DataExtractor& rhs) 224{ 225 if (this != &rhs) 226 { 227 m_start = rhs.m_start; 228 m_end = rhs.m_end; 229 m_byte_order = rhs.m_byte_order; 230 m_addr_size = rhs.m_addr_size; 231 m_data_sp = rhs.m_data_sp; 232 } 233 return *this; 234} 235 236//---------------------------------------------------------------------- 237// Destructor 238//---------------------------------------------------------------------- 239DataExtractor::~DataExtractor () 240{ 241} 242 243//------------------------------------------------------------------ 244// Clears the object contents back to a default invalid state, and 245// release any references to shared data that this object may 246// contain. 247//------------------------------------------------------------------ 248void 249DataExtractor::Clear () 250{ 251 m_start = NULL; 252 m_end = NULL; 253 m_byte_order = endian::InlHostByteOrder(); 254 m_addr_size = sizeof(void *); 255 m_data_sp.reset(); 256} 257 258//------------------------------------------------------------------ 259// If this object contains shared data, this function returns the 260// offset into that shared data. Else zero is returned. 261//------------------------------------------------------------------ 262size_t 263DataExtractor::GetSharedDataOffset () const 264{ 265 if (m_start != NULL) 266 { 267 const DataBuffer * data = m_data_sp.get(); 268 if (data != NULL) 269 { 270 const uint8_t * data_bytes = data->GetBytes(); 271 if (data_bytes != NULL) 272 { 273 assert(m_start >= data_bytes); 274 return m_start - data_bytes; 275 } 276 } 277 } 278 return 0; 279} 280 281//---------------------------------------------------------------------- 282// Set the data with which this object will extract from to data 283// starting at BYTES and set the length of the data to LENGTH bytes 284// long. The data is externally owned must be around at least as 285// long as this object points to the data. No copy of the data is 286// made, this object just refers to this data and can extract from 287// it. If this object refers to any shared data upon entry, the 288// reference to that data will be released. Is SWAP is set to true, 289// any data extracted will be endian swapped. 290//---------------------------------------------------------------------- 291lldb::offset_t 292DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian) 293{ 294 m_byte_order = endian; 295 m_data_sp.reset(); 296 if (bytes == NULL || length == 0) 297 { 298 m_start = NULL; 299 m_end = NULL; 300 } 301 else 302 { 303 m_start = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(bytes)); 304 m_end = m_start + length; 305 } 306 return GetByteSize(); 307} 308 309//---------------------------------------------------------------------- 310// Assign the data for this object to be a subrange in "data" 311// starting "data_offset" bytes into "data" and ending "data_length" 312// bytes later. If "data_offset" is not a valid offset into "data", 313// then this object will contain no bytes. If "data_offset" is 314// within "data" yet "data_length" is too large, the length will be 315// capped at the number of bytes remaining in "data". If "data" 316// contains a shared pointer to other data, then a ref counted 317// pointer to that data will be made in this object. If "data" 318// doesn't contain a shared pointer to data, then the bytes referred 319// to in "data" will need to exist at least as long as this object 320// refers to those bytes. The address size and endian swap settings 321// are copied from the current values in "data". 322//---------------------------------------------------------------------- 323lldb::offset_t 324DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length) 325{ 326 m_addr_size = data.m_addr_size; 327#ifdef LLDB_CONFIGURATION_DEBUG 328 assert (m_addr_size == 4 || m_addr_size == 8); 329#endif 330 // If "data" contains shared pointer to data, then we can use that 331 if (data.m_data_sp.get()) 332 { 333 m_byte_order = data.m_byte_order; 334 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length); 335 } 336 337 // We have a DataExtractor object that just has a pointer to bytes 338 if (data.ValidOffset(data_offset)) 339 { 340 if (data_length > data.GetByteSize() - data_offset) 341 data_length = data.GetByteSize() - data_offset; 342 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder()); 343 } 344 return 0; 345} 346 347//---------------------------------------------------------------------- 348// Assign the data for this object to be a subrange of the shared 349// data in "data_sp" starting "data_offset" bytes into "data_sp" 350// and ending "data_length" bytes later. If "data_offset" is not 351// a valid offset into "data_sp", then this object will contain no 352// bytes. If "data_offset" is within "data_sp" yet "data_length" is 353// too large, the length will be capped at the number of bytes 354// remaining in "data_sp". A ref counted pointer to the data in 355// "data_sp" will be made in this object IF the number of bytes this 356// object refers to in greater than zero (if at least one byte was 357// available starting at "data_offset") to ensure the data stays 358// around as long as it is needed. The address size and endian swap 359// settings will remain unchanged from their current settings. 360//---------------------------------------------------------------------- 361lldb::offset_t 362DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length) 363{ 364 m_start = m_end = NULL; 365 366 if (data_length > 0) 367 { 368 m_data_sp = data_sp; 369 if (data_sp.get()) 370 { 371 const size_t data_size = data_sp->GetByteSize(); 372 if (data_offset < data_size) 373 { 374 m_start = data_sp->GetBytes() + data_offset; 375 const size_t bytes_left = data_size - data_offset; 376 // Cap the length of we asked for too many 377 if (data_length <= bytes_left) 378 m_end = m_start + data_length; // We got all the bytes we wanted 379 else 380 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data 381 } 382 } 383 } 384 385 size_t new_size = GetByteSize(); 386 387 // Don't hold a shared pointer to the data buffer if we don't share 388 // any valid bytes in the shared buffer. 389 if (new_size == 0) 390 m_data_sp.reset(); 391 392 return new_size; 393} 394 395//---------------------------------------------------------------------- 396// Extract a single unsigned char from the binary data and update 397// the offset pointed to by "offset_ptr". 398// 399// RETURNS the byte that was extracted, or zero on failure. 400//---------------------------------------------------------------------- 401uint8_t 402DataExtractor::GetU8 (offset_t *offset_ptr) const 403{ 404 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1); 405 if (data) 406 return *data; 407 return 0; 408} 409 410//---------------------------------------------------------------------- 411// Extract "count" unsigned chars from the binary data and update the 412// offset pointed to by "offset_ptr". The extracted data is copied into 413// "dst". 414// 415// RETURNS the non-NULL buffer pointer upon successful extraction of 416// all the requested bytes, or NULL when the data is not available in 417// the buffer due to being out of bounds, or insufficient data. 418//---------------------------------------------------------------------- 419void * 420DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const 421{ 422 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count); 423 if (data) 424 { 425 // Copy the data into the buffer 426 memcpy (dst, data, count); 427 // Return a non-NULL pointer to the converted data as an indicator of success 428 return dst; 429 } 430 return NULL; 431} 432 433//---------------------------------------------------------------------- 434// Extract a single uint16_t from the data and update the offset 435// pointed to by "offset_ptr". 436// 437// RETURNS the uint16_t that was extracted, or zero on failure. 438//---------------------------------------------------------------------- 439uint16_t 440DataExtractor::GetU16 (offset_t *offset_ptr) const 441{ 442 uint16_t val = 0; 443 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val)); 444 if (data) 445 { 446 if (m_byte_order != endian::InlHostByteOrder()) 447 val = ReadSwapInt16(data); 448 else 449 val = ReadInt16 (data); 450 } 451 return val; 452} 453 454uint16_t 455DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const 456{ 457 uint16_t val; 458 if (m_byte_order == endian::InlHostByteOrder()) 459 val = ReadInt16 (m_start, *offset_ptr); 460 else 461 val = ReadSwapInt16(m_start, *offset_ptr); 462 *offset_ptr += sizeof(val); 463 return val; 464} 465 466uint32_t 467DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const 468{ 469 uint32_t val; 470 if (m_byte_order == endian::InlHostByteOrder()) 471 val = ReadInt32 (m_start, *offset_ptr); 472 else 473 val = ReadSwapInt32 (m_start, *offset_ptr); 474 *offset_ptr += sizeof(val); 475 return val; 476} 477 478uint64_t 479DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const 480{ 481 uint64_t val; 482 if (m_byte_order == endian::InlHostByteOrder()) 483 val = ReadInt64 (m_start, *offset_ptr); 484 else 485 val = ReadSwapInt64 (m_start, *offset_ptr); 486 *offset_ptr += sizeof(val); 487 return val; 488} 489 490 491//---------------------------------------------------------------------- 492// Extract "count" uint16_t values from the binary data and update 493// the offset pointed to by "offset_ptr". The extracted data is 494// copied into "dst". 495// 496// RETURNS the non-NULL buffer pointer upon successful extraction of 497// all the requested bytes, or NULL when the data is not available 498// in the buffer due to being out of bounds, or insufficient data. 499//---------------------------------------------------------------------- 500void * 501DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const 502{ 503 const size_t src_size = sizeof(uint16_t) * count; 504 const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size); 505 if (src) 506 { 507 if (m_byte_order != endian::InlHostByteOrder()) 508 { 509 uint16_t *dst_pos = (uint16_t *)void_dst; 510 uint16_t *dst_end = dst_pos + count; 511 const uint16_t *src_pos = src; 512 while (dst_pos < dst_end) 513 { 514 *dst_pos = ReadSwapInt16 (src_pos); 515 ++dst_pos; 516 ++src_pos; 517 } 518 } 519 else 520 { 521 memcpy (void_dst, src, src_size); 522 } 523 // Return a non-NULL pointer to the converted data as an indicator of success 524 return void_dst; 525 } 526 return NULL; 527} 528 529//---------------------------------------------------------------------- 530// Extract a single uint32_t from the data and update the offset 531// pointed to by "offset_ptr". 532// 533// RETURNS the uint32_t that was extracted, or zero on failure. 534//---------------------------------------------------------------------- 535uint32_t 536DataExtractor::GetU32 (offset_t *offset_ptr) const 537{ 538 uint32_t val = 0; 539 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val)); 540 if (data) 541 { 542 if (m_byte_order != endian::InlHostByteOrder()) 543 { 544 val = ReadSwapInt32 (data); 545 } 546 else 547 { 548 memcpy (&val, data, 4); 549 } 550 } 551 return val; 552} 553 554//---------------------------------------------------------------------- 555// Extract "count" uint32_t values from the binary data and update 556// the offset pointed to by "offset_ptr". The extracted data is 557// copied into "dst". 558// 559// RETURNS the non-NULL buffer pointer upon successful extraction of 560// all the requested bytes, or NULL when the data is not available 561// in the buffer due to being out of bounds, or insufficient data. 562//---------------------------------------------------------------------- 563void * 564DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const 565{ 566 const size_t src_size = sizeof(uint32_t) * count; 567 const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size); 568 if (src) 569 { 570 if (m_byte_order != endian::InlHostByteOrder()) 571 { 572 uint32_t *dst_pos = (uint32_t *)void_dst; 573 uint32_t *dst_end = dst_pos + count; 574 const uint32_t *src_pos = src; 575 while (dst_pos < dst_end) 576 { 577 *dst_pos = ReadSwapInt32 (src_pos); 578 ++dst_pos; 579 ++src_pos; 580 } 581 } 582 else 583 { 584 memcpy (void_dst, src, src_size); 585 } 586 // Return a non-NULL pointer to the converted data as an indicator of success 587 return void_dst; 588 } 589 return NULL; 590} 591 592//---------------------------------------------------------------------- 593// Extract a single uint64_t from the data and update the offset 594// pointed to by "offset_ptr". 595// 596// RETURNS the uint64_t that was extracted, or zero on failure. 597//---------------------------------------------------------------------- 598uint64_t 599DataExtractor::GetU64 (offset_t *offset_ptr) const 600{ 601 uint64_t val = 0; 602 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val)); 603 if (data) 604 { 605 if (m_byte_order != endian::InlHostByteOrder()) 606 { 607 val = ReadSwapInt64 (data); 608 } 609 else 610 { 611 memcpy (&val, data, 8); 612 } 613 } 614 return val; 615} 616 617//---------------------------------------------------------------------- 618// GetU64 619// 620// Get multiple consecutive 64 bit values. Return true if the entire 621// read succeeds and increment the offset pointed to by offset_ptr, else 622// return false and leave the offset pointed to by offset_ptr unchanged. 623//---------------------------------------------------------------------- 624void * 625DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const 626{ 627 const size_t src_size = sizeof(uint64_t) * count; 628 const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size); 629 if (src) 630 { 631 if (m_byte_order != endian::InlHostByteOrder()) 632 { 633 uint64_t *dst_pos = (uint64_t *)void_dst; 634 uint64_t *dst_end = dst_pos + count; 635 const uint64_t *src_pos = src; 636 while (dst_pos < dst_end) 637 { 638 *dst_pos = ReadSwapInt64 (src_pos); 639 ++dst_pos; 640 ++src_pos; 641 } 642 } 643 else 644 { 645 memcpy (void_dst, src, src_size); 646 } 647 // Return a non-NULL pointer to the converted data as an indicator of success 648 return void_dst; 649 } 650 return NULL; 651} 652 653//---------------------------------------------------------------------- 654// Extract a single integer value from the data and update the offset 655// pointed to by "offset_ptr". The size of the extracted integer 656// is specified by the "byte_size" argument. "byte_size" should have 657// a value between 1 and 4 since the return value is only 32 bits 658// wide. Any "byte_size" values less than 1 or greater than 4 will 659// result in nothing being extracted, and zero being returned. 660// 661// RETURNS the integer value that was extracted, or zero on failure. 662//---------------------------------------------------------------------- 663uint32_t 664DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const 665{ 666 switch (byte_size) 667 { 668 case 1: return GetU8 (offset_ptr); break; 669 case 2: return GetU16(offset_ptr); break; 670 case 4: return GetU32(offset_ptr); break; 671 default: 672 assert(false && "GetMaxU32 unhandled case!"); 673 break; 674 } 675 return 0; 676} 677 678//---------------------------------------------------------------------- 679// Extract a single integer value from the data and update the offset 680// pointed to by "offset_ptr". The size of the extracted integer 681// is specified by the "byte_size" argument. "byte_size" should have 682// a value >= 1 and <= 8 since the return value is only 64 bits 683// wide. Any "byte_size" values less than 1 or greater than 8 will 684// result in nothing being extracted, and zero being returned. 685// 686// RETURNS the integer value that was extracted, or zero on failure. 687//---------------------------------------------------------------------- 688uint64_t 689DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const 690{ 691 switch (size) 692 { 693 case 1: return GetU8 (offset_ptr); break; 694 case 2: return GetU16(offset_ptr); break; 695 case 4: return GetU32(offset_ptr); break; 696 case 8: return GetU64(offset_ptr); break; 697 default: 698 assert(false && "GetMax64 unhandled case!"); 699 break; 700 } 701 return 0; 702} 703 704uint64_t 705DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const 706{ 707 switch (size) 708 { 709 case 1: return GetU8_unchecked (offset_ptr); break; 710 case 2: return GetU16_unchecked (offset_ptr); break; 711 case 4: return GetU32_unchecked (offset_ptr); break; 712 case 8: return GetU64_unchecked (offset_ptr); break; 713 default: 714 assert(false && "GetMax64 unhandled case!"); 715 break; 716 } 717 return 0; 718} 719 720int64_t 721DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const 722{ 723 switch (size) 724 { 725 case 1: return (int8_t)GetU8 (offset_ptr); break; 726 case 2: return (int16_t)GetU16(offset_ptr); break; 727 case 4: return (int32_t)GetU32(offset_ptr); break; 728 case 8: return (int64_t)GetU64(offset_ptr); break; 729 default: 730 assert(false && "GetMax64 unhandled case!"); 731 break; 732 } 733 return 0; 734} 735 736uint64_t 737DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const 738{ 739 uint64_t uval64 = GetMaxU64 (offset_ptr, size); 740 if (bitfield_bit_size > 0) 741 { 742 if (bitfield_bit_offset > 0) 743 uval64 >>= bitfield_bit_offset; 744 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1); 745 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64) 746 return uval64; 747 uval64 &= bitfield_mask; 748 } 749 return uval64; 750} 751 752int64_t 753DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const 754{ 755 int64_t sval64 = GetMaxS64 (offset_ptr, size); 756 if (bitfield_bit_size > 0) 757 { 758 if (bitfield_bit_offset > 0) 759 sval64 >>= bitfield_bit_offset; 760 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1; 761 sval64 &= bitfield_mask; 762 // sign extend if needed 763 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1))) 764 sval64 |= ~bitfield_mask; 765 } 766 return sval64; 767} 768 769 770float 771DataExtractor::GetFloat (offset_t *offset_ptr) const 772{ 773 typedef float float_type; 774 float_type val = 0.0; 775 const size_t src_size = sizeof(float_type); 776 const float_type *src = (const float_type *)GetData (offset_ptr, src_size); 777 if (src) 778 { 779 if (m_byte_order != endian::InlHostByteOrder()) 780 { 781 const uint8_t *src_data = (const uint8_t *)src; 782 uint8_t *dst_data = (uint8_t *)&val; 783 for (size_t i=0; i<sizeof(float_type); ++i) 784 dst_data[sizeof(float_type) - 1 - i] = src_data[i]; 785 } 786 else 787 { 788 val = *src; 789 } 790 } 791 return val; 792} 793 794double 795DataExtractor::GetDouble (offset_t *offset_ptr) const 796{ 797 typedef double float_type; 798 float_type val = 0.0; 799 const size_t src_size = sizeof(float_type); 800 const float_type *src = (const float_type *)GetData (offset_ptr, src_size); 801 if (src) 802 { 803 if (m_byte_order != endian::InlHostByteOrder()) 804 { 805 const uint8_t *src_data = (const uint8_t *)src; 806 uint8_t *dst_data = (uint8_t *)&val; 807 for (size_t i=0; i<sizeof(float_type); ++i) 808 dst_data[sizeof(float_type) - 1 - i] = src_data[i]; 809 } 810 else 811 { 812 val = *src; 813 } 814 } 815 return val; 816} 817 818 819long double 820DataExtractor::GetLongDouble (offset_t *offset_ptr) const 821{ 822 long double val = 0.0; 823#if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64) 824 *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), endian::InlHostByteOrder()); 825#else 826 *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), endian::InlHostByteOrder()); 827#endif 828 return val; 829} 830 831 832//------------------------------------------------------------------ 833// Extract a single address from the data and update the offset 834// pointed to by "offset_ptr". The size of the extracted address 835// comes from the "this->m_addr_size" member variable and should be 836// set correctly prior to extracting any address values. 837// 838// RETURNS the address that was extracted, or zero on failure. 839//------------------------------------------------------------------ 840uint64_t 841DataExtractor::GetAddress (offset_t *offset_ptr) const 842{ 843#ifdef LLDB_CONFIGURATION_DEBUG 844 assert (m_addr_size == 4 || m_addr_size == 8); 845#endif 846 return GetMaxU64 (offset_ptr, m_addr_size); 847} 848 849uint64_t 850DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const 851{ 852#ifdef LLDB_CONFIGURATION_DEBUG 853 assert (m_addr_size == 4 || m_addr_size == 8); 854#endif 855 return GetMaxU64_unchecked (offset_ptr, m_addr_size); 856} 857 858//------------------------------------------------------------------ 859// Extract a single pointer from the data and update the offset 860// pointed to by "offset_ptr". The size of the extracted pointer 861// comes from the "this->m_addr_size" member variable and should be 862// set correctly prior to extracting any pointer values. 863// 864// RETURNS the pointer that was extracted, or zero on failure. 865//------------------------------------------------------------------ 866uint64_t 867DataExtractor::GetPointer (offset_t *offset_ptr) const 868{ 869#ifdef LLDB_CONFIGURATION_DEBUG 870 assert (m_addr_size == 4 || m_addr_size == 8); 871#endif 872 return GetMaxU64 (offset_ptr, m_addr_size); 873} 874 875//---------------------------------------------------------------------- 876// GetDwarfEHPtr 877// 878// Used for calls when the value type is specified by a DWARF EH Frame 879// pointer encoding. 880//---------------------------------------------------------------------- 881 882uint64_t 883DataExtractor::GetGNUEHPointer (offset_t *offset_ptr, uint32_t eh_ptr_enc, lldb::addr_t pc_rel_addr, lldb::addr_t text_addr, lldb::addr_t data_addr)//, BSDRelocs *data_relocs) const 884{ 885 if (eh_ptr_enc == DW_EH_PE_omit) 886 return ULLONG_MAX; // Value isn't in the buffer... 887 888 uint64_t baseAddress = 0; 889 uint64_t addressValue = 0; 890 const uint32_t addr_size = GetAddressByteSize(); 891#ifdef LLDB_CONFIGURATION_DEBUG 892 assert (addr_size == 4 || addr_size == 8); 893#endif 894 895 bool signExtendValue = false; 896 // Decode the base part or adjust our offset 897 switch (eh_ptr_enc & 0x70) 898 { 899 case DW_EH_PE_pcrel: 900 signExtendValue = true; 901 baseAddress = *offset_ptr; 902 if (pc_rel_addr != LLDB_INVALID_ADDRESS) 903 baseAddress += pc_rel_addr; 904// else 905// Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address."); 906 break; 907 908 case DW_EH_PE_textrel: 909 signExtendValue = true; 910 if (text_addr != LLDB_INVALID_ADDRESS) 911 baseAddress = text_addr; 912// else 913// Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero."); 914 break; 915 916 case DW_EH_PE_datarel: 917 signExtendValue = true; 918 if (data_addr != LLDB_INVALID_ADDRESS) 919 baseAddress = data_addr; 920// else 921// Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero."); 922 break; 923 924 case DW_EH_PE_funcrel: 925 signExtendValue = true; 926 break; 927 928 case DW_EH_PE_aligned: 929 { 930 // SetPointerSize should be called prior to extracting these so the 931 // pointer size is cached 932 assert(addr_size != 0); 933 if (addr_size) 934 { 935 // Align to a address size boundary first 936 uint32_t alignOffset = *offset_ptr % addr_size; 937 if (alignOffset) 938 offset_ptr += addr_size - alignOffset; 939 } 940 } 941 break; 942 943 default: 944 break; 945 } 946 947 // Decode the value part 948 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) 949 { 950 case DW_EH_PE_absptr : 951 { 952 addressValue = GetAddress (offset_ptr); 953// if (data_relocs) 954// addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue); 955 } 956 break; 957 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break; 958 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break; 959 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break; 960 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break; 961 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break; 962 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break; 963 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break; 964 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break; 965 default: 966 // Unhandled encoding type 967 assert(eh_ptr_enc); 968 break; 969 } 970 971 // Since we promote everything to 64 bit, we may need to sign extend 972 if (signExtendValue && addr_size < sizeof(baseAddress)) 973 { 974 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull); 975 if (sign_bit & addressValue) 976 { 977 uint64_t mask = ~sign_bit + 1; 978 addressValue |= mask; 979 } 980 } 981 return baseAddress + addressValue; 982} 983 984size_t 985DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const 986{ 987 const uint8_t *src = PeekData (offset, length); 988 if (src) 989 { 990 if (dst_byte_order != GetByteOrder()) 991 { 992 // Validate that only a word- or register-sized dst is byte swapped 993 assert (length == 1 || length == 2 || length == 4 || length == 8 || 994 length == 10 || length == 16 || length == 32); 995 996 for (uint32_t i=0; i<length; ++i) 997 ((uint8_t*)dst)[i] = src[length - i - 1]; 998 } 999 else 1000 ::memcpy (dst, src, length); 1001 return length; 1002 } 1003 return 0; 1004} 1005 1006// Extract data as it exists in target memory 1007lldb::offset_t 1008DataExtractor::CopyData (offset_t offset, 1009 offset_t length, 1010 void *dst) const 1011{ 1012 const uint8_t *src = PeekData (offset, length); 1013 if (src) 1014 { 1015 ::memcpy (dst, src, length); 1016 return length; 1017 } 1018 return 0; 1019} 1020 1021// Extract data and swap if needed when doing the copy 1022lldb::offset_t 1023DataExtractor::CopyByteOrderedData (offset_t src_offset, 1024 offset_t src_len, 1025 void *dst_void_ptr, 1026 offset_t dst_len, 1027 ByteOrder dst_byte_order) const 1028{ 1029 // Validate the source info 1030 if (!ValidOffsetForDataOfSize(src_offset, src_len)) 1031 assert (ValidOffsetForDataOfSize(src_offset, src_len)); 1032 assert (src_len > 0); 1033 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle); 1034 1035 // Validate the destination info 1036 assert (dst_void_ptr != NULL); 1037 assert (dst_len > 0); 1038 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle); 1039 1040 // Validate that only a word- or register-sized dst is byte swapped 1041 assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 || 1042 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 || 1043 dst_len == 32); 1044 1045 // Must have valid byte orders set in this object and for destination 1046 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) || 1047 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle)) 1048 return 0; 1049 1050 uint32_t i; 1051 uint8_t* dst = (uint8_t*)dst_void_ptr; 1052 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len); 1053 if (src) 1054 { 1055 if (dst_len >= src_len) 1056 { 1057 // We are copying the entire value from src into dst. 1058 // Calculate how many, if any, zeroes we need for the most 1059 // significant bytes if "dst_len" is greater than "src_len"... 1060 const size_t num_zeroes = dst_len - src_len; 1061 if (dst_byte_order == eByteOrderBig) 1062 { 1063 // Big endian, so we lead with zeroes... 1064 if (num_zeroes > 0) 1065 ::memset (dst, 0, num_zeroes); 1066 // Then either copy or swap the rest 1067 if (m_byte_order == eByteOrderBig) 1068 { 1069 ::memcpy (dst + num_zeroes, src, src_len); 1070 } 1071 else 1072 { 1073 for (i=0; i<src_len; ++i) 1074 dst[i+num_zeroes] = src[src_len - 1 - i]; 1075 } 1076 } 1077 else 1078 { 1079 // Little endian destination, so we lead the value bytes 1080 if (m_byte_order == eByteOrderBig) 1081 { 1082 for (i=0; i<src_len; ++i) 1083 dst[i] = src[src_len - 1 - i]; 1084 } 1085 else 1086 { 1087 ::memcpy (dst, src, src_len); 1088 } 1089 // And zero the rest... 1090 if (num_zeroes > 0) 1091 ::memset (dst + src_len, 0, num_zeroes); 1092 } 1093 return src_len; 1094 } 1095 else 1096 { 1097 // We are only copying some of the value from src into dst.. 1098 1099 if (dst_byte_order == eByteOrderBig) 1100 { 1101 // Big endian dst 1102 if (m_byte_order == eByteOrderBig) 1103 { 1104 // Big endian dst, with big endian src 1105 ::memcpy (dst, src + (src_len - dst_len), dst_len); 1106 } 1107 else 1108 { 1109 // Big endian dst, with little endian src 1110 for (i=0; i<dst_len; ++i) 1111 dst[i] = src[dst_len - 1 - i]; 1112 } 1113 } 1114 else 1115 { 1116 // Little endian dst 1117 if (m_byte_order == eByteOrderBig) 1118 { 1119 // Little endian dst, with big endian src 1120 for (i=0; i<dst_len; ++i) 1121 dst[i] = src[src_len - 1 - i]; 1122 } 1123 else 1124 { 1125 // Little endian dst, with big endian src 1126 ::memcpy (dst, src, dst_len); 1127 } 1128 } 1129 return dst_len; 1130 } 1131 1132 } 1133 return 0; 1134} 1135 1136 1137//---------------------------------------------------------------------- 1138// Extracts a variable length NULL terminated C string from 1139// the data at the offset pointed to by "offset_ptr". The 1140// "offset_ptr" will be updated with the offset of the byte that 1141// follows the NULL terminator byte. 1142// 1143// If the offset pointed to by "offset_ptr" is out of bounds, or if 1144// "length" is non-zero and there aren't enough available 1145// bytes, NULL will be returned and "offset_ptr" will not be 1146// updated. 1147//---------------------------------------------------------------------- 1148const char* 1149DataExtractor::GetCStr (offset_t *offset_ptr) const 1150{ 1151 const char *cstr = (const char *)PeekData (*offset_ptr, 1); 1152 if (cstr) 1153 { 1154 const char *cstr_end = cstr; 1155 const char *end = (const char *)m_end; 1156 while (cstr_end < end && *cstr_end) 1157 ++cstr_end; 1158 1159 // Now we are either at the end of the data or we point to the 1160 // NULL C string terminator with cstr_end... 1161 if (*cstr_end == '\0') 1162 { 1163 // Advance the offset with one extra byte for the NULL terminator 1164 *offset_ptr += (cstr_end - cstr + 1); 1165 return cstr; 1166 } 1167 1168 // We reached the end of the data without finding a NULL C string 1169 // terminator. Fall through and return NULL otherwise anyone that 1170 // would have used the result as a C string can wander into 1171 // unknown memory... 1172 } 1173 return NULL; 1174} 1175 1176//---------------------------------------------------------------------- 1177// Extracts a NULL terminated C string from the fixed length field of 1178// length "len" at the offset pointed to by "offset_ptr". 1179// The "offset_ptr" will be updated with the offset of the byte that 1180// follows the fixed length field. 1181// 1182// If the offset pointed to by "offset_ptr" is out of bounds, or if 1183// the offset plus the length of the field is out of bounds, or if the 1184// field does not contain a NULL terminator byte, NULL will be returned 1185// and "offset_ptr" will not be updated. 1186//---------------------------------------------------------------------- 1187const char* 1188DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const 1189{ 1190 const char *cstr = (const char *)PeekData (*offset_ptr, len); 1191 if (cstr) 1192 { 1193 if (memchr (cstr, '\0', len) == NULL) 1194 { 1195 return NULL; 1196 } 1197 *offset_ptr += len; 1198 return cstr; 1199 } 1200 return NULL; 1201} 1202 1203//------------------------------------------------------------------ 1204// Peeks at a string in the contained data. No verification is done 1205// to make sure the entire string lies within the bounds of this 1206// object's data, only "offset" is verified to be a valid offset. 1207// 1208// Returns a valid C string pointer if "offset" is a valid offset in 1209// this object's data, else NULL is returned. 1210//------------------------------------------------------------------ 1211const char * 1212DataExtractor::PeekCStr (offset_t offset) const 1213{ 1214 return (const char *)PeekData (offset, 1); 1215} 1216 1217//---------------------------------------------------------------------- 1218// Extracts an unsigned LEB128 number from this object's data 1219// starting at the offset pointed to by "offset_ptr". The offset 1220// pointed to by "offset_ptr" will be updated with the offset of the 1221// byte following the last extracted byte. 1222// 1223// Returned the extracted integer value. 1224//---------------------------------------------------------------------- 1225uint64_t 1226DataExtractor::GetULEB128 (offset_t *offset_ptr) const 1227{ 1228 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1); 1229 if (src == NULL) 1230 return 0; 1231 1232 const uint8_t *end = m_end; 1233 1234 if (src < end) 1235 { 1236 uint64_t result = *src++; 1237 if (result >= 0x80) 1238 { 1239 result &= 0x7f; 1240 int shift = 7; 1241 while (src < end) 1242 { 1243 uint8_t byte = *src++; 1244 result |= (byte & 0x7f) << shift; 1245 if ((byte & 0x80) == 0) 1246 break; 1247 shift += 7; 1248 } 1249 } 1250 *offset_ptr = src - m_start; 1251 return result; 1252 } 1253 1254 return 0; 1255} 1256 1257//---------------------------------------------------------------------- 1258// Extracts an signed LEB128 number from this object's data 1259// starting at the offset pointed to by "offset_ptr". The offset 1260// pointed to by "offset_ptr" will be updated with the offset of the 1261// byte following the last extracted byte. 1262// 1263// Returned the extracted integer value. 1264//---------------------------------------------------------------------- 1265int64_t 1266DataExtractor::GetSLEB128 (offset_t *offset_ptr) const 1267{ 1268 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1); 1269 if (src == NULL) 1270 return 0; 1271 1272 const uint8_t *end = m_end; 1273 1274 if (src < end) 1275 { 1276 int64_t result = 0; 1277 int shift = 0; 1278 int size = sizeof (int64_t) * 8; 1279 1280 uint8_t byte = 0; 1281 int bytecount = 0; 1282 1283 while (src < end) 1284 { 1285 bytecount++; 1286 byte = *src++; 1287 result |= (byte & 0x7f) << shift; 1288 shift += 7; 1289 if ((byte & 0x80) == 0) 1290 break; 1291 } 1292 1293 // Sign bit of byte is 2nd high order bit (0x40) 1294 if (shift < size && (byte & 0x40)) 1295 result |= - (1 << shift); 1296 1297 *offset_ptr += bytecount; 1298 return result; 1299 } 1300 return 0; 1301} 1302 1303//---------------------------------------------------------------------- 1304// Skips a ULEB128 number (signed or unsigned) from this object's 1305// data starting at the offset pointed to by "offset_ptr". The 1306// offset pointed to by "offset_ptr" will be updated with the offset 1307// of the byte following the last extracted byte. 1308// 1309// Returns the number of bytes consumed during the extraction. 1310//---------------------------------------------------------------------- 1311uint32_t 1312DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const 1313{ 1314 uint32_t bytes_consumed = 0; 1315 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1); 1316 if (src == NULL) 1317 return 0; 1318 1319 const uint8_t *end = m_end; 1320 1321 if (src < end) 1322 { 1323 const uint8_t *src_pos = src; 1324 while ((src_pos < end) && (*src_pos++ & 0x80)) 1325 ++bytes_consumed; 1326 *offset_ptr += src_pos - src; 1327 } 1328 return bytes_consumed; 1329} 1330 1331static bool 1332GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result) 1333{ 1334 llvm::SmallVector<uint64_t, 2> uint64_array; 1335 lldb::offset_t bytes_left = byte_size; 1336 uint64_t u64; 1337 const lldb::ByteOrder byte_order = data.GetByteOrder(); 1338 if (byte_order == lldb::eByteOrderLittle) 1339 { 1340 while (bytes_left > 0) 1341 { 1342 if (bytes_left >= 8) 1343 { 1344 u64 = data.GetU64(offset_ptr); 1345 bytes_left -= 8; 1346 } 1347 else 1348 { 1349 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left); 1350 bytes_left = 0; 1351 } 1352 uint64_array.push_back(u64); 1353 } 1354 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array)); 1355 return true; 1356 } 1357 else if (byte_order == lldb::eByteOrderBig) 1358 { 1359 lldb::offset_t be_offset = *offset_ptr + byte_size; 1360 lldb::offset_t temp_offset; 1361 while (bytes_left > 0) 1362 { 1363 if (bytes_left >= 8) 1364 { 1365 be_offset -= 8; 1366 temp_offset = be_offset; 1367 u64 = data.GetU64(&temp_offset); 1368 bytes_left -= 8; 1369 } 1370 else 1371 { 1372 be_offset -= bytes_left; 1373 temp_offset = be_offset; 1374 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left); 1375 bytes_left = 0; 1376 } 1377 uint64_array.push_back(u64); 1378 } 1379 *offset_ptr += byte_size; 1380 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array)); 1381 return true; 1382 } 1383 return false; 1384} 1385 1386static lldb::offset_t 1387DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix) 1388{ 1389 llvm::APInt apint; 1390 if (GetAPInt (data, &offset, byte_size, apint)) 1391 { 1392 std::string apint_str(apint.toString(radix, is_signed)); 1393 switch (radix) 1394 { 1395 case 2: 1396 s->Write ("0b", 2); 1397 break; 1398 case 8: 1399 s->Write ("0", 1); 1400 break; 1401 case 10: 1402 break; 1403 } 1404 s->Write(apint_str.c_str(), apint_str.size()); 1405 } 1406 return offset; 1407} 1408 1409static float 1410half2float (uint16_t half) 1411{ 1412 union { float f; uint32_t u; } u; 1413 int32_t v = (int16_t) half; 1414 1415 if (0 == (v & 0x7c00)) 1416 { 1417 u.u = v & 0x80007FFFU; 1418 return u.f * ldexpf(1, 125); 1419 } 1420 1421 v <<= 13; 1422 u.u = v | 0x70000000U; 1423 return u.f * ldexpf(1, -112); 1424} 1425 1426lldb::offset_t 1427DataExtractor::Dump (Stream *s, 1428 offset_t start_offset, 1429 lldb::Format item_format, 1430 size_t item_byte_size, 1431 size_t item_count, 1432 size_t num_per_line, 1433 uint64_t base_addr, 1434 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield 1435 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield 1436 ExecutionContextScope *exe_scope) const 1437{ 1438 if (s == NULL) 1439 return start_offset; 1440 1441 if (item_format == eFormatPointer) 1442 { 1443 if (item_byte_size != 4 && item_byte_size != 8) 1444 item_byte_size = s->GetAddressByteSize(); 1445 } 1446 1447 offset_t offset = start_offset; 1448 1449 if (item_format == eFormatInstruction) 1450 { 1451 TargetSP target_sp; 1452 if (exe_scope) 1453 target_sp = exe_scope->CalculateTarget(); 1454 if (target_sp) 1455 { 1456 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL, NULL)); 1457 if (disassembler_sp) 1458 { 1459 lldb::addr_t addr = base_addr + start_offset; 1460 lldb_private::Address so_addr; 1461 bool data_from_file = true; 1462 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) 1463 { 1464 data_from_file = false; 1465 } 1466 else 1467 { 1468 if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr)) 1469 so_addr.SetRawAddress(addr); 1470 } 1471 1472 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file); 1473 1474 if (bytes_consumed) 1475 { 1476 offset += bytes_consumed; 1477 const bool show_address = base_addr != LLDB_INVALID_ADDRESS; 1478 const bool show_bytes = true; 1479 ExecutionContext exe_ctx; 1480 exe_scope->CalculateExecutionContext(exe_ctx); 1481 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx); 1482 1483 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions. 1484 // I'll fix that but for now, just clear the list and it will go away nicely. 1485 disassembler_sp->GetInstructionList().Clear(); 1486 } 1487 } 1488 } 1489 else 1490 s->Printf ("invalid target"); 1491 1492 return offset; 1493 } 1494 1495 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8) 1496 item_format = eFormatHex; 1497 1498 lldb::offset_t line_start_offset = start_offset; 1499 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count) 1500 { 1501 if ((count % num_per_line) == 0) 1502 { 1503 if (count > 0) 1504 { 1505 if (item_format == eFormatBytesWithASCII && offset > line_start_offset) 1506 { 1507 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), ""); 1508 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0); 1509 } 1510 s->EOL(); 1511 } 1512 if (base_addr != LLDB_INVALID_ADDRESS) 1513 s->Printf ("0x%8.8" PRIx64 ": ", 1514 (uint64_t)(base_addr + (offset - start_offset)/m_target_byte_size )); 1515 1516 line_start_offset = offset; 1517 } 1518 else 1519 if (item_format != eFormatChar && 1520 item_format != eFormatCharPrintable && 1521 item_format != eFormatCharArray && 1522 count > 0) 1523 { 1524 s->PutChar(' '); 1525 } 1526 1527 uint32_t i; 1528 switch (item_format) 1529 { 1530 case eFormatBoolean: 1531 if (item_byte_size <= 8) 1532 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false"); 1533 else 1534 { 1535 s->Printf("error: unsupported byte size (%" PRIu64 ") for boolean format", (uint64_t)item_byte_size); 1536 return offset; 1537 } 1538 break; 1539 1540 case eFormatBinary: 1541 if (item_byte_size <= 8) 1542 { 1543 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1544 // Avoid std::bitset<64>::to_string() since it is missing in 1545 // earlier C++ libraries 1546 std::string binary_value(64, '0'); 1547 std::bitset<64> bits(uval64); 1548 for (i = 0; i < 64; ++i) 1549 if (bits[i]) 1550 binary_value[64 - 1 - i] = '1'; 1551 if (item_bit_size > 0) 1552 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size); 1553 else if (item_byte_size > 0 && item_byte_size <= 8) 1554 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8); 1555 } 1556 else 1557 { 1558 const bool is_signed = false; 1559 const unsigned radix = 2; 1560 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1561 } 1562 break; 1563 1564 case eFormatBytes: 1565 case eFormatBytesWithASCII: 1566 for (i=0; i<item_byte_size; ++i) 1567 { 1568 s->Printf ("%2.2x", GetU8(&offset)); 1569 } 1570 1571 // Put an extra space between the groups of bytes if more than one 1572 // is being dumped in a group (item_byte_size is more than 1). 1573 if (item_byte_size > 1) 1574 s->PutChar(' '); 1575 break; 1576 1577 case eFormatChar: 1578 case eFormatCharPrintable: 1579 case eFormatCharArray: 1580 { 1581 // If we are only printing one character surround it with single 1582 // quotes 1583 if (item_count == 1 && item_format == eFormatChar) 1584 s->PutChar('\''); 1585 1586 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1587 if (isprint(ch)) 1588 s->Printf ("%c", (char)ch); 1589 else if (item_format != eFormatCharPrintable) 1590 { 1591 switch (ch) 1592 { 1593 case '\033': s->Printf ("\\e"); break; 1594 case '\a': s->Printf ("\\a"); break; 1595 case '\b': s->Printf ("\\b"); break; 1596 case '\f': s->Printf ("\\f"); break; 1597 case '\n': s->Printf ("\\n"); break; 1598 case '\r': s->Printf ("\\r"); break; 1599 case '\t': s->Printf ("\\t"); break; 1600 case '\v': s->Printf ("\\v"); break; 1601 case '\0': s->Printf ("\\0"); break; 1602 default: 1603 if (item_byte_size == 1) 1604 s->Printf ("\\x%2.2x", (uint8_t)ch); 1605 else 1606 s->Printf ("%" PRIu64, ch); 1607 break; 1608 } 1609 } 1610 else 1611 { 1612 s->PutChar(NON_PRINTABLE_CHAR); 1613 } 1614 1615 // If we are only printing one character surround it with single quotes 1616 if (item_count == 1 && item_format == eFormatChar) 1617 s->PutChar('\''); 1618 } 1619 break; 1620 1621 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type 1622 case eFormatDecimal: 1623 if (item_byte_size <= 8) 1624 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1625 else 1626 { 1627 const bool is_signed = true; 1628 const unsigned radix = 10; 1629 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1630 } 1631 break; 1632 1633 case eFormatUnsigned: 1634 if (item_byte_size <= 8) 1635 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1636 else 1637 { 1638 const bool is_signed = false; 1639 const unsigned radix = 10; 1640 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1641 } 1642 break; 1643 1644 case eFormatOctal: 1645 if (item_byte_size <= 8) 1646 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1647 else 1648 { 1649 const bool is_signed = false; 1650 const unsigned radix = 8; 1651 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1652 } 1653 break; 1654 1655 case eFormatOSType: 1656 { 1657 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1658 s->PutChar('\''); 1659 for (i=0; i<item_byte_size; ++i) 1660 { 1661 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8)); 1662 if (isprint(ch)) 1663 s->Printf ("%c", ch); 1664 else 1665 { 1666 switch (ch) 1667 { 1668 case '\033': s->Printf ("\\e"); break; 1669 case '\a': s->Printf ("\\a"); break; 1670 case '\b': s->Printf ("\\b"); break; 1671 case '\f': s->Printf ("\\f"); break; 1672 case '\n': s->Printf ("\\n"); break; 1673 case '\r': s->Printf ("\\r"); break; 1674 case '\t': s->Printf ("\\t"); break; 1675 case '\v': s->Printf ("\\v"); break; 1676 case '\0': s->Printf ("\\0"); break; 1677 default: s->Printf ("\\x%2.2x", ch); break; 1678 } 1679 } 1680 } 1681 s->PutChar('\''); 1682 } 1683 break; 1684 1685 case eFormatCString: 1686 { 1687 const char *cstr = GetCStr(&offset); 1688 1689 if (!cstr) 1690 { 1691 s->Printf("NULL"); 1692 offset = LLDB_INVALID_OFFSET; 1693 } 1694 else 1695 { 1696 s->PutChar('\"'); 1697 1698 while (const char c = *cstr) 1699 { 1700 if (isprint(c)) 1701 { 1702 s->PutChar(c); 1703 } 1704 else 1705 { 1706 switch (c) 1707 { 1708 case '\033': s->Printf ("\\e"); break; 1709 case '\a': s->Printf ("\\a"); break; 1710 case '\b': s->Printf ("\\b"); break; 1711 case '\f': s->Printf ("\\f"); break; 1712 case '\n': s->Printf ("\\n"); break; 1713 case '\r': s->Printf ("\\r"); break; 1714 case '\t': s->Printf ("\\t"); break; 1715 case '\v': s->Printf ("\\v"); break; 1716 default: s->Printf ("\\x%2.2x", c); break; 1717 } 1718 } 1719 1720 ++cstr; 1721 } 1722 1723 s->PutChar('\"'); 1724 } 1725 } 1726 break; 1727 1728 1729 case eFormatPointer: 1730 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t)); 1731 break; 1732 1733 1734 case eFormatComplexInteger: 1735 { 1736 size_t complex_int_byte_size = item_byte_size / 2; 1737 1738 if (complex_int_byte_size > 0 && complex_int_byte_size <= 8) 1739 { 1740 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0)); 1741 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0)); 1742 } 1743 else 1744 { 1745 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex integer format", (uint64_t)item_byte_size); 1746 return offset; 1747 } 1748 } 1749 break; 1750 1751 case eFormatComplex: 1752 if (sizeof(float) * 2 == item_byte_size) 1753 { 1754 float f32_1 = GetFloat (&offset); 1755 float f32_2 = GetFloat (&offset); 1756 1757 s->Printf ("%g + %gi", f32_1, f32_2); 1758 break; 1759 } 1760 else if (sizeof(double) * 2 == item_byte_size) 1761 { 1762 double d64_1 = GetDouble (&offset); 1763 double d64_2 = GetDouble (&offset); 1764 1765 s->Printf ("%lg + %lgi", d64_1, d64_2); 1766 break; 1767 } 1768 else if (sizeof(long double) * 2 == item_byte_size) 1769 { 1770 long double ld64_1 = GetLongDouble (&offset); 1771 long double ld64_2 = GetLongDouble (&offset); 1772 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2); 1773 break; 1774 } 1775 else 1776 { 1777 s->Printf("error: unsupported byte size (%" PRIu64 ") for complex float format", (uint64_t)item_byte_size); 1778 return offset; 1779 } 1780 break; 1781 1782 default: 1783 case eFormatDefault: 1784 case eFormatHex: 1785 case eFormatHexUppercase: 1786 { 1787 bool wantsuppercase = (item_format == eFormatHexUppercase); 1788 switch (item_byte_size) 1789 { 1790 case 1: 1791 case 2: 1792 case 4: 1793 case 8: 1794 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1795 break; 1796 default: 1797 { 1798 assert (item_bit_size == 0 && item_bit_offset == 0); 1799 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size); 1800 if (bytes) 1801 { 1802 s->PutCString("0x"); 1803 uint32_t idx; 1804 if (m_byte_order == eByteOrderBig) 1805 { 1806 for (idx = 0; idx < item_byte_size; ++idx) 1807 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]); 1808 } 1809 else 1810 { 1811 for (idx = 0; idx < item_byte_size; ++idx) 1812 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]); 1813 } 1814 } 1815 } 1816 break; 1817 } 1818 } 1819 break; 1820 1821 case eFormatFloat: 1822 { 1823 TargetSP target_sp; 1824 bool used_apfloat = false; 1825 if (exe_scope) 1826 target_sp = exe_scope->CalculateTarget(); 1827 if (target_sp) 1828 { 1829 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1830 if (clang_ast) 1831 { 1832 clang::ASTContext *ast = clang_ast->getASTContext(); 1833 if (ast) 1834 { 1835 llvm::SmallVector<char, 256> sv; 1836 // Show full precision when printing float values 1837 const unsigned format_precision = 0; 1838 const unsigned format_max_padding = 100; 1839 size_t item_bit_size = item_byte_size * 8; 1840 1841 if (item_bit_size == ast->getTypeSize(ast->FloatTy)) 1842 { 1843 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size)); 1844 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint); 1845 apfloat.toString(sv, format_precision, format_max_padding); 1846 } 1847 else if (item_bit_size == ast->getTypeSize(ast->DoubleTy)) 1848 { 1849 llvm::APInt apint; 1850 if (GetAPInt (*this, &offset, item_byte_size, apint)) 1851 { 1852 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint); 1853 apfloat.toString(sv, format_precision, format_max_padding); 1854 } 1855 } 1856 else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy)) 1857 { 1858 const auto &semantics = ast->getFloatTypeSemantics(ast->LongDoubleTy); 1859 const auto byte_size = (llvm::APFloat::getSizeInBits(semantics) + 7) / 8; 1860 1861 llvm::APInt apint; 1862 if (GetAPInt(*this, &offset, byte_size, apint)) 1863 { 1864 llvm::APFloat apfloat(semantics, apint); 1865 apfloat.toString(sv, format_precision, format_max_padding); 1866 } 1867 } 1868 else if (item_bit_size == ast->getTypeSize(ast->HalfTy)) 1869 { 1870 llvm::APInt apint(item_bit_size, this->GetU16(&offset)); 1871 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint); 1872 apfloat.toString(sv, format_precision, format_max_padding); 1873 } 1874 1875 if (!sv.empty()) 1876 { 1877 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data()); 1878 used_apfloat = true; 1879 } 1880 } 1881 } 1882 } 1883 1884 if (!used_apfloat) 1885 { 1886 std::ostringstream ss; 1887 if (item_byte_size == sizeof(float) || item_byte_size == 2) 1888 { 1889 float f; 1890 if (item_byte_size == 2) 1891 { 1892 uint16_t half = this->GetU16(&offset); 1893 f = half2float(half); 1894 } 1895 else 1896 { 1897 f = GetFloat (&offset); 1898 } 1899 ss.precision(std::numeric_limits<float>::digits10); 1900 ss << f; 1901 } 1902 else if (item_byte_size == sizeof(double)) 1903 { 1904 ss.precision(std::numeric_limits<double>::digits10); 1905 ss << GetDouble(&offset); 1906 } 1907 else if (item_byte_size == sizeof(long double) || item_byte_size == 10) 1908 { 1909 ss.precision(std::numeric_limits<long double>::digits10); 1910 ss << GetLongDouble(&offset); 1911 } 1912 else 1913 { 1914 s->Printf("error: unsupported byte size (%" PRIu64 ") for float format", (uint64_t)item_byte_size); 1915 return offset; 1916 } 1917 ss.flush(); 1918 s->Printf("%s", ss.str().c_str()); 1919 } 1920 } 1921 break; 1922 1923 case eFormatUnicode16: 1924 s->Printf("U+%4.4x", GetU16 (&offset)); 1925 break; 1926 1927 case eFormatUnicode32: 1928 s->Printf("U+0x%8.8x", GetU32 (&offset)); 1929 break; 1930 1931 case eFormatAddressInfo: 1932 { 1933 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1934 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr); 1935 if (exe_scope) 1936 { 1937 TargetSP target_sp (exe_scope->CalculateTarget()); 1938 lldb_private::Address so_addr; 1939 if (target_sp) 1940 { 1941 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) 1942 { 1943 s->PutChar(' '); 1944 so_addr.Dump (s, 1945 exe_scope, 1946 Address::DumpStyleResolvedDescription, 1947 Address::DumpStyleModuleWithFileAddress); 1948 } 1949 else 1950 { 1951 so_addr.SetOffset(addr); 1952 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription); 1953 } 1954 } 1955 } 1956 } 1957 break; 1958 1959 case eFormatHexFloat: 1960 if (sizeof(float) == item_byte_size) 1961 { 1962 char float_cstr[256]; 1963 llvm::APFloat ap_float (GetFloat (&offset)); 1964 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven); 1965 s->Printf ("%s", float_cstr); 1966 break; 1967 } 1968 else if (sizeof(double) == item_byte_size) 1969 { 1970 char float_cstr[256]; 1971 llvm::APFloat ap_float (GetDouble (&offset)); 1972 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven); 1973 s->Printf ("%s", float_cstr); 1974 break; 1975 } 1976 else 1977 { 1978 s->Printf("error: unsupported byte size (%" PRIu64 ") for hex float format", (uint64_t)item_byte_size); 1979 return offset; 1980 } 1981 break; 1982 1983// please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat 1984// if you fail to do so, users will start getting different outputs depending on internal 1985// implementation details they should not care about || 1986 case eFormatVectorOfChar: // || 1987 s->PutChar('{'); // \/ 1988 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); 1989 s->PutChar('}'); 1990 break; 1991 1992 case eFormatVectorOfSInt8: 1993 s->PutChar('{'); 1994 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); 1995 s->PutChar('}'); 1996 break; 1997 1998 case eFormatVectorOfUInt8: 1999 s->PutChar('{'); 2000 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); 2001 s->PutChar('}'); 2002 break; 2003 2004 case eFormatVectorOfSInt16: 2005 s->PutChar('{'); 2006 offset = Dump (s, offset, eFormatDecimal, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0); 2007 s->PutChar('}'); 2008 break; 2009 2010 case eFormatVectorOfUInt16: 2011 s->PutChar('{'); 2012 offset = Dump (s, offset, eFormatHex, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0); 2013 s->PutChar('}'); 2014 break; 2015 2016 case eFormatVectorOfSInt32: 2017 s->PutChar('{'); 2018 offset = Dump (s, offset, eFormatDecimal, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0); 2019 s->PutChar('}'); 2020 break; 2021 2022 case eFormatVectorOfUInt32: 2023 s->PutChar('{'); 2024 offset = Dump (s, offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0); 2025 s->PutChar('}'); 2026 break; 2027 2028 case eFormatVectorOfSInt64: 2029 s->PutChar('{'); 2030 offset = Dump (s, offset, eFormatDecimal, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0); 2031 s->PutChar('}'); 2032 break; 2033 2034 case eFormatVectorOfUInt64: 2035 s->PutChar('{'); 2036 offset = Dump (s, offset, eFormatHex, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0); 2037 s->PutChar('}'); 2038 break; 2039 2040 case eFormatVectorOfFloat16: 2041 s->PutChar('{'); 2042 offset = Dump (s, offset, eFormatFloat, 2, item_byte_size / 2, item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0); 2043 s->PutChar('}'); 2044 break; 2045 2046 case eFormatVectorOfFloat32: 2047 s->PutChar('{'); 2048 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0); 2049 s->PutChar('}'); 2050 break; 2051 2052 case eFormatVectorOfFloat64: 2053 s->PutChar('{'); 2054 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0); 2055 s->PutChar('}'); 2056 break; 2057 2058 case eFormatVectorOfUInt128: 2059 s->PutChar('{'); 2060 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0); 2061 s->PutChar('}'); 2062 break; 2063 } 2064 } 2065 2066 if (item_format == eFormatBytesWithASCII && offset > line_start_offset) 2067 { 2068 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), ""); 2069 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0); 2070 } 2071 return offset; // Return the offset at which we ended up 2072} 2073 2074//---------------------------------------------------------------------- 2075// Dumps bytes from this object's data to the stream "s" starting 2076// "start_offset" bytes into this data, and ending with the byte 2077// before "end_offset". "base_addr" will be added to the offset 2078// into the dumped data when showing the offset into the data in the 2079// output information. "num_per_line" objects of type "type" will 2080// be dumped with the option to override the format for each object 2081// with "type_format". "type_format" is a printf style formatting 2082// string. If "type_format" is NULL, then an appropriate format 2083// string will be used for the supplied "type". If the stream "s" 2084// is NULL, then the output will be send to Log(). 2085//---------------------------------------------------------------------- 2086lldb::offset_t 2087DataExtractor::PutToLog 2088( 2089 Log *log, 2090 offset_t start_offset, 2091 offset_t length, 2092 uint64_t base_addr, 2093 uint32_t num_per_line, 2094 DataExtractor::Type type, 2095 const char *format 2096) const 2097{ 2098 if (log == NULL) 2099 return start_offset; 2100 2101 offset_t offset; 2102 offset_t end_offset; 2103 uint32_t count; 2104 StreamString sstr; 2105 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count) 2106 { 2107 if ((count % num_per_line) == 0) 2108 { 2109 // Print out any previous string 2110 if (sstr.GetSize() > 0) 2111 { 2112 log->Printf("%s", sstr.GetData()); 2113 sstr.Clear(); 2114 } 2115 // Reset string offset and fill the current line string with address: 2116 if (base_addr != LLDB_INVALID_ADDRESS) 2117 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset))); 2118 } 2119 2120 switch (type) 2121 { 2122 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break; 2123 case TypeChar: 2124 { 2125 char ch = GetU8(&offset); 2126 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' '); 2127 } 2128 break; 2129 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break; 2130 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break; 2131 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break; 2132 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break; 2133 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break; 2134 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break; 2135 } 2136 } 2137 2138 if (sstr.GetSize() > 0) 2139 log->Printf("%s", sstr.GetData()); 2140 2141 return offset; // Return the offset at which we ended up 2142} 2143 2144//---------------------------------------------------------------------- 2145// DumpUUID 2146// 2147// Dump out a UUID starting at 'offset' bytes into the buffer 2148//---------------------------------------------------------------------- 2149void 2150DataExtractor::DumpUUID (Stream *s, offset_t offset) const 2151{ 2152 if (s) 2153 { 2154 const uint8_t *uuid_data = PeekData(offset, 16); 2155 if ( uuid_data ) 2156 { 2157 lldb_private::UUID uuid(uuid_data, 16); 2158 uuid.Dump(s); 2159 } 2160 else 2161 { 2162 s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset); 2163 } 2164 } 2165} 2166 2167void 2168DataExtractor::DumpHexBytes (Stream *s, 2169 const void *src, 2170 size_t src_len, 2171 uint32_t bytes_per_line, 2172 addr_t base_addr) 2173{ 2174 DataExtractor data (src, src_len, eByteOrderLittle, 4); 2175 data.Dump (s, 2176 0, // Offset into "src" 2177 eFormatBytes, // Dump as hex bytes 2178 1, // Size of each item is 1 for single bytes 2179 src_len, // Number of bytes 2180 bytes_per_line, // Num bytes per line 2181 base_addr, // Base address 2182 0, 0); // Bitfield info 2183} 2184 2185size_t 2186DataExtractor::Copy (DataExtractor &dest_data) const 2187{ 2188 if (m_data_sp.get()) 2189 { 2190 // we can pass along the SP to the data 2191 dest_data.SetData(m_data_sp); 2192 } 2193 else 2194 { 2195 const uint8_t *base_ptr = m_start; 2196 size_t data_size = GetByteSize(); 2197 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size))); 2198 } 2199 return GetByteSize(); 2200} 2201 2202bool 2203DataExtractor::Append(DataExtractor& rhs) 2204{ 2205 if (rhs.GetByteOrder() != GetByteOrder()) 2206 return false; 2207 2208 if (rhs.GetByteSize() == 0) 2209 return true; 2210 2211 if (GetByteSize() == 0) 2212 return (rhs.Copy(*this) > 0); 2213 2214 size_t bytes = GetByteSize() + rhs.GetByteSize(); 2215 2216 DataBufferHeap *buffer_heap_ptr = NULL; 2217 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); 2218 2219 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL) 2220 return false; 2221 2222 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes(); 2223 2224 memcpy(bytes_ptr, GetDataStart(), GetByteSize()); 2225 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize()); 2226 2227 SetData(buffer_sp); 2228 2229 return true; 2230} 2231 2232bool 2233DataExtractor::Append(void* buf, offset_t length) 2234{ 2235 if (buf == NULL) 2236 return false; 2237 2238 if (length == 0) 2239 return true; 2240 2241 size_t bytes = GetByteSize() + length; 2242 2243 DataBufferHeap *buffer_heap_ptr = NULL; 2244 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); 2245 2246 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL) 2247 return false; 2248 2249 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes(); 2250 2251 if (GetByteSize() > 0) 2252 memcpy(bytes_ptr, GetDataStart(), GetByteSize()); 2253 2254 memcpy(bytes_ptr + GetByteSize(), buf, length); 2255 2256 SetData(buffer_sp); 2257 2258 return true; 2259} 2260 2261void 2262DataExtractor::Checksum (llvm::SmallVectorImpl<uint8_t> &dest, 2263 uint64_t max_data) 2264{ 2265 if (max_data == 0) 2266 max_data = GetByteSize(); 2267 else 2268 max_data = std::min(max_data, GetByteSize()); 2269 2270 llvm::MD5 md5; 2271 2272 const llvm::ArrayRef<uint8_t> data(GetDataStart(),max_data); 2273 md5.update(data); 2274 2275 llvm::MD5::MD5Result result; 2276 md5.final(result); 2277 2278 dest.resize(16); 2279 std::copy(result, 2280 result+16, 2281 dest.begin()); 2282} 2283 2284