LLDB mainline
DataExtractor.h
Go to the documentation of this file.
1//===-- DataExtractor.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_UTILITY_DATAEXTRACTOR_H
10#define LLDB_UTILITY_DATAEXTRACTOR_H
11
13#include "lldb/Utility/Endian.h"
14#include "lldb/lldb-defines.h"
16#include "lldb/lldb-forward.h"
17#include "lldb/lldb-types.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/Support/DataExtractor.h"
20#include "llvm/Support/SwapByteOrder.h"
21
22#include <cassert>
23#include <cstdint>
24#include <cstring>
25
26namespace lldb_private {
27class Log;
28class Stream;
29}
30namespace llvm {
31template <typename T> class SmallVectorImpl;
32}
33
34
35namespace lldb_private {
36
37/// \class DataExtractor DataExtractor.h "lldb/Core/DataExtractor.h" An data
38/// extractor class.
39///
40/// DataExtractor is a class that can extract data (swapping if needed) from a
41/// data buffer. The data buffer can be caller owned, or can be shared data
42/// that can be shared between multiple DataExtractor instances. Multiple
43/// DataExtractor objects can share the same data, yet extract values in
44/// different address sizes and byte order modes. Each object can have a
45/// unique position in the shared data and extract data from different
46/// offsets.
47///
48/// \see DataBuffer
50public:
51 /// \typedef DataExtractor::Type
52 /// Type enumerations used in the dump routines.
53 enum Type {
54 TypeUInt8, ///< Format output as unsigned 8 bit integers
55 TypeChar, ///< Format output as characters
56 TypeUInt16, ///< Format output as unsigned 16 bit integers
57 TypeUInt32, ///< Format output as unsigned 32 bit integers
58 TypeUInt64, ///< Format output as unsigned 64 bit integers
59 TypePointer, ///< Format output as pointers
60 TypeULEB128, ///< Format output as ULEB128 numbers
61 TypeSLEB128 ///< Format output as SLEB128 numbers
62 };
63
64 /// Default constructor.
65 ///
66 /// Initialize all members to a default empty state.
68
69 /// Construct with a buffer that is owned by the caller.
70 ///
71 /// This constructor allows us to use data that is owned by the caller. The
72 /// data must stay around as long as this object is valid.
73 ///
74 /// \param[in] data
75 /// A pointer to caller owned data.
76 ///
77 /// \param[in] data_length
78 /// The length in bytes of \a data.
79 ///
80 /// \param[in] byte_order
81 /// A byte order of the data that we are extracting from.
82 ///
83 /// \param[in] addr_size
84 /// A new address byte size value.
85 ///
86 /// \param[in] target_byte_size
87 /// A size of a target byte in 8-bit host bytes
88 DataExtractor(const void *data, lldb::offset_t data_length,
89 lldb::ByteOrder byte_order, uint32_t addr_size,
90 uint32_t target_byte_size = 1);
91
92 /// Construct with shared data.
93 ///
94 /// Copies the data shared pointer which adds a reference to the data
95 /// contained in \a data_sp. The shared data reference is reference counted to
96 /// ensure the data lives as long as anyone still has a valid shared pointer
97 /// 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 /// \param[in] target_byte_size
109 /// A size of a target byte in 8-bit host bytes
110 DataExtractor(const lldb::DataBufferSP &data_sp, lldb::ByteOrder byte_order,
111 uint32_t addr_size, uint32_t target_byte_size = 1);
112
113 /// Construct with shared data, but byte-order & addr-size are unspecified.
114 ///
115 /// Copies the data shared pointer which adds a reference to the data
116 /// contained in \a data_sp. The shared data reference is reference counted to
117 /// ensure the data lives as long as anyone still has a valid shared pointer
118 /// to the data in \a data_sp.
119 ///
120 /// \param[in] data_sp
121 /// A shared pointer to data.
122 DataExtractor(const lldb::DataBufferSP &data_sp,
123 uint32_t target_byte_size = 1);
124
125 /// Construct with a subset of \a data.
126 ///
127 /// Initialize this object with a subset of the data bytes in \a data. If \a
128 /// data contains shared data, then a reference to the shared data will be
129 /// added to ensure the shared data stays around as long as any objects have
130 /// references to the shared data. The byte order value and the address size
131 /// settings are copied from \a data. If \a offset is not a valid offset in
132 /// \a data, then no reference to the shared data will be added. If there
133 /// are not \a length bytes available in \a data starting at \a offset, the
134 /// length will be truncated to contain as many bytes as possible.
135 ///
136 /// \param[in] data
137 /// Another DataExtractor object that contains data.
138 ///
139 /// \param[in] offset
140 /// The offset into \a data at which the subset starts.
141 ///
142 /// \param[in] length
143 /// The length in bytes of the subset of data.
144 ///
145 /// \param[in] target_byte_size
146 /// A size of a target byte in 8-bit host bytes
147 DataExtractor(const DataExtractor &data, lldb::offset_t offset,
148 lldb::offset_t length, uint32_t target_byte_size = 1);
149
150 /// Copy constructor.
151 ///
152 /// The copy constructor is explicit as otherwise it is easy to make
153 /// unintended modification of a local copy instead of a caller's instance.
154 /// Also a needless copy of the \a m_data_sp shared pointer is/ expensive.
155 explicit DataExtractor(const DataExtractor &rhs);
156
157 /// Assignment operator.
158 ///
159 /// Copies all data, byte order and address size settings from \a rhs into
160 /// this object. If \a rhs contains shared data, a reference to that shared
161 /// data will be added.
162 ///
163 /// \param[in] rhs
164 /// Another DataExtractor object to copy.
165 ///
166 /// \return
167 /// A const reference to this object.
168 const DataExtractor &operator=(const DataExtractor &rhs);
169
170 /// Move constructor and move assignment operators to complete the rule of 5.
171 ///
172 /// They would get deleted as we already defined those of rule of 3.
173 DataExtractor(DataExtractor &&rhs) = default;
175
176 /// Destructor
177 ///
178 /// If this object contains a valid shared data reference, the reference
179 /// count on the data will be decremented, and if zero, the data will be
180 /// freed.
181 virtual ~DataExtractor();
182
183 uint32_t getTargetByteSize() const { return m_target_byte_size; }
184
185 /// Clears the object state.
186 ///
187 /// Clears the object contents back to a default invalid state, and release
188 /// any references to shared data that this object may contain.
189 void Clear();
190
191 /// Dumps the binary data as \a type objects to stream \a s (or to Log() if
192 /// \a s is nullptr) starting \a offset bytes into the data and stopping
193 /// after dumping \a length bytes. The offset into the data is displayed at
194 /// the beginning of each line and can be offset by base address \a
195 /// base_addr. \a num_per_line objects will be displayed on each line.
196 ///
197 /// \param[in] log
198 /// The log to dump the output to.
199 ///
200 /// \param[in] offset
201 /// The offset into the data at which to start dumping.
202 ///
203 /// \param[in] length
204 /// The number of bytes to dump.
205 ///
206 /// \param[in] base_addr
207 /// The base address that gets added to the offset displayed on
208 /// each line.
209 ///
210 /// \param[in] num_per_line
211 /// The number of \a type objects to display on each line.
212 ///
213 /// \param[in] type
214 /// The type of objects to use when dumping data from this
215 /// object. See DataExtractor::Type.
216 ///
217 /// \return
218 /// The offset at which dumping ended.
220 lldb::offset_t length, uint64_t base_addr,
221 uint32_t num_per_line, Type type) const;
222
223 /// Extract an arbitrary number of bytes in the specified byte order.
224 ///
225 /// Attemps to extract \a length bytes starting at \a offset bytes into this
226 /// data in the requested byte order (\a dst_byte_order) and place the
227 /// results in \a dst. \a dst must be at least \a length bytes long.
228 ///
229 /// \param[in] offset
230 /// The offset in bytes into the contained data at which to
231 /// start extracting.
232 ///
233 /// \param[in] length
234 /// The number of bytes to extract.
235 ///
236 /// \param[in] dst_byte_order
237 /// A byte order of the data that we want when the value in
238 /// copied to \a dst.
239 ///
240 /// \param[out] dst
241 /// The buffer that will receive the extracted value if there
242 /// are enough bytes available in the current data.
243 ///
244 /// \return
245 /// The number of bytes that were extracted which will be \a
246 /// length when the value is successfully extracted, or zero
247 /// if there aren't enough bytes at the specified offset.
248 size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length,
249 lldb::ByteOrder dst_byte_order, void *dst) const;
250
251 /// Extract an address from \a *offset_ptr.
252 ///
253 /// Extract a single address from the data and update the offset pointed to
254 /// by \a offset_ptr. The size of the extracted address comes from the \a
255 /// m_addr_size member variable and should be set correctly prior to
256 /// extracting any address values.
257 ///
258 /// \param[in,out] offset_ptr
259 /// A pointer to an offset within the data that will be advanced
260 /// by the appropriate number of bytes if the value is extracted
261 /// correctly. If the offset is out of bounds or there are not
262 /// enough bytes to extract this value, the offset will be left
263 /// unmodified.
264 ///
265 /// \return
266 /// The extracted address value.
267 uint64_t GetAddress(lldb::offset_t *offset_ptr) const;
268
269 uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const;
270
271 /// Get the current address size.
272 ///
273 /// Return the size in bytes of any address values this object will extract.
274 ///
275 /// \return
276 /// The size in bytes of address values that will be extracted.
277 uint32_t GetAddressByteSize() const { return m_addr_size; }
278
279 /// Get the number of bytes contained in this object.
280 ///
281 /// \return
282 /// The total number of bytes of data this object refers to.
283 uint64_t GetByteSize() const { return m_end - m_start; }
284
285 /// Extract a C string from \a *offset_ptr.
286 ///
287 /// Returns a pointer to a C String from the data at the offset pointed to
288 /// by \a offset_ptr. A variable length NULL terminated C string will be
289 /// extracted and the \a offset_ptr will be updated with the offset of the
290 /// byte that follows the NULL terminator byte.
291 ///
292 /// \param[in,out] offset_ptr
293 /// A pointer to an offset within the data that will be advanced
294 /// by the appropriate number of bytes if the value is extracted
295 /// correctly. If the offset is out of bounds or there are not
296 /// enough bytes to extract this value, the offset will be left
297 /// unmodified.
298 ///
299 /// \return
300 /// A pointer to the C string value in the data. If the offset
301 /// pointed to by \a offset_ptr is out of bounds, or if the
302 /// offset plus the length of the C string is out of bounds,
303 /// nullptr will be returned.
304 const char *GetCStr(lldb::offset_t *offset_ptr) const;
305
306 /// Extract a C string from \a *offset_ptr with field size \a len.
307 ///
308 /// Returns a pointer to a C String from the data at the offset pointed to
309 /// by \a offset_ptr, with a field length of \a len.
310 /// A NULL terminated C string will be extracted and the \a offset_ptr
311 /// will be updated with the offset of the byte that follows the fixed
312 /// length field.
313 ///
314 /// \param[in,out] offset_ptr
315 /// A pointer to an offset within the data that will be advanced
316 /// by the appropriate number of bytes if the value is extracted
317 /// correctly. If the offset is out of bounds or there are not
318 /// enough bytes to extract this value, the offset will be left
319 /// unmodified.
320 ///
321 /// \return
322 /// A pointer to the C string value in the data. If the offset
323 /// pointed to by \a offset_ptr is out of bounds, or if the
324 /// offset plus the length of the field is out of bounds, or if
325 /// the field does not contain a NULL terminator byte, nullptr will
326 /// be returned.
327 const char *GetCStr(lldb::offset_t *offset_ptr, lldb::offset_t len) const;
328
329 /// Extract \a length bytes from \a *offset_ptr.
330 ///
331 /// Returns a pointer to a bytes in this object's data at the offset pointed
332 /// to by \a offset_ptr. If \a length is zero or too large, then the offset
333 /// pointed to by \a offset_ptr will not be updated and nullptr will be
334 /// returned.
335 ///
336 /// \param[in,out] offset_ptr
337 /// A pointer to an offset within the data that will be advanced
338 /// by the appropriate number of bytes if the value is extracted
339 /// correctly. If the offset is out of bounds or there are not
340 /// enough bytes to extract this value, the offset will be left
341 /// unmodified.
342 ///
343 /// \param[in] length
344 /// The optional length of a string to extract. If the value is
345 /// zero, a NULL terminated C string will be extracted.
346 ///
347 /// \return
348 /// A pointer to the bytes in this object's data if the offset
349 /// and length are valid, or nullptr otherwise.
350 virtual const void *GetData(lldb::offset_t *offset_ptr,
351 lldb::offset_t length) const {
352 const uint8_t *ptr = PeekData(*offset_ptr, length);
353 if (ptr)
354 *offset_ptr += length;
355 return ptr;
356 }
357
358 /// Copy \a length bytes from \a *offset, without swapping bytes.
359 ///
360 /// \param[in] offset
361 /// The offset into this data from which to start copying
362 ///
363 /// \param[in] length
364 /// The length of the data to copy from this object
365 ///
366 /// \param[out] dst
367 /// The buffer to place the output data.
368 ///
369 /// \return
370 /// Returns the number of bytes that were copied, or zero if
371 /// anything goes wrong.
373 void *dst) const;
374
375 /// Copy \a dst_len bytes from \a *offset_ptr and ensure the copied data is
376 /// treated as a value that can be swapped to match the specified byte
377 /// order.
378 ///
379 /// For values that are larger than the supported integer sizes, this
380 /// function can be used to extract data in a specified byte order. It can
381 /// also be used to copy a smaller integer value from to a larger value. The
382 /// extra bytes left over will be padded correctly according to the byte
383 /// order of this object and the \a dst_byte_order. This can be very handy
384 /// when say copying a partial data value into a register.
385 ///
386 /// \param[in] src_offset
387 /// The offset into this data from which to start copying an endian
388 /// entity
389 ///
390 /// \param[in] src_len
391 /// The length of the endian data to copy from this object into the \a
392 /// dst object
393 ///
394 /// \param[out] dst
395 /// The buffer where to place the endian data. The data might need to be
396 /// byte swapped (and appropriately padded with zeroes if \a src_len !=
397 /// \a dst_len) if \a dst_byte_order does not match the byte order in
398 /// this object.
399 ///
400 /// \param[in] dst_len
401 /// The length number of bytes that the endian value will occupy is \a
402 /// dst.
403 ///
404 /// \param[in] dst_byte_order
405 /// The byte order that the endian value should be in the \a dst buffer.
406 ///
407 /// \return
408 /// Returns the number of bytes that were copied, or zero if anything
409 /// goes wrong.
411 lldb::offset_t src_len, void *dst,
412 lldb::offset_t dst_len,
413 lldb::ByteOrder dst_byte_order) const;
414
415 /// Get the data end pointer.
416 ///
417 /// \return
418 /// Returns a pointer to the next byte contained in this
419 /// object's data, or nullptr of there is no data in this object.
420 const uint8_t *GetDataEnd() const { return m_end; }
421
422 /// Get the shared data offset.
423 ///
424 /// Get the offset of the first byte of data in the shared data (if any).
425 ///
426 /// \return
427 /// If this object contains shared data, this function returns
428 /// the offset in bytes into that shared data, zero otherwise.
429 size_t GetSharedDataOffset() const;
430
431 /// Get the data start pointer.
432 ///
433 /// \return
434 /// Returns a pointer to the first byte contained in this
435 /// object's data, or nullptr of there is no data in this object.
436 const uint8_t *GetDataStart() const { return m_start; }
437
438 /// Extract a float from \a *offset_ptr.
439 ///
440 /// Extract a single float value.
441 ///
442 /// \param[in,out] offset_ptr
443 /// A pointer to an offset within the data that will be advanced
444 /// by the appropriate number of bytes if the value is extracted
445 /// correctly. If the offset is out of bounds or there are not
446 /// enough bytes to extract this value, the offset will be left
447 /// unmodified.
448 ///
449 /// \return
450 /// The floating value that was extracted, or zero on failure.
451 float GetFloat(lldb::offset_t *offset_ptr) const;
452
453 double GetDouble(lldb::offset_t *offset_ptr) const;
454
455 long double GetLongDouble(lldb::offset_t *offset_ptr) const;
456
457 /// Extract an integer of size \a byte_size from \a *offset_ptr.
458 ///
459 /// Extract a single integer value and update the offset pointed to by \a
460 /// offset_ptr. The size of the extracted integer is specified by the \a
461 /// byte_size argument. \a byte_size must have a value >= 1 and <= 4 since
462 /// the return value is only 32 bits wide.
463 ///
464 /// \param[in,out] offset_ptr
465 /// A pointer to an offset within the data that will be advanced
466 /// by the appropriate number of bytes if the value is extracted
467 /// correctly. If the offset is out of bounds or there are not
468 /// enough bytes to extract this value, the offset will be left
469 /// unmodified.
470 ///
471 /// \param[in] byte_size
472 /// The size in byte of the integer to extract.
473 ///
474 /// \return
475 /// The integer value that was extracted, or zero on failure.
476 uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const;
477
478 /// Extract an unsigned integer of size \a byte_size from \a *offset_ptr.
479 ///
480 /// Extract a single unsigned integer value and update the offset pointed to
481 /// by \a offset_ptr. The size of the extracted integer is specified by the
482 /// \a byte_size argument. \a byte_size must have a value greater than or
483 /// equal to one and less than or equal to eight since the return value is
484 /// 64 bits wide.
485 ///
486 /// \param[in,out] offset_ptr
487 /// A pointer to an offset within the data that will be advanced
488 /// by the appropriate number of bytes if the value is extracted
489 /// correctly. If the offset is out of bounds or there are not
490 /// enough bytes to extract this value, the offset will be left
491 /// unmodified.
492 ///
493 /// \param[in] byte_size
494 /// The size in byte of the integer to extract.
495 ///
496 /// \return
497 /// The unsigned integer value that was extracted, or zero on
498 /// failure.
499 uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const;
500
501 uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr,
502 size_t byte_size) const;
503
504 /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
505 ///
506 /// Extract a single signed integer value (sign extending if required) and
507 /// update the offset pointed to by \a offset_ptr. The size of the extracted
508 /// integer is specified by the \a byte_size argument. \a byte_size must
509 /// have a value greater than or equal to one and less than or equal to
510 /// eight since the return value is 64 bits wide.
511 ///
512 /// \param[in,out] offset_ptr
513 /// A pointer to an offset within the data that will be advanced
514 /// by the appropriate number of bytes if the value is extracted
515 /// correctly. If the offset is out of bounds or there are not
516 /// enough bytes to extract this value, the offset will be left
517 /// unmodified.
518 ///
519 /// \param[in] byte_size
520 /// The size in byte of the integer to extract.
521 ///
522 /// \return
523 /// The sign extended signed integer value that was extracted,
524 /// or zero on failure.
525 int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const;
526
527 /// Extract an unsigned integer of size \a byte_size from \a *offset_ptr,
528 /// then extract the bitfield from this value if \a bitfield_bit_size is
529 /// non-zero.
530 ///
531 /// Extract a single unsigned integer value and update the offset pointed to
532 /// by \a offset_ptr. The size of the extracted integer is specified by the
533 /// \a byte_size argument. \a byte_size must have a value greater than or
534 /// equal to one and less than or equal to 8 since the return value is 64
535 /// bits wide.
536 ///
537 /// \param[in,out] offset_ptr
538 /// A pointer to an offset within the data that will be advanced
539 /// by the appropriate number of bytes if the value is extracted
540 /// correctly. If the offset is out of bounds or there are not
541 /// enough bytes to extract this value, the offset will be left
542 /// unmodified.
543 ///
544 /// \param[in] size
545 /// The size in byte of the integer to extract.
546 ///
547 /// \param[in] bitfield_bit_size
548 /// The size in bits of the bitfield value to extract, or zero
549 /// to just extract the entire integer value.
550 ///
551 /// \param[in] bitfield_bit_offset
552 /// The bit offset of the bitfield value in the extracted
553 /// integer. For little-endian data, this is the offset of
554 /// the LSB of the bitfield from the LSB of the integer.
555 /// For big-endian data, this is the offset of the MSB of the
556 /// bitfield from the MSB of the integer.
557 ///
558 /// \return
559 /// The unsigned bitfield integer value that was extracted, or
560 /// zero on failure.
561 uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size,
562 uint32_t bitfield_bit_size,
563 uint32_t bitfield_bit_offset) const;
564
565 /// Extract an signed integer of size \a size from \a *offset_ptr, then
566 /// extract and sign-extend the bitfield from this value if \a
567 /// bitfield_bit_size is non-zero.
568 ///
569 /// Extract a single signed integer value (sign-extending if required) and
570 /// update the offset pointed to by \a offset_ptr. The size of the extracted
571 /// integer is specified by the \a size argument. \a size must
572 /// have a value greater than or equal to one and less than or equal to
573 /// eight since the return value is 64 bits wide.
574 ///
575 /// \param[in,out] offset_ptr
576 /// A pointer to an offset within the data that will be advanced
577 /// by the appropriate number of bytes if the value is extracted
578 /// correctly. If the offset is out of bounds or there are not
579 /// enough bytes to extract this value, the offset will be left
580 /// unmodified.
581 ///
582 /// \param[in] size
583 /// The size in bytes of the integer to extract.
584 ///
585 /// \param[in] bitfield_bit_size
586 /// The size in bits of the bitfield value to extract, or zero
587 /// to just extract the entire integer value.
588 ///
589 /// \param[in] bitfield_bit_offset
590 /// The bit offset of the bitfield value in the extracted
591 /// integer. For little-endian data, this is the offset of
592 /// the LSB of the bitfield from the LSB of the integer.
593 /// For big-endian data, this is the offset of the MSB of the
594 /// bitfield from the MSB of the integer.
595 ///
596 /// \return
597 /// The signed bitfield integer value that was extracted, or
598 /// zero on failure.
599 int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size,
600 uint32_t bitfield_bit_size,
601 uint32_t bitfield_bit_offset) const;
602
603 /// Get the current byte order value.
604 ///
605 /// \return
606 /// The current byte order value from this object's internal
607 /// state.
609
610 /// Extract a uint8_t value from \a *offset_ptr.
611 ///
612 /// Extract a single uint8_t from the binary data at the offset pointed to
613 /// by \a offset_ptr, and advance the offset on success.
614 ///
615 /// \param[in,out] offset_ptr
616 /// A pointer to an offset within the data that will be advanced
617 /// by the appropriate number of bytes if the value is extracted
618 /// correctly. If the offset is out of bounds or there are not
619 /// enough bytes to extract this value, the offset will be left
620 /// unmodified.
621 ///
622 /// \return
623 /// The extracted uint8_t value.
624 uint8_t GetU8(lldb::offset_t *offset_ptr) const;
625
626 virtual uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const {
627 uint8_t val = m_start[*offset_ptr];
628 *offset_ptr += 1;
629 return val;
630 }
631
632 virtual uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const;
633
634 virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const;
635
636 virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const;
637 /// Extract \a count uint8_t values from \a *offset_ptr.
638 ///
639 /// Extract \a count uint8_t values from the binary data at the offset
640 /// pointed to by \a offset_ptr, and advance the offset on success. The
641 /// extracted values are copied into \a dst.
642 ///
643 /// \param[in,out] offset_ptr
644 /// A pointer to an offset within the data that will be advanced
645 /// by the appropriate number of bytes if the value is extracted
646 /// correctly. If the offset is out of bounds or there are not
647 /// enough bytes to extract this value, the offset will be left
648 /// unmodified.
649 ///
650 /// \param[out] dst
651 /// A buffer to copy \a count uint8_t values into. \a dst must
652 /// be large enough to hold all requested data.
653 ///
654 /// \param[in] count
655 /// The number of uint8_t values to extract.
656 ///
657 /// \return
658 /// \a dst if all values were properly extracted and copied,
659 /// nullptr otherwise.
660 void *GetU8(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
661
662 /// Extract a uint16_t value from \a *offset_ptr.
663 ///
664 /// Extract a single uint16_t from the binary data at the offset pointed to
665 /// by \a offset_ptr, and update the offset on success.
666 ///
667 /// \param[in,out] offset_ptr
668 /// A pointer to an offset within the data that will be advanced
669 /// by the appropriate number of bytes if the value is extracted
670 /// correctly. If the offset is out of bounds or there are not
671 /// enough bytes to extract this value, the offset will be left
672 /// unmodified.
673 ///
674 /// \return
675 /// The extracted uint16_t value.
676 uint16_t GetU16(lldb::offset_t *offset_ptr) const;
677
678 /// Extract \a count uint16_t values from \a *offset_ptr.
679 ///
680 /// Extract \a count uint16_t values from the binary data at the offset
681 /// pointed to by \a offset_ptr, and advance the offset on success. The
682 /// extracted values are copied into \a dst.
683 ///
684 /// \param[in,out] offset_ptr
685 /// A pointer to an offset within the data that will be advanced
686 /// by the appropriate number of bytes if the value is extracted
687 /// correctly. If the offset is out of bounds or there are not
688 /// enough bytes to extract this value, the offset will be left
689 /// unmodified.
690 ///
691 /// \param[out] dst
692 /// A buffer to copy \a count uint16_t values into. \a dst must
693 /// be large enough to hold all requested data.
694 ///
695 /// \param[in] count
696 /// The number of uint16_t values to extract.
697 ///
698 /// \return
699 /// \a dst if all values were properly extracted and copied,
700 /// nullptr otherwise.
701 void *GetU16(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
702
703 /// Extract a uint32_t value from \a *offset_ptr.
704 ///
705 /// Extract a single uint32_t from the binary data at the offset pointed to
706 /// by \a offset_ptr, and update the offset on success.
707 ///
708 /// \param[in,out] offset_ptr
709 /// A pointer to an offset within the data that will be advanced
710 /// by the appropriate number of bytes if the value is extracted
711 /// correctly. If the offset is out of bounds or there are not
712 /// enough bytes to extract this value, the offset will be left
713 /// unmodified.
714 ///
715 /// \return
716 /// The extracted uint32_t value.
717 uint32_t GetU32(lldb::offset_t *offset_ptr) const;
718
719 /// Extract \a count uint32_t values from \a *offset_ptr.
720 ///
721 /// Extract \a count uint32_t values from the binary data at the offset
722 /// pointed to by \a offset_ptr, and advance the offset on success. The
723 /// extracted values are copied into \a dst.
724 ///
725 /// \param[in,out] offset_ptr
726 /// A pointer to an offset within the data that will be advanced
727 /// by the appropriate number of bytes if the value is extracted
728 /// correctly. If the offset is out of bounds or there are not
729 /// enough bytes to extract this value, the offset will be left
730 /// unmodified.
731 ///
732 /// \param[out] dst
733 /// A buffer to copy \a count uint32_t values into. \a dst must
734 /// be large enough to hold all requested data.
735 ///
736 /// \param[in] count
737 /// The number of uint32_t values to extract.
738 ///
739 /// \return
740 /// \a dst if all values were properly extracted and copied,
741 /// nullptr otherwise.
742 void *GetU32(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
743
744 /// Extract a uint64_t value from \a *offset_ptr.
745 ///
746 /// Extract a single uint64_t from the binary data at the offset pointed to
747 /// by \a offset_ptr, and update the offset on success.
748 ///
749 /// \param[in,out] offset_ptr
750 /// A pointer to an offset within the data that will be advanced
751 /// by the appropriate number of bytes if the value is extracted
752 /// correctly. If the offset is out of bounds or there are not
753 /// enough bytes to extract this value, the offset will be left
754 /// unmodified.
755 ///
756 /// \return
757 /// The extracted uint64_t value.
758 uint64_t GetU64(lldb::offset_t *offset_ptr) const;
759
760 /// Extract \a count uint64_t values from \a *offset_ptr.
761 ///
762 /// Extract \a count uint64_t values from the binary data at the offset
763 /// pointed to by \a offset_ptr, and advance the offset on success. The
764 /// extracted values are copied into \a dst.
765 ///
766 /// \param[in,out] offset_ptr
767 /// A pointer to an offset within the data that will be advanced
768 /// by the appropriate number of bytes if the value is extracted
769 /// correctly. If the offset is out of bounds or there are not
770 /// enough bytes to extract this value, the offset will be left
771 /// unmodified.
772 ///
773 /// \param[out] dst
774 /// A buffer to copy \a count uint64_t values into. \a dst must
775 /// be large enough to hold all requested data.
776 ///
777 /// \param[in] count
778 /// The number of uint64_t values to extract.
779 ///
780 /// \return
781 /// \a dst if all values were properly extracted and copied,
782 /// nullptr otherwise.
783 void *GetU64(lldb::offset_t *offset_ptr, void *dst, uint32_t count) const;
784
785 /// Extract a signed LEB128 value from \a *offset_ptr.
786 ///
787 /// Extracts an signed LEB128 number from this object's data starting at the
788 /// offset pointed to by \a offset_ptr. The offset pointed to by \a
789 /// offset_ptr will be updated with the offset of the byte following the
790 /// last extracted byte.
791 ///
792 /// \param[in,out] offset_ptr
793 /// A pointer to an offset within the data that will be advanced
794 /// by the appropriate number of bytes if the value is extracted
795 /// correctly. If the offset is out of bounds or there are not
796 /// enough bytes to extract this value, the offset will be left
797 /// unmodified.
798 ///
799 /// \return
800 /// The extracted signed integer value.
801 int64_t GetSLEB128(lldb::offset_t *offset_ptr) const;
802
803 /// Extract a unsigned LEB128 value from \a *offset_ptr.
804 ///
805 /// Extracts an unsigned LEB128 number from this object's data starting at
806 /// the offset pointed to by \a offset_ptr. The offset pointed to by \a
807 /// offset_ptr will be updated with the offset of the byte following the
808 /// last extracted byte.
809 ///
810 /// \param[in,out] offset_ptr
811 /// A pointer to an offset within the data that will be advanced
812 /// by the appropriate number of bytes if the value is extracted
813 /// correctly. If the offset is out of bounds or there are not
814 /// enough bytes to extract this value, the offset will be left
815 /// unmodified.
816 ///
817 /// \return
818 /// The extracted unsigned integer value.
819 uint64_t GetULEB128(lldb::offset_t *offset_ptr) const;
820
822
823 bool HasData() { return m_start && m_end && m_end - m_start > 0; }
824
825 /// Peek at a C string at \a offset.
826 ///
827 /// Peeks at a string in the contained data. No verification is done to make
828 /// sure the entire string lies within the bounds of this object's data,
829 /// only \a offset is verified to be a valid offset.
830 ///
831 /// \param[in] offset
832 /// An offset into the data.
833 ///
834 /// \return
835 /// A non-nullptr C string pointer if \a offset is a valid offset,
836 /// nullptr otherwise.
837 const char *PeekCStr(lldb::offset_t offset) const;
838
839 /// Peek at a bytes at \a offset.
840 ///
841 /// Returns a pointer to \a length bytes at \a offset as long as there are
842 /// \a length bytes available starting at \a offset.
843 ///
844 /// \return
845 /// A non-nullptr data pointer if \a offset is a valid offset and
846 /// there are \a length bytes available at that offset, nullptr
847 /// otherwise.
848 virtual const uint8_t *PeekData(lldb::offset_t offset,
849 lldb::offset_t length) const {
850 if (ValidOffsetForDataOfSize(offset, length))
851 return m_start + offset;
852 return nullptr;
853 }
854
855 /// Set the address byte size.
856 ///
857 /// Set the size in bytes that will be used when extracting any address and
858 /// pointer values from data contained in this object.
859 ///
860 /// \param[in] addr_size
861 /// The size in bytes to use when extracting addresses.
862 void SetAddressByteSize(uint32_t addr_size) {
863 assert(addr_size == 2 || addr_size == 4 || addr_size == 8);
864 m_addr_size = addr_size;
865 }
866
867 /// Set data with a buffer that is caller owned.
868 ///
869 /// Use data that is owned by the caller when extracting values. The data
870 /// must stay around as long as this object, or any object that copies a
871 /// subset of this object's data, is valid. If \a bytes is nullptr, or \a
872 /// length is zero, this object will contain no data.
873 ///
874 /// \param[in] bytes
875 /// A pointer to caller owned data.
876 ///
877 /// \param[in] length
878 /// The length in bytes of \a bytes.
879 ///
880 /// \param[in] byte_order
881 /// A byte order of the data that we are extracting from.
882 ///
883 /// \return
884 /// The number of bytes that this object now contains.
885 lldb::offset_t SetData(const void *bytes, lldb::offset_t length,
886 lldb::ByteOrder byte_order);
887
888 /// Adopt a subset of \a data.
889 ///
890 /// Set this object's data to be a subset of the data bytes in \a data. If
891 /// \a data contains shared data, then a reference to the shared data will
892 /// be added to ensure the shared data stays around as long as any objects
893 /// have references to the shared data. The byte order and the address size
894 /// settings are copied from \a data. If \a offset is not a valid offset in
895 /// \a data, then no reference to the shared data will be added. If there
896 /// are not \a length bytes available in \a data starting at \a offset, the
897 /// length will be truncated to contains as many bytes as possible.
898 ///
899 /// \param[in] data
900 /// Another DataExtractor object that contains data.
901 ///
902 /// \param[in] offset
903 /// The offset into \a data at which the subset starts.
904 ///
905 /// \param[in] length
906 /// The length in bytes of the subset of \a data.
907 ///
908 /// \return
909 /// The number of bytes that this object now contains.
911 lldb::offset_t length);
912
913 /// Adopt a subset of shared data in \a data_sp.
914 ///
915 /// Copies the data shared pointer which adds a reference to the contained
916 /// in \a data_sp. The shared data reference is reference counted to ensure
917 /// the data lives as long as anyone still has a valid shared pointer to the
918 /// data in \a data_sp. The byte order and address byte size settings remain
919 /// the same. If \a offset is not a valid offset in \a data_sp, then no
920 /// reference to the shared data will be added. If there are not \a length
921 /// bytes available in \a data starting at \a offset, the length will be
922 /// truncated to contains as many bytes as possible.
923 ///
924 /// \param[in] data_sp
925 /// A shared pointer to data.
926 ///
927 /// \param[in] offset
928 /// The offset into \a data_sp at which the subset starts.
929 ///
930 /// \param[in] length
931 /// The length in bytes of the subset of \a data_sp.
932 ///
933 /// \return
934 /// The number of bytes that this object now contains.
936 lldb::offset_t offset = 0,
938
939 /// Set the byte_order value.
940 ///
941 /// Sets the byte order of the data to extract. Extracted values will be
942 /// swapped if necessary when decoding.
943 ///
944 /// \param[in] byte_order
945 /// The byte order value to use when extracting data.
946 void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; }
947
948 /// Skip an LEB128 number at \a *offset_ptr.
949 ///
950 /// Skips a LEB128 number (signed or unsigned) from this object's data
951 /// starting at the offset pointed to by \a offset_ptr. The offset pointed
952 /// to by \a offset_ptr will be updated with the offset of the byte
953 /// following the last extracted byte.
954 ///
955 /// \param[in,out] offset_ptr
956 /// A pointer to an offset within the data that will be advanced
957 /// by the appropriate number of bytes if the value is extracted
958 /// correctly. If the offset is out of bounds or there are not
959 /// enough bytes to extract this value, the offset will be left
960 /// unmodified.
961 ///
962 /// \return
963 /// The number of bytes consumed during the extraction.
964 uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const;
965
966 /// Test the validity of \a offset.
967 ///
968 /// \return
969 /// true if \a offset is a valid offset into the data in this object,
970 /// false otherwise.
971 bool ValidOffset(lldb::offset_t offset) const {
972 return offset < GetByteSize();
973 }
974
975 /// Test the availability of \a length bytes of data from \a offset.
976 ///
977 /// \return
978 /// true if \a offset is a valid offset and there are \a
979 /// length bytes available at that offset, false otherwise.
981 lldb::offset_t length) const {
982 return length <= BytesLeft(offset);
983 }
984
985 size_t Copy(DataExtractor &dest_data) const;
986
987 bool Append(DataExtractor &rhs);
988
989 bool Append(void *bytes, lldb::offset_t length);
990
992 const lldb::offset_t size = GetByteSize();
993 if (size > offset)
994 return size - offset;
995 return 0;
996 }
997
998 void Checksum(llvm::SmallVectorImpl<uint8_t> &dest, uint64_t max_data = 0);
999
1000 llvm::ArrayRef<uint8_t> GetData() const {
1001 return {GetDataStart(), size_t(GetByteSize())};
1002 }
1003
1004 llvm::DataExtractor GetAsLLVM() const {
1006 uint8_t(GetAddressByteSize())};
1007 }
1008
1009protected:
1010 template <typename T> T Get(lldb::offset_t *offset_ptr, T fail_value) const {
1011 constexpr size_t src_size = sizeof(T);
1012 T val = fail_value;
1013
1014 const void *src = GetData(offset_ptr, src_size);
1015 if (!src)
1016 return val;
1017
1018 memcpy(&val, src, src_size);
1020 llvm::sys::swapByteOrder(val);
1021
1022 return val;
1023 }
1024
1025 // Member variables
1026 const uint8_t *m_start = nullptr; ///< A pointer to the first byte of data.
1027 const uint8_t *m_end =
1028 nullptr; ///< A pointer to the byte that is past the end of the data.
1030 m_byte_order; ///< The byte order of the data we are extracting from.
1031 uint32_t m_addr_size; ///< The address size to use when extracting addresses.
1032 /// The shared pointer to data that can be shared among multiple instances
1034 /// Making it const would require implementation of move assignment operator.
1036};
1037
1038} // namespace lldb_private
1039
1040#endif // LLDB_UTILITY_DATAEXTRACTOR_H
An data extractor class.
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
size_t GetSharedDataOffset() const
Get the shared data offset.
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
size_t Copy(DataExtractor &dest_data) const
T Get(lldb::offset_t *offset_ptr, T fail_value) const
int64_t GetMaxS64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an signed integer of size byte_size from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
bool ValidOffsetForDataOfSize(lldb::offset_t offset, lldb::offset_t length) const
Test the availability of length bytes of data from offset.
long double GetLongDouble(lldb::offset_t *offset_ptr) const
void Clear()
Clears the object state.
const uint8_t * m_start
A pointer to the first byte of data.
lldb::DataBufferSP m_data_sp
The shared pointer to data that can be shared among multiple instances.
uint32_t GetMaxU32(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an integer of size byte_size from *offset_ptr.
virtual const uint8_t * PeekData(lldb::offset_t offset, lldb::offset_t length) const
Peek at a bytes at offset.
uint32_t getTargetByteSize() const
uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const
const DataExtractor & operator=(const DataExtractor &rhs)
Assignment operator.
lldb::offset_t CopyData(lldb::offset_t offset, lldb::offset_t length, void *dst) const
Copy length bytes from *offset, without swapping bytes.
uint64_t GetMaxU64_unchecked(lldb::offset_t *offset_ptr, size_t byte_size) const
lldb::DataBufferSP & GetSharedDataBuffer()
llvm::DataExtractor GetAsLLVM() const
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
const uint8_t * m_end
A pointer to the byte that is past the end of the data.
DataExtractor()
Default constructor.
uint32_t m_target_byte_size
Making it const would require implementation of move assignment operator.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
DataExtractor & operator=(DataExtractor &&rhs)=default
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
Type
Type enumerations used in the dump routines.
@ TypeUInt32
Format output as unsigned 32 bit integers.
@ TypeSLEB128
Format output as SLEB128 numbers.
@ TypeUInt8
Format output as unsigned 8 bit integers.
@ TypeUInt64
Format output as unsigned 64 bit integers.
@ TypeULEB128
Format output as ULEB128 numbers.
@ TypePointer
Format output as pointers.
@ TypeUInt16
Format output as unsigned 16 bit integers.
@ TypeChar
Format output as characters.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an unsigned integer of size byte_size from *offset_ptr, then extract the bitfield from this v...
lldb::ByteOrder m_byte_order
The byte order of the data we are extracting from.
void Checksum(llvm::SmallVectorImpl< uint8_t > &dest, uint64_t max_data=0)
bool Append(DataExtractor &rhs)
const uint8_t * GetDataStart() const
Get the data start pointer.
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
const uint8_t * GetDataEnd() const
Get the data end pointer.
uint32_t GetAddressByteSize() const
Get the current address size.
uint32_t m_addr_size
The address size to use when extracting addresses.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
virtual uint8_t GetU8_unchecked(lldb::offset_t *offset_ptr) const
int64_t GetSLEB128(lldb::offset_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const
llvm::ArrayRef< uint8_t > GetData() const
lldb::offset_t BytesLeft(lldb::offset_t offset) const
int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an signed integer of size size from *offset_ptr, then extract and sign-extend the bitfield fr...
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
virtual uint16_t GetU16_unchecked(lldb::offset_t *offset_ptr) const
lldb::offset_t PutToLog(Log *log, lldb::offset_t offset, lldb::offset_t length, uint64_t base_addr, uint32_t num_per_line, Type type) const
Dumps the binary data as type objects to stream s (or to Log() if s is nullptr) starting offset bytes...
lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, lldb::offset_t src_len, void *dst, lldb::offset_t dst_len, lldb::ByteOrder dst_byte_order) const
Copy dst_len bytes from *offset_ptr and ensure the copied data is treated as a value that can be swap...
double GetDouble(lldb::offset_t *offset_ptr) const
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
DataExtractor(DataExtractor &&rhs)=default
Move constructor and move assignment operators to complete the rule of 5.
const char * PeekCStr(lldb::offset_t offset) const
Peek at a C string at offset.
virtual ~DataExtractor()
Destructor.
size_t ExtractBytes(lldb::offset_t offset, lldb::offset_t length, lldb::ByteOrder dst_byte_order, void *dst) const
Extract an arbitrary number of bytes in the specified byte order.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
#define LLDB_INVALID_OFFSET
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP