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