1 /* nat/VMThrowable.c - java/lang/Throwable
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Joseph Wenninger
29 $Id: VMThrowable.c 1112 2004-05-31 15:47:20Z jowenn $
41 #include "java_lang_Throwable.h"
42 #include "java_lang_VMThrowable.h"
46 * Class: java/lang/VMThrowable
47 * Method: fillInStackTrace
48 * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
50 JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, java_lang_Throwable *par1)
52 classinfo *class_java_lang_VMThrowable = NULL;
53 java_lang_VMThrowable *vmthrow;
55 if (!class_java_lang_VMThrowable)
56 class_java_lang_VMThrowable = class_new(utf_new_char("java/lang/VMThrowable"));
58 if (class_java_lang_VMThrowable == NULL)
59 panic("Needed class java.lang.VMThrowable missing");
61 vmthrow = (java_lang_VMThrowable *) native_new_and_init(class_java_lang_VMThrowable);
64 panic("Needed instance of class java.lang.VMThrowable could not be created");
67 (void) asm_get_stackTrace(&(vmthrow->vmData));
77 java_objectarray* generateStackTraceArray(JNIEnv *env,stacktraceelement *source,long pos,long size)
84 c = class_new(utf_new_char("java/lang/StackTraceElement"));
92 m = class_findmethod(c,
93 utf_new_char("<init>"),
94 utf_new_char("(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Z)V"));
97 panic("java.lang.StackTraceElement misses needed constructor");
99 oa = builtin_anewarray(size, c);
104 /* printf("Should return an array with %ld element(s)\n",size);*/
108 for(resultPos=0;pos>=0;resultPos++,pos--) {
109 java_objectheader *element;
111 if (source[pos].method==0) {
116 element=builtin_new(c);
118 panic("Memory for stack trace element could not be allocated");
121 #warning call constructor once jni is fixed to allow more than three parameters
124 (*env)->CallVoidMethod(env,element,m,
125 javastring_new(source[pos].method->class->sourcefile),
126 source[size].linenumber,
127 javastring_new(source[pos].method->class->name),
128 javastring_new(source[pos].method->name),
129 source[pos].method->flags & ACC_NATIVE);
131 if (!(source[pos].method->flags & ACC_NATIVE))setfield_critical(c,element,"fileName",
132 "Ljava/lang/String;", jobject,
133 (jobject) javastring_new(source[pos].method->class->sourcefile));
134 setfield_critical(c,element,"className", "Ljava/lang/String;", jobject,
135 (jobject) javastring_new(source[pos].method->class->name));
136 setfield_critical(c,element,"methodName", "Ljava/lang/String;", jobject,
137 (jobject) javastring_new(source[pos].method->name));
138 setfield_critical(c,element,"lineNumber", "I", jint,
139 (jint) ((source[pos].method->flags & ACC_NATIVE) ? -1:(source[pos].linenumber)));
140 setfield_critical(c,element,"isNative", "Z", jboolean,
141 (jboolean) ((source[pos].method->flags & ACC_NATIVE) ? 1:0));
146 oa->data[resultPos]=element;
155 * Class: java/lang/VMThrowable
156 * Method: getStackTrace
157 * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
159 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *par1)
164 utf* classname=par1->header.vftbl->class->name;
165 utf* init=utf_new_char("<init>");
166 utf* throwable=utf_new_char("java/lang/Throwable");
167 stacktraceelement *el=(stacktraceelement*)this->vmData;
169 /* log_text("Java_java_lang_VMThrowable_getStackTrace");
170 utf_display(par1->header.vftbl->class->name);
171 printf("\n----------------------------------------------\n");*/
175 return generateStackTraceArray(env, el, 0,0);
178 for (pos = 0; !((el[pos].method == 0) && (el[pos].linenumber ==-1)); pos++) {
179 if (el[pos].method==0) sizediff++;
183 panic("Stacktrace cannot have zero length");
189 if (el[pos].method!=0) { /* if == 0 -> some builtin native */
190 if (el[pos].method->class->name == throwable && el[pos].method->name == init) {
191 for (; pos >= 0 && el[pos].method->name == init && el[pos].method->class->name != classname; pos--) {
192 /* log_text("ignoring:");
193 utf_display(el[pos].method->name);
195 utf_display(el[pos].method->class->name);
201 log_text("Invalid stack trace for Throwable.getStackTrace()");
207 /* build the result array*/
210 return generateStackTraceArray(env,el,pos,pos-sizediff);
215 * These are local overrides for various environment variables in Emacs.
216 * Please do not remove this and leave it at the end of the file, where
217 * Emacs will automagically detect them.
218 * ---------------------------------------------------------------------
221 * indent-tabs-mode: t