author | kbarrett |
Tue, 27 Feb 2018 18:17:57 -0500 | |
changeset 49177 | eebf559c9e0d |
parent 48859 | 5a4d08efbad9 |
child 49360 | 886acec3b4c6 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
48859
5a4d08efbad9
6909265: assert(_OnDeck != Self->_MutexEvent,"invariant") with -XX:+PrintMallocFree
coleenp
parents:
47765
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, 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" |
47765
b7c7428eaab9
8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents:
47216
diff
changeset
|
26 |
#include "jvm.h" |
7397 | 27 |
#include "compiler/compileLog.hpp" |
28 |
#include "oops/oop.inline.hpp" |
|
29 |
#include "runtime/arguments.hpp" |
|
25468
5331df506290
8048241: Introduce umbrella header os.inline.hpp and clean up includes
goetz
parents:
25350
diff
changeset
|
30 |
#include "runtime/os.hpp" |
25715
d5a8dbdc5150
8049325: Introduce and clean up umbrella headers for the files in the cpu subdirectories.
goetz
parents:
25468
diff
changeset
|
31 |
#include "runtime/vm_version.hpp" |
7397 | 32 |
#include "utilities/defaultStream.hpp" |
27683
1d5707553fff
8064580: Move INCLUDE_CDS include section to the end of the include list
stefank
parents:
26135
diff
changeset
|
33 |
#include "utilities/macros.hpp" |
7397 | 34 |
#include "utilities/ostream.hpp" |
46589 | 35 |
#include "utilities/vmError.hpp" |
7397 | 36 |
#include "utilities/xmlstream.hpp" |
1 | 37 |
|
38 |
extern "C" void jio_print(const char* s); // Declarationtion of jvm method |
|
39 |
||
40 |
outputStream::outputStream(int width) { |
|
41 |
_width = width; |
|
42 |
_position = 0; |
|
43 |
_newlines = 0; |
|
44 |
_precount = 0; |
|
45 |
_indentation = 0; |
|
35869 | 46 |
_scratch = NULL; |
47 |
_scratch_len = 0; |
|
1 | 48 |
} |
49 |
||
50 |
outputStream::outputStream(int width, bool has_time_stamps) { |
|
51 |
_width = width; |
|
52 |
_position = 0; |
|
53 |
_newlines = 0; |
|
54 |
_precount = 0; |
|
55 |
_indentation = 0; |
|
35869 | 56 |
_scratch = NULL; |
57 |
_scratch_len = 0; |
|
1 | 58 |
if (has_time_stamps) _stamp.update(); |
59 |
} |
|
60 |
||
61 |
void outputStream::update_position(const char* s, size_t len) { |
|
62 |
for (size_t i = 0; i < len; i++) { |
|
63 |
char ch = s[i]; |
|
64 |
if (ch == '\n') { |
|
65 |
_newlines += 1; |
|
66 |
_precount += _position + 1; |
|
67 |
_position = 0; |
|
68 |
} else if (ch == '\t') { |
|
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
69 |
int tw = 8 - (_position & 7); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
70 |
_position += tw; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
71 |
_precount -= tw-1; // invariant: _precount + _position == total count |
1 | 72 |
} else { |
73 |
_position += 1; |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
// Execute a vsprintf, using the given buffer if necessary. |
|
79 |
// Return a pointer to the formatted string. |
|
80 |
const char* outputStream::do_vsnprintf(char* buffer, size_t buflen, |
|
81 |
const char* format, va_list ap, |
|
82 |
bool add_cr, |
|
83 |
size_t& result_len) { |
|
27471 | 84 |
assert(buflen >= 2, "buffer too small"); |
85 |
||
1 | 86 |
const char* result; |
87 |
if (add_cr) buflen--; |
|
88 |
if (!strchr(format, '%')) { |
|
89 |
// constant format string |
|
90 |
result = format; |
|
91 |
result_len = strlen(result); |
|
92 |
if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate |
|
93 |
} else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') { |
|
94 |
// trivial copy-through format string |
|
95 |
result = va_arg(ap, const char*); |
|
96 |
result_len = strlen(result); |
|
97 |
if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate |
|
27471 | 98 |
} else { |
49177
eebf559c9e0d
8196882: VS2017 Hotspot Defined vsnprintf Function Causes C2084 Already Defined Compilation Error
kbarrett
parents:
48859
diff
changeset
|
99 |
int written = os::vsnprintf(buffer, buflen, format, ap); |
eebf559c9e0d
8196882: VS2017 Hotspot Defined vsnprintf Function Causes C2084 Already Defined Compilation Error
kbarrett
parents:
48859
diff
changeset
|
100 |
assert(written >= 0, "vsnprintf encoding error"); |
1 | 101 |
result = buffer; |
49177
eebf559c9e0d
8196882: VS2017 Hotspot Defined vsnprintf Function Causes C2084 Already Defined Compilation Error
kbarrett
parents:
48859
diff
changeset
|
102 |
if ((size_t)written < buflen) { |
27471 | 103 |
result_len = written; |
104 |
} else { |
|
105 |
DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");) |
|
106 |
result_len = buflen - 1; |
|
107 |
} |
|
1 | 108 |
} |
109 |
if (add_cr) { |
|
110 |
if (result != buffer) { |
|
30281 | 111 |
memcpy(buffer, result, result_len); |
1 | 112 |
result = buffer; |
113 |
} |
|
114 |
buffer[result_len++] = '\n'; |
|
115 |
buffer[result_len] = 0; |
|
116 |
} |
|
117 |
return result; |
|
118 |
} |
|
119 |
||
35869 | 120 |
void outputStream::do_vsnprintf_and_write_with_automatic_buffer(const char* format, va_list ap, bool add_cr) { |
121 |
char buffer[O_BUFLEN]; |
|
122 |
size_t len; |
|
123 |
const char* str = do_vsnprintf(buffer, sizeof(buffer), format, ap, add_cr, len); |
|
124 |
write(str, len); |
|
125 |
} |
|
126 |
||
127 |
void outputStream::do_vsnprintf_and_write_with_scratch_buffer(const char* format, va_list ap, bool add_cr) { |
|
128 |
size_t len; |
|
129 |
const char* str = do_vsnprintf(_scratch, _scratch_len, format, ap, add_cr, len); |
|
130 |
write(str, len); |
|
131 |
} |
|
132 |
||
133 |
void outputStream::do_vsnprintf_and_write(const char* format, va_list ap, bool add_cr) { |
|
134 |
if (_scratch) { |
|
135 |
do_vsnprintf_and_write_with_scratch_buffer(format, ap, add_cr); |
|
136 |
} else { |
|
137 |
do_vsnprintf_and_write_with_automatic_buffer(format, ap, add_cr); |
|
138 |
} |
|
139 |
} |
|
140 |
||
1 | 141 |
void outputStream::print(const char* format, ...) { |
142 |
va_list ap; |
|
143 |
va_start(ap, format); |
|
35869 | 144 |
do_vsnprintf_and_write(format, ap, false); |
1 | 145 |
va_end(ap); |
146 |
} |
|
147 |
||
148 |
void outputStream::print_cr(const char* format, ...) { |
|
149 |
va_list ap; |
|
150 |
va_start(ap, format); |
|
35869 | 151 |
do_vsnprintf_and_write(format, ap, true); |
1 | 152 |
va_end(ap); |
153 |
} |
|
154 |
||
155 |
void outputStream::vprint(const char *format, va_list argptr) { |
|
35869 | 156 |
do_vsnprintf_and_write(format, argptr, false); |
1 | 157 |
} |
158 |
||
159 |
void outputStream::vprint_cr(const char* format, va_list argptr) { |
|
35869 | 160 |
do_vsnprintf_and_write(format, argptr, true); |
1 | 161 |
} |
162 |
||
163 |
void outputStream::fill_to(int col) { |
|
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
164 |
int need_fill = col - position(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
165 |
sp(need_fill); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
166 |
} |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
167 |
|
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
168 |
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
|
169 |
if (position() >= col + slop) |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
170 |
cr(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
171 |
int need_fill = col - position(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
172 |
if (need_fill < min_space) |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
173 |
need_fill = min_space; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
174 |
sp(need_fill); |
1 | 175 |
} |
176 |
||
177 |
void outputStream::put(char ch) { |
|
178 |
assert(ch != 0, "please fix call site"); |
|
179 |
char buf[] = { ch, '\0' }; |
|
180 |
write(buf, 1); |
|
181 |
} |
|
182 |
||
347
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
183 |
#define SP_USE_TABS false |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
184 |
|
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
185 |
void outputStream::sp(int count) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
186 |
if (count < 0) return; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
187 |
if (SP_USE_TABS && count >= 8) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
188 |
int target = position() + count; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
189 |
while (count >= 8) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
190 |
this->write("\t", 1); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
191 |
count -= 8; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
192 |
} |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
193 |
count = target - position(); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
194 |
} |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
195 |
while (count > 0) { |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
196 |
int nw = (count > 8) ? 8 : count; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
197 |
this->write(" ", nw); |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
198 |
count -= nw; |
df859fcca515
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
1
diff
changeset
|
199 |
} |
1 | 200 |
} |
201 |
||
202 |
void outputStream::cr() { |
|
203 |
this->write("\n", 1); |
|
204 |
} |
|
205 |
||
206 |
void outputStream::stamp() { |
|
207 |
if (! _stamp.is_updated()) { |
|
208 |
_stamp.update(); // start at 0 on first call to stamp() |
|
209 |
} |
|
210 |
||
211 |
// outputStream::stamp() may get called by ostream_abort(), use snprintf |
|
212 |
// to avoid allocating large stack buffer in print(). |
|
213 |
char buf[40]; |
|
214 |
jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds()); |
|
215 |
print_raw(buf); |
|
216 |
} |
|
217 |
||
1374
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
218 |
void outputStream::stamp(bool guard, |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
219 |
const char* prefix, |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
220 |
const char* suffix) { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
221 |
if (!guard) { |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
222 |
return; |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
223 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
224 |
print_raw(prefix); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
225 |
stamp(); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
226 |
print_raw(suffix); |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
227 |
} |
4c24294029a9
6711316: Open source the Garbage-First garbage collector
ysr
parents:
349
diff
changeset
|
228 |
|
1 | 229 |
void outputStream::date_stamp(bool guard, |
230 |
const char* prefix, |
|
231 |
const char* suffix) { |
|
232 |
if (!guard) { |
|
233 |
return; |
|
234 |
} |
|
235 |
print_raw(prefix); |
|
236 |
static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz"; |
|
237 |
static const int buffer_length = 32; |
|
238 |
char buffer[buffer_length]; |
|
239 |
const char* iso8601_result = os::iso8601_time(buffer, buffer_length); |
|
240 |
if (iso8601_result != NULL) { |
|
241 |
print_raw(buffer); |
|
242 |
} else { |
|
243 |
print_raw(error_time); |
|
244 |
} |
|
245 |
print_raw(suffix); |
|
246 |
return; |
|
247 |
} |
|
248 |
||
13476 | 249 |
outputStream& outputStream::indent() { |
1 | 250 |
while (_position < _indentation) sp(); |
13476 | 251 |
return *this; |
1 | 252 |
} |
253 |
||
254 |
void outputStream::print_jlong(jlong value) { |
|
15228
e92acc84ade3
7102489: RFE: cleanup jlong typedef on __APPLE__and _LLP64 systems.
hseigel
parents:
13964
diff
changeset
|
255 |
print(JLONG_FORMAT, value); |
1 | 256 |
} |
257 |
||
258 |
void outputStream::print_julong(julong value) { |
|
15228
e92acc84ade3
7102489: RFE: cleanup jlong typedef on __APPLE__and _LLP64 systems.
hseigel
parents:
13964
diff
changeset
|
259 |
print(JULONG_FORMAT, value); |
1 | 260 |
} |
261 |
||
13476 | 262 |
/** |
263 |
* This prints out hex data in a 'windbg' or 'xxd' form, where each line is: |
|
264 |
* <hex-address>: 8 * <hex-halfword> <ascii translation (optional)> |
|
265 |
* example: |
|
266 |
* 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000 .DOF............ |
|
267 |
* 0000010: 0000 0000 0000 0040 0000 0020 0000 0005 .......@... .... |
|
268 |
* 0000020: 0000 0000 0000 0040 0000 0000 0000 015d .......@.......] |
|
269 |
* ... |
|
270 |
* |
|
271 |
* indent is applied to each line. Ends with a CR. |
|
272 |
*/ |
|
273 |
void outputStream::print_data(void* data, size_t len, bool with_ascii) { |
|
274 |
size_t limit = (len + 16) / 16 * 16; |
|
275 |
for (size_t i = 0; i < limit; ++i) { |
|
276 |
if (i % 16 == 0) { |
|
31592
43f48e165466
8081202: Hotspot compile warning: "Invalid suffix on literal; C++11 requires a space between literal and identifier"
bpittore
parents:
30764
diff
changeset
|
277 |
indent().print(INTPTR_FORMAT_W(07) ":", i); |
13476 | 278 |
} |
279 |
if (i % 2 == 0) { |
|
280 |
print(" "); |
|
281 |
} |
|
282 |
if (i < len) { |
|
283 |
print("%02x", ((unsigned char*)data)[i]); |
|
284 |
} else { |
|
285 |
print(" "); |
|
286 |
} |
|
287 |
if ((i + 1) % 16 == 0) { |
|
288 |
if (with_ascii) { |
|
289 |
print(" "); |
|
290 |
for (size_t j = 0; j < 16; ++j) { |
|
291 |
size_t idx = i + j - 15; |
|
292 |
if (idx < len) { |
|
293 |
char c = ((char*)data)[idx]; |
|
294 |
print("%c", c >= 32 && c <= 126 ? c : '.'); |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
298 |
cr(); |
13476 | 299 |
} |
300 |
} |
|
301 |
} |
|
302 |
||
1 | 303 |
stringStream::stringStream(size_t initial_size) : outputStream() { |
304 |
buffer_length = initial_size; |
|
305 |
buffer = NEW_RESOURCE_ARRAY(char, buffer_length); |
|
306 |
buffer_pos = 0; |
|
307 |
buffer_fixed = false; |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
15803
diff
changeset
|
308 |
DEBUG_ONLY(rm = Thread::current()->current_resource_mark();) |
1 | 309 |
} |
310 |
||
311 |
// useful for output to fixed chunks of memory, such as performance counters |
|
312 |
stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() { |
|
313 |
buffer_length = fixed_buffer_size; |
|
314 |
buffer = fixed_buffer; |
|
315 |
buffer_pos = 0; |
|
316 |
buffer_fixed = true; |
|
317 |
} |
|
318 |
||
319 |
void stringStream::write(const char* s, size_t len) { |
|
320 |
size_t write_len = len; // number of non-null bytes to write |
|
321 |
size_t end = buffer_pos + len + 1; // position after write and final '\0' |
|
322 |
if (end > buffer_length) { |
|
323 |
if (buffer_fixed) { |
|
324 |
// if buffer cannot resize, silently truncate |
|
325 |
end = buffer_length; |
|
326 |
write_len = end - buffer_pos - 1; // leave room for the final '\0' |
|
327 |
} else { |
|
328 |
// For small overruns, double the buffer. For larger ones, |
|
329 |
// increase to the requested size. |
|
330 |
if (end < buffer_length * 2) { |
|
331 |
end = buffer_length * 2; |
|
332 |
} |
|
333 |
char* oldbuf = buffer; |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
15803
diff
changeset
|
334 |
assert(rm == NULL || Thread::current()->current_resource_mark() == rm, |
36570
9608a7830fe9
8149557: Resource mark breaks printing to string stream
goetz
parents:
35936
diff
changeset
|
335 |
"StringStream is re-allocated with a different ResourceMark. Current: " |
9608a7830fe9
8149557: Resource mark breaks printing to string stream
goetz
parents:
35936
diff
changeset
|
336 |
PTR_FORMAT " original: " PTR_FORMAT, |
9608a7830fe9
8149557: Resource mark breaks printing to string stream
goetz
parents:
35936
diff
changeset
|
337 |
p2i(Thread::current()->current_resource_mark()), p2i(rm)); |
1 | 338 |
buffer = NEW_RESOURCE_ARRAY(char, end); |
30281 | 339 |
if (buffer_pos > 0) { |
340 |
memcpy(buffer, oldbuf, buffer_pos); |
|
341 |
} |
|
1 | 342 |
buffer_length = end; |
343 |
} |
|
344 |
} |
|
345 |
// invariant: buffer is always null-terminated |
|
346 |
guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob"); |
|
30281 | 347 |
if (write_len > 0) { |
348 |
buffer[buffer_pos + write_len] = 0; |
|
349 |
memcpy(buffer + buffer_pos, s, write_len); |
|
350 |
buffer_pos += write_len; |
|
351 |
} |
|
1 | 352 |
|
353 |
// Note that the following does not depend on write_len. |
|
354 |
// This means that position and count get updated |
|
355 |
// even when overflow occurs. |
|
356 |
update_position(s, len); |
|
357 |
} |
|
358 |
||
359 |
char* stringStream::as_string() { |
|
19968 | 360 |
char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1); |
1 | 361 |
strncpy(copy, buffer, buffer_pos); |
362 |
copy[buffer_pos] = 0; // terminating null |
|
363 |
return copy; |
|
364 |
} |
|
365 |
||
366 |
stringStream::~stringStream() {} |
|
367 |
||
368 |
xmlStream* xtty; |
|
369 |
outputStream* tty; |
|
26135
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
370 |
CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive |
1 | 371 |
extern Mutex* tty_lock; |
372 |
||
19968 | 373 |
#define EXTRACHARLEN 32 |
374 |
#define CURRENTAPPX ".current" |
|
375 |
// convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS |
|
376 |
char* get_datetime_string(char *buf, size_t len) { |
|
377 |
os::local_time_string(buf, len); |
|
378 |
int i = (int)strlen(buf); |
|
35936
e401b754ae09
8148005: One byte may be corrupted by get_datetime_string()
asmotrak
parents:
35897
diff
changeset
|
379 |
while (--i >= 0) { |
19968 | 380 |
if (buf[i] == ' ') buf[i] = '_'; |
381 |
else if (buf[i] == ':') buf[i] = '-'; |
|
382 |
} |
|
383 |
return buf; |
|
384 |
} |
|
385 |
||
386 |
static const char* make_log_name_internal(const char* log_name, const char* force_directory, |
|
387 |
int pid, const char* tms) { |
|
388 |
const char* basename = log_name; |
|
389 |
char file_sep = os::file_separator()[0]; |
|
390 |
const char* cp; |
|
391 |
char pid_text[32]; |
|
392 |
||
393 |
for (cp = log_name; *cp != '\0'; cp++) { |
|
394 |
if (*cp == '/' || *cp == file_sep) { |
|
395 |
basename = cp + 1; |
|
396 |
} |
|
397 |
} |
|
398 |
const char* nametail = log_name; |
|
399 |
// Compute buffer length |
|
400 |
size_t buffer_length; |
|
401 |
if (force_directory != NULL) { |
|
402 |
buffer_length = strlen(force_directory) + strlen(os::file_separator()) + |
|
403 |
strlen(basename) + 1; |
|
404 |
} else { |
|
405 |
buffer_length = strlen(log_name) + 1; |
|
406 |
} |
|
407 |
||
408 |
const char* pts = strstr(basename, "%p"); |
|
409 |
int pid_pos = (pts == NULL) ? -1 : (pts - nametail); |
|
410 |
||
411 |
if (pid_pos >= 0) { |
|
412 |
jio_snprintf(pid_text, sizeof(pid_text), "pid%u", pid); |
|
413 |
buffer_length += strlen(pid_text); |
|
414 |
} |
|
415 |
||
416 |
pts = strstr(basename, "%t"); |
|
417 |
int tms_pos = (pts == NULL) ? -1 : (pts - nametail); |
|
418 |
if (tms_pos >= 0) { |
|
419 |
buffer_length += strlen(tms); |
|
420 |
} |
|
421 |
||
28509 | 422 |
// File name is too long. |
423 |
if (buffer_length > JVM_MAXPATHLEN) { |
|
424 |
return NULL; |
|
425 |
} |
|
426 |
||
19968 | 427 |
// Create big enough buffer. |
428 |
char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal); |
|
429 |
||
430 |
strcpy(buf, ""); |
|
431 |
if (force_directory != NULL) { |
|
432 |
strcat(buf, force_directory); |
|
433 |
strcat(buf, os::file_separator()); |
|
434 |
nametail = basename; // completely skip directory prefix |
|
435 |
} |
|
436 |
||
437 |
// who is first, %p or %t? |
|
438 |
int first = -1, second = -1; |
|
439 |
const char *p1st = NULL; |
|
440 |
const char *p2nd = NULL; |
|
441 |
||
442 |
if (pid_pos >= 0 && tms_pos >= 0) { |
|
443 |
// contains both %p and %t |
|
444 |
if (pid_pos < tms_pos) { |
|
445 |
// case foo%pbar%tmonkey.log |
|
446 |
first = pid_pos; |
|
447 |
p1st = pid_text; |
|
448 |
second = tms_pos; |
|
449 |
p2nd = tms; |
|
450 |
} else { |
|
451 |
// case foo%tbar%pmonkey.log |
|
452 |
first = tms_pos; |
|
453 |
p1st = tms; |
|
454 |
second = pid_pos; |
|
455 |
p2nd = pid_text; |
|
456 |
} |
|
457 |
} else if (pid_pos >= 0) { |
|
458 |
// contains %p only |
|
459 |
first = pid_pos; |
|
460 |
p1st = pid_text; |
|
461 |
} else if (tms_pos >= 0) { |
|
462 |
// contains %t only |
|
463 |
first = tms_pos; |
|
464 |
p1st = tms; |
|
465 |
} |
|
466 |
||
467 |
int buf_pos = (int)strlen(buf); |
|
468 |
const char* tail = nametail; |
|
469 |
||
470 |
if (first >= 0) { |
|
471 |
tail = nametail + first + 2; |
|
472 |
strncpy(&buf[buf_pos], nametail, first); |
|
473 |
strcpy(&buf[buf_pos + first], p1st); |
|
474 |
buf_pos = (int)strlen(buf); |
|
475 |
if (second >= 0) { |
|
476 |
strncpy(&buf[buf_pos], tail, second - first - 2); |
|
477 |
strcpy(&buf[buf_pos + second - first - 2], p2nd); |
|
478 |
tail = nametail + second + 2; |
|
479 |
} |
|
480 |
} |
|
481 |
strcat(buf, tail); // append rest of name, or all of name |
|
482 |
return buf; |
|
483 |
} |
|
484 |
||
35061 | 485 |
// log_name comes from -XX:LogFile=log_name or |
26135
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
486 |
// -XX:DumpLoadedClassList=<file_name> |
20691
63d27d9c8e79
8025849: Redundant "pid" in VM log file name (e.g. hotspot_pidpid12345.log)
vlivanov
parents:
20022
diff
changeset
|
487 |
// in log_name, %p => pid1234 and |
19968 | 488 |
// %t => YYYY-MM-DD_HH-MM-SS |
489 |
static const char* make_log_name(const char* log_name, const char* force_directory) { |
|
490 |
char timestr[32]; |
|
491 |
get_datetime_string(timestr, sizeof(timestr)); |
|
492 |
return make_log_name_internal(log_name, force_directory, os::current_process_id(), |
|
493 |
timestr); |
|
494 |
} |
|
495 |
||
1 | 496 |
fileStream::fileStream(const char* file_name) { |
497 |
_file = fopen(file_name, "w"); |
|
19968 | 498 |
if (_file != NULL) { |
499 |
_need_close = true; |
|
500 |
} else { |
|
37113 | 501 |
warning("Cannot open file %s due to %s\n", file_name, os::strerror(errno)); |
19968 | 502 |
_need_close = false; |
503 |
} |
|
1 | 504 |
} |
505 |
||
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
506 |
fileStream::fileStream(const char* file_name, const char* opentype) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
507 |
_file = fopen(file_name, opentype); |
19968 | 508 |
if (_file != NULL) { |
509 |
_need_close = true; |
|
510 |
} else { |
|
37113 | 511 |
warning("Cannot open file %s due to %s\n", file_name, os::strerror(errno)); |
19968 | 512 |
_need_close = false; |
513 |
} |
|
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
514 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
515 |
|
1 | 516 |
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
|
517 |
if (_file != NULL) { |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
518 |
// 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
|
519 |
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
|
520 |
} |
1 | 521 |
update_position(s, len); |
522 |
} |
|
523 |
||
8114
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
524 |
long fileStream::fileSize() { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
525 |
long size = -1; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
526 |
if (_file != NULL) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
527 |
long pos = ::ftell(_file); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
528 |
if (::fseek(_file, 0, SEEK_END) == 0) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
529 |
size = ::ftell(_file); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
530 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
531 |
::fseek(_file, pos, SEEK_SET); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
532 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
533 |
return size; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
534 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
535 |
|
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
536 |
char* fileStream::readln(char *data, int count ) { |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
537 |
char * ret = ::fgets(data, count, _file); |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
538 |
//Get rid of annoying \n char |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
539 |
data[::strlen(data)-1] = '\0'; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
540 |
return ret; |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
541 |
} |
340b5b8b544b
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
7405
diff
changeset
|
542 |
|
1 | 543 |
fileStream::~fileStream() { |
544 |
if (_file != NULL) { |
|
545 |
if (_need_close) fclose(_file); |
|
9990
c8683968c01b
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
8887
diff
changeset
|
546 |
_file = NULL; |
1 | 547 |
} |
548 |
} |
|
549 |
||
550 |
void fileStream::flush() { |
|
551 |
fflush(_file); |
|
552 |
} |
|
553 |
||
554 |
fdStream::fdStream(const char* file_name) { |
|
555 |
_fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
|
556 |
_need_close = true; |
|
557 |
} |
|
558 |
||
559 |
fdStream::~fdStream() { |
|
560 |
if (_fd != -1) { |
|
561 |
if (_need_close) close(_fd); |
|
562 |
_fd = -1; |
|
563 |
} |
|
564 |
} |
|
565 |
||
566 |
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
|
567 |
if (_fd != -1) { |
24b003a6fe46
6781583: Hotspot build fails on linux 64 bit platform with gcc 4.3.2
xlu
parents:
1388
diff
changeset
|
568 |
// 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
|
569 |
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
|
570 |
} |
1 | 571 |
update_position(s, len); |
572 |
} |
|
573 |
||
574 |
defaultStream* defaultStream::instance = NULL; |
|
575 |
int defaultStream::_output_fd = 1; |
|
576 |
int defaultStream::_error_fd = 2; |
|
577 |
FILE* defaultStream::_output_stream = stdout; |
|
578 |
FILE* defaultStream::_error_stream = stderr; |
|
579 |
||
580 |
#define LOG_MAJOR_VERSION 160 |
|
581 |
#define LOG_MINOR_VERSION 1 |
|
582 |
||
583 |
void defaultStream::init() { |
|
584 |
_inited = true; |
|
585 |
if (LogVMOutput || LogCompilation) { |
|
586 |
init_log(); |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
bool defaultStream::has_log_file() { |
|
591 |
// lazily create log file (at startup, LogVMOutput is false even |
|
592 |
// if +LogVMOutput is used, because the flags haven't been parsed yet) |
|
593 |
// For safer printing during fatal error handling, do not init logfile |
|
594 |
// if a VM error has been reported. |
|
46589 | 595 |
if (!_inited && !VMError::is_error_reported()) init(); |
1 | 596 |
return _log_file != NULL; |
597 |
} |
|
598 |
||
28509 | 599 |
fileStream* defaultStream::open_file(const char* log_name) { |
600 |
const char* try_name = make_log_name(log_name, NULL); |
|
601 |
if (try_name == NULL) { |
|
602 |
warning("Cannot open file %s: file name is too long.\n", log_name); |
|
603 |
return NULL; |
|
604 |
} |
|
605 |
||
606 |
fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name); |
|
607 |
FREE_C_HEAP_ARRAY(char, try_name); |
|
608 |
if (file->is_open()) { |
|
609 |
return file; |
|
610 |
} |
|
611 |
||
612 |
// Try again to open the file in the temp directory. |
|
613 |
delete file; |
|
614 |
char warnbuf[O_BUFLEN*2]; |
|
615 |
jio_snprintf(warnbuf, sizeof(warnbuf), "Warning: Cannot open log file: %s\n", log_name); |
|
616 |
// Note: This feature is for maintainer use only. No need for L10N. |
|
617 |
jio_print(warnbuf); |
|
618 |
try_name = make_log_name(log_name, os::get_temp_directory()); |
|
619 |
if (try_name == NULL) { |
|
620 |
warning("Cannot open file %s: file name is too long for directory %s.\n", log_name, os::get_temp_directory()); |
|
621 |
return NULL; |
|
622 |
} |
|
623 |
||
624 |
jio_snprintf(warnbuf, sizeof(warnbuf), |
|
625 |
"Warning: Forcing option -XX:LogFile=%s\n", try_name); |
|
626 |
jio_print(warnbuf); |
|
627 |
||
628 |
file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name); |
|
629 |
FREE_C_HEAP_ARRAY(char, try_name); |
|
630 |
if (file->is_open()) { |
|
631 |
return file; |
|
632 |
} |
|
633 |
||
634 |
delete file; |
|
635 |
return NULL; |
|
636 |
} |
|
637 |
||
1 | 638 |
void defaultStream::init_log() { |
639 |
// %%% Need a MutexLocker? |
|
20691
63d27d9c8e79
8025849: Redundant "pid" in VM log file name (e.g. hotspot_pidpid12345.log)
vlivanov
parents:
20022
diff
changeset
|
640 |
const char* log_name = LogFile != NULL ? LogFile : "hotspot_%p.log"; |
28509 | 641 |
fileStream* file = open_file(log_name); |
642 |
||
643 |
if (file != NULL) { |
|
644 |
_log_file = file; |
|
645 |
_outer_xmlStream = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file); |
|
646 |
start_log(); |
|
647 |
} else { |
|
648 |
// and leave xtty as NULL |
|
649 |
LogVMOutput = false; |
|
650 |
DisplayVMOutput = true; |
|
651 |
LogCompilation = false; |
|
1 | 652 |
} |
28509 | 653 |
} |
20010
c66a7254680c
8023134: Rename VM LogFile to hotspot_pid{pid}.log (was hotspot.log)
vlivanov
parents:
18928
diff
changeset
|
654 |
|
28509 | 655 |
void defaultStream::start_log() { |
656 |
xmlStream*xs = _outer_xmlStream; |
|
1 | 657 |
if (this == tty) xtty = xs; |
658 |
// Write XML header. |
|
659 |
xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>"); |
|
660 |
// (For now, don't bother to issue a DTD for this private format.) |
|
661 |
jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds(); |
|
662 |
// %%% Should be: jlong time_ms = os::start_time_milliseconds(), if |
|
663 |
// we ever get round to introduce that method on the os class |
|
664 |
xs->head("hotspot_log version='%d %d'" |
|
31592
43f48e165466
8081202: Hotspot compile warning: "Invalid suffix on literal; C++11 requires a space between literal and identifier"
bpittore
parents:
30764
diff
changeset
|
665 |
" process='%d' time_ms='" INT64_FORMAT "'", |
1 | 666 |
LOG_MAJOR_VERSION, LOG_MINOR_VERSION, |
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23517
diff
changeset
|
667 |
os::current_process_id(), (int64_t)time_ms); |
1 | 668 |
// Write VM version header immediately. |
669 |
xs->head("vm_version"); |
|
670 |
xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr(); |
|
671 |
xs->tail("name"); |
|
672 |
xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr(); |
|
673 |
xs->tail("release"); |
|
674 |
xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr(); |
|
675 |
xs->tail("info"); |
|
676 |
xs->tail("vm_version"); |
|
677 |
// Record information about the command-line invocation. |
|
678 |
xs->head("vm_arguments"); // Cf. Arguments::print_on() |
|
679 |
if (Arguments::num_jvm_flags() > 0) { |
|
680 |
xs->head("flags"); |
|
681 |
Arguments::print_jvm_flags_on(xs->text()); |
|
682 |
xs->tail("flags"); |
|
683 |
} |
|
684 |
if (Arguments::num_jvm_args() > 0) { |
|
685 |
xs->head("args"); |
|
686 |
Arguments::print_jvm_args_on(xs->text()); |
|
687 |
xs->tail("args"); |
|
688 |
} |
|
689 |
if (Arguments::java_command() != NULL) { |
|
690 |
xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command()); |
|
691 |
xs->tail("command"); |
|
692 |
} |
|
693 |
if (Arguments::sun_java_launcher() != NULL) { |
|
694 |
xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher()); |
|
695 |
xs->tail("launcher"); |
|
696 |
} |
|
697 |
if (Arguments::system_properties() != NULL) { |
|
698 |
xs->head("properties"); |
|
699 |
// Print it as a java-style property list. |
|
700 |
// System properties don't generally contain newlines, so don't bother with unparsing. |
|
30251
9b05c9cc4698
8077308: Fix warning: increase O_BUFLEN in ostream.hpp -- output truncated
dholmes
parents:
28509
diff
changeset
|
701 |
outputStream *text = xs->text(); |
1 | 702 |
for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) { |
36508 | 703 |
assert(p->key() != NULL, "p->key() is NULL"); |
40244
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
704 |
if (p->is_readable()) { |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
705 |
// Print in two stages to avoid problems with long |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
706 |
// keys/values. |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
707 |
text->print_raw(p->key()); |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
708 |
text->put('='); |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
709 |
assert(p->value() != NULL, "p->value() is NULL"); |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
710 |
text->print_raw_cr(p->value()); |
b3055c216762
8136930: Simplify use of module-system options by custom launchers
hseigel
parents:
37466
diff
changeset
|
711 |
} |
1 | 712 |
} |
713 |
xs->tail("properties"); |
|
714 |
} |
|
715 |
xs->tail("vm_arguments"); |
|
716 |
// tty output per se is grouped under the <tty>...</tty> element. |
|
717 |
xs->head("tty"); |
|
718 |
// All further non-markup text gets copied to the tty: |
|
719 |
xs->_text = this; // requires friend declaration! |
|
720 |
} |
|
721 |
||
722 |
// finish_log() is called during normal VM shutdown. finish_log_on_error() is |
|
723 |
// called by ostream_abort() after a fatal error. |
|
724 |
// |
|
725 |
void defaultStream::finish_log() { |
|
726 |
xmlStream* xs = _outer_xmlStream; |
|
727 |
xs->done("tty"); |
|
728 |
||
729 |
// Other log forks are appended here, at the End of Time: |
|
730 |
CompileLog::finish_log(xs->out()); // write compile logging, if any, now |
|
731 |
||
732 |
xs->done("hotspot_log"); |
|
733 |
xs->flush(); |
|
734 |
||
735 |
fileStream* file = _log_file; |
|
736 |
_log_file = NULL; |
|
737 |
||
738 |
delete _outer_xmlStream; |
|
739 |
_outer_xmlStream = NULL; |
|
740 |
||
741 |
file->flush(); |
|
742 |
delete file; |
|
743 |
} |
|
744 |
||
745 |
void defaultStream::finish_log_on_error(char *buf, int buflen) { |
|
746 |
xmlStream* xs = _outer_xmlStream; |
|
747 |
||
748 |
if (xs && xs->out()) { |
|
749 |
||
750 |
xs->done_raw("tty"); |
|
751 |
||
752 |
// Other log forks are appended here, at the End of Time: |
|
753 |
CompileLog::finish_log_on_error(xs->out(), buf, buflen); // write compile logging, if any, now |
|
754 |
||
755 |
xs->done_raw("hotspot_log"); |
|
756 |
xs->flush(); |
|
757 |
||
758 |
fileStream* file = _log_file; |
|
759 |
_log_file = NULL; |
|
760 |
_outer_xmlStream = NULL; |
|
761 |
||
762 |
if (file) { |
|
763 |
file->flush(); |
|
764 |
||
765 |
// Can't delete or close the file because delete and fclose aren't |
|
766 |
// async-safe. We are about to die, so leave it to the kernel. |
|
767 |
// delete file; |
|
768 |
} |
|
769 |
} |
|
770 |
} |
|
771 |
||
772 |
intx defaultStream::hold(intx writer_id) { |
|
773 |
bool has_log = has_log_file(); // check before locking |
|
774 |
if (// impossible, but who knows? |
|
775 |
writer_id == NO_WRITER || |
|
776 |
||
777 |
// bootstrap problem |
|
778 |
tty_lock == NULL || |
|
779 |
||
34633
2a6c7c7b30a7
8132510: Replace ThreadLocalStorage with compiler/language-based thread-local variables
dholmes
parents:
33736
diff
changeset
|
780 |
// can't grab a lock if current Thread isn't set |
2a6c7c7b30a7
8132510: Replace ThreadLocalStorage with compiler/language-based thread-local variables
dholmes
parents:
33736
diff
changeset
|
781 |
Thread::current_or_null() == NULL || |
1 | 782 |
|
783 |
// developer hook |
|
784 |
!SerializeVMOutput || |
|
785 |
||
786 |
// VM already unhealthy |
|
46589 | 787 |
VMError::is_error_reported() || |
1 | 788 |
|
789 |
// safepoint == global lock (for VM only) |
|
790 |
(SafepointSynchronize::is_synchronizing() && |
|
791 |
Thread::current()->is_VM_thread()) |
|
792 |
) { |
|
793 |
// do not attempt to lock unless we know the thread and the VM is healthy |
|
794 |
return NO_WRITER; |
|
795 |
} |
|
796 |
if (_writer == writer_id) { |
|
797 |
// already held, no need to re-grab the lock |
|
798 |
return NO_WRITER; |
|
799 |
} |
|
800 |
tty_lock->lock_without_safepoint_check(); |
|
801 |
// got the lock |
|
802 |
if (writer_id != _last_writer) { |
|
803 |
if (has_log) { |
|
804 |
_log_file->bol(); |
|
805 |
// output a hint where this output is coming from: |
|
13964 | 806 |
_log_file->print_cr("<writer thread='" UINTX_FORMAT "'/>", writer_id); |
1 | 807 |
} |
808 |
_last_writer = writer_id; |
|
809 |
} |
|
810 |
_writer = writer_id; |
|
811 |
return writer_id; |
|
812 |
} |
|
813 |
||
814 |
void defaultStream::release(intx holder) { |
|
815 |
if (holder == NO_WRITER) { |
|
816 |
// nothing to release: either a recursive lock, or we scribbled (too bad) |
|
817 |
return; |
|
818 |
} |
|
819 |
if (_writer != holder) { |
|
820 |
return; // already unlocked, perhaps via break_tty_lock_for_safepoint |
|
821 |
} |
|
822 |
_writer = NO_WRITER; |
|
823 |
tty_lock->unlock(); |
|
824 |
} |
|
825 |
||
826 |
||
827 |
// Yuck: jio_print does not accept char*/len. |
|
828 |
static void call_jio_print(const char* s, size_t len) { |
|
829 |
char buffer[O_BUFLEN+100]; |
|
830 |
if (len > sizeof(buffer)-1) { |
|
831 |
warning("increase O_BUFLEN in ostream.cpp -- output truncated"); |
|
832 |
len = sizeof(buffer)-1; |
|
833 |
} |
|
834 |
strncpy(buffer, s, len); |
|
835 |
buffer[len] = '\0'; |
|
836 |
jio_print(buffer); |
|
837 |
} |
|
838 |
||
839 |
||
840 |
void defaultStream::write(const char* s, size_t len) { |
|
841 |
intx thread_id = os::current_thread_id(); |
|
842 |
intx holder = hold(thread_id); |
|
843 |
||
844 |
if (DisplayVMOutput && |
|
845 |
(_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) { |
|
846 |
// print to output stream. It can be redirected by a vfprintf hook |
|
847 |
if (s[len] == '\0') { |
|
848 |
jio_print(s); |
|
849 |
} else { |
|
850 |
call_jio_print(s, len); |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
// print to log file |
|
855 |
if (has_log_file()) { |
|
856 |
int nl0 = _newlines; |
|
857 |
xmlTextStream::write(s, len); |
|
858 |
// flush the log file too, if there were any newlines |
|
859 |
if (nl0 != _newlines){ |
|
860 |
flush(); |
|
861 |
} |
|
862 |
} else { |
|
863 |
update_position(s, len); |
|
864 |
} |
|
865 |
||
866 |
release(holder); |
|
867 |
} |
|
868 |
||
869 |
intx ttyLocker::hold_tty() { |
|
870 |
if (defaultStream::instance == NULL) return defaultStream::NO_WRITER; |
|
871 |
intx thread_id = os::current_thread_id(); |
|
872 |
return defaultStream::instance->hold(thread_id); |
|
873 |
} |
|
874 |
||
875 |
void ttyLocker::release_tty(intx holder) { |
|
876 |
if (holder == defaultStream::NO_WRITER) return; |
|
877 |
defaultStream::instance->release(holder); |
|
878 |
} |
|
879 |
||
8333
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
880 |
bool ttyLocker::release_tty_if_locked() { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
881 |
intx thread_id = os::current_thread_id(); |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
882 |
if (defaultStream::instance->writer() == thread_id) { |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
883 |
// 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
|
884 |
// previously held. |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
885 |
release_tty(thread_id); |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
886 |
return true; |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
887 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
888 |
return false; |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
889 |
} |
11a7f6fc6419
7021531: lock ordering problems after fix for 6354181
never
parents:
8114
diff
changeset
|
890 |
|
1 | 891 |
void ttyLocker::break_tty_lock_for_safepoint(intx holder) { |
892 |
if (defaultStream::instance != NULL && |
|
893 |
defaultStream::instance->writer() == holder) { |
|
894 |
if (xtty != NULL) { |
|
895 |
xtty->print_cr("<!-- safepoint while printing -->"); |
|
896 |
} |
|
897 |
defaultStream::instance->release(holder); |
|
898 |
} |
|
899 |
// (else there was no lock to break) |
|
900 |
} |
|
901 |
||
902 |
void ostream_init() { |
|
903 |
if (defaultStream::instance == NULL) { |
|
13195 | 904 |
defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream(); |
1 | 905 |
tty = defaultStream::instance; |
906 |
||
907 |
// We want to ensure that time stamps in GC logs consider time 0 |
|
908 |
// the time when the JVM is initialized, not the first time we ask |
|
909 |
// for a time stamp. So, here, we explicitly update the time stamp |
|
910 |
// of tty. |
|
911 |
tty->time_stamp().update_to(1); |
|
912 |
} |
|
913 |
} |
|
914 |
||
915 |
void ostream_init_log() { |
|
916 |
// Note : this must be called AFTER ostream_init() |
|
917 |
||
26135
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
918 |
#if INCLUDE_CDS |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
919 |
// For -XX:DumpLoadedClassList=<file> option |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
920 |
if (DumpLoadedClassList != NULL) { |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
921 |
const char* list_name = make_log_name(DumpLoadedClassList, NULL); |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
922 |
classlist_file = new(ResourceObj::C_HEAP, mtInternal) |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
923 |
fileStream(list_name); |
27880
afb974a04396
8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents:
27471
diff
changeset
|
924 |
FREE_C_HEAP_ARRAY(char, list_name); |
26135
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
925 |
} |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
926 |
#endif |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
927 |
|
1 | 928 |
// If we haven't lazily initialized the logfile yet, do it now, |
929 |
// to avoid the possibility of lazy initialization during a VM |
|
930 |
// crash, which can affect the stability of the fatal error handler. |
|
931 |
defaultStream::instance->has_log_file(); |
|
932 |
} |
|
933 |
||
934 |
// ostream_exit() is called during normal VM exit to finish log files, flush |
|
935 |
// output and free resource. |
|
936 |
void ostream_exit() { |
|
937 |
static bool ostream_exit_called = false; |
|
938 |
if (ostream_exit_called) return; |
|
939 |
ostream_exit_called = true; |
|
26135
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
940 |
#if INCLUDE_CDS |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
941 |
if (classlist_file != NULL) { |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
942 |
delete classlist_file; |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
943 |
} |
82b516c550f7
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
25715
diff
changeset
|
944 |
#endif |
48859
5a4d08efbad9
6909265: assert(_OnDeck != Self->_MutexEvent,"invariant") with -XX:+PrintMallocFree
coleenp
parents:
47765
diff
changeset
|
945 |
if (tty != defaultStream::instance) { |
5a4d08efbad9
6909265: assert(_OnDeck != Self->_MutexEvent,"invariant") with -XX:+PrintMallocFree
coleenp
parents:
47765
diff
changeset
|
946 |
delete tty; |
5a4d08efbad9
6909265: assert(_OnDeck != Self->_MutexEvent,"invariant") with -XX:+PrintMallocFree
coleenp
parents:
47765
diff
changeset
|
947 |
} |
5a4d08efbad9
6909265: assert(_OnDeck != Self->_MutexEvent,"invariant") with -XX:+PrintMallocFree
coleenp
parents:
47765
diff
changeset
|
948 |
if (defaultStream::instance != NULL) { |
5a4d08efbad9
6909265: assert(_OnDeck != Self->_MutexEvent,"invariant") with -XX:+PrintMallocFree
coleenp
parents:
47765
diff
changeset
|
949 |
delete defaultStream::instance; |
1 | 950 |
} |
951 |
tty = NULL; |
|
952 |
xtty = NULL; |
|
953 |
defaultStream::instance = NULL; |
|
954 |
} |
|
955 |
||
956 |
// ostream_abort() is called by os::abort() when VM is about to die. |
|
957 |
void ostream_abort() { |
|
35061 | 958 |
// Here we can't delete tty, just flush its output |
1 | 959 |
if (tty) tty->flush(); |
960 |
||
961 |
if (defaultStream::instance != NULL) { |
|
962 |
static char buf[4096]; |
|
963 |
defaultStream::instance->finish_log_on_error(buf, sizeof(buf)); |
|
964 |
} |
|
965 |
} |
|
966 |
||
768 | 967 |
bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() { |
1 | 968 |
buffer_length = initial_size; |
13195 | 969 |
buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal); |
1 | 970 |
buffer_pos = 0; |
971 |
buffer_fixed = false; |
|
768 | 972 |
buffer_max = bufmax; |
1 | 973 |
} |
974 |
||
768 | 975 |
bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() { |
1 | 976 |
buffer_length = fixed_buffer_size; |
977 |
buffer = fixed_buffer; |
|
978 |
buffer_pos = 0; |
|
979 |
buffer_fixed = true; |
|
768 | 980 |
buffer_max = bufmax; |
1 | 981 |
} |
982 |
||
983 |
void bufferedStream::write(const char* s, size_t len) { |
|
768 | 984 |
|
985 |
if(buffer_pos + len > buffer_max) { |
|
986 |
flush(); |
|
987 |
} |
|
988 |
||
1 | 989 |
size_t end = buffer_pos + len; |
990 |
if (end >= buffer_length) { |
|
991 |
if (buffer_fixed) { |
|
992 |
// if buffer cannot resize, silently truncate |
|
993 |
len = buffer_length - buffer_pos - 1; |
|
994 |
} else { |
|
995 |
// For small overruns, double the buffer. For larger ones, |
|
996 |
// increase to the requested size. |
|
997 |
if (end < buffer_length * 2) { |
|
998 |
end = buffer_length * 2; |
|
999 |
} |
|
13195 | 1000 |
buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal); |
1 | 1001 |
buffer_length = end; |
1002 |
} |
|
1003 |
} |
|
1004 |
memcpy(buffer + buffer_pos, s, len); |
|
1005 |
buffer_pos += len; |
|
1006 |
update_position(s, len); |
|
1007 |
} |
|
1008 |
||
1009 |
char* bufferedStream::as_string() { |
|
1010 |
char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1); |
|
1011 |
strncpy(copy, buffer, buffer_pos); |
|
1012 |
copy[buffer_pos] = 0; // terminating null |
|
1013 |
return copy; |
|
1014 |
} |
|
1015 |
||
1016 |
bufferedStream::~bufferedStream() { |
|
1017 |
if (!buffer_fixed) { |
|
27880
afb974a04396
8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents:
27471
diff
changeset
|
1018 |
FREE_C_HEAP_ARRAY(char, buffer); |
1 | 1019 |
} |
1020 |
} |
|
1021 |
||
1022 |
#ifndef PRODUCT |
|
1023 |
||
22827 | 1024 |
#if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE) |
1 | 1025 |
#include <sys/types.h> |
1026 |
#include <sys/socket.h> |
|
1027 |
#include <netinet/in.h> |
|
1028 |
#include <arpa/inet.h> |
|
1029 |
#endif |
|
1030 |
||
1031 |
// Network access |
|
768 | 1032 |
networkStream::networkStream() : bufferedStream(1024*10, 1024*10) { |
1 | 1033 |
|
1034 |
_socket = -1; |
|
1035 |
||
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
1036 |
int result = os::socket(AF_INET, SOCK_STREAM, 0); |
1 | 1037 |
if (result <= 0) { |
1038 |
assert(false, "Socket could not be created!"); |
|
1039 |
} else { |
|
1040 |
_socket = result; |
|
1041 |
} |
|
1042 |
} |
|
1043 |
||
1044 |
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
|
1045 |
return os::recv(_socket, buf, (int)len, 0); |
1 | 1046 |
} |
1047 |
||
1048 |
void networkStream::flush() { |
|
1049 |
if (size() != 0) { |
|
11256
025cd1741566
7091417: recvfrom's 6th input should be of type socklen_t
phh
parents:
10565
diff
changeset
|
1050 |
int result = os::raw_send(_socket, (char *)base(), size(), 0); |
768 | 1051 |
assert(result != -1, "connection error"); |
1052 |
assert(result == (int)size(), "didn't send enough data"); |
|
1 | 1053 |
} |
1054 |
reset(); |
|
1055 |
} |
|
1056 |
||
1057 |
networkStream::~networkStream() { |
|
1058 |
close(); |
|
1059 |
} |
|
1060 |
||
1061 |
void networkStream::close() { |
|
1062 |
if (_socket != -1) { |
|
1063 |
flush(); |
|
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
1064 |
os::socket_close(_socket); |
1 | 1065 |
_socket = -1; |
1066 |
} |
|
1067 |
} |
|
1068 |
||
1069 |
bool networkStream::connect(const char *ip, short port) { |
|
1070 |
||
1071 |
struct sockaddr_in server; |
|
1072 |
server.sin_family = AF_INET; |
|
1073 |
server.sin_port = htons(port); |
|
1074 |
||
1075 |
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
|
1076 |
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
|
1077 |
struct hostent* host = os::get_host_by_name((char*)ip); |
1 | 1078 |
if (host != NULL) { |
1079 |
memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length); |
|
1080 |
} else { |
|
1081 |
return false; |
|
1082 |
} |
|
1083 |
} |
|
1084 |
||
1085 |
||
7405
e6fc8d3926f8
6348631: remove the use of the HPI library from Hotspot
ikrylov
parents:
7397
diff
changeset
|
1086 |
int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in)); |
1 | 1087 |
return (result >= 0); |
1088 |
} |
|
1089 |
||
1090 |
#endif |