* src/vm/jit/trace.c (trace_java_call_enter): Removed unused variable.
[cacao.git] / src / vm / jit / stacktrace.c
index 8a58dce8c98c537a87b303d0bf8254bbae06b7ed..b8847deb3ad31d9638d5ffd242b252900d2a8437 100644 (file)
@@ -65,6 +65,7 @@
 
 #include "vm/jit/asmpart.h"
 #include "vm/jit/codegen-common.h"
+#include "vm/jit/linenumbertable.h"
 #include "vm/jit/methodheader.h"
 
 #include "vmcore/class.h"
@@ -74,7 +75,7 @@
 
 /* global variables ***********************************************************/
 #if !defined(ENABLE_THREADS)
-stackframeinfo *_no_threads_stackframeinfo = NULL;
+stackframeinfo_t *_no_threads_stackframeinfo = NULL;
 #endif
 
 CYCLES_STATS_DECLARE(stacktrace_overhead        ,100,1)
@@ -91,15 +92,15 @@ CYCLES_STATS_DECLARE(stacktrace_getStack        ,40,10000)
 
 *******************************************************************************/
 
-void stacktrace_stackframeinfo_add(stackframeinfo *sfi, u1 *pv, u1 *sp, u1 *ra, u1 *xpc)
+void stacktrace_stackframeinfo_add(stackframeinfo_t *sfi, u1 *pv, u1 *sp, u1 *ra, u1 *xpc)
 {
-       stackframeinfo **psfi;
-       methodinfo      *m;
+       stackframeinfo_t **psfi;
+       codeinfo          *code;
 #if !defined(__I386__) && !defined(__X86_64__) && !defined(__S390__) && !defined(__M68K__)
-       bool             isleafmethod;
+       bool               isleafmethod;
 #endif
 #if defined(ENABLE_JIT)
-       s4               framesize;
+       s4                 framesize;
 #endif
 
        /* get current stackframe info pointer */
@@ -126,9 +127,9 @@ void stacktrace_stackframeinfo_add(stackframeinfo *sfi, u1 *pv, u1 *sp, u1 *ra,
                        }
        }
 
-       /* Get methodinfo pointer for the parent Java method. */
+       /* Get codeinfo pointer for the parent Java method. */
 
-       m = code_get_methodinfo_for_pv(pv);
+       code = code_get_codeinfo_for_pv(pv);
 
        /* XXX */
 /*     assert(m != NULL); */
@@ -179,19 +180,19 @@ void stacktrace_stackframeinfo_add(stackframeinfo *sfi, u1 *pv, u1 *sp, u1 *ra,
 
        /* Fill new stackframeinfo structure. */
 
-       sfi->prev   = *psfi;
-       sfi->method = m;
-       sfi->pv     = pv;
-       sfi->sp     = sp;
-       sfi->ra     = ra;
-       sfi->xpc    = xpc;
+       sfi->prev = *psfi;
+       sfi->code = code;
+       sfi->pv   = pv;
+       sfi->sp   = sp;
+       sfi->ra   = ra;
+       sfi->xpc  = xpc;
 
 #if !defined(NDEBUG)
        if (opt_DebugStackFrameInfo) {
                log_start();
                log_print("[stackframeinfo add   : sfi=%p, method=%p, pv=%p, sp=%p, ra=%p, xpc=%p, method=",
-                                 sfi, sfi->method, sfi->pv, sfi->sp, sfi->ra, sfi->xpc);
-               method_print(sfi->method);
+                                 sfi, sfi->code->m, sfi->pv, sfi->sp, sfi->ra, sfi->xpc);
+               method_print(sfi->code->m);
                log_print("]");
                log_finish();
        }
