- exception stuff
[cacao.git] / src / native / vm / VMThrowable.c
1 /* nat/VMThrowable.c - java/lang/Throwable
2
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
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    $Id: VMThrowable.c 1042 2004-04-26 17:12:47Z twisti $
30
31 */
32
33
34 #include "global.h"
35 #include "jni.h"
36 #include "asmpart.h"
37 #include "loader.h"
38 #include "builtin.h"
39 #include "tables.h"
40 #include "native.h"
41 #include "java_lang_Throwable.h"
42 #include "java_lang_VMThrowable.h"
43
44
45 /*
46  * Class:     java/lang/VMThrowable
47  * Method:    fillInStackTrace
48  * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
49  */
50 JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, java_lang_Throwable *par1)
51 {
52         classinfo *class_java_lang_VMThrowable = NULL;
53         java_lang_VMThrowable *vmthrow;
54
55         if (!class_java_lang_VMThrowable)
56                 class_java_lang_VMThrowable = class_new(utf_new_char("java/lang/VMThrowable"));
57
58         if (class_java_lang_VMThrowable == NULL)
59                 panic("Needed class java.lang.VMThrowable missing");
60
61         vmthrow = (java_lang_VMThrowable *) native_new_and_init(class_java_lang_VMThrowable);
62
63         if (vmthrow == NULL)
64                 panic("Needed instance of class  java.lang.VMThrowable could not be created");
65
66 #if defined(__I386__)
67         (void) asm_get_stackTrace(&(vmthrow->vmData));
68 #else
69         vmthrow->vmData=0;
70 #endif
71
72         return vmthrow;
73 }
74
75
76
77 java_objectarray* generateStackTraceArray(JNIEnv *env,stacktraceelement *source,long size)
78 {
79         long resultPos;
80         methodinfo *m;
81         classinfo *c;
82         java_objectarray *oa;
83
84         c = class_new(utf_new_char("java/lang/StackTraceElement"));
85
86         if (!c->loaded)
87                 class_load(c);
88
89         if (!c->linked)
90                 class_link(c);
91
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"));
95
96         if (!m)
97                 panic("java.lang.StackTraceElement misses needed constructor"); 
98
99         oa = builtin_anewarray(size, c);
100
101         if (!oa)
102                 return 0;
103
104 /*      printf("Should return an array with %ld element(s)\n",size);*/
105         size--;
106         
107         
108         for(resultPos=0;size>=0;resultPos++,size--) {
109                 java_objectheader *element=builtin_new(c);
110                 if (!element) {
111                         panic("Memory for stack trace element could not be allocated");
112                 }
113 #ifdef __GNUC__
114 #warning call constructor once jni is fixed to allow more than three parameters
115 #endif
116 #if 0
117                 (*env)->CallVoidMethod(env,element,m,
118                         javastring_new(source[size].method->class->sourcefile),
119                         source[size].linenumber,
120                         javastring_new(source[size].method->class->name),
121                         javastring_new(source[size].method->name),
122                         source[size].method->flags & ACC_NATIVE);
123 #else
124                 if (!(source[size].method->flags & ACC_NATIVE))setfield_critical(c,element,"fileName",          
125                 "Ljava/lang/String;",  jobject, 
126                 (jobject) javastring_new(source[size].method->class->sourcefile));
127                 setfield_critical(c,element,"className",          "Ljava/lang/String;",  jobject, 
128                 (jobject) javastring_new(source[size].method->class->name));
129                 setfield_critical(c,element,"methodName",          "Ljava/lang/String;",  jobject, 
130                 (jobject) javastring_new(source[size].method->name));
131                 setfield_critical(c,element,"lineNumber",          "I",  jint, 
132                 (jint) ((source[size].method->flags & ACC_NATIVE) ? -1:(source[size].linenumber)));
133                 setfield_critical(c,element,"isNative",          "Z",  jboolean, 
134                 (jboolean) ((source[size].method->flags & ACC_NATIVE) ? 1:0));
135
136
137 #endif                  
138
139                 oa->data[resultPos]=element;
140         }
141
142         return oa;
143
144 }
145
146
147 /*
148  * Class:     java/lang/VMThrowable
149  * Method:    getStackTrace
150  * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
151  */
152 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *par1)
153 {
154         long  pos;
155         long  maxpos;
156         utf*  classname=par1->header.vftbl->class->name;
157         utf*  init=utf_new_char("<init>");
158         utf*  throwable=utf_new_char("java/lang/Throwable");
159         stacktraceelement *el=(stacktraceelement*)this->vmData;
160
161         /*      log_text("Java_java_lang_VMThrowable_getStackTrace");
162         utf_display(par1->header.vftbl->class->name);
163         printf("\n----------------------------------------------\n");*/
164
165         if (el == 0) {
166                 return generateStackTraceArray(env, el, 0);
167         }       
168
169         for (pos = 0; el[pos].method != 0; pos++);
170
171         if (pos == 0) {
172                 panic("Stacktrace cannot have zero length");
173         }
174
175         pos--;
176         pos--;
177         maxpos = pos;
178         if (el[pos].method->class->name == throwable && el[pos].method->name == init) {
179                 for (; pos >= 0 && el[pos].method->name == init && el[pos].method->class->name != classname; pos--);
180                 pos--;
181                 if (pos < 0) {
182                         log_text("Invalid stack trace for Throwable.getStackTrace()");
183                 }
184         }
185         
186         /* build the result array*/
187         pos++; /*arraysize*/
188
189         return generateStackTraceArray(env,el,pos);     
190 }
191
192
193 /*
194  * These are local overrides for various environment variables in Emacs.
195  * Please do not remove this and leave it at the end of the file, where
196  * Emacs will automagically detect them.
197  * ---------------------------------------------------------------------
198  * Local variables:
199  * mode: c
200  * indent-tabs-mode: t
201  * c-basic-offset: 4
202  * tab-width: 4
203  * End:
204  */