blob: 6e5f99c1105e3cd858f82653a62b1c7efaf0c5a8 [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"
Benjamin Kramer581a6912012-02-27 18:46:54 +000012#include <stddef.h>
Eli Friedman88966972010-06-09 08:50:27 +000013#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20Stream::Stream (uint32_t flags, uint32_t addr_size, ByteOrder byte_order) :
21 m_flags (flags),
22 m_addr_size (addr_size),
23 m_byte_order (byte_order),
24 m_indent_level(0)
25{
26}
27
28Stream::Stream () :
29 m_flags (0),
30 m_addr_size (4),
Greg Clayton7fb56d02011-02-01 01:31:41 +000031 m_byte_order (lldb::endian::InlHostByteOrder()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032 m_indent_level(0)
33{
34}
35
36//------------------------------------------------------------------
37// Destructor
38//------------------------------------------------------------------
39Stream::~Stream ()
40{
41}
42
43ByteOrder
44Stream::SetByteOrder (ByteOrder byte_order)
45{
46 ByteOrder old_byte_order = m_byte_order;
47 m_byte_order = byte_order;
48 return old_byte_order;
49}
50
51//------------------------------------------------------------------
52// Put an offset "uval" out to the stream using the printf format
53// in "format".
54//------------------------------------------------------------------
55void
56Stream::Offset (uint32_t uval, const char *format)
57{
58 Printf (format, uval);
59}
60
61//------------------------------------------------------------------
62// Put an SLEB128 "uval" out to the stream using the printf format
63// in "format".
64//------------------------------------------------------------------
65int
66Stream::PutSLEB128 (int64_t sval)
67{
68 int bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +000069 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 {
71 bool more = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 while (more)
73 {
74 uint8_t byte = sval & 0x7fu;
75 sval >>= 7;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076 /* sign bit of byte is 2nd high order bit (0x40) */
77 if ((sval == 0 && !(byte & 0x40)) ||
78 (sval == -1 && (byte & 0x40)) )
79 more = false;
80 else
81 // more bytes to come
82 byte |= 0x80u;
83 bytes_written += Write(&byte, 1);
84 }
85 }
86 else
87 {
88 bytes_written = Printf ("0x%lli", sval);
89 }
90
91 return bytes_written;
92
93}
94
95//------------------------------------------------------------------
96// Put an ULEB128 "uval" out to the stream using the printf format
97// in "format".
98//------------------------------------------------------------------
99int
100Stream::PutULEB128 (uint64_t uval)
101{
102 int bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +0000103 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 {
105 do
106 {
107
108 uint8_t byte = uval & 0x7fu;
109 uval >>= 7;
110 if (uval != 0)
111 {
112 // more bytes to come
113 byte |= 0x80u;
114 }
115 bytes_written += Write(&byte, 1);
116 } while (uval != 0);
117 }
118 else
119 {
120 bytes_written = Printf ("0x%llx", uval);
121 }
122 return bytes_written;
123}
124
125//------------------------------------------------------------------
Enrico Granata7b59f752012-01-31 17:18:40 +0000126// Print a raw NULL terminated C string to the stream.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127//------------------------------------------------------------------
128int
129Stream::PutCString (const char *cstr)
130{
131 int cstr_len = strlen(cstr);
132 // when in binary mode, emit the NULL terminator
Greg Clayton73b472d2010-10-27 03:32:59 +0000133 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 ++cstr_len;
135 return Write (cstr, cstr_len);
136}
137
138//------------------------------------------------------------------
139// Print a double quoted NULL terminated C string to the stream
140// using the printf format in "format".
141//------------------------------------------------------------------
142void
143Stream::QuotedCString (const char *cstr, const char *format)
144{
145 Printf (format, cstr);
146}
147
148//------------------------------------------------------------------
149// Put an address "addr" out to the stream with optional prefix
150// and suffix strings.
151//------------------------------------------------------------------
152void
153Stream::Address (uint64_t addr, int addr_size, const char *prefix, const char *suffix)
154{
155 if (prefix == NULL)
156 prefix = "";
157 if (suffix == NULL)
158 suffix = "";
159// int addr_width = m_addr_size << 1;
160// Printf ("%s0x%0*llx%s", prefix, addr_width, addr, suffix);
161 Printf ("%s0x%0*llx%s", prefix, addr_size * 2, (uint64_t)addr, suffix);
162}
163
164//------------------------------------------------------------------
165// Put an address range out to the stream with optional prefix
166// and suffix strings.
167//------------------------------------------------------------------
168void
169Stream::AddressRange(uint64_t lo_addr, uint64_t hi_addr, int addr_size, const char *prefix, const char *suffix)
170{
Greg Clayton1abfe042011-11-28 00:51:27 +0000171 if (prefix && prefix[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172 PutCString (prefix);
173 Address (lo_addr, addr_size, "[");
174 Address (hi_addr, addr_size, "-", ")");
Greg Clayton1abfe042011-11-28 00:51:27 +0000175 if (suffix && suffix[0])
176 PutCString (suffix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177}
178
179
180int
181Stream::PutChar (char ch)
182{
183 return Write (&ch, 1);
184}
185
186
187//------------------------------------------------------------------
188// Print some formatted output to the stream.
189//------------------------------------------------------------------
190int
191Stream::Printf (const char *format, ...)
192{
193 va_list args;
194 va_start (args, format);
195 size_t result = PrintfVarArg(format, args);
196 va_end (args);
197 return result;
198}
199
200//------------------------------------------------------------------
201// Print some formatted output to the stream.
202//------------------------------------------------------------------
203int
204Stream::PrintfVarArg (const char *format, va_list args)
205{
206 char str[1024];
207 va_list args_copy;
208
209 va_copy (args_copy, args);
210
211 int bytes_written = 0;
212 // Try and format our string into a fixed buffer first and see if it fits
Greg Claytonc982c762010-07-09 20:39:50 +0000213 size_t length = ::vsnprintf (str, sizeof(str), format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214 if (length < sizeof(str))
215 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 // Include the NULL termination byte for binary output
Greg Clayton73b472d2010-10-27 03:32:59 +0000217 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218 length += 1;
219 // The formatted string fit into our stack based buffer, so we can just
220 // append that to our packet
221 bytes_written = Write (str, length);
222 }
223 else
224 {
225 // Our stack buffer wasn't big enough to contain the entire formatted
226 // string, so lets let vasprintf create the string for us!
227 char *str_ptr = NULL;
228 length = ::vasprintf (&str_ptr, format, args_copy);
229 if (str_ptr)
230 {
231 // Include the NULL termination byte for binary output
Greg Clayton73b472d2010-10-27 03:32:59 +0000232 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 length += 1;
234 bytes_written = Write (str_ptr, length);
235 ::free (str_ptr);
236 }
237 }
238 va_end (args_copy);
239 return bytes_written;
240}
241
242//------------------------------------------------------------------
243// Print and End of Line character to the stream
244//------------------------------------------------------------------
245int
246Stream::EOL()
247{
248 return PutChar ('\n');
249}
250
251//------------------------------------------------------------------
252// Indent the current line using the current indentation level and
253// print an optional string following the idenatation spaces.
254//------------------------------------------------------------------
255int
256Stream::Indent(const char *s)
257{
258 return Printf ("%*.*s%s", m_indent_level, m_indent_level, "", s ? s : "");
259}
260
261//------------------------------------------------------------------
262// Stream a character "ch" out to this stream.
263//------------------------------------------------------------------
264Stream&
265Stream::operator<< (char ch)
266{
267 PutChar (ch);
268 return *this;
269}
270
271//------------------------------------------------------------------
272// Stream the NULL terminated C string out to this stream.
273//------------------------------------------------------------------
274Stream&
275Stream::operator<< (const char *s)
276{
277 Printf ("%s", s);
278 return *this;
279}
280
281//------------------------------------------------------------------
282// Stream the pointer value out to this stream.
283//------------------------------------------------------------------
284Stream&
285Stream::operator<< (void *p)
286{
287 Printf ("0x%.*tx", (int)sizeof(void*) * 2, (ptrdiff_t)p);
288 return *this;
289}
290
291//------------------------------------------------------------------
292// Stream a uint8_t "uval" out to this stream.
293//------------------------------------------------------------------
294Stream&
295Stream::operator<< (uint8_t uval)
296{
297 PutHex8(uval);
298 return *this;
299}
300
301//------------------------------------------------------------------
302// Stream a uint16_t "uval" out to this stream.
303//------------------------------------------------------------------
304Stream&
305Stream::operator<< (uint16_t uval)
306{
307 PutHex16(uval, m_byte_order);
308 return *this;
309}
310
311//------------------------------------------------------------------
312// Stream a uint32_t "uval" out to this stream.
313//------------------------------------------------------------------
314Stream&
315Stream::operator<< (uint32_t uval)
316{
317 PutHex32(uval, m_byte_order);
318 return *this;
319}
320
321//------------------------------------------------------------------
322// Stream a uint64_t "uval" out to this stream.
323//------------------------------------------------------------------
324Stream&
325Stream::operator<< (uint64_t uval)
326{
327 PutHex64(uval, m_byte_order);
328 return *this;
329}
330
331//------------------------------------------------------------------
332// Stream a int8_t "sval" out to this stream.
333//------------------------------------------------------------------
334Stream&
335Stream::operator<< (int8_t sval)
336{
337 Printf ("%i", (int)sval);
338 return *this;
339}
340
341//------------------------------------------------------------------
342// Stream a int16_t "sval" out to this stream.
343//------------------------------------------------------------------
344Stream&
345Stream::operator<< (int16_t sval)
346{
347 Printf ("%i", (int)sval);
348 return *this;
349}
350
351//------------------------------------------------------------------
352// Stream a int32_t "sval" out to this stream.
353//------------------------------------------------------------------
354Stream&
355Stream::operator<< (int32_t sval)
356{
357 Printf ("%i", (int)sval);
358 return *this;
359}
360
361//------------------------------------------------------------------
362// Stream a int64_t "sval" out to this stream.
363//------------------------------------------------------------------
364Stream&
365Stream::operator<< (int64_t sval)
366{
367 Printf ("%lli", sval);
368 return *this;
369}
370
371//------------------------------------------------------------------
372// Get the current indentation level
373//------------------------------------------------------------------
374int
375Stream::GetIndentLevel() const
376{
377 return m_indent_level;
378}
379
380//------------------------------------------------------------------
381// Set the current indentation level
382//------------------------------------------------------------------
383void
384Stream::SetIndentLevel(int indent_level)
385{
386 m_indent_level = indent_level;
387}
388
389//------------------------------------------------------------------
390// Increment the current indentation level
391//------------------------------------------------------------------
392void
393Stream::IndentMore(int amount)
394{
395 m_indent_level += amount;
396}
397
398//------------------------------------------------------------------
399// Decrement the current indentation level
400//------------------------------------------------------------------
401void
402Stream::IndentLess (int amount)
403{
404 if (m_indent_level >= amount)
405 m_indent_level -= amount;
406 else
407 m_indent_level = 0;
408}
409
410//------------------------------------------------------------------
411// Get the address size in bytes
412//------------------------------------------------------------------
413uint8_t
414Stream::GetAddressByteSize() const
415{
416 return m_addr_size;
417}
418
419//------------------------------------------------------------------
420// Set the address size in bytes
421//------------------------------------------------------------------
422void
423Stream::SetAddressByteSize(uint8_t addr_size)
424{
425 m_addr_size = addr_size;
426}
427
428//------------------------------------------------------------------
429// Returns true if the verbose flag bit is set in this stream.
430//------------------------------------------------------------------
431bool
432Stream::GetVerbose() const
433{
Greg Clayton73b472d2010-10-27 03:32:59 +0000434 return m_flags.Test(eVerbose);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435}
436
437//------------------------------------------------------------------
438// Returns true if the debug flag bit is set in this stream.
439//------------------------------------------------------------------
440bool
441Stream::GetDebug() const
442{
Greg Clayton73b472d2010-10-27 03:32:59 +0000443 return m_flags.Test(eDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
446//------------------------------------------------------------------
447// The flags get accessor
448//------------------------------------------------------------------
449Flags&
450Stream::GetFlags()
451{
452 return m_flags;
453}
454
455//------------------------------------------------------------------
456// The flags const get accessor
457//------------------------------------------------------------------
458const Flags&
459Stream::GetFlags() const
460{
461 return m_flags;
462}
463
Sean Callanan609f8c52010-07-02 02:43:42 +0000464//------------------------------------------------------------------
465// The byte order get accessor
466//------------------------------------------------------------------
467
468lldb::ByteOrder
469Stream::GetByteOrder() const
470{
471 return m_byte_order;
472}
473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474int
475Stream::PrintfAsRawHex8 (const char *format, ...)
476{
477 va_list args;
478 va_list args_copy;
479 va_start (args, format);
480 va_copy (args, args_copy); // Copy this so we
481
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 char str[1024];
483 int bytes_written = 0;
484 // Try and format our string into a fixed buffer first and see if it fits
Greg Claytonc982c762010-07-09 20:39:50 +0000485 size_t length = ::vsnprintf (str, sizeof(str), format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 if (length < sizeof(str))
487 {
488 // The formatted string fit into our stack based buffer, so we can just
489 // append that to our packet
Greg Claytonc982c762010-07-09 20:39:50 +0000490 for (size_t i=0; i<length; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 bytes_written += _PutHex8 (str[i], false);
492 }
493 else
494 {
495 // Our stack buffer wasn't big enough to contain the entire formatted
496 // string, so lets let vasprintf create the string for us!
497 char *str_ptr = NULL;
498 length = ::vasprintf (&str_ptr, format, args_copy);
499 if (str_ptr)
500 {
Greg Claytonc982c762010-07-09 20:39:50 +0000501 for (size_t i=0; i<length; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 bytes_written += _PutHex8 (str_ptr[i], false);
503 ::free (str_ptr);
504 }
505 }
506 va_end (args);
507 va_end (args_copy);
508
509 return bytes_written;
510}
511
512int
513Stream::PutNHex8 (size_t n, uint8_t uvalue)
514{
515 int bytes_written = 0;
Greg Claytonc982c762010-07-09 20:39:50 +0000516 for (size_t i=0; i<n; ++i)
Greg Clayton73b472d2010-10-27 03:32:59 +0000517 bytes_written += _PutHex8 (uvalue, m_flags.Test(eAddPrefix));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 return bytes_written;
519}
520
521int
522Stream::_PutHex8 (uint8_t uvalue, bool add_prefix)
523{
524 int bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +0000525 if (m_flags.Test(eBinary))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 {
527 bytes_written = Write (&uvalue, 1);
528 }
529 else
530 {
531 if (add_prefix)
532 PutCString("0x");
533
534 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' };
535 char nibble_chars[2];
536 nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
537 nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
538 bytes_written = Write (nibble_chars, sizeof(nibble_chars));
539 }
540 return bytes_written;
541}
542
543int
544Stream::PutHex8 (uint8_t uvalue)
545{
Greg Clayton73b472d2010-10-27 03:32:59 +0000546 return _PutHex8 (uvalue, m_flags.Test(eAddPrefix));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547}
548
549int
550Stream::PutHex16 (uint16_t uvalue, ByteOrder byte_order)
551{
552 if (byte_order == eByteOrderInvalid)
553 byte_order = m_byte_order;
554
Greg Clayton73b472d2010-10-27 03:32:59 +0000555 bool add_prefix = m_flags.Test(eAddPrefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 int bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 if (byte_order == eByteOrderLittle)
558 {
Greg Claytonc982c762010-07-09 20:39:50 +0000559 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 bytes_written += _PutHex8 (uvalue >> (byte * 8), add_prefix);
561 }
562 else
563 {
Greg Claytonc982c762010-07-09 20:39:50 +0000564 for (size_t byte = sizeof(uvalue)-1; byte < sizeof(uvalue); --byte, add_prefix = false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 bytes_written += _PutHex8 (uvalue >> (byte * 8), add_prefix);
566 }
567 return bytes_written;
568}
569
570int
571Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order)
572{
573 if (byte_order == eByteOrderInvalid)
574 byte_order = m_byte_order;
575
Greg Clayton73b472d2010-10-27 03:32:59 +0000576 bool add_prefix = m_flags.Test(eAddPrefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 int bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 if (byte_order == eByteOrderLittle)
579 {
Greg Claytonc982c762010-07-09 20:39:50 +0000580 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 bytes_written += _PutHex8 (uvalue >> (byte * 8), add_prefix);
582 }
583 else
584 {
Greg Claytonc982c762010-07-09 20:39:50 +0000585 for (size_t byte = sizeof(uvalue)-1; byte < sizeof(uvalue); --byte, add_prefix = false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 bytes_written += _PutHex8 (uvalue >> (byte * 8), add_prefix);
587 }
588 return bytes_written;
589}
590
591int
592Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order)
593{
594 if (byte_order == eByteOrderInvalid)
595 byte_order = m_byte_order;
596
Greg Clayton73b472d2010-10-27 03:32:59 +0000597 bool add_prefix = m_flags.Test(eAddPrefix);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 int bytes_written = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 if (byte_order == eByteOrderLittle)
600 {
Greg Claytonc982c762010-07-09 20:39:50 +0000601 for (size_t byte = 0; byte < sizeof(uvalue); ++byte, add_prefix = false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 bytes_written += _PutHex8 (uvalue >> (byte * 8), add_prefix);
603 }
604 else
605 {
Greg Claytonc982c762010-07-09 20:39:50 +0000606 for (size_t byte = sizeof(uvalue)-1; byte < sizeof(uvalue); --byte, add_prefix = false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 bytes_written += _PutHex8 (uvalue >> (byte * 8), add_prefix);
608 }
609 return bytes_written;
610}
611
612int
613Stream::PutMaxHex64
614(
615 uint64_t uvalue,
616 size_t byte_size,
617 lldb::ByteOrder byte_order
618)
619{
620 switch (byte_size)
621 {
622 case 1: return PutHex8 (uvalue);
623 case 2: return PutHex16 (uvalue);
624 case 4: return PutHex32 (uvalue);
625 case 8: return PutHex64 (uvalue);
626 }
627 return 0;
628}
629
630int
631Stream::PutPointer (void *ptr)
632{
Greg Clayton7fb56d02011-02-01 01:31:41 +0000633 return PutRawBytes (&ptr, sizeof(ptr), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634}
635
636int
637Stream::PutFloat(float f, ByteOrder byte_order)
638{
639 if (byte_order == eByteOrderInvalid)
640 byte_order = m_byte_order;
641
Greg Clayton7fb56d02011-02-01 01:31:41 +0000642 return PutRawBytes (&f, sizeof(f), lldb::endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643}
644
645int
646Stream::PutDouble(double d, ByteOrder byte_order)
647{
648 if (byte_order == eByteOrderInvalid)
649 byte_order = m_byte_order;
650
Greg Clayton7fb56d02011-02-01 01:31:41 +0000651 return PutRawBytes (&d, sizeof(d), lldb::endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652}
653
654int
655Stream::PutLongDouble(long double ld, ByteOrder byte_order)
656{
657 if (byte_order == eByteOrderInvalid)
658 byte_order = m_byte_order;
659
Greg Clayton7fb56d02011-02-01 01:31:41 +0000660 return PutRawBytes (&ld, sizeof(ld), lldb::endian::InlHostByteOrder(), byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661}
662
663int
664Stream::PutRawBytes (const void *s, size_t src_len, ByteOrder src_byte_order, ByteOrder dst_byte_order)
665{
666 if (src_byte_order == eByteOrderInvalid)
667 src_byte_order = m_byte_order;
668
669 if (dst_byte_order == eByteOrderInvalid)
670 dst_byte_order = m_byte_order;
671
672 int bytes_written = 0;
673 const uint8_t *src = (const uint8_t *)s;
Greg Clayton73b472d2010-10-27 03:32:59 +0000674 bool binary_was_set = m_flags.Test (eBinary);
675 if (!binary_was_set)
676 m_flags.Set (eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 if (src_byte_order == dst_byte_order)
678 {
Greg Claytonc982c762010-07-09 20:39:50 +0000679 for (size_t i = 0; i < src_len; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 bytes_written += _PutHex8 (src[i], false);
681 }
682 else
683 {
Greg Claytonc982c762010-07-09 20:39:50 +0000684 for (size_t i = src_len-1; i < src_len; --i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 bytes_written += _PutHex8 (src[i], false);
686 }
Greg Clayton73b472d2010-10-27 03:32:59 +0000687 if (!binary_was_set)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 m_flags.Clear (eBinary);
689
690 return bytes_written;
691}
692
693int
694Stream::PutBytesAsRawHex8 (const void *s, size_t src_len, ByteOrder src_byte_order, ByteOrder dst_byte_order)
695{
696 if (src_byte_order == eByteOrderInvalid)
697 src_byte_order = m_byte_order;
698
699 if (dst_byte_order == eByteOrderInvalid)
700 dst_byte_order = m_byte_order;
701
702 int bytes_written = 0;
703 const uint8_t *src = (const uint8_t *)s;
Greg Clayton73b472d2010-10-27 03:32:59 +0000704 bool binary_is_set = m_flags.Test(eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 m_flags.Clear(eBinary);
706 if (src_byte_order == dst_byte_order)
707 {
Greg Claytonc982c762010-07-09 20:39:50 +0000708 for (size_t i = 0; i < src_len; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 bytes_written += _PutHex8 (src[i], false);
710 }
711 else
712 {
Greg Claytonc982c762010-07-09 20:39:50 +0000713 for (size_t i = src_len-1; i < src_len; --i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 bytes_written += _PutHex8 (src[i], false);
715 }
716 if (binary_is_set)
717 m_flags.Set(eBinary);
718
719 return bytes_written;
720}
721
722int
723Stream::PutCStringAsRawHex8 (const char *s)
724{
725 int bytes_written = 0;
Greg Clayton73b472d2010-10-27 03:32:59 +0000726 bool binary_is_set = m_flags.Test(eBinary);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 m_flags.Clear(eBinary);
728 do
729 {
730 bytes_written += _PutHex8 (*s, false);
731 ++s;
732 } while (*s);
733 if (binary_is_set)
734 m_flags.Set(eBinary);
735 return bytes_written;
736}
737
738void
739Stream::UnitTest(Stream *s)
740{
741 s->PutHex8(0x12);
742
743 s->PutChar(' ');
Greg Clayton7fb56d02011-02-01 01:31:41 +0000744 s->PutHex16(0x3456, lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 s->PutChar(' ');
746 s->PutHex16(0x3456, eByteOrderBig);
747 s->PutChar(' ');
748 s->PutHex16(0x3456, eByteOrderLittle);
749
750 s->PutChar(' ');
Greg Clayton7fb56d02011-02-01 01:31:41 +0000751 s->PutHex32(0x789abcde, lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 s->PutChar(' ');
753 s->PutHex32(0x789abcde, eByteOrderBig);
754 s->PutChar(' ');
755 s->PutHex32(0x789abcde, eByteOrderLittle);
756
757 s->PutChar(' ');
Greg Clayton7fb56d02011-02-01 01:31:41 +0000758 s->PutHex64(0x1122334455667788ull, lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 s->PutChar(' ');
760 s->PutHex64(0x1122334455667788ull, eByteOrderBig);
761 s->PutChar(' ');
762 s->PutHex64(0x1122334455667788ull, eByteOrderLittle);
763
764 const char *hola = "Hello World!!!";
765 s->PutChar(' ');
766 s->PutCString (hola);
767
768 s->PutChar(' ');
769 s->Write (hola, 5);
770
771 s->PutChar(' ');
772 s->PutCStringAsRawHex8 (hola);
773
774 s->PutChar(' ');
775 s->PutCStringAsRawHex8 ("01234");
776
777 s->PutChar(' ');
778 s->Printf ("pid=%i", 12733);
779
780 s->PutChar(' ');
781 s->PrintfAsRawHex8 ("pid=%i", 12733);
782 s->PutChar('\n');
783}
784