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