Stream.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #ifndef LLDB_UTILITY_STREAM_H
  9. #define LLDB_UTILITY_STREAM_H
  10. #include "lldb/Utility/Flags.h"
  11. #include "lldb/lldb-defines.h"
  12. #include "lldb/lldb-enumerations.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Support/FormatVariadic.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cstdarg>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <type_traits>
  20. namespace lldb_private {
  21. /// \class Stream Stream.h "lldb/Utility/Stream.h"
  22. /// A stream class that can stream formatted output to a file.
  23. class Stream {
  24. public:
  25. /// \a m_flags bit values.
  26. enum {
  27. eBinary = (1 << 0) ///< Get and put data as binary instead of as the default
  28. /// string mode.
  29. };
  30. /// Utility class for counting the bytes that were written to a stream in a
  31. /// certain time span.
  32. ///
  33. /// \example
  34. /// ByteDelta delta(*this);
  35. /// WriteDataToStream("foo");
  36. /// return *delta;
  37. class ByteDelta {
  38. Stream *m_stream;
  39. /// Bytes we have written so far when ByteDelta was created.
  40. size_t m_start;
  41. public:
  42. ByteDelta(Stream &s) : m_stream(&s), m_start(s.GetWrittenBytes()) {}
  43. /// Returns the number of bytes written to the given Stream since this
  44. /// ByteDelta object was created.
  45. size_t operator*() const { return m_stream->GetWrittenBytes() - m_start; }
  46. };
  47. /// Construct with flags and address size and byte order.
  48. ///
  49. /// Construct with dump flags \a flags and the default address size. \a
  50. /// flags can be any of the above enumeration logical OR'ed together.
  51. Stream(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order,
  52. bool colors = false);
  53. /// Construct a default Stream, not binary, host byte order and host addr
  54. /// size.
  55. ///
  56. Stream(bool colors = false);
  57. // FIXME: Streams should not be copyable.
  58. Stream(const Stream &other) : m_forwarder(*this) { (*this) = other; }
  59. Stream &operator=(const Stream &rhs) {
  60. m_flags = rhs.m_flags;
  61. m_addr_size = rhs.m_addr_size;
  62. m_byte_order = rhs.m_byte_order;
  63. m_indent_level = rhs.m_indent_level;
  64. return *this;
  65. }
  66. /// Destructor
  67. virtual ~Stream();
  68. // Subclasses must override these methods
  69. /// Flush the stream.
  70. ///
  71. /// Subclasses should flush the stream to make any output appear if the
  72. /// stream has any buffering.
  73. virtual void Flush() = 0;
  74. /// Output character bytes to the stream.
  75. ///
  76. /// Appends \a src_len characters from the buffer \a src to the stream.
  77. ///
  78. /// \param[in] src
  79. /// A buffer containing at least \a src_len bytes of data.
  80. ///
  81. /// \param[in] src_len
  82. /// A number of bytes to append to the stream.
  83. ///
  84. /// \return
  85. /// The number of bytes that were appended to the stream.
  86. size_t Write(const void *src, size_t src_len) {
  87. size_t appended_byte_count = WriteImpl(src, src_len);
  88. m_bytes_written += appended_byte_count;
  89. return appended_byte_count;
  90. }
  91. size_t GetWrittenBytes() const { return m_bytes_written; }
  92. // Member functions
  93. size_t PutChar(char ch);
  94. /// Set the byte_order value.
  95. ///
  96. /// Sets the byte order of the data to extract. Extracted values will be
  97. /// swapped if necessary when decoding.
  98. ///
  99. /// \param[in] byte_order
  100. /// The byte order value to use when extracting data.
  101. ///
  102. /// \return
  103. /// The old byte order value.
  104. lldb::ByteOrder SetByteOrder(lldb::ByteOrder byte_order);
  105. /// Format a C string from a printf style format and variable arguments and
  106. /// encode and append the resulting C string as hex bytes.
  107. ///
  108. /// \param[in] format
  109. /// A printf style format string.
  110. ///
  111. /// \param[in] ...
  112. /// Any additional arguments needed for the printf format string.
  113. ///
  114. /// \return
  115. /// The number of bytes that were appended to the stream.
  116. size_t PrintfAsRawHex8(const char *format, ...)
  117. __attribute__((__format__(__printf__, 2, 3)));
  118. /// Append an uint8_t value in the hexadecimal format to the stream.
  119. ///
  120. /// \param[in] uvalue
  121. /// The value to append.
  122. ///
  123. /// \return
  124. /// The number of bytes that were appended to the stream.
  125. size_t PutHex8(uint8_t uvalue);
  126. size_t PutNHex8(size_t n, uint8_t uvalue);
  127. size_t PutHex16(uint16_t uvalue,
  128. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  129. size_t PutHex32(uint32_t uvalue,
  130. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  131. size_t PutHex64(uint64_t uvalue,
  132. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  133. size_t PutMaxHex64(uint64_t uvalue, size_t byte_size,
  134. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  135. size_t PutFloat(float f,
  136. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  137. size_t PutDouble(double d,
  138. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  139. size_t PutLongDouble(long double ld,
  140. lldb::ByteOrder byte_order = lldb::eByteOrderInvalid);
  141. size_t PutPointer(void *ptr);
  142. // Append \a src_len bytes from \a src to the stream as hex characters (two
  143. // ascii characters per byte of input data)
  144. size_t
  145. PutBytesAsRawHex8(const void *src, size_t src_len,
  146. lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid,
  147. lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid);
  148. // Append \a src_len bytes from \a s to the stream as binary data.
  149. size_t PutRawBytes(const void *s, size_t src_len,
  150. lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid,
  151. lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid);
  152. size_t PutStringAsRawHex8(llvm::StringRef s);
  153. /// Output a NULL terminated C string \a cstr to the stream \a s.
  154. ///
  155. /// \param[in] cstr
  156. /// A NULL terminated C string.
  157. ///
  158. /// \return
  159. /// A reference to this class so multiple things can be streamed
  160. /// in one statement.
  161. Stream &operator<<(const char *cstr);
  162. Stream &operator<<(llvm::StringRef str);
  163. /// Output a pointer value \a p to the stream \a s.
  164. ///
  165. /// \param[in] p
  166. /// A void pointer.
  167. ///
  168. /// \return
  169. /// A reference to this class so multiple things can be streamed
  170. /// in one statement.
  171. Stream &operator<<(const void *p);
  172. /// Output a character \a ch to the stream \a s.
  173. ///
  174. /// \param[in] ch
  175. /// A printable character value.
  176. ///
  177. /// \return
  178. /// A reference to this class so multiple things can be streamed
  179. /// in one statement.
  180. Stream &operator<<(char ch);
  181. Stream &operator<<(uint8_t uval) = delete;
  182. Stream &operator<<(uint16_t uval) = delete;
  183. Stream &operator<<(uint32_t uval) = delete;
  184. Stream &operator<<(uint64_t uval) = delete;
  185. Stream &operator<<(int8_t sval) = delete;
  186. Stream &operator<<(int16_t sval) = delete;
  187. Stream &operator<<(int32_t sval) = delete;
  188. Stream &operator<<(int64_t sval) = delete;
  189. /// Output a C string to the stream.
  190. ///
  191. /// Print a C string \a cstr to the stream.
  192. ///
  193. /// \param[in] cstr
  194. /// The string to be output to the stream.
  195. size_t PutCString(llvm::StringRef cstr);
  196. /// Output and End of Line character to the stream.
  197. size_t EOL();
  198. /// Get the address size in bytes.
  199. ///
  200. /// \return
  201. /// The size of an address in bytes that is used when outputting
  202. /// address and pointer values to the stream.
  203. uint32_t GetAddressByteSize() const;
  204. /// The flags accessor.
  205. ///
  206. /// \return
  207. /// A reference to the Flags member variable.
  208. Flags &GetFlags();
  209. /// The flags const accessor.
  210. ///
  211. /// \return
  212. /// A const reference to the Flags member variable.
  213. const Flags &GetFlags() const;
  214. //// The byte order accessor.
  215. ////
  216. //// \return
  217. //// The byte order.
  218. lldb::ByteOrder GetByteOrder() const;
  219. /// Get the current indentation level.
  220. ///
  221. /// \return
  222. /// The current indentation level.
  223. unsigned GetIndentLevel() const;
  224. /// Indent the current line in the stream.
  225. ///
  226. /// Indent the current line using the current indentation level and print an
  227. /// optional string following the indentation spaces.
  228. ///
  229. /// \param[in] s
  230. /// A string to print following the indentation.
  231. size_t Indent(llvm::StringRef s = "");
  232. /// Decrement the current indentation level.
  233. void IndentLess(unsigned amount = 2);
  234. /// Increment the current indentation level.
  235. void IndentMore(unsigned amount = 2);
  236. /// Output an offset value.
  237. ///
  238. /// Put an offset \a uval out to the stream using the printf format in \a
  239. /// format.
  240. ///
  241. /// \param[in] offset
  242. /// The offset value.
  243. ///
  244. /// \param[in] format
  245. /// The printf style format to use when outputting the offset.
  246. void Offset(uint32_t offset, const char *format = "0x%8.8x: ");
  247. /// Output printf formatted output to the stream.
  248. ///
  249. /// Print some formatted output to the stream.
  250. ///
  251. /// \param[in] format
  252. /// A printf style format string.
  253. ///
  254. /// \param[in] ...
  255. /// Variable arguments that are needed for the printf style
  256. /// format string \a format.
  257. size_t Printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
  258. size_t PrintfVarArg(const char *format, va_list args);
  259. template <typename... Args> void Format(const char *format, Args &&... args) {
  260. PutCString(llvm::formatv(format, std::forward<Args>(args)...).str());
  261. }
  262. /// Output a quoted C string value to the stream.
  263. ///
  264. /// Print a double quoted NULL terminated C string to the stream using the
  265. /// printf format in \a format.
  266. ///
  267. /// \param[in] cstr
  268. /// A NULL terminated C string value.
  269. ///
  270. /// \param[in] format
  271. /// The optional C string format that can be overridden.
  272. void QuotedCString(const char *cstr, const char *format = "\"%s\"");
  273. /// Set the address size in bytes.
  274. ///
  275. /// \param[in] addr_size
  276. /// The new size in bytes of an address to use when outputting
  277. /// address and pointer values.
  278. void SetAddressByteSize(uint32_t addr_size);
  279. /// Set the current indentation level.
  280. ///
  281. /// \param[in] level
  282. /// The new indentation level.
  283. void SetIndentLevel(unsigned level);
  284. /// Output a SLEB128 number to the stream.
  285. ///
  286. /// Put an SLEB128 \a uval out to the stream using the printf format in \a
  287. /// format.
  288. ///
  289. /// \param[in] uval
  290. /// A uint64_t value that was extracted as a SLEB128 value.
  291. size_t PutSLEB128(int64_t uval);
  292. /// Output a ULEB128 number to the stream.
  293. ///
  294. /// Put an ULEB128 \a uval out to the stream using the printf format in \a
  295. /// format.
  296. ///
  297. /// \param[in] uval
  298. /// A uint64_t value that was extracted as a ULEB128 value.
  299. size_t PutULEB128(uint64_t uval);
  300. /// Returns a raw_ostream that forwards the data to this Stream object.
  301. llvm::raw_ostream &AsRawOstream() {
  302. return m_forwarder;
  303. }
  304. protected:
  305. // Member variables
  306. Flags m_flags; ///< Dump flags.
  307. uint32_t m_addr_size; ///< Size of an address in bytes.
  308. lldb::ByteOrder
  309. m_byte_order; ///< Byte order to use when encoding scalar types.
  310. unsigned m_indent_level; ///< Indention level.
  311. std::size_t m_bytes_written = 0; ///< Number of bytes written so far.
  312. void _PutHex8(uint8_t uvalue, bool add_prefix);
  313. /// Output character bytes to the stream.
  314. ///
  315. /// Appends \a src_len characters from the buffer \a src to the stream.
  316. ///
  317. /// \param[in] src
  318. /// A buffer containing at least \a src_len bytes of data.
  319. ///
  320. /// \param[in] src_len
  321. /// A number of bytes to append to the stream.
  322. ///
  323. /// \return
  324. /// The number of bytes that were appended to the stream.
  325. virtual size_t WriteImpl(const void *src, size_t src_len) = 0;
  326. /// \class RawOstreamForward Stream.h "lldb/Utility/Stream.h"
  327. /// This is a wrapper class that exposes a raw_ostream interface that just
  328. /// forwards to an LLDB stream, allowing to reuse LLVM algorithms that take
  329. /// a raw_ostream within the LLDB code base.
  330. class RawOstreamForward : public llvm::raw_ostream {
  331. // Note: This stream must *not* maintain its own buffer, but instead
  332. // directly write everything to the internal Stream class. Without this,
  333. // we would run into the problem that the Stream written byte count would
  334. // differ from the actually written bytes by the size of the internal
  335. // raw_ostream buffer.
  336. Stream &m_target;
  337. void write_impl(const char *Ptr, size_t Size) override {
  338. m_target.Write(Ptr, Size);
  339. }
  340. uint64_t current_pos() const override {
  341. return m_target.GetWrittenBytes();
  342. }
  343. public:
  344. RawOstreamForward(Stream &target, bool colors = false)
  345. : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {
  346. enable_colors(colors);
  347. }
  348. };
  349. RawOstreamForward m_forwarder;
  350. };
  351. /// Output an address value to this stream.
  352. ///
  353. /// Put an address \a addr out to the stream with optional \a prefix and \a
  354. /// suffix strings.
  355. ///
  356. /// \param[in] s
  357. /// The output stream.
  358. ///
  359. /// \param[in] addr
  360. /// An address value.
  361. ///
  362. /// \param[in] addr_size
  363. /// Size in bytes of the address, used for formatting.
  364. ///
  365. /// \param[in] prefix
  366. /// A prefix C string. If nullptr, no prefix will be output.
  367. ///
  368. /// \param[in] suffix
  369. /// A suffix C string. If nullptr, no suffix will be output.
  370. void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size,
  371. const char *prefix = nullptr, const char *suffix = nullptr);
  372. /// Output an address range to this stream.
  373. ///
  374. /// Put an address range \a lo_addr - \a hi_addr out to the stream with
  375. /// optional \a prefix and \a suffix strings.
  376. ///
  377. /// \param[in] s
  378. /// The output stream.
  379. ///
  380. /// \param[in] lo_addr
  381. /// The start address of the address range.
  382. ///
  383. /// \param[in] hi_addr
  384. /// The end address of the address range.
  385. ///
  386. /// \param[in] addr_size
  387. /// Size in bytes of the address, used for formatting.
  388. ///
  389. /// \param[in] prefix
  390. /// A prefix C string. If nullptr, no prefix will be output.
  391. ///
  392. /// \param[in] suffix
  393. /// A suffix C string. If nullptr, no suffix will be output.
  394. void DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr, uint64_t hi_addr,
  395. uint32_t addr_size, const char *prefix = nullptr,
  396. const char *suffix = nullptr);
  397. } // namespace lldb_private
  398. #endif // LLDB_UTILITY_STREAM_H