* src/vm/exceptions.c (exceptions_handle_exception) [__ARM__]: Added
[cacao.git] / src / vm / exceptions.c
index a24b6793ea15ca5a61ed3f267f96f17a6844138b..bab903ce08366a49115c1e3bd5739a906f6578a9 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: exceptions.c 7261 2007-01-31 10:00:12Z twisti $
-
 */
 
 
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
 
 #include "vm/types.h"
 
+#include "md-abi.h"
+
 #include "mm/memory.h"
 
 #include "native/jni.h"
+#include "native/llni.h"
 #include "native/native.h"
+
 #include "native/include/java_lang_String.h"
 #include "native/include/java_lang_Throwable.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
-#else
-# include "threads/none/threads.h"
-#endif
+#include "threads/lock-common.h"
+#include "threads/threads-common.h"
 
-#include "toolbox/logging.h"
 #include "toolbox/util.h"
 
 #include "vm/builtin.h"
 #include "vm/jit/asmpart.h"
 #include "vm/jit/jit.h"
 #include "vm/jit/methodheader.h"
+#include "vm/jit/patcher-common.h"
+#include "vm/jit/show.h"
+#include "vm/jit/stacktrace.h"
+#include "vm/jit/trace.h"
 
 #include "vmcore/class.h"
 #include "vmcore/loader.h"
+#include "vmcore/method.h"
 #include "vmcore/options.h"
 
+#if defined(ENABLE_VMLOG)
+#include <vmlog_cacao.h>
+#endif
+
 
 /* for raising exceptions from native methods *********************************/
 
 #if !defined(ENABLE_THREADS)
-java_objectheader *_no_threads_exceptionptr = NULL;
+java_object_t *_no_threads_exceptionptr = NULL;
 #endif
 
 
-/* init_system_exceptions ******************************************************
+/* exceptions_init *************************************************************
 
-   Load and link exceptions used in the system.
+   Initialize the exceptions subsystem.
 
 *******************************************************************************/
 
-bool exceptions_init(void)
+void exceptions_init(void)
 {
-       /* java/lang/Throwable */
-
-       if (!(class_java_lang_Throwable =
-                 load_class_bootstrap(utf_java_lang_Throwable)) ||
-               !link_class(class_java_lang_Throwable))
-               return false;
+#if !(defined(__ARM__) && defined(__LINUX__))
+       /* On arm-linux the first memory page can't be mmap'ed, as it
+          contains the exception vectors. */
 
-       /* java/lang/Error */
+       int pagesize;
 
-       if (!(class_java_lang_Error = load_class_bootstrap(utf_java_lang_Error)) ||
-               !link_class(class_java_lang_Error))
-               return false;
+       /* mmap a memory page at address 0x0, so our hardware-exceptions
+          work. */
 
-#if defined(ENABLE_JAVASE)
-       /* java/lang/LinkageError */
+       pagesize = getpagesize();
 
-       if (!(class_java_lang_LinkageError =
-                 load_class_bootstrap(utf_java_lang_LinkageError)) ||
-               !link_class(class_java_lang_LinkageError))
-               return false;
+       (void) memory_mmap_anon(NULL, pagesize, PROT_NONE, MAP_PRIVATE | MAP_FIXED);
 #endif
 
-       /* java/lang/NoClassDefFoundError */
-
-       if (!(class_java_lang_NoClassDefFoundError =
-                 load_class_bootstrap(utf_java_lang_NoClassDefFoundError)) ||
-               !link_class(class_java_lang_NoClassDefFoundError))
-               return false;
-
-       /* java/lang/OutOfMemoryError */
-
-       if (!(class_java_lang_OutOfMemoryError =
-                 load_class_bootstrap(utf_java_lang_OutOfMemoryError)) ||
-               !link_class(class_java_lang_OutOfMemoryError))
-               return false;
-
-       /* java/lang/VirtualMachineError */
+       /* check if we get into trouble with our hardware-exceptions */
 
-       if (!(class_java_lang_VirtualMachineError =
-                 load_class_bootstrap(utf_java_lang_VirtualMachineError)) ||
-               !link_class(class_java_lang_VirtualMachineError))
-               return false;
-
-
-       /* java/lang/Exception */
-
-       if (!(class_java_lang_Exception =
-                 load_class_bootstrap(utf_java_lang_Exception)) ||
-               !link_class(class_java_lang_Exception))
-               return false;
-
-       /* java/lang/ClassCastException */
-
-       if (!(class_java_lang_ClassCastException =
-                 load_class_bootstrap(utf_java_lang_ClassCastException)) ||
-               !link_class(class_java_lang_ClassCastException))
-               return false;
-
-       /* java/lang/ClassNotFoundException */
+       if (OFFSET(java_bytearray_t, data) <= EXCEPTION_HARDWARE_LARGEST)
+               vm_abort("signal_init: array-data offset is less or equal the maximum hardware-exception displacement: %d <= %d", OFFSET(java_bytearray_t, data), EXCEPTION_HARDWARE_LARGEST);
+}
 
-       if (!(class_java_lang_ClassNotFoundException =
-                 load_class_bootstrap(utf_java_lang_ClassNotFoundException)) ||
-               !link_class(class_java_lang_ClassNotFoundException))
-               return false;
 
-       /* java/lang/NullPointerException */
+/* exceptions_get_exception ****************************************************
 
-       if (!(class_java_lang_NullPointerException =
-                 load_class_bootstrap(utf_java_lang_NullPointerException)) ||
-               !link_class(class_java_lang_NullPointerException))
-               return false;
+   Returns the current exception pointer of the current thread.
 
+*******************************************************************************/
 
-#if defined(WITH_CLASSPATH_GNU)
-       /* java/lang/VMThrowable */
+java_handle_t *exceptions_get_exception(void)
+{
+       java_object_t *o;
+       java_handle_t *e;
+#if defined(ENABLE_THREADS)
+       threadobject  *t;
 
-       if (!(class_java_lang_VMThrowable =
-                 load_class_bootstrap(utf_java_lang_VMThrowable)) ||
-               !link_class(class_java_lang_VMThrowable))
-               return false;
+       t = THREADOBJECT;
 #endif
 
-       return true;
-}
+       /* Get the exception. */
 
+       LLNI_CRITICAL_START;
 
