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