1 //===-- Stream.h ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Stream_h_
11 #define liblldb_Stream_h_
13 #include "lldb/Utility/Flags.h"
14 #include "lldb/lldb-defines.h"
15 #include "lldb/lldb-enumerations.h" // for ByteOrder::eByteOrderInvalid
16 #include "llvm/ADT/StringRef.h" // for StringRef
17 #include "llvm/Support/FormatVariadic.h"
20 #include <stddef.h> // for size_t
21 #include <stdint.h> // for uint32_t, uint64_t, uint8_t
22 #include <type_traits> // for forward
24 namespace lldb_private {
26 //----------------------------------------------------------------------
27 /// @class Stream Stream.h "lldb/Utility/Stream.h"
28 /// A stream class that can stream formatted output to a file.
29 //----------------------------------------------------------------------
32 //------------------------------------------------------------------
33 /// \a m_flags bit values.
34 //------------------------------------------------------------------
36 eBinary = (1 << 0) ///< Get and put data as binary instead of as the default
40 //------------------------------------------------------------------
41 /// Construct with flags and address size and byte order.
43 /// Construct with dump flags \a flags and the default address size. \a
44 /// flags can be any of the above enumeration logical OR'ed together.
45 //------------------------------------------------------------------
46 Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order);
48 //------------------------------------------------------------------
49 /// Construct a default Stream, not binary, host byte order and host addr
52 //------------------------------------------------------------------
55 //------------------------------------------------------------------
57 //------------------------------------------------------------------
60 //------------------------------------------------------------------
61 // Subclasses must override these methods
62 //------------------------------------------------------------------
64 //------------------------------------------------------------------
67 /// Subclasses should flush the stream to make any output appear if the
68 /// stream has any buffering.
69 //------------------------------------------------------------------
70 virtual void Flush() = 0;
72 //------------------------------------------------------------------
73 /// Output character bytes to the stream.
75 /// Appends \a src_len characters from the buffer \a src to the stream.
78 /// A buffer containing at least \a src_len bytes of data.
80 /// @param[in] src_len
81 /// A number of bytes to append to the stream.
84 /// The number of bytes that were appended to the stream.
85 //------------------------------------------------------------------
86 virtual size_t Write(const void *src, size_t src_len) = 0;
88 //------------------------------------------------------------------
90 //------------------------------------------------------------------
91 size_t PutChar(char ch);
93 //------------------------------------------------------------------
94 /// Set the byte_order value.
96 /// Sets the byte order of the data to extract. Extracted values will be
97 /// swapped if necessary when decoding.
99 /// @param[in] byte_order
100 /// The byte order value to use when extracting data.
103 /// The old byte order value.
104 //------------------------------------------------------------------
105 lldb::ByteOrder SetByteOrder(lldb::ByteOrder byte_order);
107 //------------------------------------------------------------------
108 /// Format a C string from a printf style format and variable arguments and
109 /// encode and append the resulting C string as hex bytes.
111 /// @param[in] format
112 /// A printf style format string.
115 /// Any additional arguments needed for the printf format string.
118 /// The number of bytes that were appended to the stream.
119 //------------------------------------------------------------------
120 size_t PrintfAsRawHex8(const char *format, ...)
121 __attribute__((__format__(__printf__, 2, 3)));
123 //------------------------------------------------------------------
124 /// Format a C string from a printf style format and variable arguments and
125 /// encode and append the resulting C string as hex bytes.
127 /// @param[in] format
128 /// A printf style format string.
131 /// Any additional arguments needed for the printf format string.
134 /// The number of bytes that were appended to the stream.
135 //------------------------------------------------------------------
136 size_t PutHex8(uint8_t uvalue);
138 size_t PutNHex8(size_t n, uint8_t uvalue);
140 size_t PutHex16(uint16_t uvalue,
141 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
143 size_t PutHex32(uint32_t uvalue,
144 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
146 size_t PutHex64(uint64_t uvalue,
147 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
149 size_t PutMaxHex64(uint64_t uvalue, size_t byte_size,
150 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
151 size_t PutFloat(float f,
152 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
154 size_t PutDouble(double d,
155 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
157 size_t PutLongDouble(long double ld,
158 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
160 size_t PutPointer(void *ptr);
162 // Append \a src_len bytes from \a src to the stream as hex characters (two
163 // ascii characters per byte of input data)
165 PutBytesAsRawHex8(const void *src, size_t src_len,
166 lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid,
167 lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid);
169 // Append \a src_len bytes from \a s to the stream as binary data.
170 size_t PutRawBytes(const void *s, size_t src_len,
171 lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid,
172 lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid);
174 size_t PutCStringAsRawHex8(const char *s);
176 //------------------------------------------------------------------
177 /// Output a NULL terminated C string \a cstr to the stream \a s.
180 /// A NULL terminated C string.
183 /// A reference to this class so multiple things can be streamed
184 /// in one statement.
185 //------------------------------------------------------------------
186 Stream &operator<<(const char *cstr);
188 Stream &operator<<(llvm::StringRef str);
190 //------------------------------------------------------------------
191 /// Output a pointer value \a p to the stream \a s.
197 /// A reference to this class so multiple things can be streamed
198 /// in one statement.
199 //------------------------------------------------------------------
200 Stream &operator<<(const void *p);
202 //------------------------------------------------------------------
203 /// Output a character \a ch to the stream \a s.
206 /// A printable character value.
209 /// A reference to this class so multiple things can be streamed
210 /// in one statement.
211 //------------------------------------------------------------------
212 Stream &operator<<(char ch);
214 //------------------------------------------------------------------
215 /// Output a uint8_t \a uval to the stream \a s.
221 /// A reference to this class so multiple things can be streamed
222 /// in one statement.
223 //------------------------------------------------------------------
224 Stream &operator<<(uint8_t uval);
226 //------------------------------------------------------------------
227 /// Output a uint16_t \a uval to the stream \a s.
230 /// A uint16_t value.
233 /// A reference to this class so multiple things can be streamed
234 /// in one statement.
235 //------------------------------------------------------------------
236 Stream &operator<<(uint16_t uval);
238 //------------------------------------------------------------------
239 /// Output a uint32_t \a uval to the stream \a s.
242 /// A uint32_t value.
245 /// A reference to this class so multiple things can be streamed
246 /// in one statement.
247 //------------------------------------------------------------------
248 Stream &operator<<(uint32_t uval);
250 //------------------------------------------------------------------
251 /// Output a uint64_t \a uval to the stream \a s.
254 /// A uint64_t value.
257 /// A reference to this class so multiple things can be streamed
258 /// in one statement.
259 //------------------------------------------------------------------
260 Stream &operator<<(uint64_t uval);
262 //------------------------------------------------------------------
263 /// Output a int8_t \a sval to the stream \a s.
269 /// A reference to this class so multiple things can be streamed
270 /// in one statement.
271 //------------------------------------------------------------------
272 Stream &operator<<(int8_t sval);
274 //------------------------------------------------------------------
275 /// Output a int16_t \a sval to the stream \a s.
281 /// A reference to this class so multiple things can be streamed
282 /// in one statement.
283 //------------------------------------------------------------------
284 Stream &operator<<(int16_t sval);
286 //------------------------------------------------------------------
287 /// Output a int32_t \a sval to the stream \a s.
293 /// A reference to this class so multiple things can be streamed
294 /// in one statement.
295 //------------------------------------------------------------------
296 Stream &operator<<(int32_t sval);
298 //------------------------------------------------------------------
299 /// Output a int64_t \a sval to the stream \a s.
305 /// A reference to this class so multiple things can be streamed
306 /// in one statement.
307 //------------------------------------------------------------------
308 Stream &operator<<(int64_t sval);
310 //------------------------------------------------------------------
311 /// Output an address value to this stream.
313 /// Put an address \a addr out to the stream with optional \a prefix and \a
317 /// An address value.
319 /// @param[in] addr_size
320 /// Size in bytes of the address, used for formatting.
322 /// @param[in] prefix
323 /// A prefix C string. If nullptr, no prefix will be output.
325 /// @param[in] suffix
326 /// A suffix C string. If nullptr, no suffix will be output.
327 //------------------------------------------------------------------
328 void Address(uint64_t addr, uint32_t addr_size, const char *prefix = nullptr,
329 const char *suffix = nullptr);
331 //------------------------------------------------------------------
332 /// Output an address range to this stream.
334 /// Put an address range \a lo_addr - \a hi_addr out to the stream with
335 /// optional \a prefix and \a suffix strings.
337 /// @param[in] lo_addr
338 /// The start address of the address range.
340 /// @param[in] hi_addr
341 /// The end address of the address range.
343 /// @param[in] addr_size
344 /// Size in bytes of the address, used for formatting.
346 /// @param[in] prefix
347 /// A prefix C string. If nullptr, no prefix will be output.
349 /// @param[in] suffix
350 /// A suffix C string. If nullptr, no suffix will be output.
351 //------------------------------------------------------------------
352 void AddressRange(uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size,
353 const char *prefix = nullptr, const char *suffix = nullptr);
355 //------------------------------------------------------------------
356 /// Output a C string to the stream.
358 /// Print a C string \a cstr to the stream.
361 /// The string to be output to the stream.
362 //------------------------------------------------------------------
363 size_t PutCString(llvm::StringRef cstr);
365 //------------------------------------------------------------------
366 /// Output and End of Line character to the stream.
367 //------------------------------------------------------------------
370 //------------------------------------------------------------------
371 /// Get the address size in bytes.
374 /// The size of an address in bytes that is used when outputting
375 /// address and pointer values to the stream.
376 //------------------------------------------------------------------
377 uint32_t GetAddressByteSize() const;
379 //------------------------------------------------------------------
380 /// The flags accessor.
383 /// A reference to the Flags member variable.
384 //------------------------------------------------------------------
387 //------------------------------------------------------------------
388 /// The flags const accessor.
391 /// A const reference to the Flags member variable.
392 //------------------------------------------------------------------
393 const Flags &GetFlags() const;
395 //------------------------------------------------------------------
396 //// The byte order accessor.
400 //------------------------------------------------------------------
401 lldb::ByteOrder GetByteOrder() const;
403 //------------------------------------------------------------------
404 /// Get the current indentation level.
407 /// The current indentation level as an integer.
408 //------------------------------------------------------------------
409 int GetIndentLevel() const;
411 //------------------------------------------------------------------
412 /// Indent the current line in the stream.
414 /// Indent the current line using the current indentation level and print an
415 /// optional string following the indentation spaces.
418 /// A C string to print following the indentation. If nullptr, just
419 /// output the indentation characters.
420 //------------------------------------------------------------------
421 size_t Indent(const char *s = nullptr);
422 size_t Indent(llvm::StringRef s);
424 //------------------------------------------------------------------
425 /// Decrement the current indentation level.
426 //------------------------------------------------------------------
427 void IndentLess(int amount = 2);
429 //------------------------------------------------------------------
430 /// Increment the current indentation level.
431 //------------------------------------------------------------------
432 void IndentMore(int amount = 2);
434 //------------------------------------------------------------------
435 /// Output an offset value.
437 /// Put an offset \a uval out to the stream using the printf format in \a
440 /// @param[in] offset
441 /// The offset value.
443 /// @param[in] format
444 /// The printf style format to use when outputting the offset.
445 //------------------------------------------------------------------
446 void Offset(uint32_t offset, const char *format = "0x%8.8x: ");
448 //------------------------------------------------------------------
449 /// Output printf formatted output to the stream.
451 /// Print some formatted output to the stream.
453 /// @param[in] format
454 /// A printf style format string.
457 /// Variable arguments that are needed for the printf style
458 /// format string \a format.
459 //------------------------------------------------------------------
460 size_t Printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
462 size_t PrintfVarArg(const char *format, va_list args);
464 template <typename... Args> void Format(const char *format, Args &&... args) {
465 PutCString(llvm::formatv(format, std::forward<Args>(args)...).str());
468 //------------------------------------------------------------------
469 /// Output a quoted C string value to the stream.
471 /// Print a double quoted NULL terminated C string to the stream using the
472 /// printf format in \a format.
475 /// A NULL terminated C string value.
477 /// @param[in] format
478 /// The optional C string format that can be overridden.
479 //------------------------------------------------------------------
480 void QuotedCString(const char *cstr, const char *format = "\"%s\"");
482 //------------------------------------------------------------------
483 /// Set the address size in bytes.
485 /// @param[in] addr_size
486 /// The new size in bytes of an address to use when outputting
487 /// address and pointer values.
488 //------------------------------------------------------------------
489 void SetAddressByteSize(uint32_t addr_size);
491 //------------------------------------------------------------------
492 /// Set the current indentation level.
495 /// The new indentation level.
496 //------------------------------------------------------------------
497 void SetIndentLevel(int level);
499 //------------------------------------------------------------------
500 /// Output a SLEB128 number to the stream.
502 /// Put an SLEB128 \a uval out to the stream using the printf format in \a
506 /// A uint64_t value that was extracted as a SLEB128 value.
508 /// @param[in] format
509 /// The optional printf format that can be overridden.
510 //------------------------------------------------------------------
511 size_t PutSLEB128(int64_t uval);
513 //------------------------------------------------------------------
514 /// Output a ULEB128 number to the stream.
516 /// Put an ULEB128 \a uval out to the stream using the printf format in \a
520 /// A uint64_t value that was extracted as a ULEB128 value.
522 /// @param[in] format
523 /// The optional printf format that can be overridden.
524 //------------------------------------------------------------------
525 size_t PutULEB128(uint64_t uval);
528 //------------------------------------------------------------------
530 //------------------------------------------------------------------
531 Flags m_flags; ///< Dump flags.
532 uint32_t m_addr_size; ///< Size of an address in bytes.
534 m_byte_order; ///< Byte order to use when encoding scalar types.
535 int m_indent_level; ///< Indention level.
537 size_t _PutHex8(uint8_t uvalue, bool add_prefix);
540 } // namespace lldb_private
542 #endif // liblldb_Stream_h_