* Merged in twisti-branch.
[cacao.git] / src / vm / jit / stacktrace.c
index 35597f2b0ba0fcfc3388a63a1ab8e77167fd40cf..4052676e1e8a5eaa7a546f9d30e33b5861a5229e 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/jit/stacktrace.c - machine independent stacktrace system
 
-   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
    J. Wenninger, Institut f. Computersprachen - TU Wien
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Contact: cacao@cacaojvm.org
-
-   Authors: Joseph Wenninger
-            Christian Thalinger
-            Edwin Steiner
-
-   $Id: stacktrace.c 5941 2006-11-09 10:23:04Z twisti $
+   $Id: stacktrace.c 7584 2007-03-27 18:17:27Z tbfg $
 
 */
 
 
 #include "mm/gc-common.h"
 #include "mm/memory.h"
-#include "native/native.h"
+
+#include "vm/jit/stacktrace.h"
 
 #include "vm/global.h"                   /* required here for native includes */
-#include "native/include/java_lang_ClassLoader.h"
+#include "native/jni.h"
 #include "native/include/java_lang_Throwable.h"
-#include "native/include/java_lang_VMThrowable.h"
+
+#if defined(WITH_CLASSPATH_GNU)
+# include "native/include/java_lang_VMThrowable.h"
+#endif
 
 #if defined(ENABLE_THREADS)
 # include "threads/native/threads.h"
 #endif
 
 #include "toolbox/logging.h"
+
 #include "vm/builtin.h"
-#include "vm/class.h"
+#include "vm/cycles-stats.h"
 #include "vm/exceptions.h"
-#include "vm/loader.h"
-#include "vm/options.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
+
 #include "vm/jit/asmpart.h"
 #include "vm/jit/codegen-common.h"
 #include "vm/jit/methodheader.h"
-#include "vm/cycles-stats.h"
-
 
-/* linenumbertable_entry ******************************************************/
+#include "vmcore/class.h"
+#include "vmcore/loader.h"
+#include "vmcore/options.h"
 
-/* Keep the type of line the same as the pointer type, otherwise we
-   run into alignment troubles (like on MIPS64). */
-
-typedef struct linenumbertable_entry linenumbertable_entry;
-
-struct linenumbertable_entry {
-       ptrint  line;               /* NOTE: see doc/inlining_stacktrace.txt for  */
-       u1     *pc;                 /*       special meanings of line and pc.     */
-};
 
 /* global variables ***********************************************************/
-
 #if !defined(ENABLE_THREADS)
 stackframeinfo *_no_threads_stackframeinfo = NULL;
 #endif