@@ -216,9 +217,9 @@ void stacktrace_stackframeinfo_add(stackframeinfo *sfi, u1 *pv, u1 *sp, u1 *ra,
 
 *******************************************************************************/
 
-void stacktrace_stackframeinfo_remove(stackframeinfo *sfi)
+void stacktrace_stackframeinfo_remove(stackframeinfo_t *sfi)
 {
-       stackframeinfo **psfi;
+       stackframeinfo_t **psfi;
 
        /* clear the native world flag for the current thread */
        /* ATTENTION: Clear this flag _before_ removing the stackframe info */
@@ -233,8 +234,8 @@ void stacktrace_stackframeinfo_remove(stackframeinfo *sfi)
        if (opt_DebugStackFrameInfo) {
                log_start();
                log_print("[stackframeinfo remove: sfi=%p, method=%p, pv=%p, sp=%p, ra=%p, xpc=%p, method=",
-                                 sfi, sfi->method, sfi->pv, sfi->sp, sfi->ra, sfi->xpc);
-               method_print(sfi->method);
+                                 sfi, sfi->code->m, sfi->pv, sfi->sp, sfi->ra, sfi->xpc);
+               method_print(sfi->code->m);
                log_print("]");
                log_finish();
        }
@@ -310,18 +311,21 @@ static inline stacktracebuffer *stacktrace_entry_add(stacktracebuffer *stb, meth
 
 *******************************************************************************/
 
-static inline stacktracebuffer *stacktrace_method_add(stacktracebuffer *stb, stackframeinfo *sfi)
+static inline stacktracebuffer *stacktrace_method_add(stacktracebuffer *stb, stackframeinfo_t *sfi)
 {
-       methodinfo *m;
+       codeinfo   *code;
        void       *pv;
        void       *xpc;
+       methodinfo *m;
        int32_t     linenumber;
 
        /* Get values from the stackframeinfo. */
 
-       m   = sfi->method;
-       pv  = sfi->pv;
-       xpc = sfi->xpc;
+       code = sfi->code;
+       pv   = sfi->pv;
+       xpc  = sfi->xpc;
+
+       m = code->m;
 
        /* Skip builtin methods. */
 
@@ -330,7 +334,7 @@ static inline stacktracebuffer *stacktrace_method_add(stacktracebuffer *stb, sta
 
        /* Search the line number table. */
 
-       linenumber = dseg_get_linenumber_from_pc(&m, pv, xpc);
+       linenumber = linenumbertable_linenumber_for_pc(&m, code, xpc);
 
        /* Add a new entry to the staktrace. */
 
@@ -345,26 +349,31 @@ static inline stacktracebuffer *stacktrace_method_add(stacktracebuffer *stb, sta
    Walk the stack (or the stackframeinfo-chain) to the next method.
 
    IN:
-       sfi....stackframeinfo of current method
+       tmpsfi ... stackframeinfo of current method
+
+   RETURN:
+       true .... the sfi is filled with the new values
+       false ... we reached the top of the stacktrace
 
 *******************************************************************************/
 
-static inline void stacktrace_stack_walk(stackframeinfo *sfi)
+static inline bool stacktrace_stack_walk(stackframeinfo_t *tmpsfi)
 {
-       methodinfo *m;
-       void       *pv;
-       void       *sp;
-       void       *ra;
-       void       *xpc;
-       uint32_t    framesize;
+       codeinfo         *code;
+       void             *pv;
+       void             *sp;
+       void             *ra;
+       void             *xpc;
+       uint32_t          framesize;
+       stackframeinfo_t *prevsfi;
 
        /* Get values from the stackframeinfo. */
 
-       m   = sfi->method;
-       pv  sfi->pv;
-       sp  sfi->sp;
-       ra  sfi->ra;
-       xpc sfi->xpc;
+       code = tmpsfi->code;
+       pv   = tmpsfi->pv;
+       sp   = tmpsfi->sp;
+       ra   = tmpsfi->ra;
+       xpc  = tmpsfi->xpc;
 
        /* Get the current stack frame size. */
 
@@ -382,14 +391,8 @@ static inline void stacktrace_stack_walk(stackframeinfo *sfi)
        else
 # endif
                {
-                       /* TODO Remove jd->isleafmethod and use the flags in
-                          codeinfo. */
-
-/*                     if (!CODE_IS_LEAFMETHOD(m->code)) { */
-                       int32_t isleafmethod = *((int32_t *) (((intptr_t) pv) + IsLeaf));
-                       if (!isleafmethod) {
+                       if (!code_is_leafmethod(code))
                                ra = md_stacktrace_get_returnaddress(sp, framesize);
-                       }
                }
 #else
        ra = intrp_md_stacktrace_get_returnaddress(sp, framesize);
@@ -413,9 +416,9 @@ static inline void stacktrace_stack_walk(stackframeinfo *sfi)
 #endif
                }
 
-       /* Get the methodinfo pointer for the parent Java method. */
+       /* Get the codeinfo pointer for the parent Java method. */
 
-       m = code_get_methodinfo_for_pv(pv);
+       code = code_get_codeinfo_for_pv(pv);
 
        /* Calculate the SP for the parent Java method. */
 
@@ -434,15 +437,46 @@ static inline void stacktrace_stack_walk(stackframeinfo *sfi)
 #endif
                }
 
-       /* Store the new values in the stackframeinfo.  NOTE: We subtract
-          1 from the RA to get the XPC, because the RA points to the
-          instruction after the call instruction. */
+       /* If the new codeinfo pointer is NULL we reached a
+          asm_vm_call_method function.  In this case we get the next
+          values from the previous stackframeinfo in the chain.
+          Otherwise the new values have been calculated before. */
+
+       if (code == NULL) {
+               prevsfi = tmpsfi->prev;
+
+               /* If the previous stackframeinfo in the chain is NULL we
+                  reached the top of the stacktrace and return false. */
+
+               if (prevsfi == NULL)
+                       return false;
+
+               /* Fill the temporary stackframeinfo with the new values. */
+
+               tmpsfi->code = prevsfi->code;
+               tmpsfi->pv   = prevsfi->pv;
+               tmpsfi->sp   = prevsfi->sp;
+               tmpsfi->ra   = prevsfi->ra;
+               tmpsfi->xpc  = prevsfi->xpc;
+
+               /* Set the previous stackframe info of the temporary one to
+                  the next in the chain. */
 
-       sfi->method = m;
-       sfi->pv     = pv;
-       sfi->sp     = sp;
-       sfi->ra     = ra;
-       sfi->xpc    = (void *) (((intptr_t) ra) - 1);
+               tmpsfi->prev = prevsfi->prev;
+       }
+       else {
+               /* Store the new values in the stackframeinfo.  NOTE: We
+                  subtract 1 from the RA to get the XPC, because the RA
+                  points to the instruction after the call instruction. */
+
+               tmpsfi->code = code;
+               tmpsfi->pv   = pv;
+               tmpsfi->sp   = sp;
+               tmpsfi->ra   = ra;
+               tmpsfi->xpc  = (void *) (((intptr_t) ra) - 1);
+       }
+
+       return true;
 }
 
 
@@ -463,10 +497,15 @@ static inline void stacktrace_stack_walk(stackframeinfo *sfi)
 
 *******************************************************************************/
 
-stacktracebuffer *stacktrace_create(stackframeinfo *sfi)
+stacktracebuffer *stacktrace_create(stackframeinfo_t *sfi)
 {
        stacktracebuffer *stb;
-       stackframeinfo    tmpsfi;
+       stackframeinfo_t  tmpsfi;
+       bool              skip_fillInStackTrace;
+       bool              skip_init;
+
+       skip_fillInStackTrace = true;
+       skip_init             = true;
 
        /* Create a stacktracebuffer in dump memory. */
 
@@ -490,68 +529,78 @@ stacktracebuffer *stacktrace_create(stackframeinfo *sfi)
        if (sfi == NULL)
                return NULL;
 
-       tmpsfi.method = sfi->method;
-       tmpsfi.pv     = sfi->pv;
-       tmpsfi.sp     = sfi->sp;
-       tmpsfi.ra     = sfi->ra;
-       tmpsfi.xpc    = sfi->xpc;
+       tmpsfi.code = sfi->code;
+       tmpsfi.pv   = sfi->pv;
+       tmpsfi.sp   = sfi->sp;
+       tmpsfi.ra   = sfi->ra;
+       tmpsfi.xpc  = sfi->xpc;
+
+       /* Initially set the previous stackframe info of the temporary one
+          to the next in the chain. */
+
+       tmpsfi.prev = sfi->prev;
 
        /* Iterate till we're done. */
 
-       for (;;) {
+       do {
 #if !defined(NDEBUG)
                /* Print current method information. */
 
                if (opt_DebugStackTrace) {
                        log_start();
                        log_print("[stacktrace: method=%p, pv=%p, sp=%p, ra=%p, xpc=%p, method=",
-                                         tmpsfi.method, tmpsfi.pv, tmpsfi.sp, tmpsfi.ra,
+                                         tmpsfi.code->m, tmpsfi.pv, tmpsfi.sp, tmpsfi.ra,
                                          tmpsfi.xpc);
-                       method_print(tmpsfi.method);
+                       method_print(tmpsfi.code->m);
                        log_print("]");
                        log_finish();
                }
 #endif
 
-               /* Check for Throwable.fillInStackTrace(). */
+               /* This logic is taken from
+                  hotspot/src/share/vm/classfile/javaClasses.cpp
+                  (java_lang_Throwable::fill_in_stack_trace). */
 
-/*             if (tmpsfi.method->name != utf_fillInStackTrace) { */
-                       
-                       /* Add this method to the stacktrace. */
+               if (skip_fillInStackTrace == true) {
+                       /* Check "fillInStackTrace" only once, so we negate the
+                          flag after the first time check. */
 
-                       stb = stacktrace_method_add(stb, &tmpsfi);
-/*             } */
+#if defined(WITH_CLASSPATH_GNU)
+                       /* For GNU Classpath we also need to skip
+                          VMThrowable.fillInStackTrace(). */
 
-               /* Walk the stack (or the stackframeinfo chain) and get the
-                  next method. */
+                       if ((tmpsfi.code->m->class == class_java_lang_VMThrowable) &&
+                               (tmpsfi.code->m->name  == utf_fillInStackTrace))
+                               continue;
+#endif
 
-               stacktrace_stack_walk(&tmpsfi);
+                       skip_fillInStackTrace = false;
 
-               /* If the new methodinfo pointer is NULL we reached a
-                  asm_vm_call_method function.  In this case we get the next
-                  values from the previous stackframeinfo in the chain.
-                  Otherwise the new values have been calculated before. */
+                       if (tmpsfi.code->m->name == utf_fillInStackTrace)
+                               continue;
+               }
 
-               if (tmpsfi.method == NULL) {
-                       sfi = sfi->prev;
+               /* Skip <init> methods of the exceptions klass.  If there is
+                  <init> methods that belongs to a superclass of the
+                  exception we are going to skipping them in stack trace. */
 
-                       /* If the previous stackframeinfo in the chain is NULL we
-                          reached the top of the stacktrace and leave the
-                          loop. */
+               if (skip_init == true) {
+                       if (tmpsfi.code->m->name == utf_init) {
+/*                             throwable->is_a(method->method_holder())) { */
+                               continue;
+                       }
+                       else {
+                               /* If no "Throwable.init()" method found, we stop
+                                  checking it next time. */
 
-                       if (sfi == NULL)
-                               break;
+                               skip_init = false;
+                       }
+               }
 
-                       /* Fill the temporary stackframeinfo with the new
-                          values. */
+               /* Add this method to the stacktrace. */
 
-                       tmpsfi.method = sfi->method;
-                       tmpsfi.pv     = sfi->pv;
-                       tmpsfi.sp     = sfi->sp;
-                       tmpsfi.ra     = sfi->ra;
-                       tmpsfi.xpc    = sfi->xpc;
-               }
-       }
+               stb = stacktrace_method_add(stb, &tmpsfi);
+       } while (stacktrace_stack_walk(&tmpsfi) == true);
 
 #if !defined(NDEBUG)
        if (opt_DebugStackTrace) {