DataExtractor.h revision 0fea0512e47d1820dd78de2748b874c81eea77d6
1//===-- DataExtractor.h -----------------------------------------*- 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#ifndef liblldb_DataExtractor_h_ 11#define liblldb_DataExtractor_h_ 12#if defined (__cplusplus) 13 14 15#include "lldb/lldb-private.h" 16#include <limits.h> 17#include <stdint.h> 18#include <string.h> 19 20namespace lldb_private { 21 22//---------------------------------------------------------------------- 23/// @class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h" 24/// @brief An data extractor class. 25/// 26/// DataExtractor is a class that can extract data (swapping if needed) 27/// from a data buffer. The data buffer can be caller owned, or can be 28/// shared data that can be shared between multiple DataExtractor 29/// instances. Multiple DataExtractor objects can share the same data, 30/// yet extract values in different address sizes and byte order modes. 31/// Each object can have a unique position in the shared data and extract 32/// data from different offsets. 33/// 34/// @see DataBuffer 35//---------------------------------------------------------------------- 36class DataExtractor 37{ 38public: 39 //------------------------------------------------------------------ 40 /// @typedef DataExtractor::Type 41 /// @brief Type enumerations used in the dump routines. 42 /// @see DataExtractor::Dump() 43 /// @see DataExtractor::DumpRawHexBytes() 44 //------------------------------------------------------------------ 45 typedef enum 46 { 47 TypeUInt8, ///< Format output as unsigned 8 bit integers 48 TypeChar, ///< Format output as characters 49 TypeUInt16, ///< Format output as unsigned 16 bit integers 50 TypeUInt32, ///< Format output as unsigned 32 bit integers 51 TypeUInt64, ///< Format output as unsigned 64 bit integers 52 TypePointer, ///< Format output as pointers 53 TypeULEB128, ///< Format output as ULEB128 numbers 54 TypeSLEB128 ///< Format output as SLEB128 numbers 55 } Type; 56 57 static void 58 DumpHexBytes (Stream *s, 59 const void *src, 60 size_t src_len, 61 uint32_t bytes_per_line, 62 lldb::addr_t base_addr); // Pass LLDB_INVALID_ADDRESS to not show address at start of line 63 //------------------------------------------------------------------ 64 /// Default constructor. 65 /// 66 /// Initialize all members to a default empty state. 67 //------------------------------------------------------------------ 68 DataExtractor (); 69 70 //------------------------------------------------------------------ 71 /// Construct with a buffer that is owned by the caller. 72 /// 73 /// This constructor allows us to use data that is owned by the 74 /// caller. The data must stay around as long as this object is 75 /// valid. 76 /// 77 /// @param[in] data 78 /// A pointer to caller owned data. 79 /// 80 /// @param[in] data_length 81 /// The length in bytes of \a data. 82 /// 83 /// @param[in] byte_order 84 /// A byte order of the data that we are extracting from. 85 /// 86 /// @param[in] addr_size 87 /// A new address byte size value. 88 //------------------------------------------------------------------ 89 DataExtractor (const void* data, uint32_t data_length, lldb::ByteOrder byte_order, uint8_t addr_size); 90 91 //------------------------------------------------------------------ 92 /// Construct with shared data. 93 /// 94 /// Copies the data shared pointer which adds a reference to the 95 /// contained in \a data_sp. The shared data reference is reference 96 /// counted to ensure the data lives as long as anyone still has a 97 /// valid shared pointer to the data in \a data_sp. 98 /// 99 /// @param[in] data_sp 100 /// A shared pointer to data. 101 /// 102 /// @param[in] byte_order 103 /// A byte order of the data that we are extracting from. 104 /// 105 /// @param[in] addr_size 106 /// A new address byte size value. 107 //------------------------------------------------------------------ 108 DataExtractor (const lldb::DataBufferSP& data_sp, lldb::ByteOrder byte_order, uint8_t addr_size); 109 110 //------------------------------------------------------------------ 111 /// Construct with a subset of \a data. 112 /// 113 /// Initialize this object with a subset of the data bytes in \a 114 /// data. If \a data contains shared data, then a reference to the 115 /// shared data will be added to ensure the shared data stays around 116 /// as long as any objects have references to the shared data. The 117 /// byte order value and the address size settings are copied from \a 118 /// data. If \a offset is not a valid offset in \a data, then no 119 /// reference to the shared data will be added. If there are not 120 /// \a length bytes available in \a data starting at \a offset, 121 /// the length will be truncated to contain as many bytes as 122 /// possible. 123 /// 124 /// @param[in] data 125 /// Another DataExtractor object that contains data. 126 /// 127 /// @param[in] offset 128 /// The offset into \a data at which the subset starts. 129 /// 130 /// @param[in] length 131 /// The length in bytes of the subset of data. 132 //------------------------------------------------------------------ 133 DataExtractor (const DataExtractor& data, uint32_t offset, uint32_t length); 134 135 DataExtractor (const DataExtractor& rhs); 136 //------------------------------------------------------------------ 137 /// Assignment operator. 138 /// 139 /// Copies all data, byte order and address size settings from \a rhs into 140 /// this object. If \a rhs contains shared data, a reference to that 141 /// shared data will be added. 142 /// 143 /// @param[in] rhs 144 /// Another DataExtractor object to copy. 145 /// 146 /// @return 147 /// A const reference to this object. 148 //------------------------------------------------------------------ 149 const DataExtractor& 150 operator= (const DataExtractor& rhs); 151 152 //------------------------------------------------------------------ 153 /// Destructor 154 /// 155 /// If this object contains a valid shared data reference, the 156 /// reference count on the data will be decremented, and if zero, 157 /// the data will be freed. 158 //------------------------------------------------------------------ 159 ~DataExtractor (); 160 161 //------------------------------------------------------------------ 162 /// Clears the object state. 163 /// 164 /// Clears the object contents back to a default invalid state, and 165 /// release any references to shared data that this object may 166 /// contain. 167 //------------------------------------------------------------------ 168 void 169 Clear (); 170 171 //------------------------------------------------------------------ 172 /// Dumps the binary data as \a type objects to stream \a s (or to 173 /// Log() if \a s is NULL) starting \a offset bytes into the data 174 /// and stopping after dumping \a length bytes. The offset into the 175 /// data is displayed at the beginning of each line and can be 176 /// offset by base address \a base_addr. \a num_per_line objects 177 /// will be displayed on each line. 178 /// 179 /// @param[in] s 180 /// The stream to dump the output to. If NULL the output will 181 /// be dumped to Log(). 182 /// 183 /// @param[in] offset 184 /// The offset into the data at which to start dumping. 185 /// 186 /// @param[in] length 187 /// The number of bytes to dump. 188 /// 189 /// @param[in] base_addr 190 /// The base address that gets added to the offset displayed on 191 /// each line. 192 /// 193 /// @param[in] num_per_line 194 /// The number of \a type objects to display on each line. 195 /// 196 /// @param[in] type 197 /// The type of objects to use when dumping data from this 198 /// object. See DataExtractor::Type. 199 /// 200 /// @param[in] type_format 201 /// The optional format to use for the \a type objects. If this 202 /// is NULL, the default format for the \a type will be used. 203 /// 204 /// @return 205 /// The offset at which dumping ended. 206 //------------------------------------------------------------------ 207 uint32_t 208 PutToLog (Log *log, 209 uint32_t offset, 210 uint32_t length, 211 uint64_t base_addr, 212 uint32_t num_per_line, 213 Type type, 214 const char *type_format = NULL) const; 215 216 //------------------------------------------------------------------ 217 /// Dumps \a item_count objects into the stream \a s. 218 /// 219 /// Dumps \a item_count objects using \a item_format, each of which 220 /// are \a item_byte_size bytes long starting at offset \a offset 221 /// bytes into the contained data, into the stream \a s. \a 222 /// num_per_line objects will be dumped on each line before a new 223 /// line will be output. If \a base_addr is a valid address, then 224 /// each new line of output will be prededed by the address value 225 /// plus appropriate offset, and a colon and space. Bitfield values 226 /// can be dumped by calling this function multiple times with the 227 /// same start offset, format and size, yet differing \a 228 /// item_bit_size and \a item_bit_offset values. 229 /// 230 /// @param[in] s 231 /// The stream to dump the output to. This value can not be NULL. 232 /// 233 /// @param[in] offset 234 /// The offset into the data at which to start dumping. 235 /// 236 /// @param[in] item_format 237 /// The format to use when dumping each item. 238 /// 239 /// @param[in] item_byte_size 240 /// The byte size of each item. 241 /// 242 /// @param[in] item_count 243 /// The number of items to dump. 244 /// 245 /// @param[in] num_per_line 246 /// The number of items to display on each line. 247 /// 248 /// @param[in] base_addr 249 /// The base address that gets added to the offset displayed on 250 /// each line if the value is valid. Is \a base_addr is 251 /// LLDB_INVALID_ADDRESS then no address values will be prepended 252 /// to any lines. 253 /// 254 /// @param[in] item_bit_size 255 /// If the value to display is a bitfield, this value should 256 /// be the number of bits that the bitfield item has within the 257 /// item's byte size value. This function will need to be called 258 /// multiple times with identical \a offset and \a item_byte_size 259 /// values in order to display multiple bitfield values that 260 /// exist within the same integer value. If the items being 261 /// displayed are not bitfields, this value should be zero. 262 /// 263 /// @param[in] item_bit_offset 264 /// If the value to display is a bitfield, this value should 265 /// be the offset in bits, or shift right amount, that the 266 /// bitfield item occupies within the item's byte size value. 267 /// This function will need to be called multiple times with 268 /// identical \a offset and \a item_byte_size values in order 269 /// to display multiple bitfield values that exist within the 270 /// same integer value. If the items being displayed are not 271 /// bitfields, this value should be zero. 272 /// 273 /// @return 274 /// The offset at which dumping ended. 275 //------------------------------------------------------------------ 276 uint32_t 277 Dump (Stream *s, 278 uint32_t offset, 279 lldb::Format item_format, 280 uint32_t item_byte_size, 281 uint32_t item_count, 282 uint32_t num_per_line, 283 uint64_t base_addr, 284 uint32_t item_bit_size, 285 uint32_t item_bit_offset, 286 ExecutionContextScope *exe_scope = NULL) const; 287 288 //------------------------------------------------------------------ 289 /// Dump a UUID value at \a offset. 290 /// 291 /// Dump a UUID starting at \a offset bytes into this object's data. 292 /// If the stream \a s is NULL, the output will be sent to Log(). 293 /// 294 /// @param[in] s 295 /// The stream to dump the output to. If NULL the output will 296 /// be dumped to Log(). 297 /// 298 /// @param[in] offset 299 /// The offset into the data at which to extract and dump a 300 /// UUID value. 301 //------------------------------------------------------------------ 302 void 303 DumpUUID (Stream *s, uint32_t offset) const; 304 305 //------------------------------------------------------------------ 306 /// Extract an arbitrary number of bytes in the specified byte 307 /// order. 308 /// 309 /// Attemps to extract \a length bytes starting at \a offset bytes 310 /// into this data in the requested byte order (\a dst_byte_order) 311 /// and place the results in \a dst. \a dst must be at least \a 312 /// length bytes long. 313 /// 314 /// @param[in] offset 315 /// The offset in bytes into the contained data at which to 316 /// start extracting. 317 /// 318 /// @param[in] length 319 /// The number of bytes to extract. 320 /// 321 /// @param[in] dst_byte_order 322 /// A byte order of the data that we want when the value in 323 /// copied to \a dst. 324 /// 325 /// @param[out] dst 326 /// The buffer that will receive the extracted value if there 327 /// are enough bytes available in the current data. 328 /// 329 /// @return 330 /// The number of bytes that were extracted which will be \a 331 /// length when the value is successfully extracted, or zero 332 /// if there aren't enough bytes at the specified offset. 333 //------------------------------------------------------------------ 334 size_t 335 ExtractBytes (uint32_t offset, uint32_t length, lldb::ByteOrder dst_byte_order, void *dst) const; 336 337 //------------------------------------------------------------------ 338 /// Extract an address from \a *offset_ptr. 339 /// 340 /// Extract a single address from the data and update the offset 341 /// pointed to by \a offset_ptr. The size of the extracted address 342 /// comes from the \a m_addr_size member variable and should be 343 /// set correctly prior to extracting any address values. 344 /// 345 /// @param[in,out] offset_ptr 346 /// A pointer to an offset within the data that will be advanced 347 /// by the appropriate number of bytes if the value is extracted 348 /// correctly. If the offset is out of bounds or there are not 349 /// enough bytes to extract this value, the offset will be left 350 /// unmodified. 351 /// 352 /// @return 353 /// The extracted address value. 354 //------------------------------------------------------------------ 355 uint64_t 356 GetAddress (uint32_t *offset_ptr) const; 357 358 uint64_t 359 GetAddress_unchecked (uint32_t *offset_ptr) const; 360 361 //------------------------------------------------------------------ 362 /// Get the current address size. 363 /// 364 /// Return the size in bytes of any address values this object will 365 /// extract. 366 /// 367 /// @return 368 /// The size in bytes of address values that will be extracted. 369 //------------------------------------------------------------------ 370 uint8_t 371 GetAddressByteSize () const 372 { 373 return m_addr_size; 374 } 375 376 //------------------------------------------------------------------ 377 /// Get the number of bytes contained in this object. 378 /// 379 /// @return 380 /// The total number of bytes of data this object refers to. 381 //------------------------------------------------------------------ 382 size_t 383 GetByteSize () const 384 { 385 return m_end - m_start; 386 } 387 388 //------------------------------------------------------------------ 389 /// Extract a C string from \a *offset_ptr. 390 /// 391 /// Returns a pointer to a C String from the data at the offset 392 /// pointed to by \a offset_ptr. A variable length NULL terminated C 393 /// string will be extracted and the \a offset_ptr will be 394 /// updated with the offset of the byte that follows the NULL 395 /// terminator byte. 396 /// 397 /// @param[in,out] offset_ptr 398 /// A pointer to an offset within the data that will be advanced 399 /// by the appropriate number of bytes if the value is extracted 400 /// correctly. If the offset is out of bounds or there are not 401 /// enough bytes to extract this value, the offset will be left 402 /// unmodified. 403 /// 404 /// @return 405 /// A pointer to the C string value in the data. If the offset 406 /// pointed to by \a offset_ptr is out of bounds, or if the 407 /// offset plus the length of the C string is out of bounds, 408 /// NULL will be returned. 409 //------------------------------------------------------------------ 410 const char * 411 GetCStr (uint32_t *offset_ptr) const; 412 413 //------------------------------------------------------------------ 414 /// Extract \a length bytes from \a *offset_ptr. 415 /// 416 /// Returns a pointer to a bytes in this object's data at the offset 417 /// pointed to by \a offset_ptr. If \a length is zero or too large, 418 /// then the offset pointed to by \a offset_ptr will not be updated 419 /// and NULL will be returned. 420 /// 421 /// @param[in,out] offset_ptr 422 /// A pointer to an offset within the data that will be advanced 423 /// by the appropriate number of bytes if the value is extracted 424 /// correctly. If the offset is out of bounds or there are not 425 /// enough bytes to extract this value, the offset will be left 426 /// unmodified. 427 /// 428 /// @param[in] length 429 /// The optional length of a string to extract. If the value is 430 /// zero, a NULL terminated C string will be extracted. 431 /// 432 /// @return 433 /// A pointer to the bytes in this object's data if the offset 434 /// and length are valid, or NULL otherwise. 435 //------------------------------------------------------------------ 436 const void* 437 GetData (uint32_t *offset_ptr, uint32_t length) const; 438 439 //------------------------------------------------------------------ 440 /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied 441 /// data is treated as a value that can be swapped to match the 442 /// specified byte order. 443 /// 444 /// For values that are larger than the supported integer sizes, 445 /// this function can be used to extract data in a specified byte 446 /// order. It can also be used to copy a smaller integer value from 447 /// to a larger value. The extra bytes left over will be padded 448 /// correctly according to the byte order of this object and the 449 /// \a dst_byte_order. This can be very handy when say copying a 450 /// partial data value into a register. 451 /// 452 /// @param[in] src_offset 453 /// The offset into this data from which to start copying an 454 /// endian entity 455 /// 456 /// @param[in] src_len 457 /// The length of the endian data to copy from this object 458 /// into the \a dst object 459 /// 460 /// @param[out] dst 461 /// The buffer where to place the endian data. The data might 462 /// need to be byte swapped (and appropriately padded with 463 /// zeroes if \a src_len != \a dst_len) if \a dst_byte_order 464 /// does not match the byte order in this object. 465 /// 466 /// @param[in] dst_len 467 /// The length number of bytes that the endian value will 468 /// occupy is \a dst. 469 /// 470 /// @param[in] byte_order 471 /// The byte order that the endian value should be in the \a dst 472 /// buffer. 473 /// 474 /// @return 475 /// Returns the number of bytes that were copied, or zero if 476 /// anything goes wrong. 477 //------------------------------------------------------------------ 478 uint32_t 479 CopyByteOrderedData (uint32_t src_offset, 480 uint32_t src_len, 481 void *dst, 482 uint32_t dst_len, 483 lldb::ByteOrder dst_byte_order) const; 484 485 //------------------------------------------------------------------ 486 /// Get the data end pointer. 487 /// 488 /// @return 489 /// Returns a pointer to the next byte contained in this 490 /// object's data, or NULL of there is no data in this object. 491 //------------------------------------------------------------------ 492 const uint8_t * 493 GetDataEnd () const 494 { 495 return m_end; 496 } 497 498 //------------------------------------------------------------------ 499 /// Get the shared data offset. 500 /// 501 /// Get the offset of the first byte of data in the shared data (if 502 /// any). 503 /// 504 /// @return 505 /// If this object contains shared data, this function returns 506 /// the offset in bytes into that shared data, zero otherwise. 507 //------------------------------------------------------------------ 508 size_t 509 GetSharedDataOffset () const; 510 511 //------------------------------------------------------------------ 512 /// Get a the data start pointer. 513 /// 514 /// @return 515 /// Returns a pointer to the first byte contained in this 516 /// object's data, or NULL of there is no data in this object. 517 //------------------------------------------------------------------ 518 const uint8_t * 519 GetDataStart () const 520 { 521 return m_start; 522 } 523 524 525 //------------------------------------------------------------------ 526 /// Extract a float from \a *offset_ptr. 527 /// 528 /// Extract a single float value. 529 /// 530 /// @param[in,out] offset_ptr 531 /// A pointer to an offset within the data that will be advanced 532 /// by the appropriate number of bytes if the value is extracted 533 /// correctly. If the offset is out of bounds or there are not 534 /// enough bytes to extract this value, the offset will be left 535 /// unmodified. 536 /// 537 /// @return 538 /// The floating value that was extracted, or zero on failure. 539 //------------------------------------------------------------------ 540 float 541 GetFloat (uint32_t *offset_ptr) const; 542 543 double 544 GetDouble (uint32_t *offset_ptr) const; 545 546 long double 547 GetLongDouble (uint32_t *offset_ptr) const; 548 549 //------------------------------------------------------------------ 550 /// Extract a GNU encoded pointer value from \a *offset_ptr. 551 /// 552 /// @param[in,out] offset_ptr 553 /// A pointer to an offset within the data that will be advanced 554 /// by the appropriate number of bytes if the value is extracted 555 /// correctly. If the offset is out of bounds or there are not 556 /// enough bytes to extract this value, the offset will be left 557 /// unmodified. 558 /// 559 /// @param[in] eh_ptr_enc 560 /// The GNU pointer encoding type. 561 /// 562 /// @param[in] pc_rel_addr 563 /// The PC relative address to use when the encoding is 564 /// \c DW_GNU_EH_PE_pcrel. 565 /// 566 /// @param[in] text_addr 567 /// The text (code) relative address to use when the encoding is 568 /// \c DW_GNU_EH_PE_textrel. 569 /// 570 /// @param[in] data_addr 571 /// The data relative address to use when the encoding is 572 /// \c DW_GNU_EH_PE_datarel. 573 /// 574 /// @return 575 /// The extracted GNU encoded pointer value. 576 //------------------------------------------------------------------ 577 uint64_t 578 GetGNUEHPointer (uint32_t *offset_ptr, uint32_t eh_ptr_enc, lldb::addr_t pc_rel_addr, lldb::addr_t text_addr, lldb::addr_t data_addr); 579 580 //------------------------------------------------------------------ 581 /// Extract an integer of size \a byte_size from \a *offset_ptr. 582 /// 583 /// Extract a single integer value and update the offset pointed to 584 /// by \a offset_ptr. The size of the extracted integer is specified 585 /// by the \a byte_size argument. \a byte_size should have a value 586 /// >= 1 and <= 4 since the return value is only 32 bits wide. Any 587 /// \a byte_size values less than 1 or greater than 4 will result in 588 /// nothing being extracted, and zero being returned. 589 /// 590 /// @param[in,out] offset_ptr 591 /// A pointer to an offset within the data that will be advanced 592 /// by the appropriate number of bytes if the value is extracted 593 /// correctly. If the offset is out of bounds or there are not 594 /// enough bytes to extract this value, the offset will be left 595 /// unmodified. 596 /// 597 /// @param[in] byte_size 598 /// The size in byte of the integer to extract. 599 /// 600 /// @return 601 /// The integer value that was extracted, or zero on failure. 602 //------------------------------------------------------------------ 603 uint32_t 604 GetMaxU32 (uint32_t *offset_ptr, uint32_t byte_size) const; 605 606 //------------------------------------------------------------------ 607 /// Extract an unsigned integer of size \a byte_size from \a 608 /// *offset_ptr. 609 /// 610 /// Extract a single unsigned integer value and update the offset 611 /// pointed to by \a offset_ptr. The size of the extracted integer 612 /// is specified by the \a byte_size argument. \a byte_size should 613 /// have a value greater than or equal to one and less than or equal 614 /// to eight since the return value is 64 bits wide. Any 615 /// \a byte_size values less than 1 or greater than 8 will result in 616 /// nothing being extracted, and zero being returned. 617 /// 618 /// @param[in,out] offset_ptr 619 /// A pointer to an offset within the data that will be advanced 620 /// by the appropriate number of bytes if the value is extracted 621 /// correctly. If the offset is out of bounds or there are not 622 /// enough bytes to extract this value, the offset will be left 623 /// unmodified. 624 /// 625 /// @param[in] byte_size 626 /// The size in byte of the integer to extract. 627 /// 628 /// @return 629 /// The unsigned integer value that was extracted, or zero on 630 /// failure. 631 //------------------------------------------------------------------ 632 uint64_t 633 GetMaxU64 (uint32_t *offset_ptr, uint32_t byte_size) const; 634 635 uint64_t 636 GetMaxU64_unchecked (uint32_t *offset_ptr, uint32_t byte_size) const; 637 638 //------------------------------------------------------------------ 639 /// Extract an signed integer of size \a byte_size from \a *offset_ptr. 640 /// 641 /// Extract a single signed integer value (sign extending if required) 642 /// and update the offset pointed to by \a offset_ptr. The size of 643 /// the extracted integer is specified by the \a byte_size argument. 644 /// \a byte_size should have a value greater than or equal to one 645 /// and less than or equal to eight since the return value is 64 646 /// bits wide. Any \a byte_size values less than 1 or greater than 647 /// 8 will result in nothing being extracted, and zero being returned. 648 /// 649 /// @param[in,out] offset_ptr 650 /// A pointer to an offset within the data that will be advanced 651 /// by the appropriate number of bytes if the value is extracted 652 /// correctly. If the offset is out of bounds or there are not 653 /// enough bytes to extract this value, the offset will be left 654 /// unmodified. 655 /// 656 /// @param[in] byte_size 657 /// The size in byte of the integer to extract. 658 /// 659 /// @return 660 /// The sign extended signed integer value that was extracted, 661 /// or zero on failure. 662 //------------------------------------------------------------------ 663 int64_t 664 GetMaxS64 (uint32_t *offset_ptr, uint32_t size) const; 665 666 //------------------------------------------------------------------ 667 /// Extract an unsigned integer of size \a byte_size from \a 668 /// *offset_ptr, then extract the bitfield from this value if 669 /// \a bitfield_bit_size is non-zero. 670 /// 671 /// Extract a single unsigned integer value and update the offset 672 /// pointed to by \a offset_ptr. The size of the extracted integer 673 /// is specified by the \a byte_size argument. \a byte_size should 674 /// have a value greater than or equal to one and less than or equal 675 /// to 8 since the return value is 64 bits wide. Any 676 /// \a byte_size values less than 1 or greater than 8 will result in 677 /// nothing being extracted, and zero being returned. 678 /// 679 /// @param[in,out] offset_ptr 680 /// A pointer to an offset within the data that will be advanced 681 /// by the appropriate number of bytes if the value is extracted 682 /// correctly. If the offset is out of bounds or there are not 683 /// enough bytes to extract this value, the offset will be left 684 /// unmodified. 685 /// 686 /// @param[in] byte_size 687 /// The size in byte of the integer to extract. 688 /// 689 /// @param[in] bitfield_bit_size 690 /// The size in bits of the bitfield value to extract, or zero 691 /// to just extract the entire integer value. 692 /// 693 /// @param[in] bitfield_bit_offset 694 /// The bit offset of the bitfield value in the extracted 695 /// integer (the number of bits to shift the integer to the 696 /// right). 697 /// 698 /// @return 699 /// The unsigned bitfield integer value that was extracted, or 700 /// zero on failure. 701 //------------------------------------------------------------------ 702 uint64_t 703 GetMaxU64Bitfield (uint32_t *offset_ptr, uint32_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const; 704 705 //------------------------------------------------------------------ 706 /// Extract an signed integer of size \a byte_size from \a 707 /// *offset_ptr, then extract and signe extend the bitfield from 708 /// this value if \a bitfield_bit_size is non-zero. 709 /// 710 /// Extract a single signed integer value (sign extending if required) 711 /// and update the offset pointed to by \a offset_ptr. The size of 712 /// the extracted integer is specified by the \a byte_size argument. 713 /// \a byte_size should have a value greater than or equal to one 714 /// and less than or equal to eight since the return value is 64 715 /// bits wide. Any \a byte_size values less than 1 or greater than 716 /// 8 will result in nothing being extracted, and zero being returned. 717 /// 718 /// @param[in,out] offset_ptr 719 /// A pointer to an offset within the data that will be advanced 720 /// by the appropriate number of bytes if the value is extracted 721 /// correctly. If the offset is out of bounds or there are not 722 /// enough bytes to extract this value, the offset will be left 723 /// unmodified. 724 /// 725 /// @param[in] byte_size 726 /// The size in bytes of the integer to extract. 727 /// 728 /// @param[in] bitfield_bit_size 729 /// The size in bits of the bitfield value to extract, or zero 730 /// to just extract the entire integer value. 731 /// 732 /// @param[in] bitfield_bit_offset 733 /// The bit offset of the bitfield value in the extracted 734 /// integer (the number of bits to shift the integer to the 735 /// right). 736 /// 737 /// @return 738 /// The signed bitfield integer value that was extracted, or 739 /// zero on failure. 740 //------------------------------------------------------------------ 741 int64_t 742 GetMaxS64Bitfield (uint32_t *offset_ptr, uint32_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const; 743 744 //------------------------------------------------------------------ 745 /// Extract an pointer from \a *offset_ptr. 746 /// 747 /// Extract a single pointer from the data and update the offset 748 /// pointed to by \a offset_ptr. The size of the extracted pointer 749 /// comes from the \a m_addr_size member variable and should be 750 /// set correctly prior to extracting any pointer values. 751 /// 752 /// @param[in,out] offset_ptr 753 /// A pointer to an offset within the data that will be advanced 754 /// by the appropriate number of bytes if the value is extracted 755 /// correctly. If the offset is out of bounds or there are not 756 /// enough bytes to extract this value, the offset will be left 757 /// unmodified. 758 /// 759 /// @return 760 /// The extracted pointer value as a 64 integer. 761 //------------------------------------------------------------------ 762 uint64_t 763 GetPointer (uint32_t *offset_ptr) const; 764 765 //------------------------------------------------------------------ 766 /// Get the current byte order value. 767 /// 768 /// @return 769 /// The current byte order value from this object's internal 770 /// state. 771 //------------------------------------------------------------------ 772 lldb::ByteOrder 773 GetByteOrder() const 774 { 775 return m_byte_order; 776 } 777 778 //------------------------------------------------------------------ 779 /// Extract a uint8_t value from \a *offset_ptr. 780 /// 781 /// Extract a single uint8_t from the binary data at the offset 782 /// pointed to by \a offset_ptr, and advance the offset on success. 783 /// 784 /// @param[in,out] offset_ptr 785 /// A pointer to an offset within the data that will be advanced 786 /// by the appropriate number of bytes if the value is extracted 787 /// correctly. If the offset is out of bounds or there are not 788 /// enough bytes to extract this value, the offset will be left 789 /// unmodified. 790 /// 791 /// @return 792 /// The extracted uint8_t value. 793 //------------------------------------------------------------------ 794 uint8_t 795 GetU8 ( uint32_t *offset_ptr) const; 796 797 uint8_t 798 GetU8_unchecked (uint32_t *offset_ptr) const 799 { 800 uint8_t val = m_start[*offset_ptr]; 801 *offset_ptr += 1; 802 return val; 803 } 804 805 uint16_t 806 GetU16_unchecked (uint32_t *offset_ptr) const; 807 808 uint32_t 809 GetU32_unchecked (uint32_t *offset_ptr) const; 810 811 uint64_t 812 GetU64_unchecked (uint32_t *offset_ptr) const; 813 //------------------------------------------------------------------ 814 /// Extract \a count uint8_t values from \a *offset_ptr. 815 /// 816 /// Extract \a count uint8_t values from the binary data at the 817 /// offset pointed to by \a offset_ptr, and advance the offset on 818 /// success. The extracted values are copied into \a dst. 819 /// 820 /// @param[in,out] offset_ptr 821 /// A pointer to an offset within the data that will be advanced 822 /// by the appropriate number of bytes if the value is extracted 823 /// correctly. If the offset is out of bounds or there are not 824 /// enough bytes to extract this value, the offset will be left 825 /// unmodified. 826 /// 827 /// @param[out] dst 828 /// A buffer to copy \a count uint8_t values into. \a dst must 829 /// be large enough to hold all requested data. 830 /// 831 /// @param[in] count 832 /// The number of uint8_t values to extract. 833 /// 834 /// @return 835 /// \a dst if all values were properly extracted and copied, 836 /// NULL otherise. 837 //------------------------------------------------------------------ 838 void * 839 GetU8 ( uint32_t *offset_ptr, void *dst, uint32_t count) const; 840 841 //------------------------------------------------------------------ 842 /// Extract a uint16_t value from \a *offset_ptr. 843 /// 844 /// Extract a single uint16_t from the binary data at the offset 845 /// pointed to by \a offset_ptr, and update the offset on success. 846 /// 847 /// @param[in,out] offset_ptr 848 /// A pointer to an offset within the data that will be advanced 849 /// by the appropriate number of bytes if the value is extracted 850 /// correctly. If the offset is out of bounds or there are not 851 /// enough bytes to extract this value, the offset will be left 852 /// unmodified. 853 /// 854 /// @return 855 /// The extracted uint16_t value. 856 //------------------------------------------------------------------ 857 uint16_t 858 GetU16 (uint32_t *offset_ptr) const; 859 860 //------------------------------------------------------------------ 861 /// Extract \a count uint16_t values from \a *offset_ptr. 862 /// 863 /// Extract \a count uint16_t values from the binary data at the 864 /// offset pointed to by \a offset_ptr, and advance the offset on 865 /// success. The extracted values are copied into \a dst. 866 /// 867 /// @param[in,out] offset_ptr 868 /// A pointer to an offset within the data that will be advanced 869 /// by the appropriate number of bytes if the value is extracted 870 /// correctly. If the offset is out of bounds or there are not 871 /// enough bytes to extract this value, the offset will be left 872 /// unmodified. 873 /// 874 /// @param[out] dst 875 /// A buffer to copy \a count uint16_t values into. \a dst must 876 /// be large enough to hold all requested data. 877 /// 878 /// @param[in] count 879 /// The number of uint16_t values to extract. 880 /// 881 /// @return 882 /// \a dst if all values were properly extracted and copied, 883 /// NULL otherise. 884 //------------------------------------------------------------------ 885 void * 886 GetU16 (uint32_t *offset_ptr, void *dst, uint32_t count) const; 887 888 //------------------------------------------------------------------ 889 /// Extract a uint32_t value from \a *offset_ptr. 890 /// 891 /// Extract a single uint32_t from the binary data at the offset 892 /// pointed to by \a offset_ptr, and update the offset on success. 893 /// 894 /// @param[in,out] offset_ptr 895 /// A pointer to an offset within the data that will be advanced 896 /// by the appropriate number of bytes if the value is extracted 897 /// correctly. If the offset is out of bounds or there are not 898 /// enough bytes to extract this value, the offset will be left 899 /// unmodified. 900 /// 901 /// @return 902 /// The extracted uint32_t value. 903 //------------------------------------------------------------------ 904 uint32_t 905 GetU32 (uint32_t *offset_ptr) const; 906 907 //------------------------------------------------------------------ 908 /// Extract \a count uint32_t values from \a *offset_ptr. 909 /// 910 /// Extract \a count uint32_t values from the binary data at the 911 /// offset pointed to by \a offset_ptr, and advance the offset on 912 /// success. The extracted values are copied into \a dst. 913 /// 914 /// @param[in,out] offset_ptr 915 /// A pointer to an offset within the data that will be advanced 916 /// by the appropriate number of bytes if the value is extracted 917 /// correctly. If the offset is out of bounds or there are not 918 /// enough bytes to extract this value, the offset will be left 919 /// unmodified. 920 /// 921 /// @param[out] dst 922 /// A buffer to copy \a count uint32_t values into. \a dst must 923 /// be large enough to hold all requested data. 924 /// 925 /// @param[in] count 926 /// The number of uint32_t values to extract. 927 /// 928 /// @return 929 /// \a dst if all values were properly extracted and copied, 930 /// NULL otherise. 931 //------------------------------------------------------------------ 932 void * 933 GetU32 (uint32_t *offset_ptr, void *dst, uint32_t count) const; 934 935 //------------------------------------------------------------------ 936 /// Extract a uint64_t value from \a *offset_ptr. 937 /// 938 /// Extract a single uint64_t from the binary data at the offset 939 /// pointed to by \a offset_ptr, and update the offset on success. 940 /// 941 /// @param[in,out] offset_ptr 942 /// A pointer to an offset within the data that will be advanced 943 /// by the appropriate number of bytes if the value is extracted 944 /// correctly. If the offset is out of bounds or there are not 945 /// enough bytes to extract this value, the offset will be left 946 /// unmodified. 947 /// 948 /// @return 949 /// The extracted uint64_t value. 950 //------------------------------------------------------------------ 951 uint64_t 952 GetU64 (uint32_t *offset_ptr) const; 953 954 //------------------------------------------------------------------ 955 /// Extract \a count uint64_t values from \a *offset_ptr. 956 /// 957 /// Extract \a count uint64_t values from the binary data at the 958 /// offset pointed to by \a offset_ptr, and advance the offset on 959 /// success. The extracted values are copied into \a dst. 960 /// 961 /// @param[in,out] offset_ptr 962 /// A pointer to an offset within the data that will be advanced 963 /// by the appropriate number of bytes if the value is extracted 964 /// correctly. If the offset is out of bounds or there are not 965 /// enough bytes to extract this value, the offset will be left 966 /// unmodified. 967 /// 968 /// @param[out] dst 969 /// A buffer to copy \a count uint64_t values into. \a dst must 970 /// be large enough to hold all requested data. 971 /// 972 /// @param[in] count 973 /// The number of uint64_t values to extract. 974 /// 975 /// @return 976 /// \a dst if all values were properly extracted and copied, 977 /// NULL otherise. 978 //------------------------------------------------------------------ 979 void * 980 GetU64 ( uint32_t *offset_ptr, void *dst, uint32_t count) const; 981 982 //------------------------------------------------------------------ 983 /// Extract a signed LEB128 value from \a *offset_ptr. 984 /// 985 /// Extracts an signed LEB128 number from this object's data 986 /// starting at the offset pointed to by \a offset_ptr. The offset 987 /// pointed to by \a offset_ptr will be updated with the offset of 988 /// the byte following the last extracted byte. 989 /// 990 /// @param[in,out] offset_ptr 991 /// A pointer to an offset within the data that will be advanced 992 /// by the appropriate number of bytes if the value is extracted 993 /// correctly. If the offset is out of bounds or there are not 994 /// enough bytes to extract this value, the offset will be left 995 /// unmodified. 996 /// 997 /// @return 998 /// The extracted signed integer value. 999 //------------------------------------------------------------------ 1000 int64_t 1001 GetSLEB128 (uint32_t *offset_ptr) const; 1002 1003 //------------------------------------------------------------------ 1004 /// Extract a unsigned LEB128 value from \a *offset_ptr. 1005 /// 1006 /// Extracts an unsigned LEB128 number from this object's data 1007 /// starting at the offset pointed to by \a offset_ptr. The offset 1008 /// pointed to by \a offset_ptr will be updated with the offset of 1009 /// the byte following the last extracted byte. 1010 /// 1011 /// @param[in,out] offset_ptr 1012 /// A pointer to an offset within the data that will be advanced 1013 /// by the appropriate number of bytes if the value is extracted 1014 /// correctly. If the offset is out of bounds or there are not 1015 /// enough bytes to extract this value, the offset will be left 1016 /// unmodified. 1017 /// 1018 /// @return 1019 /// The extracted unsigned integer value. 1020 //------------------------------------------------------------------ 1021 uint64_t 1022 GetULEB128 (uint32_t *offset_ptr) const; 1023 1024 lldb::DataBufferSP & 1025 GetSharedDataBuffer () 1026 { 1027 return m_data_sp; 1028 } 1029 1030 //------------------------------------------------------------------ 1031 /// Peek at a C string at \a offset. 1032 /// 1033 /// Peeks at a string in the contained data. No verification is done 1034 /// to make sure the entire string lies within the bounds of this 1035 /// object's data, only \a offset is verified to be a valid offset. 1036 /// 1037 /// @param[in] offset 1038 /// An offset into the data. 1039 /// 1040 /// @return 1041 /// A non-NULL C string pointer if \a offset is a valid offset, 1042 /// NULL otherwise. 1043 //------------------------------------------------------------------ 1044 const char * 1045 PeekCStr (uint32_t offset) const; 1046 1047 //------------------------------------------------------------------ 1048 /// Peek at a bytes at \a offset. 1049 /// 1050 /// Returns a pointer to \a length bytes at \a offset as long as 1051 /// there are \a length bytes available starting at \a offset. 1052 /// 1053 /// @return 1054 /// A non-NULL data pointer if \a offset is a valid offset and 1055 /// there are \a length bytes available at that offset, NULL 1056 /// otherwise. 1057 //------------------------------------------------------------------ 1058 const uint8_t* 1059 PeekData (uint32_t offset, uint32_t length) const; 1060 1061 //------------------------------------------------------------------ 1062 /// Set the address byte size. 1063 /// 1064 /// Set the size in bytes that will be used when extracting any 1065 /// address and pointer values from data contained in this object. 1066 /// 1067 /// @param[in] addr_size 1068 /// The size in bytes to use when extracting addresses. 1069 //------------------------------------------------------------------ 1070 void 1071 SetAddressByteSize (uint8_t addr_size) 1072 { 1073 m_addr_size = addr_size; 1074 } 1075 1076 //------------------------------------------------------------------ 1077 /// Set data with a buffer that is caller owned. 1078 /// 1079 /// Use data that is owned by the caller when extracting values. 1080 /// The data must stay around as long as this object, or any object 1081 /// that copies a subset of this object's data, is valid. If \a 1082 /// bytes is NULL, or \a length is zero, this object will contain 1083 /// no data. 1084 /// 1085 /// @param[in] bytes 1086 /// A pointer to caller owned data. 1087 /// 1088 /// @param[in] length 1089 /// The length in bytes of \a bytes. 1090 /// 1091 /// @param[in] byte_order 1092 /// A byte order of the data that we are extracting from. 1093 /// 1094 /// @return 1095 /// The number of bytes that this object now contains. 1096 //------------------------------------------------------------------ 1097 uint32_t 1098 SetData (const void *bytes, uint32_t length, lldb::ByteOrder byte_order); 1099 1100 //------------------------------------------------------------------ 1101 /// Adopt a subset of \a data. 1102 /// 1103 /// Set this object's data to be a subset of the data bytes in \a 1104 /// data. If \a data contains shared data, then a reference to the 1105 /// shared data will be added to ensure the shared data stays around 1106 /// as long as any objects have references to the shared data. The 1107 /// byte order and the address size settings are copied from \a 1108 /// data. If \a offset is not a valid offset in \a data, then no 1109 /// reference to the shared data will be added. If there are not 1110 /// \a length bytes available in \a data starting at \a offset, 1111 /// the length will be truncated to contains as many bytes as 1112 /// possible. 1113 /// 1114 /// @param[in] data 1115 /// Another DataExtractor object that contains data. 1116 /// 1117 /// @param[in] offset 1118 /// The offset into \a data at which the subset starts. 1119 /// 1120 /// @param[in] length 1121 /// The length in bytes of the subset of \a data. 1122 /// 1123 /// @return 1124 /// The number of bytes that this object now contains. 1125 //------------------------------------------------------------------ 1126 uint32_t 1127 SetData (const DataExtractor& data, uint32_t offset, uint32_t length); 1128 1129 //------------------------------------------------------------------ 1130 /// Adopt a subset of shared data in \a data_sp. 1131 /// 1132 /// Copies the data shared pointer which adds a reference to the 1133 /// contained in \a data_sp. The shared data reference is reference 1134 /// counted to ensure the data lives as long as anyone still has a 1135 /// valid shared pointer to the data in \a data_sp. The byte order 1136 /// and address byte size settings remain the same. If 1137 /// \a offset is not a valid offset in \a data_sp, then no reference 1138 /// to the shared data will be added. If there are not \a length 1139 /// bytes available in \a data starting at \a offset, the length 1140 /// will be truncated to contains as many bytes as possible. 1141 /// 1142 /// @param[in] data_sp 1143 /// A shared pointer to data. 1144 /// 1145 /// @param[in] offset 1146 /// The offset into \a data_sp at which the subset starts. 1147 /// 1148 /// @param[in] length 1149 /// The length in bytes of the subset of \a data_sp. 1150 /// 1151 /// @return 1152 /// The number of bytes that this object now contains. 1153 //------------------------------------------------------------------ 1154 uint32_t 1155 SetData (const lldb::DataBufferSP& data_sp, uint32_t offset = 0, uint32_t length = UINT32_MAX); 1156 1157 //------------------------------------------------------------------ 1158 /// Set the byte_order value. 1159 /// 1160 /// Sets the byte order of the data to extract. Extracted values 1161 /// will be swapped if necessary when decoding. 1162 /// 1163 /// @param[in] byte_order 1164 /// The byte order value to use when extracting data. 1165 //------------------------------------------------------------------ 1166 void 1167 SetByteOrder (lldb::ByteOrder byte_order) 1168 { 1169 m_byte_order = byte_order; 1170 } 1171 1172 //------------------------------------------------------------------ 1173 /// Skip an LEB128 number at \a *offset_ptr. 1174 /// 1175 /// Skips a LEB128 number (signed or unsigned) from this object's 1176 /// data starting at the offset pointed to by \a offset_ptr. The 1177 /// offset pointed to by \a offset_ptr will be updated with the 1178 /// offset of the byte following the last extracted byte. 1179 /// 1180 /// @param[in,out] offset_ptr 1181 /// A pointer to an offset within the data that will be advanced 1182 /// by the appropriate number of bytes if the value is extracted 1183 /// correctly. If the offset is out of bounds or there are not 1184 /// enough bytes to extract this value, the offset will be left 1185 /// unmodified. 1186 /// 1187 /// @return 1188 // The number of bytes consumed during the extraction. 1189 //------------------------------------------------------------------ 1190 uint32_t 1191 Skip_LEB128 (uint32_t *offset_ptr) const; 1192 1193 //------------------------------------------------------------------ 1194 /// Test the validity of \a offset. 1195 /// 1196 /// @return 1197 /// \b true if \a offset is a valid offset into the data in this 1198 /// object, \b false otherwise. 1199 //------------------------------------------------------------------ 1200 bool 1201 ValidOffset (uint32_t offset) const 1202 { 1203 return offset < GetByteSize(); 1204 } 1205 1206 //------------------------------------------------------------------ 1207 /// Test the availability of \a length bytes of data from \a offset. 1208 /// 1209 /// @return 1210 /// \b true if \a offset is a valid offset and there are \a 1211 /// length bytes available at that offset, \b false otherwise. 1212 //------------------------------------------------------------------ 1213 bool 1214 ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const; 1215 1216 size_t 1217 Copy (DataExtractor& dest_data) const; 1218 1219 bool 1220 Append (DataExtractor& rhs); 1221 1222 bool 1223 Append (void* bytes, uint32_t length); 1224 1225protected: 1226 //------------------------------------------------------------------ 1227 // Member variables 1228 //------------------------------------------------------------------ 1229 const uint8_t * m_start; ///< A pointer to the first byte of data. 1230 const uint8_t * m_end; ///< A pointer to the byte that is past the end of the data. 1231 lldb::ByteOrder m_byte_order; ///< The byte order of the data we are extracting from. 1232 uint8_t m_addr_size; ///< The address size to use when extracting pointers or addresses 1233 mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can be shared among multilple instances 1234}; 1235 1236} // namespace lldb_private 1237 1238#endif // #if defined (__cplusplus) 1239#endif // #ifndef liblldb_DataExtractor_h_ 1240