1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_UTILS_OSTREAMS_H_ 6 #define V8_UTILS_OSTREAMS_H_ 7 8 #include <cstddef> 9 #include <cstdio> 10 #include <cstring> 11 #include <ostream> 12 #include <streambuf> 13 14 #include "include/v8config.h" 15 #include "src/base/macros.h" 16 #include "src/base/platform/mutex.h" 17 #include "src/base/strings.h" 18 #include "src/common/globals.h" 19 20 namespace v8 { 21 namespace internal { 22 23 class V8_EXPORT_PRIVATE OFStreamBase : public std::streambuf { 24 public: 25 explicit OFStreamBase(FILE* f); 26 ~OFStreamBase() override = default; 27 28 protected: 29 FILE* const f_; 30 31 int sync() override; 32 int_type overflow(int_type c) override; 33 std::streamsize xsputn(const char* s, std::streamsize n) override; 34 }; 35 36 // Output buffer and stream writing into debugger's command window. 37 class V8_EXPORT_PRIVATE DbgStreamBuf : public std::streambuf { 38 public: 39 DbgStreamBuf(); 40 ~DbgStreamBuf() override; 41 42 private: 43 int sync() override; 44 int overflow(int c) override; 45 46 char data_[256]; 47 }; 48 49 class DbgStdoutStream : public std::ostream { 50 public: 51 DbgStdoutStream(); 52 ~DbgStdoutStream() override = default; 53 54 private: 55 DbgStreamBuf streambuf_; 56 }; 57 58 // An output stream writing to a file. 59 class V8_EXPORT_PRIVATE OFStream : public std::ostream { 60 public: 61 explicit OFStream(FILE* f); 62 ~OFStream() override = default; 63 64 private: 65 OFStreamBase buf_; 66 }; 67 68 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT) 69 class V8_EXPORT_PRIVATE AndroidLogStream : public std::streambuf { 70 public: 71 virtual ~AndroidLogStream(); 72 73 protected: 74 std::streamsize xsputn(const char* s, std::streamsize n) override; 75 76 private: 77 std::string line_buffer_; 78 }; 79 80 class StdoutStream : public std::ostream { 81 public: StdoutStream()82 StdoutStream() : std::ostream(&stream_) {} 83 84 private: 85 static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex(); 86 87 AndroidLogStream stream_; 88 base::RecursiveMutexGuard mutex_guard_{GetStdoutMutex()}; 89 }; 90 #else 91 class StdoutStream : public OFStream { 92 public: StdoutStream()93 StdoutStream() : OFStream(stdout) {} 94 95 private: 96 static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex(); 97 98 base::RecursiveMutexGuard mutex_guard_{GetStdoutMutex()}; 99 }; 100 #endif 101 102 // Wrappers to disambiguate uint16_t and base::uc16. 103 struct AsUC16 { AsUC16AsUC16104 explicit AsUC16(uint16_t v) : value(v) {} 105 uint16_t value; 106 }; 107 108 struct AsUC32 { AsUC32AsUC32109 explicit AsUC32(int32_t v) : value(v) {} 110 int32_t value; 111 }; 112 113 struct AsReversiblyEscapedUC16 { AsReversiblyEscapedUC16AsReversiblyEscapedUC16114 explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {} 115 uint16_t value; 116 }; 117 118 struct AsEscapedUC16ForJSON { AsEscapedUC16ForJSONAsEscapedUC16ForJSON119 explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {} 120 uint16_t value; 121 }; 122 123 // Output the given value as hex, with a minimum width and optional prefix (0x). 124 // E.g. AsHex(23, 3, true) produces "0x017". Produces an empty string if both 125 // {min_width} and the value are 0. 126 struct AsHex { 127 explicit AsHex(uint64_t v, uint8_t min_width = 1, bool with_prefix = false) valueAsHex128 : value(v), min_width(min_width), with_prefix(with_prefix) {} 129 uint64_t value; 130 uint8_t min_width; 131 bool with_prefix; 132 AddressAsHex133 static AsHex Address(Address a) { 134 return AsHex(a, kSystemPointerHexDigits, true); 135 } 136 }; 137 138 // Output the given value as hex, separated in individual bytes. 139 // E.g. AsHexBytes(0x231712, 4) produces "12 17 23 00" if output as little 140 // endian (default), and "00 23 17 12" as big endian. Produces an empty string 141 // if both {min_bytes} and the value are 0. 142 struct AsHexBytes { 143 enum ByteOrder { kLittleEndian, kBigEndian }; 144 explicit AsHexBytes(uint64_t v, uint8_t min_bytes = 1, 145 ByteOrder byte_order = kLittleEndian) valueAsHexBytes146 : value(v), min_bytes(min_bytes), byte_order(byte_order) {} 147 uint64_t value; 148 uint8_t min_bytes; 149 ByteOrder byte_order; 150 }; 151 152 template <typename T> 153 struct PrintIteratorRange { 154 T start; 155 T end; PrintIteratorRangePrintIteratorRange156 PrintIteratorRange(T start, T end) : start(start), end(end) {} 157 }; 158 159 // Print any collection which can be iterated via std::begin and std::end. 160 // {Iterator} is the common type of {std::begin} and {std::end} called on a 161 // {const T&}. This function is only instantiable if that type exists. 162 template <typename T> 163 auto PrintCollection(const T& collection) -> PrintIteratorRange< 164 typename std::common_type<decltype(std::begin(collection)), 165 decltype(std::end(collection))>::type> { 166 return {std::begin(collection), std::end(collection)}; 167 } 168 169 // Writes the given character to the output escaping everything outside of 170 // printable/space ASCII range. Additionally escapes '\' making escaping 171 // reversible. 172 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c); 173 174 // Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '. 175 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, 176 const AsEscapedUC16ForJSON& c); 177 178 // Writes the given character to the output escaping everything outside 179 // of printable ASCII range. 180 std::ostream& operator<<(std::ostream& os, const AsUC16& c); 181 182 // Writes the given character to the output escaping everything outside 183 // of printable ASCII range. 184 std::ostream& operator<<(std::ostream& os, const AsUC32& c); 185 186 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v); 187 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, 188 const AsHexBytes& v); 189 190 template <typename T> 191 std::ostream& operator<<(std::ostream& os, const PrintIteratorRange<T>& range) { 192 const char* comma = ""; 193 os << "["; 194 for (T it = range.start; it != range.end; ++it, comma = ", ") { 195 os << comma << *it; 196 } 197 os << "]"; 198 return os; 199 } 200 201 } // namespace internal 202 } // namespace v8 203 204 #endif // V8_UTILS_OSTREAMS_H_ 205