blob: 0be3ef8546b8d6d05b8f6aafcb1acb5232330592 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <assert.h>
Chris Lattner24943d22010-06-08 16:52:24 +000011#include <stddef.h>
12
13#include <bitset>
Sean Callanan509c7282012-10-20 06:08:09 +000014#include <limits>
15#include <sstream>
Chris Lattner24943d22010-06-08 16:52:24 +000016#include <string>
17
Greg Clayton24a6bd92011-10-27 17:55:14 +000018#include "llvm/ADT/APFloat.h"
Greg Claytona4e0dad2011-08-15 02:24:40 +000019#include "llvm/ADT/APInt.h"
Greg Clayton39f54e52011-08-15 07:23:47 +000020#include "llvm/ADT/ArrayRef.h"
Eli Friedman5eeb2d32010-06-10 23:56:16 +000021#include "llvm/Support/MathExtras.h"
22
Enrico Granata91544802011-09-06 19:20:51 +000023#include "lldb/Core/DataBufferHeap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/DataExtractor.h"
25#include "lldb/Core/DataBuffer.h"
Greg Clayton24a6bd92011-10-27 17:55:14 +000026#include "lldb/Core/Disassembler.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/Log.h"
28#include "lldb/Core/Stream.h"
29#include "lldb/Core/StreamString.h"
30#include "lldb/Core/UUID.h"
31#include "lldb/Core/dwarf.h"
Greg Claytoncd548032011-02-01 01:31:41 +000032#include "lldb/Host/Endian.h"
Greg Clayton24a6bd92011-10-27 17:55:14 +000033#include "lldb/Target/ExecutionContext.h"
34#include "lldb/Target/ExecutionContextScope.h"
35#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
Greg Clayton2519d992010-06-12 01:03:17 +000040static inline uint16_t
41ReadInt16(const unsigned char* ptr, unsigned offset)
42{
43 return *(uint16_t *)(ptr + offset);
Eli Friedman5eeb2d32010-06-10 23:56:16 +000044}
Greg Clayton2519d992010-06-12 01:03:17 +000045static inline uint32_t
46ReadInt32 (const unsigned char* ptr, unsigned offset)
47{
48 return *(uint32_t *)(ptr + offset);
Eli Friedman5eeb2d32010-06-10 23:56:16 +000049}
Greg Clayton2519d992010-06-12 01:03:17 +000050
51static inline uint64_t
52ReadInt64(const unsigned char* ptr, unsigned offset)
53{
54 return *(uint64_t *)(ptr + offset);
Eli Friedman5eeb2d32010-06-10 23:56:16 +000055}
Greg Clayton2519d992010-06-12 01:03:17 +000056
57static inline uint16_t
58ReadSwapInt16(const unsigned char* ptr, unsigned offset)
59{
60 return llvm::ByteSwap_16(*(uint16_t *)(ptr + offset));
Eli Friedman5eeb2d32010-06-10 23:56:16 +000061}
Greg Clayton2519d992010-06-12 01:03:17 +000062
63static inline uint32_t
64ReadSwapInt32 (const unsigned char* ptr, unsigned offset)
65{
66 return llvm::ByteSwap_32(*(uint32_t *)(ptr + offset));
Eli Friedman5eeb2d32010-06-10 23:56:16 +000067}
Greg Clayton2519d992010-06-12 01:03:17 +000068static inline uint64_t
69ReadSwapInt64(const unsigned char* ptr, unsigned offset)
70{
71 return llvm::ByteSwap_64(*(uint64_t *)(ptr + offset));
Eli Friedman5eeb2d32010-06-10 23:56:16 +000072}
73
Chris Lattner24943d22010-06-08 16:52:24 +000074#define NON_PRINTABLE_CHAR '.'
75//----------------------------------------------------------------------
76// Default constructor.
77//----------------------------------------------------------------------
78DataExtractor::DataExtractor () :
79 m_start (NULL),
80 m_end (NULL),
Greg Claytoncd548032011-02-01 01:31:41 +000081 m_byte_order(lldb::endian::InlHostByteOrder()),
Chris Lattner24943d22010-06-08 16:52:24 +000082 m_addr_size (4),
83 m_data_sp ()
84{
85}
86
87//----------------------------------------------------------------------
88// This constructor allows us to use data that is owned by someone else.
89// The data must stay around as long as this object is valid.
90//----------------------------------------------------------------------
91DataExtractor::DataExtractor (const void* data, uint32_t length, ByteOrder endian, uint8_t addr_size) :
92 m_start ((uint8_t*)data),
93 m_end ((uint8_t*)data + length),
94 m_byte_order(endian),
95 m_addr_size (addr_size),
96 m_data_sp ()
97{
98}
99
100//----------------------------------------------------------------------
101// Make a shared pointer reference to the shared data in "data_sp" and
102// set the endian swapping setting to "swap", and the address size to
103// "addr_size". The shared data reference will ensure the data lives
104// as long as any DataExtractor objects exist that have a reference to
105// this data.
106//----------------------------------------------------------------------
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000107DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint8_t addr_size) :
Chris Lattner24943d22010-06-08 16:52:24 +0000108 m_start (NULL),
109 m_end (NULL),
110 m_byte_order(endian),
111 m_addr_size (addr_size),
112 m_data_sp ()
113{
114 SetData (data_sp);
115}
116
117//----------------------------------------------------------------------
118// Initialize this object with a subset of the data bytes in "data".
119// If "data" contains shared data, then a reference to this shared
120// data will added and the shared data will stay around as long
121// as any object contains a reference to that data. The endian
122// swap and address size settings are copied from "data".
123//----------------------------------------------------------------------
124DataExtractor::DataExtractor (const DataExtractor& data, uint32_t offset, uint32_t length) :
125 m_start(NULL),
126 m_end(NULL),
127 m_byte_order(data.m_byte_order),
128 m_addr_size(data.m_addr_size),
129 m_data_sp()
130{
131 if (data.ValidOffset(offset))
132 {
133 uint32_t bytes_available = data.GetByteSize() - offset;
134 if (length > bytes_available)
135 length = bytes_available;
136 SetData(data, offset, length);
137 }
138}
139
Greg Clayton82f07462011-05-30 00:49:24 +0000140DataExtractor::DataExtractor (const DataExtractor& rhs) :
141 m_start (rhs.m_start),
142 m_end (rhs.m_end),
143 m_byte_order (rhs.m_byte_order),
144 m_addr_size (rhs.m_addr_size),
145 m_data_sp (rhs.m_data_sp)
146{
147}
148
Chris Lattner24943d22010-06-08 16:52:24 +0000149//----------------------------------------------------------------------
150// Assignment operator
151//----------------------------------------------------------------------
152const DataExtractor&
153DataExtractor::operator= (const DataExtractor& rhs)
154{
155 if (this != &rhs)
156 {
Greg Clayton82f07462011-05-30 00:49:24 +0000157 m_start = rhs.m_start;
158 m_end = rhs.m_end;
159 m_byte_order = rhs.m_byte_order;
Chris Lattner24943d22010-06-08 16:52:24 +0000160 m_addr_size = rhs.m_addr_size;
Greg Clayton82f07462011-05-30 00:49:24 +0000161 m_data_sp = rhs.m_data_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000162 }
163 return *this;
164}
165
166//----------------------------------------------------------------------
167// Destructor
168//----------------------------------------------------------------------
169DataExtractor::~DataExtractor ()
170{
171}
172
173//------------------------------------------------------------------
174// Clears the object contents back to a default invalid state, and
175// release any references to shared data that this object may
176// contain.
177//------------------------------------------------------------------
178void
179DataExtractor::Clear ()
180{
181 m_start = NULL;
182 m_end = NULL;
Greg Claytoncd548032011-02-01 01:31:41 +0000183 m_byte_order = lldb::endian::InlHostByteOrder();
Chris Lattner24943d22010-06-08 16:52:24 +0000184 m_addr_size = 4;
185 m_data_sp.reset();
186}
187
188//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000189// If this object contains shared data, this function returns the
190// offset into that shared data. Else zero is returned.
191//------------------------------------------------------------------
192size_t
193DataExtractor::GetSharedDataOffset () const
194{
195 if (m_start != NULL)
196 {
197 const DataBuffer * data = m_data_sp.get();
198 if (data != NULL)
199 {
200 const uint8_t * data_bytes = data->GetBytes();
201 if (data_bytes != NULL)
202 {
203 assert(m_start >= data_bytes);
204 return m_start - data_bytes;
205 }
206 }
207 }
208 return 0;
209}
210
211//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000212// Returns true if there are LENGTH bytes availabe starting OFFSET
213// into the data that is in this object.
214//------------------------------------------------------------------
215bool
216DataExtractor::ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const
217{
218 size_t size = GetByteSize();
219 if (offset >= size)
220 return false; // offset isn't valid
221
222 if (length == 0)
223 return true; // No bytes requested at this offset, return true
224
225 // If we flip the bits in offset we can figure out how
226 // many bytes we have left before "offset + length"
227 // could overflow when doing unsigned arithmetic.
228 if (length > ~offset)
229 return false; // unsigned overflow
230
231 // Make sure "offset + length" is a valid offset as well.
232 // length must be greater than zero for this to be a
233 // valid expression, and we have already checked for this.
234 return ((offset + length) <= size);
235}
236
Chris Lattner24943d22010-06-08 16:52:24 +0000237//----------------------------------------------------------------------
238// Set the data with which this object will extract from to data
239// starting at BYTES and set the length of the data to LENGTH bytes
240// long. The data is externally owned must be around at least as
241// long as this object points to the data. No copy of the data is
242// made, this object just refers to this data and can extract from
243// it. If this object refers to any shared data upon entry, the
244// reference to that data will be released. Is SWAP is set to true,
245// any data extracted will be endian swapped.
246//----------------------------------------------------------------------
247uint32_t
248DataExtractor::SetData (const void *bytes, uint32_t length, ByteOrder endian)
249{
250 m_byte_order = endian;
251 m_data_sp.reset();
252 if (bytes == NULL || length == 0)
253 {
254 m_start = NULL;
255 m_end = NULL;
256 }
257 else
258 {
259 m_start = (uint8_t *)bytes;
260 m_end = m_start + length;
261 }
262 return GetByteSize();
263}
264
265//----------------------------------------------------------------------
266// Assign the data for this object to be a subrange in "data"
267// starting "data_offset" bytes into "data" and ending "data_length"
268// bytes later. If "data_offset" is not a valid offset into "data",
269// then this object will contain no bytes. If "data_offset" is
270// within "data" yet "data_length" is too large, the length will be
271// capped at the number of bytes remaining in "data". If "data"
272// contains a shared pointer to other data, then a ref counted
273// pointer to that data will be made in this object. If "data"
274// doesn't contain a shared pointer to data, then the bytes referred
275// to in "data" will need to exist at least as long as this object
276// refers to those bytes. The address size and endian swap settings
277// are copied from the current values in "data".
278//----------------------------------------------------------------------
279uint32_t
280DataExtractor::SetData (const DataExtractor& data, uint32_t data_offset, uint32_t data_length)
281{
282 m_addr_size = data.m_addr_size;
283 // If "data" contains shared pointer to data, then we can use that
284 if (data.m_data_sp.get())
285 {
286 m_byte_order = data.m_byte_order;
287 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
288 }
289
290 // We have a DataExtractor object that just has a pointer to bytes
291 if (data.ValidOffset(data_offset))
292 {
293 if (data_length > data.GetByteSize() - data_offset)
294 data_length = data.GetByteSize() - data_offset;
295 return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
296 }
297 return 0;
298}
299
300//----------------------------------------------------------------------
301// Assign the data for this object to be a subrange of the shared
302// data in "data_sp" starting "data_offset" bytes into "data_sp"
303// and ending "data_length" bytes later. If "data_offset" is not
304// a valid offset into "data_sp", then this object will contain no
305// bytes. If "data_offset" is within "data_sp" yet "data_length" is
306// too large, the length will be capped at the number of bytes
307// remaining in "data_sp". A ref counted pointer to the data in
308// "data_sp" will be made in this object IF the number of bytes this
309// object refers to in greater than zero (if at least one byte was
310// available starting at "data_offset") to ensure the data stays
311// around as long as it is needed. The address size and endian swap
312// settings will remain unchanged from their current settings.
313//----------------------------------------------------------------------
314uint32_t
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000315DataExtractor::SetData (const DataBufferSP& data_sp, uint32_t data_offset, uint32_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000316{
317 m_start = m_end = NULL;
318
319 if (data_length > 0)
320 {
321 m_data_sp = data_sp;
322 if (data_sp.get())
323 {
324 const size_t data_size = data_sp->GetByteSize();
325 if (data_offset < data_size)
326 {
327 m_start = data_sp->GetBytes() + data_offset;
328 const size_t bytes_left = data_size - data_offset;
329 // Cap the length of we asked for too many
330 if (data_length <= bytes_left)
331 m_end = m_start + data_length; // We got all the bytes we wanted
332 else
333 m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
334 }
335 }
336 }
337
338 uint32_t new_size = GetByteSize();
339
340 // Don't hold a shared pointer to the data buffer if we don't share
341 // any valid bytes in the shared buffer.
342 if (new_size == 0)
343 m_data_sp.reset();
344
345 return new_size;
346}
347
348//----------------------------------------------------------------------
349// Extract a single unsigned char from the binary data and update
350// the offset pointed to by "offset_ptr".
351//
352// RETURNS the byte that was extracted, or zero on failure.
353//----------------------------------------------------------------------
354uint8_t
355DataExtractor::GetU8 (uint32_t *offset_ptr) const
356{
357 uint8_t val = 0;
358 if ( m_start < m_end )
359 {
360 val = m_start[*offset_ptr];
361 *offset_ptr += sizeof(val);
362 }
363 return val;
364}
365
366//----------------------------------------------------------------------
367// Extract "count" unsigned chars from the binary data and update the
368// offset pointed to by "offset_ptr". The extracted data is copied into
369// "dst".
370//
371// RETURNS the non-NULL buffer pointer upon successful extraction of
372// all the requested bytes, or NULL when the data is not available in
373// the buffer due to being out of bounds, or unsufficient data.
374//----------------------------------------------------------------------
375void *
376DataExtractor::GetU8 (uint32_t *offset_ptr, void *dst, uint32_t count) const
377{
378 register uint32_t offset = *offset_ptr;
379
380 if ((count > 0) && ValidOffsetForDataOfSize(offset, count) )
381 {
382 // Copy the data into the buffer
383 memcpy (dst, m_start + offset, count);
384 // Advance the offset
385 *offset_ptr += count;
386 // Return a non-NULL pointer to the converted data as an indicator of success
387 return dst;
388 }
389 return NULL;
390}
391
392//----------------------------------------------------------------------
393// Extract a single uint16_t from the data and update the offset
394// pointed to by "offset_ptr".
395//
396// RETURNS the uint16_t that was extracted, or zero on failure.
397//----------------------------------------------------------------------
398uint16_t
399DataExtractor::GetU16 (uint32_t *offset_ptr) const
400{
401 uint16_t val = 0;
402 register uint32_t offset = *offset_ptr;
403 if ( ValidOffsetForDataOfSize(offset, sizeof(val)) )
404 {
Greg Claytoncd548032011-02-01 01:31:41 +0000405 if (m_byte_order != lldb::endian::InlHostByteOrder())
Greg Clayton2519d992010-06-12 01:03:17 +0000406 val = ReadSwapInt16(m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000407 else
Greg Clayton2519d992010-06-12 01:03:17 +0000408 val = ReadInt16 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000409
410 // Advance the offset
411 *offset_ptr += sizeof(val);
412 }
413 return val;
414}
415
Greg Clayton5fcff9a2010-09-15 08:33:30 +0000416uint16_t
417DataExtractor::GetU16_unchecked (uint32_t *offset_ptr) const
418{
Greg Claytoncd548032011-02-01 01:31:41 +0000419 uint16_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ?
Greg Clayton5fcff9a2010-09-15 08:33:30 +0000420 ReadInt16 (m_start, *offset_ptr) :
421 ReadSwapInt16(m_start, *offset_ptr);
422 *offset_ptr += sizeof(val);
423 return val;
424}
425
426uint32_t
427DataExtractor::GetU32_unchecked (uint32_t *offset_ptr) const
428{
Greg Claytoncd548032011-02-01 01:31:41 +0000429 uint32_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ?
Greg Clayton5fcff9a2010-09-15 08:33:30 +0000430 ReadInt32 (m_start, *offset_ptr) :
431 ReadSwapInt32 (m_start, *offset_ptr);
432 *offset_ptr += sizeof(val);
433 return val;
434}
435
436uint64_t
437DataExtractor::GetU64_unchecked (uint32_t *offset_ptr) const
438{
Greg Claytoncd548032011-02-01 01:31:41 +0000439 uint64_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ?
Greg Clayton5fcff9a2010-09-15 08:33:30 +0000440 ReadInt64 (m_start, *offset_ptr) :
441 ReadSwapInt64 (m_start, *offset_ptr);
442 *offset_ptr += sizeof(val);
443 return val;
444}
445
446
Chris Lattner24943d22010-06-08 16:52:24 +0000447//----------------------------------------------------------------------
448// Extract "count" uint16_t values from the binary data and update
449// the offset pointed to by "offset_ptr". The extracted data is
450// copied into "dst".
451//
452// RETURNS the non-NULL buffer pointer upon successful extraction of
453// all the requested bytes, or NULL when the data is not available
454// in the buffer due to being out of bounds, or unsufficient data.
455//----------------------------------------------------------------------
456void *
457DataExtractor::GetU16 (uint32_t *offset_ptr, void *void_dst, uint32_t count) const
458{
459 uint16_t *dst = (uint16_t *)void_dst;
460 const size_t value_size = sizeof(*dst);
461 register uint32_t offset = *offset_ptr;
462
463 if ((count > 0) && ValidOffsetForDataOfSize(offset, value_size * count) )
464 {
465 uint16_t *value_ptr;
466 uint16_t *end = dst + count;
Greg Claytoncd548032011-02-01 01:31:41 +0000467 if (m_byte_order != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +0000468 {
469 for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
Greg Clayton2519d992010-06-12 01:03:17 +0000470 *value_ptr = ReadSwapInt16 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000471 }
472 else
473 {
474 for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
Greg Clayton2519d992010-06-12 01:03:17 +0000475 *value_ptr = ReadInt16 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000476 }
477
478 // Advance the offset
479 *offset_ptr = offset;
480 // Return a non-NULL pointer to the converted data as an indicator of success
481 return dst;
482 }
483 return NULL;
484}
485
486//----------------------------------------------------------------------
487// Extract a single uint32_t from the data and update the offset
488// pointed to by "offset_ptr".
489//
490// RETURNS the uint32_t that was extracted, or zero on failure.
491//----------------------------------------------------------------------
492uint32_t
493DataExtractor::GetU32 (uint32_t *offset_ptr) const
494{
495 uint32_t val = 0;
496 register uint32_t offset = *offset_ptr;
497
498 if ( ValidOffsetForDataOfSize(offset, sizeof(val)) )
499 {
Greg Claytoncd548032011-02-01 01:31:41 +0000500 if (m_byte_order != lldb::endian::InlHostByteOrder())
Greg Clayton2519d992010-06-12 01:03:17 +0000501 val = ReadSwapInt32 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000502 else
Greg Clayton2519d992010-06-12 01:03:17 +0000503 val = ReadInt32 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000504
505 // Advance the offset
506 *offset_ptr += sizeof(val);
507 }
508 return val;
509}
510
511//----------------------------------------------------------------------
512// Extract "count" uint32_t values from the binary data and update
513// the offset pointed to by "offset_ptr". The extracted data is
514// copied into "dst".
515//
516// RETURNS the non-NULL buffer pointer upon successful extraction of
517// all the requested bytes, or NULL when the data is not available
518// in the buffer due to being out of bounds, or unsufficient data.
519//----------------------------------------------------------------------
520void *
521DataExtractor::GetU32 (uint32_t *offset_ptr, void *void_dst, uint32_t count) const
522{
523 uint32_t *dst = (uint32_t *)void_dst;
524 const size_t value_size = sizeof(*dst);
525 register uint32_t offset = *offset_ptr;
526
527 if ((count > 0) && ValidOffsetForDataOfSize(offset, value_size * count))
528 {
529 uint32_t *value_ptr;
530 uint32_t *end = dst + count;
Greg Claytoncd548032011-02-01 01:31:41 +0000531 if (m_byte_order != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +0000532 {
533 for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
Greg Clayton2519d992010-06-12 01:03:17 +0000534 *value_ptr = ReadSwapInt32 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000535
536 }
537 else
538 {
539 for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
Greg Clayton2519d992010-06-12 01:03:17 +0000540 *value_ptr = ReadInt32 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000541 }
542
543 // Advance the offset
544 *offset_ptr = offset;
545 // Return a non-NULL pointer to the converted data as an indicator of success
546 return dst;
547 }
548 return NULL;
549}
550
551//----------------------------------------------------------------------
552// Extract a single uint64_t from the data and update the offset
553// pointed to by "offset_ptr".
554//
555// RETURNS the uint64_t that was extracted, or zero on failure.
556//----------------------------------------------------------------------
557uint64_t
558DataExtractor::GetU64 (uint32_t *offset_ptr) const
559{
560 uint64_t val = 0;
561 register uint32_t offset = *offset_ptr;
562 if ( ValidOffsetForDataOfSize(offset, sizeof(val)) )
563 {
Greg Claytoncd548032011-02-01 01:31:41 +0000564 if (m_byte_order != lldb::endian::InlHostByteOrder())
Greg Clayton2519d992010-06-12 01:03:17 +0000565 val = ReadSwapInt64 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000566 else
Greg Clayton2519d992010-06-12 01:03:17 +0000567 val = ReadInt64 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000568
569 // Advance the offset
570 *offset_ptr += sizeof(val);
571 }
572 return val;
573}
574
575//----------------------------------------------------------------------
576// GetU64
577//
578// Get multiple consecutive 64 bit values. Return true if the entire
579// read succeeds and increment the offset pointed to by offset_ptr, else
580// return false and leave the offset pointed to by offset_ptr unchanged.
581//----------------------------------------------------------------------
582void *
583DataExtractor::GetU64 (uint32_t *offset_ptr, void *void_dst, uint32_t count) const
584{
585 uint64_t *dst = (uint64_t *)void_dst;
586 const size_t value_size = sizeof(uint64_t);
587 register uint32_t offset = *offset_ptr;
588
589 if ((count > 0) && ValidOffsetForDataOfSize(offset, value_size * count))
590 {
591 uint64_t *value_ptr;
592 uint64_t *end = dst + count;
Greg Claytoncd548032011-02-01 01:31:41 +0000593 if (m_byte_order != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +0000594 {
595 for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
Greg Clayton2519d992010-06-12 01:03:17 +0000596 *value_ptr = ReadSwapInt64 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000597
598 }
599 else
600 {
601 for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
Greg Clayton2519d992010-06-12 01:03:17 +0000602 *value_ptr = ReadInt64 (m_start, offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000603 }
604
605 // Advance the offset
606 *offset_ptr = offset;
607 // Return a non-NULL pointer to the converted data as an indicator of success
608 return dst;
609 }
610 return NULL;
611}
612
613//----------------------------------------------------------------------
614// Extract a single integer value from the data and update the offset
615// pointed to by "offset_ptr". The size of the extracted integer
616// is specified by the "byte_size" argument. "byte_size" should have
617// a value between 1 and 4 since the return value is only 32 bits
618// wide. Any "byte_size" values less than 1 or greater than 4 will
619// result in nothing being extracted, and zero being returned.
620//
621// RETURNS the integer value that was extracted, or zero on failure.
622//----------------------------------------------------------------------
623uint32_t
624DataExtractor::GetMaxU32 (uint32_t *offset_ptr, uint32_t byte_size) const
625{
626 switch (byte_size)
627 {
628 case 1: return GetU8 (offset_ptr); break;
629 case 2: return GetU16(offset_ptr); break;
630 case 4: return GetU32(offset_ptr); break;
631 default:
632 assert(!"GetMaxU32 unhandled case!");
633 break;
634 }
635 return 0;
636}
637
638//----------------------------------------------------------------------
639// Extract a single integer value from the data and update the offset
640// pointed to by "offset_ptr". The size of the extracted integer
641// is specified by the "byte_size" argument. "byte_size" should have
642// a value >= 1 and <= 8 since the return value is only 64 bits
643// wide. Any "byte_size" values less than 1 or greater than 8 will
644// result in nothing being extracted, and zero being returned.
645//
646// RETURNS the integer value that was extracted, or zero on failure.
647//----------------------------------------------------------------------
648uint64_t
649DataExtractor::GetMaxU64 (uint32_t *offset_ptr, uint32_t size) const
650{
651 switch (size)
652 {
653 case 1: return GetU8 (offset_ptr); break;
654 case 2: return GetU16(offset_ptr); break;
655 case 4: return GetU32(offset_ptr); break;
656 case 8: return GetU64(offset_ptr); break;
657 default:
658 assert(!"GetMax64 unhandled case!");
659 break;
660 }
661 return 0;
662}
663
Greg Clayton0fea0512011-12-30 00:32:24 +0000664uint64_t
665DataExtractor::GetMaxU64_unchecked (uint32_t *offset_ptr, uint32_t size) const
666{
667 switch (size)
668 {
669 case 1: return GetU8_unchecked (offset_ptr); break;
670 case 2: return GetU16_unchecked (offset_ptr); break;
671 case 4: return GetU32_unchecked (offset_ptr); break;
672 case 8: return GetU64_unchecked (offset_ptr); break;
673 default:
674 assert(!"GetMax64 unhandled case!");
675 break;
676 }
677 return 0;
678}
679
Chris Lattner24943d22010-06-08 16:52:24 +0000680int64_t
681DataExtractor::GetMaxS64 (uint32_t *offset_ptr, uint32_t size) const
682{
683 switch (size)
684 {
685 case 1: return (int8_t)GetU8 (offset_ptr); break;
686 case 2: return (int16_t)GetU16(offset_ptr); break;
687 case 4: return (int32_t)GetU32(offset_ptr); break;
688 case 8: return (int64_t)GetU64(offset_ptr); break;
689 default:
690 assert(!"GetMax64 unhandled case!");
691 break;
692 }
693 return 0;
694}
695
696uint64_t
697DataExtractor::GetMaxU64Bitfield (uint32_t *offset_ptr, uint32_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
698{
699 uint64_t uval64 = GetMaxU64 (offset_ptr, size);
700 if (bitfield_bit_size > 0)
701 {
702 if (bitfield_bit_offset > 0)
703 uval64 >>= bitfield_bit_offset;
Enrico Granata9762e102011-07-06 02:13:41 +0000704 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000705 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
Enrico Granata9762e102011-07-06 02:13:41 +0000706 return uval64;
Chris Lattner24943d22010-06-08 16:52:24 +0000707 uval64 &= bitfield_mask;
708 }
709 return uval64;
710}
711
712int64_t
713DataExtractor::GetMaxS64Bitfield (uint32_t *offset_ptr, uint32_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
714{
715 int64_t sval64 = GetMaxS64 (offset_ptr, size);
716 if (bitfield_bit_size > 0)
717 {
718 if (bitfield_bit_offset > 0)
719 sval64 >>= bitfield_bit_offset;
Greg Clayton36d507f2012-10-31 20:56:43 +0000720 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000721 sval64 &= bitfield_mask;
722 // sign extend if needed
Greg Clayton36d507f2012-10-31 20:56:43 +0000723 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
Chris Lattner24943d22010-06-08 16:52:24 +0000724 sval64 |= ~bitfield_mask;
725 }
726 return sval64;
727}
728
729
730float
731DataExtractor::GetFloat (uint32_t *offset_ptr) const
732{
Greg Claytonc7527b12010-09-14 04:09:57 +0000733 typedef float float_type;
734 float_type val = 0.0;
735 const uint8_t *src_data = PeekData (*offset_ptr, sizeof(float_type));
736
737 if (src_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000738 {
Greg Claytoncd548032011-02-01 01:31:41 +0000739 if (m_byte_order != lldb::endian::InlHostByteOrder())
Greg Claytonc7527b12010-09-14 04:09:57 +0000740 {
741 uint8_t *dst_data = (uint8_t *)&val;
742 for (int i=0; i<sizeof(float_type); ++i)
743 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
744 }
Chris Lattner24943d22010-06-08 16:52:24 +0000745 else
Greg Claytonc7527b12010-09-14 04:09:57 +0000746 {
747 ::memcpy (&val, src_data, sizeof (float_type));
748 }
Chris Lattner24943d22010-06-08 16:52:24 +0000749
750 // Advance the offset
751 *offset_ptr += sizeof(val);
752 }
Greg Claytonc7527b12010-09-14 04:09:57 +0000753 return val;
Chris Lattner24943d22010-06-08 16:52:24 +0000754}
755
756double
757DataExtractor::GetDouble (uint32_t *offset_ptr) const
758{
Greg Claytonc7527b12010-09-14 04:09:57 +0000759 typedef double float_type;
760 float_type val = 0.0;
761 const uint8_t *src_data = PeekData (*offset_ptr, sizeof(float_type));
762
763 if (src_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000764 {
Greg Claytoncd548032011-02-01 01:31:41 +0000765 if (m_byte_order != lldb::endian::InlHostByteOrder())
Greg Claytonc7527b12010-09-14 04:09:57 +0000766 {
767 uint8_t *dst_data = (uint8_t *)&val;
768 for (int i=0; i<sizeof(float_type); ++i)
769 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
770 }
Chris Lattner24943d22010-06-08 16:52:24 +0000771 else
Greg Claytonc7527b12010-09-14 04:09:57 +0000772 {
773 ::memcpy (&val, src_data, sizeof (float_type));
774 }
Chris Lattner24943d22010-06-08 16:52:24 +0000775
776 // Advance the offset
777 *offset_ptr += sizeof(val);
778 }
Greg Claytonc7527b12010-09-14 04:09:57 +0000779 return val;
Chris Lattner24943d22010-06-08 16:52:24 +0000780}
781
782
783long double
784DataExtractor::GetLongDouble (uint32_t *offset_ptr) const
785{
Greg Claytonc7527b12010-09-14 04:09:57 +0000786 typedef long double float_type;
787 float_type val = 0.0;
788 const uint8_t *src_data = PeekData (*offset_ptr, sizeof(float_type));
789
790 if (src_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000791 {
Greg Claytoncd548032011-02-01 01:31:41 +0000792 if (m_byte_order != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +0000793 {
Greg Claytonc7527b12010-09-14 04:09:57 +0000794 uint8_t *dst_data = (uint8_t *)&val;
795 for (int i=0; i<sizeof(float_type); ++i)
796 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
Chris Lattner24943d22010-06-08 16:52:24 +0000797 }
Greg Claytonc7527b12010-09-14 04:09:57 +0000798 else
799 {
800 ::memcpy (&val, src_data, sizeof (float_type));
801 }
802
803 // Advance the offset
804 *offset_ptr += sizeof(val);
Chris Lattner24943d22010-06-08 16:52:24 +0000805 }
Greg Claytonc7527b12010-09-14 04:09:57 +0000806 return val;
Chris Lattner24943d22010-06-08 16:52:24 +0000807}
808
809
810//------------------------------------------------------------------
811// Extract a single address from the data and update the offset
812// pointed to by "offset_ptr". The size of the extracted address
813// comes from the "this->m_addr_size" member variable and should be
814// set correctly prior to extracting any address values.
815//
816// RETURNS the address that was extracted, or zero on failure.
817//------------------------------------------------------------------
818uint64_t
819DataExtractor::GetAddress (uint32_t *offset_ptr) const
820{
821 return GetMaxU64 (offset_ptr, m_addr_size);
822}
823
Greg Clayton0fea0512011-12-30 00:32:24 +0000824uint64_t
825DataExtractor::GetAddress_unchecked (uint32_t *offset_ptr) const
826{
827 return GetMaxU64_unchecked (offset_ptr, m_addr_size);
828}
829
Chris Lattner24943d22010-06-08 16:52:24 +0000830//------------------------------------------------------------------
831// Extract a single pointer from the data and update the offset
832// pointed to by "offset_ptr". The size of the extracted pointer
833// comes from the "this->m_addr_size" member variable and should be
834// set correctly prior to extracting any pointer values.
835//
836// RETURNS the pointer that was extracted, or zero on failure.
837//------------------------------------------------------------------
838uint64_t
839DataExtractor::GetPointer (uint32_t *offset_ptr) const
840{
841 return GetMaxU64 (offset_ptr, m_addr_size);
842}
843
844//----------------------------------------------------------------------
845// GetDwarfEHPtr
846//
847// Used for calls when the value type is specified by a DWARF EH Frame
848// pointer encoding.
849//----------------------------------------------------------------------
850
851uint64_t
852DataExtractor::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)//, BSDRelocs *data_relocs) const
853{
Jason Molendaccfba722010-07-06 22:38:03 +0000854 if (eh_ptr_enc == DW_EH_PE_omit)
Greg Clayton381f9682011-04-01 18:14:08 +0000855 return ULLONG_MAX; // Value isn't in the buffer...
Chris Lattner24943d22010-06-08 16:52:24 +0000856
857 uint64_t baseAddress = 0;
858 uint64_t addressValue = 0;
859 const uint32_t addr_size = GetAddressByteSize();
860
861 bool signExtendValue = false;
862 // Decode the base part or adjust our offset
863 switch (eh_ptr_enc & 0x70)
864 {
Jason Molendaccfba722010-07-06 22:38:03 +0000865 case DW_EH_PE_pcrel:
Chris Lattner24943d22010-06-08 16:52:24 +0000866 signExtendValue = true;
867 baseAddress = *offset_ptr;
868 if (pc_rel_addr != LLDB_INVALID_ADDRESS)
869 baseAddress += pc_rel_addr;
870// else
871// Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
872 break;
873
Jason Molendaccfba722010-07-06 22:38:03 +0000874 case DW_EH_PE_textrel:
Chris Lattner24943d22010-06-08 16:52:24 +0000875 signExtendValue = true;
876 if (text_addr != LLDB_INVALID_ADDRESS)
877 baseAddress = text_addr;
878// else
879// Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
880 break;
881
Jason Molendaccfba722010-07-06 22:38:03 +0000882 case DW_EH_PE_datarel:
Chris Lattner24943d22010-06-08 16:52:24 +0000883 signExtendValue = true;
884 if (data_addr != LLDB_INVALID_ADDRESS)
885 baseAddress = data_addr;
886// else
887// Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
888 break;
889
Jason Molendaccfba722010-07-06 22:38:03 +0000890 case DW_EH_PE_funcrel:
Chris Lattner24943d22010-06-08 16:52:24 +0000891 signExtendValue = true;
892 break;
893
Jason Molendaccfba722010-07-06 22:38:03 +0000894 case DW_EH_PE_aligned:
Chris Lattner24943d22010-06-08 16:52:24 +0000895 {
896 // SetPointerSize should be called prior to extracting these so the
897 // pointer size is cached
898 assert(addr_size != 0);
899 if (addr_size)
900 {
901 // Align to a address size boundary first
902 uint32_t alignOffset = *offset_ptr % addr_size;
903 if (alignOffset)
904 offset_ptr += addr_size - alignOffset;
905 }
906 }
907 break;
908
909 default:
910 break;
911 }
912
913 // Decode the value part
Jason Molendaccfba722010-07-06 22:38:03 +0000914 switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
Chris Lattner24943d22010-06-08 16:52:24 +0000915 {
Jason Molendaccfba722010-07-06 22:38:03 +0000916 case DW_EH_PE_absptr :
Chris Lattner24943d22010-06-08 16:52:24 +0000917 {
918 addressValue = GetAddress (offset_ptr);
919// if (data_relocs)
920// addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
921 }
922 break;
Jason Molendaccfba722010-07-06 22:38:03 +0000923 case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
924 case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
925 case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
926 case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
927 case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
928 case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
929 case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
930 case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
Chris Lattner24943d22010-06-08 16:52:24 +0000931 default:
932 // Unhandled encoding type
933 assert(eh_ptr_enc);
934 break;
935 }
936
937 // Since we promote everything to 64 bit, we may need to sign extend
938 if (signExtendValue && addr_size < sizeof(baseAddress))
939 {
940 uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
941 if (sign_bit & addressValue)
942 {
943 uint64_t mask = ~sign_bit + 1;
944 addressValue |= mask;
945 }
946 }
947 return baseAddress + addressValue;
948}
949
950size_t
951DataExtractor::ExtractBytes (uint32_t offset, uint32_t length, ByteOrder dst_byte_order, void *dst) const
952{
953 const uint8_t *src = PeekData (offset, length);
954 if (src)
955 {
956 if (dst_byte_order != GetByteOrder())
957 {
958 for (uint32_t i=0; i<length; ++i)
959 ((uint8_t*)dst)[i] = src[length - i - 1];
960 }
961 else
962 ::memcpy (dst, src, length);
963 return length;
964 }
965 return 0;
966}
967//----------------------------------------------------------------------
968// Peeks at bytes in the contained data.
969//
970// Returns a valid pointer to bytes if "offset" is a valid offset in
971// and there are "length" bytes available, else NULL is returned.
972//----------------------------------------------------------------------
973const uint8_t*
974DataExtractor::PeekData (uint32_t offset, uint32_t length) const
975{
976 if ( length > 0 && ValidOffsetForDataOfSize(offset, length) )
977 return m_start + offset;
978 return NULL;
979}
980
981//----------------------------------------------------------------------
982// Returns a pointer to a bytes in this object's data at the offset
983// pointed to by "offset_ptr". If "length" is zero or too large,
984// then the offset pointed to by "offset_ptr" will not be updated
985// and NULL will be returned.
986//
987// Returns a pointer to the data if the offset and length are valid,
988// or NULL otherwise.
989//----------------------------------------------------------------------
990const void*
991DataExtractor::GetData (uint32_t *offset_ptr, uint32_t length) const
992{
993 const uint8_t* bytes = NULL;
994 register uint32_t offset = *offset_ptr;
995 if ( length > 0 && ValidOffsetForDataOfSize(offset, length) )
996 {
997 bytes = m_start + offset;
998 *offset_ptr = offset + length;
999 }
1000 return bytes;
1001}
1002
Greg Clayton061b79d2011-05-09 20:18:18 +00001003// Extract data and swap if needed when doing the copy
1004uint32_t
1005DataExtractor::CopyByteOrderedData (uint32_t src_offset,
1006 uint32_t src_len,
1007 void *dst_void_ptr,
1008 uint32_t dst_len,
1009 ByteOrder dst_byte_order) const
1010{
1011 // Validate the source info
1012 assert (ValidOffsetForDataOfSize(src_offset, src_len));
1013 assert (src_len > 0);
1014 assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
1015
1016 // Validate the destination info
1017 assert (dst_void_ptr != NULL);
1018 assert (dst_len > 0);
1019 assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
1020
1021 // Must have valid byte orders set in this object and for destination
1022 if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
1023 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
1024 return 0;
1025
1026 uint32_t i;
1027 uint8_t* dst = (uint8_t*)dst_void_ptr;
1028 const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
1029 if (src)
1030 {
Greg Claytonc0fa5332011-05-22 22:46:53 +00001031 if (dst_len >= src_len)
Greg Clayton061b79d2011-05-09 20:18:18 +00001032 {
1033 // We are copying the entire value from src into dst.
1034 // Calculate how many, if any, zeroes we need for the most
1035 // significant bytes if "dst_len" is greater than "src_len"...
1036 const uint32_t num_zeroes = dst_len - src_len;
1037 if (dst_byte_order == eByteOrderBig)
1038 {
1039 // Big endian, so we lead with zeroes...
1040 if (num_zeroes > 0)
1041 ::memset (dst, 0, num_zeroes);
1042 // Then either copy or swap the rest
1043 if (m_byte_order == eByteOrderBig)
1044 {
1045 ::memcpy (dst + num_zeroes, src, src_len);
1046 }
1047 else
1048 {
1049 for (i=0; i<src_len; ++i)
1050 dst[i+num_zeroes] = src[src_len - 1 - i];
1051 }
1052 }
1053 else
1054 {
1055 // Little endian destination, so we lead the value bytes
1056 if (m_byte_order == eByteOrderBig)
1057 {
1058 for (i=0; i<src_len; ++i)
1059 dst[i] = src[src_len - 1 - i];
1060 }
1061 else
1062 {
1063 ::memcpy (dst, src, src_len);
1064 }
1065 // And zero the rest...
1066 if (num_zeroes > 0)
1067 ::memset (dst + src_len, 0, num_zeroes);
1068 }
1069 return src_len;
1070 }
1071 else
1072 {
1073 // We are only copying some of the value from src into dst..
1074
1075 if (dst_byte_order == eByteOrderBig)
1076 {
1077 // Big endian dst
1078 if (m_byte_order == eByteOrderBig)
1079 {
1080 // Big endian dst, with big endian src
1081 ::memcpy (dst, src + (src_len - dst_len), dst_len);
1082 }
1083 else
1084 {
1085 // Big endian dst, with little endian src
1086 for (i=0; i<dst_len; ++i)
1087 dst[i] = src[dst_len - 1 - i];
1088 }
1089 }
1090 else
1091 {
1092 // Little endian dst
1093 if (m_byte_order == eByteOrderBig)
1094 {
1095 // Little endian dst, with big endian src
1096 for (i=0; i<dst_len; ++i)
1097 dst[i] = src[src_len - 1 - i];
1098 }
1099 else
1100 {
1101 // Little endian dst, with big endian src
1102 ::memcpy (dst, src, dst_len);
1103 }
1104 }
1105 return dst_len;
1106 }
1107
1108 }
1109 return 0;
1110}
1111
1112
Chris Lattner24943d22010-06-08 16:52:24 +00001113//----------------------------------------------------------------------
1114// Extracts a AsCString (fixed length, or variable length) from
1115// the data at the offset pointed to by "offset_ptr". If
1116// "length" is zero, then a variable length NULL terminated C
1117// string will be extracted from the data the "offset_ptr" will be
1118// updated with the offset of the byte that follows the NULL
1119// terminator byte. If "length" is greater than zero, then
1120// the function will make sure there are "length" bytes
1121// available in the current data and if so, return a valid pointer.
1122//
1123// If the offset pointed to by "offset_ptr" is out of bounds, or if
1124// "length" is non-zero and there aren't enough avaialable
1125// bytes, NULL will be returned and "offset_ptr" will not be
1126// updated.
1127//----------------------------------------------------------------------
1128const char*
1129DataExtractor::GetCStr (uint32_t *offset_ptr) const
1130{
1131 const char *s = NULL;
1132 if ( m_start < m_end )
1133 {
1134 s = (char*)m_start + *offset_ptr;
1135
1136 size_t length = strlen(s) + 1;
1137
1138 if (!ValidOffsetForDataOfSize(*offset_ptr, length))
1139 return NULL;
1140
1141 // Advance the offset
1142 *offset_ptr += length;
1143 }
1144 return s;
1145}
1146
1147//------------------------------------------------------------------
1148// Peeks at a string in the contained data. No verification is done
1149// to make sure the entire string lies within the bounds of this
1150// object's data, only "offset" is verified to be a valid offset.
1151//
1152// Returns a valid C string pointer if "offset" is a valid offset in
1153// this object's data, else NULL is returned.
1154//------------------------------------------------------------------
1155const char *
1156DataExtractor::PeekCStr (uint32_t offset) const
1157{
1158 if (ValidOffset (offset))
1159 return (const char*)m_start + offset;
1160 return NULL;
1161}
1162
1163//----------------------------------------------------------------------
1164// Extracts an unsigned LEB128 number from this object's data
1165// starting at the offset pointed to by "offset_ptr". The offset
1166// pointed to by "offset_ptr" will be updated with the offset of the
1167// byte following the last extracted byte.
1168//
1169// Returned the extracted integer value.
1170//----------------------------------------------------------------------
1171uint64_t
1172DataExtractor::GetULEB128 (uint32_t *offset_ptr) const
1173{
Greg Clayton36deaee2011-11-14 22:56:58 +00001174 const uint8_t *src = m_start + *offset_ptr;
1175 const uint8_t *end = m_end;
1176
1177 if (src < end)
Chris Lattner24943d22010-06-08 16:52:24 +00001178 {
Greg Clayton36deaee2011-11-14 22:56:58 +00001179 uint64_t result = *src++;
1180 if (result >= 0x80)
Chris Lattner24943d22010-06-08 16:52:24 +00001181 {
Greg Clayton36deaee2011-11-14 22:56:58 +00001182 result &= 0x7f;
1183 int shift = 7;
1184 while (src < end)
1185 {
1186 uint8_t byte = *src++;
1187 result |= (byte & 0x7f) << shift;
1188 if ((byte & 0x80) == 0)
1189 break;
1190 shift += 7;
1191 }
Chris Lattner24943d22010-06-08 16:52:24 +00001192 }
Greg Clayton36deaee2011-11-14 22:56:58 +00001193 *offset_ptr = (uint32_t)(src - m_start);
1194 return result;
Chris Lattner24943d22010-06-08 16:52:24 +00001195 }
Greg Clayton36deaee2011-11-14 22:56:58 +00001196
1197 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001198}
1199
1200//----------------------------------------------------------------------
1201// Extracts an signed LEB128 number from this object's data
1202// starting at the offset pointed to by "offset_ptr". The offset
1203// pointed to by "offset_ptr" will be updated with the offset of the
1204// byte following the last extracted byte.
1205//
1206// Returned the extracted integer value.
1207//----------------------------------------------------------------------
1208int64_t
1209DataExtractor::GetSLEB128 (uint32_t *offset_ptr) const
1210{
1211 int64_t result = 0;
1212
1213 if ( m_start < m_end )
1214 {
1215 int shift = 0;
1216 int size = sizeof (uint32_t) * 8;
1217 const uint8_t *src = m_start + *offset_ptr;
1218
Greg Clayton54e7afa2010-07-09 20:39:50 +00001219 uint8_t byte = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001220 int bytecount = 0;
1221
1222 while (src < m_end)
1223 {
1224 bytecount++;
1225 byte = *src++;
1226 result |= (byte & 0x7f) << shift;
1227 shift += 7;
1228 if ((byte & 0x80) == 0)
1229 break;
1230 }
1231
1232 // Sign bit of byte is 2nd high order bit (0x40)
1233 if (shift < size && (byte & 0x40))
1234 result |= - (1 << shift);
1235
1236 *offset_ptr += bytecount;
1237 }
1238 return result;
1239}
1240
1241//----------------------------------------------------------------------
1242// Skips a ULEB128 number (signed or unsigned) from this object's
1243// data starting at the offset pointed to by "offset_ptr". The
1244// offset pointed to by "offset_ptr" will be updated with the offset
1245// of the byte following the last extracted byte.
1246//
1247// Returns the number of bytes consumed during the extraction.
1248//----------------------------------------------------------------------
1249uint32_t
1250DataExtractor::Skip_LEB128 (uint32_t *offset_ptr) const
1251{
1252 uint32_t bytes_consumed = 0;
1253 if ( m_start < m_end )
1254 {
1255 const uint8_t *start = m_start + *offset_ptr;
1256 const uint8_t *src = start;
1257
1258 while ((src < m_end) && (*src++ & 0x80))
1259 ++bytes_consumed;
1260
1261 *offset_ptr += src - start;
1262 }
1263 return bytes_consumed;
1264}
1265
Greg Claytona4e0dad2011-08-15 02:24:40 +00001266static uint32_t
1267DumpAPInt (Stream *s, const DataExtractor &data, uint32_t offset, uint32_t byte_size, bool is_signed, unsigned radix)
1268{
1269 llvm::SmallVector<uint64_t, 2> uint64_array;
1270 uint32_t bytes_left = byte_size;
1271 uint64_t u64;
1272 const lldb::ByteOrder byte_order = data.GetByteOrder();
1273 if (byte_order == lldb::eByteOrderLittle)
1274 {
1275 while (bytes_left > 0)
1276 {
1277 if (bytes_left >= 8)
1278 {
1279 u64 = data.GetU64(&offset);
1280 bytes_left -= 8;
1281 }
1282 else
1283 {
1284 u64 = data.GetMaxU64(&offset, bytes_left);
1285 bytes_left = 0;
1286 }
1287 uint64_array.push_back(u64);
1288 }
1289 }
1290 else if (byte_order == lldb::eByteOrderBig)
1291 {
1292 uint32_t be_offset = offset + byte_size;
1293 uint32_t temp_offset;
1294 while (bytes_left > 0)
1295 {
1296 if (bytes_left >= 8)
1297 {
1298 be_offset -= 8;
1299 temp_offset = be_offset;
1300 u64 = data.GetU64(&temp_offset);
1301 bytes_left -= 8;
1302 }
1303 else
1304 {
1305 be_offset -= bytes_left;
1306 temp_offset = be_offset;
1307 u64 = data.GetMaxU64(&temp_offset, bytes_left);
1308 bytes_left = 0;
1309 }
1310 uint64_array.push_back(u64);
1311 }
1312 }
1313 else
1314 return offset;
1315
Greg Clayton39f54e52011-08-15 07:23:47 +00001316 llvm::APInt apint (byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
Greg Claytona4e0dad2011-08-15 02:24:40 +00001317
1318 std::string apint_str(apint.toString(radix, is_signed));
1319 switch (radix)
1320 {
1321 case 2:
1322 s->Write ("0b", 2);
1323 break;
1324 case 8:
1325 s->Write ("0", 1);
1326 break;
1327 case 10:
1328 break;
1329 }
1330 s->Write(apint_str.c_str(), apint_str.size());
1331 return offset;
1332}
1333
Chris Lattner24943d22010-06-08 16:52:24 +00001334uint32_t
Greg Clayton24a6bd92011-10-27 17:55:14 +00001335DataExtractor::Dump (Stream *s,
1336 uint32_t start_offset,
1337 lldb::Format item_format,
1338 uint32_t item_byte_size,
1339 uint32_t item_count,
1340 uint32_t num_per_line,
1341 uint64_t base_addr,
1342 uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
1343 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
1344 ExecutionContextScope *exe_scope) const
Chris Lattner24943d22010-06-08 16:52:24 +00001345{
1346 if (s == NULL)
1347 return start_offset;
1348
Chris Lattner24943d22010-06-08 16:52:24 +00001349 if (item_format == eFormatPointer)
1350 {
1351 if (item_byte_size != 4 && item_byte_size != 8)
1352 item_byte_size = s->GetAddressByteSize();
1353 }
Greg Claytone179a582011-01-05 18:43:15 +00001354
Greg Clayton746979d2011-10-28 23:44:55 +00001355 uint32_t offset = start_offset;
1356
Greg Clayton24a6bd92011-10-27 17:55:14 +00001357 if (item_format == eFormatInstruction)
1358 {
Greg Clayton289afcb2012-02-18 05:35:26 +00001359 TargetSP target_sp;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001360 if (exe_scope)
Greg Clayton289afcb2012-02-18 05:35:26 +00001361 target_sp = exe_scope->CalculateTarget();
1362 if (target_sp)
Greg Clayton24a6bd92011-10-27 17:55:14 +00001363 {
Greg Clayton289afcb2012-02-18 05:35:26 +00001364 DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL));
Greg Clayton24a6bd92011-10-27 17:55:14 +00001365 if (disassembler_sp)
1366 {
1367 lldb::addr_t addr = base_addr + start_offset;
1368 lldb_private::Address so_addr;
Greg Clayton289afcb2012-02-18 05:35:26 +00001369 if (!target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
Greg Clayton24a6bd92011-10-27 17:55:14 +00001370 {
Greg Clayton3508c382012-02-24 01:59:29 +00001371 so_addr.SetRawAddress(addr);
Greg Clayton24a6bd92011-10-27 17:55:14 +00001372 }
1373
Greg Clayton746979d2011-10-28 23:44:55 +00001374 size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false);
1375
1376 if (bytes_consumed)
Greg Clayton24a6bd92011-10-27 17:55:14 +00001377 {
Greg Clayton746979d2011-10-28 23:44:55 +00001378 offset += bytes_consumed;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001379 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
1380 const bool show_bytes = true;
1381 ExecutionContext exe_ctx;
1382 exe_scope->CalculateExecutionContext(exe_ctx);
1383 disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx);
1384 }
1385 }
1386 }
1387 else
1388 s->Printf ("invalid target");
1389
1390 return offset;
1391 }
1392
1393 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
Greg Claytone179a582011-01-05 18:43:15 +00001394 item_format = eFormatHex;
Chris Lattner24943d22010-06-08 16:52:24 +00001395
Greg Clayton746979d2011-10-28 23:44:55 +00001396 uint32_t line_start_offset = start_offset;
1397 for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
Chris Lattner24943d22010-06-08 16:52:24 +00001398 {
1399 if ((count % num_per_line) == 0)
1400 {
1401 if (count > 0)
1402 {
1403 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1404 {
1405 s->Printf("%*s", (num_per_line - (offset - line_start_offset)) * 3 + 2, "");
1406 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
1407 }
1408 s->EOL();
1409 }
1410 if (base_addr != LLDB_INVALID_ADDRESS)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001411 s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset)));
Chris Lattner24943d22010-06-08 16:52:24 +00001412 line_start_offset = offset;
1413 }
1414 else
1415 if (item_format != eFormatChar &&
1416 item_format != eFormatCharPrintable &&
Greg Clayton307fa072011-06-17 23:50:44 +00001417 item_format != eFormatCharArray &&
Chris Lattner24943d22010-06-08 16:52:24 +00001418 count > 0)
1419 {
1420 s->PutChar(' ');
1421 }
1422
1423 uint32_t i;
1424 switch (item_format)
1425 {
1426 case eFormatBoolean:
Greg Clayton4dd3d902012-02-27 23:00:14 +00001427 if (item_byte_size <= 8)
1428 s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
1429 else
1430 {
1431 s->Printf("error: unsupported byte size (%u) for boolean format", item_byte_size);
1432 return offset;
1433 }
Chris Lattner24943d22010-06-08 16:52:24 +00001434 break;
1435
1436 case eFormatBinary:
Greg Claytona4e0dad2011-08-15 02:24:40 +00001437 if (item_byte_size <= 8)
Chris Lattner24943d22010-06-08 16:52:24 +00001438 {
1439 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
Greg Claytone42cbfb2010-07-21 01:08:41 +00001440 // Avoid std::bitset<64>::to_string() since it is missing in
1441 // earlier C++ libraries
1442 std::string binary_value(64, '0');
1443 std::bitset<64> bits(uval64);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001444 for (i = 0; i < 64; ++i)
Greg Claytone42cbfb2010-07-21 01:08:41 +00001445 if (bits[i])
1446 binary_value[64 - 1 - i] = '1';
Chris Lattner24943d22010-06-08 16:52:24 +00001447 if (item_bit_size > 0)
1448 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
1449 else if (item_byte_size > 0 && item_byte_size <= 8)
1450 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
1451 }
Greg Claytona4e0dad2011-08-15 02:24:40 +00001452 else
1453 {
1454 const bool is_signed = false;
1455 const unsigned radix = 2;
1456 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1457 }
Chris Lattner24943d22010-06-08 16:52:24 +00001458 break;
1459
1460 case eFormatBytes:
1461 case eFormatBytesWithASCII:
1462 for (i=0; i<item_byte_size; ++i)
1463 {
1464 s->Printf ("%2.2x", GetU8(&offset));
1465 }
1466 // Put an extra space between the groups of bytes if more than one
1467 // is being dumped in a group (item_byte_size is more than 1).
1468 if (item_byte_size > 1)
1469 s->PutChar(' ');
1470 break;
1471
1472 case eFormatChar:
1473 case eFormatCharPrintable:
Greg Clayton307fa072011-06-17 23:50:44 +00001474 case eFormatCharArray:
Chris Lattner24943d22010-06-08 16:52:24 +00001475 {
1476 // If we are only printing one character surround it with single
1477 // quotes
1478 if (item_count == 1 && item_format == eFormatChar)
1479 s->PutChar('\'');
1480
Greg Clayton09436872011-11-10 03:38:56 +00001481 const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
Chris Lattner24943d22010-06-08 16:52:24 +00001482 if (isprint(ch))
Greg Clayton09436872011-11-10 03:38:56 +00001483 s->Printf ("%c", (char)ch);
Greg Clayton307fa072011-06-17 23:50:44 +00001484 else if (item_format != eFormatCharPrintable)
Chris Lattner24943d22010-06-08 16:52:24 +00001485 {
1486 switch (ch)
1487 {
Daniel Dunbaraed469f2011-10-31 22:51:00 +00001488 case '\033': s->Printf ("\\e"); break;
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001489 case '\a': s->Printf ("\\a"); break;
1490 case '\b': s->Printf ("\\b"); break;
1491 case '\f': s->Printf ("\\f"); break;
1492 case '\n': s->Printf ("\\n"); break;
1493 case '\r': s->Printf ("\\r"); break;
1494 case '\t': s->Printf ("\\t"); break;
1495 case '\v': s->Printf ("\\v"); break;
1496 case '\0': s->Printf ("\\0"); break;
Greg Claytone179a582011-01-05 18:43:15 +00001497 default:
1498 if (item_byte_size == 1)
Greg Clayton09436872011-11-10 03:38:56 +00001499 s->Printf ("\\x%2.2x", (uint8_t)ch);
Greg Claytone179a582011-01-05 18:43:15 +00001500 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001501 s->Printf ("%" PRIu64, ch);
Greg Claytone179a582011-01-05 18:43:15 +00001502 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001503 }
1504 }
1505 else
1506 {
1507 s->PutChar(NON_PRINTABLE_CHAR);
1508 }
1509
1510 // If we are only printing one character surround it with single quotes
1511 if (item_count == 1 && item_format == eFormatChar)
1512 s->PutChar('\'');
1513 }
1514 break;
1515
Greg Clayton4dd3d902012-02-27 23:00:14 +00001516 case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type
Chris Lattner24943d22010-06-08 16:52:24 +00001517 case eFormatDecimal:
Greg Clayton90bc5c62010-07-06 20:30:35 +00001518 if (item_byte_size <= 8)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001519 s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
Greg Claytona4e0dad2011-08-15 02:24:40 +00001520 else
1521 {
1522 const bool is_signed = true;
1523 const unsigned radix = 10;
1524 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1525 }
Chris Lattner24943d22010-06-08 16:52:24 +00001526 break;
1527
1528 case eFormatUnsigned:
Greg Clayton90bc5c62010-07-06 20:30:35 +00001529 if (item_byte_size <= 8)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001530 s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
Greg Claytona4e0dad2011-08-15 02:24:40 +00001531 else
1532 {
1533 const bool is_signed = false;
1534 const unsigned radix = 10;
1535 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1536 }
Chris Lattner24943d22010-06-08 16:52:24 +00001537 break;
1538
1539 case eFormatOctal:
Greg Clayton90bc5c62010-07-06 20:30:35 +00001540 if (item_byte_size <= 8)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001541 s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
Greg Claytona4e0dad2011-08-15 02:24:40 +00001542 else
1543 {
1544 const bool is_signed = false;
1545 const unsigned radix = 8;
1546 offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
1547 }
Chris Lattner24943d22010-06-08 16:52:24 +00001548 break;
1549
Greg Claytone179a582011-01-05 18:43:15 +00001550 case eFormatOSType:
1551 {
1552 uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
1553 s->PutChar('\'');
1554 for (i=0; i<item_byte_size; ++i)
1555 {
Greg Clayton4cd531f2011-10-26 21:01:16 +00001556 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
Greg Claytone179a582011-01-05 18:43:15 +00001557 if (isprint(ch))
1558 s->Printf ("%c", ch);
1559 else
1560 {
1561 switch (ch)
1562 {
Daniel Dunbarca6bc7e2011-10-31 22:51:02 +00001563 case '\033': s->Printf ("\\e"); break;
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001564 case '\a': s->Printf ("\\a"); break;
1565 case '\b': s->Printf ("\\b"); break;
1566 case '\f': s->Printf ("\\f"); break;
1567 case '\n': s->Printf ("\\n"); break;
1568 case '\r': s->Printf ("\\r"); break;
1569 case '\t': s->Printf ("\\t"); break;
1570 case '\v': s->Printf ("\\v"); break;
1571 case '\0': s->Printf ("\\0"); break;
Greg Claytone179a582011-01-05 18:43:15 +00001572 default: s->Printf ("\\x%2.2x", ch); break;
1573 }
1574 }
1575 }
1576 s->PutChar('\'');
1577 }
1578 break;
1579
Chris Lattner24943d22010-06-08 16:52:24 +00001580 case eFormatCString:
1581 {
1582 const char *cstr = GetCStr(&offset);
Sean Callananccdaf232012-01-04 17:36:30 +00001583
1584 if (!cstr)
Chris Lattner24943d22010-06-08 16:52:24 +00001585 {
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001586 s->Printf("NULL");
Chris Lattner24943d22010-06-08 16:52:24 +00001587 offset = UINT32_MAX;
1588 }
Sean Callananccdaf232012-01-04 17:36:30 +00001589 else
1590 {
1591 s->PutChar('\"');
1592
1593 while (const char c = *cstr)
1594 {
1595 if (isprint(c))
1596 {
1597 s->PutChar(c);
1598 }
1599 else
1600 {
1601 switch (c)
1602 {
1603 case '\033': s->Printf ("\\e"); break;
1604 case '\a': s->Printf ("\\a"); break;
1605 case '\b': s->Printf ("\\b"); break;
1606 case '\f': s->Printf ("\\f"); break;
1607 case '\n': s->Printf ("\\n"); break;
1608 case '\r': s->Printf ("\\r"); break;
1609 case '\t': s->Printf ("\\t"); break;
1610 case '\v': s->Printf ("\\v"); break;
1611 default: s->Printf ("\\x%2.2x", c); break;
1612 }
1613 }
1614
1615 ++cstr;
1616 }
1617
1618 s->PutChar('\"');
1619 }
Chris Lattner24943d22010-06-08 16:52:24 +00001620 }
1621 break;
1622
1623
1624 case eFormatPointer:
1625 s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
1626 break;
1627
Greg Claytone52f37b2011-01-15 02:52:14 +00001628
1629 case eFormatComplexInteger:
1630 {
1631 uint32_t complex_int_byte_size = item_byte_size / 2;
1632
1633 if (complex_int_byte_size <= 8)
1634 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001635 s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
1636 s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
Greg Claytone52f37b2011-01-15 02:52:14 +00001637 }
Greg Clayton4dd3d902012-02-27 23:00:14 +00001638 else
1639 {
1640 s->Printf("error: unsupported byte size (%u) for complex integer format", item_byte_size);
1641 return offset;
1642 }
Greg Claytone52f37b2011-01-15 02:52:14 +00001643 }
1644 break;
1645
Greg Claytone179a582011-01-05 18:43:15 +00001646 case eFormatComplex:
1647 if (sizeof(float) * 2 == item_byte_size)
1648 {
1649 float f32_1 = GetFloat (&offset);
1650 float f32_2 = GetFloat (&offset);
1651
1652 s->Printf ("%g + %gi", f32_1, f32_2);
1653 break;
1654 }
1655 else if (sizeof(double) * 2 == item_byte_size)
1656 {
1657 double d64_1 = GetDouble (&offset);
1658 double d64_2 = GetDouble (&offset);
1659
1660 s->Printf ("%lg + %lgi", d64_1, d64_2);
1661 break;
1662 }
1663 else if (sizeof(long double) * 2 == item_byte_size)
1664 {
1665 long double ld64_1 = GetLongDouble (&offset);
1666 long double ld64_2 = GetLongDouble (&offset);
1667 s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
1668 break;
1669 }
Greg Claytone52f37b2011-01-15 02:52:14 +00001670 else
1671 {
Greg Clayton4dd3d902012-02-27 23:00:14 +00001672 s->Printf("error: unsupported byte size (%u) for complex float format", item_byte_size);
1673 return offset;
Greg Claytone52f37b2011-01-15 02:52:14 +00001674 }
1675 break;
Greg Claytone179a582011-01-05 18:43:15 +00001676
Greg Clayton54e7afa2010-07-09 20:39:50 +00001677 default:
1678 case eFormatDefault:
Chris Lattner24943d22010-06-08 16:52:24 +00001679 case eFormatHex:
Enrico Granata535543d2012-08-09 19:33:34 +00001680 case eFormatHexUppercase:
Chris Lattner24943d22010-06-08 16:52:24 +00001681 {
Enrico Granata535543d2012-08-09 19:33:34 +00001682 bool wantsuppercase = (item_format == eFormatHexUppercase);
1683 if (item_byte_size <= 8)
Chris Lattner24943d22010-06-08 16:52:24 +00001684 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001685 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64, 2 * item_byte_size, 2 * item_byte_size, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
Enrico Granata535543d2012-08-09 19:33:34 +00001686 }
1687 else
1688 {
1689 assert (item_bit_size == 0 && item_bit_offset == 0);
1690 s->PutCString("0x");
1691 const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
1692 if (bytes)
Greg Clayton56bbdaf2011-04-28 20:55:26 +00001693 {
Enrico Granata535543d2012-08-09 19:33:34 +00001694 uint32_t idx;
1695 if (m_byte_order == eByteOrderBig)
1696 {
1697 for (idx = 0; idx < item_byte_size; ++idx)
1698 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
1699 }
1700 else
1701 {
1702 for (idx = 0; idx < item_byte_size; ++idx)
1703 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
1704 }
Greg Clayton56bbdaf2011-04-28 20:55:26 +00001705 }
Chris Lattner24943d22010-06-08 16:52:24 +00001706 }
1707 }
1708 break;
1709
1710 case eFormatFloat:
Chris Lattner24943d22010-06-08 16:52:24 +00001711 {
Sean Callanan509c7282012-10-20 06:08:09 +00001712 std::ostringstream ss;
Jason Molendada530892012-11-01 23:35:19 +00001713 if (item_byte_size == sizeof(float))
Sean Callanan509c7282012-10-20 06:08:09 +00001714 {
Sean Callanan509c7282012-10-20 06:08:09 +00001715 ss.precision(std::numeric_limits<float>::digits10);
1716 ss << GetFloat(&offset);
Jason Molendada530892012-11-01 23:35:19 +00001717 }
1718 else if (item_byte_size == sizeof(double))
1719 {
Sean Callanan509c7282012-10-20 06:08:09 +00001720 ss.precision(std::numeric_limits<double>::digits10);
1721 ss << GetDouble(&offset);
Jason Molendada530892012-11-01 23:35:19 +00001722 }
1723 else if (item_byte_size == sizeof(long double))
1724 {
Sean Callanan509c7282012-10-20 06:08:09 +00001725 ss.precision(std::numeric_limits<long double>::digits10);
1726 ss << GetLongDouble(&offset);
Jason Molendada530892012-11-01 23:35:19 +00001727 }
1728 else
1729 {
1730 s->Printf("error: unsupported byte size (%u) for float format", item_byte_size);
1731 return offset;
Sean Callanan509c7282012-10-20 06:08:09 +00001732 }
1733 ss.flush();
1734 s->Printf("%s", ss.str().c_str());
Greg Clayton4dd3d902012-02-27 23:00:14 +00001735 }
Chris Lattner24943d22010-06-08 16:52:24 +00001736 break;
1737
1738 case eFormatUnicode16:
Greg Clayton4dd3d902012-02-27 23:00:14 +00001739 s->Printf("U+%4.4x", GetU16 (&offset));
Chris Lattner24943d22010-06-08 16:52:24 +00001740 break;
1741
1742 case eFormatUnicode32:
Greg Clayton4dd3d902012-02-27 23:00:14 +00001743 s->Printf("U+0x%8.8x", GetU32 (&offset));
Chris Lattner24943d22010-06-08 16:52:24 +00001744 break;
1745
Greg Clayton24a6bd92011-10-27 17:55:14 +00001746 case eFormatAddressInfo:
1747 {
1748 addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001749 s->Printf("0x%*.*" PRIx64, 2 * item_byte_size, 2 * item_byte_size, addr);
Greg Clayton24a6bd92011-10-27 17:55:14 +00001750 if (exe_scope)
1751 {
Greg Clayton289afcb2012-02-18 05:35:26 +00001752 TargetSP target_sp (exe_scope->CalculateTarget());
Greg Clayton24a6bd92011-10-27 17:55:14 +00001753 lldb_private::Address so_addr;
Greg Clayton941f5da2012-07-11 22:18:24 +00001754 if (target_sp)
Greg Clayton24a6bd92011-10-27 17:55:14 +00001755 {
Greg Clayton941f5da2012-07-11 22:18:24 +00001756 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1757 {
1758 s->PutChar(' ');
1759 so_addr.Dump (s,
1760 exe_scope,
1761 Address::DumpStyleResolvedDescription,
1762 Address::DumpStyleModuleWithFileAddress);
1763 }
1764 else
1765 {
1766 so_addr.SetOffset(addr);
1767 so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
1768 }
Greg Clayton24a6bd92011-10-27 17:55:14 +00001769 }
1770 }
1771 }
1772 break;
1773
1774 case eFormatHexFloat:
1775 if (sizeof(float) == item_byte_size)
1776 {
1777 char float_cstr[256];
1778 llvm::APFloat ap_float (GetFloat (&offset));
1779 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1780 s->Printf ("%s", float_cstr);
1781 break;
1782 }
1783 else if (sizeof(double) == item_byte_size)
1784 {
1785 char float_cstr[256];
1786 llvm::APFloat ap_float (GetDouble (&offset));
1787 ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
1788 s->Printf ("%s", float_cstr);
1789 break;
1790 }
Johnny Chen758835b2012-02-16 23:09:08 +00001791 else
Greg Clayton24a6bd92011-10-27 17:55:14 +00001792 {
Greg Clayton4dd3d902012-02-27 23:00:14 +00001793 s->Printf("error: unsupported byte size (%u) for hex float format", item_byte_size);
1794 return offset;
Greg Clayton24a6bd92011-10-27 17:55:14 +00001795 }
1796 break;
1797
Enrico Granata86e7c3e2011-07-12 22:56:10 +00001798// please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
1799// if you fail to do so, users will start getting different outputs depending on internal
1800// implementation details they should not care about ||
1801 case eFormatVectorOfChar: // ||
1802 s->PutChar('{'); // \/
Johnny Chen5564d802012-02-16 22:06:47 +00001803 offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001804 s->PutChar('}');
1805 break;
1806
1807 case eFormatVectorOfSInt8:
1808 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001809 offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001810 s->PutChar('}');
1811 break;
1812
1813 case eFormatVectorOfUInt8:
1814 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001815 offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001816 s->PutChar('}');
1817 break;
1818
1819 case eFormatVectorOfSInt16:
1820 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001821 offset = Dump (s, offset, eFormatDecimal, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001822 s->PutChar('}');
1823 break;
1824
1825 case eFormatVectorOfUInt16:
1826 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001827 offset = Dump (s, offset, eFormatHex, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001828 s->PutChar('}');
1829 break;
1830
1831 case eFormatVectorOfSInt32:
1832 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001833 offset = Dump (s, offset, eFormatDecimal, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001834 s->PutChar('}');
1835 break;
1836
1837 case eFormatVectorOfUInt32:
1838 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001839 offset = Dump (s, offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001840 s->PutChar('}');
1841 break;
1842
1843 case eFormatVectorOfSInt64:
1844 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001845 offset = Dump (s, offset, eFormatDecimal, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001846 s->PutChar('}');
1847 break;
1848
1849 case eFormatVectorOfUInt64:
1850 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001851 offset = Dump (s, offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001852 s->PutChar('}');
1853 break;
1854
1855 case eFormatVectorOfFloat32:
1856 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001857 offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001858 s->PutChar('}');
1859 break;
1860
1861 case eFormatVectorOfFloat64:
1862 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001863 offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001864 s->PutChar('}');
1865 break;
1866
1867 case eFormatVectorOfUInt128:
1868 s->PutChar('{');
Johnny Chen5564d802012-02-16 22:06:47 +00001869 offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
Chris Lattner24943d22010-06-08 16:52:24 +00001870 s->PutChar('}');
1871 break;
1872 }
1873 }
1874
1875 if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
1876 {
1877 s->Printf("%*s", (num_per_line - (offset - line_start_offset)) * 3 + 2, "");
1878 Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
1879 }
1880 return offset; // Return the offset at which we ended up
1881}
1882
1883//----------------------------------------------------------------------
1884// Dumps bytes from this object's data to the stream "s" starting
1885// "start_offset" bytes into this data, and ending with the byte
1886// before "end_offset". "base_addr" will be added to the offset
1887// into the dumped data when showing the offset into the data in the
1888// output information. "num_per_line" objects of type "type" will
1889// be dumped with the option to override the format for each object
1890// with "type_format". "type_format" is a printf style formatting
1891// string. If "type_format" is NULL, then an appropriate format
1892// string will be used for the supplied "type". If the stream "s"
1893// is NULL, then the output will be send to Log().
1894//----------------------------------------------------------------------
1895uint32_t
1896DataExtractor::PutToLog
1897(
1898 Log *log,
1899 uint32_t start_offset,
1900 uint32_t length,
1901 uint64_t base_addr,
1902 uint32_t num_per_line,
1903 DataExtractor::Type type,
1904 const char *format
1905) const
1906{
1907 if (log == NULL)
1908 return start_offset;
1909
1910 uint32_t offset;
Eli Friedman79912252010-07-09 23:04:08 +00001911 uint32_t end_offset;
Chris Lattner24943d22010-06-08 16:52:24 +00001912 uint32_t count;
1913 StreamString sstr;
Eli Friedman79912252010-07-09 23:04:08 +00001914 for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
Chris Lattner24943d22010-06-08 16:52:24 +00001915 {
1916 if ((count % num_per_line) == 0)
1917 {
1918 // Print out any previous string
1919 if (sstr.GetSize() > 0)
1920 {
1921 log->Printf("%s", sstr.GetData());
1922 sstr.Clear();
1923 }
1924 // Reset string offset and fill the current line string with address:
1925 if (base_addr != LLDB_INVALID_ADDRESS)
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001926 sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
Chris Lattner24943d22010-06-08 16:52:24 +00001927 }
1928
1929 switch (type)
1930 {
Chris Lattner24943d22010-06-08 16:52:24 +00001931 case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
1932 case TypeChar:
1933 {
1934 char ch = GetU8(&offset);
1935 sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
1936 }
1937 break;
1938 case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
1939 case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001940 case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break;
1941 case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break;
1942 case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break;
1943 case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break;
Chris Lattner24943d22010-06-08 16:52:24 +00001944 }
1945 }
1946
1947 if (sstr.GetSize() > 0)
1948 log->Printf("%s", sstr.GetData());
1949
1950 return offset; // Return the offset at which we ended up
1951}
1952
1953//----------------------------------------------------------------------
1954// DumpUUID
1955//
1956// Dump out a UUID starting at 'offset' bytes into the buffer
1957//----------------------------------------------------------------------
1958void
1959DataExtractor::DumpUUID (Stream *s, uint32_t offset) const
1960{
1961 if (s)
1962 {
1963 const uint8_t *uuid_data = PeekData(offset, 16);
1964 if ( uuid_data )
1965 {
Greg Clayton0467c782011-02-04 18:53:10 +00001966 lldb_private::UUID uuid(uuid_data, 16);
Chris Lattner24943d22010-06-08 16:52:24 +00001967 uuid.Dump(s);
1968 }
1969 else
1970 {
1971 s->Printf("<not enough data for UUID at offset 0x%8.8x>", offset);
1972 }
1973 }
1974}
1975
Greg Claytond52d00f2011-07-16 03:19:08 +00001976void
1977DataExtractor::DumpHexBytes (Stream *s,
1978 const void *src,
1979 size_t src_len,
Greg Clayton8d2ea282011-07-17 20:36:25 +00001980 uint32_t bytes_per_line,
Greg Claytond52d00f2011-07-16 03:19:08 +00001981 addr_t base_addr)
1982{
1983 DataExtractor data (src, src_len, eByteOrderLittle, 4);
1984 data.Dump (s,
1985 0, // Offset into "src"
1986 eFormatBytes, // Dump as hex bytes
1987 1, // Size of each item is 1 for single bytes
1988 src_len, // Number of bytes
Greg Clayton8d2ea282011-07-17 20:36:25 +00001989 bytes_per_line, // Num bytes per line
Greg Claytond52d00f2011-07-16 03:19:08 +00001990 base_addr, // Base address
1991 0, 0); // Bitfield info
1992}
Enrico Granata91544802011-09-06 19:20:51 +00001993
1994size_t
1995DataExtractor::Copy (DataExtractor &dest_data) const
1996{
1997 if (m_data_sp.get())
1998 {
1999 // we can pass along the SP to the data
2000 dest_data.SetData(m_data_sp);
2001 }
2002 else
2003 {
2004 const uint8_t *base_ptr = m_start;
2005 size_t data_size = GetByteSize();
2006 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
2007 }
2008 return GetByteSize();
2009}
2010
2011bool
2012DataExtractor::Append(DataExtractor& rhs)
2013{
2014 if (rhs.GetByteOrder() != GetByteOrder())
2015 return false;
2016
2017 if (rhs.GetByteSize() == 0)
2018 return true;
2019
2020 if (GetByteSize() == 0)
2021 return (rhs.Copy(*this) > 0);
2022
2023 size_t bytes = GetByteSize() + rhs.GetByteSize();
2024
2025 DataBufferHeap *buffer_heap_ptr = NULL;
2026 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2027
2028 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2029 return false;
2030
2031 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2032
2033 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2034 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
2035
2036 SetData(buffer_sp);
2037
2038 return true;
2039}
2040
2041bool
2042DataExtractor::Append(void* buf, uint32_t length)
2043{
2044 if (buf == NULL)
2045 return false;
2046
2047 if (length == 0)
2048 return true;
2049
2050 size_t bytes = GetByteSize() + length;
2051
2052 DataBufferHeap *buffer_heap_ptr = NULL;
2053 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
2054
2055 if (buffer_sp.get() == NULL || buffer_heap_ptr == NULL)
2056 return false;
2057
2058 uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
2059
2060 if (GetByteSize() > 0)
2061 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
2062
2063 memcpy(bytes_ptr + GetByteSize(), buf, length);
2064
2065 SetData(buffer_sp);
2066
2067 return true;
Greg Clayton141f8d92011-10-12 00:53:29 +00002068}