1 /* src/native/vm/VMThrowable.c - java/lang/VMThrowable
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Joseph Wenninger
29 Changes: Christian Thalinger
31 $Id: VMThrowable.c 4357 2006-01-22 23:33:38Z twisti $
41 #include "native/jni.h"
42 #include "native/native.h"
43 #include "native/include/java_lang_Class.h"
44 #include "native/include/java_lang_StackTraceElement.h"
45 #include "native/include/java_lang_Throwable.h"
46 #include "native/include/java_lang_VMClass.h"
47 #include "native/include/java_lang_VMThrowable.h"
48 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/loader.h"
52 #include "vm/stringlocal.h"
53 #include "vm/jit/stacktrace.h"
57 * Class: java/lang/VMThrowable
58 * Method: fillInStackTrace
59 * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
61 JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, java_lang_Throwable *par1)
63 java_lang_VMThrowable *vmthrow;
65 vmthrow = (java_lang_VMThrowable *)
66 native_new_and_init(class_java_lang_VMThrowable);
69 log_text("Needed instance of class java.lang.VMThrowable could not be created");
73 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__X86_64__)
74 if (!cacao_stacktrace_NormalTrace((void **) &(vmthrow->vmData)))
83 * Class: java/lang/VMThrowable
84 * Method: getStackTrace
85 * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
87 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *t)
89 #if defined(__ALPHA__) || defined(__ARM__) || defined(__I386__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__X86_64__)
90 stackTraceBuffer *buf;
91 stacktraceelement *elem;
92 stacktraceelement *tmpelem;
96 bool inexceptionclass;
97 bool leftexceptionclass;
100 java_objectarray *oa;
102 java_lang_StackTraceElement *ste;
103 java_lang_String *filename;
105 java_lang_String *declaringclass;
107 buf = (stackTraceBuffer *) this->vmData;
108 c = t->header.vftbl->class;
111 log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code");
118 log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code (length<2)");
122 /* skip first 2 elements in stacktrace buffer: */
123 /* 0: VMThrowable.fillInStackTrace */
124 /* 1: Throwable.fillInStackTrace */
126 elem = &(buf->start[2]);
129 if (size && elem->method != 0) {
130 /* not a builtin native wrapper*/
132 if ((elem->method->class->name == utf_java_lang_Throwable) &&
133 (elem->method->name == utf_init)) {
134 /* We assume that we are within the initializer of the exception */
135 /* object, the exception object itself should not appear in the */
136 /* stack trace, so we skip till we reach the first function, */
137 /* which is not an init function. */
139 inexceptionclass = false;
140 leftexceptionclass = false;
143 /* check if we are in the exception class */
145 if (elem->method->class == c)
146 inexceptionclass = true;
148 /* check if we left the exception class */
150 if (inexceptionclass && (elem->method->class != c))
151 leftexceptionclass = true;
153 /* found exception start point if we left the initalizers or */
154 /* we left the exception class */
156 if ((elem->method->name != utf_init) || leftexceptionclass)
166 /* now fill the stacktrace into java objects */
168 m = class_findmethod(class_java_lang_StackTraceElement,
170 utf_new_char("(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Z)V"));
175 /* count entries with a method name */
177 for (oalength = 0, i = size, tmpelem = elem; i > 0; i--, tmpelem++)
181 /* create the stacktrace element array */
183 oa = builtin_anewarray(oalength, class_java_lang_StackTraceElement);
188 for (i = 0; size > 0; size--, elem++, i++) {
189 if (elem->method == NULL) {
194 /* allocate a new stacktrace element */
196 ste = (java_lang_StackTraceElement *)
197 builtin_new(class_java_lang_StackTraceElement);
204 if (!(elem->method->flags & ACC_NATIVE)) {
205 if (elem->method->class->sourcefile)
206 filename = javastring_new(elem->method->class->sourcefile);
214 /* get line number */
216 if (elem->method->flags & ACC_NATIVE)
219 linenumber = (elem->linenumber == 0) ? -1 : elem->linenumber;
221 /* get declaring class name */
223 declaringclass = Java_java_lang_VMClass_getName(env, NULL, (java_lang_Class *) elem->method->class);
226 /* fill the stacktrace element */
228 ste->fileName = filename;
229 ste->lineNumber = linenumber;
230 ste->declaringClass = declaringclass;
231 ste->methodName = javastring_new(elem->method->name);
232 ste->isNative = (elem->method->flags & ACC_NATIVE) ? 1 : 0;
234 oa->data[i] = (java_objectheader *) ste;
245 * These are local overrides for various environment variables in Emacs.
246 * Please do not remove this and leave it at the end of the file, where
247 * Emacs will automagically detect them.
248 * ---------------------------------------------------------------------
251 * indent-tabs-mode: t