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