1 /* src/native/vm/VMThrowable.c - java/lang/VMThrowable
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
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 Changes: Christian Thalinger
31 $Id: VMThrowable.c 2945 2005-07-09 12:16:18Z twisti $
38 #include "native/jni.h"
39 #include "native/native.h"
40 #include "native/include/java_lang_Class.h"
41 #include "native/include/java_lang_Throwable.h"
42 #include "native/include/java_lang_VMClass.h"
43 #include "native/include/java_lang_VMThrowable.h"
44 #include "vm/builtin.h"
46 #include "vm/loader.h"
47 #include "vm/stringlocal.h"
48 #include "vm/tables.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/stacktrace.h"
51 #include "vm/exceptions.h"
55 * Class: java/lang/VMThrowable
56 * Method: fillInStackTrace
57 * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
59 JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, java_lang_Throwable *par1)
61 java_lang_VMThrowable *vmthrow;
63 if ((*dontfillinexceptionstacktrace) == true) {
64 /*log_text("dontfillinexceptionstacktrace");*/
68 vmthrow = (java_lang_VMThrowable *)
69 native_new_and_init(class_java_lang_VMThrowable);
72 log_text("Needed instance of class java.lang.VMThrowable could not be created");
76 #if defined(__ALPHA__) || defined(__I386__) || defined(__POWERPC__) || defined(__X86_64__)
77 cacao_stacktrace_NormalTrace((void **) &(vmthrow->vmData));
83 static java_objectarray *generateStackTraceArray(JNIEnv *env,stacktraceelement *el, s4 size)
89 m = class_findmethod(class_java_lang_StackTraceElement,
91 utf_new_char("(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Z)V"));
94 log_text("java.lang.StackTraceElement misses needed constructor");
98 oa = builtin_anewarray(size, class_java_lang_StackTraceElement);
103 /* printf("Should return an array with %ld element(s)\n",size);*/
106 for(resultPos = 0; size > 0; size--, el++, resultPos++) {
107 java_objectheader *element;
109 if (el->method == NULL) {
114 element = builtin_new(class_java_lang_StackTraceElement);
116 log_text("Memory for stack trace element could not be allocated");
120 /* XXX call constructor once jni is fixed to allow more than three parameters */
123 (*env)->CallVoidMethod(env,element,m,
124 javastring_new(el->method->class->sourcefile),
125 source[size].linenumber,
126 javastring_new(el->method->class->name),
127 javastring_new(el->method->name),
128 el->method->flags & ACC_NATIVE);
130 if (!(el->method->flags & ACC_NATIVE))
131 setfield_critical(class_java_lang_StackTraceElement,
134 "Ljava/lang/String;",
136 (jobject) javastring_new(el->method->class->sourcefile));
138 /* setfield_critical(c,element,"className", "Ljava/lang/String;", jobject, */
139 /* (jobject) javastring_new(el->method->class->name)); */
141 setfield_critical(class_java_lang_StackTraceElement,
144 "Ljava/lang/String;",
146 (jobject) Java_java_lang_VMClass_getName(env, NULL, (java_lang_Class *) el->method->class));
148 setfield_critical(class_java_lang_StackTraceElement,
151 "Ljava/lang/String;",
153 (jobject) javastring_new(el->method->name));
155 setfield_critical(class_java_lang_StackTraceElement,
160 (jint) ((el->method->flags & ACC_NATIVE) ? -1 : (el->linenumber)));
162 setfield_critical(class_java_lang_StackTraceElement,
167 (jboolean) ((el->method->flags & ACC_NATIVE) ? 1 : 0));
170 oa->data[resultPos] = element;
178 * Class: java/lang/VMThrowable
179 * Method: getStackTrace
180 * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
182 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *t)
184 #if defined(__ALPHA__) || defined(__I386__) || defined(__POWERPC__) || defined(__X86_64__)
185 stackTraceBuffer *buf;
186 stacktraceelement *elem;
187 stacktraceelement *tmpelem;
191 bool inexceptionclass;
192 bool leftexceptionclass;
194 buf = (stackTraceBuffer *) this->vmData;
195 c = t->header.vftbl->class;
198 log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code");
205 log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code (length<2)");
209 /* skip first 2 elements in stacktrace buffer: */
210 /* 0: VMThrowable.fillInStackTrace */
211 /* 1: Throwable.fillInStackTrace */
213 elem = &(buf->start[2]);
216 if (size && elem->method != 0) {
217 /* not a builtin native wrapper*/
219 if ((elem->method->class->name == utf_java_lang_Throwable) &&
220 (elem->method->name == utf_init)) {
221 /* We assume that we are within the initializer of the exception */
222 /* object, the exception object itself should not appear in the */
223 /* stack trace, so we skip till we reach the first function, */
224 /* which is not an init function. */
226 inexceptionclass = false;
227 leftexceptionclass = false;
230 /* check if we are in the exception class */
232 if (elem->method->class == c)
233 inexceptionclass = true;
235 /* check if we left the exception class */
237 if (inexceptionclass && (elem->method->class != c))
238 leftexceptionclass = true;
240 /* found exception start point if we left the initalizers or */
241 /* we left the exception class */
243 if ((elem->method->name != utf_init) || leftexceptionclass)
252 for (elemcount = 0, tmpelem = elem; size > 0; size--, tmpelem++) {
257 return generateStackTraceArray(env, elem, elemcount);
265 * These are local overrides for various environment variables in Emacs.
266 * Please do not remove this and leave it at the end of the file, where
267 * Emacs will automagically detect them.
268 * ---------------------------------------------------------------------
271 * indent-tabs-mode: t