1 /*
2 * Copyright (c) 1998, 2014, 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 *
137 ResourceMark rm;
138 assert(h_exception() != NULL, "exception should not be NULL");
139
140 // tracing (do this up front - so it works during boot strapping)
141 if (TraceExceptions) {
142 ttyLocker ttyl;
143 tty->print_cr("Exception <%s%s%s> (" INTPTR_FORMAT ") \n"
144 "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT,
145 h_exception->print_value_string(),
146 message ? ": " : "", message ? message : "",
147 (address)h_exception(), file, line, thread);
148 }
149 // for AbortVMOnException flag
150 NOT_PRODUCT(Exceptions::debug_check_abort(h_exception, message));
151
152 // Check for special boot-strapping/vm-thread handling
153 if (special_exception(thread, file, line, h_exception)) {
154 return;
155 }
156
157 assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable");
158
159 // set the pending exception
160 thread->set_pending_exception(h_exception(), file, line);
161
162 // vm log
163 Events::log_exception(thread, "Exception <%s%s%s> (" INTPTR_FORMAT ") thrown at [%s, line %d]",
164 h_exception->print_value_string(), message ? ": " : "", message ? message : "",
165 (address)h_exception(), file, line);
166 }
167
168
169 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message,
170 Handle h_loader, Handle h_protection_domain) {
171 // Check for special boot-strapping/vm-thread handling
172 if (special_exception(thread, file, line, name, message)) return;
173 // Create and throw exception
174 Handle h_cause(thread, NULL);
175 Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);
176 _throw(thread, file, line, h_exception, message);
211 _throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL));
212 }
213 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) {
214 _throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL));
215 }
216 void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) {
217 _throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL));
218 }
219
220
221 void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, methodHandle method) {
222 Handle exception;
223 if (!THREAD->has_pending_exception()) {
224 Klass* k = SystemDictionary::StackOverflowError_klass();
225 oop e = InstanceKlass::cast(k)->allocate_instance(CHECK);
226 exception = Handle(THREAD, e); // fill_in_stack trace does gc
227 assert(InstanceKlass::cast(k)->is_initialized(), "need to increase min_stack_allowed calculation");
228 if (StackTraceInThrowable) {
229 java_lang_Throwable::fill_in_stack_trace(exception, method());
230 }
231 } else {
232 // if prior exception, throw that one instead
233 exception = Handle(THREAD, THREAD->pending_exception());
234 }
235 _throw(THREAD, file, line, exception);
236 }
237
238 void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) {
239 const int max_msg_size = 1024;
240 va_list ap;
241 va_start(ap, format);
242 char msg[max_msg_size];
243 vsnprintf(msg, max_msg_size, format, ap);
244 msg[max_msg_size-1] = '\0';
245 va_end(ap);
246 _throw_msg(thread, file, line, h_name, msg);
247 }
248
249
250 // Creates an exception oop, calls the <init> method with the given signature.
385 return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain);
386 }
387
388 // Another convenience method that creates handles for null class loaders and
389 // protection domains and null causes.
390 // If the last parameter 'to_utf8_mode' is safe_to_utf8,
391 // it means we can safely ignore the encoding scheme of the message string and
392 // convert it directly to a java UTF8 string. Otherwise, we need to take the
393 // encoding scheme of the string into account. One thing we should do at some
394 // point is to push this flag down to class java_lang_String since other
395 // classes may need similar functionalities.
396 Handle Exceptions::new_exception(Thread* thread, Symbol* name,
397 const char* message,
398 ExceptionMsgToUtf8Mode to_utf8_safe) {
399
400 Handle h_loader(thread, NULL);
401 Handle h_prot(thread, NULL);
402 Handle h_cause(thread, NULL);
403 return Exceptions::new_exception(thread, name, message, h_cause, h_loader,
404 h_prot, to_utf8_safe);
405 }
406
407 // Implementation of ExceptionMark
408
409 ExceptionMark::ExceptionMark(Thread*& thread) {
410 thread = Thread::current();
411 _thread = thread;
412 if (_thread->has_pending_exception()) {
413 oop exception = _thread->pending_exception();
414 _thread->clear_pending_exception(); // Needed to avoid infinite recursion
415 exception->print();
416 fatal("ExceptionMark constructor expects no pending exceptions");
417 }
418 }
419
420
421 ExceptionMark::~ExceptionMark() {
422 if (_thread->has_pending_exception()) {
423 Handle exception(_thread, _thread->pending_exception());
424 _thread->clear_pending_exception(); // Needed to avoid infinite recursion
|
1 /*
2 * Copyright (c) 1998, 2018, 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 *
137 ResourceMark rm;
138 assert(h_exception() != NULL, "exception should not be NULL");
139
140 // tracing (do this up front - so it works during boot strapping)
141 if (TraceExceptions) {
142 ttyLocker ttyl;
143 tty->print_cr("Exception <%s%s%s> (" INTPTR_FORMAT ") \n"
144 "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT,
145 h_exception->print_value_string(),
146 message ? ": " : "", message ? message : "",
147 (address)h_exception(), file, line, thread);
148 }
149 // for AbortVMOnException flag
150 NOT_PRODUCT(Exceptions::debug_check_abort(h_exception, message));
151
152 // Check for special boot-strapping/vm-thread handling
153 if (special_exception(thread, file, line, h_exception)) {
154 return;
155 }
156
157 if (h_exception->is_a(SystemDictionary::OutOfMemoryError_klass())) {
158 count_out_of_memory_exceptions(h_exception);
159 }
160
161 assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable");
162
163 // set the pending exception
164 thread->set_pending_exception(h_exception(), file, line);
165
166 // vm log
167 Events::log_exception(thread, "Exception <%s%s%s> (" INTPTR_FORMAT ") thrown at [%s, line %d]",
168 h_exception->print_value_string(), message ? ": " : "", message ? message : "",
169 (address)h_exception(), file, line);
170 }
171
172
173 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message,
174 Handle h_loader, Handle h_protection_domain) {
175 // Check for special boot-strapping/vm-thread handling
176 if (special_exception(thread, file, line, name, message)) return;
177 // Create and throw exception
178 Handle h_cause(thread, NULL);
179 Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);
180 _throw(thread, file, line, h_exception, message);
215 _throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL));
216 }
217 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) {
218 _throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL));
219 }
220 void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) {
221 _throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL));
222 }
223
224
225 void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, methodHandle method) {
226 Handle exception;
227 if (!THREAD->has_pending_exception()) {
228 Klass* k = SystemDictionary::StackOverflowError_klass();
229 oop e = InstanceKlass::cast(k)->allocate_instance(CHECK);
230 exception = Handle(THREAD, e); // fill_in_stack trace does gc
231 assert(InstanceKlass::cast(k)->is_initialized(), "need to increase min_stack_allowed calculation");
232 if (StackTraceInThrowable) {
233 java_lang_Throwable::fill_in_stack_trace(exception, method());
234 }
235 // Increment counter for hs_err file reporting
236 Atomic::inc(&Exceptions::_stack_overflow_errors);
237 } else {
238 // if prior exception, throw that one instead
239 exception = Handle(THREAD, THREAD->pending_exception());
240 }
241 _throw(THREAD, file, line, exception);
242 }
243
244 void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) {
245 const int max_msg_size = 1024;
246 va_list ap;
247 va_start(ap, format);
248 char msg[max_msg_size];
249 vsnprintf(msg, max_msg_size, format, ap);
250 msg[max_msg_size-1] = '\0';
251 va_end(ap);
252 _throw_msg(thread, file, line, h_name, msg);
253 }
254
255
256 // Creates an exception oop, calls the <init> method with the given signature.
391 return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain);
392 }
393
394 // Another convenience method that creates handles for null class loaders and
395 // protection domains and null causes.
396 // If the last parameter 'to_utf8_mode' is safe_to_utf8,
397 // it means we can safely ignore the encoding scheme of the message string and
398 // convert it directly to a java UTF8 string. Otherwise, we need to take the
399 // encoding scheme of the string into account. One thing we should do at some
400 // point is to push this flag down to class java_lang_String since other
401 // classes may need similar functionalities.
402 Handle Exceptions::new_exception(Thread* thread, Symbol* name,
403 const char* message,
404 ExceptionMsgToUtf8Mode to_utf8_safe) {
405
406 Handle h_loader(thread, NULL);
407 Handle h_prot(thread, NULL);
408 Handle h_cause(thread, NULL);
409 return Exceptions::new_exception(thread, name, message, h_cause, h_loader,
410 h_prot, to_utf8_safe);
411 }
412
413
414 // Exception counting for hs_err file
415 volatile int Exceptions::_stack_overflow_errors = 0;
416 volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0;
417 volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0;
418 volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0;
419
420 void Exceptions::count_out_of_memory_exceptions(Handle exception) {
421 if (exception() == Universe::out_of_memory_error_metaspace()) {
422 Atomic::inc(&_out_of_memory_error_metaspace_errors);
423 } else if (exception() == Universe::out_of_memory_error_class_metaspace()) {
424 Atomic::inc(&_out_of_memory_error_class_metaspace_errors);
425 } else {
426 // everything else reported as java heap OOM
427 Atomic::inc(&_out_of_memory_error_java_heap_errors);
428 }
429 }
430
431 void print_oom_count(outputStream* st, const char *err, int count) {
432 if (count > 0) {
433 st->print_cr("OutOfMemoryError %s=%d", err, count);
434 }
435 }
436
437 bool Exceptions::has_exception_counts() {
438 return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors +
439 _out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0;
440 }
441
442 void Exceptions::print_exception_counts_on_error(outputStream* st) {
443 print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors);
444 print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors);
445 print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors);
446 if (_stack_overflow_errors > 0) {
447 st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors);
448 }
449 }
450
451 // Implementation of ExceptionMark
452
453 ExceptionMark::ExceptionMark(Thread*& thread) {
454 thread = Thread::current();
455 _thread = thread;
456 if (_thread->has_pending_exception()) {
457 oop exception = _thread->pending_exception();
458 _thread->clear_pending_exception(); // Needed to avoid infinite recursion
459 exception->print();
460 fatal("ExceptionMark constructor expects no pending exceptions");
461 }
462 }
463
464
465 ExceptionMark::~ExceptionMark() {
466 if (_thread->has_pending_exception()) {
467 Handle exception(_thread, _thread->pending_exception());
468 _thread->clear_pending_exception(); // Needed to avoid infinite recursion
|