1 /* 2 * Copyright (c) 2003, 2015, 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 <fcntl.h> 26 #include "precompiled.hpp" 27 #include "code/codeCache.hpp" 28 #include "compiler/compileBroker.hpp" 29 #include "gc/shared/collectedHeap.hpp" 30 #include "prims/whitebox.hpp" 31 #include "runtime/arguments.hpp" 32 #include "runtime/atomic.inline.hpp" 33 #include "runtime/frame.inline.hpp" 34 #include "runtime/init.hpp" 35 #include "runtime/os.hpp" 36 #include "runtime/thread.inline.hpp" 37 #include "runtime/vmThread.hpp" 38 #include "runtime/vm_operations.hpp" 39 #include "services/memTracker.hpp" 40 #include "utilities/debug.hpp" 41 #include "utilities/decoder.hpp" 42 #include "utilities/defaultStream.hpp" 43 #include "utilities/errorReporter.hpp" 44 #include "utilities/events.hpp" 45 #include "utilities/top.hpp" 46 #include "utilities/vmError.hpp" 47 48 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 49 50 // List of environment variables that should be reported in error log file. 51 const char *env_list[] = { 52 // All platforms 53 "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH", 54 "JAVA_COMPILER", "PATH", "USERNAME", 55 56 // Env variables that are defined on Solaris/Linux/BSD 57 "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY", 58 "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE", 59 60 // defined on Linux 61 "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM", 62 63 // defined on Darwin 64 "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH", 65 "DYLD_FRAMEWORK_PATH", "DYLD_FALLBACK_FRAMEWORK_PATH", 66 "DYLD_INSERT_LIBRARIES", 67 68 // defined on Windows 69 "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR", 70 71 (const char *)0 72 }; 73 74 // A simple parser for -XX:OnError, usage: 75 // ptr = OnError; 76 // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL) 77 // ... ... 78 static char* next_OnError_command(char* buf, int buflen, const char** ptr) { 79 if (ptr == NULL || *ptr == NULL) return NULL; 80 81 const char* cmd = *ptr; 82 83 // skip leading blanks or ';' 84 while (*cmd == ' ' || *cmd == ';') cmd++; 85 86 if (*cmd == '\0') return NULL; 87 88 const char * cmdend = cmd; 89 while (*cmdend != '\0' && *cmdend != ';') cmdend++; 90 91 Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen); 92 93 *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1); 94 return buf; 95 } 96 97 static void print_bug_submit_message(outputStream *out, Thread *thread) { 98 if (out == NULL) return; 99 out->print_raw_cr("# If you would like to submit a bug report, please visit:"); 100 out->print_raw ("# "); 101 out->print_raw_cr(Arguments::java_vendor_url_bug()); 102 // If the crash is in native code, encourage user to submit a bug to the 103 // provider of that code. 104 if (thread && thread->is_Java_thread() && 105 !thread->is_hidden_from_external_view()) { 106 JavaThread* jt = (JavaThread*)thread; 107 if (jt->thread_state() == _thread_in_native) { 108 out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug."); 109 } 110 } 111 out->print_raw_cr("#"); 112 } 113 114 bool VMError::coredump_status; 115 char VMError::coredump_message[O_BUFLEN]; 116 117 void VMError::record_coredump_status(const char* message, bool status) { 118 coredump_status = status; 119 strncpy(coredump_message, message, sizeof(coredump_message)); 120 coredump_message[sizeof(coredump_message)-1] = 0; 121 } 122 123 // Return a string to describe the error 124 char* VMError::error_string(char* buf, int buflen, int id, const char* message, const char* detail_fmt, 125 va_list detail_args, address pc, const char* filename, int lineno) 126 { 127 char signame_buf[64]; 128 const char *signame = os::exception_name(id, signame_buf, sizeof(signame_buf)); 129 130 if (signame) { 131 jio_snprintf(buf, buflen, 132 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT, 133 signame, id, pc, 134 os::current_process_id(), os::current_thread_id()); 135 } else if (filename != NULL && lineno > 0) { 136 // skip directory names 137 char separator = os::file_separator()[0]; 138 const char *p = strrchr(filename, separator); 139 int n = jio_snprintf(buf, buflen, 140 "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT, 141 p ? p + 1 : filename, lineno, 142 os::current_process_id(), os::current_thread_id()); 143 if (n >= 0 && n < buflen && message) { 144 if (detail_fmt) { 145 int d = jio_snprintf(buf + n, buflen - n, "%s%s: ", os::line_separator(), message); 146 if (d >= 0) { 147 n += d; 148 if (n < buflen) { 149 jio_vsnprintf(buf + n, buflen - n, detail_fmt, detail_args); 150 } 151 } 152 } else { 153 jio_snprintf(buf + n, buflen - n, "%sError: %s", 154 os::line_separator(), message); 155 } 156 } 157 } else { 158 jio_snprintf(buf, buflen, 159 "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT, 160 id, os::current_process_id(), os::current_thread_id()); 161 } 162 163 return buf; 164 } 165 166 void VMError::print_stack_trace(outputStream* st, JavaThread* jt, 167 char* buf, int buflen, bool verbose) { 168 #ifdef ZERO 169 if (jt->zero_stack()->sp() && jt->top_zero_frame()) { 170 // StackFrameStream uses the frame anchor, which may not have 171 // been set up. This can be done at any time in Zero, however, 172 // so if it hasn't been set up then we just set it up now and 173 // clear it again when we're done. 174 bool has_last_Java_frame = jt->has_last_Java_frame(); 175 if (!has_last_Java_frame) 176 jt->set_last_Java_frame(); 177 st->print("Java frames:"); 178 179 // If the top frame is a Shark frame and the frame anchor isn't 180 // set up then it's possible that the information in the frame 181 // is garbage: it could be from a previous decache, or it could 182 // simply have never been written. So we print a warning... 183 StackFrameStream sfs(jt); 184 if (!has_last_Java_frame && !sfs.is_done()) { 185 if (sfs.current()->zeroframe()->is_shark_frame()) { 186 st->print(" (TOP FRAME MAY BE JUNK)"); 187 } 188 } 189 st->cr(); 190 191 // Print the frames 192 for(int i = 0; !sfs.is_done(); sfs.next(), i++) { 193 sfs.current()->zero_print_on_error(i, st, buf, buflen); 194 st->cr(); 195 } 196 197 // Reset the frame anchor if necessary 198 if (!has_last_Java_frame) 199 jt->reset_last_Java_frame(); 200 } 201 #else 202 if (jt->has_last_Java_frame()) { 203 st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)"); 204 for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) { 205 sfs.current()->print_on_error(st, buf, buflen, verbose); 206 st->cr(); 207 } 208 } 209 #endif // ZERO 210 } 211 212 void VMError::print_oom_reasons(outputStream* st) { 213 st->print_cr("# Possible reasons:"); 214 st->print_cr("# The system is out of physical RAM or swap space"); 215 st->print_cr("# In 32 bit mode, the process size limit was hit"); 216 st->print_cr("# Possible solutions:"); 217 st->print_cr("# Reduce memory load on the system"); 218 st->print_cr("# Increase physical memory or swap space"); 219 st->print_cr("# Check if swap backing store is full"); 220 st->print_cr("# Use 64 bit Java on a 64 bit OS"); 221 st->print_cr("# Decrease Java heap size (-Xmx/-Xms)"); 222 st->print_cr("# Decrease number of Java threads"); 223 st->print_cr("# Decrease Java thread stack sizes (-Xss)"); 224 st->print_cr("# Set larger code cache with -XX:ReservedCodeCacheSize="); 225 st->print_cr("# This output file may be truncated or incomplete."); 226 } 227 228 const char* VMError::gc_mode() { 229 if (UseG1GC) return "g1 gc"; 230 if (UseParallelGC) return "parallel gc"; 231 if (UseConcMarkSweepGC) return "concurrent mark sweep gc"; 232 if (UseSerialGC) return "serial gc"; 233 return "ERROR in GC mode"; 234 } 235 236 // This is the main function to report a fatal error. Only one thread can 237 // call this function, so we don't need to worry about MT-safety. But it's 238 // possible that the error handler itself may crash or die on an internal 239 // error, for example, when the stack/heap is badly damaged. We must be 240 // able to handle recursive errors that happen inside error handler. 241 // 242 // Error reporting is done in several steps. If a crash or internal error 243 // occurred when reporting an error, the nested signal/exception handler 244 // can skip steps that are already (or partially) done. Error reporting will 245 // continue from the next step. This allows us to retrieve and print 246 // information that may be unsafe to get after a fatal error. If it happens, 247 // you may find nested report_and_die() frames when you look at the stack 248 // in a debugger. 249 // 250 // In general, a hang in error handler is much worse than a crash or internal 251 // error, as it's harder to recover from a hang. Deadlock can happen if we 252 // try to grab a lock that is already owned by current thread, or if the 253 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the 254 // error handler and all the functions it called should avoid grabbing any 255 // lock. An important thing to notice is that memory allocation needs a lock. 256 // 257 // We should avoid using large stack allocated buffers. Many errors happen 258 // when stack space is already low. Making things even worse is that there 259 // could be nested report_and_die() calls on stack (see above). Only one 260 // thread can report error, so large buffers are statically allocated in data 261 // segment. 262 263 int VMError::_current_step; 264 const char* VMError::_current_step_info; 265 int VMError::_verbose; 266 267 void VMError::report(outputStream* st, int id, const char* message, const char* detail_fmt, 268 va_list detail_args, Thread* thread, address pc, void* siginfo, void* context, 269 const char* filename, int lineno, size_t size) { 270 # define BEGIN if (_current_step == 0) { _current_step = 1; 271 # define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s; 272 # define END } 273 274 // don't allocate large buffer on stack 275 static char buf[O_BUFLEN]; 276 277 BEGIN 278 279 STEP(10, "(printing fatal error message)") 280 281 st->print_cr("#"); 282 if (should_report_bug(id)) { 283 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:"); 284 } else { 285 st->print_cr("# There is insufficient memory for the Java " 286 "Runtime Environment to continue."); 287 } 288 289 #ifndef PRODUCT 290 // Error handler self tests 291 292 // test secondary error handling. Test it twice, to test that resetting 293 // error handler after a secondary crash works. 294 STEP(20, "(test secondary crash 1)") 295 if (_verbose && TestCrashInErrorHandler != 0) { 296 st->print_cr("Will crash now (TestCrashInErrorHandler=%d)...", 297 TestCrashInErrorHandler); 298 controlled_crash(TestCrashInErrorHandler); 299 } 300 301 STEP(30, "(test secondary crash 2)") 302 if (_verbose && TestCrashInErrorHandler != 0) { 303 st->print_cr("Will crash now (TestCrashInErrorHandler=%d)...", 304 TestCrashInErrorHandler); 305 controlled_crash(TestCrashInErrorHandler); 306 } 307 308 STEP(40, "(test safefetch in error handler)") 309 // test whether it is safe to use SafeFetch32 in Crash Handler. Test twice 310 // to test that resetting the signal handler works correctly. 311 if (_verbose && TestSafeFetchInErrorHandler) { 312 st->print_cr("Will test SafeFetch..."); 313 if (CanUseSafeFetch32()) { 314 int* const invalid_pointer = (int*) get_segfault_address(); 315 const int x = 0x76543210; 316 int i1 = SafeFetch32(invalid_pointer, x); 317 int i2 = SafeFetch32(invalid_pointer, x); 318 if (i1 == x && i2 == x) { 319 st->print_cr("SafeFetch OK."); // Correctly deflected and returned default pattern 320 } else { 321 st->print_cr("??"); 322 } 323 } else { 324 st->print_cr("not possible; skipped."); 325 } 326 } 327 #endif // PRODUCT 328 329 STEP(50, "(printing type of error)") 330 331 switch(id) { 332 case OOM_MALLOC_ERROR: 333 case OOM_MMAP_ERROR: 334 if (size) { 335 st->print("# Native memory allocation "); 336 st->print((id == (int)OOM_MALLOC_ERROR) ? "(malloc) failed to allocate " : 337 "(mmap) failed to map "); 338 jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, size); 339 st->print("%s", buf); 340 st->print(" bytes"); 341 if (detail_fmt != NULL) { 342 st->print(" for "); 343 st->vprint(detail_fmt, detail_args); 344 } 345 st->cr(); 346 } else { 347 if (detail_fmt != NULL) { 348 st->print("# "); 349 st->vprint_cr(detail_fmt, detail_args); 350 } 351 } 352 // In error file give some solutions 353 if (_verbose) { 354 print_oom_reasons(st); 355 } else { 356 return; // that's enough for the screen 357 } 358 break; 359 case INTERNAL_ERROR: 360 default: 361 break; 362 } 363 364 STEP(60, "(printing exception/signal name)") 365 366 st->print_cr("#"); 367 st->print("# "); 368 // Is it an OS exception/signal? 369 if (os::exception_name(id, buf, sizeof(buf))) { 370 st->print("%s", buf); 371 st->print(" (0x%x)", id); // signal number 372 st->print(" at pc=" PTR_FORMAT, pc); 373 } else { 374 if (should_report_bug(id)) { 375 st->print("Internal Error"); 376 } else { 377 st->print("Out of Memory Error"); 378 } 379 if (filename != NULL && lineno > 0) { 380 #ifdef PRODUCT 381 // In product mode chop off pathname? 382 char separator = os::file_separator()[0]; 383 const char *p = strrchr(filename, separator); 384 const char *file = p ? p+1 : filename; 385 #else 386 const char *file = filename; 387 #endif 388 st->print(" (%s:%d)", file, lineno); 389 } else { 390 st->print(" (0x%x)", id); 391 } 392 } 393 394 STEP(70, "(printing current thread and pid)") 395 396 // process id, thread id 397 st->print(", pid=%d", os::current_process_id()); 398 st->print(", tid=" UINTX_FORMAT, os::current_thread_id()); 399 st->cr(); 400 401 STEP(80, "(printing error message)") 402 403 if (should_report_bug(id)) { // already printed the message. 404 // error message 405 if (detail_fmt) { 406 st->print("# %s: ", message ? message : "Error"); 407 st->vprint_cr(detail_fmt, detail_args); 408 } else if (message) { 409 st->print_cr("# Error: %s", message); 410 } 411 } 412 413 STEP(90, "(printing Java version string)") 414 415 // VM version 416 st->print_cr("#"); 417 JDK_Version::current().to_string(buf, sizeof(buf)); 418 const char* runtime_name = JDK_Version::runtime_name() != NULL ? 419 JDK_Version::runtime_name() : ""; 420 const char* runtime_version = JDK_Version::runtime_version() != NULL ? 421 JDK_Version::runtime_version() : ""; 422 st->print_cr("# JRE version: %s (%s) (build %s)", runtime_name, buf, runtime_version); 423 // This is the long version with some default settings added 424 st->print_cr("# Java VM: %s (%s, %s%s%s, %s, %s)", 425 Abstract_VM_Version::vm_name(), 426 Abstract_VM_Version::vm_release(), 427 Abstract_VM_Version::vm_info_string(), 428 TieredCompilation ? ", tiered" : "", 429 UseCompressedOops ? ", compressed oops" : "", 430 gc_mode(), 431 Abstract_VM_Version::vm_platform_string() 432 ); 433 434 STEP(100, "(printing problematic frame)") 435 436 // Print current frame if we have a context (i.e. it's a crash) 437 if (context) { 438 st->print_cr("# Problematic frame:"); 439 st->print("# "); 440 frame fr = os::fetch_frame_from_context(context); 441 fr.print_on_error(st, buf, sizeof(buf)); 442 st->cr(); 443 st->print_cr("#"); 444 } 445 446 STEP(110, "(printing core file information)") 447 st->print("# "); 448 if (CreateCoredumpOnCrash) { 449 if (coredump_status) { 450 st->print("Core dump will be written. Default location: %s", coredump_message); 451 } else { 452 st->print("No core dump will be written. %s", coredump_message); 453 } 454 } else { 455 st->print("CreateCoredumpOnCrash turned off, no core file dumped"); 456 } 457 st->cr(); 458 st->print_cr("#"); 459 460 STEP(120, "(printing bug submit message)") 461 462 if (should_report_bug(id) && _verbose) { 463 print_bug_submit_message(st, thread); 464 } 465 466 STEP(130, "(printing summary)" ) 467 468 if (_verbose) { 469 st->cr(); 470 st->print_cr("--------------- S U M M A R Y ------------"); 471 st->cr(); 472 } 473 474 STEP(140, "(printing VM option summary)" ) 475 476 if (_verbose) { 477 // VM options 478 Arguments::print_summary_on(st); 479 st->cr(); 480 } 481 482 STEP(150, "(printing summary machine and OS info)") 483 484 if (_verbose) { 485 os::print_summary_info(st, buf, sizeof(buf)); 486 } 487 488 489 STEP(160, "(printing date and time)" ) 490 491 if (_verbose) { 492 os::print_date_and_time(st, buf, sizeof(buf)); 493 } 494 495 STEP(170, "(printing thread)" ) 496 497 if (_verbose) { 498 st->cr(); 499 st->print_cr("--------------- T H R E A D ---------------"); 500 st->cr(); 501 } 502 503 STEP(180, "(printing current thread)" ) 504 505 // current thread 506 if (_verbose) { 507 if (thread) { 508 st->print("Current thread (" PTR_FORMAT "): ", thread); 509 thread->print_on_error(st, buf, sizeof(buf)); 510 st->cr(); 511 } else { 512 st->print_cr("Current thread is native thread"); 513 } 514 st->cr(); 515 } 516 517 STEP(190, "(printing current compile task)" ) 518 519 if (_verbose && thread && thread->is_Compiler_thread()) { 520 CompilerThread* t = (CompilerThread*)thread; 521 if (t->task()) { 522 st->cr(); 523 st->print_cr("Current CompileTask:"); 524 t->task()->print_line_on_error(st, buf, sizeof(buf)); 525 st->cr(); 526 } 527 } 528 529 530 STEP(200, "(printing stack bounds)" ) 531 532 if (_verbose) { 533 st->print("Stack: "); 534 535 address stack_top; 536 size_t stack_size; 537 538 if (thread) { 539 stack_top = thread->stack_base(); 540 stack_size = thread->stack_size(); 541 } else { 542 stack_top = os::current_stack_base(); 543 stack_size = os::current_stack_size(); 544 } 545 546 address stack_bottom = stack_top - stack_size; 547 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top); 548 549 frame fr = context ? os::fetch_frame_from_context(context) 550 : os::current_frame(); 551 552 if (fr.sp()) { 553 st->print(", sp=" PTR_FORMAT, fr.sp()); 554 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024); 555 st->print(", free space=" SIZE_FORMAT "k", free_stack_size); 556 } 557 558 st->cr(); 559 } 560 561 STEP(210, "(printing native stack)" ) 562 563 if (_verbose) { 564 if (os::platform_print_native_stack(st, context, buf, sizeof(buf))) { 565 // We have printed the native stack in platform-specific code 566 // Windows/x64 needs special handling. 567 } else { 568 frame fr = context ? os::fetch_frame_from_context(context) 569 : os::current_frame(); 570 571 print_native_stack(st, fr, thread, buf, sizeof(buf)); 572 } 573 } 574 575 STEP(220, "(printing Java stack)" ) 576 577 if (_verbose && thread && thread->is_Java_thread()) { 578 print_stack_trace(st, (JavaThread*)thread, buf, sizeof(buf)); 579 } 580 581 STEP(230, "(printing target Java thread stack)" ) 582 583 // printing Java thread stack trace if it is involved in GC crash 584 if (_verbose && thread && (thread->is_Named_thread())) { 585 JavaThread* jt = ((NamedThread *)thread)->processed_thread(); 586 if (jt != NULL) { 587 st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id()); 588 print_stack_trace(st, jt, buf, sizeof(buf), true); 589 } 590 } 591 592 STEP(240, "(printing siginfo)" ) 593 594 // signal no, signal code, address that caused the fault 595 if (_verbose && siginfo) { 596 st->cr(); 597 os::print_siginfo(st, siginfo); 598 st->cr(); 599 } 600 601 STEP(250, "(printing register info)") 602 603 // decode register contents if possible 604 if (_verbose && context && Universe::is_fully_initialized()) { 605 os::print_register_info(st, context); 606 st->cr(); 607 } 608 609 STEP(260, "(printing registers, top of stack, instructions near pc)") 610 611 // registers, top of stack, instructions near pc 612 if (_verbose && context) { 613 os::print_context(st, context); 614 st->cr(); 615 } 616 617 STEP(270, "(printing VM operation)" ) 618 619 if (_verbose && thread && thread->is_VM_thread()) { 620 VMThread* t = (VMThread*)thread; 621 VM_Operation* op = t->vm_operation(); 622 if (op) { 623 op->print_on_error(st); 624 st->cr(); 625 st->cr(); 626 } 627 } 628 629 STEP(280, "(printing process)" ) 630 631 if (_verbose) { 632 st->cr(); 633 st->print_cr("--------------- P R O C E S S ---------------"); 634 st->cr(); 635 } 636 637 STEP(290, "(printing all threads)" ) 638 639 // all threads 640 if (_verbose && thread) { 641 Threads::print_on_error(st, thread, buf, sizeof(buf)); 642 st->cr(); 643 } 644 645 STEP(300, "(printing VM state)" ) 646 647 if (_verbose) { 648 // Safepoint state 649 st->print("VM state:"); 650 651 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing"); 652 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint"); 653 else st->print("not at safepoint"); 654 655 // Also see if error occurred during initialization or shutdown 656 if (!Universe::is_fully_initialized()) { 657 st->print(" (not fully initialized)"); 658 } else if (VM_Exit::vm_exited()) { 659 st->print(" (shutting down)"); 660 } else { 661 st->print(" (normal execution)"); 662 } 663 st->cr(); 664 st->cr(); 665 } 666 667 STEP(310, "(printing owned locks on error)" ) 668 669 // mutexes/monitors that currently have an owner 670 if (_verbose) { 671 print_owned_locks_on_error(st); 672 st->cr(); 673 } 674 675 STEP(320, "(printing number of OutOfMemoryError and StackOverflow exceptions)") 676 677 if (_verbose && Exceptions::has_exception_counts()) { 678 st->print_cr("OutOfMemory and StackOverflow Exception counts:"); 679 Exceptions::print_exception_counts_on_error(st); 680 st->cr(); 681 } 682 683 STEP(330, "(printing compressed oops mode") 684 685 if (_verbose && UseCompressedOops) { 686 Universe::print_compressed_oops_mode(st); 687 if (UseCompressedClassPointers) { 688 Metaspace::print_compressed_class_space(st); 689 } 690 st->cr(); 691 } 692 693 STEP(340, "(printing heap information)" ) 694 695 if (_verbose && Universe::is_fully_initialized()) { 696 Universe::heap()->print_on_error(st); 697 st->cr(); 698 699 st->print_cr("Polling page: " INTPTR_FORMAT, os::get_polling_page()); 700 st->cr(); 701 } 702 703 STEP(350, "(printing code cache information)" ) 704 705 if (_verbose && Universe::is_fully_initialized()) { 706 // print code cache information before vm abort 707 CodeCache::print_summary(st); 708 st->cr(); 709 } 710 711 STEP(360, "(printing ring buffers)" ) 712 713 if (_verbose) { 714 Events::print_all(st); 715 st->cr(); 716 } 717 718 STEP(370, "(printing dynamic libraries)" ) 719 720 if (_verbose) { 721 // dynamic libraries, or memory map 722 os::print_dll_info(st); 723 st->cr(); 724 } 725 726 STEP(380, "(printing VM options)" ) 727 728 if (_verbose) { 729 // VM options 730 Arguments::print_on(st); 731 st->cr(); 732 } 733 734 STEP(390, "(printing warning if internal testing API used)" ) 735 736 if (WhiteBox::used()) { 737 st->print_cr("Unsupported internal testing APIs have been used."); 738 st->cr(); 739 } 740 741 STEP(400, "(printing all environment variables)" ) 742 743 if (_verbose) { 744 os::print_environment_variables(st, env_list); 745 st->cr(); 746 } 747 748 STEP(410, "(printing signal handlers)" ) 749 750 if (_verbose) { 751 os::print_signal_handlers(st, buf, sizeof(buf)); 752 st->cr(); 753 } 754 755 STEP(420, "(Native Memory Tracking)" ) 756 if (_verbose) { 757 MemTracker::error_report(st); 758 } 759 760 STEP(430, "(printing system)" ) 761 762 if (_verbose) { 763 st->cr(); 764 st->print_cr("--------------- S Y S T E M ---------------"); 765 st->cr(); 766 } 767 768 STEP(440, "(printing OS information)" ) 769 770 if (_verbose) { 771 os::print_os_info(st); 772 st->cr(); 773 } 774 775 STEP(450, "(printing CPU info)" ) 776 if (_verbose) { 777 os::print_cpu_info(st, buf, sizeof(buf)); 778 st->cr(); 779 } 780 781 STEP(460, "(printing memory info)" ) 782 783 if (_verbose) { 784 os::print_memory_info(st); 785 st->cr(); 786 } 787 788 STEP(470, "(printing internal vm info)" ) 789 790 if (_verbose) { 791 st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string()); 792 st->cr(); 793 } 794 795 // print a defined marker to show that error handling finished correctly. 796 STEP(480, "(printing end marker)" ) 797 798 if (_verbose) { 799 st->print_cr("END."); 800 } 801 802 END 803 804 # undef BEGIN 805 # undef STEP 806 # undef END 807 } 808 809 volatile jlong VMError::first_error_tid = -1; 810 811 // An error could happen before tty is initialized or after it has been 812 // destroyed. Here we use a very simple unbuffered fdStream for printing. 813 // Only out.print_raw() and out.print_raw_cr() should be used, as other 814 // printing methods need to allocate large buffer on stack. To format a 815 // string, use jio_snprintf() with a static buffer or use staticBufferStream. 816 fdStream VMError::out(defaultStream::output_fd()); 817 fdStream VMError::log; // error log used by VMError::report_and_die() 818 819 /** Expand a pattern into a buffer starting at pos and open a file using constructed path */ 820 static int expand_and_open(const char* pattern, char* buf, size_t buflen, size_t pos) { 821 int fd = -1; 822 if (Arguments::copy_expand_pid(pattern, strlen(pattern), &buf[pos], buflen - pos)) { 823 // the O_EXCL flag will cause the open to fail if the file exists 824 fd = open(buf, O_RDWR | O_CREAT | O_EXCL, 0666); 825 } 826 return fd; 827 } 828 829 /** 830 * Construct file name for a log file and return it's file descriptor. 831 * Name and location depends on pattern, default_pattern params and access 832 * permissions. 833 */ 834 static int prepare_log_file(const char* pattern, const char* default_pattern, char* buf, size_t buflen) { 835 int fd = -1; 836 837 // If possible, use specified pattern to construct log file name 838 if (pattern != NULL) { 839 fd = expand_and_open(pattern, buf, buflen, 0); 840 } 841 842 // Either user didn't specify, or the user's location failed, 843 // so use the default name in the current directory 844 if (fd == -1) { 845 const char* cwd = os::get_current_directory(buf, buflen); 846 if (cwd != NULL) { 847 size_t pos = strlen(cwd); 848 int fsep_len = jio_snprintf(&buf[pos], buflen-pos, "%s", os::file_separator()); 849 pos += fsep_len; 850 if (fsep_len > 0) { 851 fd = expand_and_open(default_pattern, buf, buflen, pos); 852 } 853 } 854 } 855 856 // try temp directory if it exists. 857 if (fd == -1) { 858 const char* tmpdir = os::get_temp_directory(); 859 if (tmpdir != NULL && strlen(tmpdir) > 0) { 860 int pos = jio_snprintf(buf, buflen, "%s%s", tmpdir, os::file_separator()); 861 if (pos > 0) { 862 fd = expand_and_open(default_pattern, buf, buflen, pos); 863 } 864 } 865 } 866 867 return fd; 868 } 869 870 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, 871 void* context, const char* detail_fmt, ...) 872 { 873 va_list detail_args; 874 va_start(detail_args, detail_fmt); 875 report_and_die(sig, NULL, detail_fmt, detail_args, thread, pc, siginfo, context, NULL, 0, 0); 876 va_end(detail_args); 877 } 878 879 void VMError::report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context) 880 { 881 report_and_die(thread, sig, pc, siginfo, context, "%s", ""); 882 } 883 884 void VMError::report_and_die(const char* message, const char* detail_fmt, ...) 885 { 886 va_list detail_args; 887 va_start(detail_args, detail_fmt); 888 report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, NULL, NULL, NULL, NULL, NULL, 0, 0); 889 va_end(detail_args); 890 } 891 892 void VMError::report_and_die(const char* message) 893 { 894 report_and_die(message, "%s", ""); 895 } 896 897 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, const char* message, 898 const char* detail_fmt, va_list detail_args) 899 { 900 report_and_die(INTERNAL_ERROR, message, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, 0); 901 } 902 903 void VMError::report_and_die(Thread* thread, const char* filename, int lineno, size_t size, 904 VMErrorType vm_err_type, const char* detail_fmt, va_list detail_args) { 905 report_and_die(vm_err_type, NULL, detail_fmt, detail_args, thread, NULL, NULL, NULL, filename, lineno, size); 906 } 907 908 void VMError::report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args, 909 Thread* thread, address pc, void* siginfo, void* context, const char* filename, 910 int lineno, size_t size) 911 { 912 // Don't allocate large buffer on stack 913 static char buffer[O_BUFLEN]; 914 915 // How many errors occurred in error handler when reporting first_error. 916 static int recursive_error_count; 917 918 // We will first print a brief message to standard out (verbose = false), 919 // then save detailed information in log file (verbose = true). 920 static bool out_done = false; // done printing to standard out 921 static bool log_done = false; // done saving error log 922 static bool transmit_report_done = false; // done error reporting 923 924 if (SuppressFatalErrorMessage) { 925 os::abort(CreateCoredumpOnCrash); 926 } 927 jlong mytid = os::current_thread_id(); 928 if (first_error_tid == -1 && 929 Atomic::cmpxchg(mytid, &first_error_tid, -1) == -1) { 930 931 // first time 932 set_error_reported(); 933 934 if (ShowMessageBoxOnError || PauseAtExit) { 935 va_list tmp_args; 936 va_copy(tmp_args, detail_args); 937 show_message_box(buffer, sizeof(buffer), id, message, detail_fmt, tmp_args, pc, filename, lineno); 938 va_end(tmp_args); 939 940 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the 941 // WatcherThread can kill JVM if the error handler hangs. 942 ShowMessageBoxOnError = false; 943 } 944 945 os::check_dump_limit(buffer, sizeof(buffer)); 946 947 // reset signal handlers or exception filter; make sure recursive crashes 948 // are handled properly. 949 reset_signal_handlers(); 950 951 } else { 952 // If UseOsErrorReporting we call this for each level of the call stack 953 // while searching for the exception handler. Only the first level needs 954 // to be reported. 955 if (UseOSErrorReporting && log_done) return; 956 957 // This is not the first error, see if it happened in a different thread 958 // or in the same thread during error reporting. 959 if (first_error_tid != mytid) { 960 char msgbuf[64]; 961 jio_snprintf(msgbuf, sizeof(msgbuf), 962 "[thread " INT64_FORMAT " also had an error]", 963 mytid); 964 out.print_raw_cr(msgbuf); 965 966 // error reporting is not MT-safe, block current thread 967 os::infinite_sleep(); 968 969 } else { 970 if (recursive_error_count++ > 30) { 971 out.print_raw_cr("[Too many errors, abort]"); 972 os::die(); 973 } 974 975 jio_snprintf(buffer, sizeof(buffer), 976 "[error occurred during error reporting %s, id 0x%x]", 977 _current_step_info, id); 978 if (log.is_open()) { 979 log.cr(); 980 log.print_raw_cr(buffer); 981 log.cr(); 982 } else { 983 out.cr(); 984 out.print_raw_cr(buffer); 985 out.cr(); 986 } 987 } 988 } 989 990 // print to screen 991 if (!out_done) { 992 _verbose = false; 993 994 staticBufferStream sbs(buffer, sizeof(buffer), &out); 995 va_list tmp_args; 996 va_copy(tmp_args, detail_args); 997 report(&sbs, id, message, detail_fmt, tmp_args, thread, pc, siginfo, context, filename, lineno, size); 998 va_end(tmp_args); 999 1000 out_done = true; 1001 1002 _current_step = 0; // reset current_step 1003 _current_step_info = ""; // reset current_step string 1004 } 1005 1006 // print to error log file 1007 if (!log_done) { 1008 _verbose = true; 1009 1010 // see if log file is already open 1011 if (!log.is_open()) { 1012 // open log file 1013 int fd = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer)); 1014 if (fd != -1) { 1015 out.print_raw("# An error report file with more information is saved as:\n# "); 1016 out.print_raw_cr(buffer); 1017 1018 log.set_fd(fd); 1019 } else { 1020 out.print_raw_cr("# Can not save log file, dump to screen.."); 1021 log.set_fd(defaultStream::output_fd()); 1022 /* Error reporting currently needs dumpfile. 1023 * Maybe implement direct streaming in the future.*/ 1024 transmit_report_done = true; 1025 } 1026 } 1027 1028 staticBufferStream sbs(buffer, O_BUFLEN, &log); 1029 va_list tmp_args; 1030 va_copy(tmp_args, detail_args); 1031 report(&sbs, id, message, detail_fmt, tmp_args, thread, pc, siginfo, context, filename, lineno, size); 1032 va_end(tmp_args); 1033 _current_step = 0; // reset current_step 1034 _current_step_info = ""; // reset current_step string 1035 1036 // Run error reporting to determine whether or not to report the crash. 1037 if (!transmit_report_done && should_report_bug(id)) { 1038 transmit_report_done = true; 1039 const int fd2 = ::dup(log.fd()); 1040 FILE* const hs_err = ::fdopen(fd2, "r"); 1041 if (NULL != hs_err) { 1042 ErrorReporter er; 1043 er.call(hs_err, buffer, O_BUFLEN); 1044 } 1045 ::fclose(hs_err); 1046 } 1047 1048 if (log.fd() != defaultStream::output_fd()) { 1049 close(log.fd()); 1050 } 1051 1052 log.set_fd(-1); 1053 log_done = true; 1054 } 1055 1056 1057 static bool skip_OnError = false; 1058 if (!skip_OnError && OnError && OnError[0]) { 1059 skip_OnError = true; 1060 1061 out.print_raw_cr("#"); 1062 out.print_raw ("# -XX:OnError=\""); 1063 out.print_raw (OnError); 1064 out.print_raw_cr("\""); 1065 1066 char* cmd; 1067 const char* ptr = OnError; 1068 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){ 1069 out.print_raw ("# Executing "); 1070 #if defined(LINUX) || defined(_ALLBSD_SOURCE) 1071 out.print_raw ("/bin/sh -c "); 1072 #elif defined(SOLARIS) 1073 out.print_raw ("/usr/bin/sh -c "); 1074 #endif 1075 out.print_raw ("\""); 1076 out.print_raw (cmd); 1077 out.print_raw_cr("\" ..."); 1078 1079 if (os::fork_and_exec(cmd) < 0) { 1080 out.print_cr("os::fork_and_exec failed: %s (%d)", strerror(errno), errno); 1081 } 1082 } 1083 1084 // done with OnError 1085 OnError = NULL; 1086 } 1087 1088 static bool skip_replay = ReplayCompiles; // Do not overwrite file during replay 1089 if (DumpReplayDataOnError && thread && thread->is_Compiler_thread() && !skip_replay) { 1090 skip_replay = true; 1091 ciEnv* env = ciEnv::current(); 1092 if (env != NULL) { 1093 int fd = prepare_log_file(ReplayDataFile, "replay_pid%p.log", buffer, sizeof(buffer)); 1094 if (fd != -1) { 1095 FILE* replay_data_file = os::open(fd, "w"); 1096 if (replay_data_file != NULL) { 1097 fileStream replay_data_stream(replay_data_file, /*need_close=*/true); 1098 env->dump_replay_data_unsafe(&replay_data_stream); 1099 out.print_raw("#\n# Compiler replay data is saved as:\n# "); 1100 out.print_raw_cr(buffer); 1101 } else { 1102 out.print_raw("#\n# Can't open file to dump replay data. Error: "); 1103 out.print_raw_cr(strerror(os::get_last_error())); 1104 } 1105 } 1106 } 1107 } 1108 1109 static bool skip_bug_url = !should_report_bug(id); 1110 if (!skip_bug_url) { 1111 skip_bug_url = true; 1112 1113 out.print_raw_cr("#"); 1114 print_bug_submit_message(&out, thread); 1115 } 1116 1117 if (!UseOSErrorReporting) { 1118 // os::abort() will call abort hooks, try it first. 1119 static bool skip_os_abort = false; 1120 if (!skip_os_abort) { 1121 skip_os_abort = true; 1122 bool dump_core = should_report_bug(id); 1123 os::abort(dump_core && CreateCoredumpOnCrash, siginfo, context); 1124 } 1125 1126 // if os::abort() doesn't abort, try os::die(); 1127 os::die(); 1128 } 1129 } 1130 1131 /* 1132 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this 1133 * ensures utilities such as jmap can observe the process is a consistent state. 1134 */ 1135 class VM_ReportJavaOutOfMemory : public VM_Operation { 1136 private: 1137 const char* _message; 1138 public: 1139 VM_ReportJavaOutOfMemory(const char* message) { _message = message; } 1140 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; } 1141 void doit(); 1142 }; 1143 1144 void VM_ReportJavaOutOfMemory::doit() { 1145 // Don't allocate large buffer on stack 1146 static char buffer[O_BUFLEN]; 1147 1148 tty->print_cr("#"); 1149 tty->print_cr("# java.lang.OutOfMemoryError: %s", _message); 1150 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError); 1151 1152 // make heap parsability 1153 Universe::heap()->ensure_parsability(false); // no need to retire TLABs 1154 1155 char* cmd; 1156 const char* ptr = OnOutOfMemoryError; 1157 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){ 1158 tty->print("# Executing "); 1159 #if defined(LINUX) 1160 tty->print ("/bin/sh -c "); 1161 #elif defined(SOLARIS) 1162 tty->print ("/usr/bin/sh -c "); 1163 #endif 1164 tty->print_cr("\"%s\"...", cmd); 1165 1166 if (os::fork_and_exec(cmd) < 0) { 1167 tty->print_cr("os::fork_and_exec failed: %s (%d)", strerror(errno), errno); 1168 } 1169 } 1170 } 1171 1172 void VMError::report_java_out_of_memory(const char* message) { 1173 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) { 1174 MutexLocker ml(Heap_lock); 1175 VM_ReportJavaOutOfMemory op(message); 1176 VMThread::execute(&op); 1177 } 1178 }