1 //===-- Stream.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/Stream.h"
12 #include "lldb/Utility/Endian.h"
13 #include "lldb/Utility/VASPrintf.h"
14 #include "llvm/ADT/SmallString.h" // for SmallString
22 using namespace lldb_private;
24 Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
25 : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order),
29 : m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
32 //------------------------------------------------------------------
34 //------------------------------------------------------------------
37 ByteOrder Stream::SetByteOrder(ByteOrder byte_order) {
38 ByteOrder old_byte_order = m_byte_order;
39 m_byte_order = byte_order;
40 return old_byte_order;
43 //------------------------------------------------------------------
44 // Put an offset "uval" out to the stream using the printf format
46 //------------------------------------------------------------------
47 void Stream::Offset(uint32_t uval, const char *format) { Printf(format, uval); }
49 //------------------------------------------------------------------
50 // Put an SLEB128 "uval" out to the stream using the printf format
52 //------------------------------------------------------------------
53 size_t Stream::PutSLEB128(int64_t sval) {
54 size_t bytes_written = 0;
55 if (m_flags.Test(eBinary)) {
58 uint8_t byte = sval & 0x7fu;
60 /* sign bit of byte is 2nd high order bit (0x40) */
61 if ((sval == 0 && !(byte & 0x40)) || (sval == -1 && (byte & 0x40)))
66 bytes_written += Write(&byte, 1);
69 bytes_written = Printf("0x%" PRIi64, sval);
75 //------------------------------------------------------------------
76 // Put an ULEB128 "uval" out to the stream using the printf format
78 //------------------------------------------------------------------
79 size_t Stream::PutULEB128(uint64_t uval) {
80 size_t bytes_written = 0;
81 if (m_flags.Test(eBinary)) {
84 uint8_t byte = uval & 0x7fu;
90 bytes_written += Write(&byte, 1);
93 bytes_written = Printf("0x%" PRIx64, uval);
98 //------------------------------------------------------------------
99 // Print a raw NULL terminated C string to the stream.
100 //------------------------------------------------------------------
101 size_t Stream::PutCString(llvm::StringRef str) {
102 size_t bytes_written = 0;
103 bytes_written = Write(str.data(), str.size());
105 // when in binary mode, emit the NULL terminator
106 if (m_flags.Test(eBinary))
107 bytes_written += PutChar('\0');
108 return bytes_written;
111 //------------------------------------------------------------------
112 // Print a double quoted NULL terminated C string to the stream
113 // using the printf format in "format".
114 //------------------------------------------------------------------
115 void Stream::QuotedCString(const char *cstr, const char *format) {
116 Printf(format, cstr);
119 //------------------------------------------------------------------
120 // Put an address "addr" out to the stream with optional prefix
121 // and suffix strings.
122 //------------------------------------------------------------------
123 void Stream::Address(uint64_t addr, uint32_t addr_size, const char *prefix,
124 const char *suffix) {
129 // int addr_width = m_addr_size << 1;
130 // Printf ("%s0x%0*" PRIx64 "%s", prefix, addr_width, addr, suffix);
131 Printf("%s0x%0*" PRIx64 "%s", prefix, addr_size * 2, (uint64_t)addr, suffix);
134 //------------------------------------------------------------------
135 // Put an address range out to the stream with optional prefix
136 // and suffix strings.
137 //------------------------------------------------------------------
138 void Stream::AddressRange(uint64_t lo_addr, uint64_t hi_addr,
139 uint32_t addr_size, const char *prefix,
140 const char *suffix) {
141 if (prefix && prefix[0])
143 Address(lo_addr, addr_size, "[");
144 Address(hi_addr, addr_size, "-", ")");
145 if (suffix && suffix[0])
149 size_t Stream::PutChar(char ch) { return Write(&ch, 1); }
151 //------------------------------------------------------------------
152 // Print some formatted output to the stream.
153 //------------------------------------------------------------------
154 size_t Stream::Printf(const char *format, ...) {
156 va_start(args, format);
157 size_t result = PrintfVarArg(format, args);
162 //------------------------------------------------------------------
163 // Print some formatted output to the stream.
164 //------------------------------------------------------------------
165 size_t Stream::PrintfVarArg(const char *format, va_list args) {
166 llvm::SmallString<1024> buf;
167 VASprintf(buf, format, args);
169 // Include the NULL termination byte for binary output
170 size_t length = buf.size();
171 if (m_flags.Test(eBinary))
173 return Write(buf.c_str(), length);
176 //------------------------------------------------------------------
177 // Print and End of Line character to the stream
178 //------------------------------------------------------------------
179 size_t Stream::EOL() { return PutChar('\n'); }
181 //------------------------------------------------------------------
182 // Indent the current line using the current indentation level and
183 // print an optional string following the indentation spaces.
184 //------------------------------------------------------------------
185 size_t Stream::Indent(const char *s) {
186 return Printf("%*.*s%s", m_indent_level, m_indent_level, "", s ? s : "");
189 size_t Stream::Indent(llvm::StringRef str) {
190 return Printf("%*.*s%s", m_indent_level, m_indent_level, "",
194 //------------------------------------------------------------------
195 // Stream a character "ch" out to this stream.
196 //------------------------------------------------------------------
197 Stream &Stream::operator<<(char ch) {
202 //------------------------------------------------------------------
203 // Stream the NULL terminated C string out to this stream.
204 //------------------------------------------------------------------
205 Stream &Stream::operator<<(const char *s) {
210 Stream &Stream::operator<<(llvm::StringRef str) {
211 Write(str.data(), str.size());
215 //------------------------------------------------------------------
216 // Stream the pointer value out to this stream.
217 //------------------------------------------------------------------
218 Stream &Stream::operator<<(const void *p) {
219 Printf("0x%.*tx", (int)sizeof(const void *) * 2, (ptrdiff_t)p);
223 //------------------------------------------------------------------
224 // Stream a uint8_t "uval" out to this stream.
225 //------------------------------------------------------------------
226 Stream &Stream::operator<<(uint8_t uval) {
231 //------------------------------------------------------------------
232 // Stream a uint16_t "uval" out to this stream.
233 //------------------------------------------------------------------
234 Stream &Stream::operator<<(uint16_t uval) {
235 PutHex16(uval, m_byte_order);
239 //------------------------------------------------------------------
240 // Stream a uint32_t "uval" out to this stream.
241 //------------------------------------------------------------------
242 Stream &Stream::operator<<(uint32_t uval) {
243 PutHex32(uval, m_byte_order);
247 //------------------------------------------------------------------
248 // Stream a uint64_t "uval" out to this stream.
249 //------------------------------------------------------------------
250 Stream &Stream::operator<<(uint64_t uval) {
251 PutHex64(uval, m_byte_order);
255 //------------------------------------------------------------------
256 // Stream a int8_t "sval" out to this stream.
257 //------------------------------------------------------------------
258 Stream &Stream::operator<<(int8_t sval) {
259 Printf("%i", (int)sval);
263 //------------------------------------------------------------------
264 // Stream a int16_t "sval" out to this stream.
265 //------------------------------------------------------------------
266 Stream &Stream::operator<<(int16_t sval) {
267 Printf("%i", (int)sval);
271 //------------------------------------------------------------------
272 // Stream a int32_t "sval" out to this stream.
273 //------------------------------------------------------------------
274 Stream &Stream::operator<<(int32_t sval) {
275 Printf("%i", (int)sval);
279 //------------------------------------------------------------------
280 // Stream a int64_t "sval" out to this stream.
281 //------------------------------------------------------------------
282 Stream &Stream::operator<<(int64_t sval) {
283 Printf("%" PRIi64, sval);
287 //------------------------------------------------------------------
288 // Get the current indentation level
289 //------------------------------------------------------------------
290 int Stream::GetIndentLevel() const { return m_indent_level; }
292 //------------------------------------------------------------------
293 // Set the current indentation level
294 //------------------------------------------------------------------
295 void Stream::SetIndentLevel(int indent_level) { m_indent_level = indent_level; }
297 //------------------------------------------------------------------
298 // Increment the current indentation level
299 //------------------------------------------------------------------
300 void Stream::IndentMore(int amount) { m_indent_level += amount; }
302 //------------------------------------------------------------------
303 // Decrement the current indentation level
304 //------------------------------------------------------------------
305 void Stream::IndentLess(int amount) {
306 if (m_indent_level >= amount)
307 m_indent_level -= amount;
312 //------------------------------------------------------------------
313 // Get the address size in bytes
314 //------------------------------------------------------------------
315 uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }
317 //------------------------------------------------------------------
318 // Set the address size in bytes
319 //------------------------------------------------------------------
320 void Stream::SetAddressByteSize(uint32_t addr_size) { m_addr_size = addr_size; }
322 //------------------------------------------------------------------
323 // The flags get accessor
324 //------------------------------------------------------------------
325 Flags &Stream::GetFlags() { return m_flags; }
327 //------------------------------------------------------------------
328 // The flags const get accessor
329 //------------------------------------------------------------------
330 const Flags &Stream::GetFlags() const { return m_flags; }
332 //------------------------------------------------------------------
333 // The byte order get accessor
334 //------------------------------------------------------------------
336 lldb::ByteOrder Stream::GetByteOrder() const { return m_byte_order; }
338 size_t Stream::PrintfAsRawHex8(const char *format, ...) {
340 va_start(args, format);
342 llvm::SmallString<1024> buf;
343 VASprintf(buf, format, args);
347 length += _PutHex8(C, false);
354 size_t Stream::PutNHex8(size_t n, uint8_t uvalue) {
355 size_t bytes_written = 0;
356 for (size_t i = 0; i < n; ++i)
357 bytes_written += _PutHex8(uvalue, false);
358 return bytes_written;
361 size_t Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {
362 size_t bytes_written = 0;
363 if (m_flags.Test(eBinary)) {
364 bytes_written = Write(&uvalue, 1);
369 static char g_hex_to_ascii_hex_char[16] = {'0', '1', '2', '3', '4', '5',
370 '6', '7', '8', '9', 'a', 'b',
372 char nibble_chars[2];
373 nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
374 nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
375 bytes_written = Write(nibble_chars, sizeof(nibble_chars));
377 return bytes_written;
380 size_t Stream::PutHex8(uint8_t uvalue) { return _PutHex8(uvalue, false); }
382 size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) {
383 if (byte_order == eByteOrderInvalid)
384 byte_order = m_byte_order;
386 size_t bytes_written = 0;
387 if (byte_order == eByteOrderLittle) {
388 for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
389 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
391 for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
392 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
394 return bytes_written;
397 size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) {
398 if (byte_order == eByteOrderInvalid)
399 byte_order = m_byte_order;
401 size_t bytes_written = 0;
402 if (byte_order == eByteOrderLittle) {
403 for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
404 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
406 for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
407 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
409 return bytes_written;
412 size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) {
413 if (byte_order == eByteOrderInvalid)
414 byte_order = m_byte_order;
416 size_t bytes_written = 0;
417 if (byte_order == eByteOrderLittle) {
418 for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
419 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
421 for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
422 bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
424 return bytes_written;
427 size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size,
428 lldb::ByteOrder byte_order) {
431 return PutHex8((uint8_t)uvalue);
433 return PutHex16((uint16_t)uvalue);
435 return PutHex32((uint32_t)uvalue);
437 return PutHex64(uvalue);
442 size_t Stream::PutPointer(void *ptr) {
443 return PutRawBytes(&ptr, sizeof(ptr), endian::InlHostByteOrder(),
444 endian::InlHostByteOrder());
447 size_t Stream::PutFloat(float f, ByteOrder byte_order) {
448 if (byte_order == eByteOrderInvalid)
449 byte_order = m_byte_order;
451 return PutRawBytes(&f, sizeof(f), endian::InlHostByteOrder(), byte_order);
454 size_t Stream::PutDouble(double d, ByteOrder byte_order) {
455 if (byte_order == eByteOrderInvalid)
456 byte_order = m_byte_order;
458 return PutRawBytes(&d, sizeof(d), endian::InlHostByteOrder(), byte_order);
461 size_t Stream::PutLongDouble(long double ld, ByteOrder byte_order) {
462 if (byte_order == eByteOrderInvalid)
463 byte_order = m_byte_order;
465 return PutRawBytes(&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order);
468 size_t Stream::PutRawBytes(const void *s, size_t src_len,
469 ByteOrder src_byte_order, ByteOrder dst_byte_order) {
470 if (src_byte_order == eByteOrderInvalid)
471 src_byte_order = m_byte_order;
473 if (dst_byte_order == eByteOrderInvalid)
474 dst_byte_order = m_byte_order;
476 size_t bytes_written = 0;
477 const uint8_t *src = (const uint8_t *)s;
478 bool binary_was_set = m_flags.Test(eBinary);
480 m_flags.Set(eBinary);
481 if (src_byte_order == dst_byte_order) {
482 for (size_t i = 0; i < src_len; ++i)
483 bytes_written += _PutHex8(src[i], false);
485 for (size_t i = src_len - 1; i < src_len; --i)
486 bytes_written += _PutHex8(src[i], false);
489 m_flags.Clear(eBinary);
491 return bytes_written;
494 size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len,
495 ByteOrder src_byte_order,
496 ByteOrder dst_byte_order) {
497 if (src_byte_order == eByteOrderInvalid)
498 src_byte_order = m_byte_order;
500 if (dst_byte_order == eByteOrderInvalid)
501 dst_byte_order = m_byte_order;
503 size_t bytes_written = 0;
504 const uint8_t *src = (const uint8_t *)s;
505 bool binary_is_set = m_flags.Test(eBinary);
506 m_flags.Clear(eBinary);
507 if (src_byte_order == dst_byte_order) {
508 for (size_t i = 0; i < src_len; ++i)
509 bytes_written += _PutHex8(src[i], false);
511 for (size_t i = src_len - 1; i < src_len; --i)
512 bytes_written += _PutHex8(src[i], false);
515 m_flags.Set(eBinary);
517 return bytes_written;
520 size_t Stream::PutCStringAsRawHex8(const char *s) {
521 size_t bytes_written = 0;
522 bool binary_is_set = m_flags.Test(eBinary);
523 m_flags.Clear(eBinary);
525 bytes_written += _PutHex8(*s, false);
529 m_flags.Set(eBinary);
530 return bytes_written;
533 void Stream::UnitTest(Stream *s) {
537 s->PutHex16(0x3456, endian::InlHostByteOrder());
539 s->PutHex16(0x3456, eByteOrderBig);
541 s->PutHex16(0x3456, eByteOrderLittle);
544 s->PutHex32(0x789abcde, endian::InlHostByteOrder());
546 s->PutHex32(0x789abcde, eByteOrderBig);
548 s->PutHex32(0x789abcde, eByteOrderLittle);
551 s->PutHex64(0x1122334455667788ull, endian::InlHostByteOrder());
553 s->PutHex64(0x1122334455667788ull, eByteOrderBig);
555 s->PutHex64(0x1122334455667788ull, eByteOrderLittle);
557 const char *hola = "Hello World!!!";
565 s->PutCStringAsRawHex8(hola);
568 s->PutCStringAsRawHex8("01234");
571 s->Printf("pid=%i", 12733);
574 s->PrintfAsRawHex8("pid=%i", 12733);