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