src/share/vm/utilities/ostream.cpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9478
f3108e56b502
parent 9448
73d689add964
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
kevinw@9478 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "compiler/compileLog.hpp"
brutisso@6904 27 #include "gc_implementation/shared/gcId.hpp"
aoqi@0 28 #include "oops/oop.inline.hpp"
aoqi@0 29 #include "runtime/arguments.hpp"
kevinw@9478 30 #include "runtime/os.hpp"
aoqi@0 31 #include "utilities/defaultStream.hpp"
aoqi@0 32 #include "utilities/ostream.hpp"
aoqi@0 33 #include "utilities/top.hpp"
aoqi@0 34 #include "utilities/xmlstream.hpp"
aoqi@0 35 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 36 # include "os_linux.inline.hpp"
aoqi@0 37 #endif
aoqi@0 38 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 39 # include "os_solaris.inline.hpp"
aoqi@0 40 #endif
aoqi@0 41 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 42 # include "os_windows.inline.hpp"
aoqi@0 43 #endif
aoqi@0 44 #ifdef TARGET_OS_FAMILY_aix
aoqi@0 45 # include "os_aix.inline.hpp"
aoqi@0 46 #endif
aoqi@0 47 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 48 # include "os_bsd.inline.hpp"
aoqi@0 49 #endif
aoqi@0 50
aoqi@0 51 extern "C" void jio_print(const char* s); // Declarationtion of jvm method
aoqi@0 52
aoqi@0 53 outputStream::outputStream(int width) {
aoqi@0 54 _width = width;
aoqi@0 55 _position = 0;
aoqi@0 56 _newlines = 0;
aoqi@0 57 _precount = 0;
aoqi@0 58 _indentation = 0;
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 outputStream::outputStream(int width, bool has_time_stamps) {
aoqi@0 62 _width = width;
aoqi@0 63 _position = 0;
aoqi@0 64 _newlines = 0;
aoqi@0 65 _precount = 0;
aoqi@0 66 _indentation = 0;
aoqi@0 67 if (has_time_stamps) _stamp.update();
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 void outputStream::update_position(const char* s, size_t len) {
aoqi@0 71 for (size_t i = 0; i < len; i++) {
aoqi@0 72 char ch = s[i];
aoqi@0 73 if (ch == '\n') {
aoqi@0 74 _newlines += 1;
aoqi@0 75 _precount += _position + 1;
aoqi@0 76 _position = 0;
aoqi@0 77 } else if (ch == '\t') {
aoqi@0 78 int tw = 8 - (_position & 7);
aoqi@0 79 _position += tw;
aoqi@0 80 _precount -= tw-1; // invariant: _precount + _position == total count
aoqi@0 81 } else {
aoqi@0 82 _position += 1;
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 // Execute a vsprintf, using the given buffer if necessary.
aoqi@0 88 // Return a pointer to the formatted string.
aoqi@0 89 const char* outputStream::do_vsnprintf(char* buffer, size_t buflen,
aoqi@0 90 const char* format, va_list ap,
aoqi@0 91 bool add_cr,
aoqi@0 92 size_t& result_len) {
kevinw@9478 93 assert(buflen >= 2, "buffer too small");
kevinw@9478 94
aoqi@0 95 const char* result;
aoqi@0 96 if (add_cr) buflen--;
aoqi@0 97 if (!strchr(format, '%')) {
aoqi@0 98 // constant format string
aoqi@0 99 result = format;
aoqi@0 100 result_len = strlen(result);
aoqi@0 101 if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate
aoqi@0 102 } else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') {
aoqi@0 103 // trivial copy-through format string
aoqi@0 104 result = va_arg(ap, const char*);
aoqi@0 105 result_len = strlen(result);
aoqi@0 106 if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate
kevinw@9478 107 } else {
kevinw@9478 108 int written = os::vsnprintf(buffer, buflen, format, ap);
kevinw@9478 109 assert(written >= 0, "vsnprintf encoding error");
aoqi@0 110 result = buffer;
kevinw@9478 111 if ((size_t)written < buflen) {
kevinw@9478 112 result_len = written;
kevinw@9478 113 } else {
kevinw@9478 114 DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");)
kevinw@9478 115 result_len = buflen - 1;
kevinw@9478 116 }
aoqi@0 117 }
aoqi@0 118 if (add_cr) {
aoqi@0 119 if (result != buffer) {
kevinw@9478 120 memcpy(buffer, result, result_len);
aoqi@0 121 result = buffer;
aoqi@0 122 }
aoqi@0 123 buffer[result_len++] = '\n';
aoqi@0 124 buffer[result_len] = 0;
aoqi@0 125 }
aoqi@0 126 return result;
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 void outputStream::print(const char* format, ...) {
aoqi@0 130 char buffer[O_BUFLEN];
aoqi@0 131 va_list ap;
aoqi@0 132 va_start(ap, format);
aoqi@0 133 size_t len;
aoqi@0 134 const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len);
aoqi@0 135 write(str, len);
aoqi@0 136 va_end(ap);
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 void outputStream::print_cr(const char* format, ...) {
aoqi@0 140 char buffer[O_BUFLEN];
aoqi@0 141 va_list ap;
aoqi@0 142 va_start(ap, format);
aoqi@0 143 size_t len;
aoqi@0 144 const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len);
aoqi@0 145 write(str, len);
aoqi@0 146 va_end(ap);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 void outputStream::vprint(const char *format, va_list argptr) {
aoqi@0 150 char buffer[O_BUFLEN];
aoqi@0 151 size_t len;
aoqi@0 152 const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len);
aoqi@0 153 write(str, len);
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 void outputStream::vprint_cr(const char* format, va_list argptr) {
aoqi@0 157 char buffer[O_BUFLEN];
aoqi@0 158 size_t len;
aoqi@0 159 const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len);
aoqi@0 160 write(str, len);
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 void outputStream::fill_to(int col) {
aoqi@0 164 int need_fill = col - position();
aoqi@0 165 sp(need_fill);
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 void outputStream::move_to(int col, int slop, int min_space) {
aoqi@0 169 if (position() >= col + slop)
aoqi@0 170 cr();
aoqi@0 171 int need_fill = col - position();
aoqi@0 172 if (need_fill < min_space)
aoqi@0 173 need_fill = min_space;
aoqi@0 174 sp(need_fill);
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 void outputStream::put(char ch) {
aoqi@0 178 assert(ch != 0, "please fix call site");
aoqi@0 179 char buf[] = { ch, '\0' };
aoqi@0 180 write(buf, 1);
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 #define SP_USE_TABS false
aoqi@0 184
aoqi@0 185 void outputStream::sp(int count) {
aoqi@0 186 if (count < 0) return;
aoqi@0 187 if (SP_USE_TABS && count >= 8) {
aoqi@0 188 int target = position() + count;
aoqi@0 189 while (count >= 8) {
aoqi@0 190 this->write("\t", 1);
aoqi@0 191 count -= 8;
aoqi@0 192 }
aoqi@0 193 count = target - position();
aoqi@0 194 }
aoqi@0 195 while (count > 0) {
aoqi@0 196 int nw = (count > 8) ? 8 : count;
aoqi@0 197 this->write(" ", nw);
aoqi@0 198 count -= nw;
aoqi@0 199 }
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 void outputStream::cr() {
aoqi@0 203 this->write("\n", 1);
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 void outputStream::stamp() {
aoqi@0 207 if (! _stamp.is_updated()) {
aoqi@0 208 _stamp.update(); // start at 0 on first call to stamp()
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 // outputStream::stamp() may get called by ostream_abort(), use snprintf
aoqi@0 212 // to avoid allocating large stack buffer in print().
aoqi@0 213 char buf[40];
aoqi@0 214 jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds());
aoqi@0 215 print_raw(buf);
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 void outputStream::stamp(bool guard,
aoqi@0 219 const char* prefix,
aoqi@0 220 const char* suffix) {
aoqi@0 221 if (!guard) {
aoqi@0 222 return;
aoqi@0 223 }
aoqi@0 224 print_raw(prefix);
aoqi@0 225 stamp();
aoqi@0 226 print_raw(suffix);
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 void outputStream::date_stamp(bool guard,
aoqi@0 230 const char* prefix,
aoqi@0 231 const char* suffix) {
aoqi@0 232 if (!guard) {
aoqi@0 233 return;
aoqi@0 234 }
aoqi@0 235 print_raw(prefix);
aoqi@0 236 static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz";
aoqi@0 237 static const int buffer_length = 32;
aoqi@0 238 char buffer[buffer_length];
aoqi@0 239 const char* iso8601_result = os::iso8601_time(buffer, buffer_length);
aoqi@0 240 if (iso8601_result != NULL) {
aoqi@0 241 print_raw(buffer);
aoqi@0 242 } else {
aoqi@0 243 print_raw(error_time);
aoqi@0 244 }
aoqi@0 245 print_raw(suffix);
aoqi@0 246 return;
aoqi@0 247 }
aoqi@0 248
brutisso@6904 249 void outputStream::gclog_stamp(const GCId& gc_id) {
brutisso@6904 250 date_stamp(PrintGCDateStamps);
brutisso@6904 251 stamp(PrintGCTimeStamps);
brutisso@6904 252 if (PrintGCID) {
brutisso@6904 253 print("#%u: ", gc_id.id());
brutisso@6904 254 }
brutisso@6904 255 }
brutisso@6904 256
aoqi@0 257 outputStream& outputStream::indent() {
aoqi@0 258 while (_position < _indentation) sp();
aoqi@0 259 return *this;
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 void outputStream::print_jlong(jlong value) {
aoqi@0 263 print(JLONG_FORMAT, value);
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 void outputStream::print_julong(julong value) {
aoqi@0 267 print(JULONG_FORMAT, value);
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 /**
aoqi@0 271 * This prints out hex data in a 'windbg' or 'xxd' form, where each line is:
aoqi@0 272 * <hex-address>: 8 * <hex-halfword> <ascii translation (optional)>
aoqi@0 273 * example:
aoqi@0 274 * 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000 .DOF............
aoqi@0 275 * 0000010: 0000 0000 0000 0040 0000 0020 0000 0005 .......@... ....
aoqi@0 276 * 0000020: 0000 0000 0000 0040 0000 0000 0000 015d .......@.......]
aoqi@0 277 * ...
aoqi@0 278 *
aoqi@0 279 * indent is applied to each line. Ends with a CR.
aoqi@0 280 */
aoqi@0 281 void outputStream::print_data(void* data, size_t len, bool with_ascii) {
aoqi@0 282 size_t limit = (len + 16) / 16 * 16;
aoqi@0 283 for (size_t i = 0; i < limit; ++i) {
aoqi@0 284 if (i % 16 == 0) {
kevinw@9327 285 indent().print(SIZE_FORMAT_HEX_W(07) ":", i);
aoqi@0 286 }
aoqi@0 287 if (i % 2 == 0) {
aoqi@0 288 print(" ");
aoqi@0 289 }
aoqi@0 290 if (i < len) {
aoqi@0 291 print("%02x", ((unsigned char*)data)[i]);
aoqi@0 292 } else {
aoqi@0 293 print(" ");
aoqi@0 294 }
aoqi@0 295 if ((i + 1) % 16 == 0) {
aoqi@0 296 if (with_ascii) {
aoqi@0 297 print(" ");
aoqi@0 298 for (size_t j = 0; j < 16; ++j) {
aoqi@0 299 size_t idx = i + j - 15;
aoqi@0 300 if (idx < len) {
aoqi@0 301 char c = ((char*)data)[idx];
aoqi@0 302 print("%c", c >= 32 && c <= 126 ? c : '.');
aoqi@0 303 }
aoqi@0 304 }
aoqi@0 305 }
aoqi@0 306 cr();
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 stringStream::stringStream(size_t initial_size) : outputStream() {
aoqi@0 312 buffer_length = initial_size;
aoqi@0 313 buffer = NEW_RESOURCE_ARRAY(char, buffer_length);
aoqi@0 314 buffer_pos = 0;
aoqi@0 315 buffer_fixed = false;
aoqi@0 316 DEBUG_ONLY(rm = Thread::current()->current_resource_mark();)
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 // useful for output to fixed chunks of memory, such as performance counters
aoqi@0 320 stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() {
aoqi@0 321 buffer_length = fixed_buffer_size;
aoqi@0 322 buffer = fixed_buffer;
aoqi@0 323 buffer_pos = 0;
aoqi@0 324 buffer_fixed = true;
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 void stringStream::write(const char* s, size_t len) {
aoqi@0 328 size_t write_len = len; // number of non-null bytes to write
aoqi@0 329 size_t end = buffer_pos + len + 1; // position after write and final '\0'
aoqi@0 330 if (end > buffer_length) {
aoqi@0 331 if (buffer_fixed) {
aoqi@0 332 // if buffer cannot resize, silently truncate
aoqi@0 333 end = buffer_length;
aoqi@0 334 write_len = end - buffer_pos - 1; // leave room for the final '\0'
aoqi@0 335 } else {
aoqi@0 336 // For small overruns, double the buffer. For larger ones,
aoqi@0 337 // increase to the requested size.
aoqi@0 338 if (end < buffer_length * 2) {
aoqi@0 339 end = buffer_length * 2;
aoqi@0 340 }
aoqi@0 341 char* oldbuf = buffer;
aoqi@0 342 assert(rm == NULL || Thread::current()->current_resource_mark() == rm,
aoqi@0 343 "stringStream is re-allocated with a different ResourceMark");
aoqi@0 344 buffer = NEW_RESOURCE_ARRAY(char, end);
aoqi@0 345 strncpy(buffer, oldbuf, buffer_pos);
aoqi@0 346 buffer_length = end;
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349 // invariant: buffer is always null-terminated
aoqi@0 350 guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
aoqi@0 351 buffer[buffer_pos + write_len] = 0;
aoqi@0 352 strncpy(buffer + buffer_pos, s, write_len);
aoqi@0 353 buffer_pos += write_len;
aoqi@0 354
aoqi@0 355 // Note that the following does not depend on write_len.
aoqi@0 356 // This means that position and count get updated
aoqi@0 357 // even when overflow occurs.
aoqi@0 358 update_position(s, len);
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 char* stringStream::as_string() {
aoqi@0 362 char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
aoqi@0 363 strncpy(copy, buffer, buffer_pos);
aoqi@0 364 copy[buffer_pos] = 0; // terminating null
aoqi@0 365 return copy;
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 stringStream::~stringStream() {}
aoqi@0 369
aoqi@0 370 xmlStream* xtty;
aoqi@0 371 outputStream* tty;
aoqi@0 372 outputStream* gclog_or_tty;
iklam@7089 373 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
aoqi@0 374 extern Mutex* tty_lock;
aoqi@0 375
aoqi@0 376 #define EXTRACHARLEN 32
aoqi@0 377 #define CURRENTAPPX ".current"
aoqi@0 378 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS
aoqi@0 379 char* get_datetime_string(char *buf, size_t len) {
aoqi@0 380 os::local_time_string(buf, len);
aoqi@0 381 int i = (int)strlen(buf);
aoqi@0 382 while (i-- >= 0) {
aoqi@0 383 if (buf[i] == ' ') buf[i] = '_';
aoqi@0 384 else if (buf[i] == ':') buf[i] = '-';
aoqi@0 385 }
aoqi@0 386 return buf;
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 static const char* make_log_name_internal(const char* log_name, const char* force_directory,
aoqi@0 390 int pid, const char* tms) {
aoqi@0 391 const char* basename = log_name;
aoqi@0 392 char file_sep = os::file_separator()[0];
aoqi@0 393 const char* cp;
aoqi@0 394 char pid_text[32];
aoqi@0 395
aoqi@0 396 for (cp = log_name; *cp != '\0'; cp++) {
aoqi@0 397 if (*cp == '/' || *cp == file_sep) {
aoqi@0 398 basename = cp + 1;
aoqi@0 399 }
aoqi@0 400 }
aoqi@0 401 const char* nametail = log_name;
aoqi@0 402 // Compute buffer length
aoqi@0 403 size_t buffer_length;
aoqi@0 404 if (force_directory != NULL) {
aoqi@0 405 buffer_length = strlen(force_directory) + strlen(os::file_separator()) +
aoqi@0 406 strlen(basename) + 1;
aoqi@0 407 } else {
aoqi@0 408 buffer_length = strlen(log_name) + 1;
aoqi@0 409 }
aoqi@0 410
aoqi@0 411 const char* pts = strstr(basename, "%p");
aoqi@0 412 int pid_pos = (pts == NULL) ? -1 : (pts - nametail);
aoqi@0 413
aoqi@0 414 if (pid_pos >= 0) {
aoqi@0 415 jio_snprintf(pid_text, sizeof(pid_text), "pid%u", pid);
aoqi@0 416 buffer_length += strlen(pid_text);
aoqi@0 417 }
aoqi@0 418
aoqi@0 419 pts = strstr(basename, "%t");
aoqi@0 420 int tms_pos = (pts == NULL) ? -1 : (pts - nametail);
aoqi@0 421 if (tms_pos >= 0) {
aoqi@0 422 buffer_length += strlen(tms);
aoqi@0 423 }
aoqi@0 424
brutisso@7448 425 // File name is too long.
brutisso@7448 426 if (buffer_length > JVM_MAXPATHLEN) {
brutisso@7448 427 return NULL;
brutisso@7448 428 }
brutisso@7448 429
aoqi@0 430 // Create big enough buffer.
aoqi@0 431 char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
aoqi@0 432
aoqi@0 433 strcpy(buf, "");
aoqi@0 434 if (force_directory != NULL) {
aoqi@0 435 strcat(buf, force_directory);
aoqi@0 436 strcat(buf, os::file_separator());
aoqi@0 437 nametail = basename; // completely skip directory prefix
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 // who is first, %p or %t?
aoqi@0 441 int first = -1, second = -1;
aoqi@0 442 const char *p1st = NULL;
aoqi@0 443 const char *p2nd = NULL;
aoqi@0 444
aoqi@0 445 if (pid_pos >= 0 && tms_pos >= 0) {
aoqi@0 446 // contains both %p and %t
aoqi@0 447 if (pid_pos < tms_pos) {
aoqi@0 448 // case foo%pbar%tmonkey.log
aoqi@0 449 first = pid_pos;
aoqi@0 450 p1st = pid_text;
aoqi@0 451 second = tms_pos;
aoqi@0 452 p2nd = tms;
aoqi@0 453 } else {
aoqi@0 454 // case foo%tbar%pmonkey.log
aoqi@0 455 first = tms_pos;
aoqi@0 456 p1st = tms;
aoqi@0 457 second = pid_pos;
aoqi@0 458 p2nd = pid_text;
aoqi@0 459 }
aoqi@0 460 } else if (pid_pos >= 0) {
aoqi@0 461 // contains %p only
aoqi@0 462 first = pid_pos;
aoqi@0 463 p1st = pid_text;
aoqi@0 464 } else if (tms_pos >= 0) {
aoqi@0 465 // contains %t only
aoqi@0 466 first = tms_pos;
aoqi@0 467 p1st = tms;
aoqi@0 468 }
aoqi@0 469
aoqi@0 470 int buf_pos = (int)strlen(buf);
aoqi@0 471 const char* tail = nametail;
aoqi@0 472
aoqi@0 473 if (first >= 0) {
aoqi@0 474 tail = nametail + first + 2;
aoqi@0 475 strncpy(&buf[buf_pos], nametail, first);
aoqi@0 476 strcpy(&buf[buf_pos + first], p1st);
aoqi@0 477 buf_pos = (int)strlen(buf);
aoqi@0 478 if (second >= 0) {
aoqi@0 479 strncpy(&buf[buf_pos], tail, second - first - 2);
aoqi@0 480 strcpy(&buf[buf_pos + second - first - 2], p2nd);
aoqi@0 481 tail = nametail + second + 2;
aoqi@0 482 }
aoqi@0 483 }
aoqi@0 484 strcat(buf, tail); // append rest of name, or all of name
aoqi@0 485 return buf;
aoqi@0 486 }
aoqi@0 487
iklam@7089 488 // log_name comes from -XX:LogFile=log_name, -Xloggc:log_name or
iklam@7089 489 // -XX:DumpLoadedClassList=<file_name>
aoqi@0 490 // in log_name, %p => pid1234 and
aoqi@0 491 // %t => YYYY-MM-DD_HH-MM-SS
aoqi@0 492 static const char* make_log_name(const char* log_name, const char* force_directory) {
aoqi@0 493 char timestr[32];
aoqi@0 494 get_datetime_string(timestr, sizeof(timestr));
aoqi@0 495 return make_log_name_internal(log_name, force_directory, os::current_process_id(),
aoqi@0 496 timestr);
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 #ifndef PRODUCT
aoqi@0 500 void test_loggc_filename() {
aoqi@0 501 int pid;
aoqi@0 502 char tms[32];
brutisso@7448 503 char i_result[JVM_MAXPATHLEN];
aoqi@0 504 const char* o_result;
aoqi@0 505 get_datetime_string(tms, sizeof(tms));
aoqi@0 506 pid = os::current_process_id();
aoqi@0 507
aoqi@0 508 // test.log
brutisso@7448 509 jio_snprintf(i_result, JVM_MAXPATHLEN, "test.log", tms);
aoqi@0 510 o_result = make_log_name_internal("test.log", NULL, pid, tms);
aoqi@0 511 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test.log\", NULL)");
aoqi@0 512 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
aoqi@0 513
aoqi@0 514 // test-%t-%p.log
brutisso@7448 515 jio_snprintf(i_result, JVM_MAXPATHLEN, "test-%s-pid%u.log", tms, pid);
aoqi@0 516 o_result = make_log_name_internal("test-%t-%p.log", NULL, pid, tms);
aoqi@0 517 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t-%%p.log\", NULL)");
aoqi@0 518 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
aoqi@0 519
aoqi@0 520 // test-%t%p.log
brutisso@7448 521 jio_snprintf(i_result, JVM_MAXPATHLEN, "test-%spid%u.log", tms, pid);
aoqi@0 522 o_result = make_log_name_internal("test-%t%p.log", NULL, pid, tms);
aoqi@0 523 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t%%p.log\", NULL)");
aoqi@0 524 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
aoqi@0 525
aoqi@0 526 // %p%t.log
brutisso@7448 527 jio_snprintf(i_result, JVM_MAXPATHLEN, "pid%u%s.log", pid, tms);
aoqi@0 528 o_result = make_log_name_internal("%p%t.log", NULL, pid, tms);
aoqi@0 529 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p%%t.log\", NULL)");
aoqi@0 530 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
aoqi@0 531
aoqi@0 532 // %p-test.log
brutisso@7448 533 jio_snprintf(i_result, JVM_MAXPATHLEN, "pid%u-test.log", pid);
aoqi@0 534 o_result = make_log_name_internal("%p-test.log", NULL, pid, tms);
aoqi@0 535 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p-test.log\", NULL)");
aoqi@0 536 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
aoqi@0 537
aoqi@0 538 // %t.log
brutisso@7448 539 jio_snprintf(i_result, JVM_MAXPATHLEN, "%s.log", tms);
aoqi@0 540 o_result = make_log_name_internal("%t.log", NULL, pid, tms);
aoqi@0 541 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%t.log\", NULL)");
aoqi@0 542 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
brutisso@7448 543
brutisso@7448 544 {
brutisso@7448 545 // longest filename
brutisso@7448 546 char longest_name[JVM_MAXPATHLEN];
brutisso@7448 547 memset(longest_name, 'a', sizeof(longest_name));
brutisso@7448 548 longest_name[JVM_MAXPATHLEN - 1] = '\0';
brutisso@7448 549 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
brutisso@7448 550 assert(strcmp(longest_name, o_result) == 0, err_msg("longest name does not match. expected '%s' but got '%s'", longest_name, o_result));
brutisso@7448 551 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
brutisso@7448 552 }
brutisso@7448 553
brutisso@7448 554 {
brutisso@7448 555 // too long file name
brutisso@7448 556 char too_long_name[JVM_MAXPATHLEN + 100];
brutisso@7448 557 int too_long_length = sizeof(too_long_name);
brutisso@7448 558 memset(too_long_name, 'a', too_long_length);
brutisso@7448 559 too_long_name[too_long_length - 1] = '\0';
brutisso@7448 560 o_result = make_log_name_internal((const char*)&too_long_name, NULL, pid, tms);
brutisso@7448 561 assert(o_result == NULL, err_msg("Too long file name should return NULL, but got '%s'", o_result));
brutisso@7448 562 }
brutisso@7448 563
brutisso@7448 564 {
brutisso@7448 565 // too long with timestamp
brutisso@7448 566 char longest_name[JVM_MAXPATHLEN];
brutisso@7448 567 memset(longest_name, 'a', JVM_MAXPATHLEN);
brutisso@7448 568 longest_name[JVM_MAXPATHLEN - 3] = '%';
brutisso@7448 569 longest_name[JVM_MAXPATHLEN - 2] = 't';
brutisso@7448 570 longest_name[JVM_MAXPATHLEN - 1] = '\0';
brutisso@7448 571 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
brutisso@7448 572 assert(o_result == NULL, err_msg("Too long file name after timestamp expansion should return NULL, but got '%s'", o_result));
brutisso@7448 573 }
brutisso@7448 574
brutisso@7448 575 {
brutisso@7448 576 // too long with pid
brutisso@7448 577 char longest_name[JVM_MAXPATHLEN];
brutisso@7448 578 memset(longest_name, 'a', JVM_MAXPATHLEN);
brutisso@7448 579 longest_name[JVM_MAXPATHLEN - 3] = '%';
brutisso@7448 580 longest_name[JVM_MAXPATHLEN - 2] = 'p';
brutisso@7448 581 longest_name[JVM_MAXPATHLEN - 1] = '\0';
brutisso@7448 582 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
brutisso@7448 583 assert(o_result == NULL, err_msg("Too long file name after pid expansion should return NULL, but got '%s'", o_result));
brutisso@7448 584 }
aoqi@0 585 }
kevinw@9478 586
kevinw@9478 587 //////////////////////////////////////////////////////////////////////////////
kevinw@9478 588 // Test os::vsnprintf and friends.
kevinw@9478 589
kevinw@9478 590 void check_snprintf_result(int expected, size_t limit, int actual, bool expect_count) {
kevinw@9478 591 if (expect_count || ((size_t)expected < limit)) {
kevinw@9478 592 assert(expected == actual, "snprintf result not expected value");
kevinw@9478 593 } else {
kevinw@9478 594 // Make this check more permissive for jdk8u, don't assert that actual == 0.
kevinw@9478 595 // e.g. jio_vsnprintf_wrapper and jio_snprintf return -1 when expected >= limit
kevinw@9478 596 if (expected >= (int) limit) {
kevinw@9478 597 assert(actual == -1, "snprintf result should be -1 for expected >= limit");
kevinw@9478 598 } else {
kevinw@9478 599 assert(actual > 0, "snprintf result should be >0 for expected < limit");
kevinw@9478 600 }
kevinw@9478 601 }
kevinw@9478 602 }
kevinw@9478 603
kevinw@9478 604 // PrintFn is expected to be int (*)(char*, size_t, const char*, ...).
kevinw@9478 605 // But jio_snprintf is a C-linkage function with that signature, which
kevinw@9478 606 // has a different type on some platforms (like Solaris).
kevinw@9478 607 template<typename PrintFn>
kevinw@9478 608 void test_snprintf(PrintFn pf, bool expect_count) {
kevinw@9478 609 const char expected[] = "abcdefghijklmnopqrstuvwxyz";
kevinw@9478 610 const int expected_len = sizeof(expected) - 1;
kevinw@9478 611 const size_t padding_size = 10;
kevinw@9478 612 char buffer[2 * (sizeof(expected) + padding_size)];
kevinw@9478 613 char check_buffer[sizeof(buffer)];
kevinw@9478 614 const char check_char = '1'; // Something not in expected.
kevinw@9478 615 memset(check_buffer, check_char, sizeof(check_buffer));
kevinw@9478 616 const size_t sizes_to_test[] = {
kevinw@9478 617 sizeof(buffer) - padding_size, // Fits, with plenty of space to spare.
kevinw@9478 618 sizeof(buffer)/2, // Fits, with space to spare.
kevinw@9478 619 sizeof(buffer)/4, // Doesn't fit.
kevinw@9478 620 sizeof(expected) + padding_size + 1, // Fits, with a little room to spare
kevinw@9478 621 sizeof(expected) + padding_size, // Fits exactly.
kevinw@9478 622 sizeof(expected) + padding_size - 1, // Doesn't quite fit.
kevinw@9478 623 2, // One char + terminating NUL.
kevinw@9478 624 1, // Only space for terminating NUL.
kevinw@9478 625 0 }; // No space at all.
kevinw@9478 626 for (unsigned i = 0; i < ARRAY_SIZE(sizes_to_test); ++i) {
kevinw@9478 627 memset(buffer, check_char, sizeof(buffer)); // To catch stray writes.
kevinw@9478 628 size_t test_size = sizes_to_test[i];
kevinw@9478 629 ResourceMark rm;
kevinw@9478 630 stringStream s;
kevinw@9478 631 s.print("test_size: " SIZE_FORMAT, test_size);
kevinw@9478 632 size_t prefix_size = padding_size;
kevinw@9478 633 guarantee(test_size <= (sizeof(buffer) - prefix_size), "invariant");
kevinw@9478 634 size_t write_size = MIN2(sizeof(expected), test_size);
kevinw@9478 635 size_t suffix_size = sizeof(buffer) - prefix_size - write_size;
kevinw@9478 636 char* write_start = buffer + prefix_size;
kevinw@9478 637 char* write_end = write_start + write_size;
kevinw@9478 638
kevinw@9478 639 int result = pf(write_start, test_size, "%s", expected);
kevinw@9478 640
kevinw@9478 641 check_snprintf_result(expected_len, test_size, result, expect_count);
kevinw@9478 642
kevinw@9478 643 // Verify expected output.
kevinw@9478 644 if (test_size > 0) {
kevinw@9478 645 assert(0 == strncmp(write_start, expected, write_size - 1), "strncmp failure");
kevinw@9478 646 // Verify terminating NUL of output.
kevinw@9478 647 assert('\0' == write_start[write_size - 1], "null terminator failure");
kevinw@9478 648 } else {
kevinw@9478 649 guarantee(test_size == 0, "invariant");
kevinw@9478 650 guarantee(write_size == 0, "invariant");
kevinw@9478 651 guarantee(prefix_size + suffix_size == sizeof(buffer), "invariant");
kevinw@9478 652 guarantee(write_start == write_end, "invariant");
kevinw@9478 653 }
kevinw@9478 654
kevinw@9478 655 // Verify no scribbling on prefix or suffix.
kevinw@9478 656 assert(0 == strncmp(buffer, check_buffer, prefix_size), "prefix scribble");
kevinw@9478 657 assert(0 == strncmp(write_end, check_buffer, suffix_size), "suffix scribble");
kevinw@9478 658 }
kevinw@9478 659
kevinw@9478 660 // Special case of 0-length buffer with empty (except for terminator) output.
kevinw@9478 661 check_snprintf_result(0, 0, pf(NULL, 0, "%s", ""), expect_count);
kevinw@9478 662 check_snprintf_result(0, 0, pf(NULL, 0, ""), expect_count);
kevinw@9478 663 }
kevinw@9478 664
kevinw@9478 665 // This is probably equivalent to os::snprintf, but we're being
kevinw@9478 666 // explicit about what we're testing here.
kevinw@9478 667 static int vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {
kevinw@9478 668 va_list args;
kevinw@9478 669 va_start(args, fmt);
kevinw@9478 670 int result = os::vsnprintf(buf, len, fmt, args);
kevinw@9478 671 va_end(args);
kevinw@9478 672 return result;
kevinw@9478 673 }
kevinw@9478 674
kevinw@9478 675 // These are declared in jvm.h; test here, with related functions.
kevinw@9478 676 extern "C" {
kevinw@9478 677 int jio_vsnprintf(char*, size_t, const char*, va_list);
kevinw@9478 678 int jio_snprintf(char*, size_t, const char*, ...);
kevinw@9478 679 }
kevinw@9478 680
kevinw@9478 681 // This is probably equivalent to jio_snprintf, but we're being
kevinw@9478 682 // explicit about what we're testing here.
kevinw@9478 683 static int jio_vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {
kevinw@9478 684 va_list args;
kevinw@9478 685 va_start(args, fmt);
kevinw@9478 686 int result = jio_vsnprintf(buf, len, fmt, args);
kevinw@9478 687 va_end(args);
kevinw@9478 688 return result;
kevinw@9478 689 }
kevinw@9478 690
kevinw@9478 691 void test_snprintf() {
kevinw@9478 692 test_snprintf(vsnprintf_wrapper, true);
kevinw@9478 693 test_snprintf(os::snprintf, true);
kevinw@9478 694 test_snprintf(jio_vsnprintf_wrapper, false); // jio_vsnprintf returns -1 on error including exceeding buffer size
kevinw@9478 695 test_snprintf(jio_snprintf, false); // jio_snprintf calls jio_vsnprintf
kevinw@9478 696 }
aoqi@0 697 #endif // PRODUCT
aoqi@0 698
aoqi@0 699 fileStream::fileStream(const char* file_name) {
aoqi@0 700 _file = fopen(file_name, "w");
aoqi@0 701 if (_file != NULL) {
aoqi@0 702 _need_close = true;
aoqi@0 703 } else {
aoqi@0 704 warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
aoqi@0 705 _need_close = false;
aoqi@0 706 }
aoqi@0 707 }
aoqi@0 708
aoqi@0 709 fileStream::fileStream(const char* file_name, const char* opentype) {
aoqi@0 710 _file = fopen(file_name, opentype);
aoqi@0 711 if (_file != NULL) {
aoqi@0 712 _need_close = true;
aoqi@0 713 } else {
aoqi@0 714 warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
aoqi@0 715 _need_close = false;
aoqi@0 716 }
aoqi@0 717 }
aoqi@0 718
aoqi@0 719 void fileStream::write(const char* s, size_t len) {
aoqi@0 720 if (_file != NULL) {
aoqi@0 721 // Make an unused local variable to avoid warning from gcc 4.x compiler.
aoqi@0 722 size_t count = fwrite(s, 1, len, _file);
aoqi@0 723 }
aoqi@0 724 update_position(s, len);
aoqi@0 725 }
aoqi@0 726
aoqi@0 727 long fileStream::fileSize() {
aoqi@0 728 long size = -1;
aoqi@0 729 if (_file != NULL) {
aoqi@0 730 long pos = ::ftell(_file);
aoqi@0 731 if (::fseek(_file, 0, SEEK_END) == 0) {
aoqi@0 732 size = ::ftell(_file);
aoqi@0 733 }
aoqi@0 734 ::fseek(_file, pos, SEEK_SET);
aoqi@0 735 }
aoqi@0 736 return size;
aoqi@0 737 }
aoqi@0 738
aoqi@0 739 char* fileStream::readln(char *data, int count ) {
aoqi@0 740 char * ret = ::fgets(data, count, _file);
aoqi@0 741 //Get rid of annoying \n char
aoqi@0 742 data[::strlen(data)-1] = '\0';
aoqi@0 743 return ret;
aoqi@0 744 }
aoqi@0 745
aoqi@0 746 fileStream::~fileStream() {
aoqi@0 747 if (_file != NULL) {
aoqi@0 748 if (_need_close) fclose(_file);
aoqi@0 749 _file = NULL;
aoqi@0 750 }
aoqi@0 751 }
aoqi@0 752
aoqi@0 753 void fileStream::flush() {
aoqi@0 754 fflush(_file);
aoqi@0 755 }
aoqi@0 756
aoqi@0 757 fdStream::fdStream(const char* file_name) {
aoqi@0 758 _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
aoqi@0 759 _need_close = true;
aoqi@0 760 }
aoqi@0 761
aoqi@0 762 fdStream::~fdStream() {
aoqi@0 763 if (_fd != -1) {
aoqi@0 764 if (_need_close) close(_fd);
aoqi@0 765 _fd = -1;
aoqi@0 766 }
aoqi@0 767 }
aoqi@0 768
aoqi@0 769 void fdStream::write(const char* s, size_t len) {
aoqi@0 770 if (_fd != -1) {
aoqi@0 771 // Make an unused local variable to avoid warning from gcc 4.x compiler.
aoqi@0 772 size_t count = ::write(_fd, s, (int)len);
aoqi@0 773 }
aoqi@0 774 update_position(s, len);
aoqi@0 775 }
aoqi@0 776
aoqi@0 777 // dump vm version, os version, platform info, build id,
aoqi@0 778 // memory usage and command line flags into header
aoqi@0 779 void gcLogFileStream::dump_loggc_header() {
aoqi@0 780 if (is_open()) {
aoqi@0 781 print_cr("%s", Abstract_VM_Version::internal_vm_info_string());
aoqi@0 782 os::print_memory_info(this);
aoqi@0 783 print("CommandLine flags: ");
aoqi@0 784 CommandLineFlags::printSetFlags(this);
aoqi@0 785 }
aoqi@0 786 }
aoqi@0 787
aoqi@0 788 gcLogFileStream::~gcLogFileStream() {
aoqi@0 789 if (_file != NULL) {
aoqi@0 790 if (_need_close) fclose(_file);
aoqi@0 791 _file = NULL;
aoqi@0 792 }
aoqi@0 793 if (_file_name != NULL) {
aoqi@0 794 FREE_C_HEAP_ARRAY(char, _file_name, mtInternal);
aoqi@0 795 _file_name = NULL;
aoqi@0 796 }
aoqi@0 797 }
aoqi@0 798
aoqi@0 799 gcLogFileStream::gcLogFileStream(const char* file_name) {
aoqi@0 800 _cur_file_num = 0;
aoqi@0 801 _bytes_written = 0L;
aoqi@0 802 _file_name = make_log_name(file_name, NULL);
aoqi@0 803
brutisso@7448 804 if (_file_name == NULL) {
brutisso@7448 805 warning("Cannot open file %s: file name is too long.\n", file_name);
brutisso@7448 806 _need_close = false;
brutisso@7448 807 UseGCLogFileRotation = false;
brutisso@7448 808 return;
brutisso@7448 809 }
brutisso@7448 810
aoqi@0 811 // gc log file rotation
aoqi@0 812 if (UseGCLogFileRotation && NumberOfGCLogFiles > 1) {
brutisso@7448 813 char tempbuf[JVM_MAXPATHLEN];
aoqi@0 814 jio_snprintf(tempbuf, sizeof(tempbuf), "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
aoqi@0 815 _file = fopen(tempbuf, "w");
aoqi@0 816 } else {
aoqi@0 817 _file = fopen(_file_name, "w");
aoqi@0 818 }
aoqi@0 819 if (_file != NULL) {
aoqi@0 820 _need_close = true;
aoqi@0 821 dump_loggc_header();
aoqi@0 822 } else {
aoqi@0 823 warning("Cannot open file %s due to %s\n", _file_name, strerror(errno));
aoqi@0 824 _need_close = false;
aoqi@0 825 }
aoqi@0 826 }
aoqi@0 827
aoqi@0 828 void gcLogFileStream::write(const char* s, size_t len) {
aoqi@0 829 if (_file != NULL) {
aoqi@0 830 size_t count = fwrite(s, 1, len, _file);
aoqi@0 831 _bytes_written += count;
aoqi@0 832 }
aoqi@0 833 update_position(s, len);
aoqi@0 834 }
aoqi@0 835
aoqi@0 836 // rotate_log must be called from VMThread at safepoint. In case need change parameters
aoqi@0 837 // for gc log rotation from thread other than VMThread, a sub type of VM_Operation
aoqi@0 838 // should be created and be submitted to VMThread's operation queue. DO NOT call this
aoqi@0 839 // function directly. Currently, it is safe to rotate log at safepoint through VMThread.
aoqi@0 840 // That is, no mutator threads and concurrent GC threads run parallel with VMThread to
aoqi@0 841 // write to gc log file at safepoint. If in future, changes made for mutator threads or
aoqi@0 842 // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
aoqi@0 843 // must be synchronized.
aoqi@0 844 void gcLogFileStream::rotate_log(bool force, outputStream* out) {
brutisso@7448 845 char time_msg[O_BUFLEN];
aoqi@0 846 char time_str[EXTRACHARLEN];
brutisso@7448 847 char current_file_name[JVM_MAXPATHLEN];
brutisso@7448 848 char renamed_file_name[JVM_MAXPATHLEN];
aoqi@0 849
aoqi@0 850 if (!should_rotate(force)) {
aoqi@0 851 return;
aoqi@0 852 }
aoqi@0 853
aoqi@0 854 #ifdef ASSERT
aoqi@0 855 Thread *thread = Thread::current();
aoqi@0 856 assert(thread == NULL ||
aoqi@0 857 (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()),
aoqi@0 858 "Must be VMThread at safepoint");
aoqi@0 859 #endif
aoqi@0 860 if (NumberOfGCLogFiles == 1) {
aoqi@0 861 // rotate in same file
aoqi@0 862 rewind();
aoqi@0 863 _bytes_written = 0L;
aoqi@0 864 jio_snprintf(time_msg, sizeof(time_msg), "File %s rotated at %s\n",
aoqi@0 865 _file_name, os::local_time_string((char *)time_str, sizeof(time_str)));
aoqi@0 866 write(time_msg, strlen(time_msg));
aoqi@0 867
aoqi@0 868 if (out != NULL) {
aoqi@0 869 out->print("%s", time_msg);
aoqi@0 870 }
aoqi@0 871
aoqi@0 872 dump_loggc_header();
aoqi@0 873 return;
aoqi@0 874 }
aoqi@0 875
aoqi@0 876 #if defined(_WINDOWS)
aoqi@0 877 #ifndef F_OK
aoqi@0 878 #define F_OK 0
aoqi@0 879 #endif
aoqi@0 880 #endif // _WINDOWS
aoqi@0 881
aoqi@0 882 // rotate file in names extended_filename.0, extended_filename.1, ...,
aoqi@0 883 // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will
aoqi@0 884 // have a form of extended_filename.<i>.current where i is the current rotation
aoqi@0 885 // file number. After it reaches max file size, the file will be saved and renamed
aoqi@0 886 // with .current removed from its tail.
aoqi@0 887 if (_file != NULL) {
brutisso@7448 888 jio_snprintf(renamed_file_name, JVM_MAXPATHLEN, "%s.%d",
aoqi@0 889 _file_name, _cur_file_num);
brutisso@7448 890 int result = jio_snprintf(current_file_name, JVM_MAXPATHLEN,
brutisso@7448 891 "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
brutisso@7448 892 if (result >= JVM_MAXPATHLEN) {
brutisso@7448 893 warning("Cannot create new log file name: %s: file name is too long.\n", current_file_name);
brutisso@7448 894 return;
brutisso@7448 895 }
aoqi@0 896
aoqi@0 897 const char* msg = force ? "GC log rotation request has been received."
aoqi@0 898 : "GC log file has reached the maximum size.";
aoqi@0 899 jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n",
aoqi@0 900 os::local_time_string((char *)time_str, sizeof(time_str)),
aoqi@0 901 msg, renamed_file_name);
aoqi@0 902 write(time_msg, strlen(time_msg));
aoqi@0 903
aoqi@0 904 if (out != NULL) {
aoqi@0 905 out->print("%s", time_msg);
aoqi@0 906 }
aoqi@0 907
aoqi@0 908 fclose(_file);
aoqi@0 909 _file = NULL;
aoqi@0 910
aoqi@0 911 bool can_rename = true;
aoqi@0 912 if (access(current_file_name, F_OK) != 0) {
aoqi@0 913 // current file does not exist?
aoqi@0 914 warning("No source file exists, cannot rename\n");
aoqi@0 915 can_rename = false;
aoqi@0 916 }
aoqi@0 917 if (can_rename) {
aoqi@0 918 if (access(renamed_file_name, F_OK) == 0) {
aoqi@0 919 if (remove(renamed_file_name) != 0) {
aoqi@0 920 warning("Could not delete existing file %s\n", renamed_file_name);
aoqi@0 921 can_rename = false;
aoqi@0 922 }
aoqi@0 923 } else {
aoqi@0 924 // file does not exist, ok to rename
aoqi@0 925 }
aoqi@0 926 }
aoqi@0 927 if (can_rename && rename(current_file_name, renamed_file_name) != 0) {
aoqi@0 928 warning("Could not rename %s to %s\n", _file_name, renamed_file_name);
aoqi@0 929 }
aoqi@0 930 }
aoqi@0 931
aoqi@0 932 _cur_file_num++;
aoqi@0 933 if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0;
brutisso@7448 934 int result = jio_snprintf(current_file_name, JVM_MAXPATHLEN, "%s.%d" CURRENTAPPX,
aoqi@0 935 _file_name, _cur_file_num);
brutisso@7448 936 if (result >= JVM_MAXPATHLEN) {
brutisso@7448 937 warning("Cannot create new log file name: %s: file name is too long.\n", current_file_name);
brutisso@7448 938 return;
brutisso@7448 939 }
brutisso@7448 940
aoqi@0 941 _file = fopen(current_file_name, "w");
aoqi@0 942
aoqi@0 943 if (_file != NULL) {
aoqi@0 944 _bytes_written = 0L;
aoqi@0 945 _need_close = true;
aoqi@0 946 // reuse current_file_name for time_msg
brutisso@7448 947 jio_snprintf(current_file_name, JVM_MAXPATHLEN,
aoqi@0 948 "%s.%d", _file_name, _cur_file_num);
aoqi@0 949 jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n",
brutisso@7448 950 os::local_time_string((char *)time_str, sizeof(time_str)), current_file_name);
aoqi@0 951 write(time_msg, strlen(time_msg));
aoqi@0 952
aoqi@0 953 if (out != NULL) {
aoqi@0 954 out->print("%s", time_msg);
aoqi@0 955 }
aoqi@0 956
aoqi@0 957 dump_loggc_header();
aoqi@0 958 // remove the existing file
aoqi@0 959 if (access(current_file_name, F_OK) == 0) {
aoqi@0 960 if (remove(current_file_name) != 0) {
aoqi@0 961 warning("Could not delete existing file %s\n", current_file_name);
aoqi@0 962 }
aoqi@0 963 }
aoqi@0 964 } else {
aoqi@0 965 warning("failed to open rotation log file %s due to %s\n"
aoqi@0 966 "Turned off GC log file rotation\n",
aoqi@0 967 _file_name, strerror(errno));
aoqi@0 968 _need_close = false;
aoqi@0 969 FLAG_SET_DEFAULT(UseGCLogFileRotation, false);
aoqi@0 970 }
aoqi@0 971 }
aoqi@0 972
aoqi@0 973 defaultStream* defaultStream::instance = NULL;
aoqi@0 974 int defaultStream::_output_fd = 1;
aoqi@0 975 int defaultStream::_error_fd = 2;
aoqi@0 976 FILE* defaultStream::_output_stream = stdout;
aoqi@0 977 FILE* defaultStream::_error_stream = stderr;
aoqi@0 978
aoqi@0 979 #define LOG_MAJOR_VERSION 160
aoqi@0 980 #define LOG_MINOR_VERSION 1
aoqi@0 981
aoqi@0 982 void defaultStream::init() {
aoqi@0 983 _inited = true;
aoqi@0 984 if (LogVMOutput || LogCompilation) {
aoqi@0 985 init_log();
aoqi@0 986 }
aoqi@0 987 }
aoqi@0 988
aoqi@0 989 bool defaultStream::has_log_file() {
aoqi@0 990 // lazily create log file (at startup, LogVMOutput is false even
aoqi@0 991 // if +LogVMOutput is used, because the flags haven't been parsed yet)
aoqi@0 992 // For safer printing during fatal error handling, do not init logfile
aoqi@0 993 // if a VM error has been reported.
aoqi@0 994 if (!_inited && !is_error_reported()) init();
aoqi@0 995 return _log_file != NULL;
aoqi@0 996 }
aoqi@0 997
brutisso@7448 998 fileStream* defaultStream::open_file(const char* log_name) {
brutisso@7448 999 const char* try_name = make_log_name(log_name, NULL);
brutisso@7448 1000 if (try_name == NULL) {
brutisso@7448 1001 warning("Cannot open file %s: file name is too long.\n", log_name);
brutisso@7448 1002 return NULL;
brutisso@7448 1003 }
brutisso@7448 1004
brutisso@7448 1005 fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
brutisso@7448 1006 FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
brutisso@7448 1007 if (file->is_open()) {
brutisso@7448 1008 return file;
brutisso@7448 1009 }
brutisso@7448 1010
brutisso@7448 1011 // Try again to open the file in the temp directory.
brutisso@7448 1012 delete file;
brutisso@7448 1013 char warnbuf[O_BUFLEN*2];
brutisso@7448 1014 jio_snprintf(warnbuf, sizeof(warnbuf), "Warning: Cannot open log file: %s\n", log_name);
brutisso@7448 1015 // Note: This feature is for maintainer use only. No need for L10N.
brutisso@7448 1016 jio_print(warnbuf);
brutisso@7448 1017 try_name = make_log_name(log_name, os::get_temp_directory());
brutisso@7448 1018 if (try_name == NULL) {
brutisso@7448 1019 warning("Cannot open file %s: file name is too long for directory %s.\n", log_name, os::get_temp_directory());
brutisso@7448 1020 return NULL;
brutisso@7448 1021 }
brutisso@7448 1022
brutisso@7448 1023 jio_snprintf(warnbuf, sizeof(warnbuf),
brutisso@7448 1024 "Warning: Forcing option -XX:LogFile=%s\n", try_name);
brutisso@7448 1025 jio_print(warnbuf);
brutisso@7448 1026
brutisso@7448 1027 file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
brutisso@7448 1028 FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
brutisso@7448 1029 if (file->is_open()) {
brutisso@7448 1030 return file;
brutisso@7448 1031 }
brutisso@7448 1032
brutisso@7448 1033 delete file;
brutisso@7448 1034 return NULL;
brutisso@7448 1035 }
brutisso@7448 1036
aoqi@0 1037 void defaultStream::init_log() {
aoqi@0 1038 // %%% Need a MutexLocker?
aoqi@0 1039 const char* log_name = LogFile != NULL ? LogFile : "hotspot_%p.log";
brutisso@7448 1040 fileStream* file = open_file(log_name);
brutisso@7448 1041
brutisso@7448 1042 if (file != NULL) {
brutisso@7448 1043 _log_file = file;
brutisso@7448 1044 _outer_xmlStream = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file);
brutisso@7448 1045 start_log();
brutisso@7448 1046 } else {
brutisso@7448 1047 // and leave xtty as NULL
brutisso@7448 1048 LogVMOutput = false;
brutisso@7448 1049 DisplayVMOutput = true;
brutisso@7448 1050 LogCompilation = false;
aoqi@0 1051 }
brutisso@7448 1052 }
aoqi@0 1053
brutisso@7448 1054 void defaultStream::start_log() {
brutisso@7448 1055 xmlStream*xs = _outer_xmlStream;
aoqi@0 1056 if (this == tty) xtty = xs;
aoqi@0 1057 // Write XML header.
aoqi@0 1058 xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>");
aoqi@0 1059 // (For now, don't bother to issue a DTD for this private format.)
aoqi@0 1060 jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds();
aoqi@0 1061 // %%% Should be: jlong time_ms = os::start_time_milliseconds(), if
aoqi@0 1062 // we ever get round to introduce that method on the os class
aoqi@0 1063 xs->head("hotspot_log version='%d %d'"
kevinw@9327 1064 " process='%d' time_ms='" INT64_FORMAT "'",
aoqi@0 1065 LOG_MAJOR_VERSION, LOG_MINOR_VERSION,
aoqi@0 1066 os::current_process_id(), (int64_t)time_ms);
aoqi@0 1067 // Write VM version header immediately.
aoqi@0 1068 xs->head("vm_version");
aoqi@0 1069 xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr();
aoqi@0 1070 xs->tail("name");
aoqi@0 1071 xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr();
aoqi@0 1072 xs->tail("release");
aoqi@0 1073 xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr();
aoqi@0 1074 xs->tail("info");
aoqi@0 1075 xs->tail("vm_version");
aoqi@0 1076 // Record information about the command-line invocation.
aoqi@0 1077 xs->head("vm_arguments"); // Cf. Arguments::print_on()
aoqi@0 1078 if (Arguments::num_jvm_flags() > 0) {
aoqi@0 1079 xs->head("flags");
aoqi@0 1080 Arguments::print_jvm_flags_on(xs->text());
aoqi@0 1081 xs->tail("flags");
aoqi@0 1082 }
aoqi@0 1083 if (Arguments::num_jvm_args() > 0) {
aoqi@0 1084 xs->head("args");
aoqi@0 1085 Arguments::print_jvm_args_on(xs->text());
aoqi@0 1086 xs->tail("args");
aoqi@0 1087 }
aoqi@0 1088 if (Arguments::java_command() != NULL) {
aoqi@0 1089 xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command());
aoqi@0 1090 xs->tail("command");
aoqi@0 1091 }
aoqi@0 1092 if (Arguments::sun_java_launcher() != NULL) {
aoqi@0 1093 xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher());
aoqi@0 1094 xs->tail("launcher");
aoqi@0 1095 }
aoqi@0 1096 if (Arguments::system_properties() != NULL) {
aoqi@0 1097 xs->head("properties");
aoqi@0 1098 // Print it as a java-style property list.
aoqi@0 1099 // System properties don't generally contain newlines, so don't bother with unparsing.
aoqi@0 1100 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
aoqi@0 1101 xs->text()->print_cr("%s=%s", p->key(), p->value());
aoqi@0 1102 }
aoqi@0 1103 xs->tail("properties");
aoqi@0 1104 }
aoqi@0 1105 xs->tail("vm_arguments");
aoqi@0 1106 // tty output per se is grouped under the <tty>...</tty> element.
aoqi@0 1107 xs->head("tty");
aoqi@0 1108 // All further non-markup text gets copied to the tty:
aoqi@0 1109 xs->_text = this; // requires friend declaration!
aoqi@0 1110 }
aoqi@0 1111
aoqi@0 1112 // finish_log() is called during normal VM shutdown. finish_log_on_error() is
aoqi@0 1113 // called by ostream_abort() after a fatal error.
aoqi@0 1114 //
aoqi@0 1115 void defaultStream::finish_log() {
aoqi@0 1116 xmlStream* xs = _outer_xmlStream;
aoqi@0 1117 xs->done("tty");
aoqi@0 1118
aoqi@0 1119 // Other log forks are appended here, at the End of Time:
aoqi@0 1120 CompileLog::finish_log(xs->out()); // write compile logging, if any, now
aoqi@0 1121
aoqi@0 1122 xs->done("hotspot_log");
aoqi@0 1123 xs->flush();
aoqi@0 1124
aoqi@0 1125 fileStream* file = _log_file;
aoqi@0 1126 _log_file = NULL;
aoqi@0 1127
aoqi@0 1128 delete _outer_xmlStream;
aoqi@0 1129 _outer_xmlStream = NULL;
aoqi@0 1130
aoqi@0 1131 file->flush();
aoqi@0 1132 delete file;
aoqi@0 1133 }
aoqi@0 1134
aoqi@0 1135 void defaultStream::finish_log_on_error(char *buf, int buflen) {
aoqi@0 1136 xmlStream* xs = _outer_xmlStream;
aoqi@0 1137
aoqi@0 1138 if (xs && xs->out()) {
aoqi@0 1139
aoqi@0 1140 xs->done_raw("tty");
aoqi@0 1141
aoqi@0 1142 // Other log forks are appended here, at the End of Time:
aoqi@0 1143 CompileLog::finish_log_on_error(xs->out(), buf, buflen); // write compile logging, if any, now
aoqi@0 1144
aoqi@0 1145 xs->done_raw("hotspot_log");
aoqi@0 1146 xs->flush();
aoqi@0 1147
aoqi@0 1148 fileStream* file = _log_file;
aoqi@0 1149 _log_file = NULL;
aoqi@0 1150 _outer_xmlStream = NULL;
aoqi@0 1151
aoqi@0 1152 if (file) {
aoqi@0 1153 file->flush();
aoqi@0 1154
aoqi@0 1155 // Can't delete or close the file because delete and fclose aren't
aoqi@0 1156 // async-safe. We are about to die, so leave it to the kernel.
aoqi@0 1157 // delete file;
aoqi@0 1158 }
aoqi@0 1159 }
aoqi@0 1160 }
aoqi@0 1161
aoqi@0 1162 intx defaultStream::hold(intx writer_id) {
aoqi@0 1163 bool has_log = has_log_file(); // check before locking
aoqi@0 1164 if (// impossible, but who knows?
aoqi@0 1165 writer_id == NO_WRITER ||
aoqi@0 1166
aoqi@0 1167 // bootstrap problem
aoqi@0 1168 tty_lock == NULL ||
aoqi@0 1169
aoqi@0 1170 // can't grab a lock or call Thread::current() if TLS isn't initialized
aoqi@0 1171 ThreadLocalStorage::thread() == NULL ||
aoqi@0 1172
aoqi@0 1173 // developer hook
aoqi@0 1174 !SerializeVMOutput ||
aoqi@0 1175
aoqi@0 1176 // VM already unhealthy
aoqi@0 1177 is_error_reported() ||
aoqi@0 1178
aoqi@0 1179 // safepoint == global lock (for VM only)
aoqi@0 1180 (SafepointSynchronize::is_synchronizing() &&
aoqi@0 1181 Thread::current()->is_VM_thread())
aoqi@0 1182 ) {
aoqi@0 1183 // do not attempt to lock unless we know the thread and the VM is healthy
aoqi@0 1184 return NO_WRITER;
aoqi@0 1185 }
aoqi@0 1186 if (_writer == writer_id) {
aoqi@0 1187 // already held, no need to re-grab the lock
aoqi@0 1188 return NO_WRITER;
aoqi@0 1189 }
aoqi@0 1190 tty_lock->lock_without_safepoint_check();
aoqi@0 1191 // got the lock
aoqi@0 1192 if (writer_id != _last_writer) {
aoqi@0 1193 if (has_log) {
aoqi@0 1194 _log_file->bol();
aoqi@0 1195 // output a hint where this output is coming from:
aoqi@0 1196 _log_file->print_cr("<writer thread='" UINTX_FORMAT "'/>", writer_id);
aoqi@0 1197 }
aoqi@0 1198 _last_writer = writer_id;
aoqi@0 1199 }
aoqi@0 1200 _writer = writer_id;
aoqi@0 1201 return writer_id;
aoqi@0 1202 }
aoqi@0 1203
aoqi@0 1204 void defaultStream::release(intx holder) {
aoqi@0 1205 if (holder == NO_WRITER) {
aoqi@0 1206 // nothing to release: either a recursive lock, or we scribbled (too bad)
aoqi@0 1207 return;
aoqi@0 1208 }
aoqi@0 1209 if (_writer != holder) {
aoqi@0 1210 return; // already unlocked, perhaps via break_tty_lock_for_safepoint
aoqi@0 1211 }
aoqi@0 1212 _writer = NO_WRITER;
aoqi@0 1213 tty_lock->unlock();
aoqi@0 1214 }
aoqi@0 1215
aoqi@0 1216
aoqi@0 1217 // Yuck: jio_print does not accept char*/len.
aoqi@0 1218 static void call_jio_print(const char* s, size_t len) {
aoqi@0 1219 char buffer[O_BUFLEN+100];
aoqi@0 1220 if (len > sizeof(buffer)-1) {
aoqi@0 1221 warning("increase O_BUFLEN in ostream.cpp -- output truncated");
aoqi@0 1222 len = sizeof(buffer)-1;
aoqi@0 1223 }
aoqi@0 1224 strncpy(buffer, s, len);
aoqi@0 1225 buffer[len] = '\0';
aoqi@0 1226 jio_print(buffer);
aoqi@0 1227 }
aoqi@0 1228
aoqi@0 1229
aoqi@0 1230 void defaultStream::write(const char* s, size_t len) {
aoqi@0 1231 intx thread_id = os::current_thread_id();
aoqi@0 1232 intx holder = hold(thread_id);
aoqi@0 1233
aoqi@0 1234 if (DisplayVMOutput &&
aoqi@0 1235 (_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) {
aoqi@0 1236 // print to output stream. It can be redirected by a vfprintf hook
aoqi@0 1237 if (s[len] == '\0') {
aoqi@0 1238 jio_print(s);
aoqi@0 1239 } else {
aoqi@0 1240 call_jio_print(s, len);
aoqi@0 1241 }
aoqi@0 1242 }
aoqi@0 1243
aoqi@0 1244 // print to log file
aoqi@0 1245 if (has_log_file()) {
aoqi@0 1246 int nl0 = _newlines;
aoqi@0 1247 xmlTextStream::write(s, len);
aoqi@0 1248 // flush the log file too, if there were any newlines
aoqi@0 1249 if (nl0 != _newlines){
aoqi@0 1250 flush();
aoqi@0 1251 }
aoqi@0 1252 } else {
aoqi@0 1253 update_position(s, len);
aoqi@0 1254 }
aoqi@0 1255
aoqi@0 1256 release(holder);
aoqi@0 1257 }
aoqi@0 1258
aoqi@0 1259 intx ttyLocker::hold_tty() {
aoqi@0 1260 if (defaultStream::instance == NULL) return defaultStream::NO_WRITER;
aoqi@0 1261 intx thread_id = os::current_thread_id();
aoqi@0 1262 return defaultStream::instance->hold(thread_id);
aoqi@0 1263 }
aoqi@0 1264
aoqi@0 1265 void ttyLocker::release_tty(intx holder) {
aoqi@0 1266 if (holder == defaultStream::NO_WRITER) return;
aoqi@0 1267 defaultStream::instance->release(holder);
aoqi@0 1268 }
aoqi@0 1269
aoqi@0 1270 bool ttyLocker::release_tty_if_locked() {
aoqi@0 1271 intx thread_id = os::current_thread_id();
aoqi@0 1272 if (defaultStream::instance->writer() == thread_id) {
aoqi@0 1273 // release the lock and return true so callers know if was
aoqi@0 1274 // previously held.
aoqi@0 1275 release_tty(thread_id);
aoqi@0 1276 return true;
aoqi@0 1277 }
aoqi@0 1278 return false;
aoqi@0 1279 }
aoqi@0 1280
aoqi@0 1281 void ttyLocker::break_tty_lock_for_safepoint(intx holder) {
aoqi@0 1282 if (defaultStream::instance != NULL &&
aoqi@0 1283 defaultStream::instance->writer() == holder) {
aoqi@0 1284 if (xtty != NULL) {
aoqi@0 1285 xtty->print_cr("<!-- safepoint while printing -->");
aoqi@0 1286 }
aoqi@0 1287 defaultStream::instance->release(holder);
aoqi@0 1288 }
aoqi@0 1289 // (else there was no lock to break)
aoqi@0 1290 }
aoqi@0 1291
aoqi@0 1292 void ostream_init() {
aoqi@0 1293 if (defaultStream::instance == NULL) {
aoqi@0 1294 defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream();
aoqi@0 1295 tty = defaultStream::instance;
aoqi@0 1296
aoqi@0 1297 // We want to ensure that time stamps in GC logs consider time 0
aoqi@0 1298 // the time when the JVM is initialized, not the first time we ask
aoqi@0 1299 // for a time stamp. So, here, we explicitly update the time stamp
aoqi@0 1300 // of tty.
aoqi@0 1301 tty->time_stamp().update_to(1);
aoqi@0 1302 }
aoqi@0 1303 }
aoqi@0 1304
aoqi@0 1305 void ostream_init_log() {
aoqi@0 1306 // For -Xloggc:<file> option - called in runtime/thread.cpp
aoqi@0 1307 // Note : this must be called AFTER ostream_init()
aoqi@0 1308
aoqi@0 1309 gclog_or_tty = tty; // default to tty
aoqi@0 1310 if (Arguments::gc_log_filename() != NULL) {
aoqi@0 1311 fileStream * gclog = new(ResourceObj::C_HEAP, mtInternal)
aoqi@0 1312 gcLogFileStream(Arguments::gc_log_filename());
aoqi@0 1313 if (gclog->is_open()) {
aoqi@0 1314 // now we update the time stamp of the GC log to be synced up
aoqi@0 1315 // with tty.
aoqi@0 1316 gclog->time_stamp().update_to(tty->time_stamp().ticks());
aoqi@0 1317 }
aoqi@0 1318 gclog_or_tty = gclog;
aoqi@0 1319 }
aoqi@0 1320
iklam@7089 1321 #if INCLUDE_CDS
iklam@7089 1322 // For -XX:DumpLoadedClassList=<file> option
iklam@7089 1323 if (DumpLoadedClassList != NULL) {
iklam@7089 1324 const char* list_name = make_log_name(DumpLoadedClassList, NULL);
iklam@7089 1325 classlist_file = new(ResourceObj::C_HEAP, mtInternal)
iklam@7089 1326 fileStream(list_name);
iklam@7089 1327 FREE_C_HEAP_ARRAY(char, list_name, mtInternal);
iklam@7089 1328 }
iklam@7089 1329 #endif
iklam@7089 1330
aoqi@0 1331 // If we haven't lazily initialized the logfile yet, do it now,
aoqi@0 1332 // to avoid the possibility of lazy initialization during a VM
aoqi@0 1333 // crash, which can affect the stability of the fatal error handler.
aoqi@0 1334 defaultStream::instance->has_log_file();
aoqi@0 1335 }
aoqi@0 1336
aoqi@0 1337 // ostream_exit() is called during normal VM exit to finish log files, flush
aoqi@0 1338 // output and free resource.
aoqi@0 1339 void ostream_exit() {
aoqi@0 1340 static bool ostream_exit_called = false;
aoqi@0 1341 if (ostream_exit_called) return;
aoqi@0 1342 ostream_exit_called = true;
iklam@7089 1343 #if INCLUDE_CDS
iklam@7089 1344 if (classlist_file != NULL) {
iklam@7089 1345 delete classlist_file;
iklam@7089 1346 }
iklam@7089 1347 #endif
aoqi@0 1348 if (gclog_or_tty != tty) {
aoqi@0 1349 delete gclog_or_tty;
aoqi@0 1350 }
aoqi@0 1351 {
aoqi@0 1352 // we temporaly disable PrintMallocFree here
aoqi@0 1353 // as otherwise it'll lead to using of almost deleted
aoqi@0 1354 // tty or defaultStream::instance in logging facility
aoqi@0 1355 // of HeapFree(), see 6391258
aoqi@0 1356 DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);)
aoqi@0 1357 if (tty != defaultStream::instance) {
aoqi@0 1358 delete tty;
aoqi@0 1359 }
aoqi@0 1360 if (defaultStream::instance != NULL) {
aoqi@0 1361 delete defaultStream::instance;
aoqi@0 1362 }
aoqi@0 1363 }
aoqi@0 1364 tty = NULL;
aoqi@0 1365 xtty = NULL;
aoqi@0 1366 gclog_or_tty = NULL;
aoqi@0 1367 defaultStream::instance = NULL;
aoqi@0 1368 }
aoqi@0 1369
aoqi@0 1370 // ostream_abort() is called by os::abort() when VM is about to die.
aoqi@0 1371 void ostream_abort() {
aoqi@0 1372 // Here we can't delete gclog_or_tty and tty, just flush their output
aoqi@0 1373 if (gclog_or_tty) gclog_or_tty->flush();
aoqi@0 1374 if (tty) tty->flush();
aoqi@0 1375
aoqi@0 1376 if (defaultStream::instance != NULL) {
aoqi@0 1377 static char buf[4096];
aoqi@0 1378 defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
aoqi@0 1379 }
aoqi@0 1380 }
aoqi@0 1381
aoqi@0 1382 staticBufferStream::staticBufferStream(char* buffer, size_t buflen,
aoqi@0 1383 outputStream *outer_stream) {
aoqi@0 1384 _buffer = buffer;
aoqi@0 1385 _buflen = buflen;
aoqi@0 1386 _outer_stream = outer_stream;
aoqi@0 1387 // compile task prints time stamp relative to VM start
aoqi@0 1388 _stamp.update_to(1);
aoqi@0 1389 }
aoqi@0 1390
aoqi@0 1391 void staticBufferStream::write(const char* c, size_t len) {
aoqi@0 1392 _outer_stream->print_raw(c, (int)len);
aoqi@0 1393 }
aoqi@0 1394
aoqi@0 1395 void staticBufferStream::flush() {
aoqi@0 1396 _outer_stream->flush();
aoqi@0 1397 }
aoqi@0 1398
aoqi@0 1399 void staticBufferStream::print(const char* format, ...) {
aoqi@0 1400 va_list ap;
aoqi@0 1401 va_start(ap, format);
aoqi@0 1402 size_t len;
aoqi@0 1403 const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len);
aoqi@0 1404 write(str, len);
aoqi@0 1405 va_end(ap);
aoqi@0 1406 }
aoqi@0 1407
aoqi@0 1408 void staticBufferStream::print_cr(const char* format, ...) {
aoqi@0 1409 va_list ap;
aoqi@0 1410 va_start(ap, format);
aoqi@0 1411 size_t len;
aoqi@0 1412 const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len);
aoqi@0 1413 write(str, len);
aoqi@0 1414 va_end(ap);
aoqi@0 1415 }
aoqi@0 1416
aoqi@0 1417 void staticBufferStream::vprint(const char *format, va_list argptr) {
aoqi@0 1418 size_t len;
aoqi@0 1419 const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len);
aoqi@0 1420 write(str, len);
aoqi@0 1421 }
aoqi@0 1422
aoqi@0 1423 void staticBufferStream::vprint_cr(const char* format, va_list argptr) {
aoqi@0 1424 size_t len;
aoqi@0 1425 const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len);
aoqi@0 1426 write(str, len);
aoqi@0 1427 }
aoqi@0 1428
aoqi@0 1429 bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() {
aoqi@0 1430 buffer_length = initial_size;
aoqi@0 1431 buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
aoqi@0 1432 buffer_pos = 0;
aoqi@0 1433 buffer_fixed = false;
aoqi@0 1434 buffer_max = bufmax;
aoqi@0 1435 }
aoqi@0 1436
aoqi@0 1437 bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
aoqi@0 1438 buffer_length = fixed_buffer_size;
aoqi@0 1439 buffer = fixed_buffer;
aoqi@0 1440 buffer_pos = 0;
aoqi@0 1441 buffer_fixed = true;
aoqi@0 1442 buffer_max = bufmax;
aoqi@0 1443 }
aoqi@0 1444
aoqi@0 1445 void bufferedStream::write(const char* s, size_t len) {
aoqi@0 1446
aoqi@0 1447 if(buffer_pos + len > buffer_max) {
aoqi@0 1448 flush();
aoqi@0 1449 }
aoqi@0 1450
aoqi@0 1451 size_t end = buffer_pos + len;
aoqi@0 1452 if (end >= buffer_length) {
aoqi@0 1453 if (buffer_fixed) {
aoqi@0 1454 // if buffer cannot resize, silently truncate
aoqi@0 1455 len = buffer_length - buffer_pos - 1;
aoqi@0 1456 } else {
aoqi@0 1457 // For small overruns, double the buffer. For larger ones,
aoqi@0 1458 // increase to the requested size.
aoqi@0 1459 if (end < buffer_length * 2) {
aoqi@0 1460 end = buffer_length * 2;
aoqi@0 1461 }
aoqi@0 1462 buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
aoqi@0 1463 buffer_length = end;
aoqi@0 1464 }
aoqi@0 1465 }
aoqi@0 1466 memcpy(buffer + buffer_pos, s, len);
aoqi@0 1467 buffer_pos += len;
aoqi@0 1468 update_position(s, len);
aoqi@0 1469 }
aoqi@0 1470
aoqi@0 1471 char* bufferedStream::as_string() {
aoqi@0 1472 char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1);
aoqi@0 1473 strncpy(copy, buffer, buffer_pos);
aoqi@0 1474 copy[buffer_pos] = 0; // terminating null
aoqi@0 1475 return copy;
aoqi@0 1476 }
aoqi@0 1477
aoqi@0 1478 bufferedStream::~bufferedStream() {
aoqi@0 1479 if (!buffer_fixed) {
aoqi@0 1480 FREE_C_HEAP_ARRAY(char, buffer, mtInternal);
aoqi@0 1481 }
aoqi@0 1482 }
aoqi@0 1483
aoqi@0 1484 #ifndef PRODUCT
aoqi@0 1485
aoqi@0 1486 #if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE)
aoqi@0 1487 #include <sys/types.h>
aoqi@0 1488 #include <sys/socket.h>
aoqi@0 1489 #include <netinet/in.h>
aoqi@0 1490 #include <arpa/inet.h>
aoqi@0 1491 #endif
aoqi@0 1492
aoqi@0 1493 // Network access
aoqi@0 1494 networkStream::networkStream() : bufferedStream(1024*10, 1024*10) {
aoqi@0 1495
aoqi@0 1496 _socket = -1;
aoqi@0 1497
aoqi@0 1498 int result = os::socket(AF_INET, SOCK_STREAM, 0);
aoqi@0 1499 if (result <= 0) {
aoqi@0 1500 assert(false, "Socket could not be created!");
aoqi@0 1501 } else {
aoqi@0 1502 _socket = result;
aoqi@0 1503 }
aoqi@0 1504 }
aoqi@0 1505
aoqi@0 1506 int networkStream::read(char *buf, size_t len) {
aoqi@0 1507 return os::recv(_socket, buf, (int)len, 0);
aoqi@0 1508 }
aoqi@0 1509
aoqi@0 1510 void networkStream::flush() {
aoqi@0 1511 if (size() != 0) {
aoqi@0 1512 int result = os::raw_send(_socket, (char *)base(), size(), 0);
aoqi@0 1513 assert(result != -1, "connection error");
aoqi@0 1514 assert(result == (int)size(), "didn't send enough data");
aoqi@0 1515 }
aoqi@0 1516 reset();
aoqi@0 1517 }
aoqi@0 1518
aoqi@0 1519 networkStream::~networkStream() {
aoqi@0 1520 close();
aoqi@0 1521 }
aoqi@0 1522
aoqi@0 1523 void networkStream::close() {
aoqi@0 1524 if (_socket != -1) {
aoqi@0 1525 flush();
aoqi@0 1526 os::socket_close(_socket);
aoqi@0 1527 _socket = -1;
aoqi@0 1528 }
aoqi@0 1529 }
aoqi@0 1530
aoqi@0 1531 bool networkStream::connect(const char *ip, short port) {
aoqi@0 1532
aoqi@0 1533 struct sockaddr_in server;
aoqi@0 1534 server.sin_family = AF_INET;
aoqi@0 1535 server.sin_port = htons(port);
aoqi@0 1536
aoqi@0 1537 server.sin_addr.s_addr = inet_addr(ip);
aoqi@0 1538 if (server.sin_addr.s_addr == (uint32_t)-1) {
aoqi@0 1539 struct hostent* host = os::get_host_by_name((char*)ip);
aoqi@0 1540 if (host != NULL) {
aoqi@0 1541 memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length);
aoqi@0 1542 } else {
aoqi@0 1543 return false;
aoqi@0 1544 }
aoqi@0 1545 }
aoqi@0 1546
aoqi@0 1547
aoqi@0 1548 int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
aoqi@0 1549 return (result >= 0);
aoqi@0 1550 }
aoqi@0 1551
aoqi@0 1552 #endif

mercurial