Mon, 03 Jan 2011 14:09:11 -0500
6302804: Hotspot VM dies ungraceful death when C heap is exhausted in various places.
Summary: enhance the error reporting mechanism to help user to fix the problem rather than making it look like a VM error.
Reviewed-by: kvn, kamg
1 /*
2 * Copyright (c) 2003, 2010, 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 */
25 #include "precompiled.hpp"
26 #include "compiler/compileBroker.hpp"
27 #include "gc_interface/collectedHeap.hpp"
28 #include "runtime/arguments.hpp"
29 #include "runtime/frame.inline.hpp"
30 #include "runtime/init.hpp"
31 #include "runtime/os.hpp"
32 #include "runtime/thread.hpp"
33 #include "runtime/vmThread.hpp"
34 #include "runtime/vm_operations.hpp"
35 #include "utilities/debug.hpp"
36 #include "utilities/decoder.hpp"
37 #include "utilities/defaultStream.hpp"
38 #include "utilities/top.hpp"
39 #include "utilities/vmError.hpp"
41 // List of environment variables that should be reported in error log file.
42 const char *env_list[] = {
43 // All platforms
44 "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
45 "JAVA_COMPILER", "PATH", "USERNAME",
47 // Env variables that are defined on Solaris/Linux
48 "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
49 "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
51 // defined on Linux
52 "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
54 // defined on Windows
55 "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
57 (const char *)0
58 };
60 // Fatal error handler for internal errors and crashes.
61 //
62 // The default behavior of fatal error handler is to print a brief message
63 // to standard out (defaultStream::output_fd()), then save detailed information
64 // into an error report file (hs_err_pid<pid>.log) and abort VM. If multiple
65 // threads are having troubles at the same time, only one error is reported.
66 // The thread that is reporting error will abort VM when it is done, all other
67 // threads are blocked forever inside report_and_die().
69 // Constructor for crashes
70 VMError::VMError(Thread* thread, unsigned int sig, address pc, void* siginfo, void* context) {
71 _thread = thread;
72 _id = sig;
73 _pc = pc;
74 _siginfo = siginfo;
75 _context = context;
77 _verbose = false;
78 _current_step = 0;
79 _current_step_info = NULL;
81 _message = NULL;
82 _detail_msg = NULL;
83 _filename = NULL;
84 _lineno = 0;
86 _size = 0;
87 }
89 // Constructor for internal errors
90 VMError::VMError(Thread* thread, const char* filename, int lineno,
91 const char* message, const char * detail_msg)
92 {
93 _thread = thread;
94 _id = internal_error; // Value that's not an OS exception/signal
95 _filename = filename;
96 _lineno = lineno;
97 _message = message;
98 _detail_msg = detail_msg;
100 _verbose = false;
101 _current_step = 0;
102 _current_step_info = NULL;
104 _pc = NULL;
105 _siginfo = NULL;
106 _context = NULL;
108 _size = 0;
109 }
111 // Constructor for OOM errors
112 VMError::VMError(Thread* thread, const char* filename, int lineno, size_t size,
113 const char* message) {
114 _thread = thread;
115 _id = oom_error; // Value that's not an OS exception/signal
116 _filename = filename;
117 _lineno = lineno;
118 _message = message;
119 _detail_msg = NULL;
121 _verbose = false;
122 _current_step = 0;
123 _current_step_info = NULL;
125 _pc = NULL;
126 _siginfo = NULL;
127 _context = NULL;
129 _size = size;
130 }
133 // Constructor for non-fatal errors
134 VMError::VMError(const char* message) {
135 _thread = NULL;
136 _id = internal_error; // Value that's not an OS exception/signal
137 _filename = NULL;
138 _lineno = 0;
139 _message = message;
140 _detail_msg = NULL;
142 _verbose = false;
143 _current_step = 0;
144 _current_step_info = NULL;
146 _pc = NULL;
147 _siginfo = NULL;
148 _context = NULL;
150 _size = 0;
151 }
153 // -XX:OnError=<string>, where <string> can be a list of commands, separated
154 // by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by
155 // a single "%". Some examples:
156 //
157 // -XX:OnError="pmap %p" // show memory map
158 // -XX:OnError="gcore %p; dbx - %p" // dump core and launch debugger
159 // -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com"
160 // -XX:OnError="kill -9 %p" // ?#!@#
162 // A simple parser for -XX:OnError, usage:
163 // ptr = OnError;
164 // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
165 // ... ...
166 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
167 if (ptr == NULL || *ptr == NULL) return NULL;
169 const char* cmd = *ptr;
171 // skip leading blanks or ';'
172 while (*cmd == ' ' || *cmd == ';') cmd++;
174 if (*cmd == '\0') return NULL;
176 const char * cmdend = cmd;
177 while (*cmdend != '\0' && *cmdend != ';') cmdend++;
179 Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
181 *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
182 return buf;
183 }
186 static void print_bug_submit_message(outputStream *out, Thread *thread) {
187 if (out == NULL) return;
188 out->print_raw_cr("# If you would like to submit a bug report, please visit:");
189 out->print_raw ("# ");
190 out->print_raw_cr(Arguments::java_vendor_url_bug());
191 // If the crash is in native code, encourage user to submit a bug to the
192 // provider of that code.
193 if (thread && thread->is_Java_thread() &&
194 !thread->is_hidden_from_external_view()) {
195 JavaThread* jt = (JavaThread*)thread;
196 if (jt->thread_state() == _thread_in_native) {
197 out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
198 }
199 }
200 out->print_raw_cr("#");
201 }
204 // Return a string to describe the error
205 char* VMError::error_string(char* buf, int buflen) {
206 char signame_buf[64];
207 const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
209 if (signame) {
210 jio_snprintf(buf, buflen,
211 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
212 signame, _id, _pc,
213 os::current_process_id(), os::current_thread_id());
214 } else if (_filename != NULL && _lineno > 0) {
215 // skip directory names
216 char separator = os::file_separator()[0];
217 const char *p = strrchr(_filename, separator);
218 int n = jio_snprintf(buf, buflen,
219 "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
220 p ? p + 1 : _filename, _lineno,
221 os::current_process_id(), os::current_thread_id());
222 if (n >= 0 && n < buflen && _message) {
223 if (_detail_msg) {
224 jio_snprintf(buf + n, buflen - n, "%s%s: %s",
225 os::line_separator(), _message, _detail_msg);
226 } else {
227 jio_snprintf(buf + n, buflen - n, "%sError: %s",
228 os::line_separator(), _message);
229 }
230 }
231 } else {
232 jio_snprintf(buf, buflen,
233 "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
234 _id, os::current_process_id(), os::current_thread_id());
235 }
237 return buf;
238 }
240 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
241 char* buf, int buflen, bool verbose) {
242 #ifdef ZERO
243 if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
244 // StackFrameStream uses the frame anchor, which may not have
245 // been set up. This can be done at any time in Zero, however,
246 // so if it hasn't been set up then we just set it up now and
247 // clear it again when we're done.
248 bool has_last_Java_frame = jt->has_last_Java_frame();
249 if (!has_last_Java_frame)
250 jt->set_last_Java_frame();
251 st->print("Java frames:");
253 // If the top frame is a Shark frame and the frame anchor isn't
254 // set up then it's possible that the information in the frame
255 // is garbage: it could be from a previous decache, or it could
256 // simply have never been written. So we print a warning...
257 StackFrameStream sfs(jt);
258 if (!has_last_Java_frame && !sfs.is_done()) {
259 if (sfs.current()->zeroframe()->is_shark_frame()) {
260 st->print(" (TOP FRAME MAY BE JUNK)");
261 }
262 }
263 st->cr();
265 // Print the frames
266 for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
267 sfs.current()->zero_print_on_error(i, st, buf, buflen);
268 st->cr();
269 }
271 // Reset the frame anchor if necessary
272 if (!has_last_Java_frame)
273 jt->reset_last_Java_frame();
274 }
275 #else
276 if (jt->has_last_Java_frame()) {
277 st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
278 for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
279 sfs.current()->print_on_error(st, buf, buflen, verbose);
280 st->cr();
281 }
282 }
283 #endif // ZERO
284 }
286 // This is the main function to report a fatal error. Only one thread can
287 // call this function, so we don't need to worry about MT-safety. But it's
288 // possible that the error handler itself may crash or die on an internal
289 // error, for example, when the stack/heap is badly damaged. We must be
290 // able to handle recursive errors that happen inside error handler.
291 //
292 // Error reporting is done in several steps. If a crash or internal error
293 // occurred when reporting an error, the nested signal/exception handler
294 // can skip steps that are already (or partially) done. Error reporting will
295 // continue from the next step. This allows us to retrieve and print
296 // information that may be unsafe to get after a fatal error. If it happens,
297 // you may find nested report_and_die() frames when you look at the stack
298 // in a debugger.
299 //
300 // In general, a hang in error handler is much worse than a crash or internal
301 // error, as it's harder to recover from a hang. Deadlock can happen if we
302 // try to grab a lock that is already owned by current thread, or if the
303 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
304 // error handler and all the functions it called should avoid grabbing any
305 // lock. An important thing to notice is that memory allocation needs a lock.
306 //
307 // We should avoid using large stack allocated buffers. Many errors happen
308 // when stack space is already low. Making things even worse is that there
309 // could be nested report_and_die() calls on stack (see above). Only one
310 // thread can report error, so large buffers are statically allocated in data
311 // segment.
313 void VMError::report(outputStream* st) {
314 # define BEGIN if (_current_step == 0) { _current_step = 1;
315 # define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s;
316 # define END }
318 // don't allocate large buffer on stack
319 static char buf[O_BUFLEN];
321 BEGIN
323 STEP(10, "(printing fatal error message)")
325 st->print_cr("#");
326 if (should_report_bug(_id)) {
327 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
328 } else {
329 st->print_cr("# There is insufficient memory for the Java "
330 "Runtime Environment to continue.");
331 }
333 STEP(15, "(printing type of error)")
335 switch(_id) {
336 case oom_error:
337 if (_size) {
338 st->print("# Native memory allocation (malloc) failed to allocate ");
339 jio_snprintf(buf, sizeof(buf), SIZE_FORMAT, _size);
340 st->print(buf);
341 st->print(" bytes");
342 if (_message != NULL) {
343 st->print(" for ");
344 st->print(_message);
345 }
346 st->cr();
347 } else {
348 if (_message != NULL)
349 st->print("# ");
350 st->print_cr(_message);
351 }
352 // In error file give some solutions
353 if (_verbose) {
354 st->print_cr("# Possible reasons:");
355 st->print_cr("# The system is out of physical RAM or swap space");
356 st->print_cr("# In 32 bit mode, the process size limit was hit");
357 st->print_cr("# Possible solutions:");
358 st->print_cr("# Reduce memory load on the system");
359 st->print_cr("# Increase physical memory or swap space");
360 st->print_cr("# Check if swap backing store is full");
361 st->print_cr("# Use 64 bit Java on a 64 bit OS");
362 st->print_cr("# Decrease Java heap size (-Xmx/-Xms)");
363 st->print_cr("# Decrease number of Java threads");
364 st->print_cr("# Decrease Java thread stack sizes (-Xss)");
365 st->print_cr("# Set larger code cache with -XX:ReservedCodeCacheSize=");
366 st->print_cr("# This output file may be truncated or incomplete.");
367 } else {
368 return; // that's enough for the screen
369 }
370 break;
371 case internal_error:
372 default:
373 break;
374 }
376 STEP(20, "(printing exception/signal name)")
378 st->print_cr("#");
379 st->print("# ");
380 // Is it an OS exception/signal?
381 if (os::exception_name(_id, buf, sizeof(buf))) {
382 st->print("%s", buf);
383 st->print(" (0x%x)", _id); // signal number
384 st->print(" at pc=" PTR_FORMAT, _pc);
385 } else {
386 if (should_report_bug(_id)) {
387 st->print("Internal Error");
388 } else {
389 st->print("Out of Memory Error");
390 }
391 if (_filename != NULL && _lineno > 0) {
392 #ifdef PRODUCT
393 // In product mode chop off pathname?
394 char separator = os::file_separator()[0];
395 const char *p = strrchr(_filename, separator);
396 const char *file = p ? p+1 : _filename;
397 #else
398 const char *file = _filename;
399 #endif
400 size_t len = strlen(file);
401 size_t buflen = sizeof(buf);
403 strncpy(buf, file, buflen);
404 if (len + 10 < buflen) {
405 sprintf(buf + len, ":%d", _lineno);
406 }
407 st->print(" (%s)", buf);
408 } else {
409 st->print(" (0x%x)", _id);
410 }
411 }
413 STEP(30, "(printing current thread and pid)")
415 // process id, thread id
416 st->print(", pid=%d", os::current_process_id());
417 st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
418 st->cr();
420 STEP(40, "(printing error message)")
422 if (should_report_bug(_id)) { // already printed the message.
423 // error message
424 if (_detail_msg) {
425 st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg);
426 } else if (_message) {
427 st->print_cr("# Error: %s", _message);
428 }
429 }
431 STEP(50, "(printing Java version string)")
433 // VM version
434 st->print_cr("#");
435 JDK_Version::current().to_string(buf, sizeof(buf));
436 st->print_cr("# JRE version: %s", buf);
437 st->print_cr("# Java VM: %s (%s %s %s %s)",
438 Abstract_VM_Version::vm_name(),
439 Abstract_VM_Version::vm_release(),
440 Abstract_VM_Version::vm_info_string(),
441 Abstract_VM_Version::vm_platform_string(),
442 UseCompressedOops ? "compressed oops" : ""
443 );
445 STEP(60, "(printing problematic frame)")
447 // Print current frame if we have a context (i.e. it's a crash)
448 if (_context) {
449 st->print_cr("# Problematic frame:");
450 st->print("# ");
451 frame fr = os::fetch_frame_from_context(_context);
452 fr.print_on_error(st, buf, sizeof(buf));
453 st->cr();
454 st->print_cr("#");
455 }
457 STEP(65, "(printing bug submit message)")
459 if (should_report_bug(_id) && _verbose) {
460 print_bug_submit_message(st, _thread);
461 }
463 STEP(70, "(printing thread)" )
465 if (_verbose) {
466 st->cr();
467 st->print_cr("--------------- T H R E A D ---------------");
468 st->cr();
469 }
471 STEP(80, "(printing current thread)" )
473 // current thread
474 if (_verbose) {
475 if (_thread) {
476 st->print("Current thread (" PTR_FORMAT "): ", _thread);
477 _thread->print_on_error(st, buf, sizeof(buf));
478 st->cr();
479 } else {
480 st->print_cr("Current thread is native thread");
481 }
482 st->cr();
483 }
485 STEP(90, "(printing siginfo)" )
487 // signal no, signal code, address that caused the fault
488 if (_verbose && _siginfo) {
489 os::print_siginfo(st, _siginfo);
490 st->cr();
491 }
493 STEP(100, "(printing registers, top of stack, instructions near pc)")
495 // registers, top of stack, instructions near pc
496 if (_verbose && _context) {
497 os::print_context(st, _context);
498 st->cr();
499 }
501 STEP(105, "(printing register info)")
503 // decode register contents if possible
504 if (_verbose && _context && Universe::is_fully_initialized()) {
505 os::print_register_info(st, _context);
506 st->cr();
507 }
509 STEP(110, "(printing stack bounds)" )
511 if (_verbose) {
512 st->print("Stack: ");
514 address stack_top;
515 size_t stack_size;
517 if (_thread) {
518 stack_top = _thread->stack_base();
519 stack_size = _thread->stack_size();
520 } else {
521 stack_top = os::current_stack_base();
522 stack_size = os::current_stack_size();
523 }
525 address stack_bottom = stack_top - stack_size;
526 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
528 frame fr = _context ? os::fetch_frame_from_context(_context)
529 : os::current_frame();
531 if (fr.sp()) {
532 st->print(", sp=" PTR_FORMAT, fr.sp());
533 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
534 st->print(", free space=" SIZE_FORMAT "k", free_stack_size);
535 }
537 st->cr();
538 }
540 STEP(120, "(printing native stack)" )
542 if (_verbose) {
543 frame fr = _context ? os::fetch_frame_from_context(_context)
544 : os::current_frame();
546 // see if it's a valid frame
547 if (fr.pc()) {
548 st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
550 // initialize decoder to decode C frames
551 Decoder decoder;
553 int count = 0;
554 while (count++ < StackPrintLimit) {
555 fr.print_on_error(st, buf, sizeof(buf));
556 st->cr();
557 if (os::is_first_C_frame(&fr)) break;
558 fr = os::get_sender_for_C_frame(&fr);
559 }
561 if (count > StackPrintLimit) {
562 st->print_cr("...<more frames>...");
563 }
565 st->cr();
566 }
567 }
569 STEP(130, "(printing Java stack)" )
571 if (_verbose && _thread && _thread->is_Java_thread()) {
572 print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
573 }
575 STEP(135, "(printing target Java thread stack)" )
577 // printing Java thread stack trace if it is involved in GC crash
578 if (_verbose && _thread && (_thread->is_Named_thread())) {
579 JavaThread* jt = ((NamedThread *)_thread)->processed_thread();
580 if (jt != NULL) {
581 st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id());
582 print_stack_trace(st, jt, buf, sizeof(buf), true);
583 }
584 }
586 STEP(140, "(printing VM operation)" )
588 if (_verbose && _thread && _thread->is_VM_thread()) {
589 VMThread* t = (VMThread*)_thread;
590 VM_Operation* op = t->vm_operation();
591 if (op) {
592 op->print_on_error(st);
593 st->cr();
594 st->cr();
595 }
596 }
598 STEP(150, "(printing current compile task)" )
600 if (_verbose && _thread && _thread->is_Compiler_thread()) {
601 CompilerThread* t = (CompilerThread*)_thread;
602 if (t->task()) {
603 st->cr();
604 st->print_cr("Current CompileTask:");
605 t->task()->print_line_on_error(st, buf, sizeof(buf));
606 st->cr();
607 }
608 }
610 STEP(160, "(printing process)" )
612 if (_verbose) {
613 st->cr();
614 st->print_cr("--------------- P R O C E S S ---------------");
615 st->cr();
616 }
618 STEP(170, "(printing all threads)" )
620 // all threads
621 if (_verbose && _thread) {
622 Threads::print_on_error(st, _thread, buf, sizeof(buf));
623 st->cr();
624 }
626 STEP(175, "(printing VM state)" )
628 if (_verbose) {
629 // Safepoint state
630 st->print("VM state:");
632 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
633 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
634 else st->print("not at safepoint");
636 // Also see if error occurred during initialization or shutdown
637 if (!Universe::is_fully_initialized()) {
638 st->print(" (not fully initialized)");
639 } else if (VM_Exit::vm_exited()) {
640 st->print(" (shutting down)");
641 } else {
642 st->print(" (normal execution)");
643 }
644 st->cr();
645 st->cr();
646 }
648 STEP(180, "(printing owned locks on error)" )
650 // mutexes/monitors that currently have an owner
651 if (_verbose) {
652 print_owned_locks_on_error(st);
653 st->cr();
654 }
656 STEP(190, "(printing heap information)" )
658 if (_verbose && Universe::is_fully_initialized()) {
659 // print heap information before vm abort
660 Universe::print_on(st);
661 st->cr();
662 }
664 STEP(195, "(printing code cache information)" )
666 if (_verbose && Universe::is_fully_initialized()) {
667 // print code cache information before vm abort
668 CodeCache::print_bounds(st);
669 st->cr();
670 }
672 STEP(200, "(printing dynamic libraries)" )
674 if (_verbose) {
675 // dynamic libraries, or memory map
676 os::print_dll_info(st);
677 st->cr();
678 }
680 STEP(210, "(printing VM options)" )
682 if (_verbose) {
683 // VM options
684 Arguments::print_on(st);
685 st->cr();
686 }
688 STEP(220, "(printing environment variables)" )
690 if (_verbose) {
691 os::print_environment_variables(st, env_list, buf, sizeof(buf));
692 st->cr();
693 }
695 STEP(225, "(printing signal handlers)" )
697 if (_verbose) {
698 os::print_signal_handlers(st, buf, sizeof(buf));
699 st->cr();
700 }
702 STEP(230, "" )
704 if (_verbose) {
705 st->cr();
706 st->print_cr("--------------- S Y S T E M ---------------");
707 st->cr();
708 }
710 STEP(240, "(printing OS information)" )
712 if (_verbose) {
713 os::print_os_info(st);
714 st->cr();
715 }
717 STEP(250, "(printing CPU info)" )
718 if (_verbose) {
719 os::print_cpu_info(st);
720 st->cr();
721 }
723 STEP(260, "(printing memory info)" )
725 if (_verbose) {
726 os::print_memory_info(st);
727 st->cr();
728 }
730 STEP(270, "(printing internal vm info)" )
732 if (_verbose) {
733 st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
734 st->cr();
735 }
737 STEP(280, "(printing date and time)" )
739 if (_verbose) {
740 os::print_date_and_time(st);
741 st->cr();
742 }
744 END
746 # undef BEGIN
747 # undef STEP
748 # undef END
749 }
751 VMError* volatile VMError::first_error = NULL;
752 volatile jlong VMError::first_error_tid = -1;
754 void VMError::report_and_die() {
755 // Don't allocate large buffer on stack
756 static char buffer[O_BUFLEN];
758 // An error could happen before tty is initialized or after it has been
759 // destroyed. Here we use a very simple unbuffered fdStream for printing.
760 // Only out.print_raw() and out.print_raw_cr() should be used, as other
761 // printing methods need to allocate large buffer on stack. To format a
762 // string, use jio_snprintf() with a static buffer or use staticBufferStream.
763 static fdStream out(defaultStream::output_fd());
765 // How many errors occurred in error handler when reporting first_error.
766 static int recursive_error_count;
768 // We will first print a brief message to standard out (verbose = false),
769 // then save detailed information in log file (verbose = true).
770 static bool out_done = false; // done printing to standard out
771 static bool log_done = false; // done saving error log
772 static fdStream log; // error log
774 if (SuppressFatalErrorMessage) {
775 os::abort();
776 }
777 jlong mytid = os::current_thread_id();
778 if (first_error == NULL &&
779 Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
781 // first time
782 first_error_tid = mytid;
783 set_error_reported();
785 if (ShowMessageBoxOnError) {
786 show_message_box(buffer, sizeof(buffer));
788 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
789 // WatcherThread can kill JVM if the error handler hangs.
790 ShowMessageBoxOnError = false;
791 }
793 // reset signal handlers or exception filter; make sure recursive crashes
794 // are handled properly.
795 reset_signal_handlers();
797 } else {
798 // If UseOsErrorReporting we call this for each level of the call stack
799 // while searching for the exception handler. Only the first level needs
800 // to be reported.
801 if (UseOSErrorReporting && log_done) return;
803 // This is not the first error, see if it happened in a different thread
804 // or in the same thread during error reporting.
805 if (first_error_tid != mytid) {
806 jio_snprintf(buffer, sizeof(buffer),
807 "[thread " INT64_FORMAT " also had an error]",
808 mytid);
809 out.print_raw_cr(buffer);
811 // error reporting is not MT-safe, block current thread
812 os::infinite_sleep();
814 } else {
815 if (recursive_error_count++ > 30) {
816 out.print_raw_cr("[Too many errors, abort]");
817 os::die();
818 }
820 jio_snprintf(buffer, sizeof(buffer),
821 "[error occurred during error reporting %s, id 0x%x]",
822 first_error ? first_error->_current_step_info : "",
823 _id);
824 if (log.is_open()) {
825 log.cr();
826 log.print_raw_cr(buffer);
827 log.cr();
828 } else {
829 out.cr();
830 out.print_raw_cr(buffer);
831 out.cr();
832 }
833 }
834 }
836 // print to screen
837 if (!out_done) {
838 first_error->_verbose = false;
840 staticBufferStream sbs(buffer, sizeof(buffer), &out);
841 first_error->report(&sbs);
843 out_done = true;
845 first_error->_current_step = 0; // reset current_step
846 first_error->_current_step_info = ""; // reset current_step string
847 }
849 // print to error log file
850 if (!log_done) {
851 first_error->_verbose = true;
853 // see if log file is already open
854 if (!log.is_open()) {
855 // open log file
856 int fd = -1;
858 if (ErrorFile != NULL) {
859 bool copy_ok =
860 Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
861 if (copy_ok) {
862 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
863 }
864 }
866 if (fd == -1) {
867 const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
868 size_t len = strlen(cwd);
869 // either user didn't specify, or the user's location failed,
870 // so use the default name in the current directory
871 jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
872 os::file_separator(), os::current_process_id());
873 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
874 }
876 if (fd == -1) {
877 // try temp directory
878 const char * tmpdir = os::get_temp_directory();
879 jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
880 tmpdir, os::file_separator(), os::current_process_id());
881 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
882 }
884 if (fd != -1) {
885 out.print_raw("# An error report file with more information is saved as:\n# ");
886 out.print_raw_cr(buffer);
887 os::set_error_file(buffer);
889 log.set_fd(fd);
890 } else {
891 out.print_raw_cr("# Can not save log file, dump to screen..");
892 log.set_fd(defaultStream::output_fd());
893 }
894 }
896 staticBufferStream sbs(buffer, O_BUFLEN, &log);
897 first_error->report(&sbs);
898 first_error->_current_step = 0; // reset current_step
899 first_error->_current_step_info = ""; // reset current_step string
901 if (log.fd() != defaultStream::output_fd()) {
902 close(log.fd());
903 }
905 log.set_fd(-1);
906 log_done = true;
907 }
910 static bool skip_OnError = false;
911 if (!skip_OnError && OnError && OnError[0]) {
912 skip_OnError = true;
914 out.print_raw_cr("#");
915 out.print_raw ("# -XX:OnError=\"");
916 out.print_raw (OnError);
917 out.print_raw_cr("\"");
919 char* cmd;
920 const char* ptr = OnError;
921 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
922 out.print_raw ("# Executing ");
923 #if defined(LINUX)
924 out.print_raw ("/bin/sh -c ");
925 #elif defined(SOLARIS)
926 out.print_raw ("/usr/bin/sh -c ");
927 #endif
928 out.print_raw ("\"");
929 out.print_raw (cmd);
930 out.print_raw_cr("\" ...");
932 os::fork_and_exec(cmd);
933 }
935 // done with OnError
936 OnError = NULL;
937 }
939 static bool skip_bug_url = !should_report_bug(first_error->_id);
940 if (!skip_bug_url) {
941 skip_bug_url = true;
943 out.print_raw_cr("#");
944 print_bug_submit_message(&out, _thread);
945 }
947 if (!UseOSErrorReporting) {
948 // os::abort() will call abort hooks, try it first.
949 static bool skip_os_abort = false;
950 if (!skip_os_abort) {
951 skip_os_abort = true;
952 bool dump_core = should_report_bug(first_error->_id);
953 os::abort(dump_core);
954 }
956 // if os::abort() doesn't abort, try os::die();
957 os::die();
958 }
959 }
961 /*
962 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
963 * ensures utilities such as jmap can observe the process is a consistent state.
964 */
965 class VM_ReportJavaOutOfMemory : public VM_Operation {
966 private:
967 VMError *_err;
968 public:
969 VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
970 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; }
971 void doit();
972 };
974 void VM_ReportJavaOutOfMemory::doit() {
975 // Don't allocate large buffer on stack
976 static char buffer[O_BUFLEN];
978 tty->print_cr("#");
979 tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
980 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
982 // make heap parsability
983 Universe::heap()->ensure_parsability(false); // no need to retire TLABs
985 char* cmd;
986 const char* ptr = OnOutOfMemoryError;
987 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
988 tty->print("# Executing ");
989 #if defined(LINUX)
990 tty->print ("/bin/sh -c ");
991 #elif defined(SOLARIS)
992 tty->print ("/usr/bin/sh -c ");
993 #endif
994 tty->print_cr("\"%s\"...", cmd);
996 os::fork_and_exec(cmd);
997 }
998 }
1000 void VMError::report_java_out_of_memory() {
1001 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
1002 MutexLocker ml(Heap_lock);
1003 VM_ReportJavaOutOfMemory op(this);
1004 VMThread::execute(&op);
1005 }
1006 }