src/share/vm/utilities/exceptions.hpp

changeset 435
a61af66fc99e
child 1145
e5b0439ef4ae
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/exceptions.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,275 @@
     1.4 +/*
     1.5 + * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// This file provides the basic support for exception handling in the VM.
    1.29 +// Note: We do not use C++ exceptions to avoid compiler dependencies and
    1.30 +// unpredictable performance.
    1.31 +//
    1.32 +// Scheme: Exceptions are stored with the thread. There is never more
    1.33 +// than one pending exception per thread. All functions that can throw
    1.34 +// an exception carry a THREAD argument (usually the last argument and
    1.35 +// declared with the TRAPS macro). Throwing an exception means setting
    1.36 +// a pending exception in the thread. Upon return from a function that
    1.37 +// can throw an exception, we must check if an exception is pending.
    1.38 +// The CHECK macros do this in a convenient way. Carrying around the
    1.39 +// thread provides also convenient access to it (e.g. for Handle
    1.40 +// creation, w/o the need for recomputation).
    1.41 +
    1.42 +
    1.43 +
    1.44 +// Forward declarations to be independent of the include structure.
    1.45 +// This allows us to have exceptions.hpp included in top.hpp.
    1.46 +
    1.47 +class Thread;
    1.48 +class Handle;
    1.49 +class symbolHandle;
    1.50 +class symbolOopDesc;
    1.51 +class JavaCallArguments;
    1.52 +
    1.53 +// The ThreadShadow class is a helper class to access the _pending_exception
    1.54 +// field of the Thread class w/o having access to the Thread's interface (for
    1.55 +// include hierachy reasons).
    1.56 +
    1.57 +class ThreadShadow: public CHeapObj {
    1.58 + protected:
    1.59 +  oop  _pending_exception;                       // Thread has gc actions.
    1.60 +  const char* _exception_file;                   // file information for exception (debugging only)
    1.61 +  int         _exception_line;                   // line information for exception (debugging only)
    1.62 +  friend void check_ThreadShadow();              // checks _pending_exception offset
    1.63 +
    1.64 +  // The following virtual exists only to force creation of a vtable.
    1.65 +  // We need ThreadShadow to have a vtable, even in product builds,
    1.66 +  // so that its layout will start at an offset of zero relative to Thread.
    1.67 +  // Some C++ compilers are so "clever" that they put the ThreadShadow
    1.68 +  // base class at offset 4 in Thread (after Thread's vtable), if they
    1.69 +  // notice that Thread has a vtable but ThreadShadow does not.
    1.70 +  virtual void unused_initial_virtual() { }
    1.71 +
    1.72 + public:
    1.73 +  oop  pending_exception() const                 { return _pending_exception; }
    1.74 +  bool has_pending_exception() const             { return _pending_exception != NULL; }
    1.75 +  const char* exception_file() const             { return _exception_file; }
    1.76 +  int  exception_line() const                    { return _exception_line; }
    1.77 +
    1.78 +  // Code generation support
    1.79 +  static ByteSize pending_exception_offset()     { return byte_offset_of(ThreadShadow, _pending_exception); }
    1.80 +
    1.81 +  // use THROW whenever possible!
    1.82 +  void set_pending_exception(oop exception, const char* file, int line);
    1.83 +
    1.84 +  // use CLEAR_PENDING_EXCEPTION whenever possible!
    1.85 +  void clear_pending_exception();
    1.86 +
    1.87 +  ThreadShadow() : _pending_exception(NULL),
    1.88 +                   _exception_file(NULL), _exception_line(0) {}
    1.89 +};
    1.90 +
    1.91 +
    1.92 +// Exceptions is a helper class that encapsulates all operations
    1.93 +// that require access to the thread interface and which are
    1.94 +// relatively rare. The Exceptions operations should only be
    1.95 +// used directly if the macros below are insufficient.
    1.96 +
    1.97 +class Exceptions {
    1.98 +  static bool special_exception(Thread *thread, const char* file, int line, Handle exception);
    1.99 +  static bool special_exception(Thread* thread, const char* file, int line, symbolHandle name, const char* message);
   1.100 + public:
   1.101 +  // this enum is defined to indicate whether it is safe to
   1.102 +  // ignore the encoding scheme of the original message string.
   1.103 +  typedef enum {
   1.104 +    safe_to_utf8 = 0,
   1.105 +    unsafe_to_utf8 = 1
   1.106 +  } ExceptionMsgToUtf8Mode;
   1.107 +  // Throw exceptions: w/o message, w/ message & with formatted message.
   1.108 +  static void _throw_oop(Thread* thread, const char* file, int line, oop exception);
   1.109 +  static void _throw(Thread* thread, const char* file, int line, Handle exception);
   1.110 +  static void _throw_msg(Thread* thread, const char* file, int line,
   1.111 +                         symbolHandle name, const char* message, Handle loader,
   1.112 +                         Handle protection_domain);
   1.113 +  static void _throw_msg(Thread* thread, const char* file, int line,
   1.114 +                         symbolOop name, const char* message);
   1.115 +  static void _throw_msg(Thread* thread, const char* file, int line,
   1.116 +                         symbolHandle name, const char* message);
   1.117 +  static void _throw_args(Thread* thread, const char* file, int line,
   1.118 +                          symbolHandle name, symbolHandle signature,
   1.119 +                          JavaCallArguments* args);
   1.120 +  static void _throw_msg_cause(Thread* thread, const char* file,
   1.121 +                         int line, symbolHandle h_name, const char* message,
   1.122 +                         Handle h_cause, Handle h_loader, Handle h_protection_domain);
   1.123 +  static void _throw_msg_cause(Thread* thread, const char* file, int line,
   1.124 +                            symbolHandle name, const char* message, Handle cause);
   1.125 +
   1.126 +  // There is no THROW... macro for this method. Caller should remember
   1.127 +  // to do a return after calling it.
   1.128 +  static void fthrow(Thread* thread, const char* file, int line, symbolHandle name,
   1.129 +                     const char* format, ...);
   1.130 +
   1.131 +  // Create and initialize a new exception
   1.132 +  static Handle new_exception(Thread* thread, symbolHandle name,
   1.133 +                              symbolHandle signature, JavaCallArguments* args,
   1.134 +                              Handle cause, Handle loader,
   1.135 +                              Handle protection_domain);
   1.136 +
   1.137 +  static Handle new_exception(Thread* thread, symbolHandle name,
   1.138 +                              const char* message, Handle cause, Handle loader,
   1.139 +                              Handle protection_domain,
   1.140 +                              ExceptionMsgToUtf8Mode to_utf8_safe = safe_to_utf8);
   1.141 +
   1.142 + static Handle new_exception(Thread* thread, symbolOop name,
   1.143 +                             const char* message,
   1.144 +                             ExceptionMsgToUtf8Mode to_utf8_safe = safe_to_utf8);
   1.145 +
   1.146 +  static void throw_stack_overflow_exception(Thread* thread, const char* file, int line);
   1.147 +
   1.148 +  // for AbortVMOnException flag
   1.149 +  NOT_PRODUCT(static void debug_check_abort(Handle exception);)
   1.150 +  NOT_PRODUCT(static void debug_check_abort(const char *value_string);)
   1.151 +};
   1.152 +
   1.153 +
   1.154 +// The THREAD & TRAPS macros facilitate the declaration of functions that throw exceptions.
   1.155 +// Convention: Use the TRAPS macro as the last argument of such a function; e.g.:
   1.156 +//
   1.157 +// int this_function_may_trap(int x, float y, TRAPS)
   1.158 +
   1.159 +#define THREAD __the_thread__
   1.160 +#define TRAPS  Thread* THREAD
   1.161 +
   1.162 +
   1.163 +// The CHECK... macros should be used to pass along a THREAD reference and to check for pending
   1.164 +// exceptions. In special situations it is necessary to handle pending exceptions explicitly,
   1.165 +// in these cases the PENDING_EXCEPTION helper macros should be used.
   1.166 +//
   1.167 +// Macro naming conventions: Macros that end with _ require a result value to be returned. They
   1.168 +// are for functions with non-void result type. The result value is usually ignored because of
   1.169 +// the exception and is only needed for syntactic correctness. The _0 ending is a shortcut for
   1.170 +// _(0) since this is a frequent case. Example:
   1.171 +//
   1.172 +// int result = this_function_may_trap(x_arg, y_arg, CHECK_0);
   1.173 +//
   1.174 +// CAUTION: make sure that the function call using a CHECK macro is not the only statement of a
   1.175 +// conditional branch w/o enclosing {} braces, since the CHECK macros expand into several state-
   1.176 +// ments!
   1.177 +
   1.178 +#define PENDING_EXCEPTION                        (((ThreadShadow*)THREAD)->pending_exception())
   1.179 +#define HAS_PENDING_EXCEPTION                    (((ThreadShadow*)THREAD)->has_pending_exception())
   1.180 +#define CLEAR_PENDING_EXCEPTION                  (((ThreadShadow*)THREAD)->clear_pending_exception())
   1.181 +
   1.182 +#define CHECK                                    THREAD); if (HAS_PENDING_EXCEPTION) return       ; (0
   1.183 +#define CHECK_(result)                           THREAD); if (HAS_PENDING_EXCEPTION) return result; (0
   1.184 +#define CHECK_0                                  CHECK_(0)
   1.185 +#define CHECK_NH                                 CHECK_(Handle())
   1.186 +#define CHECK_NULL                               CHECK_(NULL)
   1.187 +#define CHECK_false                              CHECK_(false)
   1.188 +
   1.189 +// The THROW... macros should be used to throw an exception. They require a THREAD variable to be
   1.190 +// visible within the scope containing the THROW. Usually this is achieved by declaring the function
   1.191 +// with a TRAPS argument.
   1.192 +
   1.193 +#define THREAD_AND_LOCATION                      THREAD, __FILE__, __LINE__
   1.194 +
   1.195 +#define THROW_OOP(e)                                \
   1.196 +  { Exceptions::_throw_oop(THREAD_AND_LOCATION, e);                             return;  }
   1.197 +
   1.198 +#define THROW_HANDLE(e)                                \
   1.199 +  { Exceptions::_throw(THREAD_AND_LOCATION, e);                             return;  }
   1.200 +
   1.201 +#define THROW(name)                                 \
   1.202 +  { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, NULL); return;  }
   1.203 +
   1.204 +#define THROW_MSG(name, message)                    \
   1.205 +  { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message); return;  }
   1.206 +
   1.207 +#define THROW_MSG_LOADER(name, message, loader, protection_domain) \
   1.208 +  { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message, loader, protection_domain); return;  }
   1.209 +
   1.210 +#define THROW_ARG(name, signature, args) \
   1.211 +  { Exceptions::_throw_args(THREAD_AND_LOCATION, name, signature, args);   return; }
   1.212 +
   1.213 +#define THROW_OOP_(e, result)                       \
   1.214 +  { Exceptions::_throw_oop(THREAD_AND_LOCATION, e);                           return result; }
   1.215 +
   1.216 +#define THROW_HANDLE_(e, result)                       \
   1.217 +  { Exceptions::_throw(THREAD_AND_LOCATION, e);                           return result; }
   1.218 +
   1.219 +#define THROW_(name, result)                        \
   1.220 +  { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, NULL); return result; }
   1.221 +
   1.222 +#define THROW_MSG_(name, message, result)           \
   1.223 +  { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message); return result; }
   1.224 +
   1.225 +#define THROW_MSG_LOADER_(name, message, loader, protection_domain, result) \
   1.226 +  { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message, loader, protection_domain); return result; }
   1.227 +
   1.228 +#define THROW_ARG_(name, signature, args, result) \
   1.229 +  { Exceptions::_throw_args(THREAD_AND_LOCATION, name, signature, args); return result; }
   1.230 +
   1.231 +#define THROW_MSG_CAUSE_(name, message, cause, result)   \
   1.232 +  { Exceptions::_throw_msg_cause(THREAD_AND_LOCATION, name, message, cause); return result; }
   1.233 +
   1.234 +
   1.235 +#define THROW_OOP_0(e)                      THROW_OOP_(e, 0)
   1.236 +#define THROW_HANDLE_0(e)                   THROW_HANDLE_(e, 0)
   1.237 +#define THROW_0(name)                       THROW_(name, 0)
   1.238 +#define THROW_MSG_0(name, message)          THROW_MSG_(name, message, 0)
   1.239 +#define THROW_WRAPPED_0(name, oop_to_wrap)  THROW_WRAPPED_(name, oop_to_wrap, 0)
   1.240 +#define THROW_ARG_0(name, signature, arg)   THROW_ARG_(name, signature, arg, 0)
   1.241 +#define THROW_MSG_CAUSE_0(name, message, cause) THROW_MSG_CAUSE_(name, message, cause, 0)
   1.242 +
   1.243 +// The CATCH macro checks that no exception has been thrown by a function; it is used at
   1.244 +// call sites about which is statically known that the callee cannot throw an exception
   1.245 +// even though it is declared with TRAPS.
   1.246 +
   1.247 +#define CATCH                              \
   1.248 +  THREAD); if (HAS_PENDING_EXCEPTION) {    \
   1.249 +    oop ex = PENDING_EXCEPTION;            \
   1.250 +    CLEAR_PENDING_EXCEPTION;               \
   1.251 +    ex->print();                           \
   1.252 +    ShouldNotReachHere();                  \
   1.253 +  } (0
   1.254 +
   1.255 +
   1.256 +// ExceptionMark is a stack-allocated helper class for local exception handling.
   1.257 +// It is used with the EXCEPTION_MARK macro.
   1.258 +
   1.259 +class ExceptionMark {
   1.260 + private:
   1.261 +  Thread* _thread;
   1.262 +
   1.263 + public:
   1.264 +  ExceptionMark(Thread*& thread);
   1.265 +  ~ExceptionMark();
   1.266 +};
   1.267 +
   1.268 +
   1.269 +
   1.270 +// Use an EXCEPTION_MARK for 'local' exceptions. EXCEPTION_MARK makes sure that no
   1.271 +// pending exception exists upon entering its scope and tests that no pending exception
   1.272 +// exists when leaving the scope.
   1.273 +
   1.274 +// See also preserveException.hpp for PRESERVE_EXCEPTION_MARK macro,
   1.275 +// which preserves pre-existing exceptions and does not allow new
   1.276 +// exceptions.
   1.277 +
   1.278 +#define EXCEPTION_MARK                           Thread* THREAD; ExceptionMark __em(THREAD);

mercurial