blob: 172be77279241b78777484ac92854eb8bb6445e7 [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>
15#include <string.h>
16#include <stdlib.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
23Stream::Stream (uint32_t flags, uint32_t addr_size, ByteOrder byte_order) :
24 m_flags (flags),
25 m_addr_size (addr_size),
26 m_byte_order (byte_order),
27 m_indent_level(0)
28{
29}
30
31Stream::Stream () :
32 m_flags (0),
33 m_addr_size (4),
Bruce Mitchener9ccb9702015-11-07 04:40:13 +000034 m_byte_order (endian::InlHostByteOrder()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 m_indent_level(0)
36{
37}
38
39//------------------------------------------------------------------
40// Destructor
41//------------------------------------------------------------------
42Stream::~Stream ()
43{
44}
45
46ByteOrder
47Stream::SetByteOrder (ByteOrder byte_order)
48{
49 ByteOrder old_byte_order = m_byte_order;
50 m_byte_order = byte_order;
51 return old_byte_order;
52}
53
54//------------------------------------------------------------------
55// Put an offset "uval" out to the stream using the printf format
56// in "format".
57//------------------------------------------------------------------
58void
59Stream::Offset (uint32_t uval, const char *format)
60{
61 Printf (format, uval);
62}
63
64//------------------------------------------------------------------
65// Put an SLEB128 "uval" out to the stream using the printf format
66// in "format".
67//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +000068size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069Stream::PutSLEB128 (int64_t sval)
70{
Greg Claytonc7bece562013-01-25 18:06:21 +000071 size_t bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +000072 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 {
74 bool more = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 while (more)
76 {
77 uint8_t byte = sval & 0x7fu;
78 sval >>= 7;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 /* sign bit of byte is 2nd high order bit (0x40) */
80 if ((sval == 0 && !(byte & 0x40)) ||
81 (sval == -1 && (byte & 0x40)) )
82 more = false;
83 else
84 // more bytes to come
85 byte |= 0x80u;
86 bytes_written += Write(&byte, 1);
87 }
88 }
89 else
90 {
Daniel Malead01b2952012-11-29 21:49:15 +000091 bytes_written = Printf ("0x%" PRIi64, sval);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 }
93
94 return bytes_written;
95
96}
97
98//------------------------------------------------------------------
99// Put an ULEB128 "uval" out to the stream using the printf format
100// in "format".
101//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000102size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103Stream::PutULEB128 (uint64_t uval)
104{
Greg Claytonc7bece562013-01-25 18:06:21 +0000105 size_t bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +0000106 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 {
108 do
109 {
110
111 uint8_t byte = uval & 0x7fu;
112 uval >>= 7;
113 if (uval != 0)
114 {
115 // more bytes to come
116 byte |= 0x80u;
117 }
118 bytes_written += Write(&byte, 1);
119 } while (uval != 0);
120 }
121 else
122 {
Daniel Malead01b2952012-11-29 21:49:15 +0000123 bytes_written = Printf ("0x%" PRIx64, uval);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 }
125 return bytes_written;
126}
127
128//------------------------------------------------------------------
Enrico Granata7b59f752012-01-31 17:18:40 +0000129// Print a raw NULL terminated C string to the stream.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000131size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132Stream::PutCString (const char *cstr)
133{
Greg Claytonc7bece562013-01-25 18:06:21 +0000134 size_t cstr_len = strlen(cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 // when in binary mode, emit the NULL terminator
Greg Clayton73b472d2010-10-27 03:32:59 +0000136 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 ++cstr_len;
138 return Write (cstr, cstr_len);
139}
140
141//------------------------------------------------------------------
142// Print a double quoted NULL terminated C string to the stream
143// using the printf format in "format".
144//------------------------------------------------------------------
145void
146Stream::QuotedCString (const char *cstr, const char *format)
147{
148 Printf (format, cstr);
149}
150
151//------------------------------------------------------------------
152// Put an address "addr" out to the stream with optional prefix
153// and suffix strings.
154//------------------------------------------------------------------
155void
Greg Claytonc7bece562013-01-25 18:06:21 +0000156Stream::Address (uint64_t addr, uint32_t addr_size, const char *prefix, const char *suffix)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157{
158 if (prefix == NULL)
159 prefix = "";
160 if (suffix == NULL)
161 suffix = "";
162// int addr_width = m_addr_size << 1;
Daniel Malead01b2952012-11-29 21:49:15 +0000163// Printf ("%s0x%0*" PRIx64 "%s", prefix, addr_width, addr, suffix);
164 Printf ("%s0x%0*" PRIx64 "%s", prefix, addr_size * 2, (uint64_t)addr, suffix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
167//------------------------------------------------------------------
168// Put an address range out to the stream with optional prefix
169// and suffix strings.
170//------------------------------------------------------------------
171void
Greg Claytonc7bece562013-01-25 18:06:21 +0000172Stream::AddressRange(uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size, const char *prefix, const char *suffix)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173{
Greg Clayton1abfe042011-11-28 00:51:27 +0000174 if (prefix && prefix[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 PutCString (prefix);
176 Address (lo_addr, addr_size, "[");
177 Address (hi_addr, addr_size, "-", ")");
Greg Clayton1abfe042011-11-28 00:51:27 +0000178 if (suffix && suffix[0])
179 PutCString (suffix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180}
181
182
Greg Claytonc7bece562013-01-25 18:06:21 +0000183size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184Stream::PutChar (char ch)
185{
186 return Write (&ch, 1);
187}
188
189
190//------------------------------------------------------------------
191// Print some formatted output to the stream.
192//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000193size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194Stream::Printf (const char *format, ...)
195{
196 va_list args;
197 va_start (args, format);
198 size_t result = PrintfVarArg(format, args);
199 va_end (args);
200 return result;
201}
202
203//------------------------------------------------------------------
204// Print some formatted output to the stream.
205//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000206size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207Stream::PrintfVarArg (const char *format, va_list args)
208{
209 char str[1024];
210 va_list args_copy;
211
212 va_copy (args_copy, args);
213
Greg Claytonc7bece562013-01-25 18:06:21 +0000214 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 // Try and format our string into a fixed buffer first and see if it fits
Greg Claytonc982c762010-07-09 20:39:50 +0000216 size_t length = ::vsnprintf (str, sizeof(str), format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217 if (length < sizeof(str))
218 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219 // Include the NULL termination byte for binary output
Greg Clayton73b472d2010-10-27 03:32:59 +0000220 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 length += 1;
222 // The formatted string fit into our stack based buffer, so we can just
223 // append that to our packet
224 bytes_written = Write (str, length);
225 }
226 else
227 {
228 // Our stack buffer wasn't big enough to contain the entire formatted
229 // string, so lets let vasprintf create the string for us!
230 char *str_ptr = NULL;
231 length = ::vasprintf (&str_ptr, format, args_copy);
232 if (str_ptr)
233 {
234 // Include the NULL termination byte for binary output
Greg Clayton73b472d2010-10-27 03:32:59 +0000235 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 length += 1;
237 bytes_written = Write (str_ptr, length);
238 ::free (str_ptr);
239 }
240 }
241 va_end (args_copy);
242 return bytes_written;
243}
244
245//------------------------------------------------------------------
246// Print and End of Line character to the stream
247//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000248size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249Stream::EOL()
250{
251 return PutChar ('\n');
252}
253
254//------------------------------------------------------------------
255// Indent the current line using the current indentation level and
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000256// print an optional string following the indentation spaces.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000258size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259Stream::Indent(const char *s)
260{
261 return Printf ("%*.*s%s", m_indent_level, m_indent_level, "", s ? s : "");
262}
263
264//------------------------------------------------------------------
265// Stream a character "ch" out to this stream.
266//------------------------------------------------------------------
267Stream&
268Stream::operator<< (char ch)
269{
270 PutChar (ch);
271 return *this;
272}
273
274//------------------------------------------------------------------
275// Stream the NULL terminated C string out to this stream.
276//------------------------------------------------------------------
277Stream&
278Stream::operator<< (const char *s)
279{
280 Printf ("%s", s);
281 return *this;
282}
283
284//------------------------------------------------------------------
285// Stream the pointer value out to this stream.
286//------------------------------------------------------------------
287Stream&
Pavel Labathc2f33f62015-07-22 07:58:17 +0000288Stream::operator<< (const void *p)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289{
Pavel Labathc2f33f62015-07-22 07:58:17 +0000290 Printf ("0x%.*tx", (int)sizeof(const void*) * 2, (ptrdiff_t)p);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 return *this;
292}
293
294//------------------------------------------------------------------
295// Stream a uint8_t "uval" out to this stream.
296//------------------------------------------------------------------
297Stream&
298Stream::operator<< (uint8_t uval)
299{
300 PutHex8(uval);
301 return *this;
302}
303
304//------------------------------------------------------------------
305// Stream a uint16_t "uval" out to this stream.
306//------------------------------------------------------------------
307Stream&
308Stream::operator<< (uint16_t uval)
309{
310 PutHex16(uval, m_byte_order);
311 return *this;
312}
313
314//------------------------------------------------------------------
315// Stream a uint32_t "uval" out to this stream.
316//------------------------------------------------------------------
317Stream&
318Stream::operator<< (uint32_t uval)
319{
320 PutHex32(uval, m_byte_order);
321 return *this;
322}
323
324//------------------------------------------------------------------
325// Stream a uint64_t "uval" out to this stream.
326//------------------------------------------------------------------
327Stream&
328Stream::operator<< (uint64_t uval)
329{
330 PutHex64(uval, m_byte_order);
331 return *this;
332}
333
334//------------------------------------------------------------------
335// Stream a int8_t "sval" out to this stream.
336//------------------------------------------------------------------
337Stream&
338Stream::operator<< (int8_t sval)
339{
340 Printf ("%i", (int)sval);
341 return *this;
342}
343
344//------------------------------------------------------------------
345// Stream a int16_t "sval" out to this stream.
346//------------------------------------------------------------------
347Stream&
348Stream::operator<< (int16_t sval)
349{
350 Printf ("%i", (int)sval);
351 return *this;
352}
353
354//------------------------------------------------------------------
355// Stream a int32_t "sval" out to this stream.
356//------------------------------------------------------------------
357Stream&
358Stream::operator<< (int32_t sval)
359{
360 Printf ("%i", (int)sval);
361 return *this;
362}
363
364//------------------------------------------------------------------
365// Stream a int64_t "sval" out to this stream.
366//------------------------------------------------------------------
367Stream&
368Stream::operator<< (int64_t sval)
369{
Daniel Malead01b2952012-11-29 21:49:15 +0000370 Printf ("%" PRIi64, sval);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 return *this;
372}
373
374//------------------------------------------------------------------
375// Get the current indentation level
376//------------------------------------------------------------------
377int
378Stream::GetIndentLevel() const
379{
380 return m_indent_level;
381}
382
383//------------------------------------------------------------------
384// Set the current indentation level
385//------------------------------------------------------------------
386void
387Stream::SetIndentLevel(int indent_level)
388{
389 m_indent_level = indent_level;
390}
391
392//------------------------------------------------------------------
393// Increment the current indentation level
394//------------------------------------------------------------------
395void
396Stream::IndentMore(int amount)
397{
398 m_indent_level += amount;
399}
400
401//------------------------------------------------------------------
402// Decrement the current indentation level
403//------------------------------------------------------------------
404void
405Stream::IndentLess (int amount)
406{
407 if (m_indent_level >= amount)
408 m_indent_level -= amount;
409 else
410 m_indent_level = 0;
411}
412
413//------------------------------------------------------------------
414// Get the address size in bytes
415//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000416uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417Stream::GetAddressByteSize() const
418{
419 return m_addr_size;
420}
421
422//------------------------------------------------------------------
423// Set the address size in bytes
424//------------------------------------------------------------------
425void
Greg Claytonc7bece562013-01-25 18:06:21 +0000426Stream::SetAddressByteSize(uint32_t addr_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427{
428 m_addr_size = addr_size;
429}
430
431//------------------------------------------------------------------
432// Returns true if the verbose flag bit is set in this stream.
433//------------------------------------------------------------------
434bool
435Stream::GetVerbose() const
436{
Greg Clayton73b472d2010-10-27 03:32:59 +0000437 return m_flags.Test(eVerbose);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438}
439
440//------------------------------------------------------------------
441// Returns true if the debug flag bit is set in this stream.
442//------------------------------------------------------------------
443bool
444Stream::GetDebug() const
445{
Greg Clayton73b472d2010-10-27 03:32:59 +0000446 return m_flags.Test(eDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
449//------------------------------------------------------------------
450// The flags get accessor
451//------------------------------------------------------------------
452Flags&
453Stream::GetFlags()
454{
455 return m_flags;
456}
457
458//------------------------------------------------------------------
459// The flags const get accessor
460//------------------------------------------------------------------
461const Flags&
462Stream::GetFlags() const
463{
464 return m_flags;
465}
466
Sean Callanan609f8c52010-07-02 02:43:42 +0000467//------------------------------------------------------------------
468// The byte order get accessor
469//------------------------------------------------------------------
470
471lldb::ByteOrder
472Stream::GetByteOrder() const
473{
474 return m_byte_order;
475}
476
Greg Claytonc7bece562013-01-25 18:06:21 +0000477size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478Stream::PrintfAsRawHex8 (const char *format, ...)
479{
480 va_list args;
481 va_list args_copy;
482 va_start (args, format);
Deepak Panickal99fbc072014-03-03 15:39:47 +0000483 va_copy (args_copy,args); // Copy this so we
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 char str[1024];
Greg Claytonc7bece562013-01-25 18:06:21 +0000486 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 // Try and format our string into a fixed buffer first and see if it fits
Greg Claytonc982c762010-07-09 20:39:50 +0000488 size_t length = ::vsnprintf (str, sizeof(str), format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 if (length < sizeof(str))
490 {
491 // The formatted string fit into our stack based buffer, so we can just
492 // append that to our packet
Greg Claytonc982c762010-07-09 20:39:50 +0000493 for (size_t i=0; i<length; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 bytes_written += _PutHex8 (str[i], false);
495 }
496 else
497 {
498 // Our stack buffer wasn't big enough to contain the entire formatted
499 // string, so lets let vasprintf create the string for us!
500 char *str_ptr = NULL;
501 length = ::vasprintf (&str_ptr, format, args_copy);
502 if (str_ptr)
503 {
Greg Claytonc982c762010-07-09 20:39:50 +0000504 for (size_t i=0; i<length; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 bytes_written += _PutHex8 (str_ptr[i], false);
506 ::free (str_ptr);
507 }
508 }
509 va_end (args);
510 va_end (args_copy);
511
512 return bytes_written;
513}
514
Greg Claytonc7bece562013-01-25 18:06:21 +0000515size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516Stream::PutNHex8 (size_t n, uint8_t uvalue)
517{
Greg Claytonc7bece562013-01-25 18:06:21 +0000518 size_t bytes_written = 0;
Greg Claytonc982c762010-07-09 20:39:50 +0000519 for (size_t i=0; i<n; ++i)
Greg Clayton73b472d2010-10-27 03:32:59 +0000520 bytes_written += _PutHex8 (uvalue, m_flags.Test(eAddPrefix));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 return bytes_written;
522}
523
Greg Claytonc7bece562013-01-25 18:06:21 +0000524size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525Stream::_PutHex8 (uint8_t uvalue, bool add_prefix)
526{
Greg Claytonc7bece562013-01-25 18:06:21 +0000527 size_t bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +0000528 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529 {
530 bytes_written = Write (&uvalue, 1);
531 }
532 else
533 {
534 if (add_prefix)
535 PutCString("0x");
536
537 static char g_hex_to_ascii_hex_char[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
538 char nibble_chars[2];
539 nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
540 nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
541 bytes_written = Write (nibble_chars, sizeof(nibble_chars));
542 }
543 return bytes_written;
544}
545
Greg Claytonc7bece562013-01-25 18:06:21 +0000546size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547Stream::PutHex8 (uint8_t uvalue)
548{
Greg Clayton73b472d2010-10-27 03:32:59 +0000549 return _PutHex8 (uvalue, m_flags.Test(eAddPrefix));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550}
551
Greg Claytonc7bece562013-01-25 18:06:21 +0000552size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553Stream::PutHex16 (uint16_t uvalue, ByteOrder byte_order)
554{
555 if (byte_order == eByteOrderInvalid)
556 byte_order = m_byte_order;
557
Greg Clayton73b472d2010-10-27 03:32:59 +0000558 bool add_prefix = m_flags.Test(eAddPrefix);
Greg Claytonc7bece562013-01-25 18:06:21 +0000559 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 if (byte_order == eByteOrderLittle)
561 {
Greg Claytonc982c762010-07-09 20:39:50 +0000562 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
Greg Claytonc7bece562013-01-25 18:06:21 +0000563 bytes_written += _PutHex8 ((uint8_t)(uvalue >> (byte * 8)), add_prefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 }
565 else
566 {
Greg Claytonc982c762010-07-09 20:39:50 +0000567 for (size_t byte = sizeof(uvalue)-1; byte < sizeof(uvalue); --byte, add_prefix = false)
Greg Claytonc7bece562013-01-25 18:06:21 +0000568 bytes_written += _PutHex8 ((uint8_t)(uvalue >> (byte * 8)), add_prefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 }
570 return bytes_written;
571}
572
Greg Claytonc7bece562013-01-25 18:06:21 +0000573size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order)
575{
576 if (byte_order == eByteOrderInvalid)
577 byte_order = m_byte_order;
578
Greg Clayton73b472d2010-10-27 03:32:59 +0000579 bool add_prefix = m_flags.Test(eAddPrefix);
Greg Claytonc7bece562013-01-25 18:06:21 +0000580 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 if (byte_order == eByteOrderLittle)
582 {
Greg Claytonc982c762010-07-09 20:39:50 +0000583 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
Greg Claytonc7bece562013-01-25 18:06:21 +0000584 bytes_written += _PutHex8 ((uint8_t)(uvalue >> (byte * 8)), add_prefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 }
586 else
587 {
Greg Claytonc982c762010-07-09 20:39:50 +0000588 for (size_t byte = sizeof(uvalue)-1; byte < sizeof(uvalue); --byte, add_prefix = false)
Greg Claytonc7bece562013-01-25 18:06:21 +0000589 bytes_written += _PutHex8 ((uint8_t)(uvalue >> (byte * 8)), add_prefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 }
591 return bytes_written;
592}
593
Greg Claytonc7bece562013-01-25 18:06:21 +0000594size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order)
596{
597 if (byte_order == eByteOrderInvalid)
598 byte_order = m_byte_order;
599
Greg Clayton73b472d2010-10-27 03:32:59 +0000600 bool add_prefix = m_flags.Test(eAddPrefix);
Greg Claytonc7bece562013-01-25 18:06:21 +0000601 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 if (byte_order == eByteOrderLittle)
603 {
Greg Claytonc982c762010-07-09 20:39:50 +0000604 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
Greg Claytonc7bece562013-01-25 18:06:21 +0000605 bytes_written += _PutHex8 ((uint8_t)(uvalue >> (byte * 8)), add_prefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606 }
607 else
608 {
Greg Claytonc982c762010-07-09 20:39:50 +0000609 for (size_t byte = sizeof(uvalue)-1; byte < sizeof(uvalue); --byte, add_prefix = false)
Greg Claytonc7bece562013-01-25 18:06:21 +0000610 bytes_written += _PutHex8 ((uint8_t)(uvalue >> (byte * 8)), add_prefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 }
612 return bytes_written;
613}
614
Greg Claytonc7bece562013-01-25 18:06:21 +0000615size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616Stream::PutMaxHex64
617(
618 uint64_t uvalue,
619 size_t byte_size,
620 lldb::ByteOrder byte_order
621)
622{
623 switch (byte_size)
624 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000625 case 1: return PutHex8 ((uint8_t)uvalue);
626 case 2: return PutHex16 ((uint16_t)uvalue);
627 case 4: return PutHex32 ((uint32_t)uvalue);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 case 8: return PutHex64 (uvalue);
629 }
630 return 0;
631}
632
Greg Claytonc7bece562013-01-25 18:06:21 +0000633size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634Stream::PutPointer (void *ptr)
635{
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000636 return PutRawBytes (&ptr, sizeof(ptr), endian::InlHostByteOrder(), endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637}
638
Greg Claytonc7bece562013-01-25 18:06:21 +0000639size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640Stream::PutFloat(float f, ByteOrder byte_order)
641{
642 if (byte_order == eByteOrderInvalid)
643 byte_order = m_byte_order;
644
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000645 return PutRawBytes (&f, sizeof(f), endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646}
647
Greg Claytonc7bece562013-01-25 18:06:21 +0000648size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649Stream::PutDouble(double d, ByteOrder byte_order)
650{
651 if (byte_order == eByteOrderInvalid)
652 byte_order = m_byte_order;
653
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000654 return PutRawBytes (&d, sizeof(d), endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655}
656
Greg Claytonc7bece562013-01-25 18:06:21 +0000657size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658Stream::PutLongDouble(long double ld, ByteOrder byte_order)
659{
660 if (byte_order == eByteOrderInvalid)
661 byte_order = m_byte_order;
662
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000663 return PutRawBytes (&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664}
665
Greg Claytonc7bece562013-01-25 18:06:21 +0000666size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667Stream::PutRawBytes (const void *s, size_t src_len, ByteOrder src_byte_order, ByteOrder dst_byte_order)
668{
669 if (src_byte_order == eByteOrderInvalid)
670 src_byte_order = m_byte_order;
671
672 if (dst_byte_order == eByteOrderInvalid)
673 dst_byte_order = m_byte_order;
674
Greg Claytonc7bece562013-01-25 18:06:21 +0000675 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 const uint8_t *src = (const uint8_t *)s;
Greg Clayton73b472d2010-10-27 03:32:59 +0000677 bool binary_was_set = m_flags.Test (eBinary);
678 if (!binary_was_set)
679 m_flags.Set (eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 if (src_byte_order == dst_byte_order)
681 {
Greg Claytonc982c762010-07-09 20:39:50 +0000682 for (size_t i = 0; i < src_len; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 bytes_written += _PutHex8 (src[i], false);
684 }
685 else
686 {
Greg Claytonc982c762010-07-09 20:39:50 +0000687 for (size_t i = src_len-1; i < src_len; --i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 bytes_written += _PutHex8 (src[i], false);
689 }
Greg Clayton73b472d2010-10-27 03:32:59 +0000690 if (!binary_was_set)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 m_flags.Clear (eBinary);
692
693 return bytes_written;
694}
695
Greg Claytonc7bece562013-01-25 18:06:21 +0000696size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697Stream::PutBytesAsRawHex8 (const void *s, size_t src_len, ByteOrder src_byte_order, ByteOrder dst_byte_order)
698{
699 if (src_byte_order == eByteOrderInvalid)
700 src_byte_order = m_byte_order;
701
702 if (dst_byte_order == eByteOrderInvalid)
703 dst_byte_order = m_byte_order;
704
Greg Claytonc7bece562013-01-25 18:06:21 +0000705 size_t bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706 const uint8_t *src = (const uint8_t *)s;
Greg Clayton73b472d2010-10-27 03:32:59 +0000707 bool binary_is_set = m_flags.Test(eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 m_flags.Clear(eBinary);
709 if (src_byte_order == dst_byte_order)
710 {
Greg Claytonc982c762010-07-09 20:39:50 +0000711 for (size_t i = 0; i < src_len; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 bytes_written += _PutHex8 (src[i], false);
713 }
714 else
715 {
Greg Claytonc982c762010-07-09 20:39:50 +0000716 for (size_t i = src_len-1; i < src_len; --i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 bytes_written += _PutHex8 (src[i], false);
718 }
719 if (binary_is_set)
720 m_flags.Set(eBinary);
721
722 return bytes_written;
723}
724
Greg Claytonc7bece562013-01-25 18:06:21 +0000725size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726Stream::PutCStringAsRawHex8 (const char *s)
727{
Greg Claytonc7bece562013-01-25 18:06:21 +0000728 size_t bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +0000729 bool binary_is_set = m_flags.Test(eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730 m_flags.Clear(eBinary);
731 do
732 {
733 bytes_written += _PutHex8 (*s, false);
734 ++s;
735 } while (*s);
736 if (binary_is_set)
737 m_flags.Set(eBinary);
738 return bytes_written;
739}
740
741void
742Stream::UnitTest(Stream *s)
743{
744 s->PutHex8(0x12);
745
746 s->PutChar(' ');
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000747 s->PutHex16(0x3456, endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 s->PutChar(' ');
749 s->PutHex16(0x3456, eByteOrderBig);
750 s->PutChar(' ');
751 s->PutHex16(0x3456, eByteOrderLittle);
752
753 s->PutChar(' ');
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000754 s->PutHex32(0x789abcde, endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755 s->PutChar(' ');
756 s->PutHex32(0x789abcde, eByteOrderBig);
757 s->PutChar(' ');
758 s->PutHex32(0x789abcde, eByteOrderLittle);
759
760 s->PutChar(' ');
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000761 s->PutHex64(0x1122334455667788ull, endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762 s->PutChar(' ');
763 s->PutHex64(0x1122334455667788ull, eByteOrderBig);
764 s->PutChar(' ');
765 s->PutHex64(0x1122334455667788ull, eByteOrderLittle);
766
767 const char *hola = "Hello World!!!";
768 s->PutChar(' ');
769 s->PutCString (hola);
770
771 s->PutChar(' ');
772 s->Write (hola, 5);
773
774 s->PutChar(' ');
775 s->PutCStringAsRawHex8 (hola);
776
777 s->PutChar(' ');
778 s->PutCStringAsRawHex8 ("01234");
779
780 s->PutChar(' ');
781 s->Printf ("pid=%i", 12733);
782
783 s->PutChar(' ');
784 s->PrintfAsRawHex8 ("pid=%i", 12733);
785 s->PutChar('\n');
786}
787