@@ -207,7 +195,7 @@ void stacktrace_create_extern_stackframeinfo(stackframeinfo *sfi, u1 *pv,
                                                                                         u1 *sp, u1 *ra, u1 *xpc)
 {
        stackframeinfo **psfi;
-#if !defined(__I386__) && !defined(__X86_64__)
+#if !defined(__I386__) && !defined(__X86_64__) && !defined(__S390__)
        bool             isleafmethod;
 #endif
 #if defined(ENABLE_JIT)
@@ -229,7 +217,11 @@ void stacktrace_create_extern_stackframeinfo(stackframeinfo *sfi, u1 *pv,
 #endif
                        {
 #if defined(ENABLE_JIT)
+# if defined(__SPARC_64__)
+                               pv = md_get_pv_from_stackframe(sp);
+# else
                                pv = md_codegen_get_pv_from_pc(ra);
+# endif
 #endif
                        }
        }
@@ -240,9 +232,11 @@ void stacktrace_create_extern_stackframeinfo(stackframeinfo *sfi, u1 *pv,
 
        if (!opt_intrp) {
 # endif
-# if defined(__I386__) || defined(__X86_64__)
+# if defined(__I386__) || defined(__X86_64__) || defined(__S390__)
                /* On i386 and x86_64 we always have to get the return address
                   from the stack. */
+               /* On S390 we use REG_RA as REG_ITMP3, so we have always to get
+                  the RA from stack. */
 
                framesize = *((u4 *) (pv + FrameSize));
 
@@ -359,7 +353,7 @@ java_objectheader *stacktrace_inline_arithmeticexception(u1 *pv, u1 *sp,
 
        /* create exception */
 
-       o = new_arithmeticexception();
+       o = exceptions_new_arithmeticexception();
 
        /* remove stackframeinfo */
 
@@ -390,7 +384,7 @@ java_objectheader *stacktrace_inline_arrayindexoutofboundsexception(u1 *pv,
 
        /* create exception */
 
-       o = new_arrayindexoutofboundsexception(index);
+       o = exceptions_new_arrayindexoutofboundsexception(index);
 
        /* remove stackframeinfo */
 
@@ -514,9 +508,17 @@ java_objectheader *stacktrace_inline_fillInStackTrace(u1 *pv, u1 *sp, u1 *ra,
 
        /* resolve methodinfo pointer from exception object */
 
+#if defined(ENABLE_JAVASE)
        m = class_resolvemethod(o->vftbl->class,
                                                        utf_fillInStackTrace,
                                                        utf_void__java_lang_Throwable);
+#elif defined(ENABLE_JAVAME_CLDC1_1)
+       m = class_resolvemethod(o->vftbl->class,
+                                                       utf_fillInStackTrace,
+                                                       utf_void__void);
+#else
+#error IMPLEMENT ME!
+#endif
 
        /* call function */
 
@@ -548,7 +550,7 @@ java_objectheader *stacktrace_hardware_arithmeticexception(u1 *pv, u1 *sp,
 
        /* create exception */
 
-       o = new_arithmeticexception();
+       o = exceptions_new_arithmeticexception();
 
        /* remove stackframeinfo */
 
@@ -622,94 +624,6 @@ static void stacktrace_add_entry(stacktracebuffer *stb, methodinfo *m, u2 line)
 }
 
 
-/* stacktrace_add_method_intern ************************************************
-
-   This function is used by stacktrace_add_method to search the line number
-   table for the line corresponding to a given pc. The function recurses for
-   inlined methods.
-
-*******************************************************************************/
-
-static bool stacktrace_add_method_intern(stacktracebuffer *stb, 
-                                                                                methodinfo *m, 
-                                                                                linenumbertable_entry *lntentry,
-                                                                                ptrint lntsize,
-                                                                                u1 *pc)
-{
-       linenumbertable_entry *lntinline;   /* special entry for inlined method */
-
-       assert(stb);
-       assert(lntentry);
-
-       /* Find the line number for the specified PC (going backwards
-          in the linenumber table). The linenumber table size is zero
-          in native stubs. */
-
-       for (; lntsize > 0; lntsize--, lntentry--) {
-
-               /* did we reach the current line? */
-
-               /* Note: In case of inlining this may actually compare the pc
-                  against a methodinfo *, yielding a non-sensical
-                  result. This is no problem, however, as we ignore such
-                  entries in the switch below. This way we optimize for the
-                  common case (ie. a real pc in lntentry->pc). */
-
-               if (pc >= lntentry->pc) {
-
-                       /* check for special inline entries (see
-                          doc/inlining_stacktrace.txt for details */
-
-                       if ((s4)lntentry->line < 0) {
-                               switch (lntentry->line) {
-                                       case -1: 
-                                               /* begin of inlined method (ie. INLINE_END
-                                                  instruction) */
-
-                                               lntinline = --lntentry;/* get entry with methodinfo * */
-                                               lntentry--;            /* skip the special entry      */
-                                               lntsize -= 2;
-
-                                               /* search inside the inlined method */
-                                               if (stacktrace_add_method_intern(
-                                                                       stb, 
-                                                                       (methodinfo*) lntinline->pc,
-                                                                       lntentry,
-                                                                       lntsize,
-                                                                       pc))
-                                               {
-                                                       /* the inlined method contained the pc */
-                                                       assert(lntinline->line <= -3);
-                                                       stacktrace_add_entry(stb, m, (-3) - lntinline->line);
-                                                       return true;
-                                               }
-                                               /* pc was not in inlined method, continue
-                                                  search.  Entries inside the inlined method
-                                                  will be skipped because their lntentry->pc
-                                                  is higher than pc.  */
-                                               break;
-
-                                       case -2: 
-                                               /* end of inlined method */
-                                               return false;
-
-                                       /* default: is only reached for an -3-line entry
-                                          after a skipped -2 entry. We can safely ignore
-                                          it and continue searching.  */
-                               }
-                       }
-                       else {
-                               /* found a normal entry */
-                               stacktrace_add_entry(stb, m, lntentry->line);
-                               return true;
-                       }
-               }
-       }
-
-       /* not found */
-       return false;
-}
-
 /* stacktrace_add_method *******************************************************
 
    Add stacktrace entries[1] for the given method to the stacktrace buffer.
@@ -731,20 +645,8 @@ static bool stacktrace_add_method_intern(stacktracebuffer *stb,
 static bool stacktrace_add_method(stacktracebuffer *stb, methodinfo *m, u1 *pv,
                                                                  u1 *pc)
 {
-       ptrint                 lntsize;     /* size of line number table          */
-       u1                    *lntstart;    /* start of line number table         */
-       linenumbertable_entry *lntentry;    /* points to last entry in the table  */
-       codeinfo              *code;        /* compiled realization of method     */
-
-       /* get size of line number table */
-
-       lntsize  = *((ptrint *) (pv + LineNumberTableSize));
-       lntstart = *((u1 **)    (pv + LineNumberTableStart));
-
-       /* Subtract the size of the line number entry of the structure,
-          since the line number table start points to the pc. */
-
-       lntentry = (linenumbertable_entry *) (lntstart - SIZEOF_VOID_P);
+       codeinfo *code;                     /* compiled realization of method     */
+       s4        linenumber;
 
        /* find the realization of the method the pc is in */
 
@@ -752,12 +654,11 @@ static bool stacktrace_add_method(stacktracebuffer *stb, methodinfo *m, u1 *pv,
 
        /* search the line number table */
 
-       if (stacktrace_add_method_intern(stb, m, lntentry, lntsize, pc))
-               return true;
+       linenumber = dseg_get_linenumber_from_pc(&m, pv, pc);
 
-       /* If we get here, just add the entry with line number 0. */
+       /* now add a new entry to the staktrace */
 
-       stacktrace_add_entry(stb, m, 0);
+       stacktrace_add_entry(stb, m, linenumber);
 
        return true;
 }
@@ -937,6 +838,8 @@ stacktracebuffer *stacktrace_create(threadobject* thread)
 
 #if defined(__I386__) || defined (__X86_64__)
                                        sp += framesize + SIZEOF_VOID_P;
+#elif defined(__SPARC_64__)
+                                       sp = md_get_framepointer(sp);
 #else
                                        sp += framesize;
 #endif
@@ -1017,7 +920,12 @@ stacktracebuffer *stacktrace_create(threadobject* thread)
 #endif
                                {
 #if defined(ENABLE_JIT)
+# if defined(__SPARC_64__)
+                                       sp = md_get_framepointer(sp);
+                                       pv = md_get_pv_from_stackframe(sp);
+# else
                                        pv = md_codegen_get_pv_from_pc(ra);
+# endif
 #endif
                                }
 
@@ -1035,8 +943,10 @@ stacktracebuffer *stacktrace_create(threadobject* thread)
                        else
 #endif
                                {
-#if defined(__I386__) || defined (__X86_64__)
+#if defined(__I386__) || defined (__X86_64__) || defined (__M68K__)
                                        sp += framesize + SIZEOF_VOID_P;
+#elif defined(__SPARC_64__)
+                                       /* already has the new sp */
 #else
                                        sp += framesize;
 #endif
@@ -1204,6 +1114,7 @@ return_NULL:
 
 *******************************************************************************/
 
+#if defined(ENABLE_JAVASE)
 classinfo *stacktrace_getCurrentClass(void)
 {
        stacktracebuffer  *stb;
@@ -1253,6 +1164,7 @@ return_NULL:
 
        return NULL;
 }
+#endif /* ENABLE_JAVASE */
 
 
 /* stacktrace_getStack *********************************************************
@@ -1265,17 +1177,18 @@ return_NULL:
 
 *******************************************************************************/
 
+#if defined(ENABLE_JAVASE)
 java_objectarray *stacktrace_getStack(void)
 {
-       stacktracebuffer *stb;
-       stacktrace_entry *ste;
-       java_objectarray *oa;
-       java_objectarray *classes;
-       java_objectarray *methodnames;
-       classinfo        *c;
-       java_lang_String *str;
-       s4                i;
-       s4                dumpsize;
+       stacktracebuffer  *stb;
+       stacktrace_entry  *ste;
+       java_objectarray  *oa;
+       java_objectarray  *classes;
+       java_objectarray  *methodnames;
+       classinfo         *c;
+       java_objectheader *string;
+       s4                 i;
+       s4                 dumpsize;
        CYCLES_STATS_DECLARE_AND_START
 
        /* mark start of dump memory area */
@@ -1285,7 +1198,8 @@ java_objectarray *stacktrace_getStack(void)
        /* create a stacktrace for the current thread */
 
        stb = stacktrace_create(THREADOBJECT);
-       if (!stb)
+
+       if (stb == NULL)
                goto return_NULL;
 
        /* get the first stacktrace entry */
@@ -1296,17 +1210,17 @@ java_objectarray *stacktrace_getStack(void)
 
        oa = builtin_anewarray(2, arrayclass_java_lang_Object);
 
-       if (!oa)
+       if (oa == NULL)
                goto return_NULL;
 
        classes = builtin_anewarray(stb->used, class_java_lang_Class);
 
-       if (!classes)
+       if (classes == NULL)
                goto return_NULL;
 
        methodnames = builtin_anewarray(stb->used, class_java_lang_String);
 
-       if (!methodnames)
+       if (methodnames == NULL)
                goto return_NULL;
 
        /* set up the 2-dimensional array */
@@ -1320,12 +1234,13 @@ java_objectarray *stacktrace_getStack(void)
                c = ste->method->class;
 
                classes->data[i] = (java_objectheader *) c;
-               str = javastring_new(ste->method->name);
 
-               if (!str)
+               string = javastring_new(ste->method->name);
+
+               if (string == NULL)
                        goto return_NULL;
 
-               methodnames->data[i] = (java_objectheader *) str;
+               methodnames->data[i] = string;
        }
 
        /* return the 2-dimensional array */
@@ -1343,6 +1258,7 @@ return_NULL:
 
        return NULL;
 }
+#endif /* ENABLE_JAVASE */
 
 
 /* stacktrace_print_trace_from_buffer ******************************************
@@ -1353,7 +1269,7 @@ return_NULL:
 
 *******************************************************************************/
 
-static void stacktrace_print_trace_from_buffer(stacktracebuffer *stb)
+void stacktrace_print_trace_from_buffer(stacktracebuffer *stb)
 {
        stacktrace_entry *ste;
        methodinfo       *m;
@@ -1398,28 +1314,6 @@ void stacktrace_dump_trace(threadobject *thread)
        stacktracebuffer *stb;
        s4                dumpsize;
 
-#if 0
-       /* get methodinfo pointer from data segment */
-
-       m = *((methodinfo **) (pv + MethodPointer));
-
-       /* get current stackframe info pointer */
-
-       psfi = STACKFRAMEINFO;
-
-       /* fill new stackframe info structure */
-
-       sfi->prev   = *psfi;
-       sfi->method = NULL;
-       sfi->pv     = NULL;
-       sfi->sp     = sp;
-       sfi->ra     = ra;
-
-       /* store new stackframe info pointer */
-
-       *psfi = sfi;
-#endif
-
        /* mark start of dump memory area */
 
        dumpsize = dump_size();
@@ -1430,7 +1324,7 @@ void stacktrace_dump_trace(threadobject *thread)
 
        /* print stacktrace */
 
-       if (stb)
+       if (stb != NULL)
                stacktrace_print_trace_from_buffer(stb);
        else {
                puts("\t<<No stacktrace available>>");
@@ -1451,7 +1345,9 @@ void stacktrace_dump_trace(threadobject *thread)
 void stacktrace_print_trace(java_objectheader *xptr)
 {
        java_lang_Throwable   *t;
+#if defined(WITH_CLASSPATH_GNU)
        java_lang_VMThrowable *vmt;
+#endif
        stacktracecontainer   *stc;
        stacktracebuffer      *stb;
 
@@ -1462,9 +1358,14 @@ void stacktrace_print_trace(java_objectheader *xptr)
 
        /* now print the stacktrace */
 
+#if defined(WITH_CLASSPATH_GNU)
        vmt = t->vmState;
        stc = (stacktracecontainer *) vmt->vmData;
        stb = &(stc->stb);
+#elif defined(WITH_CLASSPATH_CLDC1_1)
+       stc = (stacktracecontainer *) t->backtrace;
+       stb = &(stc->stb);
+#endif
 
        stacktrace_print_trace_from_buffer(stb);
 }