LLDB  mainline
Stream.cpp
Go to the documentation of this file.
1 //===-- Stream.cpp ----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/Stream.h"
10 
11 #include "lldb/Utility/Endian.h"
12 #include "lldb/Utility/VASPrintf.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/LEB128.h"
15 
16 #include <string>
17 
18 #include <inttypes.h>
19 #include <stddef.h>
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
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),
26  m_indent_level(0), m_forwarder(*this) {}
27 
29  : m_flags(0), m_addr_size(4), m_byte_order(endian::InlHostByteOrder()),
30  m_indent_level(0), m_forwarder(*this) {}
31 
32 // Destructor
34 
36  ByteOrder old_byte_order = m_byte_order;
37  m_byte_order = byte_order;
38  return old_byte_order;
39 }
40 
41 // Put an offset "uval" out to the stream using the printf format in "format".
42 void Stream::Offset(uint32_t uval, const char *format) { Printf(format, uval); }
43 
44 // Put an SLEB128 "uval" out to the stream using the printf format in "format".
45 size_t Stream::PutSLEB128(int64_t sval) {
46  if (m_flags.Test(eBinary))
47  return llvm::encodeSLEB128(sval, m_forwarder);
48  else
49  return Printf("0x%" PRIi64, sval);
50 }
51 
52 // Put an ULEB128 "uval" out to the stream using the printf format in "format".
53 size_t Stream::PutULEB128(uint64_t uval) {
54  if (m_flags.Test(eBinary))
55  return llvm::encodeULEB128(uval, m_forwarder);
56  else
57  return Printf("0x%" PRIx64, uval);
58 }
59 
60 // Print a raw NULL terminated C string to the stream.
61 size_t Stream::PutCString(llvm::StringRef str) {
62  size_t bytes_written = 0;
63  bytes_written = Write(str.data(), str.size());
64 
65  // when in binary mode, emit the NULL terminator
66  if (m_flags.Test(eBinary))
67  bytes_written += PutChar('\0');
68  return bytes_written;
69 }
70 
71 // Print a double quoted NULL terminated C string to the stream using the
72 // printf format in "format".
73 void Stream::QuotedCString(const char *cstr, const char *format) {
74  Printf(format, cstr);
75 }
76 
77 // Put an address "addr" out to the stream with optional prefix and suffix
78 // strings.
79 void Stream::Address(uint64_t addr, uint32_t addr_size, const char *prefix,
80  const char *suffix) {
81  if (prefix == nullptr)
82  prefix = "";
83  if (suffix == nullptr)
84  suffix = "";
85  // int addr_width = m_addr_size << 1;
86  // Printf ("%s0x%0*" PRIx64 "%s", prefix, addr_width, addr, suffix);
87  Printf("%s0x%0*" PRIx64 "%s", prefix, addr_size * 2, (uint64_t)addr, suffix);
88 }
89 
90 // Put an address range out to the stream with optional prefix and suffix
91 // strings.
92 void Stream::AddressRange(uint64_t lo_addr, uint64_t hi_addr,
93  uint32_t addr_size, const char *prefix,
94  const char *suffix) {
95  if (prefix && prefix[0])
96  PutCString(prefix);
97  Address(lo_addr, addr_size, "[");
98  Address(hi_addr, addr_size, "-", ")");
99  if (suffix && suffix[0])
100  PutCString(suffix);
101 }
102 
103 size_t Stream::PutChar(char ch) { return Write(&ch, 1); }
104 
105 // Print some formatted output to the stream.
106 size_t Stream::Printf(const char *format, ...) {
107  va_list args;
108  va_start(args, format);
109  size_t result = PrintfVarArg(format, args);
110  va_end(args);
111  return result;
112 }
113 
114 // Print some formatted output to the stream.
115 size_t Stream::PrintfVarArg(const char *format, va_list args) {
116  llvm::SmallString<1024> buf;
117  VASprintf(buf, format, args);
118 
119  // Include the NULL termination byte for binary output
120  size_t length = buf.size();
121  if (m_flags.Test(eBinary))
122  ++length;
123  return Write(buf.c_str(), length);
124 }
125 
126 // Print and End of Line character to the stream
127 size_t Stream::EOL() { return PutChar('\n'); }
128 
129 // Indent the current line using the current indentation level and print an
130 // optional string following the indentation spaces.
131 size_t Stream::Indent(const char *s) {
132  return Printf("%*.*s%s", m_indent_level, m_indent_level, "", s ? s : "");
133 }
134 
135 size_t Stream::Indent(llvm::StringRef str) {
136  return Printf("%*.*s%s", m_indent_level, m_indent_level, "",
137  str.str().c_str());
138 }
139 
140 // Stream a character "ch" out to this stream.
142  PutChar(ch);
143  return *this;
144 }
145 
146 // Stream the NULL terminated C string out to this stream.
147 Stream &Stream::operator<<(const char *s) {
148  Printf("%s", s);
149  return *this;
150 }
151 
152 Stream &Stream::operator<<(llvm::StringRef str) {
153  Write(str.data(), str.size());
154  return *this;
155 }
156 
157 // Stream the pointer value out to this stream.
158 Stream &Stream::operator<<(const void *p) {
159  Printf("0x%.*tx", (int)sizeof(const void *) * 2, (ptrdiff_t)p);
160  return *this;
161 }
162 
163 // Stream a uint8_t "uval" out to this stream.
164 Stream &Stream::operator<<(uint8_t uval) {
165  PutHex8(uval);
166  return *this;
167 }
168 
169 // Stream a uint16_t "uval" out to this stream.
171  PutHex16(uval, m_byte_order);
172  return *this;
173 }
174 
175 // Stream a uint32_t "uval" out to this stream.
177  PutHex32(uval, m_byte_order);
178  return *this;
179 }
180 
181 // Stream a uint64_t "uval" out to this stream.
182 Stream &Stream::operator<<(uint64_t uval) {
183  PutHex64(uval, m_byte_order);
184  return *this;
185 }
186 
187 // Stream a int8_t "sval" out to this stream.
188 Stream &Stream::operator<<(int8_t sval) {
189  Printf("%i", (int)sval);
190  return *this;
191 }
192 
193 // Stream a int16_t "sval" out to this stream.
194 Stream &Stream::operator<<(int16_t sval) {
195  Printf("%i", (int)sval);
196  return *this;
197 }
198 
199 // Stream a int32_t "sval" out to this stream.
200 Stream &Stream::operator<<(int32_t sval) {
201  Printf("%i", (int)sval);
202  return *this;
203 }
204 
205 // Stream a int64_t "sval" out to this stream.
206 Stream &Stream::operator<<(int64_t sval) {
207  Printf("%" PRIi64, sval);
208  return *this;
209 }
210 
211 // Get the current indentation level
212 int Stream::GetIndentLevel() const { return m_indent_level; }
213 
214 // Set the current indentation level
215 void Stream::SetIndentLevel(int indent_level) { m_indent_level = indent_level; }
216 
217 // Increment the current indentation level
218 void Stream::IndentMore(int amount) { m_indent_level += amount; }
219 
220 // Decrement the current indentation level
221 void Stream::IndentLess(int amount) {
222  if (m_indent_level >= amount)
223  m_indent_level -= amount;
224  else
225  m_indent_level = 0;
226 }
227 
228 // Get the address size in bytes
230 
231 // Set the address size in bytes
232 void Stream::SetAddressByteSize(uint32_t addr_size) { m_addr_size = addr_size; }
233 
234 // The flags get accessor
236 
237 // The flags const get accessor
238 const Flags &Stream::GetFlags() const { return m_flags; }
239 
240 // The byte order get accessor
241 
243 
244 size_t Stream::PrintfAsRawHex8(const char *format, ...) {
245  va_list args;
246  va_start(args, format);
247 
248  llvm::SmallString<1024> buf;
249  VASprintf(buf, format, args);
250 
251  ByteDelta delta(*this);
252  for (char C : buf)
253  _PutHex8(C, false);
254 
255  va_end(args);
256 
257  return *delta;
258 }
259 
260 size_t Stream::PutNHex8(size_t n, uint8_t uvalue) {
261  ByteDelta delta(*this);
262  for (size_t i = 0; i < n; ++i)
263  _PutHex8(uvalue, false);
264  return *delta;
265 }
266 
267 void Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {
268  if (m_flags.Test(eBinary)) {
269  Write(&uvalue, 1);
270  } else {
271  if (add_prefix)
272  PutCString("0x");
273 
274  static char g_hex_to_ascii_hex_char[16] = {'0', '1', '2', '3', '4', '5',
275  '6', '7', '8', '9', 'a', 'b',
276  'c', 'd', 'e', 'f'};
277  char nibble_chars[2];
278  nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
279  nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
280  Write(nibble_chars, sizeof(nibble_chars));
281  }
282 }
283 
284 size_t Stream::PutHex8(uint8_t uvalue) {
285  ByteDelta delta(*this);
286  _PutHex8(uvalue, false);
287  return *delta;
288 }
289 
290 size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) {
291  ByteDelta delta(*this);
292 
293  if (byte_order == eByteOrderInvalid)
294  byte_order = m_byte_order;
295 
296  if (byte_order == eByteOrderLittle) {
297  for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
298  _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
299  } else {
300  for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
301  _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
302  }
303  return *delta;
304 }
305 
306 size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) {
307  ByteDelta delta(*this);
308 
309  if (byte_order == eByteOrderInvalid)
310  byte_order = m_byte_order;
311 
312  if (byte_order == eByteOrderLittle) {
313  for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
314  _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
315  } else {
316  for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
317  _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
318  }
319  return *delta;
320 }
321 
322 size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) {
323  ByteDelta delta(*this);
324 
325  if (byte_order == eByteOrderInvalid)
326  byte_order = m_byte_order;
327 
328  if (byte_order == eByteOrderLittle) {
329  for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
330  _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
331  } else {
332  for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
333  _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
334  }
335  return *delta;
336 }
337 
338 size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size,
339  lldb::ByteOrder byte_order) {
340  switch (byte_size) {
341  case 1:
342  return PutHex8((uint8_t)uvalue);
343  case 2:
344  return PutHex16((uint16_t)uvalue, byte_order);
345  case 4:
346  return PutHex32((uint32_t)uvalue, byte_order);
347  case 8:
348  return PutHex64(uvalue, byte_order);
349  }
350  return 0;
351 }
352 
353 size_t Stream::PutPointer(void *ptr) {
354  return PutRawBytes(&ptr, sizeof(ptr), endian::InlHostByteOrder(),
356 }
357 
358 size_t Stream::PutFloat(float f, ByteOrder byte_order) {
359  if (byte_order == eByteOrderInvalid)
360  byte_order = m_byte_order;
361 
362  return PutRawBytes(&f, sizeof(f), endian::InlHostByteOrder(), byte_order);
363 }
364 
365 size_t Stream::PutDouble(double d, ByteOrder byte_order) {
366  if (byte_order == eByteOrderInvalid)
367  byte_order = m_byte_order;
368 
369  return PutRawBytes(&d, sizeof(d), endian::InlHostByteOrder(), byte_order);
370 }
371 
372 size_t Stream::PutLongDouble(long double ld, ByteOrder byte_order) {
373  if (byte_order == eByteOrderInvalid)
374  byte_order = m_byte_order;
375 
376  return PutRawBytes(&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order);
377 }
378 
379 size_t Stream::PutRawBytes(const void *s, size_t src_len,
380  ByteOrder src_byte_order, ByteOrder dst_byte_order) {
381  ByteDelta delta(*this);
382 
383  if (src_byte_order == eByteOrderInvalid)
384  src_byte_order = m_byte_order;
385 
386  if (dst_byte_order == eByteOrderInvalid)
387  dst_byte_order = m_byte_order;
388 
389  const uint8_t *src = (const uint8_t *)s;
390  bool binary_was_set = m_flags.Test(eBinary);
391  if (!binary_was_set)
393  if (src_byte_order == dst_byte_order) {
394  for (size_t i = 0; i < src_len; ++i)
395  _PutHex8(src[i], false);
396  } else {
397  for (size_t i = src_len - 1; i < src_len; --i)
398  _PutHex8(src[i], false);
399  }
400  if (!binary_was_set)
402 
403  return *delta;
404 }
405 
406 size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len,
407  ByteOrder src_byte_order,
408  ByteOrder dst_byte_order) {
409  ByteDelta delta(*this);
410  if (src_byte_order == eByteOrderInvalid)
411  src_byte_order = m_byte_order;
412 
413  if (dst_byte_order == eByteOrderInvalid)
414  dst_byte_order = m_byte_order;
415 
416  const uint8_t *src = (const uint8_t *)s;
417  bool binary_is_set = m_flags.Test(eBinary);
419  if (src_byte_order == dst_byte_order) {
420  for (size_t i = 0; i < src_len; ++i)
421  _PutHex8(src[i], false);
422  } else {
423  for (size_t i = src_len - 1; i < src_len; --i)
424  _PutHex8(src[i], false);
425  }
426  if (binary_is_set)
428 
429  return *delta;
430 }
431 
432 size_t Stream::PutStringAsRawHex8(llvm::StringRef s) {
433  ByteDelta delta(*this);
434  bool binary_is_set = m_flags.Test(eBinary);
436  for (char c : s)
437  _PutHex8(c, false);
438  if (binary_is_set)
440  return *delta;
441 }
void Address(uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:79
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Stream & operator<<(const char *cstr)
Output a NULL terminated C string cstr to the stream s.
Definition: Stream.cpp:147
Get and put data as binary instead of as the default string mode.
Definition: Stream.h:32
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t m_addr_size
Size of an address in bytes.
Definition: Stream.h:477
size_t PutHex16(uint16_t uvalue, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:290
Flags & GetFlags()
The flags accessor.
Definition: Stream.cpp:235
void SetAddressByteSize(uint32_t addr_size)
Set the address size in bytes.
Definition: Stream.cpp:232
size_t PutDouble(double d, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:365
void SetIndentLevel(int level)
Set the current indentation level.
Definition: Stream.cpp:215
void AddressRange(uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address range to this stream.
Definition: Stream.cpp:92
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:100
size_t size_t PrintfVarArg(const char *format, va_list args)
Definition: Stream.cpp:115
size_t PutMaxHex64(uint64_t uvalue, size_t byte_size, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:338
virtual ~Stream()
Destructor.
Definition: Stream.cpp:33
lldb::ByteOrder GetByteOrder() const
Definition: Stream.cpp:242
ValueType Clear(ValueType mask=~(ValueType) 0)
Clear one or more flags.
Definition: Flags.h:72
size_t PutStringAsRawHex8(llvm::StringRef s)
Definition: Stream.cpp:432
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:107
int GetIndentLevel() const
Get the current indentation level.
Definition: Stream.cpp:212
Stream()
Construct a default Stream, not binary, host byte order and host addr size.
Definition: Stream.cpp:28
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
size_t PutHex32(uint32_t uvalue, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:306
void IndentLess(int amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:221
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
size_t PutHex64(uint64_t uvalue, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:322
int m_indent_level
Indention level.
Definition: Stream.h:480
size_t PutLongDouble(long double ld, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:372
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
Definition: VASprintf.cpp:19
ValueType Set(ValueType mask)
Set one or more flags by logical OR&#39;ing mask with the current flags.
Definition: Flags.h:84
lldb::ByteOrder m_byte_order
Byte order to use when encoding scalar types.
Definition: Stream.h:479
void QuotedCString(const char *cstr, const char *format="\s\)
Output a quoted C string value to the stream.
Definition: Stream.cpp:73
size_t PrintfAsRawHex8(const char *format,...) __attribute__((__format__(__printf__
Format a C string from a printf style format and variable arguments and encode and append the resulti...
Definition: Stream.cpp:244
size_t PutChar(char ch)
Definition: Stream.cpp:103
A class to manage flags.
Definition: Flags.h:22
size_t PutNHex8(size_t n, uint8_t uvalue)
Definition: Stream.cpp:260
RawOstreamForward m_forwarder
Definition: Stream.h:523
size_t size_t PutHex8(uint8_t uvalue)
Append an uint8_t value in the hexadecimal format to the stream.
Definition: Stream.cpp:284
Flags m_flags
Dump flags.
Definition: Stream.h:476
Definition: SBAddress.h:15
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
void Offset(uint32_t offset, const char *format="0x%8.8x: ")
Output an offset value.
Definition: Stream.cpp:42
uint32_t GetAddressByteSize() const
Get the address size in bytes.
Definition: Stream.cpp:229
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
size_t PutSLEB128(int64_t uval)
Output a SLEB128 number to the stream.
Definition: Stream.cpp:45
size_t PutULEB128(uint64_t uval)
Output a ULEB128 number to the stream.
Definition: Stream.cpp:53
void IndentMore(int amount=2)
Increment the current indentation level.
Definition: Stream.cpp:218
lldb::ByteOrder SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
Definition: Stream.cpp:35
size_t PutPointer(void *ptr)
Definition: Stream.cpp:353
size_t PutBytesAsRawHex8(const void *src, size_t src_len, lldb::ByteOrder src_byte_order=lldb::eByteOrderInvalid, lldb::ByteOrder dst_byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:406
size_t PutRawBytes(const void *s, size_t src_len, lldb::ByteOrder src_byte_order=lldb::eByteOrderInvalid, lldb::ByteOrder dst_byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:379
void _PutHex8(uint8_t uvalue, bool add_prefix)
Definition: Stream.cpp:267
size_t PutFloat(float f, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
Definition: Stream.cpp:358