blob: f8d78c33b429f76ec57041e1b359e4d5fe66d9e1 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Stream.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 "lldb/Core/Stream.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000011#include "lldb/Host/Endian.h"
Zachary Turnerf343968f2016-08-09 23:06:08 +000012#include "lldb/Host/PosixApi.h"
Benjamin Kramer581a6912012-02-27 18:46:54 +000013#include <stddef.h>
Eli Friedman88966972010-06-09 08:50:27 +000014#include <stdio.h>
Eli Friedman88966972010-06-09 08:50:27 +000015#include <stdlib.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include <string.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Daniel Malead01b2952012-11-29 21:49:15 +000018#include <inttypes.h>
19
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020using namespace lldb;
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
24 : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order),
25 m_indent_level(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027Stream::Stream()
28 : m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
29 m_indent_level(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31//------------------------------------------------------------------
32// Destructor
33//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000034Stream::~Stream() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036ByteOrder Stream::SetByteOrder(ByteOrder byte_order) {
37 ByteOrder old_byte_order = m_byte_order;
38 m_byte_order = byte_order;
39 return old_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040}
41
42//------------------------------------------------------------------
43// Put an offset "uval" out to the stream using the printf format
44// in "format".
45//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000046void Stream::Offset(uint32_t uval, const char *format) { Printf(format, uval); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48//------------------------------------------------------------------
49// Put an SLEB128 "uval" out to the stream using the printf format
50// in "format".
51//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000052size_t Stream::PutSLEB128(int64_t sval) {
53 size_t bytes_written = 0;
54 if (m_flags.Test(eBinary)) {
55 bool more = true;
56 while (more) {
57 uint8_t byte = sval & 0x7fu;
58 sval >>= 7;
59 /* sign bit of byte is 2nd high order bit (0x40) */
60 if ((sval == 0 && !(byte & 0x40)) || (sval == -1 && (byte & 0x40)))
61 more = false;
62 else
63 // more bytes to come
64 byte |= 0x80u;
65 bytes_written += Write(&byte, 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 } else {
68 bytes_written = Printf("0x%" PRIi64, sval);
69 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
74//------------------------------------------------------------------
75// Put an ULEB128 "uval" out to the stream using the printf format
76// in "format".
77//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000078size_t Stream::PutULEB128(uint64_t uval) {
79 size_t bytes_written = 0;
80 if (m_flags.Test(eBinary)) {
81 do {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 uint8_t byte = uval & 0x7fu;
84 uval >>= 7;
85 if (uval != 0) {
86 // more bytes to come
87 byte |= 0x80u;
88 }
89 bytes_written += Write(&byte, 1);
90 } while (uval != 0);
91 } else {
92 bytes_written = Printf("0x%" PRIx64, uval);
93 }
94 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095}
96
97//------------------------------------------------------------------
Enrico Granata7b59f752012-01-31 17:18:40 +000098// Print a raw NULL terminated C string to the stream.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100size_t Stream::PutCString(const char *cstr) {
101 size_t cstr_len = strlen(cstr);
102 // when in binary mode, emit the NULL terminator
103 if (m_flags.Test(eBinary))
104 ++cstr_len;
105 return Write(cstr, cstr_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106}
107
108//------------------------------------------------------------------
109// Print a double quoted NULL terminated C string to the stream
110// using the printf format in "format".
111//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112void Stream::QuotedCString(const char *cstr, const char *format) {
113 Printf(format, cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
116//------------------------------------------------------------------
117// Put an address "addr" out to the stream with optional prefix
118// and suffix strings.
119//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120void Stream::Address(uint64_t addr, uint32_t addr_size, const char *prefix,
121 const char *suffix) {
122 if (prefix == NULL)
123 prefix = "";
124 if (suffix == NULL)
125 suffix = "";
126 // int addr_width = m_addr_size << 1;
127 // Printf ("%s0x%0*" PRIx64 "%s", prefix, addr_width, addr, suffix);
128 Printf("%s0x%0*" PRIx64 "%s", prefix, addr_size * 2, (uint64_t)addr, suffix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129}
130
131//------------------------------------------------------------------
132// Put an address range out to the stream with optional prefix
133// and suffix strings.
134//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135void Stream::AddressRange(uint64_t lo_addr, uint64_t hi_addr,
136 uint32_t addr_size, const char *prefix,
137 const char *suffix) {
138 if (prefix && prefix[0])
139 PutCString(prefix);
140 Address(lo_addr, addr_size, "[");
141 Address(hi_addr, addr_size, "-", ")");
142 if (suffix && suffix[0])
143 PutCString(suffix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146size_t Stream::PutChar(char ch) { return Write(&ch, 1); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
148//------------------------------------------------------------------
149// Print some formatted output to the stream.
150//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151size_t Stream::Printf(const char *format, ...) {
152 va_list args;
153 va_start(args, format);
154 size_t result = PrintfVarArg(format, args);
155 va_end(args);
156 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157}
158
159//------------------------------------------------------------------
160// Print some formatted output to the stream.
161//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162size_t Stream::PrintfVarArg(const char *format, va_list args) {
163 char str[1024];
164 va_list args_copy;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 va_copy(args_copy, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 size_t bytes_written = 0;
169 // Try and format our string into a fixed buffer first and see if it fits
170 size_t length = ::vsnprintf(str, sizeof(str), format, args);
171 if (length < sizeof(str)) {
172 // Include the NULL termination byte for binary output
173 if (m_flags.Test(eBinary))
174 length += 1;
175 // The formatted string fit into our stack based buffer, so we can just
176 // append that to our packet
177 bytes_written = Write(str, length);
178 } else {
179 // Our stack buffer wasn't big enough to contain the entire formatted
180 // string, so lets let vasprintf create the string for us!
181 char *str_ptr = NULL;
182 length = ::vasprintf(&str_ptr, format, args_copy);
183 if (str_ptr) {
184 // Include the NULL termination byte for binary output
185 if (m_flags.Test(eBinary))
186 length += 1;
187 bytes_written = Write(str_ptr, length);
188 ::free(str_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 }
191 va_end(args_copy);
192 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193}
194
195//------------------------------------------------------------------
196// Print and End of Line character to the stream
197//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198size_t Stream::EOL() { return PutChar('\n'); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199
200//------------------------------------------------------------------
201// Indent the current line using the current indentation level and
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000202// print an optional string following the indentation spaces.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204size_t Stream::Indent(const char *s) {
205 return Printf("%*.*s%s", m_indent_level, m_indent_level, "", s ? s : "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
208//------------------------------------------------------------------
209// Stream a character "ch" out to this stream.
210//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211Stream &Stream::operator<<(char ch) {
212 PutChar(ch);
213 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
216//------------------------------------------------------------------
217// Stream the NULL terminated C string out to this stream.
218//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219Stream &Stream::operator<<(const char *s) {
220 Printf("%s", s);
221 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
224//------------------------------------------------------------------
225// Stream the pointer value out to this stream.
226//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227Stream &Stream::operator<<(const void *p) {
228 Printf("0x%.*tx", (int)sizeof(const void *) * 2, (ptrdiff_t)p);
229 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230}
231
232//------------------------------------------------------------------
233// Stream a uint8_t "uval" out to this stream.
234//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235Stream &Stream::operator<<(uint8_t uval) {
236 PutHex8(uval);
237 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
240//------------------------------------------------------------------
241// Stream a uint16_t "uval" out to this stream.
242//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243Stream &Stream::operator<<(uint16_t uval) {
244 PutHex16(uval, m_byte_order);
245 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
248//------------------------------------------------------------------
249// Stream a uint32_t "uval" out to this stream.
250//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251Stream &Stream::operator<<(uint32_t uval) {
252 PutHex32(uval, m_byte_order);
253 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
256//------------------------------------------------------------------
257// Stream a uint64_t "uval" out to this stream.
258//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259Stream &Stream::operator<<(uint64_t uval) {
260 PutHex64(uval, m_byte_order);
261 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
264//------------------------------------------------------------------
265// Stream a int8_t "sval" out to this stream.
266//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267Stream &Stream::operator<<(int8_t sval) {
268 Printf("%i", (int)sval);
269 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270}
271
272//------------------------------------------------------------------
273// Stream a int16_t "sval" out to this stream.
274//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275Stream &Stream::operator<<(int16_t sval) {
276 Printf("%i", (int)sval);
277 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278}
279
280//------------------------------------------------------------------
281// Stream a int32_t "sval" out to this stream.
282//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283Stream &Stream::operator<<(int32_t sval) {
284 Printf("%i", (int)sval);
285 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286}
287
288//------------------------------------------------------------------
289// Stream a int64_t "sval" out to this stream.
290//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291Stream &Stream::operator<<(int64_t sval) {
292 Printf("%" PRIi64, sval);
293 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
296//------------------------------------------------------------------
297// Get the current indentation level
298//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299int Stream::GetIndentLevel() const { return m_indent_level; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300
301//------------------------------------------------------------------
302// Set the current indentation level
303//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304void Stream::SetIndentLevel(int indent_level) { m_indent_level = indent_level; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305
306//------------------------------------------------------------------
307// Increment the current indentation level
308//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309void Stream::IndentMore(int amount) { m_indent_level += amount; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310
311//------------------------------------------------------------------
312// Decrement the current indentation level
313//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314void Stream::IndentLess(int amount) {
315 if (m_indent_level >= amount)
316 m_indent_level -= amount;
317 else
318 m_indent_level = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
321//------------------------------------------------------------------
322// Get the address size in bytes
323//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325
326//------------------------------------------------------------------
327// Set the address size in bytes
328//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329void Stream::SetAddressByteSize(uint32_t addr_size) { m_addr_size = addr_size; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330
331//------------------------------------------------------------------
332// Returns true if the verbose flag bit is set in this stream.
333//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334bool Stream::GetVerbose() const { return m_flags.Test(eVerbose); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335
336//------------------------------------------------------------------
337// Returns true if the debug flag bit is set in this stream.
338//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339bool Stream::GetDebug() const { return m_flags.Test(eDebug); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340
341//------------------------------------------------------------------
342// The flags get accessor
343//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344Flags &Stream::GetFlags() { return m_flags; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345
346//------------------------------------------------------------------
347// The flags const get accessor
348//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349const Flags &Stream::GetFlags() const { return m_flags; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350
Sean Callanan609f8c52010-07-02 02:43:42 +0000351//------------------------------------------------------------------
352// The byte order get accessor
353//------------------------------------------------------------------
354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355lldb::ByteOrder Stream::GetByteOrder() const { return m_byte_order; }
Sean Callanan609f8c52010-07-02 02:43:42 +0000356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357size_t Stream::PrintfAsRawHex8(const char *format, ...) {
358 va_list args;
359 va_list args_copy;
360 va_start(args, format);
361 va_copy(args_copy, args); // Copy this so we
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 char str[1024];
364 size_t bytes_written = 0;
365 // Try and format our string into a fixed buffer first and see if it fits
366 size_t length = ::vsnprintf(str, sizeof(str), format, args);
367 if (length < sizeof(str)) {
368 // The formatted string fit into our stack based buffer, so we can just
369 // append that to our packet
370 for (size_t i = 0; i < length; ++i)
371 bytes_written += _PutHex8(str[i], false);
372 } else {
373 // Our stack buffer wasn't big enough to contain the entire formatted
374 // string, so lets let vasprintf create the string for us!
375 char *str_ptr = NULL;
376 length = ::vasprintf(&str_ptr, format, args_copy);
377 if (str_ptr) {
378 for (size_t i = 0; i < length; ++i)
379 bytes_written += _PutHex8(str_ptr[i], false);
380 ::free(str_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 }
383 va_end(args);
384 va_end(args_copy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389size_t Stream::PutNHex8(size_t n, uint8_t uvalue) {
390 size_t bytes_written = 0;
391 for (size_t i = 0; i < n; ++i)
392 bytes_written += _PutHex8(uvalue, m_flags.Test(eAddPrefix));
393 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396size_t Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {
397 size_t bytes_written = 0;
398 if (m_flags.Test(eBinary)) {
399 bytes_written = Write(&uvalue, 1);
400 } else {
401 if (add_prefix)
402 PutCString("0x");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 static char g_hex_to_ascii_hex_char[16] = {'0', '1', '2', '3', '4', '5',
405 '6', '7', '8', '9', 'a', 'b',
406 'c', 'd', 'e', 'f'};
407 char nibble_chars[2];
408 nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
409 nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
410 bytes_written = Write(nibble_chars, sizeof(nibble_chars));
411 }
412 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413}
414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415size_t Stream::PutHex8(uint8_t uvalue) {
416 return _PutHex8(uvalue, m_flags.Test(eAddPrefix));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417}
418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) {
420 if (byte_order == eByteOrderInvalid)
421 byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 bool add_prefix = m_flags.Test(eAddPrefix);
424 size_t bytes_written = 0;
425 if (byte_order == eByteOrderLittle) {
426 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
427 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), add_prefix);
428 } else {
429 for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue);
430 --byte, add_prefix = false)
431 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), add_prefix);
432 }
433 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434}
435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) {
437 if (byte_order == eByteOrderInvalid)
438 byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
Kate Stoneb9c1b512016-09-06 20:57:50 +0000440 bool add_prefix = m_flags.Test(eAddPrefix);
441 size_t bytes_written = 0;
442 if (byte_order == eByteOrderLittle) {
443 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
444 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), add_prefix);
445 } else {
446 for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue);
447 --byte, add_prefix = false)
448 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), add_prefix);
449 }
450 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451}
452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) {
454 if (byte_order == eByteOrderInvalid)
455 byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 bool add_prefix = m_flags.Test(eAddPrefix);
458 size_t bytes_written = 0;
459 if (byte_order == eByteOrderLittle) {
460 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
461 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), add_prefix);
462 } else {
463 for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue);
464 --byte, add_prefix = false)
465 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), add_prefix);
466 }
467 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size,
471 lldb::ByteOrder byte_order) {
472 switch (byte_size) {
473 case 1:
474 return PutHex8((uint8_t)uvalue);
475 case 2:
476 return PutHex16((uint16_t)uvalue);
477 case 4:
478 return PutHex32((uint32_t)uvalue);
479 case 8:
480 return PutHex64(uvalue);
481 }
482 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483}
484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485size_t Stream::PutPointer(void *ptr) {
486 return PutRawBytes(&ptr, sizeof(ptr), endian::InlHostByteOrder(),
487 endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490size_t Stream::PutFloat(float f, ByteOrder byte_order) {
491 if (byte_order == eByteOrderInvalid)
492 byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 return PutRawBytes(&f, sizeof(f), endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495}
496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497size_t Stream::PutDouble(double d, ByteOrder byte_order) {
498 if (byte_order == eByteOrderInvalid)
499 byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 return PutRawBytes(&d, sizeof(d), endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502}
503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504size_t Stream::PutLongDouble(long double ld, ByteOrder byte_order) {
505 if (byte_order == eByteOrderInvalid)
506 byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 return PutRawBytes(&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511size_t Stream::PutRawBytes(const void *s, size_t src_len,
512 ByteOrder src_byte_order, ByteOrder dst_byte_order) {
513 if (src_byte_order == eByteOrderInvalid)
514 src_byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 if (dst_byte_order == eByteOrderInvalid)
517 dst_byte_order = m_byte_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 size_t bytes_written = 0;
520 const uint8_t *src = (const uint8_t *)s;
521 bool binary_was_set = m_flags.Test(eBinary);
522 if (!binary_was_set)
523 m_flags.Set(eBinary);
524 if (src_byte_order == dst_byte_order) {
525 for (size_t i = 0; i < src_len; ++i)
526 bytes_written += _PutHex8(src[i], false);
527 } else {
528 for (size_t i = src_len - 1; i < src_len; --i)
529 bytes_written += _PutHex8(src[i], false);
530 }
531 if (!binary_was_set)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 m_flags.Clear(eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535}
536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len,
538 ByteOrder src_byte_order,
539 ByteOrder dst_byte_order) {
540 if (src_byte_order == eByteOrderInvalid)
541 src_byte_order = m_byte_order;
542
543 if (dst_byte_order == eByteOrderInvalid)
544 dst_byte_order = m_byte_order;
545
546 size_t bytes_written = 0;
547 const uint8_t *src = (const uint8_t *)s;
548 bool binary_is_set = m_flags.Test(eBinary);
549 m_flags.Clear(eBinary);
550 if (src_byte_order == dst_byte_order) {
551 for (size_t i = 0; i < src_len; ++i)
552 bytes_written += _PutHex8(src[i], false);
553 } else {
554 for (size_t i = src_len - 1; i < src_len; --i)
555 bytes_written += _PutHex8(src[i], false);
556 }
557 if (binary_is_set)
558 m_flags.Set(eBinary);
559
560 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563size_t Stream::PutCStringAsRawHex8(const char *s) {
564 size_t bytes_written = 0;
565 bool binary_is_set = m_flags.Test(eBinary);
566 m_flags.Clear(eBinary);
567 do {
568 bytes_written += _PutHex8(*s, false);
569 ++s;
570 } while (*s);
571 if (binary_is_set)
572 m_flags.Set(eBinary);
573 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574}
575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576void Stream::UnitTest(Stream *s) {
577 s->PutHex8(0x12);
578
579 s->PutChar(' ');
580 s->PutHex16(0x3456, endian::InlHostByteOrder());
581 s->PutChar(' ');
582 s->PutHex16(0x3456, eByteOrderBig);
583 s->PutChar(' ');
584 s->PutHex16(0x3456, eByteOrderLittle);
585
586 s->PutChar(' ');
587 s->PutHex32(0x789abcde, endian::InlHostByteOrder());
588 s->PutChar(' ');
589 s->PutHex32(0x789abcde, eByteOrderBig);
590 s->PutChar(' ');
591 s->PutHex32(0x789abcde, eByteOrderLittle);
592
593 s->PutChar(' ');
594 s->PutHex64(0x1122334455667788ull, endian::InlHostByteOrder());
595 s->PutChar(' ');
596 s->PutHex64(0x1122334455667788ull, eByteOrderBig);
597 s->PutChar(' ');
598 s->PutHex64(0x1122334455667788ull, eByteOrderLittle);
599
600 const char *hola = "Hello World!!!";
601 s->PutChar(' ');
602 s->PutCString(hola);
603
604 s->PutChar(' ');
605 s->Write(hola, 5);
606
607 s->PutChar(' ');
608 s->PutCStringAsRawHex8(hola);
609
610 s->PutChar(' ');
611 s->PutCStringAsRawHex8("01234");
612
613 s->PutChar(' ');
614 s->Printf("pid=%i", 12733);
615
616 s->PutChar(' ');
617 s->PrintfAsRawHex8("pid=%i", 12733);
618 s->PutChar('\n');
619}