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