Tue, 23 Nov 2010 13:22:55 -0800
6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, 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/defaultStream.hpp"
37 #include "utilities/top.hpp"
38 #include "utilities/vmError.hpp"
40 // List of environment variables that should be reported in error log file.
41 const char *env_list[] = {
42 // All platforms
43 "JAVA_HOME", "JRE_HOME", "JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "CLASSPATH",
44 "JAVA_COMPILER", "PATH", "USERNAME",
46 // Env variables that are defined on Solaris/Linux
47 "LD_LIBRARY_PATH", "LD_PRELOAD", "SHELL", "DISPLAY",
48 "HOSTTYPE", "OSTYPE", "ARCH", "MACHTYPE",
50 // defined on Linux
51 "LD_ASSUME_KERNEL", "_JAVA_SR_SIGNUM",
53 // defined on Windows
54 "OS", "PROCESSOR_IDENTIFIER", "_ALT_JAVA_HOME_DIR",
56 (const char *)0
57 };
59 // Fatal error handler for internal errors and crashes.
60 //
61 // The default behavior of fatal error handler is to print a brief message
62 // to standard out (defaultStream::output_fd()), then save detailed information
63 // into an error report file (hs_err_pid<pid>.log) and abort VM. If multiple
64 // threads are having troubles at the same time, only one error is reported.
65 // The thread that is reporting error will abort VM when it is done, all other
66 // threads are blocked forever inside report_and_die().
68 // Constructor for crashes
69 VMError::VMError(Thread* thread, int sig, address pc, void* siginfo, void* context) {
70 _thread = thread;
71 _id = sig;
72 _pc = pc;
73 _siginfo = siginfo;
74 _context = context;
76 _verbose = false;
77 _current_step = 0;
78 _current_step_info = NULL;
80 _message = NULL;
81 _detail_msg = NULL;
82 _filename = NULL;
83 _lineno = 0;
85 _size = 0;
86 }
88 // Constructor for internal errors
89 VMError::VMError(Thread* thread, const char* filename, int lineno,
90 const char* message, const char * detail_msg)
91 {
92 _thread = thread;
93 _id = internal_error; // Value that's not an OS exception/signal
94 _filename = filename;
95 _lineno = lineno;
96 _message = message;
97 _detail_msg = detail_msg;
99 _verbose = false;
100 _current_step = 0;
101 _current_step_info = NULL;
103 _pc = NULL;
104 _siginfo = NULL;
105 _context = NULL;
107 _size = 0;
108 }
110 // Constructor for OOM errors
111 VMError::VMError(Thread* thread, const char* filename, int lineno, size_t size,
112 const char* message) {
113 _thread = thread;
114 _id = oom_error; // Value that's not an OS exception/signal
115 _filename = filename;
116 _lineno = lineno;
117 _message = message;
118 _detail_msg = NULL;
120 _verbose = false;
121 _current_step = 0;
122 _current_step_info = NULL;
124 _pc = NULL;
125 _siginfo = NULL;
126 _context = NULL;
128 _size = size;
129 }
132 // Constructor for non-fatal errors
133 VMError::VMError(const char* message) {
134 _thread = NULL;
135 _id = internal_error; // Value that's not an OS exception/signal
136 _filename = NULL;
137 _lineno = 0;
138 _message = message;
139 _detail_msg = NULL;
141 _verbose = false;
142 _current_step = 0;
143 _current_step_info = NULL;
145 _pc = NULL;
146 _siginfo = NULL;
147 _context = NULL;
149 _size = 0;
150 }
152 // -XX:OnError=<string>, where <string> can be a list of commands, separated
153 // by ';'. "%p" is replaced by current process id (pid); "%%" is replaced by
154 // a single "%". Some examples:
155 //
156 // -XX:OnError="pmap %p" // show memory map
157 // -XX:OnError="gcore %p; dbx - %p" // dump core and launch debugger
158 // -XX:OnError="cat hs_err_pid%p.log | mail my_email@sun.com"
159 // -XX:OnError="kill -9 %p" // ?#!@#
161 // A simple parser for -XX:OnError, usage:
162 // ptr = OnError;
163 // while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr) != NULL)
164 // ... ...
165 static char* next_OnError_command(char* buf, int buflen, const char** ptr) {
166 if (ptr == NULL || *ptr == NULL) return NULL;
168 const char* cmd = *ptr;
170 // skip leading blanks or ';'
171 while (*cmd == ' ' || *cmd == ';') cmd++;
173 if (*cmd == '\0') return NULL;
175 const char * cmdend = cmd;
176 while (*cmdend != '\0' && *cmdend != ';') cmdend++;
178 Arguments::copy_expand_pid(cmd, cmdend - cmd, buf, buflen);
180 *ptr = (*cmdend == '\0' ? cmdend : cmdend + 1);
181 return buf;
182 }
185 static void print_bug_submit_message(outputStream *out, Thread *thread) {
186 if (out == NULL) return;
187 out->print_raw_cr("# If you would like to submit a bug report, please visit:");
188 out->print_raw ("# ");
189 out->print_raw_cr(Arguments::java_vendor_url_bug());
190 // If the crash is in native code, encourage user to submit a bug to the
191 // provider of that code.
192 if (thread && thread->is_Java_thread() &&
193 !thread->is_hidden_from_external_view()) {
194 JavaThread* jt = (JavaThread*)thread;
195 if (jt->thread_state() == _thread_in_native) {
196 out->print_cr("# The crash happened outside the Java Virtual Machine in native code.\n# See problematic frame for where to report the bug.");
197 }
198 }
199 out->print_raw_cr("#");
200 }
203 // Return a string to describe the error
204 char* VMError::error_string(char* buf, int buflen) {
205 char signame_buf[64];
206 const char *signame = os::exception_name(_id, signame_buf, sizeof(signame_buf));
208 if (signame) {
209 jio_snprintf(buf, buflen,
210 "%s (0x%x) at pc=" PTR_FORMAT ", pid=%d, tid=" UINTX_FORMAT,
211 signame, _id, _pc,
212 os::current_process_id(), os::current_thread_id());
213 } else if (_filename != NULL && _lineno > 0) {
214 // skip directory names
215 char separator = os::file_separator()[0];
216 const char *p = strrchr(_filename, separator);
217 int n = jio_snprintf(buf, buflen,
218 "Internal Error at %s:%d, pid=%d, tid=" UINTX_FORMAT,
219 p ? p + 1 : _filename, _lineno,
220 os::current_process_id(), os::current_thread_id());
221 if (n >= 0 && n < buflen && _message) {
222 if (_detail_msg) {
223 jio_snprintf(buf + n, buflen - n, "%s%s: %s",
224 os::line_separator(), _message, _detail_msg);
225 } else {
226 jio_snprintf(buf + n, buflen - n, "%sError: %s",
227 os::line_separator(), _message);
228 }
229 }
230 } else {
231 jio_snprintf(buf, buflen,
232 "Internal Error (0x%x), pid=%d, tid=" UINTX_FORMAT,
233 _id, os::current_process_id(), os::current_thread_id());
234 }
236 return buf;
237 }
239 void VMError::print_stack_trace(outputStream* st, JavaThread* jt,
240 char* buf, int buflen, bool verbose) {
241 #ifdef ZERO
242 if (jt->zero_stack()->sp() && jt->top_zero_frame()) {
243 // StackFrameStream uses the frame anchor, which may not have
244 // been set up. This can be done at any time in Zero, however,
245 // so if it hasn't been set up then we just set it up now and
246 // clear it again when we're done.
247 bool has_last_Java_frame = jt->has_last_Java_frame();
248 if (!has_last_Java_frame)
249 jt->set_last_Java_frame();
250 st->print("Java frames:");
252 // If the top frame is a Shark frame and the frame anchor isn't
253 // set up then it's possible that the information in the frame
254 // is garbage: it could be from a previous decache, or it could
255 // simply have never been written. So we print a warning...
256 StackFrameStream sfs(jt);
257 if (!has_last_Java_frame && !sfs.is_done()) {
258 if (sfs.current()->zeroframe()->is_shark_frame()) {
259 st->print(" (TOP FRAME MAY BE JUNK)");
260 }
261 }
262 st->cr();
264 // Print the frames
265 for(int i = 0; !sfs.is_done(); sfs.next(), i++) {
266 sfs.current()->zero_print_on_error(i, st, buf, buflen);
267 st->cr();
268 }
270 // Reset the frame anchor if necessary
271 if (!has_last_Java_frame)
272 jt->reset_last_Java_frame();
273 }
274 #else
275 if (jt->has_last_Java_frame()) {
276 st->print_cr("Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)");
277 for(StackFrameStream sfs(jt); !sfs.is_done(); sfs.next()) {
278 sfs.current()->print_on_error(st, buf, buflen, verbose);
279 st->cr();
280 }
281 }
282 #endif // ZERO
283 }
285 // This is the main function to report a fatal error. Only one thread can
286 // call this function, so we don't need to worry about MT-safety. But it's
287 // possible that the error handler itself may crash or die on an internal
288 // error, for example, when the stack/heap is badly damaged. We must be
289 // able to handle recursive errors that happen inside error handler.
290 //
291 // Error reporting is done in several steps. If a crash or internal error
292 // occurred when reporting an error, the nested signal/exception handler
293 // can skip steps that are already (or partially) done. Error reporting will
294 // continue from the next step. This allows us to retrieve and print
295 // information that may be unsafe to get after a fatal error. If it happens,
296 // you may find nested report_and_die() frames when you look at the stack
297 // in a debugger.
298 //
299 // In general, a hang in error handler is much worse than a crash or internal
300 // error, as it's harder to recover from a hang. Deadlock can happen if we
301 // try to grab a lock that is already owned by current thread, or if the
302 // owner is blocked forever (e.g. in os::infinite_sleep()). If possible, the
303 // error handler and all the functions it called should avoid grabbing any
304 // lock. An important thing to notice is that memory allocation needs a lock.
305 //
306 // We should avoid using large stack allocated buffers. Many errors happen
307 // when stack space is already low. Making things even worse is that there
308 // could be nested report_and_die() calls on stack (see above). Only one
309 // thread can report error, so large buffers are statically allocated in data
310 // segment.
312 void VMError::report(outputStream* st) {
313 # define BEGIN if (_current_step == 0) { _current_step = 1;
314 # define STEP(n, s) } if (_current_step < n) { _current_step = n; _current_step_info = s;
315 # define END }
317 // don't allocate large buffer on stack
318 static char buf[O_BUFLEN];
320 BEGIN
322 STEP(10, "(printing fatal error message)")
324 st->print_cr("#");
325 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
327 STEP(15, "(printing type of error)")
329 switch(_id) {
330 case oom_error:
331 st->print_cr("#");
332 st->print("# java.lang.OutOfMemoryError: ");
333 if (_size) {
334 st->print("requested ");
335 sprintf(buf,SIZE_FORMAT,_size);
336 st->print(buf);
337 st->print(" bytes");
338 if (_message != NULL) {
339 st->print(" for ");
340 st->print(_message);
341 }
342 st->print_cr(". Out of swap space?");
343 } else {
344 if (_message != NULL)
345 st->print_cr(_message);
346 }
347 break;
348 case internal_error:
349 default:
350 break;
351 }
353 STEP(20, "(printing exception/signal name)")
355 st->print_cr("#");
356 st->print("# ");
357 // Is it an OS exception/signal?
358 if (os::exception_name(_id, buf, sizeof(buf))) {
359 st->print("%s", buf);
360 st->print(" (0x%x)", _id); // signal number
361 st->print(" at pc=" PTR_FORMAT, _pc);
362 } else {
363 st->print("Internal Error");
364 if (_filename != NULL && _lineno > 0) {
365 #ifdef PRODUCT
366 // In product mode chop off pathname?
367 char separator = os::file_separator()[0];
368 const char *p = strrchr(_filename, separator);
369 const char *file = p ? p+1 : _filename;
370 #else
371 const char *file = _filename;
372 #endif
373 size_t len = strlen(file);
374 size_t buflen = sizeof(buf);
376 strncpy(buf, file, buflen);
377 if (len + 10 < buflen) {
378 sprintf(buf + len, ":%d", _lineno);
379 }
380 st->print(" (%s)", buf);
381 } else {
382 st->print(" (0x%x)", _id);
383 }
384 }
386 STEP(30, "(printing current thread and pid)")
388 // process id, thread id
389 st->print(", pid=%d", os::current_process_id());
390 st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
391 st->cr();
393 STEP(40, "(printing error message)")
395 // error message
396 if (_detail_msg) {
397 st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg);
398 } else if (_message) {
399 st->print_cr("# Error: %s", _message);
400 }
402 STEP(50, "(printing Java version string)")
404 // VM version
405 st->print_cr("#");
406 JDK_Version::current().to_string(buf, sizeof(buf));
407 st->print_cr("# JRE version: %s", buf);
408 st->print_cr("# Java VM: %s (%s %s %s %s)",
409 Abstract_VM_Version::vm_name(),
410 Abstract_VM_Version::vm_release(),
411 Abstract_VM_Version::vm_info_string(),
412 Abstract_VM_Version::vm_platform_string(),
413 UseCompressedOops ? "compressed oops" : ""
414 );
416 STEP(60, "(printing problematic frame)")
418 // Print current frame if we have a context (i.e. it's a crash)
419 if (_context) {
420 st->print_cr("# Problematic frame:");
421 st->print("# ");
422 frame fr = os::fetch_frame_from_context(_context);
423 fr.print_on_error(st, buf, sizeof(buf));
424 st->cr();
425 st->print_cr("#");
426 }
428 STEP(65, "(printing bug submit message)")
430 if (_verbose) print_bug_submit_message(st, _thread);
432 STEP(70, "(printing thread)" )
434 if (_verbose) {
435 st->cr();
436 st->print_cr("--------------- T H R E A D ---------------");
437 st->cr();
438 }
440 STEP(80, "(printing current thread)" )
442 // current thread
443 if (_verbose) {
444 if (_thread) {
445 st->print("Current thread (" PTR_FORMAT "): ", _thread);
446 _thread->print_on_error(st, buf, sizeof(buf));
447 st->cr();
448 } else {
449 st->print_cr("Current thread is native thread");
450 }
451 st->cr();
452 }
454 STEP(90, "(printing siginfo)" )
456 // signal no, signal code, address that caused the fault
457 if (_verbose && _siginfo) {
458 os::print_siginfo(st, _siginfo);
459 st->cr();
460 }
462 STEP(100, "(printing registers, top of stack, instructions near pc)")
464 // registers, top of stack, instructions near pc
465 if (_verbose && _context) {
466 os::print_context(st, _context);
467 st->cr();
468 }
470 STEP(105, "(printing register info)")
472 // decode register contents if possible
473 if (_verbose && _context && Universe::is_fully_initialized()) {
474 os::print_register_info(st, _context);
475 st->cr();
476 }
478 STEP(110, "(printing stack bounds)" )
480 if (_verbose) {
481 st->print("Stack: ");
483 address stack_top;
484 size_t stack_size;
486 if (_thread) {
487 stack_top = _thread->stack_base();
488 stack_size = _thread->stack_size();
489 } else {
490 stack_top = os::current_stack_base();
491 stack_size = os::current_stack_size();
492 }
494 address stack_bottom = stack_top - stack_size;
495 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
497 frame fr = _context ? os::fetch_frame_from_context(_context)
498 : os::current_frame();
500 if (fr.sp()) {
501 st->print(", sp=" PTR_FORMAT, fr.sp());
502 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
503 st->print(", free space=" SIZE_FORMAT "k", free_stack_size);
504 }
506 st->cr();
507 }
509 STEP(120, "(printing native stack)" )
511 if (_verbose) {
512 frame fr = _context ? os::fetch_frame_from_context(_context)
513 : os::current_frame();
515 // see if it's a valid frame
516 if (fr.pc()) {
517 st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
519 int count = 0;
521 while (count++ < StackPrintLimit) {
522 fr.print_on_error(st, buf, sizeof(buf));
523 st->cr();
524 if (os::is_first_C_frame(&fr)) break;
525 fr = os::get_sender_for_C_frame(&fr);
526 }
528 if (count > StackPrintLimit) {
529 st->print_cr("...<more frames>...");
530 }
532 st->cr();
533 }
534 }
536 STEP(130, "(printing Java stack)" )
538 if (_verbose && _thread && _thread->is_Java_thread()) {
539 print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
540 }
542 STEP(135, "(printing target Java thread stack)" )
544 // printing Java thread stack trace if it is involved in GC crash
545 if (_verbose && _thread && (_thread->is_Named_thread())) {
546 JavaThread* jt = ((NamedThread *)_thread)->processed_thread();
547 if (jt != NULL) {
548 st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id());
549 print_stack_trace(st, jt, buf, sizeof(buf), true);
550 }
551 }
553 STEP(140, "(printing VM operation)" )
555 if (_verbose && _thread && _thread->is_VM_thread()) {
556 VMThread* t = (VMThread*)_thread;
557 VM_Operation* op = t->vm_operation();
558 if (op) {
559 op->print_on_error(st);
560 st->cr();
561 st->cr();
562 }
563 }
565 STEP(150, "(printing current compile task)" )
567 if (_verbose && _thread && _thread->is_Compiler_thread()) {
568 CompilerThread* t = (CompilerThread*)_thread;
569 if (t->task()) {
570 st->cr();
571 st->print_cr("Current CompileTask:");
572 t->task()->print_line_on_error(st, buf, sizeof(buf));
573 st->cr();
574 }
575 }
577 STEP(160, "(printing process)" )
579 if (_verbose) {
580 st->cr();
581 st->print_cr("--------------- P R O C E S S ---------------");
582 st->cr();
583 }
585 STEP(170, "(printing all threads)" )
587 // all threads
588 if (_verbose && _thread) {
589 Threads::print_on_error(st, _thread, buf, sizeof(buf));
590 st->cr();
591 }
593 STEP(175, "(printing VM state)" )
595 if (_verbose) {
596 // Safepoint state
597 st->print("VM state:");
599 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
600 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
601 else st->print("not at safepoint");
603 // Also see if error occurred during initialization or shutdown
604 if (!Universe::is_fully_initialized()) {
605 st->print(" (not fully initialized)");
606 } else if (VM_Exit::vm_exited()) {
607 st->print(" (shutting down)");
608 } else {
609 st->print(" (normal execution)");
610 }
611 st->cr();
612 st->cr();
613 }
615 STEP(180, "(printing owned locks on error)" )
617 // mutexes/monitors that currently have an owner
618 if (_verbose) {
619 print_owned_locks_on_error(st);
620 st->cr();
621 }
623 STEP(190, "(printing heap information)" )
625 if (_verbose && Universe::is_fully_initialized()) {
626 // print heap information before vm abort
627 Universe::print_on(st);
628 st->cr();
629 }
631 STEP(195, "(printing code cache information)" )
633 if (_verbose && Universe::is_fully_initialized()) {
634 // print code cache information before vm abort
635 CodeCache::print_bounds(st);
636 st->cr();
637 }
639 STEP(200, "(printing dynamic libraries)" )
641 if (_verbose) {
642 // dynamic libraries, or memory map
643 os::print_dll_info(st);
644 st->cr();
645 }
647 STEP(210, "(printing VM options)" )
649 if (_verbose) {
650 // VM options
651 Arguments::print_on(st);
652 st->cr();
653 }
655 STEP(220, "(printing environment variables)" )
657 if (_verbose) {
658 os::print_environment_variables(st, env_list, buf, sizeof(buf));
659 st->cr();
660 }
662 STEP(225, "(printing signal handlers)" )
664 if (_verbose) {
665 os::print_signal_handlers(st, buf, sizeof(buf));
666 st->cr();
667 }
669 STEP(230, "" )
671 if (_verbose) {
672 st->cr();
673 st->print_cr("--------------- S Y S T E M ---------------");
674 st->cr();
675 }
677 STEP(240, "(printing OS information)" )
679 if (_verbose) {
680 os::print_os_info(st);
681 st->cr();
682 }
684 STEP(250, "(printing CPU info)" )
685 if (_verbose) {
686 os::print_cpu_info(st);
687 st->cr();
688 }
690 STEP(260, "(printing memory info)" )
692 if (_verbose) {
693 os::print_memory_info(st);
694 st->cr();
695 }
697 STEP(270, "(printing internal vm info)" )
699 if (_verbose) {
700 st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
701 st->cr();
702 }
704 STEP(280, "(printing date and time)" )
706 if (_verbose) {
707 os::print_date_and_time(st);
708 st->cr();
709 }
711 END
713 # undef BEGIN
714 # undef STEP
715 # undef END
716 }
718 VMError* volatile VMError::first_error = NULL;
719 volatile jlong VMError::first_error_tid = -1;
721 void VMError::report_and_die() {
722 // Don't allocate large buffer on stack
723 static char buffer[O_BUFLEN];
725 // An error could happen before tty is initialized or after it has been
726 // destroyed. Here we use a very simple unbuffered fdStream for printing.
727 // Only out.print_raw() and out.print_raw_cr() should be used, as other
728 // printing methods need to allocate large buffer on stack. To format a
729 // string, use jio_snprintf() with a static buffer or use staticBufferStream.
730 static fdStream out(defaultStream::output_fd());
732 // How many errors occurred in error handler when reporting first_error.
733 static int recursive_error_count;
735 // We will first print a brief message to standard out (verbose = false),
736 // then save detailed information in log file (verbose = true).
737 static bool out_done = false; // done printing to standard out
738 static bool log_done = false; // done saving error log
739 static fdStream log; // error log
741 if (SuppressFatalErrorMessage) {
742 os::abort();
743 }
744 jlong mytid = os::current_thread_id();
745 if (first_error == NULL &&
746 Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
748 // first time
749 first_error_tid = mytid;
750 set_error_reported();
752 if (ShowMessageBoxOnError) {
753 show_message_box(buffer, sizeof(buffer));
755 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
756 // WatcherThread can kill JVM if the error handler hangs.
757 ShowMessageBoxOnError = false;
758 }
760 // reset signal handlers or exception filter; make sure recursive crashes
761 // are handled properly.
762 reset_signal_handlers();
764 } else {
765 // If UseOsErrorReporting we call this for each level of the call stack
766 // while searching for the exception handler. Only the first level needs
767 // to be reported.
768 if (UseOSErrorReporting && log_done) return;
770 // This is not the first error, see if it happened in a different thread
771 // or in the same thread during error reporting.
772 if (first_error_tid != mytid) {
773 jio_snprintf(buffer, sizeof(buffer),
774 "[thread " INT64_FORMAT " also had an error]",
775 mytid);
776 out.print_raw_cr(buffer);
778 // error reporting is not MT-safe, block current thread
779 os::infinite_sleep();
781 } else {
782 if (recursive_error_count++ > 30) {
783 out.print_raw_cr("[Too many errors, abort]");
784 os::die();
785 }
787 jio_snprintf(buffer, sizeof(buffer),
788 "[error occurred during error reporting %s, id 0x%x]",
789 first_error ? first_error->_current_step_info : "",
790 _id);
791 if (log.is_open()) {
792 log.cr();
793 log.print_raw_cr(buffer);
794 log.cr();
795 } else {
796 out.cr();
797 out.print_raw_cr(buffer);
798 out.cr();
799 }
800 }
801 }
803 // print to screen
804 if (!out_done) {
805 first_error->_verbose = false;
807 staticBufferStream sbs(buffer, sizeof(buffer), &out);
808 first_error->report(&sbs);
810 out_done = true;
812 first_error->_current_step = 0; // reset current_step
813 first_error->_current_step_info = ""; // reset current_step string
814 }
816 // print to error log file
817 if (!log_done) {
818 first_error->_verbose = true;
820 // see if log file is already open
821 if (!log.is_open()) {
822 // open log file
823 int fd = -1;
825 if (ErrorFile != NULL) {
826 bool copy_ok =
827 Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
828 if (copy_ok) {
829 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
830 }
831 }
833 if (fd == -1) {
834 const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
835 size_t len = strlen(cwd);
836 // either user didn't specify, or the user's location failed,
837 // so use the default name in the current directory
838 jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
839 os::file_separator(), os::current_process_id());
840 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
841 }
843 if (fd == -1) {
844 // try temp directory
845 const char * tmpdir = os::get_temp_directory();
846 jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
847 tmpdir, os::file_separator(), os::current_process_id());
848 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
849 }
851 if (fd != -1) {
852 out.print_raw("# An error report file with more information is saved as:\n# ");
853 out.print_raw_cr(buffer);
854 os::set_error_file(buffer);
856 log.set_fd(fd);
857 } else {
858 out.print_raw_cr("# Can not save log file, dump to screen..");
859 log.set_fd(defaultStream::output_fd());
860 }
861 }
863 staticBufferStream sbs(buffer, O_BUFLEN, &log);
864 first_error->report(&sbs);
865 first_error->_current_step = 0; // reset current_step
866 first_error->_current_step_info = ""; // reset current_step string
868 if (log.fd() != defaultStream::output_fd()) {
869 close(log.fd());
870 }
872 log.set_fd(-1);
873 log_done = true;
874 }
877 static bool skip_OnError = false;
878 if (!skip_OnError && OnError && OnError[0]) {
879 skip_OnError = true;
881 out.print_raw_cr("#");
882 out.print_raw ("# -XX:OnError=\"");
883 out.print_raw (OnError);
884 out.print_raw_cr("\"");
886 char* cmd;
887 const char* ptr = OnError;
888 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
889 out.print_raw ("# Executing ");
890 #if defined(LINUX)
891 out.print_raw ("/bin/sh -c ");
892 #elif defined(SOLARIS)
893 out.print_raw ("/usr/bin/sh -c ");
894 #endif
895 out.print_raw ("\"");
896 out.print_raw (cmd);
897 out.print_raw_cr("\" ...");
899 os::fork_and_exec(cmd);
900 }
902 // done with OnError
903 OnError = NULL;
904 }
906 static bool skip_bug_url = false;
907 if (!skip_bug_url) {
908 skip_bug_url = true;
910 out.print_raw_cr("#");
911 print_bug_submit_message(&out, _thread);
912 }
914 if (!UseOSErrorReporting) {
915 // os::abort() will call abort hooks, try it first.
916 static bool skip_os_abort = false;
917 if (!skip_os_abort) {
918 skip_os_abort = true;
919 os::abort();
920 }
922 // if os::abort() doesn't abort, try os::die();
923 os::die();
924 }
925 }
927 /*
928 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
929 * ensures utilities such as jmap can observe the process is a consistent state.
930 */
931 class VM_ReportJavaOutOfMemory : public VM_Operation {
932 private:
933 VMError *_err;
934 public:
935 VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
936 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; }
937 void doit();
938 };
940 void VM_ReportJavaOutOfMemory::doit() {
941 // Don't allocate large buffer on stack
942 static char buffer[O_BUFLEN];
944 tty->print_cr("#");
945 tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
946 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
948 // make heap parsability
949 Universe::heap()->ensure_parsability(false); // no need to retire TLABs
951 char* cmd;
952 const char* ptr = OnOutOfMemoryError;
953 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
954 tty->print("# Executing ");
955 #if defined(LINUX)
956 tty->print ("/bin/sh -c ");
957 #elif defined(SOLARIS)
958 tty->print ("/usr/bin/sh -c ");
959 #endif
960 tty->print_cr("\"%s\"...", cmd);
962 os::fork_and_exec(cmd);
963 }
964 }
966 void VMError::report_java_out_of_memory() {
967 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
968 MutexLocker ml(Heap_lock);
969 VM_ReportJavaOutOfMemory op(this);
970 VMThread::execute(&op);
971 }
972 }