* Updated header: Added 2006. Changed address of FSF. Changed email
[cacao.git] / src / native / vm / VMThrowable.c
index af837a2b4d452e5506e8a9725a8446ac7b9a430f..e15ee36aca7771164b296e5fead27043798ffede 100644 (file)
@@ -1,9 +1,9 @@
-/* native/vm/VMThrowable.c - java/lang/VMThrowable
+/* src/native/vm/VMThrowable.c - java/lang/VMThrowable
 
-   Copyright (C) 1996-2005 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
+   Copyright (C) 1996-2005, 2006 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
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Joseph Wenninger
 
    Changes: Christian Thalinger
 
-   $Id: VMThrowable.c 1919 2005-02-10 10:08:53Z twisti $
+   $Id: VMThrowable.c 4357 2006-01-22 23:33:38Z twisti $
 
 */
 
 
+#include <assert.h>
+
+#include "config.h"
+#include "vm/types.h"
+
 #include "native/jni.h"
 #include "native/native.h"
 #include "native/include/java_lang_Class.h"
+#include "native/include/java_lang_StackTraceElement.h"
 #include "native/include/java_lang_Throwable.h"
 #include "native/include/java_lang_VMClass.h"
 #include "native/include/java_lang_VMThrowable.h"
 #include "vm/builtin.h"
 #include "vm/class.h"
+#include "vm/exceptions.h"
 #include "vm/loader.h"
 #include "vm/stringlocal.h"
-#include "vm/tables.h"
-#include "vm/jit/asmpart.h"
 #include "vm/jit/stacktrace.h"
 
 
@@ -57,143 +62,181 @@ JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackT
 {
        java_lang_VMThrowable *vmthrow;
 
-       vmthrow = (java_lang_VMThrowable *) native_new_and_init(class_java_lang_VMThrowable);
+       vmthrow = (java_lang_VMThrowable *)
+               native_new_and_init(class_java_lang_VMThrowable);
 
-       if (!vmthrow)
-               panic("Needed instance of class  java.lang.VMThrowable could not be created");
+       if (!vmthrow) {
+               log_text("Needed instance of class  java.lang.VMThrowable could not be created");
+               assert(0);
+       }
 
-#if defined(__I386__) || defined(__ALPHA__)
-       cacao_stacktrace_NormalTrace(&(vmthrow->vmData));
+#if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__X86_64__)
+       if (!cacao_stacktrace_NormalTrace((void **) &(vmthrow->vmData)))
+               return NULL;
 #endif
+
        return vmthrow;
 }
 
 
