src/share/vm/utilities/vmError.cpp

Mon, 09 Aug 2010 17:51:56 -0700

author
never
date
Mon, 09 Aug 2010 17:51:56 -0700
changeset 2044
f4f596978298
parent 2036
126ea7725993
parent 2039
66c5dadb4d61
child 2262
1e9a9d2e6509
permissions
-rw-r--r--

Merge

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

mercurial