Sat, 11 Dec 2010 13:20:56 -0500
7003748: Decode C stack frames when symbols are presented (PhoneHome project)
Summary: Implemented in-process C native stack frame decoding when symbols are available.
Reviewed-by: coleenp, never
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, 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 st->print_cr("# A fatal error has been detected by the Java Runtime Environment:");
328 STEP(15, "(printing type of error)")
330 switch(_id) {
331 case oom_error:
332 st->print_cr("#");
333 st->print("# java.lang.OutOfMemoryError: ");
334 if (_size) {
335 st->print("requested ");
336 sprintf(buf,SIZE_FORMAT,_size);
337 st->print(buf);
338 st->print(" bytes");
339 if (_message != NULL) {
340 st->print(" for ");
341 st->print(_message);
342 }
343 st->print_cr(". Out of swap space?");
344 } else {
345 if (_message != NULL)
346 st->print_cr(_message);
347 }
348 break;
349 case internal_error:
350 default:
351 break;
352 }
354 STEP(20, "(printing exception/signal name)")
356 st->print_cr("#");
357 st->print("# ");
358 // Is it an OS exception/signal?
359 if (os::exception_name(_id, buf, sizeof(buf))) {
360 st->print("%s", buf);
361 st->print(" (0x%x)", _id); // signal number
362 st->print(" at pc=" PTR_FORMAT, _pc);
363 } else {
364 st->print("Internal Error");
365 if (_filename != NULL && _lineno > 0) {
366 #ifdef PRODUCT
367 // In product mode chop off pathname?
368 char separator = os::file_separator()[0];
369 const char *p = strrchr(_filename, separator);
370 const char *file = p ? p+1 : _filename;
371 #else
372 const char *file = _filename;
373 #endif
374 size_t len = strlen(file);
375 size_t buflen = sizeof(buf);
377 strncpy(buf, file, buflen);
378 if (len + 10 < buflen) {
379 sprintf(buf + len, ":%d", _lineno);
380 }
381 st->print(" (%s)", buf);
382 } else {
383 st->print(" (0x%x)", _id);
384 }
385 }
387 STEP(30, "(printing current thread and pid)")
389 // process id, thread id
390 st->print(", pid=%d", os::current_process_id());
391 st->print(", tid=" UINTX_FORMAT, os::current_thread_id());
392 st->cr();
394 STEP(40, "(printing error message)")
396 // error message
397 if (_detail_msg) {
398 st->print_cr("# %s: %s", _message ? _message : "Error", _detail_msg);
399 } else if (_message) {
400 st->print_cr("# Error: %s", _message);
401 }
403 STEP(50, "(printing Java version string)")
405 // VM version
406 st->print_cr("#");
407 JDK_Version::current().to_string(buf, sizeof(buf));
408 st->print_cr("# JRE version: %s", buf);
409 st->print_cr("# Java VM: %s (%s %s %s %s)",
410 Abstract_VM_Version::vm_name(),
411 Abstract_VM_Version::vm_release(),
412 Abstract_VM_Version::vm_info_string(),
413 Abstract_VM_Version::vm_platform_string(),
414 UseCompressedOops ? "compressed oops" : ""
415 );
417 STEP(60, "(printing problematic frame)")
419 // Print current frame if we have a context (i.e. it's a crash)
420 if (_context) {
421 st->print_cr("# Problematic frame:");
422 st->print("# ");
423 frame fr = os::fetch_frame_from_context(_context);
424 fr.print_on_error(st, buf, sizeof(buf));
425 st->cr();
426 st->print_cr("#");
427 }
429 STEP(65, "(printing bug submit message)")
431 if (_verbose) print_bug_submit_message(st, _thread);
433 STEP(70, "(printing thread)" )
435 if (_verbose) {
436 st->cr();
437 st->print_cr("--------------- T H R E A D ---------------");
438 st->cr();
439 }
441 STEP(80, "(printing current thread)" )
443 // current thread
444 if (_verbose) {
445 if (_thread) {
446 st->print("Current thread (" PTR_FORMAT "): ", _thread);
447 _thread->print_on_error(st, buf, sizeof(buf));
448 st->cr();
449 } else {
450 st->print_cr("Current thread is native thread");
451 }
452 st->cr();
453 }
455 STEP(90, "(printing siginfo)" )
457 // signal no, signal code, address that caused the fault
458 if (_verbose && _siginfo) {
459 os::print_siginfo(st, _siginfo);
460 st->cr();
461 }
463 STEP(100, "(printing registers, top of stack, instructions near pc)")
465 // registers, top of stack, instructions near pc
466 if (_verbose && _context) {
467 os::print_context(st, _context);
468 st->cr();
469 }
471 STEP(105, "(printing register info)")
473 // decode register contents if possible
474 if (_verbose && _context && Universe::is_fully_initialized()) {
475 os::print_register_info(st, _context);
476 st->cr();
477 }
479 STEP(110, "(printing stack bounds)" )
481 if (_verbose) {
482 st->print("Stack: ");
484 address stack_top;
485 size_t stack_size;
487 if (_thread) {
488 stack_top = _thread->stack_base();
489 stack_size = _thread->stack_size();
490 } else {
491 stack_top = os::current_stack_base();
492 stack_size = os::current_stack_size();
493 }
495 address stack_bottom = stack_top - stack_size;
496 st->print("[" PTR_FORMAT "," PTR_FORMAT "]", stack_bottom, stack_top);
498 frame fr = _context ? os::fetch_frame_from_context(_context)
499 : os::current_frame();
501 if (fr.sp()) {
502 st->print(", sp=" PTR_FORMAT, fr.sp());
503 size_t free_stack_size = pointer_delta(fr.sp(), stack_bottom, 1024);
504 st->print(", free space=" SIZE_FORMAT "k", free_stack_size);
505 }
507 st->cr();
508 }
510 STEP(120, "(printing native stack)" )
512 if (_verbose) {
513 frame fr = _context ? os::fetch_frame_from_context(_context)
514 : os::current_frame();
516 // see if it's a valid frame
517 if (fr.pc()) {
518 st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
520 // initialize decoder to decode C frames
521 Decoder decoder;
523 int count = 0;
524 while (count++ < StackPrintLimit) {
525 fr.print_on_error(st, buf, sizeof(buf));
526 st->cr();
527 if (os::is_first_C_frame(&fr)) break;
528 fr = os::get_sender_for_C_frame(&fr);
529 }
531 if (count > StackPrintLimit) {
532 st->print_cr("...<more frames>...");
533 }
535 st->cr();
536 }
537 }
539 STEP(130, "(printing Java stack)" )
541 if (_verbose && _thread && _thread->is_Java_thread()) {
542 print_stack_trace(st, (JavaThread*)_thread, buf, sizeof(buf));
543 }
545 STEP(135, "(printing target Java thread stack)" )
547 // printing Java thread stack trace if it is involved in GC crash
548 if (_verbose && _thread && (_thread->is_Named_thread())) {
549 JavaThread* jt = ((NamedThread *)_thread)->processed_thread();
550 if (jt != NULL) {
551 st->print_cr("JavaThread " PTR_FORMAT " (nid = " UINTX_FORMAT ") was being processed", jt, jt->osthread()->thread_id());
552 print_stack_trace(st, jt, buf, sizeof(buf), true);
553 }
554 }
556 STEP(140, "(printing VM operation)" )
558 if (_verbose && _thread && _thread->is_VM_thread()) {
559 VMThread* t = (VMThread*)_thread;
560 VM_Operation* op = t->vm_operation();
561 if (op) {
562 op->print_on_error(st);
563 st->cr();
564 st->cr();
565 }
566 }
568 STEP(150, "(printing current compile task)" )
570 if (_verbose && _thread && _thread->is_Compiler_thread()) {
571 CompilerThread* t = (CompilerThread*)_thread;
572 if (t->task()) {
573 st->cr();
574 st->print_cr("Current CompileTask:");
575 t->task()->print_line_on_error(st, buf, sizeof(buf));
576 st->cr();
577 }
578 }
580 STEP(160, "(printing process)" )
582 if (_verbose) {
583 st->cr();
584 st->print_cr("--------------- P R O C E S S ---------------");
585 st->cr();
586 }
588 STEP(170, "(printing all threads)" )
590 // all threads
591 if (_verbose && _thread) {
592 Threads::print_on_error(st, _thread, buf, sizeof(buf));
593 st->cr();
594 }
596 STEP(175, "(printing VM state)" )
598 if (_verbose) {
599 // Safepoint state
600 st->print("VM state:");
602 if (SafepointSynchronize::is_synchronizing()) st->print("synchronizing");
603 else if (SafepointSynchronize::is_at_safepoint()) st->print("at safepoint");
604 else st->print("not at safepoint");
606 // Also see if error occurred during initialization or shutdown
607 if (!Universe::is_fully_initialized()) {
608 st->print(" (not fully initialized)");
609 } else if (VM_Exit::vm_exited()) {
610 st->print(" (shutting down)");
611 } else {
612 st->print(" (normal execution)");
613 }
614 st->cr();
615 st->cr();
616 }
618 STEP(180, "(printing owned locks on error)" )
620 // mutexes/monitors that currently have an owner
621 if (_verbose) {
622 print_owned_locks_on_error(st);
623 st->cr();
624 }
626 STEP(190, "(printing heap information)" )
628 if (_verbose && Universe::is_fully_initialized()) {
629 // print heap information before vm abort
630 Universe::print_on(st);
631 st->cr();
632 }
634 STEP(195, "(printing code cache information)" )
636 if (_verbose && Universe::is_fully_initialized()) {
637 // print code cache information before vm abort
638 CodeCache::print_bounds(st);
639 st->cr();
640 }
642 STEP(200, "(printing dynamic libraries)" )
644 if (_verbose) {
645 // dynamic libraries, or memory map
646 os::print_dll_info(st);
647 st->cr();
648 }
650 STEP(210, "(printing VM options)" )
652 if (_verbose) {
653 // VM options
654 Arguments::print_on(st);
655 st->cr();
656 }
658 STEP(220, "(printing environment variables)" )
660 if (_verbose) {
661 os::print_environment_variables(st, env_list, buf, sizeof(buf));
662 st->cr();
663 }
665 STEP(225, "(printing signal handlers)" )
667 if (_verbose) {
668 os::print_signal_handlers(st, buf, sizeof(buf));
669 st->cr();
670 }
672 STEP(230, "" )
674 if (_verbose) {
675 st->cr();
676 st->print_cr("--------------- S Y S T E M ---------------");
677 st->cr();
678 }
680 STEP(240, "(printing OS information)" )
682 if (_verbose) {
683 os::print_os_info(st);
684 st->cr();
685 }
687 STEP(250, "(printing CPU info)" )
688 if (_verbose) {
689 os::print_cpu_info(st);
690 st->cr();
691 }
693 STEP(260, "(printing memory info)" )
695 if (_verbose) {
696 os::print_memory_info(st);
697 st->cr();
698 }
700 STEP(270, "(printing internal vm info)" )
702 if (_verbose) {
703 st->print_cr("vm_info: %s", Abstract_VM_Version::internal_vm_info_string());
704 st->cr();
705 }
707 STEP(280, "(printing date and time)" )
709 if (_verbose) {
710 os::print_date_and_time(st);
711 st->cr();
712 }
714 END
716 # undef BEGIN
717 # undef STEP
718 # undef END
719 }
721 VMError* volatile VMError::first_error = NULL;
722 volatile jlong VMError::first_error_tid = -1;
724 void VMError::report_and_die() {
725 // Don't allocate large buffer on stack
726 static char buffer[O_BUFLEN];
728 // An error could happen before tty is initialized or after it has been
729 // destroyed. Here we use a very simple unbuffered fdStream for printing.
730 // Only out.print_raw() and out.print_raw_cr() should be used, as other
731 // printing methods need to allocate large buffer on stack. To format a
732 // string, use jio_snprintf() with a static buffer or use staticBufferStream.
733 static fdStream out(defaultStream::output_fd());
735 // How many errors occurred in error handler when reporting first_error.
736 static int recursive_error_count;
738 // We will first print a brief message to standard out (verbose = false),
739 // then save detailed information in log file (verbose = true).
740 static bool out_done = false; // done printing to standard out
741 static bool log_done = false; // done saving error log
742 static fdStream log; // error log
744 if (SuppressFatalErrorMessage) {
745 os::abort();
746 }
747 jlong mytid = os::current_thread_id();
748 if (first_error == NULL &&
749 Atomic::cmpxchg_ptr(this, &first_error, NULL) == NULL) {
751 // first time
752 first_error_tid = mytid;
753 set_error_reported();
755 if (ShowMessageBoxOnError) {
756 show_message_box(buffer, sizeof(buffer));
758 // User has asked JVM to abort. Reset ShowMessageBoxOnError so the
759 // WatcherThread can kill JVM if the error handler hangs.
760 ShowMessageBoxOnError = false;
761 }
763 // reset signal handlers or exception filter; make sure recursive crashes
764 // are handled properly.
765 reset_signal_handlers();
767 } else {
768 // If UseOsErrorReporting we call this for each level of the call stack
769 // while searching for the exception handler. Only the first level needs
770 // to be reported.
771 if (UseOSErrorReporting && log_done) return;
773 // This is not the first error, see if it happened in a different thread
774 // or in the same thread during error reporting.
775 if (first_error_tid != mytid) {
776 jio_snprintf(buffer, sizeof(buffer),
777 "[thread " INT64_FORMAT " also had an error]",
778 mytid);
779 out.print_raw_cr(buffer);
781 // error reporting is not MT-safe, block current thread
782 os::infinite_sleep();
784 } else {
785 if (recursive_error_count++ > 30) {
786 out.print_raw_cr("[Too many errors, abort]");
787 os::die();
788 }
790 jio_snprintf(buffer, sizeof(buffer),
791 "[error occurred during error reporting %s, id 0x%x]",
792 first_error ? first_error->_current_step_info : "",
793 _id);
794 if (log.is_open()) {
795 log.cr();
796 log.print_raw_cr(buffer);
797 log.cr();
798 } else {
799 out.cr();
800 out.print_raw_cr(buffer);
801 out.cr();
802 }
803 }
804 }
806 // print to screen
807 if (!out_done) {
808 first_error->_verbose = false;
810 staticBufferStream sbs(buffer, sizeof(buffer), &out);
811 first_error->report(&sbs);
813 out_done = true;
815 first_error->_current_step = 0; // reset current_step
816 first_error->_current_step_info = ""; // reset current_step string
817 }
819 // print to error log file
820 if (!log_done) {
821 first_error->_verbose = true;
823 // see if log file is already open
824 if (!log.is_open()) {
825 // open log file
826 int fd = -1;
828 if (ErrorFile != NULL) {
829 bool copy_ok =
830 Arguments::copy_expand_pid(ErrorFile, strlen(ErrorFile), buffer, sizeof(buffer));
831 if (copy_ok) {
832 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
833 }
834 }
836 if (fd == -1) {
837 const char *cwd = os::get_current_directory(buffer, sizeof(buffer));
838 size_t len = strlen(cwd);
839 // either user didn't specify, or the user's location failed,
840 // so use the default name in the current directory
841 jio_snprintf(&buffer[len], sizeof(buffer)-len, "%shs_err_pid%u.log",
842 os::file_separator(), os::current_process_id());
843 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
844 }
846 if (fd == -1) {
847 // try temp directory
848 const char * tmpdir = os::get_temp_directory();
849 jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log",
850 tmpdir, os::file_separator(), os::current_process_id());
851 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666);
852 }
854 if (fd != -1) {
855 out.print_raw("# An error report file with more information is saved as:\n# ");
856 out.print_raw_cr(buffer);
857 os::set_error_file(buffer);
859 log.set_fd(fd);
860 } else {
861 out.print_raw_cr("# Can not save log file, dump to screen..");
862 log.set_fd(defaultStream::output_fd());
863 }
864 }
866 staticBufferStream sbs(buffer, O_BUFLEN, &log);
867 first_error->report(&sbs);
868 first_error->_current_step = 0; // reset current_step
869 first_error->_current_step_info = ""; // reset current_step string
871 if (log.fd() != defaultStream::output_fd()) {
872 close(log.fd());
873 }
875 log.set_fd(-1);
876 log_done = true;
877 }
880 static bool skip_OnError = false;
881 if (!skip_OnError && OnError && OnError[0]) {
882 skip_OnError = true;
884 out.print_raw_cr("#");
885 out.print_raw ("# -XX:OnError=\"");
886 out.print_raw (OnError);
887 out.print_raw_cr("\"");
889 char* cmd;
890 const char* ptr = OnError;
891 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
892 out.print_raw ("# Executing ");
893 #if defined(LINUX)
894 out.print_raw ("/bin/sh -c ");
895 #elif defined(SOLARIS)
896 out.print_raw ("/usr/bin/sh -c ");
897 #endif
898 out.print_raw ("\"");
899 out.print_raw (cmd);
900 out.print_raw_cr("\" ...");
902 os::fork_and_exec(cmd);
903 }
905 // done with OnError
906 OnError = NULL;
907 }
909 static bool skip_bug_url = false;
910 if (!skip_bug_url) {
911 skip_bug_url = true;
913 out.print_raw_cr("#");
914 print_bug_submit_message(&out, _thread);
915 }
917 if (!UseOSErrorReporting) {
918 // os::abort() will call abort hooks, try it first.
919 static bool skip_os_abort = false;
920 if (!skip_os_abort) {
921 skip_os_abort = true;
922 os::abort();
923 }
925 // if os::abort() doesn't abort, try os::die();
926 os::die();
927 }
928 }
930 /*
931 * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this
932 * ensures utilities such as jmap can observe the process is a consistent state.
933 */
934 class VM_ReportJavaOutOfMemory : public VM_Operation {
935 private:
936 VMError *_err;
937 public:
938 VM_ReportJavaOutOfMemory(VMError *err) { _err = err; }
939 VMOp_Type type() const { return VMOp_ReportJavaOutOfMemory; }
940 void doit();
941 };
943 void VM_ReportJavaOutOfMemory::doit() {
944 // Don't allocate large buffer on stack
945 static char buffer[O_BUFLEN];
947 tty->print_cr("#");
948 tty->print_cr("# java.lang.OutOfMemoryError: %s", _err->message());
949 tty->print_cr("# -XX:OnOutOfMemoryError=\"%s\"", OnOutOfMemoryError);
951 // make heap parsability
952 Universe::heap()->ensure_parsability(false); // no need to retire TLABs
954 char* cmd;
955 const char* ptr = OnOutOfMemoryError;
956 while ((cmd = next_OnError_command(buffer, sizeof(buffer), &ptr)) != NULL){
957 tty->print("# Executing ");
958 #if defined(LINUX)
959 tty->print ("/bin/sh -c ");
960 #elif defined(SOLARIS)
961 tty->print ("/usr/bin/sh -c ");
962 #endif
963 tty->print_cr("\"%s\"...", cmd);
965 os::fork_and_exec(cmd);
966 }
967 }
969 void VMError::report_java_out_of_memory() {
970 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
971 MutexLocker ml(Heap_lock);
972 VM_ReportJavaOutOfMemory op(this);
973 VMThread::execute(&op);
974 }
975 }