1/*
2 * Copyright (c) 1998, 2012, 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 "runtime/handles.inline.hpp"
27#include "utilities/preserveException.hpp"
28
29// TODO: These three classes should be refactored
30
31PreserveExceptionMark::PreserveExceptionMark(Thread*& thread) {
32  thread     = Thread::current();
33  _thread    = thread;
34  _preserved_exception_oop = Handle(thread, _thread->pending_exception());
35  _preserved_exception_line = _thread->exception_line();
36  _preserved_exception_file = _thread->exception_file();
37  _thread->clear_pending_exception(); // Needed to avoid infinite recursion
38}
39
40
41PreserveExceptionMark::~PreserveExceptionMark() {
42  if (_thread->has_pending_exception()) {
43    oop exception = _thread->pending_exception();
44    _thread->clear_pending_exception(); // Needed to avoid infinite recursion
45    exception->print();
46    fatal("PreserveExceptionMark destructor expects no pending exceptions");
47  }
48  if (_preserved_exception_oop() != NULL) {
49    _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
50  }
51}
52
53
54// This code is cloned from PreserveExceptionMark, except that:
55//   returned pending exceptions do not cause a crash.
56//   thread is passed in, not set (not a reference parameter)
57//   and bug 6431341 has been addressed.
58
59CautiouslyPreserveExceptionMark::CautiouslyPreserveExceptionMark(Thread* thread) {
60  _thread    = thread;
61  _preserved_exception_oop = Handle(thread, _thread->pending_exception());
62  _preserved_exception_line = _thread->exception_line();
63  _preserved_exception_file = _thread->exception_file();
64  _thread->clear_pending_exception(); // Pending exceptions are checked in the destructor
65}
66
67
68CautiouslyPreserveExceptionMark::~CautiouslyPreserveExceptionMark() {
69  assert(!_thread->has_pending_exception(), "unexpected exception generated");
70  if (_thread->has_pending_exception()) {
71    _thread->clear_pending_exception();
72  }
73  if (_preserved_exception_oop() != NULL) {
74    _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
75  }
76}
77
78
79void WeakPreserveExceptionMark::preserve() {
80  _preserved_exception_oop = Handle(_thread, _thread->pending_exception());
81  _preserved_exception_line = _thread->exception_line();
82  _preserved_exception_file = _thread->exception_file();
83  _thread->clear_pending_exception();
84}
85
86void WeakPreserveExceptionMark::restore() {
87  if (!_thread->has_pending_exception()) {
88    _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
89  }
90}
91