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