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