-static void throw_exception_exit_intern(bool doexit)
-{
-       java_objectheader *xptr;
-       classinfo *c;
-       methodinfo *pss;
-
-       xptr = *exceptionptr;
-
-       if (xptr) {
-               /* clear exception, because we are calling jit code again */
-               *exceptionptr = NULL;
+#if defined(ENABLE_THREADS)
+       o = t->_exceptionptr;
+#else
+       o = _no_threads_exceptionptr;
+#endif
 
-               c = xptr->vftbl->class;
+       e = LLNI_WRAP(o);
 
-               pss = class_resolveclassmethod(c,
-                                                                          utf_printStackTrace,
-                                                                          utf_void__void,
-                                                                          class_java_lang_Object,
-                                                                          false);
+       LLNI_CRITICAL_END;
 
-               /* print the stacktrace */
+       /* Return the exception. */
 
-               if (pss) {
-                       (void) vm_call_method(pss, xptr);
+       return e;
+}
 
-                       /* This normally means, we are EXTREMLY out of memory or have a   */
-                       /* serious problem while printStackTrace. But may be another      */
-                       /* exception, so print it.                                        */
 
-                       if (*exceptionptr) {
-                               java_lang_Throwable *t;
+/* exceptions_set_exception ****************************************************
 
-                               t = (java_lang_Throwable *) *exceptionptr;
+   Sets the exception pointer of the current thread.
 
-                               fprintf(stderr, "Exception while printStackTrace(): ");
-                               utf_fprint_printable_ascii_classname(stderr, t->header.vftbl->class->name);
+*******************************************************************************/
 
-                               if (t->detailMessage) {
-                                       char *buf;
+void exceptions_set_exception(java_handle_t *e)
+{
+       threadobject  *t;
+       java_object_t *o;
 
-                                       buf = javastring_tochar((java_objectheader *) t->detailMessage);
-                                       fprintf(stderr, ": %s", buf);
-                                       MFREE(buf, char, strlen(buf));
-                               }
-                                       
-                               fprintf(stderr, "\n");
-                       }
+#if defined(ENABLE_THREADS)
+       t = THREADOBJECT;
+#else
+       t = NULL;
+#endif
 
-               } else {
-                       utf_fprint_printable_ascii_classname(stderr, c->name);
-                       fprintf(stderr, ": printStackTrace()V not found!\n");
-               }
+       /* Set the exception. */
 
-               fflush(stderr);
+       LLNI_CRITICAL_START;
 
-               /* good bye! */
+       o = LLNI_UNWRAP(e);
 
-               if (doexit)
-                       exit(1);
+#if !defined(NDEBUG)
+       if (opt_DebugExceptions) {
+               printf("[exceptions_set_exception  : t=%p, o=%p, class=",
+                          (void *) t, (void *) o);
+               class_print(o->vftbl->class);
+               printf("]\n");
        }
-}
+#endif
 
+#if defined(ENABLE_THREADS)
+       t->_exceptionptr = o;
+#else
+       _no_threads_exceptionptr = o;
+#endif
 
-void throw_exception(void)
-{
-       throw_exception_exit_intern(false);
+       LLNI_CRITICAL_END;
 }
 
 
-void throw_exception_exit(void)
-{
-       throw_exception_exit_intern(true);
-}
+/* exceptions_clear_exception **************************************************
 
+   Clears the current exception pointer of the current thread.
 
-void throw_main_exception(void)
+*******************************************************************************/
+
+void exceptions_clear_exception(void)
 {
-       fprintf(stderr, "Exception in thread \"main\" ");
-       fflush(stderr);
+       threadobject *t;
 
-       throw_exception_exit_intern(false);
-}
+#if defined(ENABLE_THREADS)
+       t = THREADOBJECT;
+#else
+       t = NULL;
+#endif
 
+       /* Set the exception. */
 
-void throw_main_exception_exit(void)
-{
-       fprintf(stderr, "Exception in thread \"main\" ");
-       fflush(stderr);
+#if !defined(NDEBUG)
+       if (opt_DebugExceptions) {
+               printf("[exceptions_clear_exception: t=%p]\n", (void *) t);
+       }
+#endif
 
-       throw_exception_exit_intern(true);
+#if defined(ENABLE_THREADS)
+       t->_exceptionptr = NULL;
+#else
+       _no_threads_exceptionptr = NULL;
+#endif
 }
 
 
-void throw_cacao_exception_exit(const char *exception, const char *message, ...)
-{
-       s4 i;
-       char *tmp;
-       s4 len;
-       va_list ap;
-
-       len = strlen(exception);
-       tmp = MNEW(char, len + 1);
-       strncpy(tmp, exception, len);
-       tmp[len] = '\0';
+/* exceptions_get_and_clear_exception ******************************************
 
-       /* convert to classname */
+   Gets the exception pointer of the current thread and clears it.
+   This function may return NULL.
 
-       for (i = len - 1; i >= 0; i--)
-               if (tmp[i] == '/') tmp[i] = '.';
+*******************************************************************************/
 
-       fprintf(stderr, "Exception in thread \"main\" %s", tmp);
+java_handle_t *exceptions_get_and_clear_exception(void)
+{
+       java_handle_t *o;
 
-       MFREE(tmp, char, len);
+       /* Get the exception... */
 
-       if (strlen(message) > 0) {
-               fprintf(stderr, ": ");
+       o = exceptions_get_exception();
 
-               va_start(ap, message);
-               vfprintf(stderr, message, ap);
-               va_end(ap);
-       }
+       /* ...and clear the exception if it is set. */
 
-       fprintf(stderr, "\n");
-       fflush(stderr);
+       if (o != NULL)
+               exceptions_clear_exception();
 
-       /* good bye! */
+       /* return the exception */
 
-       exit(1);
+       return o;
 }
 
 
-/* exceptions_new_class ********************************************************
+/* exceptions_abort ************************************************************
 
-   Creates an exception object from the given class and initalizes it.
+   Prints exception to be thrown and aborts.
 
    IN:
-      class....class pointer
+      classname....class name
+      message......exception message
 
 *******************************************************************************/
 
-static java_objectheader *exceptions_new_class(classinfo *c)
+static void exceptions_abort(utf *classname, utf *message)
 {
-       java_objectheader *o;
+       log_println("exception thrown while VM is initializing: ");
 
-       o = native_new_and_init(c);
+       log_start();
+       utf_display_printable_ascii_classname(classname);
 
-       if (o == NULL)
-               return *exceptionptr;
+       if (message != NULL) {
+               log_print(": ");
+               utf_display_printable_ascii_classname(message);
+       }
 
-       return o;
+       log_finish();
+
+       vm_abort("Aborting...");
 }
 
 
@@ -330,171 +285,230 @@ static java_objectheader *exceptions_new_class(classinfo *c)
 
 *******************************************************************************/
 
-static java_objectheader *exceptions_new_utf(utf *classname)
+static java_handle_t *exceptions_new_utf(utf *classname)
 {
-       classinfo         *c;
-       java_objectheader *o;
+       classinfo     *c;
+       java_handle_t *o;
+
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
 
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
-               return *exceptionptr;
+               return exceptions_get_exception();
+
+       o = native_new_and_init(c);
 
-       o = exceptions_new_class(c);
+       if (o == NULL)
+               return exceptions_get_exception();
 
        return o;
 }
 
 
-/* exceptions_throw_class ******************************************************
+/* exceptions_throw_utf ********************************************************
 
-   Creates an exception object from the given class, initalizes and
+   Creates an exception object with the given name, initalizes and
    throws it.
 
    IN:
-      class....class pointer
+      classname....class name in UTF-8
 
 *******************************************************************************/
 
-static void exceptions_throw_class(classinfo *c)
+static void exceptions_throw_utf(utf *classname)
 {
-       java_objectheader *o;
+       java_handle_t *o;
 
-       o = exceptions_new_class(c);
+       o = exceptions_new_utf(classname);
 
        if (o == NULL)
                return;
 
-       *exceptionptr = o;
+       exceptions_set_exception(o);
 }
 
 
-/* exceptions_throw_utf ********************************************************
+/* exceptions_throw_utf_throwable **********************************************
 
-   Creates an exception object with the given name, initalizes and
-   throws it.
+   Creates an exception object with the given name and initalizes it
+   with the given java/lang/Throwable exception.
 
    IN:
       classname....class name in UTF-8
+         cause........the given Throwable
 
 *******************************************************************************/
 
-static void exceptions_throw_utf(utf *classname)
+static void exceptions_throw_utf_throwable(utf *classname,
+                                                                                  java_handle_t *cause)
 {
-       classinfo *c;
+       classinfo           *c;
+       java_handle_t       *o;
+       methodinfo          *m;
+       java_lang_Throwable *object;
+
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
+       object = (java_lang_Throwable *) cause;
 
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
                return;
 
-       exceptions_throw_class(c);
+       /* create object */
+
+       o = builtin_new(c);
+       
+       if (o == NULL)
+               return;
+
+       /* call initializer */
+
+       m = class_resolveclassmethod(c,
+                                                                utf_init,
+                                                                utf_java_lang_Throwable__void,
+                                                                NULL,
+                                                                true);
+                                                     
+       if (m == NULL)
+               return;
+
+       (void) vm_call_method(m, o, cause);
+
+       exceptions_set_exception(o);
 }
 
 
-/* exceptions_throw_utf_throwable **********************************************
+/* exceptions_throw_utf_exception **********************************************
 
    Creates an exception object with the given name and initalizes it
-   with the given java/lang/Throwable exception.
+   with the given java/lang/Exception exception.
 
    IN:
       classname....class name in UTF-8
-         cause........the given Throwable
+         exception....the given Exception
 
 *******************************************************************************/
 
-static void exceptions_throw_utf_throwable(utf *classname,
-                                                                                  java_objectheader *cause)
+static void exceptions_throw_utf_exception(utf *classname,
+                                                                                  java_handle_t *exception)
 {
-       java_objectheader *o;
-       classinfo         *c;
-   
+       classinfo     *c;
+       java_handle_t *o;
+       methodinfo    *m;
+
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
                return;
 
-       o = native_new_and_init_throwable(c, cause);
+       /* create object */
 
+       o = builtin_new(c);
+       
        if (o == NULL)
                return;
 
-       *exceptionptr = o;
+       /* call initializer */
+
+       m = class_resolveclassmethod(c,
+                                                                utf_init,
+                                                                utf_java_lang_Exception__V,
+                                                                NULL,
+                                                                true);
+                                                     
+       if (m == NULL)
+               return;
+
+       (void) vm_call_method(m, o, exception);
+
+       exceptions_set_exception(o);
 }
 
 
-/* exceptions_new_utf_javastring ***********************************************
+/* exceptions_throw_utf_cause **************************************************
 
    Creates an exception object with the given name and initalizes it
-   with the given java/lang/String message.
+   with the given java/lang/Throwable exception with initCause.
 
    IN:
       classname....class name in UTF-8
-         message......the message as a java.lang.String
-
-   RETURN VALUE:
-      an exception pointer (in any case -- either it is the newly created
-         exception, or an exception thrown while trying to create it).
+         cause........the given Throwable
 
 *******************************************************************************/
 
-static java_objectheader *exceptions_new_utf_javastring(utf *classname,
-                                                                                                               java_objectheader *message)
+static void exceptions_throw_utf_cause(utf *classname, java_handle_t *cause)
 {
-       java_objectheader *o;
-       classinfo         *c;
-   
+       classinfo           *c;
+       java_handle_t       *o;
+       methodinfo          *m;
+       java_lang_String    *s;
+       java_lang_Throwable *object;
+
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
+
+       object = (java_lang_Throwable *) cause;
+
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
-               return *exceptionptr;
+               return;
 
-       o = native_new_and_init_string(c, message);
+       /* create object */
 
+       o = builtin_new(c);
+       
        if (o == NULL)
-               return *exceptionptr;
-
-       return o;
-}
-
+               return;
 
-/* exceptions_new_class_utf ****************************************************
+       /* call initializer */
 
-   Creates an exception object of the given class and initalizes it.
+       m = class_resolveclassmethod(c,
+                                                                utf_init,
+                                                                utf_java_lang_String__void,
+                                                                NULL,
+                                                                true);
+                                                     
+       if (m == NULL)
+               return;
 
-   IN:
-      c..........class pointer
-      message....the message as UTF-8 string
+       LLNI_field_get_ref(object, detailMessage, s);
 
-*******************************************************************************/
+       (void) vm_call_method(m, o, s);
 
-static java_objectheader *exceptions_new_class_utf(classinfo *c, utf *message)
-{
-       java_objectheader *o;
-       java_objectheader *s;
+       /* call initCause */
 
-       s = javastring_new(message);
+       m = class_resolveclassmethod(c,
+                                                                utf_initCause,
+                                                                utf_java_lang_Throwable__java_lang_Throwable,
+                                                                NULL,
+                                                                true);
 
-       if (s == NULL)
-               return *exceptionptr;
+       if (m == NULL)
+               return;
 
-       o = native_new_and_init_string(c, s);
+       (void) vm_call_method(m, o, cause);
 
-       if (o == NULL)
-               return *exceptionptr;
-
-       return o;
+       exceptions_set_exception(o);
 }
 
 
-/* exceptions_new_utf_utf ******************************************************
+/* exceptions_new_utf_javastring ***********************************************
 
    Creates an exception object with the given name and initalizes it
-   with the given utf message.
+   with the given java/lang/String message.
 
    IN:
       classname....class name in UTF-8
-         message......the message as an utf *
+         message......the message as a java.lang.String
 
    RETURN VALUE:
       an exception pointer (in any case -- either it is the newly created
@@ -502,30 +516,37 @@ static java_objectheader *exceptions_new_class_utf(classinfo *c, utf *message)
 
 *******************************************************************************/
 
-static java_objectheader *exceptions_new_utf_utf(utf *classname, utf *message)
+static java_handle_t *exceptions_new_utf_javastring(utf *classname,
+                                                                                                       java_handle_t *message)
 {
-       classinfo         *c;
-       java_objectheader *o;
+       java_handle_t *o;
+       classinfo     *c;
+   
+       if (vm_initializing)
+               exceptions_abort(classname, NULL);
 
        c = load_class_bootstrap(classname);
 
        if (c == NULL)
-               return *exceptionptr;
+               return exceptions_get_exception();
 
-       o = exceptions_new_class_utf(c, message);
+       o = native_new_and_init_string(c, message);
+
+       if (o == NULL)
+               return exceptions_get_exception();
 
        return o;
 }
 
 
-/* new_exception_message *******************************************************
+/* exceptions_new_utf_utf ******************************************************
 
    Creates an exception object with the given name and initalizes it
-   with the given char message.
+   with the given utf message.
 
    IN:
       classname....class name in UTF-8
-         message......message in UTF-8
+         message......the message as an utf *
 
    RETURN VALUE:
       an exception pointer (in any case -- either it is the newly created
@@ -533,37 +554,31 @@ static java_objectheader *exceptions_new_utf_utf(utf *classname, utf *message)
 
 *******************************************************************************/
 
-static java_objectheader *new_exception_message(const char *classname,
-                                                                                               const char *message)
+static java_handle_t *exceptions_new_utf_utf(utf *classname, utf *message)
 {
-       java_objectheader *o;
-       java_objectheader *s;
-
-       s = javastring_new_from_utf_string(message);
+       classinfo     *c;
+       java_handle_t *s;
+       java_handle_t *o;
 
-       if (s == NULL)
-               return *exceptionptr;
-
-       o = exceptions_new_utf_javastring(classname, s);
+       if (vm_initializing)
+               exceptions_abort(classname, message);
 
-       return o;
-}
+       c = load_class_bootstrap(classname);
 
+       if (c == NULL)
+               return exceptions_get_exception();
 
-/* exceptions_throw_class_utf **************************************************
+       s = javastring_new(message);
 
-   Creates an exception object of the given class, initalizes and
-   throws it with the given utf message.
+       if (s == NULL)
+               return exceptions_get_exception();
 
-   IN:
-      c..........class pointer
-         message....the message as an UTF-8
+       o = native_new_and_init_string(c, s);
 
-*******************************************************************************/
+       if (o == NULL)
+               return exceptions_get_exception();
 
-static void exceptions_throw_class_utf(classinfo *c, utf *message)
-{
-       *exceptionptr = exceptions_new_class_utf(c, message);
+       return o;
 }
 
 
@@ -580,39 +595,11 @@ static void exceptions_throw_class_utf(classinfo *c, utf *message)
 
 static void exceptions_throw_utf_utf(utf *classname, utf *message)
 {
-       *exceptionptr = exceptions_new_utf_utf(classname, message);
-}
-
-
-/* new_exception_int ***********************************************************
-
-   Creates an exception object with the given name and initalizes it
-   with the given int value.
-
-   IN:
-      classname....class name in UTF-8
-         i............the integer
-
-   RETURN VALUE:
-      an exception pointer (in any case -- either it is the newly created
-         exception, or an exception thrown while trying to create it).
-
-*******************************************************************************/
-
-java_objectheader *new_exception_int(const char *classname, s4 i)
-{
-       java_objectheader *o;
-       classinfo         *c;
-   
-       if (!(c = load_class_bootstrap(utf_new_char(classname))))
-               return *exceptionptr;
-
-       o = native_new_and_init_int(c, i);
+       java_handle_t *o;
 
-       if (!o)
-               return *exceptionptr;
+       o = exceptions_new_utf_utf(classname, message);
 
-       return o;
+       exceptions_set_exception(o);
 }
 
 
@@ -622,9 +609,9 @@ java_objectheader *new_exception_int(const char *classname, s4 i)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_new_abstractmethoderror(void)
+java_handle_t *exceptions_new_abstractmethoderror(void)
 {
-       java_objectheader *o;
+       java_handle_t *o;
 
        o = exceptions_new_utf(utf_java_lang_AbstractMethodError);
 
@@ -639,11 +626,11 @@ java_objectheader *exceptions_new_abstractmethoderror(void)
 *******************************************************************************/
 
 #if defined(ENABLE_JAVAME_CLDC1_1)
-static java_objectheader *exceptions_new_error(utf *message)
+static java_handle_t *exceptions_new_error(utf *message)
 {
-       java_objectheader *o;
+       java_handle_t *o;
 
-       o = exceptions_new_class_utf(class_java_lang_Error, message);
+       o = exceptions_new_utf_utf(utf_java_lang_Error, message);
 
        return o;
 }
@@ -657,14 +644,15 @@ static java_objectheader *exceptions_new_error(utf *message)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_asm_new_abstractmethoderror(u1 *sp, u1 *ra)
+java_object_t *exceptions_asm_new_abstractmethoderror(u1 *sp, u1 *ra)
 {
-       stackframeinfo     sfi;
-       java_objectheader *e;
+       stackframeinfo_t  sfi;
+       java_handle_t    *e;
+       java_object_t    *o;
 
-       /* create the stackframeinfo (XPC is equal to RA) */
+       /* Fill and add a stackframeinfo (XPC is equal to RA). */
 
-       stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, ra);
+       stacktrace_stackframeinfo_add(&sfi, NULL, sp, ra, ra);
 
        /* create the exception */
 
@@ -674,11 +662,16 @@ java_objectheader *exceptions_asm_new_abstractmethoderror(u1 *sp, u1 *ra)
        e = exceptions_new_error(utf_java_lang_AbstractMethodError);
 #endif
 
-       /* remove the stackframeinfo */
+       /* Remove the stackframeinfo. */
 
-       stacktrace_remove_stackframeinfo(&sfi);
+       stacktrace_stackframeinfo_remove(&sfi);
 
-       return e;
+       /* unwrap the exception */
+       /* ATTENTION: do the this _after_ the stackframeinfo was removed */
+
+       o = LLNI_UNWRAP(e);
+
+       return o;
 }
 
 
@@ -688,9 +681,9 @@ java_objectheader *exceptions_asm_new_abstractmethoderror(u1 *sp, u1 *ra)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_new_arraystoreexception(void)
+java_handle_t *exceptions_new_arraystoreexception(void)
 {
-       java_objectheader *o;
+       java_handle_t *o;
 
        o = exceptions_new_utf(utf_java_lang_ArrayStoreException);
 
@@ -716,36 +709,13 @@ void exceptions_throw_abstractmethoderror(void)
    classloader.
 
    IN:
-      c............the class in which the error was found
+      c....the class in which the error was found
 
 *******************************************************************************/
 
 void exceptions_throw_classcircularityerror(classinfo *c)
 {
-       java_objectheader *o;
-       char              *msg;
-       s4                 msglen;
-
-       /* calculate message length */
-
-       msglen = utf_bytes(c->name) + strlen("0");
-
-       /* allocate a buffer */
-
-       msg = MNEW(char, msglen);
-
-       /* print message into allocated buffer */
-
-       utf_copy_classname(msg, c->name);
-
-       o = new_exception_message(utf_java_lang_ClassCircularityError, msg);
-
-       MFREE(msg, char, msglen);
-
-       if (o == NULL)
-               return;
-
-       *exceptionptr = o;
+       exceptions_throw_utf_utf(utf_java_lang_ClassCircularityError, c->name);
 }
 
 
@@ -761,10 +731,10 @@ void exceptions_throw_classcircularityerror(classinfo *c)
 
 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
 {
-       java_objectheader *o;
-       char              *msg;
-       s4                 msglen;
-       va_list            ap;
+       char    *msg;
+       s4       msglen;
+       va_list  ap;
+       utf     *u;
 
        /* calculate message length */
 
@@ -800,11 +770,15 @@ void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
        if (c != NULL)
                strcat(msg, ")");
 
-       o = new_exception_message(utf_java_lang_ClassFormatError, msg);
+       u = utf_new_char(msg);
+
+       /* free memory */
 
        MFREE(msg, char, msglen);
 
-       *exceptionptr = o;
+       /* throw exception */
+
+       exceptions_throw_utf_utf(utf_java_lang_ClassFormatError, u);
 }
 
 
@@ -819,10 +793,8 @@ void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
 *******************************************************************************/
 
 void exceptions_throw_classnotfoundexception(utf *name)
-{
-       /* we use class here, as this one is rather frequent */
-
-       exceptions_throw_class_utf(class_java_lang_ClassNotFoundException, name);
+{      
+       exceptions_throw_utf_utf(utf_java_lang_ClassNotFoundException, name);
 }
 
 
@@ -837,51 +809,54 @@ void exceptions_throw_classnotfoundexception(utf *name)
 
 void exceptions_throw_noclassdeffounderror(utf *name)
 {
-       exceptions_throw_class_utf(class_java_lang_NoClassDefFoundError, name);
+       exceptions_throw_utf_utf(utf_java_lang_NoClassDefFoundError, name);
 }
 
 
-/* classnotfoundexception_to_noclassdeffounderror ******************************
+/* exceptions_throw_noclassdeffounderror_cause *********************************
 
-   Check the *exceptionptr for a ClassNotFoundException. If it is one,
-   convert it to a NoClassDefFoundError.
+   Generates and throws a java.lang.NoClassDefFoundError with the
+   given cause.
 
 *******************************************************************************/
 
-void classnotfoundexception_to_noclassdeffounderror(void)
+void exceptions_throw_noclassdeffounderror_cause(java_handle_t *cause)
 {
-       java_objectheader   *xptr;
-       java_objectheader   *cause;
-       java_lang_Throwable *t;
-       java_lang_String    *s;
+       exceptions_throw_utf_cause(utf_java_lang_NoClassDefFoundError, cause);
+}
 
-       /* get the cause */
 
-       cause = *exceptionptr;
+/* exceptions_throw_noclassdeffounderror_wrong_name ****************************
 
-       /* convert ClassNotFoundException's to NoClassDefFoundError's */
+   Generates and throws a java.lang.NoClassDefFoundError with a
+   specific message:
 
-       if (builtin_instanceof(cause, class_java_lang_ClassNotFoundException)) {
-               /* clear exception, because we are calling jit code again */
+   IN:
+      name.........name of the class not found as a utf *
 
-               *exceptionptr = NULL;
+*******************************************************************************/
 
-               /* create new error */
+void exceptions_throw_noclassdeffounderror_wrong_name(classinfo *c, utf *name)
+{
+       char *msg;
+       s4    msglen;
+       utf  *u;
 
-               t = (java_lang_Throwable *) cause;
-               s = t->detailMessage;
+       msglen = utf_bytes(c->name) + strlen(" (wrong name: ") +
+               utf_bytes(name) + strlen(")") + strlen("0");
 
-               xptr = exceptions_new_utf_javastring(utf_java_lang_NoClassDefFoundError, s);
+       msg = MNEW(char, msglen);
 
-               /* we had an exception while creating the error */
+       utf_copy_classname(msg, c->name);
+       strcat(msg, " (wrong name: ");
+       utf_cat_classname(msg, name);
+       strcat(msg, ")");
 
-               if (*exceptionptr)
-                       return;
+       u = utf_new_char(msg);
 
-               /* set new exception */
+       MFREE(msg, char, msglen);
 
-               *exceptionptr = xptr;
-       }
+       exceptions_throw_noclassdeffounderror(u);
 }
 
 
@@ -895,7 +870,7 @@ void classnotfoundexception_to_noclassdeffounderror(void)
 
 *******************************************************************************/
 
-void exceptions_throw_exceptionininitializererror(java_objectheader *cause)
+void exceptions_throw_exceptionininitializererror(java_handle_t *cause)
 {
        exceptions_throw_utf_throwable(utf_java_lang_ExceptionInInitializerError,
                                                                   cause);
@@ -914,9 +889,9 @@ void exceptions_throw_exceptionininitializererror(java_objectheader *cause)
 
 void exceptions_throw_incompatibleclasschangeerror(classinfo *c, const char *message)
 {
-       java_objectheader *o;
-       char              *msg;
-       s4                 msglen;
+       char *msg;
+       s4    msglen;
+       utf  *u;
 
        /* calculate exception message length */
 
@@ -929,17 +904,15 @@ void exceptions_throw_incompatibleclasschangeerror(classinfo *c, const char *mes
        utf_copy_classname(msg, c->name);
        strcat(msg, message);
 
-       o = native_new_and_init_string(utf_java_lang_IncompatibleClassChangeError,
-                                                                  javastring_new_from_utf_string(msg));
+       u = utf_new_char(msg);
 
        /* free memory */
 
        MFREE(msg, char, msglen);
 
-       if (o == NULL)
-               return;
+       /* throw exception */
 
-       *exceptionptr = o;
+       exceptions_throw_utf_utf(utf_java_lang_IncompatibleClassChangeError, u);
 }
 
 
@@ -966,10 +939,10 @@ void exceptions_throw_instantiationerror(classinfo *c)
 
 void exceptions_throw_internalerror(const char *message, ...)
 {
-       java_objectheader *o;
-       va_list            ap;
-       char              *msg;
-       s4                 msglen;
+       va_list  ap;
+       char    *msg;
+       s4       msglen;
+       utf     *u;
 
        /* calculate exception message length */
 
@@ -987,18 +960,15 @@ void exceptions_throw_internalerror(const char *message, ...)
        vsprintf(msg, message, ap);
        va_end(ap);
 
-       /* create exception object */
-
-       o = new_exception_message(utf_java_lang_InternalError, msg);
+       u = utf_new_char(msg);
 
        /* free memory */
 
        MFREE(msg, char, msglen);
 
-       if (o == NULL)
-               return;
+       /* throw exception */
 
-       *exceptionptr = o;
+       exceptions_throw_utf_utf(utf_java_lang_InternalError, u);
 }
 
 
@@ -1015,39 +985,35 @@ void exceptions_throw_internalerror(const char *message, ...)
 
 void exceptions_throw_linkageerror(const char *message, classinfo *c)
 {
-       java_objectheader *o;
-       char              *msg;
-       s4                 msglen;
+       utf  *u;
+       char *msg;
+       int   len;
 
        /* calculate exception message length */
 
-       msglen = strlen(message) + 1;
+       len = strlen(message) + 1;
 
        if (c != NULL)
-               msglen += utf_bytes(c->name);
+               len += utf_bytes(c->name);
                
        /* allocate memory */
 
-       msg = MNEW(char, msglen);
+       msg = MNEW(char, len);
 
        /* generate message */
 
-       strcpy(msg,message);
+       strcpy(msg, message);
 
        if (c != NULL)
                utf_cat_classname(msg, c->name);
 
-       o = native_new_and_init_string(class_java_lang_LinkageError,
-                                                                  javastring_new_from_utf_string(msg));
+       u = utf_new_char(msg);
 
        /* free memory */
 
-       MFREE(msg, char, msglen);
+       MFREE(msg, char, len);
 
-       if (o == NULL)
-               return;
-
-       *exceptionptr = o;
+       exceptions_throw_utf_utf(utf_java_lang_LinkageError, u);
 }
 
 
@@ -1132,7 +1098,11 @@ void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
 
        MFREE(msg, char, msglen);
 
+#if defined(ENABLE_JAVASE)
        exceptions_throw_utf_utf(utf_java_lang_NoSuchMethodError, u);
+#else
+       exceptions_throw_utf_utf(utf_java_lang_Error, u);
+#endif
 }
 
 
@@ -1144,7 +1114,7 @@ void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
 
 void exceptions_throw_outofmemoryerror(void)
 {
-       exceptions_throw_class(class_java_lang_OutOfMemoryError);
+       exceptions_throw_utf(utf_java_lang_OutOfMemoryError);
 }
 
 
@@ -1160,7 +1130,11 @@ void exceptions_throw_outofmemoryerror(void)
 
 void exceptions_throw_unsatisfiedlinkerror(utf *name)
 {
+#if defined(ENABLE_JAVASE)
        exceptions_throw_utf_utf(utf_java_lang_UnsatisfiedLinkError, name);
+#else
+       exceptions_throw_utf_utf(utf_java_lang_Error, name);
+#endif
 }
 
 
@@ -1177,9 +1151,9 @@ void exceptions_throw_unsatisfiedlinkerror(utf *name)
 
 void exceptions_throw_unsupportedclassversionerror(classinfo *c, u4 ma, u4 mi)
 {
-       java_objectheader *o;
-       char              *msg;
-    s4                 msglen;
+       char *msg;
+    s4    msglen;
+       utf  *u;
 
        /* calculate exception message length */
 
@@ -1198,18 +1172,15 @@ void exceptions_throw_unsupportedclassversionerror(classinfo *c, u4 ma, u4 mi)
        sprintf(msg + strlen(msg), " (Unsupported major.minor version %d.%d)",
                        ma, mi);
 
-       /* create exception object */
-
-       o = new_exception_message(utf_java_lang_UnsupportedClassVersionError, msg);
+       u = utf_new_char(msg);
 
        /* free memory */
 
        MFREE(msg, char, msglen);
 
-       if (o == NULL)
-               return;
+       /* throw exception */
 
-       *exceptionptr = o;
+       exceptions_throw_utf_utf(utf_java_lang_UnsupportedClassVersionError, u);
 }
 
 
@@ -1225,10 +1196,10 @@ void exceptions_throw_unsupportedclassversionerror(classinfo *c, u4 ma, u4 mi)
 
 void exceptions_throw_verifyerror(methodinfo *m, const char *message, ...)
 {
-       java_objectheader *o;
-       va_list            ap;
-       char              *msg;
-       s4                 msglen;
+       va_list  ap;
+       char    *msg;
+       s4       msglen;
+       utf     *u;
 
        /* calculate exception message length */
 
@@ -1265,15 +1236,15 @@ void exceptions_throw_verifyerror(methodinfo *m, const char *message, ...)
        vsprintf(msg + strlen(msg), message, ap);
        va_end(ap);
 
-       /* create exception object */
-
-       o = new_exception_message(utf_java_lang_VerifyError, msg);
+       u = utf_new_char(msg);
 
        /* free memory */
 
        MFREE(msg, char, msglen);
 
-       *exceptionptr = o;
+       /* throw exception */
+
+       exceptions_throw_utf_utf(utf_java_lang_VerifyError, u);
 }
 
 
@@ -1291,18 +1262,18 @@ void exceptions_throw_verifyerror(methodinfo *m, const char *message, ...)
 
 *******************************************************************************/
 
-void exceptions_throw_verifyerror_for_stack(methodinfo *m,int type)
+void exceptions_throw_verifyerror_for_stack(methodinfo *m, int type)
 {
-       java_objectheader *o;
-       char              *msg;
-       s4                 msglen;
-       char              *typename;
+       char *msg;
+       s4    msglen;
+       char *typename;
+       utf  *u;
 
        /* calculate exception message length */
 
        msglen = 0;
 
-       if (m)
+       if (m != NULL)
                msglen = strlen("(class: ") + utf_bytes(m->class->name) +
                        strlen(", method: ") + utf_bytes(m->name) +
                        strlen(" signature: ") + utf_bytes(m->descriptor) +
@@ -1315,7 +1286,7 @@ void exceptions_throw_verifyerror_for_stack(methodinfo *m,int type)
 
        /* generate message */
 
-       if (m) {
+       if (m != NULL) {
                strcpy(msg, "(class: ");
                utf_cat_classname(msg, m->class->name);
                strcat(msg, ", method: ");
@@ -1328,7 +1299,8 @@ void exceptions_throw_verifyerror_for_stack(methodinfo *m,int type)
                msg[0] = 0;
        }
 
-       strcat(msg,"Expecting to find ");
+       strcat(msg, "Expecting to find ");
+
        switch (type) {
                case TYPE_INT: typename = "integer"; break;
                case TYPE_LNG: typename = "long"; break;
@@ -1338,18 +1310,19 @@ void exceptions_throw_verifyerror_for_stack(methodinfo *m,int type)
                case TYPE_RET: typename = "returnAddress"; break;
                default:       typename = "<INVALID>"; assert(0); break;
        }
+
        strcat(msg, typename);
        strcat(msg, " on stack");
 
-       /* create exception object */
-
-       o = new_exception_message(utf_java_lang_VerifyError, msg);
+       u = utf_new_char(msg);
 
        /* free memory */
 
        MFREE(msg, char, msglen);
 
-       *exceptionptr = o;
+       /* throw exception */
+
+       exceptions_throw_utf_utf(utf_java_lang_VerifyError, u);
 }
 
 
@@ -1359,14 +1332,12 @@ void exceptions_throw_verifyerror_for_stack(methodinfo *m,int type)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_new_arithmeticexception(void)
+java_handle_t *exceptions_new_arithmeticexception(void)
 {
-       java_objectheader *o;
+       java_handle_t *o;
 
-       o = new_exception_message(utf_java_lang_ArithmeticException, "/ by zero");
-
-       if (o == NULL)
-               return *exceptionptr;
+       o = exceptions_new_utf_utf(utf_java_lang_ArithmeticException,
+                                                          utf_division_by_zero);
 
        return o;
 }
@@ -1379,11 +1350,11 @@ java_objectheader *exceptions_new_arithmeticexception(void)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_new_arrayindexoutofboundsexception(s4 index)
+java_handle_t *exceptions_new_arrayindexoutofboundsexception(s4 index)
 {
-       java_objectheader *o;
-       methodinfo        *m;
-       java_objectheader *s;
+       java_handle_t *o;
+       methodinfo    *m;
+       java_handle_t *s;
 
        /* convert the index into a String, like Sun does */
 
@@ -1394,18 +1365,18 @@ java_objectheader *exceptions_new_arrayindexoutofboundsexception(s4 index)
                                                                 true);
 
        if (m == NULL)
-               return *exceptionptr;
+               return exceptions_get_exception();
 
        s = vm_call_method(m, NULL, index);
 
        if (s == NULL)
-               return *exceptionptr;
+               return exceptions_get_exception();
 
        o = exceptions_new_utf_javastring(utf_java_lang_ArrayIndexOutOfBoundsException,
                                                                          s);
 
        if (o == NULL)
-               return *exceptionptr;
+               return exceptions_get_exception();
 
        return o;
 }
@@ -1433,7 +1404,6 @@ void exceptions_throw_arrayindexoutofboundsexception(void)
 void exceptions_throw_arraystoreexception(void)
 {
        exceptions_throw_utf(utf_java_lang_ArrayStoreException);
-/*     e = native_new_and_init(class_java_lang_ArrayStoreException); */
 }
 
 
@@ -1443,20 +1413,17 @@ void exceptions_throw_arraystoreexception(void)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_new_classcastexception(java_objectheader *o)
+java_handle_t *exceptions_new_classcastexception(java_handle_t *o)
 {
-       java_objectheader *e;
-       utf               *classname;
-       java_lang_String  *s;
-
-       classname = o->vftbl->class->name;
+       java_handle_t *e;
+       classinfo     *c;
+       utf           *classname;
 
-       s = javastring_new(classname);
+       LLNI_class_get(o, c);
 
-       e = native_new_and_init_string(class_java_lang_ClassCastException, s);
+       classname = c->name;
 
-       if (e == NULL)
-               return *exceptionptr;
+       e = exceptions_new_utf_utf(utf_java_lang_ClassCastException, classname);
 
        return e;
 }
@@ -1481,11 +1448,9 @@ void exceptions_throw_clonenotsupportedexception(void)
 
 *******************************************************************************/
 
-void exceptions_throw_illegalaccessexception(classinfo *c)
+void exceptions_throw_illegalaccessexception(utf *message)
 {
-       /* XXX handle argument */
-
-       exceptions_throw_utf(utf_java_lang_IllegalAccessException);
+       exceptions_throw_utf_utf(utf_java_lang_IllegalAccessException, message);
 }
 
 
@@ -1541,17 +1506,17 @@ void exceptions_throw_interruptedexception(void)
 
 /* exceptions_throw_invocationtargetexception **********************************
 
-   Generates and throws a java.lang.InvocationTargetException for the
-   VM.
+   Generates and throws a java.lang.reflect.InvocationTargetException
+   for the VM.
 
    IN:
       cause......cause exception object
 
 *******************************************************************************/
 
-void exceptions_throw_invocationtargetexception(java_objectheader *cause)
+void exceptions_throw_invocationtargetexception(java_handle_t *cause)
 {
-       exceptions_throw_utf_throwable(utf_java_lang_InvocationTargetException,
+       exceptions_throw_utf_throwable(utf_java_lang_reflect_InvocationTargetException,
                                                                   cause);
 }
 
@@ -1575,11 +1540,11 @@ void exceptions_throw_negativearraysizeexception(void)
 
 *******************************************************************************/
 
-java_objectheader *exceptions_new_nullpointerexception(void)
+java_handle_t *exceptions_new_nullpointerexception(void)
 {
-       java_objectheader *o;
+       java_handle_t *o;
 
-       o = exceptions_new_class(class_java_lang_NullPointerException);
+       o = exceptions_new_utf(utf_java_lang_NullPointerException);
 
        return o;
 }
@@ -1594,7 +1559,20 @@ java_objectheader *exceptions_new_nullpointerexception(void)
 
 void exceptions_throw_nullpointerexception(void)
 {
-       exceptions_throw_class(class_java_lang_NullPointerException);
+       exceptions_throw_utf(utf_java_lang_NullPointerException);
+}
+
+
+/* exceptions_throw_privilegedactionexception **********************************
+
+   Generates and throws a java.security.PrivilegedActionException.
+
+*******************************************************************************/
+
+void exceptions_throw_privilegedactionexception(java_handle_t *exception)
+{
+       exceptions_throw_utf_exception(utf_java_security_PrivilegedActionException,
+                                                                  exception);
 }
 
 
@@ -1611,75 +1589,102 @@ void exceptions_throw_stringindexoutofboundsexception(void)
 }
 
 
-/* exceptions_get_exception ****************************************************
+/* exceptions_classnotfoundexception_to_noclassdeffounderror *******************
 
-   Returns the current exception pointer of the current thread.
+   Check the exception for a ClassNotFoundException. If it is one,
+   convert it to a NoClassDefFoundError.
 
 *******************************************************************************/
 
-java_objectheader *exceptions_get_exception(void)
+void exceptions_classnotfoundexception_to_noclassdeffounderror(void)
 {
-       /* return the exception */
+       classinfo           *c;
+       java_handle_t       *o;
+       java_handle_t       *cause;
+       java_lang_Throwable *object;
+       java_lang_String    *s;
 
-       return *exceptionptr;
-}
+       /* Load java/lang/ClassNotFoundException for the instanceof
+          check. */
 
+       c = load_class_bootstrap(utf_java_lang_ClassNotFoundException);
 
-/* exceptions_set_exception ****************************************************
+       if (c == NULL)
+               return;
 
-   Sets the exception pointer of the current thread.
+       /* Get the cause. */
 
-*******************************************************************************/
+       cause = exceptions_get_exception();
 
-void exceptions_set_exception(java_objectheader *o)
-{
-       /* set the exception */
+       /* Convert ClassNotFoundException's to NoClassDefFoundError's. */
 
-       *exceptionptr = o;
-}
+       if (builtin_instanceof(cause, c)) {
+               /* clear exception, because we are calling jit code again */
 
+               exceptions_clear_exception();
 
-/* exceptions_clear_exception **************************************************
+               /* create new error */
 
-   Clears the current exception pointer of the current thread.
+               object = (java_lang_Throwable *) cause;
+               LLNI_field_get_ref(object, detailMessage, s);
 
-*******************************************************************************/
+               o = exceptions_new_utf_javastring(utf_java_lang_NoClassDefFoundError,
+                                                                                 (java_handle_t *) s);
 
-void exceptions_clear_exception(void)
-{
-       /* and clear the exception */
+               /* we had an exception while creating the error */
 
-       *exceptionptr = NULL;
+               if (exceptions_get_exception())
+                       return;
+
+               /* set new exception */
+
+               exceptions_set_exception(o);
+       }
 }
 
 
-/* exceptions_get_and_clear_exception ******************************************
+/* exceptions_fillinstacktrace *************************************************
 
-   Gets the exception pointer of the current thread and clears it.
-   This function may return NULL.
+   Calls the fillInStackTrace-method of the currently thrown
+   exception.
 
 *******************************************************************************/
 
-java_objectheader *exceptions_get_and_clear_exception(void)
+java_handle_t *exceptions_fillinstacktrace(void)
 {
-       java_objectheader **p;
-       java_objectheader  *e;
+       java_handle_t *o;
+       classinfo     *c;
+       methodinfo    *m;
 
-       /* get the pointer of the exception pointer */
+       /* get exception */
 
-       p = exceptionptr;
+       o = exceptions_get_and_clear_exception();
 
-       /* get the exception */
+       assert(o);
 
-       e = *p;
+       /* resolve methodinfo pointer from exception object */
 
-       /* and clear the exception */
+       LLNI_class_get(o, c);
 
-       *p = NULL;
+#if defined(ENABLE_JAVASE)
+       m = class_resolvemethod(c,
+                                                       utf_fillInStackTrace,
+                                                       utf_void__java_lang_Throwable);
+#elif defined(ENABLE_JAVAME_CLDC1_1)
+       m = class_resolvemethod(c,
+                                                       utf_fillInStackTrace,
+                                                       utf_void__void);
+#else
+#error IMPLEMENT ME!
+#endif
 
-       /* return the exception */
+       /* call function */
 
-       return e;
+       (void) vm_call_method(m, o);
+
+       /* return exception object */
+
+       return o;
 }
 
 
@@ -1702,24 +1707,42 @@ java_objectheader *exceptions_get_and_clear_exception(void)
 *******************************************************************************/
 
 #if defined(ENABLE_JIT)
-u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp)
+u1 *exceptions_handle_exception(java_object_t *xptro, u1 *xpc, u1 *pv, u1 *sp)
 {
+       stackframeinfo_t       sfi;
+       java_handle_t         *xptr;
        methodinfo            *m;
        codeinfo              *code;
-       s4                     issync;
        dseg_exception_entry  *ex;
        s4                     exceptiontablelength;
        s4                     i;
        classref_or_classinfo  cr;
        classinfo             *c;
 #if defined(ENABLE_THREADS)
-       java_objectheader     *o;
+       java_object_t         *o;
 #endif
+       u1                    *result;
+
+#ifdef __S390__
+       /* Addresses are 31 bit integers */
+#      define ADDR_MASK(x) (u1 *)((u4)(x) & 0x7FFFFFFF)
+#else
+#      define ADDR_MASK(x) (x)
+#endif
+
+       xptr = LLNI_WRAP(xptro);
+       xpc  = ADDR_MASK(xpc);
+
+       /* Fill and add a stackframeinfo (XPC is equal to RA). */
 
-       /* get info from the method header */
+       stacktrace_stackframeinfo_add(&sfi, pv, sp, xpc, xpc);
+
+       result = NULL;
+
+       /* Get the codeinfo for the current method. */
+
+       code = code_get_codeinfo_for_pv(pv);
 
-       code                 = *((codeinfo **)            (pv + CodeinfoPointer));
-       issync               = *((s4 *)                   (pv + IsSync));
        ex                   =   (dseg_exception_entry *) (pv + ExTableStart);
        exceptiontablelength = *((s4 *)                   (pv + ExTableSize));
 
@@ -1731,8 +1754,12 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
 #if !defined(NDEBUG)
        /* print exception trace */
 
-       if (opt_verbose || opt_verbosecall || opt_verboseexception)
-               builtin_trace_exception(xptr, m, xpc, 1);
+       if (opt_TraceExceptions)
+               trace_exception(LLNI_DIRECT(xptr), m, xpc);
+
+# if defined(ENABLE_VMLOG)
+       vmlog_cacao_throw(xptr);
+# endif
 #endif
 
        for (i = 0; i < exceptiontablelength; i++) {
@@ -1745,12 +1772,14 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
                   special case of asm_vm_call_method.  So, just return the
                   proper exception handler. */
 
-               if ((ex->startpc == NULL) && (ex->endpc == NULL))
-                       return (u1 *) (ptrint) &asm_vm_call_method_exception_handler;
+               if ((ex->startpc == NULL) && (ex->endpc == NULL)) {
+                       result = (u1 *) (ptrint) &asm_vm_call_method_exception_handler;
+                       goto exceptions_handle_exception_return;
+               }
 
                /* is the xpc is the current catch range */
 
-               if ((ex->startpc <= xpc) && (xpc < ex->endpc)) {
+               if ((ADDR_MASK(ex->startpc) <= xpc) && (xpc < ADDR_MASK(ex->endpc))) {
                        cr = ex->catchtype;
 
                        /* NULL catches everything */
@@ -1759,13 +1788,18 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
 #if !defined(NDEBUG)
                                /* Print stacktrace of exception when caught. */
 
-                               if (opt_verboseexception) {
+# if defined(ENABLE_VMLOG)
+                               vmlog_cacao_catch(xptr);
+# endif
+
+                               if (opt_TraceExceptions) {
                                        exceptions_print_exception(xptr);
                                        stacktrace_print_trace(xptr);
                                }
 #endif
 
-                               return ex->handlerpc;
+                               result = ex->handlerpc;
+                               goto exceptions_handle_exception_return;
                        }
 
                        /* resolve or load/link the exception class */
@@ -1784,7 +1818,7 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
 
                                if (c == NULL) {
                                        /* Exception resolving the exception class, argh! */
-                                       return NULL;
+                                       goto exceptions_handle_exception_return;
                                }
 
                                /* Ok, we resolved it. Enter it in the table, so we don't */
@@ -1800,14 +1834,14 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
                                        /* use the methods' classloader */
                                        if (!load_class_from_classloader(c->name,
                                                                                                         m->class->classloader))
-                                               return NULL;
+                                               goto exceptions_handle_exception_return;
 
                                /* XXX I think, if it is not linked, we can be sure that     */
                                /* the exception object is no (indirect) instance of it, no? */
                                /* -Edwin                                                    */
                                if (!(c->state & CLASS_LINKED))
                                        if (!link_class(c))
-                                               return NULL;
+                                               goto exceptions_handle_exception_return;
                        }
 
                        /* is the thrown exception an instance of the catch class? */
@@ -1816,29 +1850,34 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
 #if !defined(NDEBUG)
                                /* Print stacktrace of exception when caught. */
 
-                               if (opt_verboseexception) {
+# if defined(ENABLE_VMLOG)
+                               vmlog_cacao_catch(xptr);
+# endif
+
+                               if (opt_TraceExceptions) {
                                        exceptions_print_exception(xptr);
                                        stacktrace_print_trace(xptr);
                                }
 #endif
 
-                               return ex->handlerpc;
+                               result = ex->handlerpc;
+                               goto exceptions_handle_exception_return;
                        }
                }
        }
 
 #if defined(ENABLE_THREADS)
-       /* is this method synchronized? */
+       /* Is this method realization synchronized? */
 
-       if (issync) {
-               /* get synchronization object */
+       if (code_is_synchronized(code)) {
+               /* Get synchronization object. */
 
-# if defined(__MIPS__) && (SIZEOF_VOID_P == 4)
+# if defined(__ARM__) || (defined(__MIPS__) && (SIZEOF_VOID_P == 4)) || defined(__I386__) || defined(__S390__) || defined(__POWERPC__)
                /* XXX change this if we ever want to use 4-byte stackslots */
-               o = *((java_objectheader **) (sp + issync - 8));
+               o = *((java_object_t **) (sp + code->synchronizedoffset - 8));
 # else
-               o = *((java_objectheader **) (sp + issync - SIZEOF_VOID_P));
-#endif
+               o = *((java_object_t **) (sp + code->synchronizedoffset - SIZEOF_VOID_P));
+# endif
 
                assert(o != NULL);
 
@@ -1848,7 +1887,38 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
 
        /* none of the exceptions catch this one */
 
-       return NULL;
+#if !defined(NDEBUG)
+# if defined(ENABLE_VMLOG)
+       vmlog_cacao_unwnd_method(m);
+# endif
+
+# if defined(ENABLE_DEBUG_FILTER)
+       if (show_filters_test_verbosecall_exit(m)) {
+# endif
+
+       /* outdent the log message */
+
+       if (opt_verbosecall) {
+               if (TRACEJAVACALLINDENT)
+                       TRACEJAVACALLINDENT--;
+               else
+                       log_text("exceptions_handle_exception: WARNING: unmatched unindent");
+       }
+
+# if defined(ENABLE_DEBUG_FILTER)
+       }
+# endif
+#endif /* !defined(NDEBUG) */
+
+       result = NULL;
+
+exceptions_handle_exception_return:
+
+       /* Remove the stackframeinfo. */
+
+       stacktrace_stackframeinfo_remove(&sfi);
+
+       return result;
 }
 #endif /* defined(ENABLE_JIT) */
 
@@ -1860,12 +1930,14 @@ u1 *exceptions_handle_exception(java_objectheader *xptr, u1 *xpc, u1 *pv, u1 *sp
 
 *******************************************************************************/
 
-void exceptions_print_exception(java_objectheader *xptr)
+void exceptions_print_exception(java_handle_t *xptr)
 {
        java_lang_Throwable   *t;
 #if defined(ENABLE_JAVASE)
        java_lang_Throwable   *cause;
 #endif
+       java_lang_String      *s;
+       classinfo             *c;
        utf                   *u;
 
        t = (java_lang_Throwable *) xptr;
@@ -1876,15 +1948,18 @@ void exceptions_print_exception(java_objectheader *xptr)
        }
 
 #if defined(ENABLE_JAVASE)
-       cause = t->cause;
+       LLNI_field_get_ref(t, cause, cause);
 #endif
 
        /* print the root exception */
 
-       utf_display_printable_ascii_classname(t->header.vftbl->class->name);
+       LLNI_class_get(t, c);
+       utf_display_printable_ascii_classname(c->name);
 
-       if (t->detailMessage != NULL) {
-               u = javastring_toutf(t->detailMessage, false);
+       LLNI_field_get_ref(t, detailMessage, s);
+
+       if (s != NULL) {
+               u = javastring_toutf((java_handle_t *) s, false);
 
                printf(": ");
                utf_display_printable_ascii(u);
@@ -1897,10 +1972,14 @@ void exceptions_print_exception(java_objectheader *xptr)
 
        if ((cause != NULL) && (cause != t)) {
                printf("Caused by: ");
-               utf_display_printable_ascii_classname(cause->header.vftbl->class->name);
+               
+               LLNI_class_get(cause, c);
+               utf_display_printable_ascii_classname(c->name);
 
-               if (cause->detailMessage) {
-                       u = javastring_toutf(cause->detailMessage, false);
+               LLNI_field_get_ref(cause, detailMessage, s);
+
+               if (s != NULL) {
+                       u = javastring_toutf((java_handle_t *) s, false);
 
                        printf(": ");
                        utf_display_printable_ascii(u);
@@ -1921,11 +2000,83 @@ void exceptions_print_exception(java_objectheader *xptr)
 
 void exceptions_print_current_exception(void)
 {
-       java_objectheader *xptr;
+       java_handle_t *o;
+
+       o = exceptions_get_exception();
+
+       exceptions_print_exception(o);
+}
+
+
+/* exceptions_print_stacktrace *************************************************
+
+   Prints a pending exception with Throwable.printStackTrace().  If
+   there happens an exception during printStackTrace(), we print the
+   thrown exception and the original one.
+
+   NOTE: This function calls Java code.
+
+*******************************************************************************/
+
+void exceptions_print_stacktrace(void)
+{
+       java_handle_t *oxptr;
+       java_handle_t *xptr;
+       classinfo     *c;
+       methodinfo    *m;
+
+       /* get original exception */
+
+       oxptr = exceptions_get_and_clear_exception();
+
+       if (oxptr == NULL)
+               vm_abort("exceptions_print_stacktrace: no exception thrown");
+
+       /* clear exception, because we are calling jit code again */
+
+       LLNI_class_get(oxptr, c);
+
+       /* find the printStackTrace() method */
+
+       m = class_resolveclassmethod(c,
+                                                                utf_printStackTrace,
+                                                                utf_void__void,
+                                                                class_java_lang_Object,
+                                                                false);
+
+       if (m == NULL)
+               vm_abort("exceptions_print_stacktrace: printStackTrace()V not found");
 
-       xptr = *exceptionptr;
+       /* print compatibility message */
 
-       exceptions_print_exception(xptr);
+       fprintf(stderr, "Exception in thread \"main\" ");
+
+       /* print the stacktrace */
+
+       (void) vm_call_method(m, oxptr);
+
+       /* This normally means, we are EXTREMLY out of memory or
+          have a serious problem while printStackTrace. But may
+          be another exception, so print it. */
+
+       xptr = exceptions_get_exception();
+
+       if (xptr != NULL) {
+               fprintf(stderr, "Exception while printStackTrace(): ");
+
+               /* now print original exception */
+
+               exceptions_print_exception(xptr);
+               stacktrace_print_trace(xptr);
+
+               /* now print original exception */
+
+               fprintf(stderr, "Original exception was: ");
+               exceptions_print_exception(oxptr);
+               stacktrace_print_trace(oxptr);
+       }
+
+       fflush(stderr);
 }