* Added powerpc defines
[cacao.git] / src / native / vm / VMThrowable.c
1 /* src/native/vm/VMThrowable.c - java/lang/VMThrowable
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Joseph Wenninger
28
29    Changes: Christian Thalinger
30
31    $Id: VMThrowable.c 2945 2005-07-09 12:16:18Z twisti $
32
33 */
34
35
36 #include <assert.h>
37
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"
45 #include "vm/class.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"
52
53
54 /*
55  * Class:     java/lang/VMThrowable
56  * Method:    fillInStackTrace
57  * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
58  */
59 JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, java_lang_Throwable *par1)
60 {
61         java_lang_VMThrowable *vmthrow;
62
63         if ((*dontfillinexceptionstacktrace) == true) {
64                 /*log_text("dontfillinexceptionstacktrace");*/
65                 return 0;
66         }
67
68         vmthrow = (java_lang_VMThrowable *)
69                 native_new_and_init(class_java_lang_VMThrowable);
70
71         if (!vmthrow) {
72                 log_text("Needed instance of class  java.lang.VMThrowable could not be created");
73                 assert(0);
74         }
75
76 #if defined(__ALPHA__) || defined(__I386__) || defined(__POWERPC__) || defined(__X86_64__)
77         cacao_stacktrace_NormalTrace((void **) &(vmthrow->vmData));
78 #endif
79         return vmthrow;
80 }
81
82
83 static java_objectarray *generateStackTraceArray(JNIEnv *env,stacktraceelement *el, s4 size)
84 {
85         long resultPos;
86         methodinfo *m;
87         java_objectarray *oa;
88
89         m = class_findmethod(class_java_lang_StackTraceElement,
90                                                  utf_init,
91                                                  utf_new_char("(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Z)V"));
92
93         if (!m) {
94                 log_text("java.lang.StackTraceElement misses needed constructor");      
95                 assert(0);
96         }
97
98         oa = builtin_anewarray(size, class_java_lang_StackTraceElement);
99
100         if (!oa)
101                 return 0;
102
103 /*      printf("Should return an array with %ld element(s)\n",size);*/
104         /*pos--;*/
105         
106         for(resultPos = 0; size > 0; size--, el++, resultPos++) {
107                 java_objectheader *element;
108
109                 if (el->method == NULL) {
110                         resultPos--;
111                         continue;
112                 }
113
114                 element = builtin_new(class_java_lang_StackTraceElement);
115                 if (!element) {
116                         log_text("Memory for stack trace element could not be allocated");
117                         assert(0);
118                 }
119
120                 /* XXX call constructor once jni is fixed to allow more than three parameters */
121
122 #if 0
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);
129 #else
130                 if (!(el->method->flags & ACC_NATIVE))
131                         setfield_critical(class_java_lang_StackTraceElement,
132                                                           element,
133                                                           "fileName",
134                                                           "Ljava/lang/String;",
135                                                           jobject,
136                                                           (jobject) javastring_new(el->method->class->sourcefile));
137
138 /*              setfield_critical(c,element,"className",          "Ljava/lang/String;",  jobject,  */
139 /*              (jobject) javastring_new(el->method->class->name)); */
140
141                 setfield_critical(class_java_lang_StackTraceElement,
142                                                   element,
143                                                   "declaringClass",
144                                                   "Ljava/lang/String;",
145                                                   jobject, 
146                                                   (jobject) Java_java_lang_VMClass_getName(env, NULL, (java_lang_Class *) el->method->class));
147
148                 setfield_critical(class_java_lang_StackTraceElement,
149                                                   element,
150                                                   "methodName",
151                                                   "Ljava/lang/String;",
152                                                   jobject,
153                                                   (jobject) javastring_new(el->method->name));
154
155                 setfield_critical(class_java_lang_StackTraceElement,
156                                                   element,
157                                                   "lineNumber",
158                                                   "I",
159                                                   jint, 
160                                                   (jint) ((el->method->flags & ACC_NATIVE) ? -1 : (el->linenumber)));
161
162                 setfield_critical(class_java_lang_StackTraceElement,
163                                                   element,
164                                                   "isNative",
165                                                   "Z",
166                                                   jboolean, 
167                                                   (jboolean) ((el->method->flags & ACC_NATIVE) ? 1 : 0));
168 #endif                  
169
170                 oa->data[resultPos] = element;
171         }
172
173         return oa;
174 }
175
176
177 /*
178  * Class:     java/lang/VMThrowable
179  * Method:    getStackTrace
180  * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
181  */
182 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *t)
183 {
184 #if defined(__ALPHA__) || defined(__I386__) || defined(__POWERPC__) || defined(__X86_64__)
185         stackTraceBuffer  *buf;
186         stacktraceelement *elem;
187         stacktraceelement *tmpelem;
188         u8                 size;
189         s4                 elemcount;
190         classinfo         *c;
191         bool               inexceptionclass;
192         bool               leftexceptionclass;
193
194         buf = (stackTraceBuffer *) this->vmData;
195         c = t->header.vftbl->class;
196
197         if (!buf) {
198                 log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code");
199                 assert(0);
200         }
201         
202         size = buf->full;
203
204         if (size < 2) {
205                 log_text("Invalid java.lang.VMThrowable.vmData field in java.lang.VMThrowable.getStackTrace native code (length<2)");
206                 assert(0);
207         }
208
209         /* skip first 2 elements in stacktrace buffer:                            */
210         /*   0: VMThrowable.fillInStackTrace                                      */
211         /*   1: Throwable.fillInStackTrace                                        */
212
213         elem = &(buf->start[2]);
214         size -= 2;
215
216         if (size && elem->method != 0) {
217                 /* not a builtin native wrapper*/
218
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.                                 */
225
226                         inexceptionclass = false;
227                         leftexceptionclass = false;
228
229                         while (size > 0) {
230                                 /* check if we are in the exception class */
231
232                                 if (elem->method->class == c)
233                                         inexceptionclass = true;
234
235                                 /* check if we left the exception class */
236
237                                 if (inexceptionclass && (elem->method->class != c))
238                                         leftexceptionclass = true;
239
240                                 /* found exception start point if we left the initalizers or  */
241                                 /* we left the exception class                                */
242
243                                 if ((elem->method->name != utf_init) || leftexceptionclass)
244                                         break;
245
246                                 elem++;
247                                 size--;
248                         }
249                 }
250         }
251         
252         for (elemcount = 0, tmpelem = elem; size > 0; size--, tmpelem++) {
253                 if (tmpelem->method)
254                         elemcount++;
255         }
256
257         return generateStackTraceArray(env, elem, elemcount);
258 #else
259         return 0;
260 #endif
261 }
262
263
264 /*
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  * ---------------------------------------------------------------------
269  * Local variables:
270  * mode: c
271  * indent-tabs-mode: t
272  * c-basic-offset: 4
273  * tab-width: 4
274  * End:
275  */