author | tschatzl |
Mon, 15 May 2017 12:20:15 +0200 | |
changeset 45114 | 45644c5f6b8e |
parent 37254 | 8631304f255c |
permissions | -rw-r--r-- |
1 | 1 |
/* |
35869 | 2 |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
1 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1388
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1388
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1388
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#ifndef SHARE_VM_UTILITIES_OSTREAM_HPP |
26 |
#define SHARE_VM_UTILITIES_OSTREAM_HPP |
|
27 |
||
28 |
#include "memory/allocation.hpp" |
|
29 |
#include "runtime/timer.hpp" |
|
35897 | 30 |
#include "utilities/globalDefinitions.hpp" |
7397 | 31 |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
17121
diff
changeset
|
32 |
DEBUG_ONLY(class ResourceMark;) |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
17121
diff
changeset
|
33 |
|
1 | 34 |
// Output streams for printing |
35 |
// |
|
36 |
// Printing guidelines: |
|
37 |
// Where possible, please use tty->print() and tty->print_cr(). |
|
38 |
// For product mode VM warnings use warning() which internally uses tty. |
|
39 |
// In places where tty is not initialized yet or too much overhead, |
|
40 |
// we may use jio_printf: |
|
41 |
// jio_fprintf(defaultStream::output_stream(), "Message"); |
|
42 |
// This allows for redirection via -XX:+DisplayVMOutputToStdout and |
|
43 |
// -XX:+DisplayVMOutputToStderr |
|
44 |
class outputStream : public ResourceObj { |
|
45 |
protected: |
|
46 |
int _indentation; // current indentation |
|
47 |
int _width; // width of the page |
|
48 |
int _position; // position on the current line |
|
49 |
int _newlines; // number of '\n' output so far |
|
50 |
julong _precount; // number of chars output, less _position |
|
51 |
TimeStamp _stamp; // for time stamps |
|
35869 | 52 |
char* _scratch; // internal scratch buffer for printf |
53 |
size_t _scratch_len; // size of internal scratch buffer |
|
1 | 54 |
|
55 |
void update_position(const char* s, size_t len); |
|
56 |
static const char* do_vsnprintf(char* buffer, size_t buflen, |
|
57 |
const char* format, va_list ap, |
|
58 |
bool add_cr, |
|
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
59 |
size_t& result_len) ATTRIBUTE_PRINTF(3, 0); |
1 | 60 |
|
35869 | 61 |
// calls do_vsnprintf and writes output to stream; uses an on-stack buffer. |
62 |
void do_vsnprintf_and_write_with_automatic_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); |
|
63 |
// calls do_vsnprintf and writes output to stream; uses the user-provided buffer; |
|
64 |
void do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); |
|
65 |
// calls do_vsnprintf, then writes output to stream. |
|
66 |
void do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) ATTRIBUTE_PRINTF(2, 0); |
|
67 |
||
1 | 68 |
public: |
69 |
// creation |
|
70 |
outputStream(int width = 80); |
|
71 |
outputStream(int width, bool has_time_stamps); |
|
72 |
||
73 |
// indentation |
|
13476 | 74 |
outputStream& indent(); |
1 | 75 |
void inc() { _indentation++; }; |
76 |
void dec() { _indentation--; }; |
|
13476 | 77 |
void inc(int n) { _indentation += n; }; |
78 |
void dec(int n) { _indentation -= n; }; |
|
1 | 79 |
int indentation() const { return _indentation; } |
80 |
void set_indentation(int i) { _indentation = i; } |
|
81 |
void fill_to(int col); |
|
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
82 |
void move_to(int col, int slop = 6, int min_space = 2); |
1 | 83 |
|
84 |
// sizing |
|
85 |
int width() const { return _width; } |
|
86 |
int position() const { return _position; } |
|
87 |
int newlines() const { return _newlines; } |
|
88 |
julong count() const { return _precount + _position; } |
|
89 |
void set_count(julong count) { _precount = count - _position; } |
|
90 |
void set_position(int pos) { _position = pos; } |
|
91 |
||
92 |
// printing |
|
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
93 |
void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
94 |
void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
95 |
void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); |
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
96 |
void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); |
1 | 97 |
void print_raw(const char* str) { write(str, strlen(str)); } |
98 |
void print_raw(const char* str, int len) { write(str, len); } |
|
99 |
void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } |
|
100 |
void print_raw_cr(const char* str, int len){ write(str, len); cr(); } |
|
13476 | 101 |
void print_data(void* data, size_t len, bool with_ascii); |
1 | 102 |
void put(char ch); |
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
103 |
void sp(int count = 1); |
1 | 104 |
void cr(); |
105 |
void bol() { if (_position > 0) cr(); } |
|
106 |
||
107 |
// Time stamp |
|
108 |
TimeStamp& time_stamp() { return _stamp; } |
|
109 |
void stamp(); |
|
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
347
diff
changeset
|
110 |
void stamp(bool guard, const char* prefix, const char* suffix); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
347
diff
changeset
|
111 |
void stamp(bool guard) { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
347
diff
changeset
|
112 |
stamp(guard, "", ": "); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
347
diff
changeset
|
113 |
} |
1 | 114 |
// Date stamp |
115 |
void date_stamp(bool guard, const char* prefix, const char* suffix); |
|
116 |
// A simplified call that includes a suffix of ": " |
|
117 |
void date_stamp(bool guard) { |
|
118 |
date_stamp(guard, "", ": "); |
|
119 |
} |
|
120 |
||
121 |
// portable printing of 64 bit integers |
|
122 |
void print_jlong(jlong value); |
|
123 |
void print_julong(julong value); |
|
124 |
||
125 |
// flushing |
|
126 |
virtual void flush() {} |
|
127 |
virtual void write(const char* str, size_t len) = 0; |
|
23517 | 128 |
virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation |
9990
c8683968c01b
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
8921
diff
changeset
|
129 |
virtual ~outputStream() {} // close properly on deletion |
1 | 130 |
|
35869 | 131 |
// Caller may specify their own scratch buffer to use for printing; otherwise, |
132 |
// an automatic buffer on the stack (with O_BUFLEN len) is used. |
|
133 |
void set_scratch_buffer(char* p, size_t len) { _scratch = p; _scratch_len = len; } |
|
134 |
||
1 | 135 |
void dec_cr() { dec(); cr(); } |
136 |
void inc_cr() { inc(); cr(); } |
|
137 |
}; |
|
138 |
||
139 |
// standard output |
|
9990
c8683968c01b
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
8921
diff
changeset
|
140 |
// ANSI C++ name collision |
1 | 141 |
extern outputStream* tty; // tty output |
142 |
||
13476 | 143 |
class streamIndentor : public StackObj { |
144 |
private: |
|
145 |
outputStream* _str; |
|
146 |
int _amount; |
|
147 |
||
148 |
public: |
|
149 |
streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) { |
|
150 |
_str->inc(_amount); |
|
151 |
} |
|
152 |
~streamIndentor() { _str->dec(_amount); } |
|
153 |
}; |
|
154 |
||
155 |
||
1 | 156 |
// advisory locking for the shared tty stream: |
157 |
class ttyLocker: StackObj { |
|
8333
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
158 |
friend class ttyUnlocker; |
1 | 159 |
private: |
160 |
intx _holder; |
|
161 |
||
162 |
public: |
|
163 |
static intx hold_tty(); // returns a "holder" token |
|
164 |
static void release_tty(intx holder); // must witness same token |
|
8333
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
165 |
static bool release_tty_if_locked(); // returns true if lock was released |
1 | 166 |
static void break_tty_lock_for_safepoint(intx holder); |
167 |
||
168 |
ttyLocker() { _holder = hold_tty(); } |
|
169 |
~ttyLocker() { release_tty(_holder); } |
|
170 |
}; |
|
171 |
||
8333
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
172 |
// Release the tty lock if it's held and reacquire it if it was |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
173 |
// locked. Used to avoid lock ordering problems. |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
174 |
class ttyUnlocker: StackObj { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
175 |
private: |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
176 |
bool _was_locked; |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
177 |
public: |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
178 |
ttyUnlocker() { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
179 |
_was_locked = ttyLocker::release_tty_if_locked(); |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
180 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
181 |
~ttyUnlocker() { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
182 |
if (_was_locked) { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
183 |
ttyLocker::hold_tty(); |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
184 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
185 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
186 |
}; |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
187 |
|
1 | 188 |
// for writing to strings; buffer will expand automatically |
189 |
class stringStream : public outputStream { |
|
190 |
protected: |
|
191 |
char* buffer; |
|
192 |
size_t buffer_pos; |
|
193 |
size_t buffer_length; |
|
194 |
bool buffer_fixed; |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
17121
diff
changeset
|
195 |
DEBUG_ONLY(ResourceMark* rm;) |
1 | 196 |
public: |
197 |
stringStream(size_t initial_bufsize = 256); |
|
198 |
stringStream(char* fixed_buffer, size_t fixed_buffer_size); |
|
199 |
~stringStream(); |
|
200 |
virtual void write(const char* c, size_t len); |
|
201 |
size_t size() { return buffer_pos; } |
|
202 |
const char* base() { return buffer; } |
|
203 |
void reset() { buffer_pos = 0; _precount = 0; _position = 0; } |
|
204 |
char* as_string(); |
|
205 |
}; |
|
206 |
||
207 |
class fileStream : public outputStream { |
|
208 |
protected: |
|
209 |
FILE* _file; |
|
210 |
bool _need_close; |
|
211 |
public: |
|
9990
c8683968c01b
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
8921
diff
changeset
|
212 |
fileStream() { _file = NULL; _need_close = false; } |
1 | 213 |
fileStream(const char* file_name); |
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
214 |
fileStream(const char* file_name, const char* opentype); |
17121
e40a97c700d9
8012260: ciReplay: Include PID into the name of replay data file
vlivanov
parents:
15803
diff
changeset
|
215 |
fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; } |
1 | 216 |
~fileStream(); |
217 |
bool is_open() const { return _file != NULL; } |
|
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
218 |
void set_need_close(bool b) { _need_close = b;} |
1 | 219 |
virtual void write(const char* c, size_t len); |
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
220 |
size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); } |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
221 |
char* readln(char *data, int count); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
222 |
int eof() { return feof(_file); } |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
223 |
long fileSize(); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7397
diff
changeset
|
224 |
void rewind() { ::rewind(_file); } |
1 | 225 |
void flush(); |
226 |
}; |
|
227 |
||
26135
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25350
diff
changeset
|
228 |
CDS_ONLY(extern fileStream* classlist_file;) |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25350
diff
changeset
|
229 |
|
1 | 230 |
// unlike fileStream, fdStream does unbuffered I/O by calling |
231 |
// open() and write() directly. It is async-safe, but output |
|
232 |
// from multiple thread may be mixed together. Used by fatal |
|
233 |
// error handler. |
|
234 |
class fdStream : public outputStream { |
|
235 |
protected: |
|
236 |
int _fd; |
|
237 |
bool _need_close; |
|
238 |
public: |
|
239 |
fdStream(const char* file_name); |
|
240 |
fdStream(int fd = -1) { _fd = fd; _need_close = false; } |
|
241 |
~fdStream(); |
|
242 |
bool is_open() const { return _fd != -1; } |
|
243 |
void set_fd(int fd) { _fd = fd; _need_close = false; } |
|
244 |
int fd() const { return _fd; } |
|
245 |
virtual void write(const char* c, size_t len); |
|
246 |
void flush() {}; |
|
247 |
}; |
|
248 |
||
249 |
void ostream_init(); |
|
250 |
void ostream_init_log(); |
|
251 |
void ostream_exit(); |
|
252 |
void ostream_abort(); |
|
253 |
||
254 |
// In the non-fixed buffer case an underlying buffer will be created and |
|
255 |
// managed in C heap. Not MT-safe. |
|
256 |
class bufferedStream : public outputStream { |
|
257 |
protected: |
|
258 |
char* buffer; |
|
259 |
size_t buffer_pos; |
|
768 | 260 |
size_t buffer_max; |
1 | 261 |
size_t buffer_length; |
262 |
bool buffer_fixed; |
|
263 |
public: |
|
768 | 264 |
bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); |
265 |
bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); |
|
1 | 266 |
~bufferedStream(); |
267 |
virtual void write(const char* c, size_t len); |
|
268 |
size_t size() { return buffer_pos; } |
|
269 |
const char* base() { return buffer; } |
|
270 |
void reset() { buffer_pos = 0; _precount = 0; _position = 0; } |
|
271 |
char* as_string(); |
|
272 |
}; |
|
273 |
||
274 |
#define O_BUFLEN 2000 // max size of output of individual print() methods |
|
275 |
||
276 |
#ifndef PRODUCT |
|
277 |
||
278 |
class networkStream : public bufferedStream { |
|
279 |
||
280 |
private: |
|
281 |
int _socket; |
|
282 |
||
283 |
public: |
|
284 |
networkStream(); |
|
285 |
~networkStream(); |
|
286 |
||
287 |
bool connect(const char *host, short port); |
|
288 |
bool is_open() const { return _socket != -1; } |
|
289 |
int read(char *buf, size_t len); |
|
290 |
void close(); |
|
291 |
virtual void flush(); |
|
292 |
}; |
|
293 |
||
294 |
#endif |
|
7397 | 295 |
|
296 |
#endif // SHARE_VM_UTILITIES_OSTREAM_HPP |