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