author | jrose |
Sat, 09 Apr 2011 21:16:12 -0700 | |
changeset 9124 | f60dee480d49 |
parent 8887 | 1e9e60a225a6 |
child 9990 | c8683968c01b |
permissions | -rw-r--r-- |
1 | 1 |
/* |
8867
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, 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:
5237
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5237
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:
5237
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
26 |
#include "compiler/compileLog.hpp" |
|
27 |
#include "oops/oop.inline.hpp" |
|
28 |
#include "runtime/arguments.hpp" |
|
29 |
#include "utilities/defaultStream.hpp" |
|
30 |
#include "utilities/ostream.hpp" |
|
31 |
#include "utilities/top.hpp" |
|
32 |
#include "utilities/xmlstream.hpp" |
|
33 |
#ifdef TARGET_OS_FAMILY_linux |
|
34 |
# include "os_linux.inline.hpp" |
|
35 |
#endif |
|
36 |
#ifdef TARGET_OS_FAMILY_solaris |
|
37 |
# include "os_solaris.inline.hpp" |
|
38 |
#endif |
|
39 |
#ifdef TARGET_OS_FAMILY_windows |
|
40 |
# include "os_windows.inline.hpp" |
|
41 |
#endif |
|
1 | 42 |
|
43 |
extern "C" void jio_print(const char* s); // Declarationtion of jvm method |
|
44 |
||
45 |
outputStream::outputStream(int width) { |
|
46 |
_width = width; |
|
47 |
_position = 0; |
|
48 |
_newlines = 0; |
|
49 |
_precount = 0; |
|
50 |
_indentation = 0; |
|
51 |
} |
|
52 |
||
53 |
outputStream::outputStream(int width, bool has_time_stamps) { |
|
54 |
_width = width; |
|
55 |
_position = 0; |
|
56 |
_newlines = 0; |
|
57 |
_precount = 0; |
|
58 |
_indentation = 0; |
|
59 |
if (has_time_stamps) _stamp.update(); |
|
60 |
} |
|
61 |
||
62 |
void outputStream::update_position(const char* s, size_t len) { |
|
63 |
for (size_t i = 0; i < len; i++) { |
|
64 |
char ch = s[i]; |
|
65 |
if (ch == '\n') { |
|
66 |
_newlines += 1; |
|
67 |
_precount += _position + 1; |
|
68 |
_position = 0; |
|
69 |
} else if (ch == '\t') { |
|
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
70 |
int tw = 8 - (_position & 7); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
71 |
_position += tw; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
72 |
_precount -= tw-1; // invariant: _precount + _position == total count |
1 | 73 |
} else { |
74 |
_position += 1; |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
// Execute a vsprintf, using the given buffer if necessary. |
|
80 |
// Return a pointer to the formatted string. |
|
81 |
const char* outputStream::do_vsnprintf(char* buffer, size_t buflen, |
|
82 |
const char* format, va_list ap, |
|
83 |
bool add_cr, |
|
84 |
size_t& result_len) { |
|
85 |
const char* result; |
|
86 |
if (add_cr) buflen--; |
|
87 |
if (!strchr(format, '%')) { |
|
88 |
// constant format string |
|
89 |
result = format; |
|
90 |
result_len = strlen(result); |
|
91 |
if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate |
|
92 |
} else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') { |
|
93 |
// trivial copy-through format string |
|
94 |
result = va_arg(ap, const char*); |
|
95 |
result_len = strlen(result); |
|
96 |
if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate |
|
97 |
} else if (vsnprintf(buffer, buflen, format, ap) >= 0) { |
|
98 |
result = buffer; |
|
99 |
result_len = strlen(result); |
|
100 |
} else { |
|
101 |
DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");) |
|
102 |
result = buffer; |
|
103 |
result_len = buflen - 1; |
|
104 |
buffer[result_len] = 0; |
|
105 |
} |
|
106 |
if (add_cr) { |
|
107 |
if (result != buffer) { |
|
108 |
strncpy(buffer, result, buflen); |
|
109 |
result = buffer; |
|
110 |
} |
|
111 |
buffer[result_len++] = '\n'; |
|
112 |
buffer[result_len] = 0; |
|
113 |
} |
|
114 |
return result; |
|
115 |
} |
|
116 |
||
117 |
void outputStream::print(const char* format, ...) { |
|
118 |
char buffer[O_BUFLEN]; |
|
119 |
va_list ap; |
|
120 |
va_start(ap, format); |
|
121 |
size_t len; |
|
122 |
const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len); |
|
123 |
write(str, len); |
|
124 |
va_end(ap); |
|
125 |
} |
|
126 |
||
127 |
void outputStream::print_cr(const char* format, ...) { |
|
128 |
char buffer[O_BUFLEN]; |
|
129 |
va_list ap; |
|
130 |
va_start(ap, format); |
|
131 |
size_t len; |
|
132 |
const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len); |
|
133 |
write(str, len); |
|
134 |
va_end(ap); |
|
135 |
} |
|
136 |
||
137 |
void outputStream::vprint(const char *format, va_list argptr) { |
|
138 |
char buffer[O_BUFLEN]; |
|
139 |
size_t len; |
|
140 |
const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len); |
|
141 |
write(str, len); |
|
142 |
} |
|
143 |
||
144 |
void outputStream::vprint_cr(const char* format, va_list argptr) { |
|
145 |
char buffer[O_BUFLEN]; |
|
146 |
size_t len; |
|
147 |
const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len); |
|
148 |
write(str, len); |
|
149 |
} |
|
150 |
||
151 |
void outputStream::fill_to(int col) { |
|
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
152 |
int need_fill = col - position(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
153 |
sp(need_fill); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
154 |
} |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
155 |
|
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
156 |
void outputStream::move_to(int col, int slop, int min_space) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
157 |
if (position() >= col + slop) |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
158 |
cr(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
159 |
int need_fill = col - position(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
160 |
if (need_fill < min_space) |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
161 |
need_fill = min_space; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
162 |
sp(need_fill); |
1 | 163 |
} |
164 |
||
165 |
void outputStream::put(char ch) { |
|
166 |
assert(ch != 0, "please fix call site"); |
|
167 |
char buf[] = { ch, '\0' }; |
|
168 |
write(buf, 1); |
|
169 |
} |
|
170 |
||
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
171 |
#define SP_USE_TABS false |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
172 |
|
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
173 |
void outputStream::sp(int count) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
174 |
if (count < 0) return; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
175 |
if (SP_USE_TABS && count >= 8) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
176 |
int target = position() + count; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
177 |
while (count >= 8) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
178 |
this->write("\t", 1); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
179 |
count -= 8; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
180 |
} |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
181 |
count = target - position(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
182 |
} |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
183 |
while (count > 0) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
184 |
int nw = (count > 8) ? 8 : count; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
185 |
this->write(" ", nw); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
186 |
count -= nw; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
187 |
} |
1 | 188 |
} |
189 |
||
190 |
void outputStream::cr() { |
|
191 |
this->write("\n", 1); |
|
192 |
} |
|
193 |
||
194 |
void outputStream::stamp() { |
|
195 |
if (! _stamp.is_updated()) { |
|
196 |
_stamp.update(); // start at 0 on first call to stamp() |
|
197 |
} |
|
198 |
||
199 |
// outputStream::stamp() may get called by ostream_abort(), use snprintf |
|
200 |
// to avoid allocating large stack buffer in print(). |
|
201 |
char buf[40]; |
|
202 |
jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds()); |
|
203 |
print_raw(buf); |
|
204 |
} |
|
205 |
||
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
206 |
void outputStream::stamp(bool guard, |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
207 |
const char* prefix, |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
208 |
const char* suffix) { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
209 |
if (!guard) { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
210 |
return; |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
211 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
212 |
print_raw(prefix); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
213 |
stamp(); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
214 |
print_raw(suffix); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
215 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
216 |
|
1 | 217 |
void outputStream::date_stamp(bool guard, |
218 |
const char* prefix, |
|
219 |
const char* suffix) { |
|
220 |
if (!guard) { |
|
221 |
return; |
|
222 |
} |
|
223 |
print_raw(prefix); |
|
224 |
static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz"; |
|
225 |
static const int buffer_length = 32; |
|
226 |
char buffer[buffer_length]; |
|
227 |
const char* iso8601_result = os::iso8601_time(buffer, buffer_length); |
|
228 |
if (iso8601_result != NULL) { |
|
229 |
print_raw(buffer); |
|
230 |
} else { |
|
231 |
print_raw(error_time); |
|
232 |
} |
|
233 |
print_raw(suffix); |
|
234 |
return; |
|
235 |
} |
|
236 |
||
237 |
void outputStream::indent() { |
|
238 |
while (_position < _indentation) sp(); |
|
239 |
} |
|
240 |
||
241 |
void outputStream::print_jlong(jlong value) { |
|
242 |
// N.B. Same as INT64_FORMAT |
|
243 |
print(os::jlong_format_specifier(), value); |
|
244 |
} |
|
245 |
||
246 |
void outputStream::print_julong(julong value) { |
|
247 |
// N.B. Same as UINT64_FORMAT |
|
248 |
print(os::julong_format_specifier(), value); |
|
249 |
} |
|
250 |
||
251 |
stringStream::stringStream(size_t initial_size) : outputStream() { |
|
252 |
buffer_length = initial_size; |
|
253 |
buffer = NEW_RESOURCE_ARRAY(char, buffer_length); |
|
254 |
buffer_pos = 0; |
|
255 |
buffer_fixed = false; |
|
256 |
} |
|
257 |
||
258 |
// useful for output to fixed chunks of memory, such as performance counters |
|
259 |
stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() { |
|
260 |
buffer_length = fixed_buffer_size; |
|
261 |
buffer = fixed_buffer; |
|
262 |
buffer_pos = 0; |
|
263 |
buffer_fixed = true; |
|
264 |
} |
|
265 |
||
266 |
void stringStream::write(const char* s, size_t len) { |
|
267 |
size_t write_len = len; // number of non-null bytes to write |
|
268 |
size_t end = buffer_pos + len + 1; // position after write and final '\0' |
|
269 |
if (end > buffer_length) { |
|
270 |
if (buffer_fixed) { |
|
271 |
// if buffer cannot resize, silently truncate |
|
272 |
end = buffer_length; |
|
273 |
write_len = end - buffer_pos - 1; // leave room for the final '\0' |
|
274 |
} else { |
|
275 |
// For small overruns, double the buffer. For larger ones, |
|
276 |
// increase to the requested size. |
|
277 |
if (end < buffer_length * 2) { |
|
278 |
end = buffer_length * 2; |
|
279 |
} |
|
280 |
char* oldbuf = buffer; |
|
281 |
buffer = NEW_RESOURCE_ARRAY(char, end); |
|
282 |
strncpy(buffer, oldbuf, buffer_pos); |
|
283 |
buffer_length = end; |
|
284 |
} |
|
285 |
} |
|
286 |
// invariant: buffer is always null-terminated |
|
287 |
guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob"); |
|
288 |
buffer[buffer_pos + write_len] = 0; |
|
289 |
strncpy(buffer + buffer_pos, s, write_len); |
|
290 |
buffer_pos += write_len; |
|
291 |
||
292 |
// Note that the following does not depend on write_len. |
|
293 |
// This means that position and count get updated |
|
294 |
// even when overflow occurs. |
|
295 |
update_position(s, len); |
|
296 |
} |
|
297 |
||
298 |
char* stringStream::as_string() { |
|
299 |
char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1); |
|
300 |
strncpy(copy, buffer, buffer_pos); |
|
301 |
copy[buffer_pos] = 0; // terminating null |
|
302 |
return copy; |
|
303 |
} |
|
304 |
||
305 |
stringStream::~stringStream() {} |
|
306 |
||
307 |
xmlStream* xtty; |
|
308 |
outputStream* tty; |
|
309 |
outputStream* gclog_or_tty; |
|
310 |
extern Mutex* tty_lock; |
|
311 |
||
312 |
fileStream::fileStream(const char* file_name) { |
|
313 |
_file = fopen(file_name, "w"); |
|
314 |
_need_close = true; |
|
315 |
} |
|
316 |
||
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
317 |
fileStream::fileStream(const char* file_name, const char* opentype) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
318 |
_file = fopen(file_name, opentype); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
319 |
_need_close = true; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
320 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
321 |
|
1 | 322 |
void fileStream::write(const char* s, size_t len) { |
1889
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
323 |
if (_file != NULL) { |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
324 |
// Make an unused local variable to avoid warning from gcc 4.x compiler. |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
325 |
size_t count = fwrite(s, 1, len, _file); |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
326 |
} |
1 | 327 |
update_position(s, len); |
328 |
} |
|
329 |
||
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
330 |
long fileStream::fileSize() { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
331 |
long size = -1; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
332 |
if (_file != NULL) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
333 |
long pos = ::ftell(_file); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
334 |
if (::fseek(_file, 0, SEEK_END) == 0) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
335 |
size = ::ftell(_file); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
336 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
337 |
::fseek(_file, pos, SEEK_SET); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
338 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
339 |
return size; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
340 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
341 |
|
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
342 |
char* fileStream::readln(char *data, int count ) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
343 |
char * ret = ::fgets(data, count, _file); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
344 |
//Get rid of annoying \n char |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
345 |
data[::strlen(data)-1] = '\0'; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
346 |
return ret; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
347 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
348 |
|
1 | 349 |
fileStream::~fileStream() { |
350 |
if (_file != NULL) { |
|
351 |
if (_need_close) fclose(_file); |
|
352 |
_file = NULL; |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
void fileStream::flush() { |
|
357 |
fflush(_file); |
|
358 |
} |
|
359 |
||
360 |
fdStream::fdStream(const char* file_name) { |
|
361 |
_fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
|
362 |
_need_close = true; |
|
363 |
} |
|
364 |
||
365 |
fdStream::~fdStream() { |
|
366 |
if (_fd != -1) { |
|
367 |
if (_need_close) close(_fd); |
|
368 |
_fd = -1; |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
void fdStream::write(const char* s, size_t len) { |
|
1889
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
373 |
if (_fd != -1) { |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
374 |
// Make an unused local variable to avoid warning from gcc 4.x compiler. |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
375 |
size_t count = ::write(_fd, s, (int)len); |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
376 |
} |
1 | 377 |
update_position(s, len); |
378 |
} |
|
379 |
||
380 |
defaultStream* defaultStream::instance = NULL; |
|
381 |
int defaultStream::_output_fd = 1; |
|
382 |
int defaultStream::_error_fd = 2; |
|
383 |
FILE* defaultStream::_output_stream = stdout; |
|
384 |
FILE* defaultStream::_error_stream = stderr; |
|
385 |
||
386 |
#define LOG_MAJOR_VERSION 160 |
|
387 |
#define LOG_MINOR_VERSION 1 |
|
388 |
||
389 |
void defaultStream::init() { |
|
390 |
_inited = true; |
|
391 |
if (LogVMOutput || LogCompilation) { |
|
392 |
init_log(); |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
bool defaultStream::has_log_file() { |
|
397 |
// lazily create log file (at startup, LogVMOutput is false even |
|
398 |
// if +LogVMOutput is used, because the flags haven't been parsed yet) |
|
399 |
// For safer printing during fatal error handling, do not init logfile |
|
400 |
// if a VM error has been reported. |
|
401 |
if (!_inited && !is_error_reported()) init(); |
|
402 |
return _log_file != NULL; |
|
403 |
} |
|
404 |
||
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
405 |
static const char* make_log_name(const char* log_name, const char* force_directory) { |
1 | 406 |
const char* basename = log_name; |
407 |
char file_sep = os::file_separator()[0]; |
|
408 |
const char* cp; |
|
409 |
for (cp = log_name; *cp != '\0'; cp++) { |
|
410 |
if (*cp == '/' || *cp == file_sep) { |
|
411 |
basename = cp+1; |
|
412 |
} |
|
413 |
} |
|
414 |
const char* nametail = log_name; |
|
415 |
||
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
416 |
// Compute buffer length |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
417 |
size_t buffer_length; |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
418 |
if (force_directory != NULL) { |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
419 |
buffer_length = strlen(force_directory) + strlen(os::file_separator()) + |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
420 |
strlen(basename) + 1; |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
421 |
} else { |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
422 |
buffer_length = strlen(log_name) + 1; |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
423 |
} |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
424 |
|
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
425 |
const char* star = strchr(basename, '*'); |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
426 |
int star_pos = (star == NULL) ? -1 : (star - nametail); |
8867
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
427 |
int skip = 1; |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
428 |
if (star == NULL) { |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
429 |
// Try %p |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
430 |
star = strstr(basename, "%p"); |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
431 |
if (star != NULL) { |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
432 |
skip = 2; |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
433 |
} |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
434 |
} |
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
435 |
star_pos = (star == NULL) ? -1 : (star - nametail); |
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
436 |
|
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
437 |
char pid[32]; |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
438 |
if (star_pos >= 0) { |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
439 |
jio_snprintf(pid, sizeof(pid), "%u", os::current_process_id()); |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
440 |
buffer_length += strlen(pid); |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
441 |
} |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
442 |
|
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
443 |
// Create big enough buffer. |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
444 |
char *buf = NEW_C_HEAP_ARRAY(char, buffer_length); |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
445 |
|
1 | 446 |
strcpy(buf, ""); |
447 |
if (force_directory != NULL) { |
|
448 |
strcat(buf, force_directory); |
|
449 |
strcat(buf, os::file_separator()); |
|
450 |
nametail = basename; // completely skip directory prefix |
|
451 |
} |
|
452 |
||
453 |
if (star_pos >= 0) { |
|
8867
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
454 |
// convert foo*bar.log or foo%pbar.log to foo123bar.log |
1 | 455 |
int buf_pos = (int) strlen(buf); |
456 |
strncpy(&buf[buf_pos], nametail, star_pos); |
|
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
457 |
strcpy(&buf[buf_pos + star_pos], pid); |
8867
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
458 |
nametail += star_pos + skip; // skip prefix and pid format |
1 | 459 |
} |
460 |
||
461 |
strcat(buf, nametail); // append rest of name, or all of name |
|
462 |
return buf; |
|
463 |
} |
|
464 |
||
465 |
void defaultStream::init_log() { |
|
466 |
// %%% Need a MutexLocker? |
|
467 |
const char* log_name = LogFile != NULL ? LogFile : "hotspot.log"; |
|
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
468 |
const char* try_name = make_log_name(log_name, NULL); |
1 | 469 |
fileStream* file = new(ResourceObj::C_HEAP) fileStream(try_name); |
470 |
if (!file->is_open()) { |
|
471 |
// Try again to open the file. |
|
472 |
char warnbuf[O_BUFLEN*2]; |
|
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
473 |
jio_snprintf(warnbuf, sizeof(warnbuf), |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
474 |
"Warning: Cannot open log file: %s\n", try_name); |
1 | 475 |
// Note: This feature is for maintainer use only. No need for L10N. |
476 |
jio_print(warnbuf); |
|
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
477 |
FREE_C_HEAP_ARRAY(char, try_name); |
8867
6febcc945802
7022204: LogFile wildcarding should use %p instead of star
never
parents:
8333
diff
changeset
|
478 |
try_name = make_log_name("hs_pid%p.log", os::get_temp_directory()); |
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
479 |
jio_snprintf(warnbuf, sizeof(warnbuf), |
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
480 |
"Warning: Forcing option -XX:LogFile=%s\n", try_name); |
1 | 481 |
jio_print(warnbuf); |
482 |
delete file; |
|
483 |
file = new(ResourceObj::C_HEAP) fileStream(try_name); |
|
5237
aab592fd4f44
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
2105
diff
changeset
|
484 |
FREE_C_HEAP_ARRAY(char, try_name); |
1 | 485 |
} |
486 |
if (file->is_open()) { |
|
487 |
_log_file = file; |
|
488 |
xmlStream* xs = new(ResourceObj::C_HEAP) xmlStream(file); |
|
489 |
_outer_xmlStream = xs; |
|
490 |
if (this == tty) xtty = xs; |
|
491 |
// Write XML header. |
|
492 |
xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>"); |
|
493 |
// (For now, don't bother to issue a DTD for this private format.) |
|
494 |
jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds(); |
|
495 |
// %%% Should be: jlong time_ms = os::start_time_milliseconds(), if |
|
496 |
// we ever get round to introduce that method on the os class |
|
497 |
xs->head("hotspot_log version='%d %d'" |
|
498 |
" process='%d' time_ms='"INT64_FORMAT"'", |
|
499 |
LOG_MAJOR_VERSION, LOG_MINOR_VERSION, |
|
500 |
os::current_process_id(), time_ms); |
|
501 |
// Write VM version header immediately. |
|
502 |
xs->head("vm_version"); |
|
503 |
xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr(); |
|
504 |
xs->tail("name"); |
|
505 |
xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr(); |
|
506 |
xs->tail("release"); |
|
507 |
xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr(); |
|
508 |
xs->tail("info"); |
|
509 |
xs->tail("vm_version"); |
|
510 |
// Record information about the command-line invocation. |
|
511 |
xs->head("vm_arguments"); // Cf. Arguments::print_on() |
|
512 |
if (Arguments::num_jvm_flags() > 0) { |
|
513 |
xs->head("flags"); |
|
514 |
Arguments::print_jvm_flags_on(xs->text()); |
|
515 |
xs->tail("flags"); |
|
516 |
} |
|
517 |
if (Arguments::num_jvm_args() > 0) { |
|
518 |
xs->head("args"); |
|
519 |
Arguments::print_jvm_args_on(xs->text()); |
|
520 |
xs->tail("args"); |
|
521 |
} |
|
522 |
if (Arguments::java_command() != NULL) { |
|
523 |
xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command()); |
|
524 |
xs->tail("command"); |
|
525 |
} |
|
526 |
if (Arguments::sun_java_launcher() != NULL) { |
|
527 |
xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher()); |
|
528 |
xs->tail("launcher"); |
|
529 |
} |
|
530 |
if (Arguments::system_properties() != NULL) { |
|
531 |
xs->head("properties"); |
|
532 |
// Print it as a java-style property list. |
|
533 |
// System properties don't generally contain newlines, so don't bother with unparsing. |
|
534 |
for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) { |
|
535 |
xs->text()->print_cr("%s=%s", p->key(), p->value()); |
|
536 |
} |
|
537 |
xs->tail("properties"); |
|
538 |
} |
|
539 |
xs->tail("vm_arguments"); |
|
540 |
// tty output per se is grouped under the <tty>...</tty> element. |
|
541 |
xs->head("tty"); |
|
542 |
// All further non-markup text gets copied to the tty: |
|
543 |
xs->_text = this; // requires friend declaration! |
|
544 |
} else { |
|
545 |
delete(file); |
|
546 |
// and leave xtty as NULL |
|
547 |
LogVMOutput = false; |
|
548 |
DisplayVMOutput = true; |
|
549 |
LogCompilation = false; |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
// finish_log() is called during normal VM shutdown. finish_log_on_error() is |
|
554 |
// called by ostream_abort() after a fatal error. |
|
555 |
// |
|
556 |
void defaultStream::finish_log() { |
|
557 |
xmlStream* xs = _outer_xmlStream; |
|
558 |
xs->done("tty"); |
|
559 |
||
560 |
// Other log forks are appended here, at the End of Time: |
|
561 |
CompileLog::finish_log(xs->out()); // write compile logging, if any, now |
|
562 |
||
563 |
xs->done("hotspot_log"); |
|
564 |
xs->flush(); |
|
565 |
||
566 |
fileStream* file = _log_file; |
|
567 |
_log_file = NULL; |
|
568 |
||
569 |
delete _outer_xmlStream; |
|
570 |
_outer_xmlStream = NULL; |
|
571 |
||
572 |
file->flush(); |
|
573 |
delete file; |
|
574 |
} |
|
575 |
||
576 |
void defaultStream::finish_log_on_error(char *buf, int buflen) { |
|
577 |
xmlStream* xs = _outer_xmlStream; |
|
578 |
||
579 |
if (xs && xs->out()) { |
|
580 |
||
581 |
xs->done_raw("tty"); |
|
582 |
||
583 |
// Other log forks are appended here, at the End of Time: |
|
584 |
CompileLog::finish_log_on_error(xs->out(), buf, buflen); // write compile logging, if any, now |
|
585 |
||
586 |
xs->done_raw("hotspot_log"); |
|
587 |
xs->flush(); |
|
588 |
||
589 |
fileStream* file = _log_file; |
|
590 |
_log_file = NULL; |
|
591 |
_outer_xmlStream = NULL; |
|
592 |
||
593 |
if (file) { |
|
594 |
file->flush(); |
|
595 |
||
596 |
// Can't delete or close the file because delete and fclose aren't |
|
597 |
// async-safe. We are about to die, so leave it to the kernel. |
|
598 |
// delete file; |
|
599 |
} |
|
600 |
} |
|
601 |
} |
|
602 |
||
603 |
intx defaultStream::hold(intx writer_id) { |
|
604 |
bool has_log = has_log_file(); // check before locking |
|
605 |
if (// impossible, but who knows? |
|
606 |
writer_id == NO_WRITER || |
|
607 |
||
608 |
// bootstrap problem |
|
609 |
tty_lock == NULL || |
|
610 |
||
611 |
// can't grab a lock or call Thread::current() if TLS isn't initialized |
|
612 |
ThreadLocalStorage::thread() == NULL || |
|
613 |
||
614 |
// developer hook |
|
615 |
!SerializeVMOutput || |
|
616 |
||
617 |
// VM already unhealthy |
|
618 |
is_error_reported() || |
|
619 |
||
620 |
// safepoint == global lock (for VM only) |
|
621 |
(SafepointSynchronize::is_synchronizing() && |
|
622 |
Thread::current()->is_VM_thread()) |
|
623 |
) { |
|
624 |
// do not attempt to lock unless we know the thread and the VM is healthy |
|
625 |
return NO_WRITER; |
|
626 |
} |
|
627 |
if (_writer == writer_id) { |
|
628 |
// already held, no need to re-grab the lock |
|
629 |
return NO_WRITER; |
|
630 |
} |
|
631 |
tty_lock->lock_without_safepoint_check(); |
|
632 |
// got the lock |
|
633 |
if (writer_id != _last_writer) { |
|
634 |
if (has_log) { |
|
635 |
_log_file->bol(); |
|
636 |
// output a hint where this output is coming from: |
|
637 |
_log_file->print_cr("<writer thread='"INTX_FORMAT"'/>", writer_id); |
|
638 |
} |
|
639 |
_last_writer = writer_id; |
|
640 |
} |
|
641 |
_writer = writer_id; |
|
642 |
return writer_id; |
|
643 |
} |
|
644 |
||
645 |
void defaultStream::release(intx holder) { |
|
646 |
if (holder == NO_WRITER) { |
|
647 |
// nothing to release: either a recursive lock, or we scribbled (too bad) |
|
648 |
return; |
|
649 |
} |
|
650 |
if (_writer != holder) { |
|
651 |
return; // already unlocked, perhaps via break_tty_lock_for_safepoint |
|
652 |
} |
|
653 |
_writer = NO_WRITER; |
|
654 |
tty_lock->unlock(); |
|
655 |
} |
|
656 |
||
657 |
||
658 |
// Yuck: jio_print does not accept char*/len. |
|
659 |
static void call_jio_print(const char* s, size_t len) { |
|
660 |
char buffer[O_BUFLEN+100]; |
|
661 |
if (len > sizeof(buffer)-1) { |
|
662 |
warning("increase O_BUFLEN in ostream.cpp -- output truncated"); |
|
663 |
len = sizeof(buffer)-1; |
|
664 |
} |
|
665 |
strncpy(buffer, s, len); |
|
666 |
buffer[len] = '\0'; |
|
667 |
jio_print(buffer); |
|
668 |
} |
|
669 |
||
670 |
||
671 |
void defaultStream::write(const char* s, size_t len) { |
|
672 |
intx thread_id = os::current_thread_id(); |
|
673 |
intx holder = hold(thread_id); |
|
674 |
||
675 |
if (DisplayVMOutput && |
|
676 |
(_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) { |
|
677 |
// print to output stream. It can be redirected by a vfprintf hook |
|
678 |
if (s[len] == '\0') { |
|
679 |
jio_print(s); |
|
680 |
} else { |
|
681 |
call_jio_print(s, len); |
|
682 |
} |
|
683 |
} |
|
684 |
||
685 |
// print to log file |
|
686 |
if (has_log_file()) { |
|
687 |
int nl0 = _newlines; |
|
688 |
xmlTextStream::write(s, len); |
|
689 |
// flush the log file too, if there were any newlines |
|
690 |
if (nl0 != _newlines){ |
|
691 |
flush(); |
|
692 |
} |
|
693 |
} else { |
|
694 |
update_position(s, len); |
|
695 |
} |
|
696 |
||
697 |
release(holder); |
|
698 |
} |
|
699 |
||
700 |
intx ttyLocker::hold_tty() { |
|
701 |
if (defaultStream::instance == NULL) return defaultStream::NO_WRITER; |
|
702 |
intx thread_id = os::current_thread_id(); |
|
703 |
return defaultStream::instance->hold(thread_id); |
|
704 |
} |
|
705 |
||
706 |
void ttyLocker::release_tty(intx holder) { |
|
707 |
if (holder == defaultStream::NO_WRITER) return; |
|
708 |
defaultStream::instance->release(holder); |
|
709 |
} |
|
710 |
||
8333
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
711 |
bool ttyLocker::release_tty_if_locked() { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
712 |
intx thread_id = os::current_thread_id(); |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
713 |
if (defaultStream::instance->writer() == thread_id) { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
714 |
// release the lock and return true so callers know if was |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
715 |
// previously held. |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
716 |
release_tty(thread_id); |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
717 |
return true; |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
718 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
719 |
return false; |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
720 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
721 |
|
1 | 722 |
void ttyLocker::break_tty_lock_for_safepoint(intx holder) { |
723 |
if (defaultStream::instance != NULL && |
|
724 |
defaultStream::instance->writer() == holder) { |
|
725 |
if (xtty != NULL) { |
|
726 |
xtty->print_cr("<!-- safepoint while printing -->"); |
|
727 |
} |
|
728 |
defaultStream::instance->release(holder); |
|
729 |
} |
|
730 |
// (else there was no lock to break) |
|
731 |
} |
|
732 |
||
733 |
void ostream_init() { |
|
734 |
if (defaultStream::instance == NULL) { |
|
735 |
defaultStream::instance = new(ResourceObj::C_HEAP) defaultStream(); |
|
736 |
tty = defaultStream::instance; |
|
737 |
||
738 |
// We want to ensure that time stamps in GC logs consider time 0 |
|
739 |
// the time when the JVM is initialized, not the first time we ask |
|
740 |
// for a time stamp. So, here, we explicitly update the time stamp |
|
741 |
// of tty. |
|
742 |
tty->time_stamp().update_to(1); |
|
743 |
} |
|
744 |
} |
|
745 |
||
746 |
void ostream_init_log() { |
|
747 |
// For -Xloggc:<file> option - called in runtime/thread.cpp |
|
748 |
// Note : this must be called AFTER ostream_init() |
|
749 |
||
750 |
gclog_or_tty = tty; // default to tty |
|
751 |
if (Arguments::gc_log_filename() != NULL) { |
|
752 |
fileStream * gclog = new(ResourceObj::C_HEAP) |
|
753 |
fileStream(Arguments::gc_log_filename()); |
|
754 |
if (gclog->is_open()) { |
|
755 |
// now we update the time stamp of the GC log to be synced up |
|
756 |
// with tty. |
|
757 |
gclog->time_stamp().update_to(tty->time_stamp().ticks()); |
|
758 |
gclog_or_tty = gclog; |
|
759 |
} |
|
760 |
} |
|
761 |
||
762 |
// If we haven't lazily initialized the logfile yet, do it now, |
|
763 |
// to avoid the possibility of lazy initialization during a VM |
|
764 |
// crash, which can affect the stability of the fatal error handler. |
|
765 |
defaultStream::instance->has_log_file(); |
|
766 |
} |
|
767 |
||
768 |
// ostream_exit() is called during normal VM exit to finish log files, flush |
|
769 |
// output and free resource. |
|
770 |
void ostream_exit() { |
|
771 |
static bool ostream_exit_called = false; |
|
772 |
if (ostream_exit_called) return; |
|
773 |
ostream_exit_called = true; |
|
774 |
if (gclog_or_tty != tty) { |
|
775 |
delete gclog_or_tty; |
|
776 |
} |
|
777 |
{ |
|
778 |
// we temporaly disable PrintMallocFree here |
|
779 |
// as otherwise it'll lead to using of almost deleted |
|
780 |
// tty or defaultStream::instance in logging facility |
|
781 |
// of HeapFree(), see 6391258 |
|
782 |
DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);) |
|
783 |
if (tty != defaultStream::instance) { |
|
784 |
delete tty; |
|
785 |
} |
|
786 |
if (defaultStream::instance != NULL) { |
|
787 |
delete defaultStream::instance; |
|
788 |
} |
|
789 |
} |
|
790 |
tty = NULL; |
|
791 |
xtty = NULL; |
|
792 |
gclog_or_tty = NULL; |
|
793 |
defaultStream::instance = NULL; |
|
794 |
} |
|
795 |
||
796 |
// ostream_abort() is called by os::abort() when VM is about to die. |
|
797 |
void ostream_abort() { |
|
798 |
// Here we can't delete gclog_or_tty and tty, just flush their output |
|
799 |
if (gclog_or_tty) gclog_or_tty->flush(); |
|
800 |
if (tty) tty->flush(); |
|
801 |
||
802 |
if (defaultStream::instance != NULL) { |
|
803 |
static char buf[4096]; |
|
804 |
defaultStream::instance->finish_log_on_error(buf, sizeof(buf)); |
|
805 |
} |
|
806 |
} |
|
807 |
||
808 |
staticBufferStream::staticBufferStream(char* buffer, size_t buflen, |
|
809 |
outputStream *outer_stream) { |
|
810 |
_buffer = buffer; |
|
811 |
_buflen = buflen; |
|
812 |
_outer_stream = outer_stream; |
|
8887
1e9e60a225a6
7032849: 7022998 changes broke hs_err compile task print
kvn
parents:
8867
diff
changeset
|
813 |
// compile task prints time stamp relative to VM start |
1e9e60a225a6
7032849: 7022998 changes broke hs_err compile task print
kvn
parents:
8867
diff
changeset
|
814 |
_stamp.update_to(1); |
1 | 815 |
} |
816 |
||
817 |
void staticBufferStream::write(const char* c, size_t len) { |
|
818 |
_outer_stream->print_raw(c, (int)len); |
|
819 |
} |
|
820 |
||
821 |
void staticBufferStream::flush() { |
|
822 |
_outer_stream->flush(); |
|
823 |
} |
|
824 |
||
825 |
void staticBufferStream::print(const char* format, ...) { |
|
826 |
va_list ap; |
|
827 |
va_start(ap, format); |
|
828 |
size_t len; |
|
829 |
const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len); |
|
830 |
write(str, len); |
|
831 |
va_end(ap); |
|
832 |
} |
|
833 |
||
834 |
void staticBufferStream::print_cr(const char* format, ...) { |
|
835 |
va_list ap; |
|
836 |
va_start(ap, format); |
|
837 |
size_t len; |
|
838 |
const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len); |
|
839 |
write(str, len); |
|
840 |
va_end(ap); |
|
841 |
} |
|
842 |
||
843 |
void staticBufferStream::vprint(const char *format, va_list argptr) { |
|
844 |
size_t len; |
|
845 |
const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len); |
|
846 |
write(str, len); |
|
847 |
} |
|
848 |
||
849 |
void staticBufferStream::vprint_cr(const char* format, va_list argptr) { |
|
850 |
size_t len; |
|
851 |
const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len); |
|
852 |
write(str, len); |
|
853 |
} |
|
854 |
||
768 | 855 |
bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() { |
1 | 856 |
buffer_length = initial_size; |
857 |
buffer = NEW_C_HEAP_ARRAY(char, buffer_length); |
|
858 |
buffer_pos = 0; |
|
859 |
buffer_fixed = false; |
|
768 | 860 |
buffer_max = bufmax; |
1 | 861 |
} |
862 |
||
768 | 863 |
bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() { |
1 | 864 |
buffer_length = fixed_buffer_size; |
865 |
buffer = fixed_buffer; |
|
866 |
buffer_pos = 0; |
|
867 |
buffer_fixed = true; |
|
768 | 868 |
buffer_max = bufmax; |
1 | 869 |
} |
870 |
||
871 |
void bufferedStream::write(const char* s, size_t len) { |
|
768 | 872 |
|
873 |
if(buffer_pos + len > buffer_max) { |
|
874 |
flush(); |
|
875 |
} |
|
876 |
||
1 | 877 |
size_t end = buffer_pos + len; |
878 |
if (end >= buffer_length) { |
|
879 |
if (buffer_fixed) { |
|
880 |
// if buffer cannot resize, silently truncate |
|
881 |
len = buffer_length - buffer_pos - 1; |
|
882 |
} else { |
|
883 |
// For small overruns, double the buffer. For larger ones, |
|
884 |
// increase to the requested size. |
|
885 |
if (end < buffer_length * 2) { |
|
886 |
end = buffer_length * 2; |
|
887 |
} |
|
888 |
buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end); |
|
889 |
buffer_length = end; |
|
890 |
} |
|
891 |
} |
|
892 |
memcpy(buffer + buffer_pos, s, len); |
|
893 |
buffer_pos += len; |
|
894 |
update_position(s, len); |
|
895 |
} |
|
896 |
||
897 |
char* bufferedStream::as_string() { |
|
898 |
char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1); |
|
899 |
strncpy(copy, buffer, buffer_pos); |
|
900 |
copy[buffer_pos] = 0; // terminating null |
|
901 |
return copy; |
|
902 |
} |
|
903 |
||
904 |
bufferedStream::~bufferedStream() { |
|
905 |
if (!buffer_fixed) { |
|
906 |
FREE_C_HEAP_ARRAY(char, buffer); |
|
907 |
} |
|
908 |
} |
|
909 |
||
910 |
#ifndef PRODUCT |
|
911 |
||
912 |
#if defined(SOLARIS) || defined(LINUX) |
|
913 |
#include <sys/types.h> |
|
914 |
#include <sys/socket.h> |
|
915 |
#include <netinet/in.h> |
|
916 |
#include <arpa/inet.h> |
|
917 |
#endif |
|
918 |
||
919 |
// Network access |
|
768 | 920 |
networkStream::networkStream() : bufferedStream(1024*10, 1024*10) { |
1 | 921 |
|
922 |
_socket = -1; |
|
923 |
||
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
924 |
int result = os::socket(AF_INET, SOCK_STREAM, 0); |
1 | 925 |
if (result <= 0) { |
926 |
assert(false, "Socket could not be created!"); |
|
927 |
} else { |
|
928 |
_socket = result; |
|
929 |
} |
|
930 |
} |
|
931 |
||
932 |
int networkStream::read(char *buf, size_t len) { |
|
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
933 |
return os::recv(_socket, buf, (int)len, 0); |
1 | 934 |
} |
935 |
||
936 |
void networkStream::flush() { |
|
937 |
if (size() != 0) { |
|
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
938 |
int result = os::raw_send(_socket, (char *)base(), (int)size(), 0); |
768 | 939 |
assert(result != -1, "connection error"); |
940 |
assert(result == (int)size(), "didn't send enough data"); |
|
1 | 941 |
} |
942 |
reset(); |
|
943 |
} |
|
944 |
||
945 |
networkStream::~networkStream() { |
|
946 |
close(); |
|
947 |
} |
|
948 |
||
949 |
void networkStream::close() { |
|
950 |
if (_socket != -1) { |
|
951 |
flush(); |
|
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
952 |
os::socket_close(_socket); |
1 | 953 |
_socket = -1; |
954 |
} |
|
955 |
} |
|
956 |
||
957 |
bool networkStream::connect(const char *ip, short port) { |
|
958 |
||
959 |
struct sockaddr_in server; |
|
960 |
server.sin_family = AF_INET; |
|
961 |
server.sin_port = htons(port); |
|
962 |
||
963 |
server.sin_addr.s_addr = inet_addr(ip); |
|
253
cc8062372007
6679422: networkStream::connect() in ostream.cpp is not 64-bit clean
jcoomes
parents:
1
diff
changeset
|
964 |
if (server.sin_addr.s_addr == (uint32_t)-1) { |
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
965 |
struct hostent* host = os::get_host_by_name((char*)ip); |
1 | 966 |
if (host != NULL) { |
967 |
memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length); |
|
968 |
} else { |
|
969 |
return false; |
|
970 |
} |
|
971 |
} |
|
972 |
||
973 |
||
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
974 |
int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in)); |
1 | 975 |
return (result >= 0); |
976 |
} |
|
977 |
||
978 |
#endif |