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 #ifndef SHARE_VM_UTILITIES_VMERROR_HPP
  26 #define SHARE_VM_UTILITIES_VMERROR_HPP
  27 
  28 #include "utilities/globalDefinitions.hpp"
  29 
  30 class Decoder;
  31 class VM_ReportJavaOutOfMemory;
  32 
  33 class VMError : public AllStatic {
  34   friend class VM_ReportJavaOutOfMemory;
  35   friend class Decoder;
  36 
  37   static int         _id;               // Solaris/Linux signals: 0 - SIGRTMAX
  38                                         // Windows exceptions: 0xCxxxxxxx system errors
  39                                         //                     0x8xxxxxxx system warnings
  40 
  41   static const char* _message;
  42   static char        _detail_msg[1024];
  43 
  44   static Thread*     _thread;           // NULL if it's native thread
  45 
  46   // additional info for crashes
  47   static address     _pc;               // faulting PC
  48   static void*       _siginfo;          // ExceptionRecord on Windows,
  49                                         // siginfo_t on Solaris/Linux
  50   static void*       _context;          // ContextRecord on Windows,
  51                                         // ucontext_t on Solaris/Linux
  52 
  53   // additional info for VM internal errors
  54   static const char* _filename;
  55   static int         _lineno;
  56 
  57   // used by reporting about OOM
  58   static size_t      _size;
  59 
  60   // used by fatal error handler
  61   static int         _current_step;
  62   static const char* _current_step_info;
  63 
  64   // Thread id of the first error. We must be able to handle native thread,
  65   // so use thread id instead of Thread* to identify thread.
  66   static volatile jlong    first_error_tid;
  67 
  68   // Core dump status, false if we have been unable to write a core/minidump for some reason
  69   static bool coredump_status;
  70 
  71   // When coredump_status is set to true this will contain the name/path to the core/minidump,
  72   // if coredump_status if false, this will (hopefully) contain a useful error explaining why
  73   // no core/minidump has been written to disk
  74   static char coredump_message[O_BUFLEN];
  75 
  76 
  77   // set signal handlers on Solaris/Linux or the default exception filter
  78   // on Windows, to handle recursive crashes.
  79   static void reset_signal_handlers();
  80 
  81   // handle -XX:+ShowMessageBoxOnError. buf is used to format the message string
  82   static void show_message_box(char* buf, int buflen);
  83 
  84   // generate an error report
  85   static void report(outputStream* st, bool verbose);
  86 
  87   // generate a stack trace
  88   static void print_stack_trace(outputStream* st, JavaThread* jt,
  89                                 char* buf, int buflen, bool verbose = false);
  90 
  91   static const char* gc_mode();
  92   static void print_oom_reasons(outputStream* st);
  93 
  94   static bool should_report_bug(unsigned int id) {
  95     return (id != OOM_MALLOC_ERROR) && (id != OOM_MMAP_ERROR);
  96   }
  97 
  98   static void report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo,
  99                              void* context, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(6, 7);
 100   static void report_and_die(const char* message, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(2, 3);
 101 
 102   static fdStream out;
 103   static fdStream log; // error log used by VMError::report_and_die()
 104 
 105 public:
 106 
 107   // return a string to describe the error
 108   static char* error_string(char* buf, int buflen);
 109 
 110   // Record status of core/minidump
 111   static void record_coredump_status(const char* message, bool status);
 112 
 113   // main error reporting function
 114   static void report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
 115                              Thread* thread, address pc, void* siginfo, void* context,
 116                              const char* filename, int lineno, size_t size) ATTRIBUTE_PRINTF(3, 0);
 117 
 118   static void report_and_die(Thread* thread, unsigned int sig, address pc,
 119                              void* siginfo, void* context);
 120 
 121   static void report_and_die(Thread* thread,const char* filename, int lineno, const char* message,
 122                              const char* detail_fmt, va_list detail_args) ATTRIBUTE_PRINTF(5, 0);
 123 
 124   static void report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
 125                              VMErrorType vm_err_type, const char* detail_fmt,
 126                              va_list detail_args) ATTRIBUTE_PRINTF(6, 0);
 127 
 128   static void report_and_die(const char* message);
 129 
 130   // reporting OutOfMemoryError
 131   static void report_java_out_of_memory(const char* message);
 132 
 133   // returns original flags for signal, if it was resetted, or -1 if
 134   // signal was not changed by error reporter
 135   static int get_resetted_sigflags(int sig);
 136 
 137   // returns original handler for signal, if it was resetted, or NULL if
 138   // signal was not changed by error reporter
 139   static address get_resetted_sighandler(int sig);
 140 
 141   // check to see if fatal error reporting is in progress
 142   static bool fatal_error_in_progress() { return first_error_tid != -1; }
 143 
 144   static jlong get_first_error_tid() {
 145     return first_error_tid;
 146   }
 147 };
 148 
 149 #endif // SHARE_VM_UTILITIES_VMERROR_HPP