1 /* 2 * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2017, 2020 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "jvm.h" 28 #include "cds/metaspaceShared.hpp" 29 #include "code/codeCache.hpp" 30 #include "compiler/compileBroker.hpp" 31 #include "compiler/disassembler.hpp" 32 #include "gc/shared/gcConfig.hpp" 33 #include "gc/shared/gcLogPrecious.hpp" 34 #include "logging/logConfiguration.hpp" 35 #include "memory/metaspace.hpp" 36 #include "memory/metaspaceUtils.hpp" 37 #include "memory/resourceArea.inline.hpp" 38 #include "memory/universe.hpp" 39 #include "oops/compressedKlass.hpp" 40 #include "oops/compressedOops.hpp" 41 #include "prims/whitebox.hpp" 42 #include "runtime/arguments.hpp" 43 #include "runtime/atomic.hpp" 44 #include "runtime/frame.inline.hpp" 45 #include "runtime/init.hpp" 46 #include "runtime/os.hpp" 47 #include "runtime/osThread.hpp" 48 #include "runtime/safefetch.hpp" 49 #include "runtime/safepointMechanism.hpp" 50 #include "runtime/stackFrameStream.inline.hpp" 51 #include "runtime/thread.inline.hpp" 52 #include "runtime/threadSMR.hpp" 53 #include "runtime/vmThread.hpp" 54 #include "runtime/vmOperations.hpp" 55 #include "runtime/vm_version.hpp" 56 #include "runtime/flags/jvmFlag.hpp" 57 #include "services/memTracker.hpp" 58 #include "utilities/debug.hpp" 59 #include "utilities/decoder.hpp" 60 #include "utilities/defaultStream.hpp" 61 #include "utilities/events.hpp" 62 #include "utilities/vmError.hpp" 63 #include "utilities/macros.hpp" 64 #if INCLUDE_JFR 65 #include "jfr/jfr.hpp" 66 #endif 67 #if INCLUDE_JVMCI 68 #include "jvmci/jvmci.hpp" 69 #endif 70 71 #ifndef PRODUCT 72 #include <signal.h> 73 #endif // PRODUCT 74 75 bool VMError::coredump_status; 76 char VMError::coredump_message[O_BUFLEN]; 77 int VMError::_current_step; 78 const char* VMError::_current_step_info; 79 volatile jlong VMError::_reporting_start_time = -1; 80 volatile bool VMError::_reporting_did_timeout = false; 81 volatile jlong VMError::_step_start_time = -1; 82 volatile bool VMError::_step_did_timeout = false; 83 volatile intptr_t VMError::_first_error_tid = -1; 84 int VMError::_id; 85 const char* VMError::_message; 86 char VMError::_detail_msg[1024]; 87 Thread* VMError::_thread; 88 address VMError::_pc; 89 void* VMError::_siginfo; 90 void* VMError::_context; 91 bool VMError::_print_native_stack_used = false; 92 const char* VMError::_filename; 93 int VMError::_lineno; 94 size_t VMError::_size; 95 96 // List of environment variables that should be reported in error log file. 97 static const char* env_list[] = { 98 // All platforms 99 "JAVA_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH", 100 "PATH", "USERNAME", 101 102 // Env variables that are defined on Linux/BSD 103 "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY", 104 "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE", 105 "LANG", "LC_ALL", "LC_CTYPE", "LC_NUMERIC", "LC_TIME", 106 "TERM", "TMPDIR", "TZ", 107 108 // defined on AIX 109 "LIBPATH", "LDR_PRELOAD", "LDR_PRELOAD64", 110 111 // defined on Linux/AIX/BSD 112 "_JAVA_SR_SIGNUM", 113 114 // defined on Darwin 115 "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH", 116 "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH", 117 "DYLD_INSERT_LIBRARIES", 118 119 // defined on Windows 120 "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR", "TMP", "TEMP", 121 122 (const char *)0 123 }; 124 125 // A simple parser for -XX:OnError, usage: 126 // ptr = OnError; 127 // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL) 128 // ... ... 129 static char* next_OnError_command(char* buf, int buflen, const char** ptr) { 130 if (ptr == NULL || *ptr == NULL) return NULL; 131 132 const char* cmd = *ptr; 133 134 // skip leading blanks or ';' 135 while (*cmd == ' ' || *cmd == ';') cmd++; 136 137 if (*cmd == '\0') return NULL; 138 139 const char * cmdend = cmd; 140 while (*cmdend != '\0' && *cmdend != ';') cmdend++; 141 142 Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen); 143 144 *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1); 145 return buf; 146 } 147 148 static void print_bug_submit_message(outputStream *out, Thread *thread) { 149 if (out == NULL) return; 150 const char *url = Arguments::java_vendor_url_bug(); 151 if (url == NULL || *url == '\0') 152 url = JDK_Version::runtime_vendor_vm_bug_url(); 153 if (url != NULL && *url != '\0') { 154 out->print_raw_cr("# If you would like to submit a bug report, please visit:"); 155 out->print_raw ("# "); 156 out->print_raw_cr(url); 157 } 158 // If the crash is in native code, encourage user to submit a bug to the 159 // provider of that code. 160 if (thread && thread->is_Java_thread() && 161 !thread->is_hidden_from_external_view()) { 162 if (JavaThread::cast(thread)->thread_state() == _thread_in_native) { 163 out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug."); 164 } 165 } 166 out->print_raw_cr("#"); 167 } 168 169 void VMError::record_coredump_status(const char* message, bool status) { 170 coredump_status = status; 171 strncpy(coredump_message, message, sizeof(coredump_message)); 172 coredump_message[sizeof(coredump_message)-1] = 0; 173 } 174 175 // Return a string to describe the error 176 char* VMError::error_string(char* buf, int buflen) { 177 char signame_buf[64]; 178 const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf)); 179 180 if (signame) { 181 jio_snprintf(buf, buflen, 182 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT, 183 signame, _id, _pc, 184 os::current_process_id(), os::current_thread_id()); 185 } else if (_filename != NULL && _lineno > 0) { 186 // skip directory names 187 char separator = os::file_separator()[0]; 188 const char *p = strrchr(_filename, separator); 189 int n = jio_snprintf(buf, buflen, 190 "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT, 191 p ? p + 1 : _filename, _lineno, 192 os::current_process_id(), os::current_thread_id()); 193 if (n >= 0 && n < buflen && _message) { 194 if (strlen(_detail_msg) > 0) { 195 jio_snprintf(buf + n, buflen - n, "%s%s: %s", 196 os::line_separator(), _message, _detail_msg); 197 } else { 198 jio_snprintf(buf + n, buflen - n, "%sError: %s", 199 os::line_separator(), _message); 200 } 201 } 202 } else { 203 jio_snprintf(buf, buflen, 204 "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT, 205 _id, os::current_process_id(), os::current_thread_id()); 206 } 207 208 return buf; 209 } 210 211 void VMError::print_stack_trace(outputStream* st, JavaThread* jt, 212 char* buf, int buflen, bool verbose) { 213 #ifdef ZERO 214 if (jt->zero_stack()->sp() && jt->top_zero_frame()) { 215 // StackFrameStream uses the frame anchor, which may not have 216 // been set up. This can be done at any time in Zero, however, 217 // so if it hasn't been set up then we just set it up now and 218 // clear it again when we're done. 219 bool has_last_Java_frame = jt->has_last_Java_frame(); 220 if (!has_last_Java_frame) 221 jt->set_last_Java_frame(); 222 st->print("Java frames:"); 223 st->cr(); 224 225 // Print the frames 226 StackFrameStream sfs(jt, true /* update */, true /* process_frames */); 227 for(int i = 0; !sfs.is_done(); sfs.next(), i++) { 228 sfs.current()->zero_print_on_error(i, st, buf, buflen); 229 st->cr(); 230 } 231 232 // Reset the frame anchor if necessary 233 if (!has_last_Java_frame) 234 jt->reset_last_Java_frame(); 235 } 236 #else 237 if (jt->has_last_Java_frame()) { 238 st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)"); 239 for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); !sfs.is_done(); sfs.next()) { 240 sfs.current()->print_on_error(st, buf, buflen, verbose); 241 st->cr(); 242 } 243 } 244 #endif // ZERO 245 } 246 247 /** 248 * Adds `value` to `list` iff it's not already present and there is sufficient 249 * capacity (i.e. length(list) < `list_capacity`). The length of the list 250 * is the index of the first nullptr entry or `list_capacity` if there are 251 * no nullptr entries. 252 * 253 * @ return true if the value was added, false otherwise 254 */ 255 static bool add_if_absent(address value, address* list, int list_capacity) { 256 for (int i = 0; i < list_capacity; i++) { 257 if (list[i] == value) { 258 return false; 259 } 260 if (list[i] == nullptr) { 261 list[i] = value; 262 if (i + 1 < list_capacity) { 263 list[i + 1] = nullptr; 264 } 265 return true; 266 } 267 } 268 return false; 269 } 270 271 /** 272 * Prints the VM generated code unit, if any, containing `pc` if it has not already 273 * been printed. If the code unit is an InterpreterCodelet or StubCodeDesc, it is 274 * only printed if `is_crash_pc` is true. 275 * 276 * @param printed array of code units that have already been printed (delimited by NULL entry) 277 * @param printed_capacity the capacity of `printed` 278 * @return true if the code unit was printed, false otherwise 279 */ 280 static bool print_code(outputStream* st, Thread* thread, address pc, bool is_crash_pc, 281 address* printed, int printed_capacity) { 282 if (Interpreter::contains(pc)) { 283 if (is_crash_pc) { 284 // The interpreter CodeBlob is very large so try to print the codelet instead. 285 InterpreterCodelet* codelet = Interpreter::codelet_containing(pc); 286 if (codelet != nullptr) { 287 if (add_if_absent((address) codelet, printed, printed_capacity)) { 288 codelet->print_on(st); 289 Disassembler::decode(codelet->code_begin(), codelet->code_end(), st); 290 return true; 291 } 292 } 293 } 294 } else { 295 StubCodeDesc* desc = StubCodeDesc::desc_for(pc); 296 if (desc != nullptr) { 297 if (is_crash_pc) { 298 if (add_if_absent((address) desc, printed, printed_capacity)) { 299 desc->print_on(st); 300 Disassembler::decode(desc->begin(), desc->end(), st); 301 return true; 302 } 303 } 304 } else if (thread != nullptr) { 305 CodeBlob* cb = CodeCache::find_blob(pc); 306 if (cb != nullptr && add_if_absent((address) cb, printed, printed_capacity)) { 307 // Disassembling nmethod will incur resource memory allocation, 308 // only do so when thread is valid. 309 ResourceMark rm(thread); 310 Disassembler::decode(cb, st); 311 st->cr(); 312 return true; 313 } 314 } 315 } 316 return false; 317 } 318 319 /** 320 * Gets the caller frame of `fr`. 321 * 322 * @returns an invalid frame (i.e. fr.pc() === 0) if the caller cannot be obtained 323 */ 324 static frame next_frame(frame fr, Thread* t) { 325 // Compiled code may use EBP register on x86 so it looks like 326 // non-walkable C frame. Use frame.sender() for java frames. 327 frame invalid; 328 if (t != nullptr && t->is_Java_thread()) { 329 // Catch very first native frame by using stack address. 330 // For JavaThread stack_base and stack_size should be set. 331 if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) { 332 return invalid; 333 } 334 if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) { 335 RegisterMap map(JavaThread::cast(t), false); // No update 336 return fr.sender(&map); 337 } else { 338 // is_first_C_frame() does only simple checks for frame pointer, 339 // it will pass if java compiled code has a pointer in EBP. 340 if (os::is_first_C_frame(&fr)) return invalid; 341 return os::get_sender_for_C_frame(&fr); 342 } 343 } else { 344 if (os::is_first_C_frame(&fr)) return invalid; 345 return os::get_sender_for_C_frame(&fr); 346 } 347 } 348 349 void VMError::print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size) { 350 351 // see if it's a valid frame 352 if (fr.pc()) { 353 st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)"); 354 355 int count = 0; 356 while (count++ < StackPrintLimit) { 357 fr.print_on_error(st, buf, buf_size); 358 if (fr.pc()) { // print source file and line, if available 359 char buf[128]; 360 int line_no; 361 if (Decoder::get_source_info(fr.pc(), buf, sizeof(buf), &line_no)) { 362 st->print(" (%s:%d)", buf, line_no); 363 } 364 } 365 st->cr(); 366 fr = next_frame(fr, t); 367 if (fr.pc() == nullptr) { 368 break; 369 } 370 } 371 372 if (count > StackPrintLimit) { 373 st->print_cr("...<more frames>..."); 374 } 375 376 st->cr(); 377 } 378 } 379 380 static void print_oom_reasons(outputStream* st) { 381 st->print_cr("# Possible reasons:"); 382 st->print_cr("# The system is out of physical RAM or swap space"); 383 if (UseCompressedOops) { 384 st->print_cr("# The process is running with CompressedOops enabled, and the Java Heap may be blocking the growth of the native heap"); 385 } 386 if (LogBytesPerWord == 2) { 387 st->print_cr("# In 32 bit mode, the process size limit was hit"); 388 } 389 st->print_cr("# Possible solutions:"); 390 st->print_cr("# Reduce memory load on the system"); 391 st->print_cr("# Increase physical memory or swap space"); 392 st->print_cr("# Check if swap backing store is full"); 393 if (LogBytesPerWord == 2) { 394 st->print_cr("# Use 64 bit Java on a 64 bit OS"); 395 } 396 st->print_cr("# Decrease Java heap size (-Xmx/-Xms)"); 397 st->print_cr("# Decrease number of Java threads"); 398 st->print_cr("# Decrease Java thread stack sizes (-Xss)"); 399 st->print_cr("# Set larger code cache with -XX:ReservedCodeCacheSize="); 400 if (UseCompressedOops) { 401 switch (CompressedOops::mode()) { 402 case CompressedOops::UnscaledNarrowOop: 403 st->print_cr("# JVM is running with Unscaled Compressed Oops mode in which the Java heap is"); 404 st->print_cr("# placed in the first 4GB address space. The Java Heap base address is the"); 405 st->print_cr("# maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress"); 406 st->print_cr("# to set the Java Heap base and to place the Java Heap above 4GB virtual address."); 407 break; 408 case CompressedOops::ZeroBasedNarrowOop: 409 st->print_cr("# JVM is running with Zero Based Compressed Oops mode in which the Java heap is"); 410 st->print_cr("# placed in the first 32GB address space. The Java Heap base address is the"); 411 st->print_cr("# maximum limit for the native heap growth. Please use -XX:HeapBaseMinAddress"); 412 st->print_cr("# to set the Java Heap base and to place the Java Heap above 32GB virtual address."); 413 break; 414 default: 415 break; 416 } 417 } 418 st->print_cr("# This output file may be truncated or incomplete."); 419 } 420 421 static void report_vm_version(outputStream* st, char* buf, int buflen) { 422 // VM version 423 st->print_cr("#"); 424 JDK_Version::current().to_string(buf, buflen); 425 const char* runtime_name = JDK_Version::runtime_name() != NULL ? 426 JDK_Version::runtime_name() : ""; 427 const char* runtime_version = JDK_Version::runtime_version() != NULL ? 428 JDK_Version::runtime_version() : ""; 429 const char* vendor_version = JDK_Version::runtime_vendor_version() != NULL ? 430 JDK_Version::runtime_vendor_version() : ""; 431 const char* jdk_debug_level = VM_Version::printable_jdk_debug_level() != NULL ? 432 VM_Version::printable_jdk_debug_level() : ""; 433 434 st->print_cr("# JRE version: %s%s%s (%s) (%sbuild %s)", runtime_name, 435 (*vendor_version != '\0') ? " " : "", vendor_version, 436 buf, jdk_debug_level, runtime_version); 437 438 // This is the long version with some default settings added 439 st->print_cr("# Java VM: %s%s%s (%s%s, %s%s%s%s%s%s, %s, %s)", 440 VM_Version::vm_name(), 441 (*vendor_version != '\0') ? " " : "", vendor_version, 442 jdk_debug_level, 443 VM_Version::vm_release(), 444 VM_Version::vm_info_string(), 445 TieredCompilation ? ", tiered" : "", 446 #if INCLUDE_JVMCI 447 EnableJVMCI ? ", jvmci" : "", 448 UseJVMCICompiler ? ", jvmci compiler" : "", 449 #else 450 "", "", 451 #endif 452 UseCompressedOops ? ", compressed oops" : "", 453 UseCompressedClassPointers ? ", compressed class ptrs" : "", 454 GCConfig::hs_err_name(), 455 VM_Version::vm_platform_string() 456 ); 457 } 458 459 // Returns true if at least one thread reported a fatal error and fatal error handling is in process. 460 bool VMError::is_error_reported() { 461 return _first_error_tid != -1; 462 } 463 464 // Returns true if the current thread reported a fatal error. 465 bool VMError::is_error_reported_in_current_thread() { 466 return _first_error_tid == os::current_thread_id(); 467 } 468 469 // Helper, return current timestamp for timeout handling. 470 jlong VMError::get_current_timestamp() { 471 return os::javaTimeNanos(); 472 } 473 // Factor to translate the timestamp to seconds. 474 #define TIMESTAMP_TO_SECONDS_FACTOR (1000 * 1000 * 1000) 475 476 void VMError::record_reporting_start_time() { 477 const jlong now = get_current_timestamp(); 478 Atomic::store(&_reporting_start_time, now); 479 } 480 481 jlong VMError::get_reporting_start_time() { 482 return Atomic::load(&_reporting_start_time); 483 } 484 485 void VMError::record_step_start_time() { 486 const jlong now = get_current_timestamp(); 487 Atomic::store(&_step_start_time, now); 488 } 489 490 jlong VMError::get_step_start_time() { 491 return Atomic::load(&_step_start_time); 492 } 493 494 void VMError::clear_step_start_time() { 495 return Atomic::store(&_step_start_time, (jlong)0); 496 } 497 498 // This is the main function to report a fatal error. Only one thread can 499 // call this function, so we don't need to worry about MT-safety. But it's 500 // possible that the error handler itself may crash or die on an internal 501 // error, for example, when the stack/heap is badly damaged. We must be 502 // able to handle recursive errors that happen inside error handler. 503 // 504 // Error reporting is done in several steps. If a crash or internal error 505 // occurred when reporting an error, the nested signal/exception handler 506 // can skip steps that are already (or partially) done. Error reporting will 507 // continue from the next step. This allows us to retrieve and print 508 // information that may be unsafe to get after a fatal error. If it happens, 509 // you may find nested report_and_die() frames when you look at the stack 510 // in a debugger. 511 // 512 // In general, a hang in error handler is much worse than a crash or internal 513 // error, as it's harder to recover from a hang. Deadlock can happen if we 514 // try to grab a lock that is already owned by current thread, or if the 515 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the 516 // error handler and all the functions it called should avoid grabbing any 517 // lock. An important thing to notice is that memory allocation needs a lock. 518 // 519 // We should avoid using large stack allocated buffers. Many errors happen 520 // when stack space is already low. Making things even worse is that there 521 // could be nested report_and_die() calls on stack (see above). Only one 522 // thread can report error, so large buffers are statically allocated in data 523 // segment. 524 void VMError::report(outputStream* st, bool _verbose) { 525 526 # define BEGIN if (_current_step == 0) { _current_step = __LINE__; 527 # define STEP(s) } if (_current_step < __LINE__) { _current_step = __LINE__; _current_step_info = s; \ 528 record_step_start_time(); _step_did_timeout = false; 529 # define END clear_step_start_time(); } 530 531 // don't allocate large buffer on stack 532 static char buf[O_BUFLEN]; 533 534 BEGIN 535 536 STEP("printing fatal error message") 537 538 st->print_cr("#"); 539 if (should_report_bug(_id)) { 540 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:"); 541 } else { 542 st->print_cr("# There is insufficient memory for the Java " 543 "Runtime Environment to continue."); 544 } 545 546 #ifdef ASSERT 547 // Error handler self tests 548 // Meaning of codes passed through in the tests. 549 #define TEST_SECONDARY_CRASH 14 550 #define TEST_RESOURCE_MARK_CRASH 2 551 552 // test secondary error handling. Test it twice, to test that resetting 553 // error handler after a secondary crash works. 554 STEP("test secondary crash 1") 555 if (_verbose && TestCrashInErrorHandler == TEST_SECONDARY_CRASH) { 556 st->print_cr("Will crash now (TestCrashInErrorHandler=" UINTX_FORMAT ")...", 557 TestCrashInErrorHandler); 558 controlled_crash(TestCrashInErrorHandler); 559 } 560 561 STEP("test secondary crash 2") 562 if (_verbose && TestCrashInErrorHandler == TEST_SECONDARY_CRASH) { 563 st->print_cr("Will crash now (TestCrashInErrorHandler=" UINTX_FORMAT ")...", 564 TestCrashInErrorHandler); 565 controlled_crash(TestCrashInErrorHandler); 566 } 567 568 STEP("test missing ResourceMark does not crash") 569 if (_verbose && TestCrashInErrorHandler == TEST_RESOURCE_MARK_CRASH) { 570 stringStream message; 571 message.print("This is a message with no ResourceMark"); 572 tty->print_cr("%s", message.as_string()); 573 } 574 575 // TestUnresponsiveErrorHandler: We want to test both step timeouts and global timeout. 576 // Step to global timeout ratio is 4:1, so in order to be absolutely sure we hit the 577 // global timeout, let's execute the timeout step five times. 578 // See corresponding test in test/runtime/ErrorHandling/TimeoutInErrorHandlingTest.java 579 STEP("setup for test unresponsive error reporting step") 580 if (_verbose && TestUnresponsiveErrorHandler) { 581 // We record reporting_start_time for this test here because we 582 // care about the time spent executing TIMEOUT_TEST_STEP and not 583 // about the time it took us to get here. 584 tty->print_cr("Recording reporting_start_time for TestUnresponsiveErrorHandler."); 585 record_reporting_start_time(); 586 } 587 588 #define TIMEOUT_TEST_STEP STEP("test unresponsive error reporting step") \ 589 if (_verbose && TestUnresponsiveErrorHandler) { os::infinite_sleep(); } 590 TIMEOUT_TEST_STEP 591 TIMEOUT_TEST_STEP 592 TIMEOUT_TEST_STEP 593 TIMEOUT_TEST_STEP 594 TIMEOUT_TEST_STEP 595 596 STEP("test safefetch in error handler") 597 // test whether it is safe to use SafeFetch32 in Crash Handler. Test twice 598 // to test that resetting the signal handler works correctly. 599 if (_verbose && TestSafeFetchInErrorHandler) { 600 st->print_cr("Will test SafeFetch..."); 601 int* const invalid_pointer = (int*)segfault_address; 602 const int x = 0x76543210; 603 int i1 = SafeFetch32(invalid_pointer, x); 604 int i2 = SafeFetch32(invalid_pointer, x); 605 if (i1 == x && i2 == x) { 606 st->print_cr("SafeFetch OK."); // Correctly deflected and returned default pattern 607 } else { 608 st->print_cr("??"); 609 } 610 } 611 #endif // ASSERT 612 613 STEP("printing type of error") 614 615 switch(static_cast<unsigned int>(_id)) { 616 case OOM_MALLOC_ERROR: 617 case OOM_MMAP_ERROR: 618 case OOM_MPROTECT_ERROR: 619 if (_size) { 620 st->print("# Native memory allocation "); 621 st->print((_id == (int)OOM_MALLOC_ERROR) ? "(malloc) failed to allocate " : 622 (_id == (int)OOM_MMAP_ERROR) ? "(mmap) failed to map " : 623 "(mprotect) failed to protect "); 624 jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size); 625 st->print("%s", buf); 626 st->print(" bytes"); 627 if (strlen(_detail_msg) > 0) { 628 st->print(" for "); 629 st->print("%s", _detail_msg); 630 } 631 st->cr(); 632 } else { 633 if (strlen(_detail_msg) > 0) { 634 st->print("# "); 635 st->print_cr("%s", _detail_msg); 636 } 637 } 638 // In error file give some solutions 639 if (_verbose) { 640 print_oom_reasons(st); 641 } else { 642 return; // that's enough for the screen 643 } 644 break; 645 case INTERNAL_ERROR: 646 default: 647 break; 648 } 649 650 STEP("printing exception/signal name") 651 652 st->print_cr("#"); 653 st->print("# "); 654 // Is it an OS exception/signal? 655 if (os::exception_name(_id, buf, sizeof(buf))) { 656 st->print("%s", buf); 657 st->print(" (0x%x)", _id); // signal number 658 st->print(" at pc=" PTR_FORMAT, p2i(_pc)); 659 if (_siginfo != NULL && os::signal_sent_by_kill(_siginfo)) { 660 st->print(" (sent by kill)"); 661 } 662 } else { 663 if (should_report_bug(_id)) { 664 st->print("Internal Error"); 665 } else { 666 st->print("Out of Memory Error"); 667 } 668 if (_filename != NULL && _lineno > 0) { 669 #ifdef PRODUCT 670 // In product mode chop off pathname? 671 char separator = os::file_separator()[0]; 672 const char *p = strrchr(_filename, separator); 673 const char *file = p ? p+1 : _filename; 674 #else 675 const char *file = _filename; 676 #endif 677 st->print(" (%s:%d)", file, _lineno); 678 } else { 679 st->print(" (0x%x)", _id); 680 } 681 } 682 683 STEP("printing current thread and pid") 684 685 // process id, thread id 686 st->print(", pid=%d", os::current_process_id()); 687 st->print(", tid=" UINTX_FORMAT, os::current_thread_id()); 688 st->cr(); 689 690 STEP("printing error message") 691 692 if (should_report_bug(_id)) { // already printed the message. 693 // error message 694 if (strlen(_detail_msg) > 0) { 695 st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg); 696 } else if (_message) { 697 st->print_cr("# Error: %s", _message); 698 } 699 } 700 701 STEP("printing Java version string") 702 703 report_vm_version(st, buf, sizeof(buf)); 704 705 STEP("printing problematic frame") 706 707 // Print current frame if we have a context (i.e. it's a crash) 708 if (_context) { 709 st->print_cr("# Problematic frame:"); 710 st->print("# "); 711 frame fr = os::fetch_frame_from_context(_context); 712 fr.print_on_error(st, buf, sizeof(buf)); 713 st->cr(); 714 st->print_cr("#"); 715 } 716 717 STEP("printing core file information") 718 st->print("# "); 719 if (CreateCoredumpOnCrash) { 720 if (coredump_status) { 721 st->print("Core dump will be written. Default location: %s", coredump_message); 722 } else { 723 st->print("No core dump will be written. %s", coredump_message); 724 } 725 } else { 726 st->print("CreateCoredumpOnCrash turned off, no core file dumped"); 727 } 728 st->cr(); 729 st->print_cr("#"); 730 731 JFR_ONLY(STEP("printing jfr information")) 732 JFR_ONLY(Jfr::on_vm_error_report(st);) 733 734 STEP("printing bug submit message") 735 736 if (should_submit_bug_report(_id) && _verbose) { 737 print_bug_submit_message(st, _thread); 738 } 739 740 STEP("printing summary") 741 742 if (_verbose) { 743 st->cr(); 744 st->print_cr("--------------- S U M M A R Y ------------"); 745 st->cr(); 746 } 747 748 STEP("printing VM option summary") 749 750 if (_verbose) { 751 // VM options 752 Arguments::print_summary_on(st); 753 st->cr(); 754 } 755 756 STEP("printing summary machine and OS info") 757 758 if (_verbose) { 759 os::print_summary_info(st, buf, sizeof(buf)); 760 } 761 762 STEP("printing date and time") 763 764 if (_verbose) { 765 os::print_date_and_time(st, buf, sizeof(buf)); 766 } 767 768 STEP("printing thread") 769 770 if (_verbose) { 771 st->cr(); 772 st->print_cr("--------------- T H R E A D ---------------"); 773 st->cr(); 774 } 775 776 STEP("printing current thread") 777 778 // current thread 779 if (_verbose) { 780 if (_thread) { 781 st->print("Current thread (" PTR_FORMAT "): ", p2i(_thread)); 782 _thread->print_on_error(st, buf, sizeof(buf)); 783 st->cr(); 784 } else { 785 st->print_cr("Current thread is native thread"); 786 } 787 st->cr(); 788 } 789 790 STEP("printing current compile task") 791 792 if (_verbose && _thread && _thread->is_Compiler_thread()) { 793 CompilerThread* t = (CompilerThread*)_thread; 794 if (t->task()) { 795 st->cr(); 796 st->print_cr("Current CompileTask:"); 797 t->task()->print_line_on_error(st, buf, sizeof(buf)); 798 st->cr(); 799 } 800 } 801 802 STEP("printing stack bounds") 803 804 if (_verbose) { 805 st->print("Stack: "); 806 807 address stack_top; 808 size_t stack_size; 809 810 if (_thread) { 811 stack_top = _thread->stack_base(); 812 stack_size = _thread->stack_size(); 813 } else { 814 stack_top = os::current_stack_base(); 815 stack_size = os::current_stack_size(); 816 } 817 818 address stack_bottom = stack_top - stack_size; 819 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", p2i(stack_bottom), p2i(stack_top)); 820 821 frame fr = _context ? os::fetch_frame_from_context(_context) 822 : os::current_frame(); 823 824 if (fr.sp()) { 825 st->print(", sp=" PTR_FORMAT, p2i(fr.sp())); 826 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024); 827 st->print(", free space=" SIZE_FORMAT "k", free_stack_size); 828 } 829 830 st->cr(); 831 } 832 833 STEP("printing native stack") 834 835 if (_verbose) { 836 if (os::platform_print_native_stack(st, _context, buf, sizeof(buf))) { 837 // We have printed the native stack in platform-specific code 838 // Windows/x64 needs special handling. 839 } else { 840 frame fr = _context ? os::fetch_frame_from_context(_context) 841 : os::current_frame(); 842 843 print_native_stack(st, fr, _thread, buf, sizeof(buf)); 844 _print_native_stack_used = true; 845 } 846 } 847 848 STEP("printing Java stack") 849 850 if (_verbose && _thread && _thread->is_Java_thread()) { 851 print_stack_trace(st, JavaThread::cast(_thread), buf, sizeof(buf)); 852 } 853 854 STEP("printing target Java thread stack") 855 856 // printing Java thread stack trace if it is involved in GC crash 857 if (_verbose && _thread && (_thread->is_Named_thread())) { 858 Thread* thread = ((NamedThread *)_thread)->processed_thread(); 859 if (thread != NULL && thread->is_Java_thread()) { 860 JavaThread* jt = JavaThread::cast(thread); 861 st->print_cr("JavaThread " PTR_FORMAT " (nid = %d) was being processed", p2i(jt), jt->osthread()->thread_id()); 862 print_stack_trace(st, jt, buf, sizeof(buf), true); 863 } 864 } 865 866 STEP("printing siginfo") 867 868 // signal no, signal code, address that caused the fault 869 if (_verbose && _siginfo) { 870 st->cr(); 871 os::print_siginfo(st, _siginfo); 872 st->cr(); 873 } 874 875 STEP("CDS archive access warning") 876 877 // Print an explicit hint if we crashed on access to the CDS archive. 878 if (_verbose && _siginfo) { 879 check_failing_cds_access(st, _siginfo); 880 st->cr(); 881 } 882 883 STEP("printing register info") 884 885 // decode register contents if possible 886 if (_verbose && _context && _thread && Universe::is_fully_initialized()) { 887 ResourceMark rm(_thread); 888 os::print_register_info(st, _context); 889 st->cr(); 890 } 891 892 STEP("printing registers, top of stack, instructions near pc") 893 894 // registers, top of stack, instructions near pc 895 if (_verbose && _context) { 896 os::print_context(st, _context); 897 st->cr(); 898 } 899 900 STEP("inspecting top of stack") 901 902 // decode stack contents if possible 903 if (_verbose && _context && _thread && Universe::is_fully_initialized()) { 904 frame fr = os::fetch_frame_from_context(_context); 905 const int slots = 8; 906 const intptr_t *start = fr.sp(); 907 const intptr_t *end = start + slots; 908 if (is_aligned(start, sizeof(intptr_t)) && os::is_readable_range(start, end)) { 909 st->print_cr("Stack slot to memory mapping:"); 910 for (int i = 0; i < slots; ++i) { 911 st->print("stack at sp + %d slots: ", i); 912 ResourceMark rm(_thread); 913 os::print_location(st, *(start + i)); 914 } 915 } 916 st->cr(); 917 } 918 919 STEP("printing code blobs if possible") 920 921 if (_verbose) { 922 const int printed_capacity = max_error_log_print_code; 923 address printed[printed_capacity]; 924 printed[0] = nullptr; 925 int printed_len = 0; 926 // Even though ErrorLogPrintCodeLimit is ranged checked 927 // during argument parsing, there's no way to prevent it 928 // subsequently (i.e., after parsing) being set to a 929 // value outside the range. 930 int limit = MIN2(ErrorLogPrintCodeLimit, printed_capacity); 931 if (limit > 0) { 932 // Scan the native stack 933 if (!_print_native_stack_used) { 934 // Only try to print code of the crashing frame since 935 // the native stack cannot be walked with next_frame. 936 if (print_code(st, _thread, _pc, true, printed, printed_capacity)) { 937 printed_len++; 938 } 939 } else { 940 frame fr = _context ? os::fetch_frame_from_context(_context) 941 : os::current_frame(); 942 while (printed_len < limit && fr.pc() != nullptr) { 943 if (print_code(st, _thread, fr.pc(), fr.pc() == _pc, printed, printed_capacity)) { 944 printed_len++; 945 } 946 fr = next_frame(fr, _thread); 947 } 948 } 949 950 // Scan the Java stack 951 if (_thread != nullptr && _thread->is_Java_thread()) { 952 JavaThread* jt = JavaThread::cast(_thread); 953 if (jt->has_last_Java_frame()) { 954 for (StackFrameStream sfs(jt, true /* update */, true /* process_frames */); printed_len < limit && !sfs.is_done(); sfs.next()) { 955 address pc = sfs.current()->pc(); 956 if (print_code(st, _thread, pc, pc == _pc, printed, printed_capacity)) { 957 printed_len++; 958 } 959 } 960 } 961 } 962 } 963 } 964 965 STEP("printing VM operation") 966 967 if (_verbose && _thread && _thread->is_VM_thread()) { 968 VMThread* t = (VMThread*)_thread; 969 VM_Operation* op = t->vm_operation(); 970 if (op) { 971 op->print_on_error(st); 972 st->cr(); 973 st->cr(); 974 } 975 } 976 977 STEP("printing process") 978 979 if (_verbose) { 980 st->cr(); 981 st->print_cr("--------------- P R O C E S S ---------------"); 982 st->cr(); 983 } 984 985 #ifndef _WIN32 986 STEP("printing user info") 987 988 if (ExtensiveErrorReports && _verbose) { 989 os::Posix::print_user_info(st); 990 } 991 #endif 992 993 STEP("printing all threads") 994 995 // all threads 996 if (_verbose && _thread) { 997 Threads::print_on_error(st, _thread, buf, sizeof(buf)); 998 st->cr(); 999 } 1000 1001 STEP("printing VM state") 1002 1003 if (_verbose) { 1004 // Safepoint state 1005 st->print("VM state: "); 1006 1007 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing"); 1008 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint"); 1009 else st->print("not at safepoint"); 1010 1011 // Also see if error occurred during initialization or shutdown 1012 if (!Universe::is_fully_initialized()) { 1013 st->print(" (not fully initialized)"); 1014 } else if (VM_Exit::vm_exited()) { 1015 st->print(" (shutting down)"); 1016 } else { 1017 st->print(" (normal execution)"); 1018 } 1019 st->cr(); 1020 st->cr(); 1021 } 1022 1023 STEP("printing owned locks on error") 1024 1025 // mutexes/monitors that currently have an owner 1026 if (_verbose) { 1027 print_owned_locks_on_error(st); 1028 st->cr(); 1029 } 1030 1031 STEP("printing number of OutOfMemoryError and StackOverflow exceptions") 1032 1033 if (_verbose && Exceptions::has_exception_counts()) { 1034 st->print_cr("OutOfMemory and StackOverflow Exception counts:"); 1035 Exceptions::print_exception_counts_on_error(st); 1036 st->cr(); 1037 } 1038 1039 #ifdef _LP64 1040 STEP("printing compressed oops mode") 1041 1042 if (_verbose && UseCompressedOops) { 1043 CompressedOops::print_mode(st); 1044 st->cr(); 1045 } 1046 1047 STEP("printing compressed klass pointers mode") 1048 1049 if (_verbose && UseCompressedClassPointers) { 1050 CDS_ONLY(MetaspaceShared::print_on(st);) 1051 Metaspace::print_compressed_class_space(st); 1052 CompressedKlassPointers::print_mode(st); 1053 st->cr(); 1054 } 1055 #endif 1056 1057 STEP("printing heap information") 1058 1059 if (_verbose) { 1060 GCLogPrecious::print_on_error(st); 1061 1062 if (Universe::heap() != NULL) { 1063 Universe::heap()->print_on_error(st); 1064 st->cr(); 1065 } 1066 1067 if (Universe::is_fully_initialized()) { 1068 st->print_cr("Polling page: " INTPTR_FORMAT, p2i(SafepointMechanism::get_polling_page())); 1069 st->cr(); 1070 } 1071 } 1072 1073 STEP("printing metaspace information") 1074 1075 if (_verbose && Universe::is_fully_initialized()) { 1076 st->print_cr("Metaspace:"); 1077 MetaspaceUtils::print_basic_report(st, 0); 1078 } 1079 1080 STEP("printing code cache information") 1081 1082 if (_verbose && Universe::is_fully_initialized()) { 1083 // print code cache information before vm abort 1084 CodeCache::print_summary(st); 1085 st->cr(); 1086 } 1087 1088 STEP("printing ring buffers") 1089 1090 if (_verbose) { 1091 Events::print_all(st); 1092 st->cr(); 1093 } 1094 1095 STEP("printing dynamic libraries") 1096 1097 if (_verbose) { 1098 // dynamic libraries, or memory map 1099 os::print_dll_info(st); 1100 st->cr(); 1101 } 1102 1103 STEP("printing native decoder state") 1104 1105 if (_verbose) { 1106 Decoder::print_state_on(st); 1107 st->cr(); 1108 } 1109 1110 STEP("printing VM options") 1111 1112 if (_verbose) { 1113 // VM options 1114 Arguments::print_on(st); 1115 st->cr(); 1116 } 1117 1118 STEP("printing flags") 1119 1120 if (_verbose) { 1121 JVMFlag::printFlags( 1122 st, 1123 true, // with comments 1124 false, // no ranges 1125 true); // skip defaults 1126 st->cr(); 1127 } 1128 1129 STEP("printing warning if internal testing API used") 1130 1131 if (WhiteBox::used()) { 1132 st->print_cr("Unsupported internal testing APIs have been used."); 1133 st->cr(); 1134 } 1135 1136 STEP("printing log configuration") 1137 if (_verbose){ 1138 st->print_cr("Logging:"); 1139 LogConfiguration::describe_current_configuration(st); 1140 st->cr(); 1141 } 1142 1143 STEP("printing all environment variables") 1144 1145 if (_verbose) { 1146 os::print_environment_variables(st, env_list); 1147 st->cr(); 1148 } 1149 1150 STEP("printing signal handlers") 1151 1152 if (_verbose) { 1153 os::print_signal_handlers(st, buf, sizeof(buf)); 1154 st->cr(); 1155 } 1156 1157 STEP("Native Memory Tracking") 1158 if (_verbose) { 1159 MemTracker::error_report(st); 1160 } 1161 1162 STEP("printing system") 1163 1164 if (_verbose) { 1165 st->cr(); 1166 st->print_cr("--------------- S Y S T E M ---------------"); 1167 st->cr(); 1168 } 1169 1170 STEP("printing OS information") 1171 1172 if (_verbose) { 1173 os::print_os_info(st); 1174 st->cr(); 1175 } 1176 1177 STEP("printing CPU info") 1178 if (_verbose) { 1179 os::print_cpu_info(st, buf, sizeof(buf)); 1180 st->cr(); 1181 } 1182 1183 STEP("printing memory info") 1184 1185 if (_verbose) { 1186 os::print_memory_info(st); 1187 st->cr(); 1188 } 1189 1190 STEP("printing internal vm info") 1191 1192 if (_verbose) { 1193 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string()); 1194 st->cr(); 1195 } 1196 1197 // print a defined marker to show that error handling finished correctly. 1198 STEP("printing end marker") 1199 1200 if (_verbose) { 1201 st->print_cr("END."); 1202 } 1203 1204 END 1205 1206 # undef BEGIN 1207 # undef STEP 1208 # undef END 1209 } 1210 1211 // Report for the vm_info_cmd. This prints out the information above omitting 1212 // crash and thread specific information. If output is added above, it should be added 1213 // here also, if it is safe to call during a running process. 1214 void VMError::print_vm_info(outputStream* st) { 1215 1216 char buf[O_BUFLEN]; 1217 report_vm_version(st, buf, sizeof(buf)); 1218 1219 // STEP("printing summary") 1220 1221 st->cr(); 1222 st->print_cr("--------------- S U M M A R Y ------------"); 1223 st->cr(); 1224 1225 // STEP("printing VM option summary") 1226 1227 // VM options 1228 Arguments::print_summary_on(st); 1229 st->cr(); 1230 1231 // STEP("printing summary machine and OS info") 1232 1233 os::print_summary_info(st, buf, sizeof(buf)); 1234 1235 // STEP("printing date and time") 1236 1237 os::print_date_and_time(st, buf, sizeof(buf)); 1238 1239 // Skip: STEP("printing thread") 1240 1241 // STEP("printing process") 1242 1243 st->cr(); 1244 st->print_cr("--------------- P R O C E S S ---------------"); 1245 st->cr(); 1246 1247 // STEP("printing number of OutOfMemoryError and StackOverflow exceptions") 1248 1249 if (Exceptions::has_exception_counts()) { 1250 st->print_cr("OutOfMemory and StackOverflow Exception counts:"); 1251 Exceptions::print_exception_counts_on_error(st); 1252 st->cr(); 1253 } 1254 1255 #ifdef _LP64 1256 // STEP("printing compressed oops mode") 1257 if (UseCompressedOops) { 1258 CompressedOops::print_mode(st); 1259 st->cr(); 1260 } 1261 1262 // STEP("printing compressed class ptrs mode") 1263 if (UseCompressedClassPointers) { 1264 CDS_ONLY(MetaspaceShared::print_on(st);) 1265 Metaspace::print_compressed_class_space(st); 1266 CompressedKlassPointers::print_mode(st); 1267 st->cr(); 1268 } 1269 #endif 1270 1271 // STEP("printing heap information") 1272 1273 if (Universe::is_fully_initialized()) { 1274 MutexLocker hl(Heap_lock); 1275 GCLogPrecious::print_on_error(st); 1276 Universe::heap()->print_on_error(st); 1277 st->cr(); 1278 st->print_cr("Polling page: " INTPTR_FORMAT, p2i(SafepointMechanism::get_polling_page())); 1279 st->cr(); 1280 } 1281 1282 // STEP("printing metaspace information") 1283 1284 if (Universe::is_fully_initialized()) { 1285 st->print_cr("Metaspace:"); 1286 MetaspaceUtils::print_basic_report(st, 0); 1287 } 1288 1289 // STEP("printing code cache information") 1290 1291 if (Universe::is_fully_initialized()) { 1292 // print code cache information before vm abort 1293 CodeCache::print_summary(st); 1294 st->cr(); 1295 } 1296 1297 // STEP("printing ring buffers") 1298 1299 Events::print_all(st); 1300 st->cr(); 1301 1302 // STEP("printing dynamic libraries") 1303 1304 // dynamic libraries, or memory map 1305 os::print_dll_info(st); 1306 st->cr(); 1307 1308 // STEP("printing VM options") 1309 1310 // VM options 1311 Arguments::print_on(st); 1312 st->cr(); 1313 1314 // STEP("printing warning if internal testing API used") 1315 1316 if (WhiteBox::used()) { 1317 st->print_cr("Unsupported internal testing APIs have been used."); 1318 st->cr(); 1319 } 1320 1321 // STEP("printing log configuration") 1322 st->print_cr("Logging:"); 1323 LogConfiguration::describe(st); 1324 st->cr(); 1325 1326 // STEP("printing all environment variables") 1327 1328 os::print_environment_variables(st, env_list); 1329 st->cr(); 1330 1331 // STEP("printing signal handlers") 1332 1333 os::print_signal_handlers(st, buf, sizeof(buf)); 1334 st->cr(); 1335 1336 // STEP("Native Memory Tracking") 1337 1338 MemTracker::error_report(st); 1339 1340 // STEP("printing system") 1341 1342 st->cr(); 1343 st->print_cr("--------------- S Y S T E M ---------------"); 1344 st->cr(); 1345 1346 // STEP("printing OS information") 1347 1348 os::print_os_info(st); 1349 st->cr(); 1350 1351 // STEP("printing CPU info") 1352 1353 os::print_cpu_info(st, buf, sizeof(buf)); 1354 st->cr(); 1355 1356 // STEP("printing memory info") 1357 1358 os::print_memory_info(st); 1359 st->cr(); 1360 1361 // STEP("printing internal vm info") 1362 1363 st->print_cr("vm_info: %s", VM_Version::internal_vm_info_string()); 1364 st->cr(); 1365 1366 // print a defined marker to show that error handling finished correctly. 1367 // STEP("printing end marker") 1368 1369 st->print_cr("END."); 1370 } 1371 1372 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */ 1373 static int expand_and_open(const char* pattern, bool overwrite_existing, char* buf, size_t buflen, size_t pos) { 1374 int fd = -1; 1375 int mode = O_RDWR | O_CREAT; 1376 if (overwrite_existing) { 1377 mode |= O_TRUNC; 1378 } else { 1379 mode |= O_EXCL; 1380 } 1381 if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) { 1382 fd = open(buf, mode, 0666); 1383 } 1384 return fd; 1385 } 1386 1387 /** 1388 * Construct file name for a log file and return it's file descriptor. 1389 * Name and location depends on pattern, default_pattern params and access 1390 * permissions. 1391 */ 1392 int VMError::prepare_log_file(const char* pattern, const char* default_pattern, bool overwrite_existing, char* buf, size_t buflen) { 1393 int fd = -1; 1394 1395 // If possible, use specified pattern to construct log file name 1396 if (pattern != NULL) { 1397 fd = expand_and_open(pattern, overwrite_existing, buf, buflen, 0); 1398 } 1399 1400 // Either user didn't specify, or the user's location failed, 1401 // so use the default name in the current directory 1402 if (fd == -1) { 1403 const char* cwd = os::get_current_directory(buf, buflen); 1404 if (cwd != NULL) { 1405 size_t pos = strlen(cwd); 1406 int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator()); 1407 pos += fsep_len; 1408 if (fsep_len > 0) { 1409 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos); 1410 } 1411 } 1412 } 1413 1414 // try temp directory if it exists. 1415 if (fd == -1) { 1416 const char* tmpdir = os::get_temp_directory(); 1417 if (tmpdir != NULL && strlen(tmpdir) > 0) { 1418 int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator()); 1419 if (pos > 0) { 1420 fd = expand_and_open(default_pattern, overwrite_existing, buf, buflen, pos); 1421 } 1422 } 1423 } 1424 1425 return fd; 1426 } 1427 1428 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, 1429 void* context, const char* detail_fmt, ...) 1430 { 1431 va_list detail_args; 1432 va_start(detail_args, detail_fmt); 1433 report_and_die(sig, NULL, detail_fmt, detail_args, thread, pc, siginfo, context, NULL, 0, 0); 1434 va_end(detail_args); 1435 } 1436 1437 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context) 1438 { 1439 report_and_die(thread, sig, pc, siginfo, context, "%s", ""); 1440 } 1441 1442 void VMError::report_and_die(Thread* thread, void* context, const char* filename, int lineno, const char* message, 1443 const char* detail_fmt, va_list detail_args) 1444 { 1445 report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, NULL, NULL, context, filename, lineno, 0); 1446 } 1447 1448 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size, 1449 VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) { 1450 report_and_die(vm_err_type, NULL, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, size); 1451 } 1452 1453 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args, 1454 Thread* thread, address pc, void* siginfo, void* context, const char* filename, 1455 int lineno, size_t size) 1456 { 1457 // A single scratch buffer to be used from here on. 1458 // Do not rely on it being preserved across function calls. 1459 static char buffer[O_BUFLEN]; 1460 1461 // File descriptor to tty to print an error summary to. 1462 // Hard wired to stdout; see JDK-8215004 (compatibility concerns). 1463 static const int fd_out = 1; // stdout 1464 1465 // File descriptor to the error log file. 1466 static int fd_log = -1; 1467 1468 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT 1469 // Disarm assertion poison page, since from this point on we do not need this mechanism anymore and it may 1470 // cause problems in error handling during native OOM, see JDK-8227275. 1471 disarm_assert_poison(); 1472 #endif 1473 1474 // Use local fdStream objects only. Do not use global instances whose initialization 1475 // relies on dynamic initialization (see JDK-8214975). Do not rely on these instances 1476 // to carry over into recursions or invocations from other threads. 1477 fdStream out(fd_out); 1478 out.set_scratch_buffer(buffer, sizeof(buffer)); 1479 1480 // Depending on the re-entrance depth at this point, fd_log may be -1 or point to an open hs-err file. 1481 fdStream log(fd_log); 1482 log.set_scratch_buffer(buffer, sizeof(buffer)); 1483 1484 // How many errors occurred in error handler when reporting first_error. 1485 static int recursive_error_count; 1486 1487 // We will first print a brief message to standard out (verbose = false), 1488 // then save detailed information in log file (verbose = true). 1489 static bool out_done = false; // done printing to standard out 1490 static bool log_done = false; // done saving error log 1491 1492 intptr_t mytid = os::current_thread_id(); 1493 if (_first_error_tid == -1 && 1494 Atomic::cmpxchg(&_first_error_tid, (intptr_t)-1, mytid) == -1) { 1495 1496 if (SuppressFatalErrorMessage) { 1497 os::abort(CreateCoredumpOnCrash); 1498 } 1499 1500 // Initialize time stamps to use the same base. 1501 out.time_stamp().update_to(1); 1502 log.time_stamp().update_to(1); 1503 1504 _id = id; 1505 _message = message; 1506 _thread = thread; 1507 _pc = pc; 1508 _siginfo = siginfo; 1509 _context = context; 1510 _filename = filename; 1511 _lineno = lineno; 1512 _size = size; 1513 jio_vsnprintf(_detail_msg, sizeof(_detail_msg), detail_fmt, detail_args); 1514 1515 reporting_started(); 1516 if (!TestUnresponsiveErrorHandler) { 1517 // Record reporting_start_time unless we're running the 1518 // TestUnresponsiveErrorHandler test. For that test we record 1519 // reporting_start_time at the beginning of the test. 1520 record_reporting_start_time(); 1521 } else { 1522 out.print_raw_cr("Delaying recording reporting_start_time for TestUnresponsiveErrorHandler."); 1523 } 1524 1525 if (ShowMessageBoxOnError || PauseAtExit) { 1526 show_message_box(buffer, sizeof(buffer)); 1527 1528 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the 1529 // WatcherThread can kill JVM if the error handler hangs. 1530 ShowMessageBoxOnError = false; 1531 } 1532 1533 os::check_dump_limit(buffer, sizeof(buffer)); 1534 1535 // reset signal handlers or exception filter; make sure recursive crashes 1536 // are handled properly. 1537 install_secondary_signal_handler(); 1538 } else { 1539 #if defined(_WINDOWS) 1540 // If UseOSErrorReporting we call this for each level of the call stack 1541 // while searching for the exception handler. Only the first level needs 1542 // to be reported. 1543 if (UseOSErrorReporting && log_done) return; 1544 #endif 1545 1546 // This is not the first error, see if it happened in a different thread 1547 // or in the same thread during error reporting. 1548 if (_first_error_tid != mytid) { 1549 if (!SuppressFatalErrorMessage) { 1550 char msgbuf[64]; 1551 jio_snprintf(msgbuf, sizeof(msgbuf), 1552 "[thread " INTX_FORMAT " also had an error]", 1553 mytid); 1554 out.print_raw_cr(msgbuf); 1555 } 1556 1557 // Error reporting is not MT-safe, nor can we let the current thread 1558 // proceed, so we block it. 1559 os::infinite_sleep(); 1560 1561 } else { 1562 if (recursive_error_count++ > 30) { 1563 if (!SuppressFatalErrorMessage) { 1564 out.print_raw_cr("[Too many errors, abort]"); 1565 } 1566 os::die(); 1567 } 1568 1569 if (SuppressFatalErrorMessage) { 1570 // If we already hit a secondary error during abort, then calling 1571 // it again is likely to hit another one. But eventually, if we 1572 // don't deadlock somewhere, we will call os::die() above. 1573 os::abort(CreateCoredumpOnCrash); 1574 } 1575 1576 outputStream* const st = log.is_open() ? &log : &out; 1577 st->cr(); 1578 1579 // Timeout handling. 1580 if (_step_did_timeout) { 1581 // The current step had a timeout. Lets continue reporting with the next step. 1582 st->print_raw("[timeout occurred during error reporting in step \""); 1583 st->print_raw(_current_step_info); 1584 st->print_cr("\"] after " INT64_FORMAT " s.", 1585 (int64_t) 1586 ((get_current_timestamp() - _step_start_time) / TIMESTAMP_TO_SECONDS_FACTOR)); 1587 } else if (_reporting_did_timeout) { 1588 // We hit ErrorLogTimeout. Reporting will stop altogether. Let's wrap things 1589 // up, the process is about to be stopped by the WatcherThread. 1590 st->print_cr("------ Timeout during error reporting after " INT64_FORMAT " s. ------", 1591 (int64_t) 1592 ((get_current_timestamp() - _reporting_start_time) / TIMESTAMP_TO_SECONDS_FACTOR)); 1593 st->flush(); 1594 // Watcherthread is about to call os::die. Lets just wait. 1595 os::infinite_sleep(); 1596 } else { 1597 // Crash or assert during error reporting. Lets continue reporting with the next step. 1598 stringStream ss(buffer, sizeof(buffer)); 1599 // Note: this string does get parsed by a number of jtreg tests, 1600 // see hotspot/jtreg/runtime/ErrorHandling. 1601 ss.print("[error occurred during error reporting (%s), id 0x%x", 1602 _current_step_info, id); 1603 char signal_name[64]; 1604 if (os::exception_name(id, signal_name, sizeof(signal_name))) { 1605 ss.print(", %s (0x%x) at pc=" PTR_FORMAT, signal_name, id, p2i(pc)); 1606 } else { 1607 if (should_report_bug(id)) { 1608 ss.print(", Internal Error (%s:%d)", 1609 filename == NULL ? "??" : filename, lineno); 1610 } else { 1611 ss.print(", Out of Memory Error (%s:%d)", 1612 filename == NULL ? "??" : filename, lineno); 1613 } 1614 } 1615 ss.print("]"); 1616 st->print_raw_cr(buffer); 1617 st->cr(); 1618 } 1619 } 1620 } 1621 1622 // Part 1: print an abbreviated version (the '#' section) to stdout. 1623 if (!out_done) { 1624 // Suppress this output if we plan to print Part 2 to stdout too. 1625 // No need to have the "#" section twice. 1626 if (!(ErrorFileToStdout && out.fd() == 1)) { 1627 report(&out, false); 1628 } 1629 1630 out_done = true; 1631 1632 _current_step = 0; 1633 _current_step_info = ""; 1634 } 1635 1636 // Part 2: print a full error log file (optionally to stdout or stderr). 1637 // print to error log file 1638 if (!log_done) { 1639 // see if log file is already open 1640 if (!log.is_open()) { 1641 // open log file 1642 if (ErrorFileToStdout) { 1643 fd_log = 1; 1644 } else if (ErrorFileToStderr) { 1645 fd_log = 2; 1646 } else { 1647 fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", true, 1648 buffer, sizeof(buffer)); 1649 if (fd_log != -1) { 1650 out.print_raw("# An error report file with more information is saved as:\n# "); 1651 out.print_raw_cr(buffer); 1652 } else { 1653 out.print_raw_cr("# Can not save log file, dump to screen.."); 1654 fd_log = 1; 1655 } 1656 } 1657 log.set_fd(fd_log); 1658 } 1659 1660 report(&log, true); 1661 log_done = true; 1662 _current_step = 0; 1663 _current_step_info = ""; 1664 1665 if (fd_log > 3) { 1666 ::close(fd_log); 1667 fd_log = -1; 1668 } 1669 1670 log.set_fd(-1); 1671 } 1672 1673 JFR_ONLY(Jfr::on_vm_shutdown(true);) 1674 1675 if (PrintNMTStatistics) { 1676 fdStream fds(fd_out); 1677 MemTracker::final_report(&fds); 1678 } 1679 1680 static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay 1681 if (DumpReplayDataOnError && _thread && _thread->is_Compiler_thread() && !skip_replay) { 1682 skip_replay = true; 1683 ciEnv* env = ciEnv::current(); 1684 if (env != NULL) { 1685 const bool overwrite = false; // We do not overwrite an existing replay file. 1686 int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", overwrite, buffer, sizeof(buffer)); 1687 if (fd != -1) { 1688 FILE* replay_data_file = os::fdopen(fd, "w"); 1689 if (replay_data_file != NULL) { 1690 fileStream replay_data_stream(replay_data_file, /*need_close=*/true); 1691 env->dump_replay_data_unsafe(&replay_data_stream); 1692 out.print_raw("#\n# Compiler replay data is saved as:\n# "); 1693 out.print_raw_cr(buffer); 1694 } else { 1695 int e = errno; 1696 out.print_raw("#\n# Can't open file to dump replay data. Error: "); 1697 out.print_raw_cr(os::strerror(e)); 1698 } 1699 } 1700 } 1701 } 1702 1703 #if INCLUDE_JVMCI 1704 if (JVMCI::fatal_log_filename() != NULL) { 1705 out.print_raw("#\n# The JVMCI shared library error report file is saved as:\n# "); 1706 out.print_raw_cr(JVMCI::fatal_log_filename()); 1707 } 1708 #endif 1709 1710 static bool skip_bug_url = !should_submit_bug_report(_id); 1711 if (!skip_bug_url) { 1712 skip_bug_url = true; 1713 1714 out.print_raw_cr("#"); 1715 print_bug_submit_message(&out, _thread); 1716 } 1717 1718 static bool skip_OnError = false; 1719 if (!skip_OnError && OnError && OnError[0]) { 1720 skip_OnError = true; 1721 1722 // Flush output and finish logs before running OnError commands. 1723 ostream_abort(); 1724 1725 out.print_raw_cr("#"); 1726 out.print_raw ("# -XX:OnError=\""); 1727 out.print_raw (OnError); 1728 out.print_raw_cr("\""); 1729 1730 char* cmd; 1731 const char* ptr = OnError; 1732 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){ 1733 out.print_raw ("# Executing "); 1734 #if defined(LINUX) || defined(_ALLBSD_SOURCE) 1735 out.print_raw ("/bin/sh -c "); 1736 #elif defined(_WINDOWS) 1737 out.print_raw ("cmd /C "); 1738 #endif 1739 out.print_raw ("\""); 1740 out.print_raw (cmd); 1741 out.print_raw_cr("\" ..."); 1742 1743 if (os::fork_and_exec(cmd) < 0) { 1744 out.print_cr("os::fork_and_exec failed: %s (%s=%d)", 1745 os::strerror(errno), os::errno_name(errno), errno); 1746 } 1747 } 1748 1749 // done with OnError 1750 OnError = NULL; 1751 } 1752 1753 if (WINDOWS_ONLY(!UseOSErrorReporting) NOT_WINDOWS(true)) { 1754 // os::abort() will call abort hooks, try it first. 1755 static bool skip_os_abort = false; 1756 if (!skip_os_abort) { 1757 skip_os_abort = true; 1758 bool dump_core = should_report_bug(_id); 1759 os::abort(dump_core && CreateCoredumpOnCrash, _siginfo, _context); 1760 } 1761 1762 // if os::abort() doesn't abort, try os::die(); 1763 os::die(); 1764 } 1765 } 1766 1767 /* 1768 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this 1769 * ensures utilities such as jmap can observe the process is a consistent state. 1770 */ 1771 class VM_ReportJavaOutOfMemory : public VM_Operation { 1772 private: 1773 const char* _message; 1774 public: 1775 VM_ReportJavaOutOfMemory(const char* message) { _message = message; } 1776 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; } 1777 void doit(); 1778 }; 1779 1780 void VM_ReportJavaOutOfMemory::doit() { 1781 // Don't allocate large buffer on stack 1782 static char buffer[O_BUFLEN]; 1783 1784 tty->print_cr("#"); 1785 tty->print_cr("# java.lang.OutOfMemoryError: %s", _message); 1786 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError); 1787 1788 // make heap parsability 1789 Universe::heap()->ensure_parsability(false); // no need to retire TLABs 1790 1791 char* cmd; 1792 const char* ptr = OnOutOfMemoryError; 1793 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){ 1794 tty->print("# Executing "); 1795 #if defined(LINUX) 1796 tty->print ("/bin/sh -c "); 1797 #endif 1798 tty->print_cr("\"%s\"...", cmd); 1799 1800 if (os::fork_and_exec(cmd) < 0) { 1801 tty->print_cr("os::fork_and_exec failed: %s (%s=%d)", 1802 os::strerror(errno), os::errno_name(errno), errno); 1803 } 1804 } 1805 } 1806 1807 void VMError::report_java_out_of_memory(const char* message) { 1808 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) { 1809 MutexLocker ml(Heap_lock); 1810 VM_ReportJavaOutOfMemory op(message); 1811 VMThread::execute(&op); 1812 } 1813 } 1814 1815 void VMError::show_message_box(char *buf, int buflen) { 1816 bool yes; 1817 do { 1818 error_string(buf, buflen); 1819 yes = os::start_debugging(buf,buflen); 1820 } while (yes); 1821 } 1822 1823 // Timeout handling: check if a timeout happened (either a single step did 1824 // timeout or the whole of error reporting hit ErrorLogTimeout). Interrupt 1825 // the reporting thread if that is the case. 1826 bool VMError::check_timeout() { 1827 1828 if (ErrorLogTimeout == 0) { 1829 return false; 1830 } 1831 1832 // Do not check for timeouts if we still have a message box to show to the 1833 // user or if there are OnError handlers to be run. 1834 if (ShowMessageBoxOnError 1835 || (OnError != NULL && OnError[0] != '\0') 1836 || Arguments::abort_hook() != NULL) { 1837 return false; 1838 } 1839 1840 const jlong reporting_start_time_l = get_reporting_start_time(); 1841 const jlong now = get_current_timestamp(); 1842 // Timestamp is stored in nanos. 1843 if (reporting_start_time_l > 0) { 1844 const jlong end = reporting_start_time_l + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR; 1845 if (end <= now && !_reporting_did_timeout) { 1846 // We hit ErrorLogTimeout and we haven't interrupted the reporting 1847 // thread yet. 1848 _reporting_did_timeout = true; 1849 interrupt_reporting_thread(); 1850 return true; // global timeout 1851 } 1852 } 1853 1854 const jlong step_start_time_l = get_step_start_time(); 1855 if (step_start_time_l > 0) { 1856 // A step times out after a quarter of the total timeout. Steps are mostly fast unless they 1857 // hang for some reason, so this simple rule allows for three hanging step and still 1858 // hopefully leaves time enough for the rest of the steps to finish. 1859 const jlong end = step_start_time_l + (jlong)ErrorLogTimeout * TIMESTAMP_TO_SECONDS_FACTOR / 4; 1860 if (end <= now && !_step_did_timeout) { 1861 // The step timed out and we haven't interrupted the reporting 1862 // thread yet. 1863 _step_did_timeout = true; 1864 interrupt_reporting_thread(); 1865 return false; // (Not a global timeout) 1866 } 1867 } 1868 1869 return false; 1870 1871 } 1872 1873 #ifdef ASSERT 1874 typedef void (*voidfun_t)(); 1875 1876 // Crash with an authentic sigfpe 1877 volatile int sigfpe_int = 0; 1878 static void crash_with_sigfpe() { 1879 1880 // generate a native synchronous SIGFPE where possible; 1881 sigfpe_int = sigfpe_int/sigfpe_int; 1882 1883 // if that did not cause a signal (e.g. on ppc), just 1884 // raise the signal. 1885 #ifndef _WIN32 1886 // OSX implements raise(sig) incorrectly so we need to 1887 // explicitly target the current thread 1888 pthread_kill(pthread_self(), SIGFPE); 1889 #endif 1890 1891 } // end: crash_with_sigfpe 1892 1893 // crash with sigsegv at non-null address. 1894 static void crash_with_segfault() { 1895 1896 int* crash_addr = reinterpret_cast<int*>(VMError::segfault_address); 1897 *crash_addr = 1; 1898 1899 } // end: crash_with_segfault 1900 1901 // crash in a controlled way: 1902 // 1 - assert 1903 // 2 - guarantee 1904 // 14 - SIGSEGV 1905 // 15 - SIGFPE 1906 void VMError::controlled_crash(int how) { 1907 1908 // Case 14 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SafeFetchInErrorHandlingTest.java. 1909 // Case 15 is tested by test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java. 1910 // Case 16 is tested by test/hotspot/jtreg/runtime/ErrorHandling/ThreadsListHandleInErrorHandlingTest.java. 1911 // Case 17 is tested by test/hotspot/jtreg/runtime/ErrorHandling/NestedThreadsListHandleInErrorHandlingTest.java. 1912 1913 // We try to grab Threads_lock to keep ThreadsSMRSupport::print_info_on() 1914 // from racing with Threads::add() or Threads::remove() as we 1915 // generate the hs_err_pid file. This makes our ErrorHandling tests 1916 // more stable. 1917 if (!Threads_lock->owned_by_self()) { 1918 Threads_lock->try_lock(); 1919 // The VM is going to die so no need to unlock Thread_lock. 1920 } 1921 1922 switch (how) { 1923 case 1: assert(how == 0, "test assert"); break; 1924 case 2: guarantee(how == 0, "test guarantee"); break; 1925 1926 // The other cases are unused. 1927 case 14: crash_with_segfault(); break; 1928 case 15: crash_with_sigfpe(); break; 1929 case 16: { 1930 ThreadsListHandle tlh; 1931 fatal("Force crash with an active ThreadsListHandle."); 1932 } 1933 case 17: { 1934 ThreadsListHandle tlh; 1935 { 1936 ThreadsListHandle tlh2; 1937 fatal("Force crash with a nested ThreadsListHandle."); 1938 } 1939 } 1940 default: 1941 // If another number is given, give a generic crash. 1942 fatal("Crashing with number %d", how); 1943 } 1944 tty->print_cr("controlled_crash: survived intentional crash. Did you suppress the assert?"); 1945 ShouldNotReachHere(); 1946 } 1947 #endif // !ASSERT