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