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