Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <assert.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | |
| 13 | #include <bitset> |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 14 | #include <limits> |
| 15 | #include <sstream> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/APFloat.h" |
Greg Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APInt.h" |
Greg Clayton | 39f54e5 | 2011-08-15 07:23:47 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/ArrayRef.h" |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
| 22 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 23 | #include "lldb/Core/DataBufferHeap.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Core/DataExtractor.h" |
| 25 | #include "lldb/Core/DataBuffer.h" |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 26 | #include "lldb/Core/Disassembler.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 32 | #include "lldb/Host/Endian.h" |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 33 | #include "lldb/Target/ExecutionContext.h" |
| 34 | #include "lldb/Target/ExecutionContextScope.h" |
| 35 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 40 | static inline uint16_t |
| 41 | ReadInt16(const unsigned char* ptr, unsigned offset) |
| 42 | { |
| 43 | return *(uint16_t *)(ptr + offset); |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 44 | } |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 45 | static inline uint32_t |
| 46 | ReadInt32 (const unsigned char* ptr, unsigned offset) |
| 47 | { |
| 48 | return *(uint32_t *)(ptr + offset); |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 49 | } |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 50 | |
| 51 | static inline uint64_t |
| 52 | ReadInt64(const unsigned char* ptr, unsigned offset) |
| 53 | { |
| 54 | return *(uint64_t *)(ptr + offset); |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 55 | } |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 56 | |
| 57 | static inline uint16_t |
| 58 | ReadSwapInt16(const unsigned char* ptr, unsigned offset) |
| 59 | { |
| 60 | return llvm::ByteSwap_16(*(uint16_t *)(ptr + offset)); |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 61 | } |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 62 | |
| 63 | static inline uint32_t |
| 64 | ReadSwapInt32 (const unsigned char* ptr, unsigned offset) |
| 65 | { |
| 66 | return llvm::ByteSwap_32(*(uint32_t *)(ptr + offset)); |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 67 | } |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 68 | static inline uint64_t |
| 69 | ReadSwapInt64(const unsigned char* ptr, unsigned offset) |
| 70 | { |
| 71 | return llvm::ByteSwap_64(*(uint64_t *)(ptr + offset)); |
Eli Friedman | 5eeb2d3 | 2010-06-10 23:56:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | #define NON_PRINTABLE_CHAR '.' |
| 75 | //---------------------------------------------------------------------- |
| 76 | // Default constructor. |
| 77 | //---------------------------------------------------------------------- |
| 78 | DataExtractor::DataExtractor () : |
| 79 | m_start (NULL), |
| 80 | m_end (NULL), |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 81 | m_byte_order(lldb::endian::InlHostByteOrder()), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | 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 | //---------------------------------------------------------------------- |
| 91 | DataExtractor::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 Clayton | 66ed2fb | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 107 | DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint8_t addr_size) : |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | 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 | //---------------------------------------------------------------------- |
| 124 | DataExtractor::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 Clayton | 82f0746 | 2011-05-30 00:49:24 +0000 | [diff] [blame] | 140 | DataExtractor::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | //---------------------------------------------------------------------- |
| 150 | // Assignment operator |
| 151 | //---------------------------------------------------------------------- |
| 152 | const DataExtractor& |
| 153 | DataExtractor::operator= (const DataExtractor& rhs) |
| 154 | { |
| 155 | if (this != &rhs) |
| 156 | { |
Greg Clayton | 82f0746 | 2011-05-30 00:49:24 +0000 | [diff] [blame] | 157 | m_start = rhs.m_start; |
| 158 | m_end = rhs.m_end; |
| 159 | m_byte_order = rhs.m_byte_order; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | m_addr_size = rhs.m_addr_size; |
Greg Clayton | 82f0746 | 2011-05-30 00:49:24 +0000 | [diff] [blame] | 161 | m_data_sp = rhs.m_data_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | } |
| 163 | return *this; |
| 164 | } |
| 165 | |
| 166 | //---------------------------------------------------------------------- |
| 167 | // Destructor |
| 168 | //---------------------------------------------------------------------- |
| 169 | DataExtractor::~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 | //------------------------------------------------------------------ |
| 178 | void |
| 179 | DataExtractor::Clear () |
| 180 | { |
| 181 | m_start = NULL; |
| 182 | m_end = NULL; |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 183 | m_byte_order = lldb::endian::InlHostByteOrder(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | m_addr_size = 4; |
| 185 | m_data_sp.reset(); |
| 186 | } |
| 187 | |
| 188 | //------------------------------------------------------------------ |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | // If this object contains shared data, this function returns the |
| 190 | // offset into that shared data. Else zero is returned. |
| 191 | //------------------------------------------------------------------ |
| 192 | size_t |
| 193 | DataExtractor::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | // Returns true if there are LENGTH bytes availabe starting OFFSET |
| 213 | // into the data that is in this object. |
| 214 | //------------------------------------------------------------------ |
| 215 | bool |
| 216 | DataExtractor::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | //---------------------------------------------------------------------- |
| 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 | //---------------------------------------------------------------------- |
| 247 | uint32_t |
| 248 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 279 | uint32_t |
| 280 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 314 | uint32_t |
Greg Clayton | 66ed2fb | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 315 | DataExtractor::SetData (const DataBufferSP& data_sp, uint32_t data_offset, uint32_t data_length) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 316 | { |
| 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 | //---------------------------------------------------------------------- |
| 354 | uint8_t |
| 355 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 375 | void * |
| 376 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 398 | uint16_t |
| 399 | DataExtractor::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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 405 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 406 | val = ReadSwapInt16(m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | else |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 408 | val = ReadInt16 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | |
| 410 | // Advance the offset |
| 411 | *offset_ptr += sizeof(val); |
| 412 | } |
| 413 | return val; |
| 414 | } |
| 415 | |
Greg Clayton | 5fcff9a | 2010-09-15 08:33:30 +0000 | [diff] [blame] | 416 | uint16_t |
| 417 | DataExtractor::GetU16_unchecked (uint32_t *offset_ptr) const |
| 418 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 419 | uint16_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ? |
Greg Clayton | 5fcff9a | 2010-09-15 08:33:30 +0000 | [diff] [blame] | 420 | ReadInt16 (m_start, *offset_ptr) : |
| 421 | ReadSwapInt16(m_start, *offset_ptr); |
| 422 | *offset_ptr += sizeof(val); |
| 423 | return val; |
| 424 | } |
| 425 | |
| 426 | uint32_t |
| 427 | DataExtractor::GetU32_unchecked (uint32_t *offset_ptr) const |
| 428 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 429 | uint32_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ? |
Greg Clayton | 5fcff9a | 2010-09-15 08:33:30 +0000 | [diff] [blame] | 430 | ReadInt32 (m_start, *offset_ptr) : |
| 431 | ReadSwapInt32 (m_start, *offset_ptr); |
| 432 | *offset_ptr += sizeof(val); |
| 433 | return val; |
| 434 | } |
| 435 | |
| 436 | uint64_t |
| 437 | DataExtractor::GetU64_unchecked (uint32_t *offset_ptr) const |
| 438 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 439 | uint64_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ? |
Greg Clayton | 5fcff9a | 2010-09-15 08:33:30 +0000 | [diff] [blame] | 440 | ReadInt64 (m_start, *offset_ptr) : |
| 441 | ReadSwapInt64 (m_start, *offset_ptr); |
| 442 | *offset_ptr += sizeof(val); |
| 443 | return val; |
| 444 | } |
| 445 | |
| 446 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | //---------------------------------------------------------------------- |
| 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 | //---------------------------------------------------------------------- |
| 456 | void * |
| 457 | DataExtractor::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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 467 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | { |
| 469 | for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 470 | *value_ptr = ReadSwapInt16 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 471 | } |
| 472 | else |
| 473 | { |
| 474 | for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 475 | *value_ptr = ReadInt16 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 476 | } |
| 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 | //---------------------------------------------------------------------- |
| 492 | uint32_t |
| 493 | DataExtractor::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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 500 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 501 | val = ReadSwapInt32 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 502 | else |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 503 | val = ReadInt32 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 504 | |
| 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 | //---------------------------------------------------------------------- |
| 520 | void * |
| 521 | DataExtractor::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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 531 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | { |
| 533 | for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 534 | *value_ptr = ReadSwapInt32 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 540 | *value_ptr = ReadInt32 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | } |
| 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 | //---------------------------------------------------------------------- |
| 557 | uint64_t |
| 558 | DataExtractor::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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 564 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 565 | val = ReadSwapInt64 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 566 | else |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 567 | val = ReadInt64 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 568 | |
| 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 | //---------------------------------------------------------------------- |
| 582 | void * |
| 583 | DataExtractor::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 Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 593 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 594 | { |
| 595 | for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 596 | *value_ptr = ReadSwapInt64 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size) |
Greg Clayton | 2519d99 | 2010-06-12 01:03:17 +0000 | [diff] [blame] | 602 | *value_ptr = ReadInt64 (m_start, offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | } |
| 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 | //---------------------------------------------------------------------- |
| 623 | uint32_t |
| 624 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 648 | uint64_t |
| 649 | DataExtractor::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 Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 664 | uint64_t |
| 665 | DataExtractor::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | int64_t |
| 681 | DataExtractor::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 | |
| 696 | uint64_t |
| 697 | DataExtractor::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 Granata | 9762e10 | 2011-07-06 02:13:41 +0000 | [diff] [blame] | 704 | uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1); |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 705 | if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64) |
Enrico Granata | 9762e10 | 2011-07-06 02:13:41 +0000 | [diff] [blame] | 706 | return uval64; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 707 | uval64 &= bitfield_mask; |
| 708 | } |
| 709 | return uval64; |
| 710 | } |
| 711 | |
| 712 | int64_t |
| 713 | DataExtractor::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 Clayton | 36d507f | 2012-10-31 20:56:43 +0000 | [diff] [blame] | 720 | uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 721 | sval64 &= bitfield_mask; |
| 722 | // sign extend if needed |
Greg Clayton | 36d507f | 2012-10-31 20:56:43 +0000 | [diff] [blame] | 723 | if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 724 | sval64 |= ~bitfield_mask; |
| 725 | } |
| 726 | return sval64; |
| 727 | } |
| 728 | |
| 729 | |
| 730 | float |
| 731 | DataExtractor::GetFloat (uint32_t *offset_ptr) const |
| 732 | { |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 733 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 739 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 740 | { |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 745 | else |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 746 | { |
| 747 | ::memcpy (&val, src_data, sizeof (float_type)); |
| 748 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | |
| 750 | // Advance the offset |
| 751 | *offset_ptr += sizeof(val); |
| 752 | } |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 753 | return val; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | double |
| 757 | DataExtractor::GetDouble (uint32_t *offset_ptr) const |
| 758 | { |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 759 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 764 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 765 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 766 | { |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 771 | else |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 772 | { |
| 773 | ::memcpy (&val, src_data, sizeof (float_type)); |
| 774 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 775 | |
| 776 | // Advance the offset |
| 777 | *offset_ptr += sizeof(val); |
| 778 | } |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 779 | return val; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
| 783 | long double |
| 784 | DataExtractor::GetLongDouble (uint32_t *offset_ptr) const |
| 785 | { |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 786 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 791 | { |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 792 | if (m_byte_order != lldb::endian::InlHostByteOrder()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 793 | { |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 794 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 797 | } |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 798 | else |
| 799 | { |
| 800 | ::memcpy (&val, src_data, sizeof (float_type)); |
| 801 | } |
| 802 | |
| 803 | // Advance the offset |
| 804 | *offset_ptr += sizeof(val); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 805 | } |
Greg Clayton | c7527b1 | 2010-09-14 04:09:57 +0000 | [diff] [blame] | 806 | return val; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 807 | } |
| 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 | //------------------------------------------------------------------ |
| 818 | uint64_t |
| 819 | DataExtractor::GetAddress (uint32_t *offset_ptr) const |
| 820 | { |
| 821 | return GetMaxU64 (offset_ptr, m_addr_size); |
| 822 | } |
| 823 | |
Greg Clayton | 0fea051 | 2011-12-30 00:32:24 +0000 | [diff] [blame] | 824 | uint64_t |
| 825 | DataExtractor::GetAddress_unchecked (uint32_t *offset_ptr) const |
| 826 | { |
| 827 | return GetMaxU64_unchecked (offset_ptr, m_addr_size); |
| 828 | } |
| 829 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 830 | //------------------------------------------------------------------ |
| 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 | //------------------------------------------------------------------ |
| 838 | uint64_t |
| 839 | DataExtractor::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 | |
| 851 | uint64_t |
| 852 | DataExtractor::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 Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 854 | if (eh_ptr_enc == DW_EH_PE_omit) |
Greg Clayton | 381f968 | 2011-04-01 18:14:08 +0000 | [diff] [blame] | 855 | return ULLONG_MAX; // Value isn't in the buffer... |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 856 | |
| 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 Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 865 | case DW_EH_PE_pcrel: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 866 | 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 Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 874 | case DW_EH_PE_textrel: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 875 | 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 Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 882 | case DW_EH_PE_datarel: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 883 | 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 Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 890 | case DW_EH_PE_funcrel: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 891 | signExtendValue = true; |
| 892 | break; |
| 893 | |
Jason Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 894 | case DW_EH_PE_aligned: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 895 | { |
| 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 Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 914 | switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 915 | { |
Jason Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 916 | case DW_EH_PE_absptr : |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 917 | { |
| 918 | addressValue = GetAddress (offset_ptr); |
| 919 | // if (data_relocs) |
| 920 | // addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue); |
| 921 | } |
| 922 | break; |
Jason Molenda | ccfba72 | 2010-07-06 22:38:03 +0000 | [diff] [blame] | 923 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 931 | 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 | |
| 950 | size_t |
| 951 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 973 | const uint8_t* |
| 974 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 990 | const void* |
| 991 | DataExtractor::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 Clayton | 061b79d | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 1003 | // Extract data and swap if needed when doing the copy |
| 1004 | uint32_t |
| 1005 | DataExtractor::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 Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 1031 | if (dst_len >= src_len) |
Greg Clayton | 061b79d | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 1032 | { |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1113 | //---------------------------------------------------------------------- |
| 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 | //---------------------------------------------------------------------- |
| 1128 | const char* |
| 1129 | DataExtractor::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 | //------------------------------------------------------------------ |
| 1155 | const char * |
| 1156 | DataExtractor::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 | //---------------------------------------------------------------------- |
| 1171 | uint64_t |
| 1172 | DataExtractor::GetULEB128 (uint32_t *offset_ptr) const |
| 1173 | { |
Greg Clayton | 36deaee | 2011-11-14 22:56:58 +0000 | [diff] [blame] | 1174 | const uint8_t *src = m_start + *offset_ptr; |
| 1175 | const uint8_t *end = m_end; |
| 1176 | |
| 1177 | if (src < end) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1178 | { |
Greg Clayton | 36deaee | 2011-11-14 22:56:58 +0000 | [diff] [blame] | 1179 | uint64_t result = *src++; |
| 1180 | if (result >= 0x80) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1181 | { |
Greg Clayton | 36deaee | 2011-11-14 22:56:58 +0000 | [diff] [blame] | 1182 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1192 | } |
Greg Clayton | 36deaee | 2011-11-14 22:56:58 +0000 | [diff] [blame] | 1193 | *offset_ptr = (uint32_t)(src - m_start); |
| 1194 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1195 | } |
Greg Clayton | 36deaee | 2011-11-14 22:56:58 +0000 | [diff] [blame] | 1196 | |
| 1197 | return 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1198 | } |
| 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 | //---------------------------------------------------------------------- |
| 1208 | int64_t |
| 1209 | DataExtractor::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 Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1219 | uint8_t byte = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1220 | 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 | //---------------------------------------------------------------------- |
| 1249 | uint32_t |
| 1250 | DataExtractor::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 Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1266 | static uint32_t |
| 1267 | DumpAPInt (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 Clayton | 39f54e5 | 2011-08-15 07:23:47 +0000 | [diff] [blame] | 1316 | llvm::APInt apint (byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array)); |
Greg Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1317 | |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1334 | uint32_t |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1335 | DataExtractor::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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1345 | { |
| 1346 | if (s == NULL) |
| 1347 | return start_offset; |
| 1348 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1349 | if (item_format == eFormatPointer) |
| 1350 | { |
| 1351 | if (item_byte_size != 4 && item_byte_size != 8) |
| 1352 | item_byte_size = s->GetAddressByteSize(); |
| 1353 | } |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1354 | |
Greg Clayton | 746979d | 2011-10-28 23:44:55 +0000 | [diff] [blame] | 1355 | uint32_t offset = start_offset; |
| 1356 | |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1357 | if (item_format == eFormatInstruction) |
| 1358 | { |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 1359 | TargetSP target_sp; |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1360 | if (exe_scope) |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 1361 | target_sp = exe_scope->CalculateTarget(); |
| 1362 | if (target_sp) |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1363 | { |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 1364 | DisassemblerSP disassembler_sp (Disassembler::FindPlugin(target_sp->GetArchitecture(), NULL)); |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1365 | if (disassembler_sp) |
| 1366 | { |
| 1367 | lldb::addr_t addr = base_addr + start_offset; |
| 1368 | lldb_private::Address so_addr; |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 1369 | if (!target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1370 | { |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1371 | so_addr.SetRawAddress(addr); |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Greg Clayton | 746979d | 2011-10-28 23:44:55 +0000 | [diff] [blame] | 1374 | size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false); |
| 1375 | |
| 1376 | if (bytes_consumed) |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1377 | { |
Greg Clayton | 746979d | 2011-10-28 23:44:55 +0000 | [diff] [blame] | 1378 | offset += bytes_consumed; |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1379 | 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 Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1394 | item_format = eFormatHex; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1395 | |
Greg Clayton | 746979d | 2011-10-28 23:44:55 +0000 | [diff] [blame] | 1396 | uint32_t line_start_offset = start_offset; |
| 1397 | for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1398 | { |
| 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 Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1411 | s->Printf ("0x%8.8" PRIx64 ": ", (uint64_t)(base_addr + (offset - start_offset))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1412 | line_start_offset = offset; |
| 1413 | } |
| 1414 | else |
| 1415 | if (item_format != eFormatChar && |
| 1416 | item_format != eFormatCharPrintable && |
Greg Clayton | 307fa07 | 2011-06-17 23:50:44 +0000 | [diff] [blame] | 1417 | item_format != eFormatCharArray && |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1418 | count > 0) |
| 1419 | { |
| 1420 | s->PutChar(' '); |
| 1421 | } |
| 1422 | |
| 1423 | uint32_t i; |
| 1424 | switch (item_format) |
| 1425 | { |
| 1426 | case eFormatBoolean: |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1427 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1434 | break; |
| 1435 | |
| 1436 | case eFormatBinary: |
Greg Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1437 | if (item_byte_size <= 8) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1438 | { |
| 1439 | uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); |
Greg Clayton | e42cbfb | 2010-07-21 01:08:41 +0000 | [diff] [blame] | 1440 | // 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 Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1444 | for (i = 0; i < 64; ++i) |
Greg Clayton | e42cbfb | 2010-07-21 01:08:41 +0000 | [diff] [blame] | 1445 | if (bits[i]) |
| 1446 | binary_value[64 - 1 - i] = '1'; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1447 | 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 Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1452 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1458 | 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 Clayton | 307fa07 | 2011-06-17 23:50:44 +0000 | [diff] [blame] | 1474 | case eFormatCharArray: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1475 | { |
| 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 Clayton | 0943687 | 2011-11-10 03:38:56 +0000 | [diff] [blame] | 1481 | const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1482 | if (isprint(ch)) |
Greg Clayton | 0943687 | 2011-11-10 03:38:56 +0000 | [diff] [blame] | 1483 | s->Printf ("%c", (char)ch); |
Greg Clayton | 307fa07 | 2011-06-17 23:50:44 +0000 | [diff] [blame] | 1484 | else if (item_format != eFormatCharPrintable) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1485 | { |
| 1486 | switch (ch) |
| 1487 | { |
Daniel Dunbar | aed469f | 2011-10-31 22:51:00 +0000 | [diff] [blame] | 1488 | case '\033': s->Printf ("\\e"); break; |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1489 | 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 Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1497 | default: |
| 1498 | if (item_byte_size == 1) |
Greg Clayton | 0943687 | 2011-11-10 03:38:56 +0000 | [diff] [blame] | 1499 | s->Printf ("\\x%2.2x", (uint8_t)ch); |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1500 | else |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1501 | s->Printf ("%" PRIu64, ch); |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1502 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1503 | } |
| 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 Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1516 | case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1517 | case eFormatDecimal: |
Greg Clayton | 90bc5c6 | 2010-07-06 20:30:35 +0000 | [diff] [blame] | 1518 | if (item_byte_size <= 8) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1519 | s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); |
Greg Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1520 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1526 | break; |
| 1527 | |
| 1528 | case eFormatUnsigned: |
Greg Clayton | 90bc5c6 | 2010-07-06 20:30:35 +0000 | [diff] [blame] | 1529 | if (item_byte_size <= 8) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1530 | s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); |
Greg Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1531 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1537 | break; |
| 1538 | |
| 1539 | case eFormatOctal: |
Greg Clayton | 90bc5c6 | 2010-07-06 20:30:35 +0000 | [diff] [blame] | 1540 | if (item_byte_size <= 8) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1541 | s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset)); |
Greg Clayton | a4e0dad | 2011-08-15 02:24:40 +0000 | [diff] [blame] | 1542 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1550 | 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 Clayton | 4cd531f | 2011-10-26 21:01:16 +0000 | [diff] [blame] | 1556 | uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8)); |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1557 | if (isprint(ch)) |
| 1558 | s->Printf ("%c", ch); |
| 1559 | else |
| 1560 | { |
| 1561 | switch (ch) |
| 1562 | { |
Daniel Dunbar | ca6bc7e | 2011-10-31 22:51:02 +0000 | [diff] [blame] | 1563 | case '\033': s->Printf ("\\e"); break; |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1564 | 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 Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1572 | default: s->Printf ("\\x%2.2x", ch); break; |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | s->PutChar('\''); |
| 1577 | } |
| 1578 | break; |
| 1579 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1580 | case eFormatCString: |
| 1581 | { |
| 1582 | const char *cstr = GetCStr(&offset); |
Sean Callanan | ccdaf23 | 2012-01-04 17:36:30 +0000 | [diff] [blame] | 1583 | |
| 1584 | if (!cstr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1585 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1586 | s->Printf("NULL"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1587 | offset = UINT32_MAX; |
| 1588 | } |
Sean Callanan | ccdaf23 | 2012-01-04 17:36:30 +0000 | [diff] [blame] | 1589 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1620 | } |
| 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 Clayton | e52f37b | 2011-01-15 02:52:14 +0000 | [diff] [blame] | 1628 | |
| 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 Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1635 | 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 Clayton | e52f37b | 2011-01-15 02:52:14 +0000 | [diff] [blame] | 1637 | } |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1638 | else |
| 1639 | { |
| 1640 | s->Printf("error: unsupported byte size (%u) for complex integer format", item_byte_size); |
| 1641 | return offset; |
| 1642 | } |
Greg Clayton | e52f37b | 2011-01-15 02:52:14 +0000 | [diff] [blame] | 1643 | } |
| 1644 | break; |
| 1645 | |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1646 | 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 Clayton | e52f37b | 2011-01-15 02:52:14 +0000 | [diff] [blame] | 1670 | else |
| 1671 | { |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1672 | s->Printf("error: unsupported byte size (%u) for complex float format", item_byte_size); |
| 1673 | return offset; |
Greg Clayton | e52f37b | 2011-01-15 02:52:14 +0000 | [diff] [blame] | 1674 | } |
| 1675 | break; |
Greg Clayton | e179a58 | 2011-01-05 18:43:15 +0000 | [diff] [blame] | 1676 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1677 | default: |
| 1678 | case eFormatDefault: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1679 | case eFormatHex: |
Enrico Granata | 535543d | 2012-08-09 19:33:34 +0000 | [diff] [blame] | 1680 | case eFormatHexUppercase: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1681 | { |
Enrico Granata | 535543d | 2012-08-09 19:33:34 +0000 | [diff] [blame] | 1682 | bool wantsuppercase = (item_format == eFormatHexUppercase); |
| 1683 | if (item_byte_size <= 8) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1684 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1685 | 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 Granata | 535543d | 2012-08-09 19:33:34 +0000 | [diff] [blame] | 1686 | } |
| 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 Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 1693 | { |
Enrico Granata | 535543d | 2012-08-09 19:33:34 +0000 | [diff] [blame] | 1694 | 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 Clayton | 56bbdaf | 2011-04-28 20:55:26 +0000 | [diff] [blame] | 1705 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | break; |
| 1709 | |
| 1710 | case eFormatFloat: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1711 | { |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 1712 | std::ostringstream ss; |
Jason Molenda | da53089 | 2012-11-01 23:35:19 +0000 | [diff] [blame] | 1713 | if (item_byte_size == sizeof(float)) |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 1714 | { |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 1715 | ss.precision(std::numeric_limits<float>::digits10); |
| 1716 | ss << GetFloat(&offset); |
Jason Molenda | da53089 | 2012-11-01 23:35:19 +0000 | [diff] [blame] | 1717 | } |
| 1718 | else if (item_byte_size == sizeof(double)) |
| 1719 | { |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 1720 | ss.precision(std::numeric_limits<double>::digits10); |
| 1721 | ss << GetDouble(&offset); |
Jason Molenda | da53089 | 2012-11-01 23:35:19 +0000 | [diff] [blame] | 1722 | } |
| 1723 | else if (item_byte_size == sizeof(long double)) |
| 1724 | { |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 1725 | ss.precision(std::numeric_limits<long double>::digits10); |
| 1726 | ss << GetLongDouble(&offset); |
Jason Molenda | da53089 | 2012-11-01 23:35:19 +0000 | [diff] [blame] | 1727 | } |
| 1728 | else |
| 1729 | { |
| 1730 | s->Printf("error: unsupported byte size (%u) for float format", item_byte_size); |
| 1731 | return offset; |
Sean Callanan | 509c728 | 2012-10-20 06:08:09 +0000 | [diff] [blame] | 1732 | } |
| 1733 | ss.flush(); |
| 1734 | s->Printf("%s", ss.str().c_str()); |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1735 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1736 | break; |
| 1737 | |
| 1738 | case eFormatUnicode16: |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1739 | s->Printf("U+%4.4x", GetU16 (&offset)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1740 | break; |
| 1741 | |
| 1742 | case eFormatUnicode32: |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1743 | s->Printf("U+0x%8.8x", GetU32 (&offset)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1744 | break; |
| 1745 | |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1746 | case eFormatAddressInfo: |
| 1747 | { |
| 1748 | addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset); |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1749 | s->Printf("0x%*.*" PRIx64, 2 * item_byte_size, 2 * item_byte_size, addr); |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1750 | if (exe_scope) |
| 1751 | { |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 1752 | TargetSP target_sp (exe_scope->CalculateTarget()); |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1753 | lldb_private::Address so_addr; |
Greg Clayton | 941f5da | 2012-07-11 22:18:24 +0000 | [diff] [blame] | 1754 | if (target_sp) |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1755 | { |
Greg Clayton | 941f5da | 2012-07-11 22:18:24 +0000 | [diff] [blame] | 1756 | 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 Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1769 | } |
| 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 Chen | 758835b | 2012-02-16 23:09:08 +0000 | [diff] [blame] | 1791 | else |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1792 | { |
Greg Clayton | 4dd3d90 | 2012-02-27 23:00:14 +0000 | [diff] [blame] | 1793 | s->Printf("error: unsupported byte size (%u) for hex float format", item_byte_size); |
| 1794 | return offset; |
Greg Clayton | 24a6bd9 | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 1795 | } |
| 1796 | break; |
| 1797 | |
Enrico Granata | 86e7c3e | 2011-07-12 22:56:10 +0000 | [diff] [blame] | 1798 | // 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 Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1803 | offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1804 | s->PutChar('}'); |
| 1805 | break; |
| 1806 | |
| 1807 | case eFormatVectorOfSInt8: |
| 1808 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1809 | offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1810 | s->PutChar('}'); |
| 1811 | break; |
| 1812 | |
| 1813 | case eFormatVectorOfUInt8: |
| 1814 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1815 | offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1816 | s->PutChar('}'); |
| 1817 | break; |
| 1818 | |
| 1819 | case eFormatVectorOfSInt16: |
| 1820 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1821 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1822 | s->PutChar('}'); |
| 1823 | break; |
| 1824 | |
| 1825 | case eFormatVectorOfUInt16: |
| 1826 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1827 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1828 | s->PutChar('}'); |
| 1829 | break; |
| 1830 | |
| 1831 | case eFormatVectorOfSInt32: |
| 1832 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1833 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1834 | s->PutChar('}'); |
| 1835 | break; |
| 1836 | |
| 1837 | case eFormatVectorOfUInt32: |
| 1838 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1839 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1840 | s->PutChar('}'); |
| 1841 | break; |
| 1842 | |
| 1843 | case eFormatVectorOfSInt64: |
| 1844 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1845 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1846 | s->PutChar('}'); |
| 1847 | break; |
| 1848 | |
| 1849 | case eFormatVectorOfUInt64: |
| 1850 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1851 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1852 | s->PutChar('}'); |
| 1853 | break; |
| 1854 | |
| 1855 | case eFormatVectorOfFloat32: |
| 1856 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1857 | offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1858 | s->PutChar('}'); |
| 1859 | break; |
| 1860 | |
| 1861 | case eFormatVectorOfFloat64: |
| 1862 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1863 | offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1864 | s->PutChar('}'); |
| 1865 | break; |
| 1866 | |
| 1867 | case eFormatVectorOfUInt128: |
| 1868 | s->PutChar('{'); |
Johnny Chen | 5564d80 | 2012-02-16 22:06:47 +0000 | [diff] [blame] | 1869 | offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1870 | 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 | //---------------------------------------------------------------------- |
| 1895 | uint32_t |
| 1896 | DataExtractor::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 Friedman | 7991225 | 2010-07-09 23:04:08 +0000 | [diff] [blame] | 1911 | uint32_t end_offset; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1912 | uint32_t count; |
| 1913 | StreamString sstr; |
Eli Friedman | 7991225 | 2010-07-09 23:04:08 +0000 | [diff] [blame] | 1914 | for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1915 | { |
| 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 Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1926 | sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset))); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | switch (type) |
| 1930 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1931 | 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 Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1940 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1944 | } |
| 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 | //---------------------------------------------------------------------- |
| 1958 | void |
| 1959 | DataExtractor::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 Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 1966 | lldb_private::UUID uuid(uuid_data, 16); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1967 | 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 Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 1976 | void |
| 1977 | DataExtractor::DumpHexBytes (Stream *s, |
| 1978 | const void *src, |
| 1979 | size_t src_len, |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 1980 | uint32_t bytes_per_line, |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 1981 | 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 Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 1989 | bytes_per_line, // Num bytes per line |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 1990 | base_addr, // Base address |
| 1991 | 0, 0); // Bitfield info |
| 1992 | } |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 1993 | |
| 1994 | size_t |
| 1995 | DataExtractor::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 | |
| 2011 | bool |
| 2012 | DataExtractor::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 | |
| 2041 | bool |
| 2042 | DataExtractor::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 Clayton | 141f8d9 | 2011-10-12 00:53:29 +0000 | [diff] [blame] | 2068 | } |