DataExtractor.cpp revision 269024
158579Srwatson//===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===// 221526Sdavidn// 358579Srwatson// The LLVM Compiler Infrastructure 458579Srwatson// 521526Sdavidn// This file is distributed under the University of Illinois Open Source 621526Sdavidn// License. See LICENSE.TXT for details. 721526Sdavidn// 821526Sdavidn//===----------------------------------------------------------------------===// 921526Sdavidn 1050472Speter#include <assert.h> 1121526Sdavidn#include <stddef.h> 1221526Sdavidn 1339375Smsmith#include <bitset> 1439375Smsmith#include <limits> 1521526Sdavidn#include <sstream> 1642149Shoek#include <string> 1721526Sdavidn 1821526Sdavidn#include "clang/AST/ASTContext.h" 1921526Sdavidn 2021526Sdavidn#include "llvm/ADT/APFloat.h" 2169015Sobrien#include "llvm/ADT/APInt.h" 2270189Srwatson#include "llvm/ADT/ArrayRef.h" 2321526Sdavidn#include "llvm/ADT/SmallVector.h" 2487887Smikeh#include "llvm/Support/MathExtras.h" 2558579Srwatson 2642515Sasami 2739375Smsmith#include "lldb/Core/DataBufferHeap.h" 2839375Smsmith#include "lldb/Core/DataExtractor.h" 2939375Smsmith#include "lldb/Core/DataBuffer.h" 3039375Smsmith#include "lldb/Core/Disassembler.h" 3139375Smsmith#include "lldb/Core/Log.h" 3239375Smsmith#include "lldb/Core/Stream.h" 3339375Smsmith#include "lldb/Core/StreamString.h" 3439375Smsmith#include "lldb/Core/UUID.h" 3539375Smsmith#include "lldb/Core/dwarf.h" 3661184Salfred#include "lldb/Host/Endian.h" 3721538Sdavidn#include "lldb/Symbol/ClangASTContext.h" 3821526Sdavidn#include "lldb/Target/ExecutionContext.h" 3939375Smsmith#include "lldb/Target/ExecutionContextScope.h" 4021526Sdavidn#include "lldb/Target/SectionLoadList.h" 4121943Sdavidn#include "lldb/Target/Target.h" 4221538Sdavidn 4339375Smsmithusing namespace lldb; 4439375Smsmithusing namespace lldb_private; 4539375Smsmith 4621538Sdavidnstatic inline uint16_t 4739375SmsmithReadInt16(const unsigned char* ptr, offset_t offset) 4839375Smsmith{ 4921538Sdavidn uint16_t value; 5039375Smsmith memcpy (&value, ptr + offset, 2); 5121526Sdavidn return value; 5239375Smsmith} 5339375Smsmith 5439424Sdtstatic inline uint32_t 5539375SmsmithReadInt32 (const unsigned char* ptr, offset_t offset) 5639375Smsmith{ 5739375Smsmith uint32_t value; 5839375Smsmith memcpy (&value, ptr + offset, 4); 5921526Sdavidn return value; 6021526Sdavidn} 6139375Smsmith 6221526Sdavidnstatic inline uint64_t 6348814SnikReadInt64(const unsigned char* ptr, offset_t offset) 6448814Snik{ 6521526Sdavidn uint64_t value; 6639375Smsmith memcpy (&value, ptr + offset, 8); 6739375Smsmith return value; 6821526Sdavidn} 6921526Sdavidn 7039375Smsmithstatic inline uint16_t 7121526SdavidnReadInt16(const void* ptr) 7291527Srwatson{ 7339375Smsmith uint16_t value; 7439375Smsmith memcpy (&value, ptr, 2); 7521526Sdavidn return value; 7621526Sdavidn} 7721526Sdavidn 7839375Smsmithstatic inline uint32_t 7939375SmsmithReadInt32 (const void* ptr) 8039375Smsmith{ 8139375Smsmith uint32_t value; 8239375Smsmith memcpy (&value, ptr, 4); 8339375Smsmith return value; 8439375Smsmith} 8539375Smsmith 8639375Smsmithstatic inline uint64_t 8739375SmsmithReadInt64(const void* ptr) 8839375Smsmith{ 8921526Sdavidn uint64_t value; 9039375Smsmith memcpy (&value, ptr, 8); 9139375Smsmith return value; 9239375Smsmith} 9339375Smsmith 9439375Smsmithstatic inline uint16_t 9539375SmsmithReadSwapInt16(const unsigned char* ptr, offset_t offset) 9639375Smsmith{ 9739375Smsmith uint16_t value; 9839375Smsmith memcpy (&value, ptr + offset, 2); 9939375Smsmith return llvm::ByteSwap_16(value); 10039375Smsmith} 10139375Smsmith 10239375Smsmithstatic inline uint32_t 10339375SmsmithReadSwapInt32 (const unsigned char* ptr, offset_t offset) 10421526Sdavidn{ 10521526Sdavidn uint32_t value; 10639375Smsmith memcpy (&value, ptr + offset, 4); 10739375Smsmith return llvm::ByteSwap_32(value); 10839375Smsmith} 10939375Smsmith 11070189Srwatsonstatic inline uint64_t 11139375SmsmithReadSwapInt64(const unsigned char* ptr, offset_t offset) 11243220Sdg{ 11339375Smsmith uint64_t value; 11439375Smsmith memcpy (&value, ptr + offset, 8); 11542587Sasami return llvm::ByteSwap_64(value); 11639375Smsmith} 11739375Smsmith 11839375Smsmithstatic inline uint16_t 11939375SmsmithReadSwapInt16(const void* ptr) 12039375Smsmith{ 12139375Smsmith uint16_t value; 12239375Smsmith memcpy (&value, ptr, 2); 12339375Smsmith return llvm::ByteSwap_16(value); 12439375Smsmith} 12539375Smsmith 12639375Smsmithstatic inline uint32_t 12746209ShoekReadSwapInt32 (const void* ptr) 12839375Smsmith{ 12939375Smsmith uint32_t value; 13039375Smsmith memcpy (&value, ptr, 4); 13121526Sdavidn return llvm::ByteSwap_32(value); 13221526Sdavidn} 13339375Smsmith 13439375Smsmithstatic inline uint64_t 13539375SmsmithReadSwapInt64(const void* ptr) 13639375Smsmith{ 13739375Smsmith uint64_t value; 13839375Smsmith memcpy (&value, ptr, 8); 13939375Smsmith return llvm::ByteSwap_64(value); 14039375Smsmith} 14139375Smsmith 14239375Smsmith#define NON_PRINTABLE_CHAR '.' 14339375Smsmith//---------------------------------------------------------------------- 14439375Smsmith// Default constructor. 14539375Smsmith//---------------------------------------------------------------------- 14625369SacheDataExtractor::DataExtractor () : 14725369Sache m_start (NULL), 14839375Smsmith m_end (NULL), 14939375Smsmith m_byte_order(lldb::endian::InlHostByteOrder()), 15039375Smsmith m_addr_size (4), 15139375Smsmith m_data_sp () 15239375Smsmith{ 15339375Smsmith} 15439375Smsmith 15539375Smsmith//---------------------------------------------------------------------- 15639375Smsmith// This constructor allows us to use data that is owned by someone else. 15739375Smsmith// The data must stay around as long as this object is valid. 15839375Smsmith//---------------------------------------------------------------------- 15939375SmsmithDataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size) : 16039375Smsmith m_start ((uint8_t*)data), 16139375Smsmith m_end ((uint8_t*)data + length), 16239375Smsmith m_byte_order(endian), 16339375Smsmith m_addr_size (addr_size), 16439375Smsmith m_data_sp () 16539375Smsmith{ 16639375Smsmith} 16739375Smsmith 16839375Smsmith//---------------------------------------------------------------------- 16939375Smsmith// Make a shared pointer reference to the shared data in "data_sp" and 17039375Smsmith// set the endian swapping setting to "swap", and the address size to 17139375Smsmith// "addr_size". The shared data reference will ensure the data lives 17239375Smsmith// as long as any DataExtractor objects exist that have a reference to 17339375Smsmith// this data. 17439375Smsmith//---------------------------------------------------------------------- 17539375SmsmithDataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size) : 17639375Smsmith m_start (NULL), 17739375Smsmith m_end (NULL), 17839375Smsmith m_byte_order(endian), 17939375Smsmith m_addr_size (addr_size), 18039375Smsmith m_data_sp () 18139375Smsmith{ 18239375Smsmith SetData (data_sp); 18339375Smsmith} 18439375Smsmith 18539375Smsmith//---------------------------------------------------------------------- 18639375Smsmith// Initialize this object with a subset of the data bytes in "data". 18739375Smsmith// If "data" contains shared data, then a reference to this shared 18839375Smsmith// data will added and the shared data will stay around as long 18939375Smsmith// as any object contains a reference to that data. The endian 19039375Smsmith// swap and address size settings are copied from "data". 19139375Smsmith//---------------------------------------------------------------------- 19239375SmsmithDataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length) : 19339375Smsmith m_start(NULL), 19439375Smsmith m_end(NULL), 19539375Smsmith m_byte_order(data.m_byte_order), 19639375Smsmith m_addr_size(data.m_addr_size), 19739375Smsmith m_data_sp() 19839375Smsmith{ 19939375Smsmith if (data.ValidOffset(offset)) 20039375Smsmith { 20139375Smsmith offset_t bytes_available = data.GetByteSize() - offset; 20239375Smsmith if (length > bytes_available) 20339375Smsmith length = bytes_available; 20439375Smsmith SetData(data, offset, length); 20539375Smsmith } 20639375Smsmith} 20739375Smsmith 20839375SmsmithDataExtractor::DataExtractor (const DataExtractor& rhs) : 20939375Smsmith m_start (rhs.m_start), 21039375Smsmith m_end (rhs.m_end), 21139375Smsmith m_byte_order (rhs.m_byte_order), 21239375Smsmith m_addr_size (rhs.m_addr_size), 21339375Smsmith m_data_sp (rhs.m_data_sp) 21439375Smsmith{ 21539375Smsmith} 21639375Smsmith 21739375Smsmith//---------------------------------------------------------------------- 21839375Smsmith// Assignment operator 21939375Smsmith//---------------------------------------------------------------------- 22039375Smsmithconst DataExtractor& 22139375SmsmithDataExtractor::operator= (const DataExtractor& rhs) 22239375Smsmith{ 22339375Smsmith if (this != &rhs) 22439375Smsmith { 22539375Smsmith m_start = rhs.m_start; 22639375Smsmith m_end = rhs.m_end; 22739375Smsmith m_byte_order = rhs.m_byte_order; 22839375Smsmith m_addr_size = rhs.m_addr_size; 22939375Smsmith m_data_sp = rhs.m_data_sp; 23039375Smsmith } 23139375Smsmith return *this; 23239375Smsmith} 23339375Smsmith 23439375Smsmith//---------------------------------------------------------------------- 23539375Smsmith// Destructor 23639375Smsmith//---------------------------------------------------------------------- 23739375SmsmithDataExtractor::~DataExtractor () 23839375Smsmith{ 23939375Smsmith} 24039375Smsmith 24139375Smsmith//------------------------------------------------------------------ 24239375Smsmith// Clears the object contents back to a default invalid state, and 24339375Smsmith// release any references to shared data that this object may 24439375Smsmith// contain. 24546209Shoek//------------------------------------------------------------------ 24639375Smsmithvoid 24739375SmsmithDataExtractor::Clear () 24839375Smsmith{ 24939375Smsmith m_start = NULL; 25039375Smsmith m_end = NULL; 25139375Smsmith m_byte_order = lldb::endian::InlHostByteOrder(); 25239375Smsmith m_addr_size = 4; 25339375Smsmith m_data_sp.reset(); 25439375Smsmith} 25539375Smsmith 25639375Smsmith//------------------------------------------------------------------ 25739375Smsmith// If this object contains shared data, this function returns the 25839375Smsmith// offset into that shared data. Else zero is returned. 25939375Smsmith//------------------------------------------------------------------ 26039375Smsmithsize_t 26139375SmsmithDataExtractor::GetSharedDataOffset () const 26239375Smsmith{ 26339375Smsmith if (m_start != NULL) 26439375Smsmith { 26539375Smsmith const DataBuffer * data = m_data_sp.get(); 26639375Smsmith if (data != NULL) 26739375Smsmith { 26839375Smsmith const uint8_t * data_bytes = data->GetBytes(); 26939375Smsmith if (data_bytes != NULL) 27039375Smsmith { 27139375Smsmith assert(m_start >= data_bytes); 27239375Smsmith return m_start - data_bytes; 27339375Smsmith } 27439375Smsmith } 27539375Smsmith } 27639375Smsmith return 0; 27739375Smsmith} 27839375Smsmith 27939375Smsmith//---------------------------------------------------------------------- 28039375Smsmith// Set the data with which this object will extract from to data 28139375Smsmith// starting at BYTES and set the length of the data to LENGTH bytes 28239375Smsmith// long. The data is externally owned must be around at least as 28339375Smsmith// long as this object points to the data. No copy of the data is 28439375Smsmith// made, this object just refers to this data and can extract from 28539375Smsmith// it. If this object refers to any shared data upon entry, the 28639375Smsmith// reference to that data will be released. Is SWAP is set to true, 28739375Smsmith// any data extracted will be endian swapped. 28839375Smsmith//---------------------------------------------------------------------- 28939375Smsmithlldb::offset_t 29039375SmsmithDataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian) 29139375Smsmith{ 29239375Smsmith m_byte_order = endian; 29339375Smsmith m_data_sp.reset(); 29439375Smsmith if (bytes == NULL || length == 0) 29539375Smsmith { 29639375Smsmith m_start = NULL; 29739375Smsmith m_end = NULL; 29839375Smsmith } 29939375Smsmith else 30039375Smsmith { 30139375Smsmith m_start = (uint8_t *)bytes; 30239375Smsmith m_end = m_start + length; 30339375Smsmith } 30491527Srwatson return GetByteSize(); 30539375Smsmith} 30639375Smsmith 30769015Sobrien//---------------------------------------------------------------------- 30869015Sobrien// Assign the data for this object to be a subrange in "data" 30969015Sobrien// starting "data_offset" bytes into "data" and ending "data_length" 31069015Sobrien// bytes later. If "data_offset" is not a valid offset into "data", 31169015Sobrien// then this object will contain no bytes. If "data_offset" is 31283325Sru// within "data" yet "data_length" is too large, the length will be 31369015Sobrien// capped at the number of bytes remaining in "data". If "data" 314// contains a shared pointer to other data, then a ref counted 315// pointer to that data will be made in this object. If "data" 316// doesn't contain a shared pointer to data, then the bytes referred 317// to in "data" will need to exist at least as long as this object 318// refers to those bytes. The address size and endian swap settings 319// are copied from the current values in "data". 320//---------------------------------------------------------------------- 321lldb::offset_t 322DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length) 323{ 324 m_addr_size = data.m_addr_size; 325 // If "data" contains shared pointer to data, then we can use that 326 if (data.m_data_sp.get()) 327 { 328 m_byte_order = data.m_byte_order; 329 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length); 330 } 331 332 // We have a DataExtractor object that just has a pointer to bytes 333 if (data.ValidOffset(data_offset)) 334 { 335 if (data_length > data.GetByteSize() - data_offset) 336 data_length = data.GetByteSize() - data_offset; 337 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder()); 338 } 339 return 0; 340} 341 342//---------------------------------------------------------------------- 343// Assign the data for this object to be a subrange of the shared 344// data in "data_sp" starting "data_offset" bytes into "data_sp" 345// and ending "data_length" bytes later. If "data_offset" is not 346// a valid offset into "data_sp", then this object will contain no 347// bytes. If "data_offset" is within "data_sp" yet "data_length" is 348// too large, the length will be capped at the number of bytes 349// remaining in "data_sp". A ref counted pointer to the data in 350// "data_sp" will be made in this object IF the number of bytes this 351// object refers to in greater than zero (if at least one byte was 352// available starting at "data_offset") to ensure the data stays 353// around as long as it is needed. The address size and endian swap 354// settings will remain unchanged from their current settings. 355//---------------------------------------------------------------------- 356lldb::offset_t 357DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length) 358{ 359 m_start = m_end = NULL; 360 361 if (data_length > 0) 362 { 363 m_data_sp = data_sp; 364 if (data_sp.get()) 365 { 366 const size_t data_size = data_sp->GetByteSize(); 367 if (data_offset < data_size) 368 { 369 m_start = data_sp->GetBytes() + data_offset; 370 const size_t bytes_left = data_size - data_offset; 371 // Cap the length of we asked for too many 372 if (data_length <= bytes_left) 373 m_end = m_start + data_length; // We got all the bytes we wanted 374 else 375 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data 376 } 377 } 378 } 379 380 size_t new_size = GetByteSize(); 381 382 // Don't hold a shared pointer to the data buffer if we don't share 383 // any valid bytes in the shared buffer. 384 if (new_size == 0) 385 m_data_sp.reset(); 386 387 return new_size; 388} 389 390//---------------------------------------------------------------------- 391// Extract a single unsigned char from the binary data and update 392// the offset pointed to by "offset_ptr". 393// 394// RETURNS the byte that was extracted, or zero on failure. 395//---------------------------------------------------------------------- 396uint8_t 397DataExtractor::GetU8 (offset_t *offset_ptr) const 398{ 399 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1); 400 if (data) 401 return *data; 402 return 0; 403} 404 405//---------------------------------------------------------------------- 406// Extract "count" unsigned chars from the binary data and update the 407// offset pointed to by "offset_ptr". The extracted data is copied into 408// "dst". 409// 410// RETURNS the non-NULL buffer pointer upon successful extraction of 411// all the requested bytes, or NULL when the data is not available in 412// the buffer due to being out of bounds, or unsufficient data. 413//---------------------------------------------------------------------- 414void * 415DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const 416{ 417 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count); 418 if (data) 419 { 420 // Copy the data into the buffer 421 memcpy (dst, data, count); 422 // Return a non-NULL pointer to the converted data as an indicator of success 423 return dst; 424 } 425 return NULL; 426} 427 428//---------------------------------------------------------------------- 429// Extract a single uint16_t from the data and update the offset 430// pointed to by "offset_ptr". 431// 432// RETURNS the uint16_t that was extracted, or zero on failure. 433//---------------------------------------------------------------------- 434uint16_t 435DataExtractor::GetU16 (offset_t *offset_ptr) const 436{ 437 uint16_t val = 0; 438 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val)); 439 if (data) 440 { 441 if (m_byte_order != lldb::endian::InlHostByteOrder()) 442 val = ReadSwapInt16(data); 443 else 444 val = ReadInt16 (data); 445 } 446 return val; 447} 448 449uint16_t 450DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const 451{ 452 uint16_t val; 453 if (m_byte_order == lldb::endian::InlHostByteOrder()) 454 val = ReadInt16 (m_start, *offset_ptr); 455 else 456 val = ReadSwapInt16(m_start, *offset_ptr); 457 *offset_ptr += sizeof(val); 458 return val; 459} 460 461uint32_t 462DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const 463{ 464 uint32_t val; 465 if (m_byte_order == lldb::endian::InlHostByteOrder()) 466 val = ReadInt32 (m_start, *offset_ptr); 467 else 468 val = ReadSwapInt32 (m_start, *offset_ptr); 469 *offset_ptr += sizeof(val); 470 return val; 471} 472 473uint64_t 474DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const 475{ 476 uint64_t val; 477 if (m_byte_order == lldb::endian::InlHostByteOrder()) 478 val = ReadInt64 (m_start, *offset_ptr); 479 else 480 val = ReadSwapInt64 (m_start, *offset_ptr); 481 *offset_ptr += sizeof(val); 482 return val; 483} 484 485 486//---------------------------------------------------------------------- 487// Extract "count" uint16_t values from the binary data and update 488// the offset pointed to by "offset_ptr". The extracted data is 489// copied into "dst". 490// 491// RETURNS the non-NULL buffer pointer upon successful extraction of 492// all the requested bytes, or NULL when the data is not available 493// in the buffer due to being out of bounds, or unsufficient data. 494//---------------------------------------------------------------------- 495void * 496DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const 497{ 498 const size_t src_size = sizeof(uint16_t) * count; 499 const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size); 500 if (src) 501 { 502 if (m_byte_order != lldb::endian::InlHostByteOrder()) 503 { 504 uint16_t *dst_pos = (uint16_t *)void_dst; 505 uint16_t *dst_end = dst_pos + count; 506 const uint16_t *src_pos = src; 507 while (dst_pos < dst_end) 508 { 509 *dst_pos = ReadSwapInt16 (src_pos); 510 ++dst_pos; 511 ++src_pos; 512 } 513 } 514 else 515 { 516 memcpy (void_dst, src, src_size); 517 } 518 // Return a non-NULL pointer to the converted data as an indicator of success 519 return void_dst; 520 } 521 return NULL; 522} 523 524//---------------------------------------------------------------------- 525// Extract a single uint32_t from the data and update the offset 526// pointed to by "offset_ptr". 527// 528// RETURNS the uint32_t that was extracted, or zero on failure. 529//---------------------------------------------------------------------- 530uint32_t 531DataExtractor::GetU32 (offset_t *offset_ptr) const 532{ 533 uint32_t val = 0; 534 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val)); 535 if (data) 536 { 537 if (m_byte_order != lldb::endian::InlHostByteOrder()) 538 { 539 val = ReadSwapInt32 (data); 540 } 541 else 542 { 543 memcpy (&val, data, 4); 544 } 545 } 546 return val; 547} 548 549//---------------------------------------------------------------------- 550// Extract "count" uint32_t values from the binary data and update 551// the offset pointed to by "offset_ptr". The extracted data is 552// copied into "dst". 553// 554// RETURNS the non-NULL buffer pointer upon successful extraction of 555// all the requested bytes, or NULL when the data is not available 556// in the buffer due to being out of bounds, or unsufficient data. 557//---------------------------------------------------------------------- 558void * 559DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const 560{ 561 const size_t src_size = sizeof(uint32_t) * count; 562 const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size); 563 if (src) 564 { 565 if (m_byte_order != lldb::endian::InlHostByteOrder()) 566 { 567 uint32_t *dst_pos = (uint32_t *)void_dst; 568 uint32_t *dst_end = dst_pos + count; 569 const uint32_t *src_pos = src; 570 while (dst_pos < dst_end) 571 { 572 *dst_pos = ReadSwapInt32 (src_pos); 573 ++dst_pos; 574 ++src_pos; 575 } 576 } 577 else 578 { 579 memcpy (void_dst, src, src_size); 580 } 581 // Return a non-NULL pointer to the converted data as an indicator of success 582 return void_dst; 583 } 584 return NULL; 585} 586 587//---------------------------------------------------------------------- 588// Extract a single uint64_t from the data and update the offset 589// pointed to by "offset_ptr". 590// 591// RETURNS the uint64_t that was extracted, or zero on failure. 592//---------------------------------------------------------------------- 593uint64_t 594DataExtractor::GetU64 (offset_t *offset_ptr) const 595{ 596 uint64_t val = 0; 597 const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val)); 598 if (data) 599 { 600 if (m_byte_order != lldb::endian::InlHostByteOrder()) 601 { 602 val = ReadSwapInt64 (data); 603 } 604 else 605 { 606 memcpy (&val, data, 8); 607 } 608 } 609 return val; 610} 611 612//---------------------------------------------------------------------- 613// GetU64 614// 615// Get multiple consecutive 64 bit values. Return true if the entire 616// read succeeds and increment the offset pointed to by offset_ptr, else 617// return false and leave the offset pointed to by offset_ptr unchanged. 618//---------------------------------------------------------------------- 619void * 620DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const 621{ 622 const size_t src_size = sizeof(uint64_t) * count; 623 const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size); 624 if (src) 625 { 626 if (m_byte_order != lldb::endian::InlHostByteOrder()) 627 { 628 uint64_t *dst_pos = (uint64_t *)void_dst; 629 uint64_t *dst_end = dst_pos + count; 630 const uint64_t *src_pos = src; 631 while (dst_pos < dst_end) 632 { 633 *dst_pos = ReadSwapInt64 (src_pos); 634 ++dst_pos; 635 ++src_pos; 636 } 637 } 638 else 639 { 640 memcpy (void_dst, src, src_size); 641 } 642 // Return a non-NULL pointer to the converted data as an indicator of success 643 return void_dst; 644 } 645 return NULL; 646} 647 648//---------------------------------------------------------------------- 649// Extract a single integer value from the data and update the offset 650// pointed to by "offset_ptr". The size of the extracted integer 651// is specified by the "byte_size" argument. "byte_size" should have 652// a value between 1 and 4 since the return value is only 32 bits 653// wide. Any "byte_size" values less than 1 or greater than 4 will 654// result in nothing being extracted, and zero being returned. 655// 656// RETURNS the integer value that was extracted, or zero on failure. 657//---------------------------------------------------------------------- 658uint32_t 659DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const 660{ 661 switch (byte_size) 662 { 663 case 1: return GetU8 (offset_ptr); break; 664 case 2: return GetU16(offset_ptr); break; 665 case 4: return GetU32(offset_ptr); break; 666 default: 667 assert("GetMaxU32 unhandled case!" == NULL); 668 break; 669 } 670 return 0; 671} 672 673//---------------------------------------------------------------------- 674// Extract a single integer value from the data and update the offset 675// pointed to by "offset_ptr". The size of the extracted integer 676// is specified by the "byte_size" argument. "byte_size" should have 677// a value >= 1 and <= 8 since the return value is only 64 bits 678// wide. Any "byte_size" values less than 1 or greater than 8 will 679// result in nothing being extracted, and zero being returned. 680// 681// RETURNS the integer value that was extracted, or zero on failure. 682//---------------------------------------------------------------------- 683uint64_t 684DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const 685{ 686 switch (size) 687 { 688 case 1: return GetU8 (offset_ptr); break; 689 case 2: return GetU16(offset_ptr); break; 690 case 4: return GetU32(offset_ptr); break; 691 case 8: return GetU64(offset_ptr); break; 692 default: 693 assert("GetMax64 unhandled case!" == NULL); 694 break; 695 } 696 return 0; 697} 698 699uint64_t 700DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const 701{ 702 switch (size) 703 { 704 case 1: return GetU8_unchecked (offset_ptr); break; 705 case 2: return GetU16_unchecked (offset_ptr); break; 706 case 4: return GetU32_unchecked (offset_ptr); break; 707 case 8: return GetU64_unchecked (offset_ptr); break; 708 default: 709 assert("GetMax64 unhandled case!" == NULL); 710 break; 711 } 712 return 0; 713} 714 715int64_t 716DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const 717{ 718 switch (size) 719 { 720 case 1: return (int8_t)GetU8 (offset_ptr); break; 721 case 2: return (int16_t)GetU16(offset_ptr); break; 722 case 4: return (int32_t)GetU32(offset_ptr); break; 723 case 8: return (int64_t)GetU64(offset_ptr); break; 724 default: 725 assert("GetMax64 unhandled case!" == NULL); 726 break; 727 } 728 return 0; 729} 730 731uint64_t 732DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const 733{ 734 uint64_t uval64 = GetMaxU64 (offset_ptr, size); 735 if (bitfield_bit_size > 0) 736 { 737 if (bitfield_bit_offset > 0) 738 uval64 >>= bitfield_bit_offset; 739 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1); 740 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64) 741 return uval64; 742 uval64 &= bitfield_mask; 743 } 744 return uval64; 745} 746 747int64_t 748DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const 749{ 750 int64_t sval64 = GetMaxS64 (offset_ptr, size); 751 if (bitfield_bit_size > 0) 752 { 753 if (bitfield_bit_offset > 0) 754 sval64 >>= bitfield_bit_offset; 755 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1; 756 sval64 &= bitfield_mask; 757 // sign extend if needed 758 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1))) 759 sval64 |= ~bitfield_mask; 760 } 761 return sval64; 762} 763 764 765float 766DataExtractor::GetFloat (offset_t *offset_ptr) const 767{ 768 typedef float float_type; 769 float_type val = 0.0; 770 const size_t src_size = sizeof(float_type); 771 const float_type *src = (const float_type *)GetData (offset_ptr, src_size); 772 if (src) 773 { 774 if (m_byte_order != lldb::endian::InlHostByteOrder()) 775 { 776 const uint8_t *src_data = (const uint8_t *)src; 777 uint8_t *dst_data = (uint8_t *)&val; 778 for (size_t i=0; i<sizeof(float_type); ++i) 779 dst_data[sizeof(float_type) - 1 - i] = src_data[i]; 780 } 781 else 782 { 783 val = *src; 784 } 785 } 786 return val; 787} 788 789double 790DataExtractor::GetDouble (offset_t *offset_ptr) const 791{ 792 typedef double float_type; 793 float_type val = 0.0; 794 const size_t src_size = sizeof(float_type); 795 const float_type *src = (const float_type *)GetData (offset_ptr, src_size); 796 if (src) 797 { 798 if (m_byte_order != lldb::endian::InlHostByteOrder()) 799 { 800 const uint8_t *src_data = (const uint8_t *)src; 801 uint8_t *dst_data = (uint8_t *)&val; 802 for (size_t i=0; i<sizeof(float_type); ++i) 803 dst_data[sizeof(float_type) - 1 - i] = src_data[i]; 804 } 805 else 806 { 807 val = *src; 808 } 809 } 810 return val; 811} 812 813 814long double 815DataExtractor::GetLongDouble (offset_t *offset_ptr) const 816{ 817 long double val = 0.0; 818#if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64) 819 *offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), lldb::endian::InlHostByteOrder()); 820#else 821 *offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), lldb::endian::InlHostByteOrder()); 822#endif 823 return val; 824} 825 826 827//------------------------------------------------------------------ 828// Extract a single address from the data and update the offset 829// pointed to by "offset_ptr". The size of the extracted address 830// comes from the "this->m_addr_size" member variable and should be 831// set correctly prior to extracting any address values. 832// 833// RETURNS the address that was extracted, or zero on failure. 834//------------------------------------------------------------------ 835uint64_t 836DataExtractor::GetAddress (offset_t *offset_ptr) const 837{ 838 return GetMaxU64 (offset_ptr, m_addr_size); 839} 840 841uint64_t 842DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const 843{ 844 return GetMaxU64_unchecked (offset_ptr, m_addr_size); 845} 846 847//------------------------------------------------------------------ 848// Extract a single pointer from the data and update the offset 849// pointed to by "offset_ptr". The size of the extracted pointer 850// comes from the "this->m_addr_size" member variable and should be 851// set correctly prior to extracting any pointer values. 852// 853// RETURNS the pointer that was extracted, or zero on failure. 854//------------------------------------------------------------------ 855uint64_t 856DataExtractor::GetPointer (offset_t *offset_ptr) const 857{ 858 return GetMaxU64 (offset_ptr, m_addr_size); 859} 860 861//---------------------------------------------------------------------- 862// GetDwarfEHPtr 863// 864// Used for calls when the value type is specified by a DWARF EH Frame 865// pointer encoding. 866//---------------------------------------------------------------------- 867 868uint64_t 869DataExtractor::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 870{ 871 if (eh_ptr_enc == DW_EH_PE_omit) 872 return ULLONG_MAX; // Value isn't in the buffer... 873 874 uint64_t baseAddress = 0; 875 uint64_t addressValue = 0; 876 const uint32_t addr_size = GetAddressByteSize(); 877 878 bool signExtendValue = false; 879 // Decode the base part or adjust our offset 880 switch (eh_ptr_enc & 0x70) 881 { 882 case DW_EH_PE_pcrel: 883 signExtendValue = true; 884 baseAddress = *offset_ptr; 885 if (pc_rel_addr != LLDB_INVALID_ADDRESS) 886 baseAddress += pc_rel_addr; 887// else 888// Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address."); 889 break; 890 891 case DW_EH_PE_textrel: 892 signExtendValue = true; 893 if (text_addr != LLDB_INVALID_ADDRESS) 894 baseAddress = text_addr; 895// else 896// Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero."); 897 break; 898 899 case DW_EH_PE_datarel: 900 signExtendValue = true; 901 if (data_addr != LLDB_INVALID_ADDRESS) 902 baseAddress = data_addr; 903// else 904// Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero."); 905 break; 906 907 case DW_EH_PE_funcrel: 908 signExtendValue = true; 909 break; 910 911 case DW_EH_PE_aligned: 912 { 913 // SetPointerSize should be called prior to extracting these so the 914 // pointer size is cached 915 assert(addr_size != 0); 916 if (addr_size) 917 { 918 // Align to a address size boundary first 919 uint32_t alignOffset = *offset_ptr % addr_size; 920 if (alignOffset) 921 offset_ptr += addr_size - alignOffset; 922 } 923 } 924 break; 925 926 default: 927 break; 928 } 929 930 // Decode the value part 931 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) 932 { 933 case DW_EH_PE_absptr : 934 { 935 addressValue = GetAddress (offset_ptr); 936// if (data_relocs) 937// addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue); 938 } 939 break; 940 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break; 941 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break; 942 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break; 943 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break; 944 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break; 945 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break; 946 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break; 947 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break; 948 default: 949 // Unhandled encoding type 950 assert(eh_ptr_enc); 951 break; 952 } 953 954 // Since we promote everything to 64 bit, we may need to sign extend 955 if (signExtendValue && addr_size < sizeof(baseAddress)) 956 { 957 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull); 958 if (sign_bit & addressValue) 959 { 960 uint64_t mask = ~sign_bit + 1; 961 addressValue |= mask; 962 } 963 } 964 return baseAddress + addressValue; 965} 966 967size_t 968DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const 969{ 970 const uint8_t *src = PeekData (offset, length); 971 if (src) 972 { 973 if (dst_byte_order != GetByteOrder()) 974 { 975 // Validate that only a word- or register-sized dst is byte swapped 976 assert (length == 1 || length == 2 || length == 4 || length == 8 || 977 length == 10 || length == 16 || length == 32); 978 979 for (uint32_t i=0; i<length; ++i) 980 ((uint8_t*)dst)[i] = src[length - i - 1]; 981 } 982 else 983 ::memcpy (dst, src, length); 984 return length; 985 } 986 return 0; 987} 988 989// Extract data as it exists in target memory 990lldb::offset_t 991DataExtractor::CopyData (offset_t offset, 992 offset_t length, 993 void *dst) const 994{ 995 const uint8_t *src = PeekData (offset, length); 996 if (src) 997 { 998 ::memcpy (dst, src, length); 999 return length; 1000 } 1001 return 0; 1002} 1003 1004// Extract data and swap if needed when doing the copy 1005lldb::offset_t 1006DataExtractor::CopyByteOrderedData (offset_t src_offset, 1007 offset_t src_len, 1008 void *dst_void_ptr, 1009 offset_t dst_len, 1010 ByteOrder dst_byte_order) const 1011{ 1012 // Validate the source info 1013 if (!ValidOffsetForDataOfSize(src_offset, src_len)) 1014 assert (ValidOffsetForDataOfSize(src_offset, src_len)); 1015 assert (src_len > 0); 1016 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle); 1017 1018 // Validate the destination info 1019 assert (dst_void_ptr != NULL); 1020 assert (dst_len > 0); 1021 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle); 1022 1023 // Validate that only a word- or register-sized dst is byte swapped 1024 assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 || 1025 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 || 1026 dst_len == 32); 1027 1028 // Must have valid byte orders set in this object and for destination 1029 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) || 1030 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle)) 1031 return 0; 1032 1033 uint32_t i; 1034 uint8_t* dst = (uint8_t*)dst_void_ptr; 1035 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len); 1036 if (src) 1037 { 1038 if (dst_len >= src_len) 1039 { 1040 // We are copying the entire value from src into dst. 1041 // Calculate how many, if any, zeroes we need for the most 1042 // significant bytes if "dst_len" is greater than "src_len"... 1043 const size_t num_zeroes = dst_len - src_len; 1044 if (dst_byte_order == eByteOrderBig) 1045 { 1046 // Big endian, so we lead with zeroes... 1047 if (num_zeroes > 0) 1048 ::memset (dst, 0, num_zeroes); 1049 // Then either copy or swap the rest 1050 if (m_byte_order == eByteOrderBig) 1051 { 1052 ::memcpy (dst + num_zeroes, src, src_len); 1053 } 1054 else 1055 { 1056 for (i=0; i<src_len; ++i) 1057 dst[i+num_zeroes] = src[src_len - 1 - i]; 1058 } 1059 } 1060 else 1061 { 1062 // Little endian destination, so we lead the value bytes 1063 if (m_byte_order == eByteOrderBig) 1064 { 1065 for (i=0; i<src_len; ++i) 1066 dst[i] = src[src_len - 1 - i]; 1067 } 1068 else 1069 { 1070 ::memcpy (dst, src, src_len); 1071 } 1072 // And zero the rest... 1073 if (num_zeroes > 0) 1074 ::memset (dst + src_len, 0, num_zeroes); 1075 } 1076 return src_len; 1077 } 1078 else 1079 { 1080 // We are only copying some of the value from src into dst.. 1081 1082 if (dst_byte_order == eByteOrderBig) 1083 { 1084 // Big endian dst 1085 if (m_byte_order == eByteOrderBig) 1086 { 1087 // Big endian dst, with big endian src 1088 ::memcpy (dst, src + (src_len - dst_len), dst_len); 1089 } 1090 else 1091 { 1092 // Big endian dst, with little endian src 1093 for (i=0; i<dst_len; ++i) 1094 dst[i] = src[dst_len - 1 - i]; 1095 } 1096 } 1097 else 1098 { 1099 // Little endian dst 1100 if (m_byte_order == eByteOrderBig) 1101 { 1102 // Little endian dst, with big endian src 1103 for (i=0; i<dst_len; ++i) 1104 dst[i] = src[src_len - 1 - i]; 1105 } 1106 else 1107 { 1108 // Little endian dst, with big endian src 1109 ::memcpy (dst, src, dst_len); 1110 } 1111 } 1112 return dst_len; 1113 } 1114 1115 } 1116 return 0; 1117} 1118 1119 1120//---------------------------------------------------------------------- 1121// Extracts a variable length NULL terminated C string from 1122// the data at the offset pointed to by "offset_ptr". The 1123// "offset_ptr" will be updated with the offset of the byte that 1124// follows the NULL terminator byte. 1125// 1126// If the offset pointed to by "offset_ptr" is out of bounds, or if 1127// "length" is non-zero and there aren't enough avaialable 1128// bytes, NULL will be returned and "offset_ptr" will not be 1129// updated. 1130//---------------------------------------------------------------------- 1131const char* 1132DataExtractor::GetCStr (offset_t *offset_ptr) const 1133{ 1134 const char *cstr = (const char *)PeekData (*offset_ptr, 1); 1135 if (cstr) 1136 { 1137 const char *cstr_end = cstr; 1138 const char *end = (const char *)m_end; 1139 while (cstr_end < end && *cstr_end) 1140 ++cstr_end; 1141 1142 // Now we are either at the end of the data or we point to the 1143 // NULL C string terminator with cstr_end... 1144 if (*cstr_end == '\0') 1145 { 1146 // Advance the offset with one extra byte for the NULL terminator 1147 *offset_ptr += (cstr_end - cstr + 1); 1148 return cstr; 1149 } 1150 1151 // We reached the end of the data without finding a NULL C string 1152 // terminator. Fall through and return NULL otherwise anyone that 1153 // would have used the result as a C string can wonder into 1154 // unknown memory... 1155 } 1156 return NULL; 1157} 1158 1159//---------------------------------------------------------------------- 1160// Extracts a NULL terminated C string from the fixed length field of 1161// length "len" at the offset pointed to by "offset_ptr". 1162// The "offset_ptr" will be updated with the offset of the byte that 1163// follows the fixed length field. 1164// 1165// If the offset pointed to by "offset_ptr" is out of bounds, or if 1166// the offset plus the length of the field is out of bounds, or if the 1167// field does not contain a NULL terminator byte, NULL will be returned 1168// and "offset_ptr" will not be updated. 1169//---------------------------------------------------------------------- 1170const char* 1171DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const 1172{ 1173 const char *cstr = (const char *)PeekData (*offset_ptr, len); 1174 if (cstr) 1175 { 1176 if (memchr (cstr, '\0', len) == NULL) 1177 { 1178 return NULL; 1179 } 1180 *offset_ptr += len; 1181 return cstr; 1182 } 1183 return NULL; 1184} 1185 1186//------------------------------------------------------------------ 1187// Peeks at a string in the contained data. No verification is done 1188// to make sure the entire string lies within the bounds of this 1189// object's data, only "offset" is verified to be a valid offset. 1190// 1191// Returns a valid C string pointer if "offset" is a valid offset in 1192// this object's data, else NULL is returned. 1193//------------------------------------------------------------------ 1194const char * 1195DataExtractor::PeekCStr (offset_t offset) const 1196{ 1197 return (const char *)PeekData (offset, 1); 1198} 1199 1200//---------------------------------------------------------------------- 1201// Extracts an unsigned LEB128 number from this object's data 1202// starting at the offset pointed to by "offset_ptr". The offset 1203// pointed to by "offset_ptr" will be updated with the offset of the 1204// byte following the last extracted byte. 1205// 1206// Returned the extracted integer value. 1207//---------------------------------------------------------------------- 1208uint64_t 1209DataExtractor::GetULEB128 (offset_t *offset_ptr) const 1210{ 1211 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1); 1212 if (src == NULL) 1213 return 0; 1214 1215 const uint8_t *end = m_end; 1216 1217 if (src < end) 1218 { 1219 uint64_t result = *src++; 1220 if (result >= 0x80) 1221 { 1222 result &= 0x7f; 1223 int shift = 7; 1224 while (src < end) 1225 { 1226 uint8_t byte = *src++; 1227 result |= (byte & 0x7f) << shift; 1228 if ((byte & 0x80) == 0) 1229 break; 1230 shift += 7; 1231 } 1232 } 1233 *offset_ptr = src - m_start; 1234 return result; 1235 } 1236 1237 return 0; 1238} 1239 1240//---------------------------------------------------------------------- 1241// Extracts an signed LEB128 number from this object's data 1242// starting at the offset pointed to by "offset_ptr". The offset 1243// pointed to by "offset_ptr" will be updated with the offset of the 1244// byte following the last extracted byte. 1245// 1246// Returned the extracted integer value. 1247//---------------------------------------------------------------------- 1248int64_t 1249DataExtractor::GetSLEB128 (offset_t *offset_ptr) const 1250{ 1251 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1); 1252 if (src == NULL) 1253 return 0; 1254 1255 const uint8_t *end = m_end; 1256 1257 if (src < end) 1258 { 1259 int64_t result = 0; 1260 int shift = 0; 1261 int size = sizeof (int64_t) * 8; 1262 1263 uint8_t byte = 0; 1264 int bytecount = 0; 1265 1266 while (src < end) 1267 { 1268 bytecount++; 1269 byte = *src++; 1270 result |= (byte & 0x7f) << shift; 1271 shift += 7; 1272 if ((byte & 0x80) == 0) 1273 break; 1274 } 1275 1276 // Sign bit of byte is 2nd high order bit (0x40) 1277 if (shift < size && (byte & 0x40)) 1278 result |= - (1 << shift); 1279 1280 *offset_ptr += bytecount; 1281 return result; 1282 } 1283 return 0; 1284} 1285 1286//---------------------------------------------------------------------- 1287// Skips a ULEB128 number (signed or unsigned) from this object's 1288// data starting at the offset pointed to by "offset_ptr". The 1289// offset pointed to by "offset_ptr" will be updated with the offset 1290// of the byte following the last extracted byte. 1291// 1292// Returns the number of bytes consumed during the extraction. 1293//---------------------------------------------------------------------- 1294uint32_t 1295DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const 1296{ 1297 uint32_t bytes_consumed = 0; 1298 const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1); 1299 if (src == NULL) 1300 return 0; 1301 1302 const uint8_t *end = m_end; 1303 1304 if (src < end) 1305 { 1306 const uint8_t *src_pos = src; 1307 while ((src_pos < end) && (*src_pos++ & 0x80)) 1308 ++bytes_consumed; 1309 *offset_ptr += src_pos - src; 1310 } 1311 return bytes_consumed; 1312} 1313 1314static bool 1315GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result) 1316{ 1317 llvm::SmallVector<uint64_t, 2> uint64_array; 1318 lldb::offset_t bytes_left = byte_size; 1319 uint64_t u64; 1320 const lldb::ByteOrder byte_order = data.GetByteOrder(); 1321 if (byte_order == lldb::eByteOrderLittle) 1322 { 1323 while (bytes_left > 0) 1324 { 1325 if (bytes_left >= 8) 1326 { 1327 u64 = data.GetU64(offset_ptr); 1328 bytes_left -= 8; 1329 } 1330 else 1331 { 1332 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left); 1333 bytes_left = 0; 1334 } 1335 uint64_array.push_back(u64); 1336 } 1337 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array)); 1338 return true; 1339 } 1340 else if (byte_order == lldb::eByteOrderBig) 1341 { 1342 lldb::offset_t be_offset = *offset_ptr + byte_size; 1343 lldb::offset_t temp_offset; 1344 while (bytes_left > 0) 1345 { 1346 if (bytes_left >= 8) 1347 { 1348 be_offset -= 8; 1349 temp_offset = be_offset; 1350 u64 = data.GetU64(&temp_offset); 1351 bytes_left -= 8; 1352 } 1353 else 1354 { 1355 be_offset -= bytes_left; 1356 temp_offset = be_offset; 1357 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left); 1358 bytes_left = 0; 1359 } 1360 uint64_array.push_back(u64); 1361 } 1362 *offset_ptr += byte_size; 1363 result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array)); 1364 return true; 1365 } 1366 return false; 1367} 1368 1369static lldb::offset_t 1370DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix) 1371{ 1372 llvm::APInt apint; 1373 if (GetAPInt (data, &offset, byte_size, apint)) 1374 { 1375 std::string apint_str(apint.toString(radix, is_signed)); 1376 switch (radix) 1377 { 1378 case 2: 1379 s->Write ("0b", 2); 1380 break; 1381 case 8: 1382 s->Write ("0", 1); 1383 break; 1384 case 10: 1385 break; 1386 } 1387 s->Write(apint_str.c_str(), apint_str.size()); 1388 } 1389 return offset; 1390} 1391 1392static float half2float (uint16_t half) 1393{ 1394#ifdef _MSC_VER 1395 llvm_unreachable("half2float not implemented for MSVC"); 1396#else 1397 union{ float f; uint32_t u;}u; 1398 int32_t v = (int16_t) half; 1399 1400 if( 0 == (v & 0x7c00)) 1401 { 1402 u.u = v & 0x80007FFFU; 1403 return u.f * ldexpf(1, 125); 1404 } 1405 1406 v <<= 13; 1407 u.u = v | 0x70000000U; 1408 return u.f * ldexpf(1, -112); 1409#endif 1410} 1411 1412lldb::offset_t 1413DataExtractor::Dump (Stream *s, 1414 offset_t start_offset, 1415 lldb::Format item_format, 1416 size_t item_byte_size, 1417 size_t item_count, 1418 size_t num_per_line, 1419 uint64_t base_addr, 1420 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield 1421 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield 1422 ExecutionContextScope *exe_scope) const 1423{ 1424 if (s == NULL) 1425 return start_offset; 1426 1427 if (item_format == eFormatPointer) 1428 { 1429 if (item_byte_size != 4 && item_byte_size != 8) 1430 item_byte_size = s->GetAddressByteSize(); 1431 } 1432 1433 offset_t offset = start_offset; 1434 1435 if (item_format == eFormatInstruction) 1436 { 1437 TargetSP target_sp; 1438 if (exe_scope) 1439 target_sp = exe_scope->CalculateTarget(); 1440 if (target_sp) 1441 { 1442 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL, NULL)); 1443 if (disassembler_sp) 1444 { 1445 lldb::addr_t addr = base_addr + start_offset; 1446 lldb_private::Address so_addr; 1447 bool data_from_file = true; 1448 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) 1449 { 1450 data_from_file = false; 1451 } 1452 else 1453 { 1454 if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr)) 1455 so_addr.SetRawAddress(addr); 1456 } 1457 1458 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file); 1459 1460 if (bytes_consumed) 1461 { 1462 offset += bytes_consumed; 1463 const bool show_address = base_addr != LLDB_INVALID_ADDRESS; 1464 const bool show_bytes = true; 1465 ExecutionContext exe_ctx; 1466 exe_scope->CalculateExecutionContext(exe_ctx); 1467 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx); 1468 1469 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions. 1470 // I'll fix that but for now, just clear the list and it will go away nicely. 1471 disassembler_sp->GetInstructionList().Clear(); 1472 } 1473 } 1474 } 1475 else 1476 s->Printf ("invalid target"); 1477 1478 return offset; 1479 } 1480 1481 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8) 1482 item_format = eFormatHex; 1483 1484 lldb::offset_t line_start_offset = start_offset; 1485 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count) 1486 { 1487 if ((count % num_per_line) == 0) 1488 { 1489 if (count > 0) 1490 { 1491 if (item_format == eFormatBytesWithASCII && offset > line_start_offset) 1492 { 1493 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), ""); 1494 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, LLDB_INVALID_OFFSET, LLDB_INVALID_ADDRESS, 0, 0); 1495 } 1496 s->EOL(); 1497 } 1498 if (base_addr != LLDB_INVALID_ADDRESS) 1499 s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset))); 1500 line_start_offset = offset; 1501 } 1502 else 1503 if (item_format != eFormatChar && 1504 item_format != eFormatCharPrintable && 1505 item_format != eFormatCharArray && 1506 count > 0) 1507 { 1508 s->PutChar(' '); 1509 } 1510 1511 uint32_t i; 1512 switch (item_format) 1513 { 1514 case eFormatBoolean: 1515 if (item_byte_size <= 8) 1516 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false"); 1517 else 1518 { 1519 s->Printf("error: unsupported byte size (%zu) for boolean format", item_byte_size); 1520 return offset; 1521 } 1522 break; 1523 1524 case eFormatBinary: 1525 if (item_byte_size <= 8) 1526 { 1527 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1528 // Avoid std::bitset<64>::to_string() since it is missing in 1529 // earlier C++ libraries 1530 std::string binary_value(64, '0'); 1531 std::bitset<64> bits(uval64); 1532 for (i = 0; i < 64; ++i) 1533 if (bits[i]) 1534 binary_value[64 - 1 - i] = '1'; 1535 if (item_bit_size > 0) 1536 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size); 1537 else if (item_byte_size > 0 && item_byte_size <= 8) 1538 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8); 1539 } 1540 else 1541 { 1542 const bool is_signed = false; 1543 const unsigned radix = 2; 1544 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1545 } 1546 break; 1547 1548 case eFormatBytes: 1549 case eFormatBytesWithASCII: 1550 for (i=0; i<item_byte_size; ++i) 1551 { 1552 s->Printf ("%2.2x", GetU8(&offset)); 1553 } 1554 // Put an extra space between the groups of bytes if more than one 1555 // is being dumped in a group (item_byte_size is more than 1). 1556 if (item_byte_size > 1) 1557 s->PutChar(' '); 1558 break; 1559 1560 case eFormatChar: 1561 case eFormatCharPrintable: 1562 case eFormatCharArray: 1563 { 1564 // If we are only printing one character surround it with single 1565 // quotes 1566 if (item_count == 1 && item_format == eFormatChar) 1567 s->PutChar('\''); 1568 1569 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1570 if (isprint(ch)) 1571 s->Printf ("%c", (char)ch); 1572 else if (item_format != eFormatCharPrintable) 1573 { 1574 switch (ch) 1575 { 1576 case '\033': s->Printf ("\\e"); break; 1577 case '\a': s->Printf ("\\a"); break; 1578 case '\b': s->Printf ("\\b"); break; 1579 case '\f': s->Printf ("\\f"); break; 1580 case '\n': s->Printf ("\\n"); break; 1581 case '\r': s->Printf ("\\r"); break; 1582 case '\t': s->Printf ("\\t"); break; 1583 case '\v': s->Printf ("\\v"); break; 1584 case '\0': s->Printf ("\\0"); break; 1585 default: 1586 if (item_byte_size == 1) 1587 s->Printf ("\\x%2.2x", (uint8_t)ch); 1588 else 1589 s->Printf ("%" PRIu64, ch); 1590 break; 1591 } 1592 } 1593 else 1594 { 1595 s->PutChar(NON_PRINTABLE_CHAR); 1596 } 1597 1598 // If we are only printing one character surround it with single quotes 1599 if (item_count == 1 && item_format == eFormatChar) 1600 s->PutChar('\''); 1601 } 1602 break; 1603 1604 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type 1605 case eFormatDecimal: 1606 if (item_byte_size <= 8) 1607 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1608 else 1609 { 1610 const bool is_signed = true; 1611 const unsigned radix = 10; 1612 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1613 } 1614 break; 1615 1616 case eFormatUnsigned: 1617 if (item_byte_size <= 8) 1618 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1619 else 1620 { 1621 const bool is_signed = false; 1622 const unsigned radix = 10; 1623 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1624 } 1625 break; 1626 1627 case eFormatOctal: 1628 if (item_byte_size <= 8) 1629 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); 1630 else 1631 { 1632 const bool is_signed = false; 1633 const unsigned radix = 8; 1634 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix); 1635 } 1636 break; 1637 1638 case eFormatOSType: 1639 { 1640 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1641 s->PutChar('\''); 1642 for (i=0; i<item_byte_size; ++i) 1643 { 1644 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8)); 1645 if (isprint(ch)) 1646 s->Printf ("%c", ch); 1647 else 1648 { 1649 switch (ch) 1650 { 1651 case '\033': s->Printf ("\\e"); break; 1652 case '\a': s->Printf ("\\a"); break; 1653 case '\b': s->Printf ("\\b"); break; 1654 case '\f': s->Printf ("\\f"); break; 1655 case '\n': s->Printf ("\\n"); break; 1656 case '\r': s->Printf ("\\r"); break; 1657 case '\t': s->Printf ("\\t"); break; 1658 case '\v': s->Printf ("\\v"); break; 1659 case '\0': s->Printf ("\\0"); break; 1660 default: s->Printf ("\\x%2.2x", ch); break; 1661 } 1662 } 1663 } 1664 s->PutChar('\''); 1665 } 1666 break; 1667 1668 case eFormatCString: 1669 { 1670 const char *cstr = GetCStr(&offset); 1671 1672 if (!cstr) 1673 { 1674 s->Printf("NULL"); 1675 offset = LLDB_INVALID_OFFSET; 1676 } 1677 else 1678 { 1679 s->PutChar('\"'); 1680 1681 while (const char c = *cstr) 1682 { 1683 if (isprint(c)) 1684 { 1685 s->PutChar(c); 1686 } 1687 else 1688 { 1689 switch (c) 1690 { 1691 case '\033': s->Printf ("\\e"); break; 1692 case '\a': s->Printf ("\\a"); break; 1693 case '\b': s->Printf ("\\b"); break; 1694 case '\f': s->Printf ("\\f"); break; 1695 case '\n': s->Printf ("\\n"); break; 1696 case '\r': s->Printf ("\\r"); break; 1697 case '\t': s->Printf ("\\t"); break; 1698 case '\v': s->Printf ("\\v"); break; 1699 default: s->Printf ("\\x%2.2x", c); break; 1700 } 1701 } 1702 1703 ++cstr; 1704 } 1705 1706 s->PutChar('\"'); 1707 } 1708 } 1709 break; 1710 1711 1712 case eFormatPointer: 1713 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t)); 1714 break; 1715 1716 1717 case eFormatComplexInteger: 1718 { 1719 size_t complex_int_byte_size = item_byte_size / 2; 1720 1721 if (complex_int_byte_size <= 8) 1722 { 1723 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0)); 1724 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0)); 1725 } 1726 else 1727 { 1728 s->Printf("error: unsupported byte size (%zu) for complex integer format", item_byte_size); 1729 return offset; 1730 } 1731 } 1732 break; 1733 1734 case eFormatComplex: 1735 if (sizeof(float) * 2 == item_byte_size) 1736 { 1737 float f32_1 = GetFloat (&offset); 1738 float f32_2 = GetFloat (&offset); 1739 1740 s->Printf ("%g + %gi", f32_1, f32_2); 1741 break; 1742 } 1743 else if (sizeof(double) * 2 == item_byte_size) 1744 { 1745 double d64_1 = GetDouble (&offset); 1746 double d64_2 = GetDouble (&offset); 1747 1748 s->Printf ("%lg + %lgi", d64_1, d64_2); 1749 break; 1750 } 1751 else if (sizeof(long double) * 2 == item_byte_size) 1752 { 1753 long double ld64_1 = GetLongDouble (&offset); 1754 long double ld64_2 = GetLongDouble (&offset); 1755 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2); 1756 break; 1757 } 1758 else 1759 { 1760 s->Printf("error: unsupported byte size (%zu) for complex float format", item_byte_size); 1761 return offset; 1762 } 1763 break; 1764 1765 default: 1766 case eFormatDefault: 1767 case eFormatHex: 1768 case eFormatHexUppercase: 1769 { 1770 bool wantsuppercase = (item_format == eFormatHexUppercase); 1771 switch (item_byte_size) 1772 { 1773 case 1: 1774 case 2: 1775 case 4: 1776 case 8: 1777 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)); 1778 break; 1779 default: 1780 { 1781 assert (item_bit_size == 0 && item_bit_offset == 0); 1782 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size); 1783 if (bytes) 1784 { 1785 s->PutCString("0x"); 1786 uint32_t idx; 1787 if (m_byte_order == eByteOrderBig) 1788 { 1789 for (idx = 0; idx < item_byte_size; ++idx) 1790 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]); 1791 } 1792 else 1793 { 1794 for (idx = 0; idx < item_byte_size; ++idx) 1795 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]); 1796 } 1797 } 1798 } 1799 break; 1800 } 1801 } 1802 break; 1803 1804 case eFormatFloat: 1805 { 1806 TargetSP target_sp; 1807 bool used_apfloat = false; 1808 if (exe_scope) 1809 target_sp = exe_scope->CalculateTarget(); 1810 if (target_sp) 1811 { 1812 ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext(); 1813 if (clang_ast) 1814 { 1815 clang::ASTContext *ast = clang_ast->getASTContext(); 1816 if (ast) 1817 { 1818 llvm::SmallVector<char, 256> sv; 1819 // Show full precision when printing float values 1820 const unsigned format_precision = 0; 1821 const unsigned format_max_padding = 100; 1822 size_t item_bit_size = item_byte_size * 8; 1823 1824 if (item_bit_size == ast->getTypeSize(ast->FloatTy)) 1825 { 1826 llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size)); 1827 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint); 1828 apfloat.toString(sv, format_precision, format_max_padding); 1829 } 1830 else if (item_bit_size == ast->getTypeSize(ast->DoubleTy)) 1831 { 1832 llvm::APInt apint; 1833 if (GetAPInt (*this, &offset, item_byte_size, apint)) 1834 { 1835 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint); 1836 apfloat.toString(sv, format_precision, format_max_padding); 1837 } 1838 } 1839 else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy)) 1840 { 1841 llvm::APInt apint; 1842 switch (target_sp->GetArchitecture().GetCore()) 1843 { 1844 case ArchSpec::eCore_x86_32_i386: 1845 case ArchSpec::eCore_x86_32_i486: 1846 case ArchSpec::eCore_x86_32_i486sx: 1847 case ArchSpec::eCore_x86_64_x86_64: 1848 case ArchSpec::eCore_x86_64_x86_64h: 1849 // clang will assert when contructing the apfloat if we use a 16 byte integer value 1850 if (GetAPInt (*this, &offset, 10, apint)) 1851 { 1852 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint); 1853 apfloat.toString(sv, format_precision, format_max_padding); 1854 } 1855 break; 1856 1857 default: 1858 if (GetAPInt (*this, &offset, item_byte_size, apint)) 1859 { 1860 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->LongDoubleTy), apint); 1861 apfloat.toString(sv, format_precision, format_max_padding); 1862 } 1863 break; 1864 } 1865 } 1866 else if (item_bit_size == ast->getTypeSize(ast->HalfTy)) 1867 { 1868 llvm::APInt apint(item_bit_size, this->GetU16(&offset)); 1869 llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint); 1870 apfloat.toString(sv, format_precision, format_max_padding); 1871 } 1872 1873 if (!sv.empty()) 1874 { 1875 s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data()); 1876 used_apfloat = true; 1877 } 1878 } 1879 } 1880 } 1881 1882 if (!used_apfloat) 1883 { 1884 std::ostringstream ss; 1885 if (item_byte_size == sizeof(float) || item_byte_size == 2) 1886 { 1887 float f; 1888 if (item_byte_size == 2) 1889 { 1890 uint16_t half = this->GetU16(&offset); 1891 f = half2float(half); 1892 } 1893 else 1894 { 1895 f = GetFloat (&offset); 1896 } 1897 ss.precision(std::numeric_limits<float>::digits10); 1898 ss << f; 1899 } 1900 else if (item_byte_size == sizeof(double)) 1901 { 1902 ss.precision(std::numeric_limits<double>::digits10); 1903 ss << GetDouble(&offset); 1904 } 1905 else if (item_byte_size == sizeof(long double) || item_byte_size == 10) 1906 { 1907 ss.precision(std::numeric_limits<long double>::digits10); 1908 ss << GetLongDouble(&offset); 1909 } 1910 else 1911 { 1912 s->Printf("error: unsupported byte size (%zu) for float format", item_byte_size); 1913 return offset; 1914 } 1915 ss.flush(); 1916 s->Printf("%s", ss.str().c_str()); 1917 } 1918 } 1919 break; 1920 1921 case eFormatUnicode16: 1922 s->Printf("U+%4.4x", GetU16 (&offset)); 1923 break; 1924 1925 case eFormatUnicode32: 1926 s->Printf("U+0x%8.8x", GetU32 (&offset)); 1927 break; 1928 1929 case eFormatAddressInfo: 1930 { 1931 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); 1932 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr); 1933 if (exe_scope) 1934 { 1935 TargetSP target_sp (exe_scope->CalculateTarget()); 1936 lldb_private::Address so_addr; 1937 if (target_sp) 1938 { 1939 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) 1940 { 1941 s->PutChar(' '); 1942 so_addr.Dump (s, 1943 exe_scope, 1944 Address::DumpStyleResolvedDescription, 1945 Address::DumpStyleModuleWithFileAddress); 1946 } 1947 else 1948 { 1949 so_addr.SetOffset(addr); 1950 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription); 1951 } 1952 } 1953 } 1954 } 1955 break; 1956 1957 case eFormatHexFloat: 1958 if (sizeof(float) == item_byte_size) 1959 { 1960 char float_cstr[256]; 1961 llvm::APFloat ap_float (GetFloat (&offset)); 1962 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven); 1963 s->Printf ("%s", float_cstr); 1964 break; 1965 } 1966 else if (sizeof(double) == item_byte_size) 1967 { 1968 char float_cstr[256]; 1969 llvm::APFloat ap_float (GetDouble (&offset)); 1970 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven); 1971 s->Printf ("%s", float_cstr); 1972 break; 1973 } 1974 else 1975 { 1976 s->Printf("error: unsupported byte size (%zu) for hex float format", item_byte_size); 1977 return offset; 1978 } 1979 break; 1980 1981// please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat 1982// if you fail to do so, users will start getting different outputs depending on internal 1983// implementation details they should not care about || 1984 case eFormatVectorOfChar: // || 1985 s->PutChar('{'); // \/ 1986 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); 1987 s->PutChar('}'); 1988 break; 1989 1990 case eFormatVectorOfSInt8: 1991 s->PutChar('{'); 1992 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); 1993 s->PutChar('}'); 1994 break; 1995 1996 case eFormatVectorOfUInt8: 1997 s->PutChar('{'); 1998 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); 1999 s->PutChar('}'); 2000 break; 2001 2002 case eFormatVectorOfSInt16: 2003 s->PutChar('{'); 2004 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); 2005 s->PutChar('}'); 2006 break; 2007 2008 case eFormatVectorOfUInt16: 2009 s->PutChar('{'); 2010 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); 2011 s->PutChar('}'); 2012 break; 2013 2014 case eFormatVectorOfSInt32: 2015 s->PutChar('{'); 2016 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); 2017 s->PutChar('}'); 2018 break; 2019 2020 case eFormatVectorOfUInt32: 2021 s->PutChar('{'); 2022 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); 2023 s->PutChar('}'); 2024 break; 2025 2026 case eFormatVectorOfSInt64: 2027 s->PutChar('{'); 2028 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); 2029 s->PutChar('}'); 2030 break; 2031 2032 case eFormatVectorOfUInt64: 2033 s->PutChar('{'); 2034 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); 2035 s->PutChar('}'); 2036 break; 2037 2038 case eFormatVectorOfFloat32: 2039 s->PutChar('{'); 2040 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0); 2041 s->PutChar('}'); 2042 break; 2043 2044 case eFormatVectorOfFloat64: 2045 s->PutChar('{'); 2046 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0); 2047 s->PutChar('}'); 2048 break; 2049 2050 case eFormatVectorOfUInt128: 2051 s->PutChar('{'); 2052 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0); 2053 s->PutChar('}'); 2054 break; 2055 } 2056 } 2057 2058 if (item_format == eFormatBytesWithASCII && offset > line_start_offset) 2059 { 2060 s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), ""); 2061 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, LLDB_INVALID_OFFSET, LLDB_INVALID_ADDRESS, 0, 0); 2062 } 2063 return offset; // Return the offset at which we ended up 2064} 2065 2066//---------------------------------------------------------------------- 2067// Dumps bytes from this object's data to the stream "s" starting 2068// "start_offset" bytes into this data, and ending with the byte 2069// before "end_offset". "base_addr" will be added to the offset 2070// into the dumped data when showing the offset into the data in the 2071// output information. "num_per_line" objects of type "type" will 2072// be dumped with the option to override the format for each object 2073// with "type_format". "type_format" is a printf style formatting 2074// string. If "type_format" is NULL, then an appropriate format 2075// string will be used for the supplied "type". If the stream "s" 2076// is NULL, then the output will be send to Log(). 2077//---------------------------------------------------------------------- 2078lldb::offset_t 2079DataExtractor::PutToLog 2080( 2081 Log *log, 2082 offset_t start_offset, 2083 offset_t length, 2084 uint64_t base_addr, 2085 uint32_t num_per_line, 2086 DataExtractor::Type type, 2087 const char *format 2088) const 2089{ 2090 if (log == NULL) 2091 return start_offset; 2092 2093 offset_t offset; 2094 offset_t end_offset; 2095 uint32_t count; 2096 StreamString sstr; 2097 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count) 2098 { 2099 if ((count % num_per_line) == 0) 2100 { 2101 // Print out any previous string 2102 if (sstr.GetSize() > 0) 2103 { 2104 log->Printf("%s", sstr.GetData()); 2105 sstr.Clear(); 2106 } 2107 // Reset string offset and fill the current line string with address: 2108 if (base_addr != LLDB_INVALID_ADDRESS) 2109 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset))); 2110 } 2111 2112 switch (type) 2113 { 2114 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break; 2115 case TypeChar: 2116 { 2117 char ch = GetU8(&offset); 2118 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' '); 2119 } 2120 break; 2121 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break; 2122 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break; 2123 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break; 2124 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break; 2125 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break; 2126 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break; 2127 } 2128 } 2129 2130 if (sstr.GetSize() > 0) 2131 log->Printf("%s", sstr.GetData()); 2132 2133 return offset; // Return the offset at which we ended up 2134} 2135 2136//---------------------------------------------------------------------- 2137// DumpUUID 2138// 2139// Dump out a UUID starting at 'offset' bytes into the buffer 2140//---------------------------------------------------------------------- 2141void 2142DataExtractor::DumpUUID (Stream *s, offset_t offset) const 2143{ 2144 if (s) 2145 { 2146 const uint8_t *uuid_data = PeekData(offset, 16); 2147 if ( uuid_data ) 2148 { 2149 lldb_private::UUID uuid(uuid_data, 16); 2150 uuid.Dump(s); 2151 } 2152 else 2153 { 2154 s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset); 2155 } 2156 } 2157} 2158 2159void 2160DataExtractor::DumpHexBytes (Stream *s, 2161 const void *src, 2162 size_t src_len, 2163 uint32_t bytes_per_line, 2164 addr_t base_addr) 2165{ 2166 DataExtractor data (src, src_len, eByteOrderLittle, 4); 2167 data.Dump (s, 2168 0, // Offset into "src" 2169 eFormatBytes, // Dump as hex bytes 2170 1, // Size of each item is 1 for single bytes 2171 src_len, // Number of bytes 2172 bytes_per_line, // Num bytes per line 2173 base_addr, // Base address 2174 0, 0); // Bitfield info 2175} 2176 2177size_t 2178DataExtractor::Copy (DataExtractor &dest_data) const 2179{ 2180 if (m_data_sp.get()) 2181 { 2182 // we can pass along the SP to the data 2183 dest_data.SetData(m_data_sp); 2184 } 2185 else 2186 { 2187 const uint8_t *base_ptr = m_start; 2188 size_t data_size = GetByteSize(); 2189 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size))); 2190 } 2191 return GetByteSize(); 2192} 2193 2194bool 2195DataExtractor::Append(DataExtractor& rhs) 2196{ 2197 if (rhs.GetByteOrder() != GetByteOrder()) 2198 return false; 2199 2200 if (rhs.GetByteSize() == 0) 2201 return true; 2202 2203 if (GetByteSize() == 0) 2204 return (rhs.Copy(*this) > 0); 2205 2206 size_t bytes = GetByteSize() + rhs.GetByteSize(); 2207 2208 DataBufferHeap *buffer_heap_ptr = NULL; 2209 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); 2210 2211 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL) 2212 return false; 2213 2214 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes(); 2215 2216 memcpy(bytes_ptr, GetDataStart(), GetByteSize()); 2217 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize()); 2218 2219 SetData(buffer_sp); 2220 2221 return true; 2222} 2223 2224bool 2225DataExtractor::Append(void* buf, offset_t length) 2226{ 2227 if (buf == NULL) 2228 return false; 2229 2230 if (length == 0) 2231 return true; 2232 2233 size_t bytes = GetByteSize() + length; 2234 2235 DataBufferHeap *buffer_heap_ptr = NULL; 2236 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); 2237 2238 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL) 2239 return false; 2240 2241 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes(); 2242 2243 if (GetByteSize() > 0) 2244 memcpy(bytes_ptr, GetDataStart(), GetByteSize()); 2245 2246 memcpy(bytes_ptr + GetByteSize(), buf, length); 2247 2248 SetData(buffer_sp); 2249 2250 return true; 2251} 2252