1 /*
2  * Copyright (c) 1998, 2020, 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 "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "logging/log.hpp"
31 #include "logging/logStream.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "memory/universe.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/init.hpp"
37 #include "runtime/java.hpp"
38 #include "runtime/javaCalls.hpp"
39 #include "runtime/os.hpp"
40 #include "runtime/thread.inline.hpp"
41 #include "runtime/threadCritical.hpp"
42 #include "runtime/atomic.hpp"
43 #include "utilities/events.hpp"
44 #include "utilities/exceptions.hpp"
45 
46 // Implementation of ThreadShadow
check_ThreadShadow()47 void check_ThreadShadow() {
48   const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception);
49   const ByteSize offset2 = Thread::pending_exception_offset();
50   if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly");
51 }
52 
53 
set_pending_exception(oop exception,const char * file,int line)54 void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) {
55   assert(exception != NULL && oopDesc::is_oop(exception), "invalid exception oop");
56   _pending_exception = exception;
57   _exception_file    = file;
58   _exception_line    = line;
59 }
60 
clear_pending_exception()61 void ThreadShadow::clear_pending_exception() {
62   LogTarget(Debug, exceptions) lt;
63   if (_pending_exception != NULL && lt.is_enabled()) {
64     ResourceMark rm;
65     LogStream ls(lt);
66     ls.print("Thread::clear_pending_exception: cleared exception:");
67     _pending_exception->print_on(&ls);
68   }
69   _pending_exception = NULL;
70   _exception_file    = NULL;
71   _exception_line    = 0;
72 }
73 
clear_pending_nonasync_exception()74 void ThreadShadow::clear_pending_nonasync_exception() {
75   // Do not clear probable async exceptions.
76   if (!_pending_exception->is_a(SystemDictionary::ThreadDeath_klass()) &&
77       (_pending_exception->klass() != SystemDictionary::InternalError_klass() ||
78        java_lang_InternalError::during_unsafe_access(_pending_exception) != JNI_TRUE)) {
79     clear_pending_exception();
80   }
81 }
82 
83 // Implementation of Exceptions
84 
special_exception(Thread * thread,const char * file,int line,Handle h_exception)85 bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) {
86   // bootstrapping check
87   if (!Universe::is_fully_initialized()) {
88    vm_exit_during_initialization(h_exception);
89    ShouldNotReachHere();
90   }
91 
92 #ifdef ASSERT
93   // Check for trying to throw stack overflow before initialization is complete
94   // to prevent infinite recursion trying to initialize stack overflow without
95   // adequate stack space.
96   // This can happen with stress testing a large value of StackShadowPages
97   if (h_exception()->klass() == SystemDictionary::StackOverflowError_klass()) {
98     InstanceKlass* ik = InstanceKlass::cast(h_exception->klass());
99     assert(ik->is_initialized(),
100            "need to increase java_thread_min_stack_allowed calculation");
101   }
102 #endif // ASSERT
103 
104   if (thread->is_VM_thread()
105       || !thread->can_call_java()) {
106     // We do not care what kind of exception we get for the vm-thread or a thread which
107     // is compiling.  We just install a dummy exception object
108     thread->set_pending_exception(Universe::vm_exception(), file, line);
109     return true;
110   }
111 
112   return false;
113 }
114 
special_exception(Thread * thread,const char * file,int line,Symbol * h_name,const char * message)115 bool Exceptions::special_exception(Thread* thread, const char* file, int line, Symbol* h_name, const char* message) {
116   // bootstrapping check
117   if (!Universe::is_fully_initialized()) {
118     if (h_name == NULL) {
119       // atleast an informative message.
120       vm_exit_during_initialization("Exception", message);
121     } else {
122       vm_exit_during_initialization(h_name, message);
123     }
124     ShouldNotReachHere();
125   }
126 
127   if (thread->is_VM_thread()
128       || !thread->can_call_java()) {
129     // We do not care what kind of exception we get for the vm-thread or a thread which
130     // is compiling.  We just install a dummy exception object
131     thread->set_pending_exception(Universe::vm_exception(), file, line);
132     return true;
133   }
134   return false;
135 }
136 
137 // This method should only be called from generated code,
138 // therefore the exception oop should be in the oopmap.
_throw_oop(Thread * thread,const char * file,int line,oop exception)139 void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) {
140   assert(exception != NULL, "exception should not be NULL");
141   Handle h_exception(thread, exception);
142   _throw(thread, file, line, h_exception);
143 }
144 
_throw(Thread * thread,const char * file,int line,Handle h_exception,const char * message)145 void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) {
146   ResourceMark rm(thread);
147   assert(h_exception() != NULL, "exception should not be NULL");
148 
149   // tracing (do this up front - so it works during boot strapping)
150   // Note, the print_value_string() argument is not called unless logging is enabled!
151   log_info(exceptions)("Exception <%s%s%s> (" INTPTR_FORMAT ") \n"
152                        "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT,
153                        h_exception->print_value_string(),
154                        message ? ": " : "", message ? message : "",
155                        p2i(h_exception()), file, line, p2i(thread));
156 
157   // for AbortVMOnException flag
158   Exceptions::debug_check_abort(h_exception, message);
159 
160   // Check for special boot-strapping/vm-thread handling
161   if (special_exception(thread, file, line, h_exception)) {
162     return;
163   }
164 
165   if (h_exception->is_a(SystemDictionary::OutOfMemoryError_klass())) {
166     count_out_of_memory_exceptions(h_exception);
167   }
168 
169   if (h_exception->is_a(SystemDictionary::LinkageError_klass())) {
170     Atomic::inc(&_linkage_errors);
171   }
172 
173   assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable");
174 
175   // set the pending exception
176   thread->set_pending_exception(h_exception(), file, line);
177 
178   // vm log
179   Events::log_exception(thread, h_exception, message, file, line);
180 }
181 
182 
_throw_msg(Thread * thread,const char * file,int line,Symbol * name,const char * message,Handle h_loader,Handle h_protection_domain)183 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message,
184                             Handle h_loader, Handle h_protection_domain) {
185   // Check for special boot-strapping/vm-thread handling
186   if (special_exception(thread, file, line, name, message)) return;
187   // Create and throw exception
188   Handle h_cause(thread, NULL);
189   Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);
190   _throw(thread, file, line, h_exception, message);
191 }
192 
_throw_msg_cause(Thread * thread,const char * file,int line,Symbol * name,const char * message,Handle h_cause,Handle h_loader,Handle h_protection_domain)193 void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause,
194                                   Handle h_loader, Handle h_protection_domain) {
195   // Check for special boot-strapping/vm-thread handling
196   if (special_exception(thread, file, line, name, message)) return;
197   // Create and throw exception and init cause
198   Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);
199   _throw(thread, file, line, h_exception, message);
200 }
201 
_throw_cause(Thread * thread,const char * file,int line,Symbol * name,Handle h_cause,Handle h_loader,Handle h_protection_domain)202 void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause,
203                               Handle h_loader, Handle h_protection_domain) {
204   // Check for special boot-strapping/vm-thread handling
205   if (special_exception(thread, file, line, h_cause)) return;
206   // Create and throw exception
207   Handle h_exception = new_exception(thread, name, h_cause, h_loader, h_protection_domain);
208   _throw(thread, file, line, h_exception, NULL);
209 }
210 
_throw_args(Thread * thread,const char * file,int line,Symbol * name,Symbol * signature,JavaCallArguments * args)211 void Exceptions::_throw_args(Thread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) {
212   // Check for special boot-strapping/vm-thread handling
213   if (special_exception(thread, file, line, name, NULL)) return;
214   // Create and throw exception
215   Handle h_loader(thread, NULL);
216   Handle h_prot(thread, NULL);
217   Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot);
218   _throw(thread, file, line, exception);
219 }
220 
221 
222 // Methods for default parameters.
223 // NOTE: These must be here (and not in the header file) because of include circularities.
_throw_msg_cause(Thread * thread,const char * file,int line,Symbol * name,const char * message,Handle h_cause)224 void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) {
225   _throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL));
226 }
_throw_msg(Thread * thread,const char * file,int line,Symbol * name,const char * message)227 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) {
228   _throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL));
229 }
_throw_cause(Thread * thread,const char * file,int line,Symbol * name,Handle h_cause)230 void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) {
231   _throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL));
232 }
233 
234 
throw_stack_overflow_exception(Thread * THREAD,const char * file,int line,const methodHandle & method)235 void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, const methodHandle& method) {
236   Handle exception;
237   if (!THREAD->has_pending_exception()) {
238     InstanceKlass* k = SystemDictionary::StackOverflowError_klass();
239     oop e = k->allocate_instance(CHECK);
240     exception = Handle(THREAD, e);  // fill_in_stack trace does gc
241     assert(k->is_initialized(), "need to increase java_thread_min_stack_allowed calculation");
242     if (StackTraceInThrowable) {
243       java_lang_Throwable::fill_in_stack_trace(exception, method);
244     }
245     // Increment counter for hs_err file reporting
246     Atomic::inc(&Exceptions::_stack_overflow_errors);
247   } else {
248     // if prior exception, throw that one instead
249     exception = Handle(THREAD, THREAD->pending_exception());
250   }
251   _throw(THREAD, file, line, exception);
252 }
253 
throw_unsafe_access_internal_error(Thread * thread,const char * file,int line,const char * message)254 void Exceptions::throw_unsafe_access_internal_error(Thread* thread, const char* file, int line, const char* message) {
255   Handle h_exception = new_exception(thread, vmSymbols::java_lang_InternalError(), message);
256   java_lang_InternalError::set_during_unsafe_access(h_exception());
257   _throw(thread, file, line, h_exception, message);
258 }
259 
fthrow(Thread * thread,const char * file,int line,Symbol * h_name,const char * format,...)260 void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) {
261   const int max_msg_size = 1024;
262   va_list ap;
263   va_start(ap, format);
264   char msg[max_msg_size];
265   os::vsnprintf(msg, max_msg_size, format, ap);
266   va_end(ap);
267   _throw_msg(thread, file, line, h_name, msg);
268 }
269 
270 
271 // Creates an exception oop, calls the <init> method with the given signature.
272 // and returns a Handle
new_exception(Thread * thread,Symbol * name,Symbol * signature,JavaCallArguments * args,Handle h_loader,Handle h_protection_domain)273 Handle Exceptions::new_exception(Thread *thread, Symbol* name,
274                                  Symbol* signature, JavaCallArguments *args,
275                                  Handle h_loader, Handle h_protection_domain) {
276   assert(Universe::is_fully_initialized(),
277     "cannot be called during initialization");
278   assert(thread->is_Java_thread(), "can only be called by a Java thread");
279   assert(!thread->has_pending_exception(), "already has exception");
280 
281   Handle h_exception;
282 
283   // Resolve exception klass, and check for pending exception below.
284   Klass* klass = SystemDictionary::resolve_or_fail(name, h_loader, h_protection_domain, true, thread);
285 
286   if (!thread->has_pending_exception()) {
287     assert(klass != NULL, "klass must exist");
288     h_exception = JavaCalls::construct_new_instance(InstanceKlass::cast(klass),
289                                 signature,
290                                 args,
291                                 thread);
292   }
293 
294   // Check if another exception was thrown in the process, if so rethrow that one
295   if (thread->has_pending_exception()) {
296     h_exception = Handle(thread, thread->pending_exception());
297     thread->clear_pending_exception();
298   }
299   return h_exception;
300 }
301 
302 // Creates an exception oop, calls the <init> method with the given signature.
303 // and returns a Handle
304 // Initializes the cause if cause non-null
new_exception(Thread * thread,Symbol * name,Symbol * signature,JavaCallArguments * args,Handle h_cause,Handle h_loader,Handle h_protection_domain)305 Handle Exceptions::new_exception(Thread *thread, Symbol* name,
306                                  Symbol* signature, JavaCallArguments *args,
307                                  Handle h_cause,
308                                  Handle h_loader, Handle h_protection_domain) {
309   Handle h_exception = new_exception(thread, name, signature, args, h_loader, h_protection_domain);
310 
311   // Future: object initializer should take a cause argument
312   if (h_cause.not_null()) {
313     assert(h_cause->is_a(SystemDictionary::Throwable_klass()),
314         "exception cause is not a subclass of java/lang/Throwable");
315     JavaValue result1(T_OBJECT);
316     JavaCallArguments args1;
317     args1.set_receiver(h_exception);
318     args1.push_oop(h_cause);
319     JavaCalls::call_virtual(&result1, h_exception->klass(),
320                                       vmSymbols::initCause_name(),
321                                       vmSymbols::throwable_throwable_signature(),
322                                       &args1,
323                                       thread);
324   }
325 
326   // Check if another exception was thrown in the process, if so rethrow that one
327   if (thread->has_pending_exception()) {
328     h_exception = Handle(thread, thread->pending_exception());
329     thread->clear_pending_exception();
330   }
331   return h_exception;
332 }
333 
334 // Convenience method. Calls either the <init>() or <init>(Throwable) method when
335 // creating a new exception
new_exception(Thread * thread,Symbol * name,Handle h_cause,Handle h_loader,Handle h_protection_domain,ExceptionMsgToUtf8Mode to_utf8_safe)336 Handle Exceptions::new_exception(Thread* thread, Symbol* name,
337                                  Handle h_cause,
338                                  Handle h_loader, Handle h_protection_domain,
339                                  ExceptionMsgToUtf8Mode to_utf8_safe) {
340   JavaCallArguments args;
341   Symbol* signature = NULL;
342   if (h_cause.is_null()) {
343     signature = vmSymbols::void_method_signature();
344   } else {
345     signature = vmSymbols::throwable_void_signature();
346     args.push_oop(h_cause);
347   }
348   return new_exception(thread, name, signature, &args, h_loader, h_protection_domain);
349 }
350 
351 // Convenience method. Calls either the <init>() or <init>(String) method when
352 // creating a new exception
new_exception(Thread * thread,Symbol * name,const char * message,Handle h_cause,Handle h_loader,Handle h_protection_domain,ExceptionMsgToUtf8Mode to_utf8_safe)353 Handle Exceptions::new_exception(Thread* thread, Symbol* name,
354                                  const char* message, Handle h_cause,
355                                  Handle h_loader, Handle h_protection_domain,
356                                  ExceptionMsgToUtf8Mode to_utf8_safe) {
357   JavaCallArguments args;
358   Symbol* signature = NULL;
359   if (message == NULL) {
360     signature = vmSymbols::void_method_signature();
361   } else {
362     // We want to allocate storage, but we can't do that if there's
363     // a pending exception, so we preserve any pending exception
364     // around the allocation.
365     // If we get an exception from the allocation, prefer that to
366     // the exception we are trying to build, or the pending exception.
367     // This is sort of like what PRESERVE_EXCEPTION_MARK does, except
368     // for the preferencing and the early returns.
369     Handle incoming_exception(thread, NULL);
370     if (thread->has_pending_exception()) {
371       incoming_exception = Handle(thread, thread->pending_exception());
372       thread->clear_pending_exception();
373     }
374     Handle msg;
375     if (to_utf8_safe == safe_to_utf8) {
376       // Make a java UTF8 string.
377       msg = java_lang_String::create_from_str(message, thread);
378     } else {
379       // Make a java string keeping the encoding scheme of the original string.
380       msg = java_lang_String::create_from_platform_dependent_str(message, thread);
381     }
382     if (thread->has_pending_exception()) {
383       Handle exception(thread, thread->pending_exception());
384       thread->clear_pending_exception();
385       return exception;
386     }
387     if (incoming_exception.not_null()) {
388       return incoming_exception;
389     }
390     args.push_oop(msg);
391     signature = vmSymbols::string_void_signature();
392   }
393   return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain);
394 }
395 
396 // Another convenience method that creates handles for null class loaders and
397 // protection domains and null causes.
398 // If the last parameter 'to_utf8_mode' is safe_to_utf8,
399 // it means we can safely ignore the encoding scheme of the message string and
400 // convert it directly to a java UTF8 string. Otherwise, we need to take the
401 // encoding scheme of the string into account. One thing we should do at some
402 // point is to push this flag down to class java_lang_String since other
403 // classes may need similar functionalities.
new_exception(Thread * thread,Symbol * name,const char * message,ExceptionMsgToUtf8Mode to_utf8_safe)404 Handle Exceptions::new_exception(Thread* thread, Symbol* name,
405                                  const char* message,
406                                  ExceptionMsgToUtf8Mode to_utf8_safe) {
407 
408   Handle       h_loader(thread, NULL);
409   Handle       h_prot(thread, NULL);
410   Handle       h_cause(thread, NULL);
411   return Exceptions::new_exception(thread, name, message, h_cause, h_loader,
412                                    h_prot, to_utf8_safe);
413 }
414 
415 // invokedynamic uses wrap_dynamic_exception for:
416 //    - bootstrap method resolution
417 //    - post call to MethodHandleNatives::linkCallSite
418 // dynamically computed constant uses wrap_dynamic_exception for:
419 //    - bootstrap method resolution
420 //    - post call to MethodHandleNatives::linkDynamicConstant
wrap_dynamic_exception(bool is_indy,Thread * THREAD)421 void Exceptions::wrap_dynamic_exception(bool is_indy, Thread* THREAD) {
422   if (THREAD->has_pending_exception()) {
423     bool log_indy = log_is_enabled(Debug, methodhandles, indy) && is_indy;
424     bool log_condy = log_is_enabled(Debug, methodhandles, condy) && !is_indy;
425     LogStreamHandle(Debug, methodhandles, indy) lsh_indy;
426     LogStreamHandle(Debug, methodhandles, condy) lsh_condy;
427     LogStream* ls = NULL;
428     if (log_indy) {
429       ls = &lsh_indy;
430     } else if (log_condy) {
431       ls = &lsh_condy;
432     }
433     oop exception = THREAD->pending_exception();
434 
435     // See the "Linking Exceptions" section for the invokedynamic instruction
436     // in JVMS 6.5.
437     if (exception->is_a(SystemDictionary::Error_klass())) {
438       // Pass through an Error, including BootstrapMethodError, any other form
439       // of linkage error, or say ThreadDeath/OutOfMemoryError
440       if (ls != NULL) {
441         ls->print_cr("bootstrap method invocation wraps BSME around " INTPTR_FORMAT, p2i((void *)exception));
442         exception->print_on(ls);
443       }
444       return;
445     }
446 
447     // Otherwise wrap the exception in a BootstrapMethodError
448     if (ls != NULL) {
449       ls->print_cr("%s throws BSME for " INTPTR_FORMAT, is_indy ? "invokedynamic" : "dynamic constant", p2i((void *)exception));
450       exception->print_on(ls);
451     }
452     Handle nested_exception(THREAD, exception);
453     THREAD->clear_pending_exception();
454     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
455   }
456 }
457 
458 // Exception counting for hs_err file
459 volatile int Exceptions::_stack_overflow_errors = 0;
460 volatile int Exceptions::_linkage_errors = 0;
461 volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0;
462 volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0;
463 volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0;
464 
count_out_of_memory_exceptions(Handle exception)465 void Exceptions::count_out_of_memory_exceptions(Handle exception) {
466   if (exception() == Universe::out_of_memory_error_metaspace()) {
467      Atomic::inc(&_out_of_memory_error_metaspace_errors);
468   } else if (exception() == Universe::out_of_memory_error_class_metaspace()) {
469      Atomic::inc(&_out_of_memory_error_class_metaspace_errors);
470   } else {
471      // everything else reported as java heap OOM
472      Atomic::inc(&_out_of_memory_error_java_heap_errors);
473   }
474 }
475 
print_oom_count(outputStream * st,const char * err,int count)476 void print_oom_count(outputStream* st, const char *err, int count) {
477   if (count > 0) {
478     st->print_cr("OutOfMemoryError %s=%d", err, count);
479   }
480 }
481 
has_exception_counts()482 bool Exceptions::has_exception_counts() {
483   return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors +
484          _out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0;
485 }
486 
print_exception_counts_on_error(outputStream * st)487 void Exceptions::print_exception_counts_on_error(outputStream* st) {
488   print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors);
489   print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors);
490   print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors);
491   if (_stack_overflow_errors > 0) {
492     st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors);
493   }
494   if (_linkage_errors > 0) {
495     st->print_cr("LinkageErrors=%d", _linkage_errors);
496   }
497 }
498 
499 // Implementation of ExceptionMark
500 
ExceptionMark(Thread * & thread)501 ExceptionMark::ExceptionMark(Thread*& thread) {
502   thread     = Thread::current();
503   _thread    = thread;
504   if (_thread->has_pending_exception()) {
505     oop exception = _thread->pending_exception();
506     _thread->clear_pending_exception(); // Needed to avoid infinite recursion
507     exception->print();
508     fatal("ExceptionMark constructor expects no pending exceptions");
509   }
510 }
511 
512 
~ExceptionMark()513 ExceptionMark::~ExceptionMark() {
514   if (_thread->has_pending_exception()) {
515     Handle exception(_thread, _thread->pending_exception());
516     _thread->clear_pending_exception(); // Needed to avoid infinite recursion
517     if (is_init_completed()) {
518       exception->print();
519       fatal("ExceptionMark destructor expects no pending exceptions");
520     } else {
521       vm_exit_during_initialization(exception);
522     }
523   }
524 }
525 
526 // ----------------------------------------------------------------------------------------
527 
528 // caller frees value_string if necessary
debug_check_abort(const char * value_string,const char * message)529 void Exceptions::debug_check_abort(const char *value_string, const char* message) {
530   if (AbortVMOnException != NULL && value_string != NULL &&
531       strstr(value_string, AbortVMOnException)) {
532     if (AbortVMOnExceptionMessage == NULL || (message != NULL &&
533         strstr(message, AbortVMOnExceptionMessage))) {
534       fatal("Saw %s, aborting", value_string);
535     }
536   }
537 }
538 
debug_check_abort(Handle exception,const char * message)539 void Exceptions::debug_check_abort(Handle exception, const char* message) {
540   if (AbortVMOnException != NULL) {
541     debug_check_abort_helper(exception, message);
542   }
543 }
544 
debug_check_abort_helper(Handle exception,const char * message)545 void Exceptions::debug_check_abort_helper(Handle exception, const char* message) {
546   ResourceMark rm;
547   if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) {
548     oop msg = java_lang_Throwable::message(exception());
549     if (msg != NULL) {
550       message = java_lang_String::as_utf8_string(msg);
551     }
552   }
553   debug_check_abort(exception()->klass()->external_name(), message);
554 }
555 
556 // for logging exceptions
log_exception(Handle exception,const char * message)557 void Exceptions::log_exception(Handle exception, const char* message) {
558   ResourceMark rm;
559   Symbol* detail_message = java_lang_Throwable::detail_message(exception());
560   if (detail_message != NULL) {
561     log_info(exceptions)("Exception <%s: %s>\n thrown in %s",
562                          exception->print_value_string(),
563                          detail_message->as_C_string(),
564                          message);
565   } else {
566     log_info(exceptions)("Exception <%s>\n thrown in %s",
567                          exception->print_value_string(),
568                          message);
569   }
570 }
571