LLDB mainline
Stream.h
Go to the documentation of this file.
1//===-- Stream.h ------------------------------------------------*- 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#ifndef LLDB_UTILITY_STREAM_H
10#define LLDB_UTILITY_STREAM_H
11
12#include "lldb/Utility/Flags.h"
13#include "lldb/lldb-defines.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/raw_ostream.h"
18
19#include <cstdarg>
20#include <cstddef>
21#include <cstdint>
22#include <type_traits>
23
24namespace lldb_private {
25
26/// \class Stream Stream.h "lldb/Utility/Stream.h"
27/// A stream class that can stream formatted output to a file.
28class Stream {
29public:
30 /// \a m_flags bit values.
31 enum {
32 eBinary = (1 << 0) ///< Get and put data as binary instead of as the default
33 /// string mode.
34 };
35
36 /// Struct to store information for color highlighting in the stream.
38 llvm::StringRef pattern; ///< Regex pattern for highlighting.
39 llvm::StringRef prefix; ///< ANSI color code to start colorization.
40 llvm::StringRef suffix; ///< ANSI color code to end colorization.
41
42 HighlightSettings(llvm::StringRef p, llvm::StringRef pre,
43 llvm::StringRef suf)
44 : pattern(p), prefix(pre), suffix(suf) {}
45 };
46
47 /// Utility class for counting the bytes that were written to a stream in a
48 /// certain time span.
49 ///
50 /// \example
51 /// ByteDelta delta(*this);
52 /// WriteDataToStream("foo");
53 /// return *delta;
54 class ByteDelta {
56 /// Bytes we have written so far when ByteDelta was created.
57 size_t m_start;
58
59 public:
61 /// Returns the number of bytes written to the given Stream since this
62 /// ByteDelta object was created.
63 size_t operator*() const { return m_stream->GetWrittenBytes() - m_start; }
64 };
65
66 /// Construct with flags and address size and byte order.
67 ///
68 /// Construct with dump flags \a flags and the default address size. \a
69 /// flags can be any of the above enumeration logical OR'ed together.
70 Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order,
71 bool colors = false);
72
73 /// Construct a default Stream, not binary, host byte order and host addr
74 /// size.
75 ///
76 Stream(bool colors = false);
77
78 // FIXME: Streams should not be copyable.
79 Stream(const Stream &other) : m_forwarder(*this) { (*this) = other; }
80
81 Stream &operator=(const Stream &rhs) {
82 m_flags = rhs.m_flags;
86 return *this;
87 }
88
89 /// Destructor
90 virtual ~Stream();
91
92 // Subclasses must override these methods
93
94 /// Flush the stream.
95 ///
96 /// Subclasses should flush the stream to make any output appear if the
97 /// stream has any buffering.
98 virtual void Flush() = 0;
99
100 /// Output character bytes to the stream.
101 ///
102 /// Appends \a src_len characters from the buffer \a src to the stream.
103 ///
104 /// \param[in] src
105 /// A buffer containing at least \a src_len bytes of data.
106 ///
107 /// \param[in] src_len
108 /// A number of bytes to append to the stream.
109 ///
110 /// \return
111 /// The number of bytes that were appended to the stream.
112 size_t Write(const void *src, size_t src_len) {
113 size_t appended_byte_count = WriteImpl(src, src_len);
114 m_bytes_written += appended_byte_count;
115 return appended_byte_count;
116 }
117
118 size_t GetWrittenBytes() const { return m_bytes_written; }
119
120 // Member functions
121 size_t PutChar(char ch);
122
123 /// Set the byte_order value.
124 ///
125 /// Sets the byte order of the data to extract. Extracted values will be
126 /// swapped if necessary when decoding.
127 ///
128 /// \param[in] byte_order
129 /// The byte order value to use when extracting data.
130 ///
131 /// \return
132 /// The old byte order value.
134
135 /// Format a C string from a printf style format and variable arguments and
136 /// encode and append the resulting C string as hex bytes.
137 ///
138 /// \param[in] format
139 /// A printf style format string.
140 ///
141 /// \param[in] ...
142 /// Any additional arguments needed for the printf format string.
143 ///
144 /// \return
145 /// The number of bytes that were appended to the stream.
146 size_t PrintfAsRawHex8(const char *format, ...)
147 __attribute__((__format__(__printf__, 2, 3)));
148
149 /// Append an uint8_t value in the hexadecimal format to the stream.
150 ///
151 /// \param[in] uvalue
152 /// The value to append.
153 ///
154 /// \return
155 /// The number of bytes that were appended to the stream.
156 size_t PutHex8(uint8_t uvalue);
157
158 size_t PutNHex8(size_t n, uint8_t uvalue);
159
160 size_t PutHex16(uint16_t uvalue,
161 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
162
163 size_t PutHex32(uint32_t uvalue,
164 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
165
166 size_t PutHex64(uint64_t uvalue,
167 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
168
169 size_t PutMaxHex64(uint64_t uvalue, size_t byte_size,
170 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
171 size_t PutFloat(float f,
172 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
173
174 size_t PutDouble(double d,
175 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
176
177 size_t PutLongDouble(long double ld,
178 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
179
180 size_t PutPointer(void *ptr);
181
182 // Append \a src_len bytes from \a src to the stream as hex characters (two
183 // ascii characters per byte of input data)
184 size_t
185 PutBytesAsRawHex8(const void *src, size_t src_len,
186 lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid,
187 lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid);
188
189 // Append \a src_len bytes from \a s to the stream as binary data.
190 size_t PutRawBytes(const void *s, size_t src_len,
191 lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid,
192 lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid);
193
194 size_t PutStringAsRawHex8(llvm::StringRef s);
195
196 /// Output a NULL terminated C string \a cstr to the stream \a s.
197 ///
198 /// \param[in] cstr
199 /// A NULL terminated C string.
200 ///
201 /// \return
202 /// A reference to this class so multiple things can be streamed
203 /// in one statement.
204 Stream &operator<<(const char *cstr);
205
206 Stream &operator<<(llvm::StringRef str);
207
208 /// Output a pointer value \a p to the stream \a s.
209 ///
210 /// \param[in] p
211 /// A void pointer.
212 ///
213 /// \return
214 /// A reference to this class so multiple things can be streamed
215 /// in one statement.
216 Stream &operator<<(const void *p);
217
218 /// Output a character \a ch to the stream \a s.
219 ///
220 /// \param[in] ch
221 /// A printable character value.
222 ///
223 /// \return
224 /// A reference to this class so multiple things can be streamed
225 /// in one statement.
226 Stream &operator<<(char ch);
227
228 Stream &operator<<(uint8_t uval) = delete;
229 Stream &operator<<(uint16_t uval) = delete;
230 Stream &operator<<(uint32_t uval) = delete;
231 Stream &operator<<(uint64_t uval) = delete;
232 Stream &operator<<(int8_t sval) = delete;
233 Stream &operator<<(int16_t sval) = delete;
234 Stream &operator<<(int32_t sval) = delete;
235 Stream &operator<<(int64_t sval) = delete;
236
237 /// Output a C string to the stream.
238 ///
239 /// Print a C string \a cstr to the stream.
240 ///
241 /// \param[in] cstr
242 /// The string to be output to the stream.
243 size_t PutCString(llvm::StringRef cstr);
244
245 /// Output a C string to the stream with color highlighting.
246 ///
247 /// Print a C string \a text to the stream, applying color highlighting to
248 /// the portions of the string that match the regex pattern \a pattern. The
249 /// pattern is matched as many times as possible throughout the string. If \a
250 /// pattern is nullptr, then no highlighting is applied.
251 ///
252 /// The highlighting is applied by enclosing the matching text in ANSI color
253 /// codes. The \a prefix parameter specifies the ANSI code to start the color
254 /// (the standard value is assumed to be 'ansi.fg.red', representing red
255 /// foreground), and the \a suffix parameter specifies the ANSI code to end
256 /// the color (the standard value is assumed to be 'ansi.normal', resetting to
257 /// default text style). These constants should be defined appropriately in
258 /// your environment.
259 ///
260 /// \param[in] text
261 /// The string to be output to the stream.
262 ///
263 /// \param[in] settings
264 /// Optional print hilight settings.
266 llvm::StringRef text,
267 std::optional<HighlightSettings> settings = std::nullopt);
268
269 /// Output and End of Line character to the stream.
270 size_t EOL();
271
272 /// Get the address size in bytes.
273 ///
274 /// \return
275 /// The size of an address in bytes that is used when outputting
276 /// address and pointer values to the stream.
277 uint32_t GetAddressByteSize() const;
278
279 /// The flags accessor.
280 ///
281 /// \return
282 /// A reference to the Flags member variable.
283 Flags &GetFlags();
284
285 /// The flags const accessor.
286 ///
287 /// \return
288 /// A const reference to the Flags member variable.
289 const Flags &GetFlags() const;
290
291 //// The byte order accessor.
292 ////
293 //// \return
294 //// The byte order.
295 lldb::ByteOrder GetByteOrder() const;
296
297 /// Get the current indentation level.
298 ///
299 /// \return
300 /// The current indentation level.
301 unsigned GetIndentLevel() const;
302
303 /// Set the current indentation level.
304 ///
305 /// \param[in] level
306 /// The new indentation level.
307 void SetIndentLevel(unsigned level);
308
309 /// Indent the current line in the stream.
310 ///
311 /// Indent the current line using the current indentation level and print an
312 /// optional string following the indentation spaces.
313 ///
314 /// \param[in] s
315 /// A string to print following the indentation.
316 size_t Indent(llvm::StringRef s = "");
317
318 /// Decrement the current indentation level.
319 void IndentLess(unsigned amount = 2);
320
321 /// Increment the current indentation level.
322 void IndentMore(unsigned amount = 2);
323
324 struct IndentScope {
328
329 private:
332 };
333
334 /// Create an indentation scope that restores the original indent level when
335 /// the object goes out of scope (RAII).
336 IndentScope MakeIndentScope(unsigned indent_amount = 2);
337
338 /// Output an offset value.
339 ///
340 /// Put an offset \a uval out to the stream using the printf format in \a
341 /// format.
342 ///
343 /// \param[in] offset
344 /// The offset value.
345 ///
346 /// \param[in] format
347 /// The printf style format to use when outputting the offset.
348 void Offset(uint32_t offset, const char *format = "0x%8.8x: ");
349
350 /// Output printf formatted output to the stream.
351 ///
352 /// Print some formatted output to the stream.
353 ///
354 /// \param[in] format
355 /// A printf style format string.
356 ///
357 /// \param[in] ...
358 /// Variable arguments that are needed for the printf style
359 /// format string \a format.
360 size_t Printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
361
362 size_t PrintfVarArg(const char *format, va_list args);
363
364 template <typename... Args> void Format(const char *format, Args &&... args) {
365 PutCString(llvm::formatv(format, std::forward<Args>(args)...).str());
366 }
367
368 /// Output a quoted C string value to the stream.
369 ///
370 /// Print a double quoted NULL terminated C string to the stream using the
371 /// printf format in \a format.
372 ///
373 /// \param[in] cstr
374 /// A NULL terminated C string value.
375 ///
376 /// \param[in] format
377 /// The optional C string format that can be overridden.
378 void QuotedCString(const char *cstr, const char *format = "\"%s\"");
379
380 /// Set the address size in bytes.
381 ///
382 /// \param[in] addr_size
383 /// The new size in bytes of an address to use when outputting
384 /// address and pointer values.
385 void SetAddressByteSize(uint32_t addr_size);
386
387 /// Output a SLEB128 number to the stream.
388 ///
389 /// Put an SLEB128 \a uval out to the stream using the printf format in \a
390 /// format.
391 ///
392 /// \param[in] uval
393 /// A uint64_t value that was extracted as a SLEB128 value.
394 size_t PutSLEB128(int64_t uval);
395
396 /// Output a ULEB128 number to the stream.
397 ///
398 /// Put an ULEB128 \a uval out to the stream using the printf format in \a
399 /// format.
400 ///
401 /// \param[in] uval
402 /// A uint64_t value that was extracted as a ULEB128 value.
403 size_t PutULEB128(uint64_t uval);
404
405 /// Returns a raw_ostream that forwards the data to this Stream object.
406 llvm::raw_ostream &AsRawOstream() {
407 return m_forwarder;
408 }
409
410protected:
411 // Member variables
412 Flags m_flags; ///< Dump flags.
413 uint32_t m_addr_size = 4; ///< Size of an address in bytes.
415 m_byte_order; ///< Byte order to use when encoding scalar types.
416 unsigned m_indent_level = 0; ///< Indention level.
417 std::size_t m_bytes_written = 0; ///< Number of bytes written so far.
418
419 void _PutHex8(uint8_t uvalue, bool add_prefix);
420
421 /// Output character bytes to the stream.
422 ///
423 /// Appends \a src_len characters from the buffer \a src to the stream.
424 ///
425 /// \param[in] src
426 /// A buffer containing at least \a src_len bytes of data.
427 ///
428 /// \param[in] src_len
429 /// A number of bytes to append to the stream.
430 ///
431 /// \return
432 /// The number of bytes that were appended to the stream.
433 virtual size_t WriteImpl(const void *src, size_t src_len) = 0;
434
435 /// \class RawOstreamForward Stream.h "lldb/Utility/Stream.h"
436 /// This is a wrapper class that exposes a raw_ostream interface that just
437 /// forwards to an LLDB stream, allowing to reuse LLVM algorithms that take
438 /// a raw_ostream within the LLDB code base.
439 class RawOstreamForward : public llvm::raw_ostream {
440 // Note: This stream must *not* maintain its own buffer, but instead
441 // directly write everything to the internal Stream class. Without this,
442 // we would run into the problem that the Stream written byte count would
443 // differ from the actually written bytes by the size of the internal
444 // raw_ostream buffer.
445
447 void write_impl(const char *Ptr, size_t Size) override {
448 m_target.Write(Ptr, Size);
449 }
450
451 uint64_t current_pos() const override {
452 return m_target.GetWrittenBytes();
453 }
454
455 public:
456 RawOstreamForward(Stream &target, bool colors = false)
457 : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {
458 enable_colors(colors);
459 }
460 };
462};
463
464/// Output an address value to this stream.
465///
466/// Put an address \a addr out to the stream with optional \a prefix and \a
467/// suffix strings.
468///
469/// \param[in] s
470/// The output stream.
471///
472/// \param[in] addr
473/// An address value.
474///
475/// \param[in] addr_size
476/// Size in bytes of the address, used for formatting.
477///
478/// \param[in] prefix
479/// A prefix C string. If nullptr, no prefix will be output.
480///
481/// \param[in] suffix
482/// A suffix C string. If nullptr, no suffix will be output.
483void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size,
484 const char *prefix = nullptr, const char *suffix = nullptr);
485
486/// Output an address range to this stream.
487///
488/// Put an address range \a lo_addr - \a hi_addr out to the stream with
489/// optional \a prefix and \a suffix strings.
490///
491/// \param[in] s
492/// The output stream.
493///
494/// \param[in] lo_addr
495/// The start address of the address range.
496///
497/// \param[in] hi_addr
498/// The end address of the address range.
499///
500/// \param[in] addr_size
501/// Size in bytes of the address, used for formatting.
502///
503/// \param[in] prefix
504/// A prefix C string. If nullptr, no prefix will be output.
505///
506/// \param[in] suffix
507/// A suffix C string. If nullptr, no suffix will be output.
508void DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr, uint64_t hi_addr,
509 uint32_t addr_size, const char *prefix = nullptr,
510 const char *suffix = nullptr);
511
512} // namespace lldb_private
513
514#endif // LLDB_UTILITY_STREAM_H
A command line argument class.
Definition Args.h:33
A class to manage flags.
Definition Flags.h:22
size_t operator*() const
Returns the number of bytes written to the given Stream since this ByteDelta object was created.
Definition Stream.h:63
size_t m_start
Bytes we have written so far when ByteDelta was created.
Definition Stream.h:57
This is a wrapper class that exposes a raw_ostream interface that just forwards to an LLDB stream,...
Definition Stream.h:439
uint64_t current_pos() const override
Definition Stream.h:451
RawOstreamForward(Stream &target, bool colors=false)
Definition Stream.h:456
void write_impl(const char *Ptr, size_t Size) override
Definition Stream.h:447
A stream class that can stream formatted output to a file.
Definition Stream.h:28
unsigned m_indent_level
Indention level.
Definition Stream.h:416
void Format(const char *format, Args &&... args)
Definition Stream.h:364
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
virtual size_t WriteImpl(const void *src, size_t src_len)=0
Output character bytes to the stream.
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
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:406
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
Stream & operator=(const Stream &rhs)
Definition Stream.h:81
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
std::size_t m_bytes_written
Number of bytes written so far.
Definition Stream.h:417
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
virtual void Flush()=0
Flush the stream.
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
size_t GetWrittenBytes() const
Definition Stream.h:118
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
Stream(const Stream &other)
Definition Stream.h:79
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
A class that represents a running process on the host machine.
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.
Struct to store information for color highlighting in the stream.
Definition Stream.h:37
llvm::StringRef pattern
Regex pattern for highlighting.
Definition Stream.h:38
llvm::StringRef prefix
ANSI color code to start colorization.
Definition Stream.h:39
llvm::StringRef suffix
ANSI color code to end colorization.
Definition Stream.h:40
HighlightSettings(llvm::StringRef p, llvm::StringRef pre, llvm::StringRef suf)
Definition Stream.h:42