1 /*
2  * Copyright (c) 2003, 2019, 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_UTILITIES_VMERROR_HPP
26 #define SHARE_UTILITIES_VMERROR_HPP
27 
28 #include "utilities/globalDefinitions.hpp"
29 
30 class Decoder;
31 class frame;
32 class VM_ReportJavaOutOfMemory;
33 
34 class VMError : public AllStatic {
35   friend class VMStructs;
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 intptr_t _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   // Timeout handling:
77   // Timestamp at which error reporting started; -1 if no error reporting in progress.
78   static volatile jlong _reporting_start_time;
79   // Whether or not error reporting did timeout.
80   static volatile bool _reporting_did_timeout;
81   // Timestamp at which the last error reporting step started; -1 if no error reporting
82   //   in progress.
83   static volatile jlong _step_start_time;
84   // Whether or not the last error reporting step did timeout.
85   static volatile bool _step_did_timeout;
86 
87   static bool _error_reported;
88 
89  public:
90 
91   // set signal handlers on Solaris/Linux or the default exception filter
92   // on Windows, to handle recursive crashes.
93   static void reset_signal_handlers();
94 
95   // handle -XX:+ShowMessageBoxOnError. buf is used to format the message string
96   static void show_message_box(char* buf, int buflen);
97 
98   // generate an error report
99   static void report(outputStream* st, bool verbose);
100 
101   // generate a stack trace
102   static void print_stack_trace(outputStream* st, JavaThread* jt,
103                                 char* buf, int buflen, bool verbose = false);
104 
105   // public for use by the internal non-product debugger.
106   NOT_PRODUCT(public:)
107   static void print_native_stack(outputStream* st, frame fr, Thread* t,
108                                  char* buf, int buf_size);
109   NOT_PRODUCT(private:)
110 
should_report_bug(unsigned int id)111   static bool should_report_bug(unsigned int id) {
112     return (id != OOM_MALLOC_ERROR) && (id != OOM_MMAP_ERROR);
113   }
114 
115   // Write a hint to the stream in case siginfo relates to a segv/bus error
116   // and the offending address points into CDS store.
117   static void check_failing_cds_access(outputStream* st, const void* siginfo);
118 
119   // Timeout handling.
120   // Hook functions for platform dependend functionality:
121   static void reporting_started();
122   static void interrupt_reporting_thread();
123 
124   // Helper function to get the current timestamp.
125   static jlong get_current_timestamp();
126 
127   // Accessors to get/set the start times for step and total timeout.
128   static void record_reporting_start_time();
129   static jlong get_reporting_start_time();
130   static void record_step_start_time();
131   static jlong get_step_start_time();
132   static void clear_step_start_time();
133 
134 public:
135 
136   // return a string to describe the error
137   static char* error_string(char* buf, int buflen);
138 
139   // Record status of core/minidump
140   static void record_coredump_status(const char* message, bool status);
141 
142   // support for VM.info diagnostic command
143   static void print_vm_info(outputStream* st);
144 
145   // main error reporting function
146   static void report_and_die(Thread* thread, unsigned int sig, address pc, void* siginfo,
147                              void* context, const char* detail_fmt, ...) ATTRIBUTE_PRINTF(6, 7);
148 
149   static void report_and_die(int id, const char* message, const char* detail_fmt, va_list detail_args,
150                              Thread* thread, address pc, void* siginfo, void* context,
151                              const char* filename, int lineno, size_t size) ATTRIBUTE_PRINTF(3, 0);
152 
153   static void report_and_die(Thread* thread, unsigned int sig, address pc,
154                              void* siginfo, void* context);
155 
156   static void report_and_die(Thread* thread, void* context, const char* filename, int lineno, const char* message,
157                              const char* detail_fmt, va_list detail_args) ATTRIBUTE_PRINTF(6, 0);
158 
159   static void report_and_die(Thread* thread, const char* filename, int lineno, size_t size,
160                              VMErrorType vm_err_type, const char* detail_fmt,
161                              va_list detail_args) ATTRIBUTE_PRINTF(6, 0);
162 
163   // reporting OutOfMemoryError
164   static void report_java_out_of_memory(const char* message);
165 
166   // returns original flags for signal, if it was resetted, or -1 if
167   // signal was not changed by error reporter
168   static int get_resetted_sigflags(int sig);
169 
170   // returns original handler for signal, if it was resetted, or NULL if
171   // signal was not changed by error reporter
172   static address get_resetted_sighandler(int sig);
173 
174   // check to see if fatal error reporting is in progress
fatal_error_in_progress()175   static bool fatal_error_in_progress() { return _first_error_tid != -1; }
176 
get_first_error_tid()177   static intptr_t get_first_error_tid() { return _first_error_tid; }
178 
179   // Called by the WatcherThread to check if error reporting has timed-out.
180   //  Returns true if error reporting has not completed within the ErrorLogTimeout limit.
181   static bool check_timeout();
182 
183   // Support for avoiding multiple asserts
184   static bool is_error_reported();
185 
186   // Test vmassert(), fatal(), guarantee(), etc.
187   NOT_PRODUCT(static void test_error_handler();)
188   NOT_PRODUCT(static void controlled_crash(int how);)
189 
190   // returns an address which is guaranteed to generate a SIGSEGV on read,
191   // for test purposes, which is not NULL and contains bits in every word
192   static void* get_segfault_address();
193 };
194 #endif // SHARE_UTILITIES_VMERROR_HPP
195