Tue, 08 Aug 2017 15:57:29 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "classfile/systemDictionary.hpp" |
aoqi@0 | 27 | #include "classfile/vmSymbols.hpp" |
aoqi@0 | 28 | #include "compiler/compileBroker.hpp" |
aoqi@0 | 29 | #include "oops/oop.inline.hpp" |
aoqi@0 | 30 | #include "runtime/init.hpp" |
aoqi@0 | 31 | #include "runtime/java.hpp" |
aoqi@0 | 32 | #include "runtime/javaCalls.hpp" |
aoqi@0 | 33 | #include "runtime/thread.inline.hpp" |
aoqi@0 | 34 | #include "runtime/threadCritical.hpp" |
aoqi@0 | 35 | #include "utilities/events.hpp" |
aoqi@0 | 36 | #include "utilities/exceptions.hpp" |
aoqi@0 | 37 | |
aoqi@0 | 38 | PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
aoqi@0 | 39 | |
aoqi@0 | 40 | // Implementation of ThreadShadow |
aoqi@0 | 41 | void check_ThreadShadow() { |
aoqi@0 | 42 | const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception); |
aoqi@0 | 43 | const ByteSize offset2 = Thread::pending_exception_offset(); |
aoqi@0 | 44 | if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly"); |
aoqi@0 | 45 | } |
aoqi@0 | 46 | |
aoqi@0 | 47 | |
aoqi@0 | 48 | void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) { |
aoqi@0 | 49 | assert(exception != NULL && exception->is_oop(), "invalid exception oop"); |
aoqi@0 | 50 | _pending_exception = exception; |
aoqi@0 | 51 | _exception_file = file; |
aoqi@0 | 52 | _exception_line = line; |
aoqi@0 | 53 | } |
aoqi@0 | 54 | |
aoqi@0 | 55 | void ThreadShadow::clear_pending_exception() { |
aoqi@0 | 56 | if (TraceClearedExceptions) { |
aoqi@0 | 57 | if (_pending_exception != NULL) { |
aoqi@0 | 58 | tty->print_cr("Thread::clear_pending_exception: cleared exception:"); |
aoqi@0 | 59 | _pending_exception->print(); |
aoqi@0 | 60 | } |
aoqi@0 | 61 | } |
aoqi@0 | 62 | _pending_exception = NULL; |
aoqi@0 | 63 | _exception_file = NULL; |
aoqi@0 | 64 | _exception_line = 0; |
aoqi@0 | 65 | } |
aoqi@0 | 66 | // Implementation of Exceptions |
aoqi@0 | 67 | |
aoqi@0 | 68 | bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) { |
aoqi@0 | 69 | // bootstrapping check |
aoqi@0 | 70 | if (!Universe::is_fully_initialized()) { |
aoqi@0 | 71 | vm_exit_during_initialization(h_exception); |
aoqi@0 | 72 | ShouldNotReachHere(); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | #ifdef ASSERT |
aoqi@0 | 76 | // Check for trying to throw stack overflow before initialization is complete |
aoqi@0 | 77 | // to prevent infinite recursion trying to initialize stack overflow without |
aoqi@0 | 78 | // adequate stack space. |
aoqi@0 | 79 | // This can happen with stress testing a large value of StackShadowPages |
aoqi@0 | 80 | if (h_exception()->klass() == SystemDictionary::StackOverflowError_klass()) { |
aoqi@0 | 81 | InstanceKlass* ik = InstanceKlass::cast(h_exception->klass()); |
aoqi@0 | 82 | assert(ik->is_initialized(), |
aoqi@0 | 83 | "need to increase min_stack_allowed calculation"); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | #endif // ASSERT |
aoqi@0 | 86 | |
aoqi@0 | 87 | if (thread->is_VM_thread() |
aoqi@0 | 88 | || thread->is_Compiler_thread() ) { |
aoqi@0 | 89 | // We do not care what kind of exception we get for the vm-thread or a thread which |
aoqi@0 | 90 | // is compiling. We just install a dummy exception object |
aoqi@0 | 91 | thread->set_pending_exception(Universe::vm_exception(), file, line); |
aoqi@0 | 92 | return true; |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | return false; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | bool Exceptions::special_exception(Thread* thread, const char* file, int line, Symbol* h_name, const char* message) { |
aoqi@0 | 99 | // bootstrapping check |
aoqi@0 | 100 | if (!Universe::is_fully_initialized()) { |
aoqi@0 | 101 | if (h_name == NULL) { |
aoqi@0 | 102 | // atleast an informative message. |
aoqi@0 | 103 | vm_exit_during_initialization("Exception", message); |
aoqi@0 | 104 | } else { |
aoqi@0 | 105 | vm_exit_during_initialization(h_name, message); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | ShouldNotReachHere(); |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | if (thread->is_VM_thread() |
aoqi@0 | 111 | || thread->is_Compiler_thread() ) { |
aoqi@0 | 112 | // We do not care what kind of exception we get for the vm-thread or a thread which |
aoqi@0 | 113 | // is compiling. We just install a dummy exception object |
aoqi@0 | 114 | thread->set_pending_exception(Universe::vm_exception(), file, line); |
aoqi@0 | 115 | return true; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | return false; |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | // This method should only be called from generated code, |
aoqi@0 | 121 | // therefore the exception oop should be in the oopmap. |
aoqi@0 | 122 | void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) { |
aoqi@0 | 123 | assert(exception != NULL, "exception should not be NULL"); |
aoqi@0 | 124 | Handle h_exception = Handle(thread, exception); |
aoqi@0 | 125 | _throw(thread, file, line, h_exception); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) { |
aoqi@0 | 129 | ResourceMark rm; |
aoqi@0 | 130 | assert(h_exception() != NULL, "exception should not be NULL"); |
aoqi@0 | 131 | |
aoqi@0 | 132 | // tracing (do this up front - so it works during boot strapping) |
aoqi@0 | 133 | if (TraceExceptions) { |
aoqi@0 | 134 | ttyLocker ttyl; |
aoqi@0 | 135 | tty->print_cr("Exception <%s%s%s> (" INTPTR_FORMAT ") \n" |
aoqi@0 | 136 | "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT, |
aoqi@0 | 137 | h_exception->print_value_string(), |
aoqi@0 | 138 | message ? ": " : "", message ? message : "", |
aoqi@0 | 139 | (address)h_exception(), file, line, thread); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | // for AbortVMOnException flag |
aoqi@0 | 142 | NOT_PRODUCT(Exceptions::debug_check_abort(h_exception, message)); |
aoqi@0 | 143 | |
aoqi@0 | 144 | // Check for special boot-strapping/vm-thread handling |
aoqi@0 | 145 | if (special_exception(thread, file, line, h_exception)) { |
aoqi@0 | 146 | return; |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable"); |
aoqi@0 | 150 | |
aoqi@0 | 151 | // set the pending exception |
aoqi@0 | 152 | thread->set_pending_exception(h_exception(), file, line); |
aoqi@0 | 153 | |
aoqi@0 | 154 | // vm log |
aoqi@0 | 155 | Events::log_exception(thread, "Exception <%s%s%s> (" INTPTR_FORMAT ") thrown at [%s, line %d]", |
aoqi@0 | 156 | h_exception->print_value_string(), message ? ": " : "", message ? message : "", |
aoqi@0 | 157 | (address)h_exception(), file, line); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | |
aoqi@0 | 161 | void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message, |
aoqi@0 | 162 | Handle h_loader, Handle h_protection_domain) { |
aoqi@0 | 163 | // Check for special boot-strapping/vm-thread handling |
aoqi@0 | 164 | if (special_exception(thread, file, line, name, message)) return; |
aoqi@0 | 165 | // Create and throw exception |
aoqi@0 | 166 | Handle h_cause(thread, NULL); |
aoqi@0 | 167 | Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain); |
aoqi@0 | 168 | _throw(thread, file, line, h_exception, message); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause, |
aoqi@0 | 172 | Handle h_loader, Handle h_protection_domain) { |
aoqi@0 | 173 | // Check for special boot-strapping/vm-thread handling |
aoqi@0 | 174 | if (special_exception(thread, file, line, name, message)) return; |
aoqi@0 | 175 | // Create and throw exception and init cause |
aoqi@0 | 176 | Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain); |
aoqi@0 | 177 | _throw(thread, file, line, h_exception, message); |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause, |
aoqi@0 | 181 | Handle h_loader, Handle h_protection_domain) { |
aoqi@0 | 182 | // Check for special boot-strapping/vm-thread handling |
aoqi@0 | 183 | if (special_exception(thread, file, line, h_cause)) return; |
aoqi@0 | 184 | // Create and throw exception |
aoqi@0 | 185 | Handle h_exception = new_exception(thread, name, h_cause, h_loader, h_protection_domain); |
aoqi@0 | 186 | _throw(thread, file, line, h_exception, NULL); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | void Exceptions::_throw_args(Thread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) { |
aoqi@0 | 190 | // Check for special boot-strapping/vm-thread handling |
aoqi@0 | 191 | if (special_exception(thread, file, line, name, NULL)) return; |
aoqi@0 | 192 | // Create and throw exception |
aoqi@0 | 193 | Handle h_loader(thread, NULL); |
aoqi@0 | 194 | Handle h_prot(thread, NULL); |
aoqi@0 | 195 | Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot); |
aoqi@0 | 196 | _throw(thread, file, line, exception); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | |
aoqi@0 | 199 | |
aoqi@0 | 200 | // Methods for default parameters. |
aoqi@0 | 201 | // NOTE: These must be here (and not in the header file) because of include circularities. |
aoqi@0 | 202 | void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) { |
aoqi@0 | 203 | _throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL)); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) { |
aoqi@0 | 206 | _throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL)); |
aoqi@0 | 207 | } |
aoqi@0 | 208 | void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) { |
aoqi@0 | 209 | _throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL)); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | |
aoqi@0 | 213 | void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, methodHandle method) { |
aoqi@0 | 214 | Handle exception; |
aoqi@0 | 215 | if (!THREAD->has_pending_exception()) { |
aoqi@0 | 216 | Klass* k = SystemDictionary::StackOverflowError_klass(); |
aoqi@0 | 217 | oop e = InstanceKlass::cast(k)->allocate_instance(CHECK); |
aoqi@0 | 218 | exception = Handle(THREAD, e); // fill_in_stack trace does gc |
aoqi@0 | 219 | assert(InstanceKlass::cast(k)->is_initialized(), "need to increase min_stack_allowed calculation"); |
aoqi@0 | 220 | if (StackTraceInThrowable) { |
aoqi@0 | 221 | java_lang_Throwable::fill_in_stack_trace(exception, method()); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | } else { |
aoqi@0 | 224 | // if prior exception, throw that one instead |
aoqi@0 | 225 | exception = Handle(THREAD, THREAD->pending_exception()); |
aoqi@0 | 226 | } |
aoqi@0 | 227 | _throw(THREAD, file, line, exception); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | |
aoqi@0 | 230 | void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) { |
aoqi@0 | 231 | const int max_msg_size = 1024; |
aoqi@0 | 232 | va_list ap; |
aoqi@0 | 233 | va_start(ap, format); |
aoqi@0 | 234 | char msg[max_msg_size]; |
aoqi@0 | 235 | vsnprintf(msg, max_msg_size, format, ap); |
aoqi@0 | 236 | msg[max_msg_size-1] = '\0'; |
aoqi@0 | 237 | va_end(ap); |
aoqi@0 | 238 | _throw_msg(thread, file, line, h_name, msg); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | |
aoqi@0 | 242 | // Creates an exception oop, calls the <init> method with the given signature. |
aoqi@0 | 243 | // and returns a Handle |
aoqi@0 | 244 | Handle Exceptions::new_exception(Thread *thread, Symbol* name, |
aoqi@0 | 245 | Symbol* signature, JavaCallArguments *args, |
aoqi@0 | 246 | Handle h_loader, Handle h_protection_domain) { |
aoqi@0 | 247 | assert(Universe::is_fully_initialized(), |
aoqi@0 | 248 | "cannot be called during initialization"); |
aoqi@0 | 249 | assert(thread->is_Java_thread(), "can only be called by a Java thread"); |
aoqi@0 | 250 | assert(!thread->has_pending_exception(), "already has exception"); |
aoqi@0 | 251 | |
aoqi@0 | 252 | Handle h_exception; |
aoqi@0 | 253 | |
aoqi@0 | 254 | // Resolve exception klass |
aoqi@0 | 255 | Klass* ik = SystemDictionary::resolve_or_fail(name, h_loader, h_protection_domain, true, thread); |
aoqi@0 | 256 | instanceKlassHandle klass(thread, ik); |
aoqi@0 | 257 | |
aoqi@0 | 258 | if (!thread->has_pending_exception()) { |
aoqi@0 | 259 | assert(klass.not_null(), "klass must exist"); |
aoqi@0 | 260 | // We are about to create an instance - so make sure that klass is initialized |
aoqi@0 | 261 | klass->initialize(thread); |
aoqi@0 | 262 | if (!thread->has_pending_exception()) { |
aoqi@0 | 263 | // Allocate new exception |
aoqi@0 | 264 | h_exception = klass->allocate_instance_handle(thread); |
aoqi@0 | 265 | if (!thread->has_pending_exception()) { |
aoqi@0 | 266 | JavaValue result(T_VOID); |
aoqi@0 | 267 | args->set_receiver(h_exception); |
aoqi@0 | 268 | // Call constructor |
aoqi@0 | 269 | JavaCalls::call_special(&result, klass, |
aoqi@0 | 270 | vmSymbols::object_initializer_name(), |
aoqi@0 | 271 | signature, |
aoqi@0 | 272 | args, |
aoqi@0 | 273 | thread); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | } |
aoqi@0 | 276 | } |
aoqi@0 | 277 | |
aoqi@0 | 278 | // Check if another exception was thrown in the process, if so rethrow that one |
aoqi@0 | 279 | if (thread->has_pending_exception()) { |
aoqi@0 | 280 | h_exception = Handle(thread, thread->pending_exception()); |
aoqi@0 | 281 | thread->clear_pending_exception(); |
aoqi@0 | 282 | } |
aoqi@0 | 283 | return h_exception; |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | // Creates an exception oop, calls the <init> method with the given signature. |
aoqi@0 | 287 | // and returns a Handle |
aoqi@0 | 288 | // Initializes the cause if cause non-null |
aoqi@0 | 289 | Handle Exceptions::new_exception(Thread *thread, Symbol* name, |
aoqi@0 | 290 | Symbol* signature, JavaCallArguments *args, |
aoqi@0 | 291 | Handle h_cause, |
aoqi@0 | 292 | Handle h_loader, Handle h_protection_domain) { |
aoqi@0 | 293 | Handle h_exception = new_exception(thread, name, signature, args, h_loader, h_protection_domain); |
aoqi@0 | 294 | |
aoqi@0 | 295 | // Future: object initializer should take a cause argument |
aoqi@0 | 296 | if (h_cause.not_null()) { |
aoqi@0 | 297 | assert(h_cause->is_a(SystemDictionary::Throwable_klass()), |
aoqi@0 | 298 | "exception cause is not a subclass of java/lang/Throwable"); |
aoqi@0 | 299 | JavaValue result1(T_OBJECT); |
aoqi@0 | 300 | JavaCallArguments args1; |
aoqi@0 | 301 | args1.set_receiver(h_exception); |
aoqi@0 | 302 | args1.push_oop(h_cause); |
aoqi@0 | 303 | JavaCalls::call_virtual(&result1, h_exception->klass(), |
aoqi@0 | 304 | vmSymbols::initCause_name(), |
aoqi@0 | 305 | vmSymbols::throwable_throwable_signature(), |
aoqi@0 | 306 | &args1, |
aoqi@0 | 307 | thread); |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | // Check if another exception was thrown in the process, if so rethrow that one |
aoqi@0 | 311 | if (thread->has_pending_exception()) { |
aoqi@0 | 312 | h_exception = Handle(thread, thread->pending_exception()); |
aoqi@0 | 313 | thread->clear_pending_exception(); |
aoqi@0 | 314 | } |
aoqi@0 | 315 | return h_exception; |
aoqi@0 | 316 | } |
aoqi@0 | 317 | |
aoqi@0 | 318 | // Convenience method. Calls either the <init>() or <init>(Throwable) method when |
aoqi@0 | 319 | // creating a new exception |
aoqi@0 | 320 | Handle Exceptions::new_exception(Thread* thread, Symbol* name, |
aoqi@0 | 321 | Handle h_cause, |
aoqi@0 | 322 | Handle h_loader, Handle h_protection_domain, |
aoqi@0 | 323 | ExceptionMsgToUtf8Mode to_utf8_safe) { |
aoqi@0 | 324 | JavaCallArguments args; |
aoqi@0 | 325 | Symbol* signature = NULL; |
aoqi@0 | 326 | if (h_cause.is_null()) { |
aoqi@0 | 327 | signature = vmSymbols::void_method_signature(); |
aoqi@0 | 328 | } else { |
aoqi@0 | 329 | signature = vmSymbols::throwable_void_signature(); |
aoqi@0 | 330 | args.push_oop(h_cause); |
aoqi@0 | 331 | } |
aoqi@0 | 332 | return new_exception(thread, name, signature, &args, h_loader, h_protection_domain); |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | // Convenience method. Calls either the <init>() or <init>(String) method when |
aoqi@0 | 336 | // creating a new exception |
aoqi@0 | 337 | Handle Exceptions::new_exception(Thread* thread, Symbol* name, |
aoqi@0 | 338 | const char* message, Handle h_cause, |
aoqi@0 | 339 | Handle h_loader, Handle h_protection_domain, |
aoqi@0 | 340 | ExceptionMsgToUtf8Mode to_utf8_safe) { |
aoqi@0 | 341 | JavaCallArguments args; |
aoqi@0 | 342 | Symbol* signature = NULL; |
aoqi@0 | 343 | if (message == NULL) { |
aoqi@0 | 344 | signature = vmSymbols::void_method_signature(); |
aoqi@0 | 345 | } else { |
aoqi@0 | 346 | // We want to allocate storage, but we can't do that if there's |
aoqi@0 | 347 | // a pending exception, so we preserve any pending exception |
aoqi@0 | 348 | // around the allocation. |
aoqi@0 | 349 | // If we get an exception from the allocation, prefer that to |
aoqi@0 | 350 | // the exception we are trying to build, or the pending exception. |
aoqi@0 | 351 | // This is sort of like what PRESERVE_EXCEPTION_MARK does, except |
aoqi@0 | 352 | // for the preferencing and the early returns. |
aoqi@0 | 353 | Handle incoming_exception(thread, NULL); |
aoqi@0 | 354 | if (thread->has_pending_exception()) { |
aoqi@0 | 355 | incoming_exception = Handle(thread, thread->pending_exception()); |
aoqi@0 | 356 | thread->clear_pending_exception(); |
aoqi@0 | 357 | } |
aoqi@0 | 358 | Handle msg; |
aoqi@0 | 359 | if (to_utf8_safe == safe_to_utf8) { |
aoqi@0 | 360 | // Make a java UTF8 string. |
aoqi@0 | 361 | msg = java_lang_String::create_from_str(message, thread); |
aoqi@0 | 362 | } else { |
aoqi@0 | 363 | // Make a java string keeping the encoding scheme of the original string. |
aoqi@0 | 364 | msg = java_lang_String::create_from_platform_dependent_str(message, thread); |
aoqi@0 | 365 | } |
aoqi@0 | 366 | if (thread->has_pending_exception()) { |
aoqi@0 | 367 | Handle exception(thread, thread->pending_exception()); |
aoqi@0 | 368 | thread->clear_pending_exception(); |
aoqi@0 | 369 | return exception; |
aoqi@0 | 370 | } |
aoqi@0 | 371 | if (incoming_exception.not_null()) { |
aoqi@0 | 372 | return incoming_exception; |
aoqi@0 | 373 | } |
aoqi@0 | 374 | args.push_oop(msg); |
aoqi@0 | 375 | signature = vmSymbols::string_void_signature(); |
aoqi@0 | 376 | } |
aoqi@0 | 377 | return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain); |
aoqi@0 | 378 | } |
aoqi@0 | 379 | |
aoqi@0 | 380 | // Another convenience method that creates handles for null class loaders and |
aoqi@0 | 381 | // protection domains and null causes. |
aoqi@0 | 382 | // If the last parameter 'to_utf8_mode' is safe_to_utf8, |
aoqi@0 | 383 | // it means we can safely ignore the encoding scheme of the message string and |
aoqi@0 | 384 | // convert it directly to a java UTF8 string. Otherwise, we need to take the |
aoqi@0 | 385 | // encoding scheme of the string into account. One thing we should do at some |
aoqi@0 | 386 | // point is to push this flag down to class java_lang_String since other |
aoqi@0 | 387 | // classes may need similar functionalities. |
aoqi@0 | 388 | Handle Exceptions::new_exception(Thread* thread, Symbol* name, |
aoqi@0 | 389 | const char* message, |
aoqi@0 | 390 | ExceptionMsgToUtf8Mode to_utf8_safe) { |
aoqi@0 | 391 | |
aoqi@0 | 392 | Handle h_loader(thread, NULL); |
aoqi@0 | 393 | Handle h_prot(thread, NULL); |
aoqi@0 | 394 | Handle h_cause(thread, NULL); |
aoqi@0 | 395 | return Exceptions::new_exception(thread, name, message, h_cause, h_loader, |
aoqi@0 | 396 | h_prot, to_utf8_safe); |
aoqi@0 | 397 | } |
aoqi@0 | 398 | |
aoqi@0 | 399 | // Implementation of ExceptionMark |
aoqi@0 | 400 | |
aoqi@0 | 401 | ExceptionMark::ExceptionMark(Thread*& thread) { |
aoqi@0 | 402 | thread = Thread::current(); |
aoqi@0 | 403 | _thread = thread; |
aoqi@0 | 404 | if (_thread->has_pending_exception()) { |
aoqi@0 | 405 | oop exception = _thread->pending_exception(); |
aoqi@0 | 406 | _thread->clear_pending_exception(); // Needed to avoid infinite recursion |
aoqi@0 | 407 | exception->print(); |
aoqi@0 | 408 | fatal("ExceptionMark constructor expects no pending exceptions"); |
aoqi@0 | 409 | } |
aoqi@0 | 410 | } |
aoqi@0 | 411 | |
aoqi@0 | 412 | |
aoqi@0 | 413 | ExceptionMark::~ExceptionMark() { |
aoqi@0 | 414 | if (_thread->has_pending_exception()) { |
aoqi@0 | 415 | Handle exception(_thread, _thread->pending_exception()); |
aoqi@0 | 416 | _thread->clear_pending_exception(); // Needed to avoid infinite recursion |
aoqi@0 | 417 | if (is_init_completed()) { |
aoqi@0 | 418 | exception->print(); |
aoqi@0 | 419 | fatal("ExceptionMark destructor expects no pending exceptions"); |
aoqi@0 | 420 | } else { |
aoqi@0 | 421 | vm_exit_during_initialization(exception); |
aoqi@0 | 422 | } |
aoqi@0 | 423 | } |
aoqi@0 | 424 | } |
aoqi@0 | 425 | |
aoqi@0 | 426 | // ---------------------------------------------------------------------------------------- |
aoqi@0 | 427 | |
aoqi@0 | 428 | #ifndef PRODUCT |
aoqi@0 | 429 | // caller frees value_string if necessary |
aoqi@0 | 430 | void Exceptions::debug_check_abort(const char *value_string, const char* message) { |
aoqi@0 | 431 | if (AbortVMOnException != NULL && value_string != NULL && |
aoqi@0 | 432 | strstr(value_string, AbortVMOnException)) { |
aoqi@0 | 433 | if (AbortVMOnExceptionMessage == NULL || message == NULL || |
aoqi@0 | 434 | strcmp(message, AbortVMOnExceptionMessage) == 0) { |
aoqi@0 | 435 | fatal(err_msg("Saw %s, aborting", value_string)); |
aoqi@0 | 436 | } |
aoqi@0 | 437 | } |
aoqi@0 | 438 | } |
aoqi@0 | 439 | |
aoqi@0 | 440 | void Exceptions::debug_check_abort(Handle exception, const char* message) { |
aoqi@0 | 441 | if (AbortVMOnException != NULL) { |
aoqi@0 | 442 | ResourceMark rm; |
aoqi@0 | 443 | if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) { |
aoqi@0 | 444 | oop msg = java_lang_Throwable::message(exception); |
aoqi@0 | 445 | if (msg != NULL) { |
aoqi@0 | 446 | message = java_lang_String::as_utf8_string(msg); |
aoqi@0 | 447 | } |
aoqi@0 | 448 | } |
aoqi@0 | 449 | debug_check_abort(InstanceKlass::cast(exception()->klass())->external_name(), message); |
aoqi@0 | 450 | } |
aoqi@0 | 451 | } |
aoqi@0 | 452 | #endif |