duke@435: /* trims@1907: * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_exceptions.cpp.incl" duke@435: duke@435: duke@435: // Implementation of ThreadShadow duke@435: void check_ThreadShadow() { duke@435: const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception); duke@435: const ByteSize offset2 = Thread::pending_exception_offset(); duke@435: if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly"); duke@435: } duke@435: duke@435: duke@435: void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) { duke@435: assert(exception != NULL && exception->is_oop(), "invalid exception oop"); duke@435: _pending_exception = exception; duke@435: _exception_file = file; duke@435: _exception_line = line; duke@435: } duke@435: duke@435: void ThreadShadow::clear_pending_exception() { duke@435: if (TraceClearedExceptions) { duke@435: if (_pending_exception != NULL) { duke@435: tty->print_cr("Thread::clear_pending_exception: cleared exception:"); duke@435: _pending_exception->print(); duke@435: } duke@435: } duke@435: _pending_exception = NULL; duke@435: _exception_file = NULL; duke@435: _exception_line = 0; duke@435: } duke@435: // Implementation of Exceptions duke@435: duke@435: bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) { duke@435: // bootstrapping check duke@435: if (!Universe::is_fully_initialized()) { duke@435: vm_exit_during_initialization(h_exception); duke@435: ShouldNotReachHere(); duke@435: } duke@435: duke@435: if (thread->is_VM_thread() duke@435: || thread->is_Compiler_thread() ) { duke@435: // We do not care what kind of exception we get for the vm-thread or a thread which duke@435: // is compiling. We just install a dummy exception object duke@435: thread->set_pending_exception(Universe::vm_exception(), file, line); duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: bool Exceptions::special_exception(Thread* thread, const char* file, int line, symbolHandle h_name, const char* message) { duke@435: // bootstrapping check duke@435: if (!Universe::is_fully_initialized()) { duke@435: if (h_name.is_null()) { duke@435: // atleast an informative message. duke@435: vm_exit_during_initialization("Exception", message); duke@435: } else { duke@435: vm_exit_during_initialization(h_name, message); duke@435: } duke@435: ShouldNotReachHere(); duke@435: } duke@435: duke@435: if (thread->is_VM_thread() duke@435: || thread->is_Compiler_thread() ) { duke@435: // We do not care what kind of exception we get for the vm-thread or a thread which duke@435: // is compiling. We just install a dummy exception object duke@435: thread->set_pending_exception(Universe::vm_exception(), file, line); duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // This method should only be called from generated code, duke@435: // therefore the exception oop should be in the oopmap. duke@435: void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) { duke@435: assert(exception != NULL, "exception should not be NULL"); duke@435: Handle h_exception = Handle(thread, exception); duke@435: _throw(thread, file, line, h_exception); duke@435: } duke@435: never@1446: void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) { duke@435: assert(h_exception() != NULL, "exception should not be NULL"); duke@435: duke@435: // tracing (do this up front - so it works during boot strapping) duke@435: if (TraceExceptions) { duke@435: ttyLocker ttyl; duke@435: ResourceMark rm; never@1446: tty->print_cr("Exception <%s>%s%s (" INTPTR_FORMAT " ) \n" never@1446: "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT, never@1446: h_exception->print_value_string(), never@1446: message ? ": " : "", message ? message : "", never@1446: (address)h_exception(), file, line, thread); duke@435: } duke@435: // for AbortVMOnException flag kvn@2039: NOT_PRODUCT(Exceptions::debug_check_abort(h_exception, message)); duke@435: duke@435: // Check for special boot-strapping/vm-thread handling duke@435: if (special_exception(thread, file, line, h_exception)) return; duke@435: never@1577: assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable"); duke@435: duke@435: // set the pending exception duke@435: thread->set_pending_exception(h_exception(), file, line); duke@435: duke@435: // vm log duke@435: Events::log("throw_exception " INTPTR_FORMAT, (address)h_exception()); duke@435: } duke@435: duke@435: duke@435: void Exceptions::_throw_msg(Thread* thread, const char* file, int line, symbolHandle h_name, const char* message, Handle h_loader, Handle h_protection_domain) { duke@435: // Check for special boot-strapping/vm-thread handling duke@435: if (special_exception(thread, file, line, h_name, message)) return; duke@435: // Create and throw exception duke@435: Handle h_cause(thread, NULL); duke@435: Handle h_exception = new_exception(thread, h_name, message, h_cause, h_loader, h_protection_domain); never@1446: _throw(thread, file, line, h_exception, message); duke@435: } duke@435: duke@435: // Throw an exception with a message and a cause duke@435: void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, symbolHandle h_name, const char* message, Handle h_cause, Handle h_loader, Handle h_protection_domain) { duke@435: // Check for special boot-strapping/vm-thread handling duke@435: if (special_exception(thread, file, line, h_name, message)) return; duke@435: // Create and throw exception and init cause duke@435: Handle h_exception = new_exception(thread, h_name, message, h_cause, h_loader, h_protection_domain); never@1446: _throw(thread, file, line, h_exception, message); duke@435: } duke@435: duke@435: // This version creates handles and calls the other version duke@435: void Exceptions::_throw_msg(Thread* thread, const char* file, int line, duke@435: symbolOop name, const char* message) { duke@435: symbolHandle h_name(thread, name); duke@435: Handle h_loader(thread, NULL); duke@435: Handle h_protection_domain(thread, NULL); duke@435: Exceptions::_throw_msg(thread, file, line, h_name, message, h_loader, h_protection_domain); duke@435: } duke@435: duke@435: // This version already has a handle for name duke@435: void Exceptions::_throw_msg(Thread* thread, const char* file, int line, duke@435: symbolHandle name, const char* message) { duke@435: Handle h_loader(thread, NULL); duke@435: Handle h_protection_domain(thread, NULL); duke@435: Exceptions::_throw_msg(thread, file, line, name, message, h_loader, h_protection_domain); duke@435: } duke@435: duke@435: // This version already has a handle for name duke@435: void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, duke@435: symbolHandle name, const char* message, Handle cause) { duke@435: Handle h_loader(thread, NULL); duke@435: Handle h_protection_domain(thread, NULL); duke@435: Exceptions::_throw_msg_cause(thread, file, line, name, message, cause, h_loader, h_protection_domain); duke@435: } duke@435: duke@435: void Exceptions::_throw_args(Thread* thread, const char* file, int line, symbolHandle h_name, symbolHandle h_signature, JavaCallArguments *args) { duke@435: // Check for special boot-strapping/vm-thread handling duke@435: if (special_exception(thread, file, line, h_name, NULL)) return; duke@435: // Create and throw exception duke@435: Handle h_loader(thread, NULL); duke@435: Handle h_prot(thread, NULL); duke@435: Handle h_cause(thread, NULL); duke@435: Handle exception = new_exception(thread, h_name, h_signature, args, h_cause, h_loader, h_prot); duke@435: _throw(thread, file, line, exception); duke@435: } duke@435: duke@435: duke@435: void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line) { duke@435: Handle exception; duke@435: if (!THREAD->has_pending_exception()) { duke@435: klassOop k = SystemDictionary::StackOverflowError_klass(); duke@435: oop e = instanceKlass::cast(k)->allocate_instance(CHECK); duke@435: exception = Handle(THREAD, e); // fill_in_stack trace does gc duke@435: if (StackTraceInThrowable) { duke@435: java_lang_Throwable::fill_in_stack_trace(exception); duke@435: } duke@435: } else { duke@435: // if prior exception, throw that one instead duke@435: exception = Handle(THREAD, THREAD->pending_exception()); duke@435: } duke@435: _throw_oop(THREAD, file, line, exception()); duke@435: } duke@435: duke@435: void Exceptions::fthrow(Thread* thread, const char* file, int line, symbolHandle h_name, const char* format, ...) { duke@435: const int max_msg_size = 1024; duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: char msg[max_msg_size]; duke@435: vsnprintf(msg, max_msg_size, format, ap); duke@435: msg[max_msg_size-1] = '\0'; duke@435: va_end(ap); duke@435: _throw_msg(thread, file, line, h_name, msg); duke@435: } duke@435: duke@435: // Creates an exception oop, calls the method with the given signature. duke@435: // and returns a Handle duke@435: // Initializes the cause if cause non-null duke@435: Handle Exceptions::new_exception(Thread *thread, symbolHandle h_name, duke@435: symbolHandle signature, duke@435: JavaCallArguments *args, duke@435: Handle h_cause, Handle h_loader, duke@435: Handle h_protection_domain) { duke@435: assert(Universe::is_fully_initialized(), duke@435: "cannot be called during initialization"); duke@435: assert(thread->is_Java_thread(), "can only be called by a Java thread"); duke@435: assert(!thread->has_pending_exception(), "already has exception"); duke@435: duke@435: Handle h_exception; duke@435: duke@435: // Resolve exception klass duke@435: klassOop ik = SystemDictionary::resolve_or_fail(h_name, h_loader, h_protection_domain, true, thread); duke@435: instanceKlassHandle klass (thread, ik); duke@435: duke@435: if (!thread->has_pending_exception()) { duke@435: assert(klass.not_null(), "klass must exist"); duke@435: // We are about to create an instance - so make sure that klass is initialized duke@435: klass->initialize(thread); duke@435: if (!thread->has_pending_exception()) { duke@435: // Allocate new exception duke@435: h_exception = klass->allocate_instance_handle(thread); duke@435: if (!thread->has_pending_exception()) { duke@435: JavaValue result(T_VOID); duke@435: args->set_receiver(h_exception); duke@435: // Call constructor duke@435: JavaCalls::call_special(&result, klass, duke@435: vmSymbolHandles::object_initializer_name(), duke@435: signature, duke@435: args, duke@435: thread); duke@435: duke@435: } duke@435: } duke@435: duke@435: // Future: object initializer should take a cause argument duke@435: if (h_cause() != NULL) { never@1577: assert(h_cause->is_a(SystemDictionary::Throwable_klass()), duke@435: "exception cause is not a subclass of java/lang/Throwable"); duke@435: JavaValue result1(T_OBJECT); duke@435: JavaCallArguments args1; duke@435: args1.set_receiver(h_exception); duke@435: args1.push_oop(h_cause); duke@435: JavaCalls::call_virtual(&result1, klass, duke@435: vmSymbolHandles::initCause_name(), duke@435: vmSymbolHandles::throwable_throwable_signature(), duke@435: &args1, duke@435: thread); duke@435: } duke@435: } duke@435: duke@435: // Check if another exception was thrown in the process, if so rethrow that one duke@435: if (thread->has_pending_exception()) { duke@435: h_exception = Handle(thread, thread->pending_exception()); duke@435: thread->clear_pending_exception(); duke@435: } duke@435: return h_exception; duke@435: } duke@435: duke@435: // Convenience method. Calls either the () or (String) method when duke@435: // creating a new exception duke@435: Handle Exceptions::new_exception(Thread* thread, symbolHandle h_name, duke@435: const char* message, Handle h_cause, duke@435: Handle h_loader, duke@435: Handle h_protection_domain, duke@435: ExceptionMsgToUtf8Mode to_utf8_safe) { duke@435: JavaCallArguments args; duke@435: symbolHandle signature; duke@435: if (message == NULL) { duke@435: signature = vmSymbolHandles::void_method_signature(); duke@435: } else { duke@435: // We want to allocate storage, but we can't do that if there's duke@435: // a pending exception, so we preserve any pending exception duke@435: // around the allocation. duke@435: // If we get an exception from the allocation, prefer that to duke@435: // the exception we are trying to build, or the pending exception. duke@435: // This is sort of like what PRESERVE_EXCEPTION_MARK does, except duke@435: // for the preferencing and the early returns. duke@435: Handle incoming_exception (thread, NULL); duke@435: if (thread->has_pending_exception()) { duke@435: incoming_exception = Handle(thread, thread->pending_exception()); duke@435: thread->clear_pending_exception(); duke@435: } duke@435: Handle msg; duke@435: if (to_utf8_safe == safe_to_utf8) { duke@435: // Make a java UTF8 string. duke@435: msg = java_lang_String::create_from_str(message, thread); duke@435: } else { duke@435: // Make a java string keeping the encoding scheme of the original string. duke@435: msg = java_lang_String::create_from_platform_dependent_str(message, thread); duke@435: } duke@435: if (thread->has_pending_exception()) { duke@435: Handle exception(thread, thread->pending_exception()); duke@435: thread->clear_pending_exception(); duke@435: return exception; duke@435: } duke@435: if (incoming_exception.not_null()) { duke@435: return incoming_exception; duke@435: } duke@435: args.push_oop(msg); duke@435: signature = vmSymbolHandles::string_void_signature(); duke@435: } duke@435: return new_exception(thread, h_name, signature, &args, h_cause, h_loader, h_protection_domain); duke@435: } duke@435: duke@435: // Another convenience method that creates handles for null class loaders and duke@435: // protection domains and null causes. duke@435: // If the last parameter 'to_utf8_mode' is safe_to_utf8, duke@435: // it means we can safely ignore the encoding scheme of the message string and duke@435: // convert it directly to a java UTF8 string. Otherwise, we need to take the duke@435: // encoding scheme of the string into account. One thing we should do at some duke@435: // point is to push this flag down to class java_lang_String since other duke@435: // classes may need similar functionalities. duke@435: Handle Exceptions::new_exception(Thread* thread, duke@435: symbolOop name, duke@435: const char* message, duke@435: ExceptionMsgToUtf8Mode to_utf8_safe) { duke@435: duke@435: symbolHandle h_name(thread, name); duke@435: Handle h_loader(thread, NULL); duke@435: Handle h_prot(thread, NULL); duke@435: Handle h_cause(thread, NULL); duke@435: return Exceptions::new_exception(thread, h_name, message, h_cause, h_loader, duke@435: h_prot, to_utf8_safe); duke@435: } duke@435: duke@435: // Implementation of ExceptionMark duke@435: duke@435: ExceptionMark::ExceptionMark(Thread*& thread) { duke@435: thread = Thread::current(); duke@435: _thread = thread; duke@435: if (_thread->has_pending_exception()) { duke@435: oop exception = _thread->pending_exception(); duke@435: _thread->clear_pending_exception(); // Needed to avoid infinite recursion duke@435: exception->print(); duke@435: fatal("ExceptionMark constructor expects no pending exceptions"); duke@435: } duke@435: } duke@435: duke@435: duke@435: ExceptionMark::~ExceptionMark() { duke@435: if (_thread->has_pending_exception()) { duke@435: Handle exception(_thread, _thread->pending_exception()); duke@435: _thread->clear_pending_exception(); // Needed to avoid infinite recursion duke@435: if (is_init_completed()) { duke@435: exception->print(); duke@435: fatal("ExceptionMark destructor expects no pending exceptions"); duke@435: } else { duke@435: vm_exit_during_initialization(exception); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // ---------------------------------------------------------------------------------------- duke@435: duke@435: #ifndef PRODUCT duke@435: // caller frees value_string if necessary kvn@2039: void Exceptions::debug_check_abort(const char *value_string, const char* message) { duke@435: if (AbortVMOnException != NULL && value_string != NULL && duke@435: strstr(value_string, AbortVMOnException)) { kvn@2039: if (AbortVMOnExceptionMessage == NULL || message == NULL || kvn@2039: strcmp(message, AbortVMOnExceptionMessage) == 0) { kvn@2039: fatal(err_msg("Saw %s, aborting", value_string)); kvn@2039: } duke@435: } duke@435: } duke@435: kvn@2039: void Exceptions::debug_check_abort(Handle exception, const char* message) { duke@435: if (AbortVMOnException != NULL) { duke@435: ResourceMark rm; kvn@2039: if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) { kvn@2039: oop msg = java_lang_Throwable::message(exception); kvn@2039: if (msg != NULL) { kvn@2039: message = java_lang_String::as_utf8_string(msg); kvn@2039: } kvn@2039: } kvn@2039: debug_check_abort(instanceKlass::cast(exception()->klass())->external_name(), message); duke@435: } duke@435: } duke@435: #endif