-static
-java_objectarray* generateStackTraceArray(JNIEnv *env,stacktraceelement *el,long size)
+/*
+ * Class:     java/lang/VMThrowable
+ * Method:    getStackTrace
+ * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
+ */
+JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *t)
 {
-       long resultPos;
-       methodinfo *m;
-       classinfo *c;
-       java_objectarray *oa;
+#if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__X86_64__)
+       stackTraceBuffer            *buf;
+       stacktraceelement           *elem;
+       stacktraceelement           *tmpelem;
+       s4                           size;
+       s4                           i;
+       classinfo                   *c;
+       bool                         inexceptionclass;
+       bool                         leftexceptionclass;
+
+       methodinfo                  *m;
+       java_objectarray            *oa;
+       s4                           oalength;
+       java_lang_StackTraceElement *ste;
+       java_lang_String            *filename;
+       s4                           linenumber;
+       java_lang_String            *declaringclass;
+
+       buf = (stackTraceBuffer *) this->vmData;
+       c = t->header.vftbl->class;
+
+       if (!buf) {
+               log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code");
+               assert(0);
+       }
+       
+       size = buf->full;
+
+       if (size < 2) {
+               log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code (length<2)");
+               assert(0);
+       }
+
+       /* skip first 2 elements in stacktrace buffer:                            */
+       /*   0: VMThrowable.fillInStackTrace                                      */
+       /*   1: Throwable.fillInStackTrace                                        */
+
+       elem = &(buf->start[2]);
+       size -= 2;
 
-       c = class_new(utf_new_char("java/lang/StackTraceElement"));
+       if (size && elem->method != 0) {
+               /* not a builtin native wrapper*/
 
-       if (!c->loaded)
-               class_load(c);
+               if ((elem->method->class->name == utf_java_lang_Throwable) &&
+                       (elem->method->name == utf_init)) {
+                       /* We assume that we are within the initializer of the exception  */
+                       /* object, the exception object itself should not appear in the   */
+                       /* stack trace, so we skip till we reach the first function,      */
+                       /* which is not an init function.                                 */
 
-       if (!c->linked)
-               class_link(c);
+                       inexceptionclass = false;
+                       leftexceptionclass = false;
 
-       m = class_findmethod(c,
-                                                utf_new_char("<init>"),
+                       while (size > 0) {
+                               /* check if we are in the exception class */
+
+                               if (elem->method->class == c)
+                                       inexceptionclass = true;
+
+                               /* check if we left the exception class */
+
+                               if (inexceptionclass && (elem->method->class != c))
+                                       leftexceptionclass = true;
+
+                               /* found exception start point if we left the initalizers or  */
+                               /* we left the exception class                                */
+
+                               if ((elem->method->name != utf_init) || leftexceptionclass)
+                                       break;
+
+                               elem++;
+                               size--;
+                       }
+               }
+       }
+
+
+       /* now fill the stacktrace into java objects */
+
+       m = class_findmethod(class_java_lang_StackTraceElement,
+                                                utf_init,
                                                 utf_new_char("(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Z)V"));
 
        if (!m)
-               panic("java.lang.StackTraceElement misses needed constructor"); 
+               return NULL;
 
-       oa = builtin_anewarray(size, c);
+       /* count entries with a method name */
 
-       if (!oa)
-               return 0;
+       for (oalength = 0, i = size, tmpelem = elem; i > 0; i--, tmpelem++)
+               if (tmpelem->method)
+                       oalength++;
 
-/*     printf("Should return an array with %ld element(s)\n",size);*/
-       /*pos--;*/
-       
-       for(resultPos=0;size>0;size--,el++,resultPos++) {
-               java_objectheader *element;
+       /* create the stacktrace element array */
 
-               if (el->method==0) {
-                       resultPos--;
+       oa = builtin_anewarray(oalength, class_java_lang_StackTraceElement);
+
+       if (!oa)
+               return NULL;
+
+       for (i = 0; size > 0; size--, elem++, i++) {
+               if (elem->method == NULL) {
+                       i--;
                        continue;
                }
 
-               element=builtin_new(c);
-               if (!element) {
-                       panic("Memory for stack trace element could not be allocated");
-               }
-#ifdef __GNUC__
-#warning call constructor once jni is fixed to allow more than three parameters
-#endif
-#if 0
-               (*env)->CallVoidMethod(env,element,m,
-                       javastring_new(el->method->class->sourcefile),
-                       source[size].linenumber,
-                       javastring_new(el->method->class->name),
-                       javastring_new(el->method->name),
-                       el->method->flags & ACC_NATIVE);
-#else
-               if (!(el->method->flags & ACC_NATIVE))setfield_critical(c,element,"fileName",          
-               "Ljava/lang/String;",  jobject, 
-               (jobject) javastring_new(el->method->class->sourcefile));
-/*             setfield_critical(c,element,"className",          "Ljava/lang/String;",  jobject,  */
-/*             (jobject) javastring_new(el->method->class->name)); */
-               setfield_critical(c,element,"declaringClass",          "Ljava/lang/String;",  jobject, 
-               (jobject) Java_java_lang_VMClass_getName(env, NULL, (java_lang_Class *) el->method->class));
-               setfield_critical(c,element,"methodName",          "Ljava/lang/String;",  jobject, 
-               (jobject) javastring_new(el->method->name));
-               setfield_critical(c,element,"lineNumber",          "I",  jint, 
-               (jint) ((el->method->flags & ACC_NATIVE) ? -1:(el->linenumber)));
-               setfield_critical(c,element,"isNative",          "Z",  jboolean, 
-               (jboolean) ((el->method->flags & ACC_NATIVE) ? 1:0));
-
-
-#endif                 
-
-               oa->data[resultPos]=element;
-       }
+               /* allocate a new stacktrace element */
 
-       return oa;
+               ste = (java_lang_StackTraceElement *)
+                       builtin_new(class_java_lang_StackTraceElement);
 
-}
+               if (!ste)
+                       return NULL;
 
+               /* get filename */
 
-/*
- * Class:     java/lang/VMThrowable
- * Method:    getStackTrace
- * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
- */
-JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *par1)
-{
-#if defined(__I386__) || defined(__ALPHA__)
-       stackTraceBuffer *buf=(stackTraceBuffer*)this->vmData;
-       u8 size;
-       stacktraceelement *el;
-       classinfo  *excClass=par1->header.vftbl->class;
-       utf*  init=utf_new_char("<init>");
-       utf*  throwable=utf_new_char("java/lang/Throwable");
-       long destElementCount;
-       stacktraceelement *tmpEl;
-
-       if (!buf) panic("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code");
-       
-       size=buf->full;
-       if (size<=2) panic("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code (length<=2)");
-       size -=2;
-       el=&(buf->start[2]); /* element 0==VMThrowable.fillInStackTrace native call, 1==Throwable.fillInStackTrace*/
-       if (el->method!=0) { /* => not a builtin native wrapper*/
-               if ((el->method->class->name==throwable) && (el->method->name == init) ){
-                       /* We assume that we are within the initializer of the exception object, the exception object itself should not appear
-                               in the stack trace, so we skip till we reach the first function, which is not an init function or till we reach the exception object class*/
-                       for (; (size>0) && (el->method->name==init) && (el->method->class!=excClass); el++, size--) {
-                               /* just loop*/
-                       }
-                       size --;
-                       el++;
-                       /*if (size<1) {
-                               log_text("Invalid stacktrace for VMThrowable.getStackTrace()");
-                       }*/
+               if (!(elem->method->flags & ACC_NATIVE)) {
+                       if (elem->method->class->sourcefile)
+                               filename = javastring_new(elem->method->class->sourcefile);
+                       else
+                               filename = NULL;
+
+               } else {
+                       filename = NULL;
                }
-       }
 
-       
-       for (destElementCount = 0, tmpEl=el; size>0; size--,tmpEl++) {
-               if (tmpEl->method!=0) destElementCount++;
+               /* get line number */
+
+               if (elem->method->flags & ACC_NATIVE)
+                       linenumber = -1;
+               else
+                       linenumber = (elem->linenumber == 0) ? -1 : elem->linenumber;
+
+               /* get declaring class name */
+
+               declaringclass = Java_java_lang_VMClass_getName(env, NULL, (java_lang_Class *) elem->method->class);
+
+
+               /* fill the stacktrace element */
+
+               ste->fileName       = filename;
+               ste->lineNumber     = linenumber;
+               ste->declaringClass = declaringclass;
+               ste->methodName     = javastring_new(elem->method->name);
+               ste->isNative       = (elem->method->flags & ACC_NATIVE) ? 1 : 0;
+
+               oa->data[i] = (java_objectheader *) ste;
        }
 
-       return generateStackTraceArray(env,el,destElementCount);
+       return oa;
 #else
-       return 0;
+       return NULL;
 #endif
 }