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