3cbed421ebc512173623e5482c47f8f659c140de
[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 1082 2004-05-26 15:04:54Z jowenn $
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 pos,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         pos--;
106         
107         
108         for(resultPos=0;pos>=0;resultPos++,pos--) {
109
110                 if (source[pos].method==0) {
111                         resultPos--;
112                         continue;
113                 }
114
115                 java_objectheader *element=builtin_new(c);
116                 if (!element) {
117                         panic("Memory for stack trace element could not be allocated");
118                 }
119 #ifdef __GNUC__
120 #warning call constructor once jni is fixed to allow more than three parameters
121 #endif
122 #if 0
123                 (*env)->CallVoidMethod(env,element,m,
124                         javastring_new(source[pos].method->class->sourcefile),
125                         source[size].linenumber,
126                         javastring_new(source[pos].method->class->name),
127                         javastring_new(source[pos].method->name),
128                         source[pos].method->flags & ACC_NATIVE);
129 #else
130                 if (!(source[pos].method->flags & ACC_NATIVE))setfield_critical(c,element,"fileName",          
131                 "Ljava/lang/String;",  jobject, 
132                 (jobject) javastring_new(source[pos].method->class->sourcefile));
133                 setfield_critical(c,element,"className",          "Ljava/lang/String;",  jobject, 
134                 (jobject) javastring_new(source[pos].method->class->name));
135                 setfield_critical(c,element,"methodName",          "Ljava/lang/String;",  jobject, 
136                 (jobject) javastring_new(source[pos].method->name));
137                 setfield_critical(c,element,"lineNumber",          "I",  jint, 
138                 (jint) ((source[pos].method->flags & ACC_NATIVE) ? -1:(source[pos].linenumber)));
139                 setfield_critical(c,element,"isNative",          "Z",  jboolean, 
140                 (jboolean) ((source[pos].method->flags & ACC_NATIVE) ? 1:0));
141
142
143 #endif                  
144
145                 oa->data[resultPos]=element;
146         }
147
148         return oa;
149
150 }
151
152
153 /*
154  * Class:     java/lang/VMThrowable
155  * Method:    getStackTrace
156  * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
157  */
158 JNIEXPORT java_objectarray* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *par1)
159 {
160         long  pos;
161         long  maxpos;
162         long  sizediff;
163         utf*  classname=par1->header.vftbl->class->name;
164         utf*  init=utf_new_char("<init>");
165         utf*  throwable=utf_new_char("java/lang/Throwable");
166         stacktraceelement *el=(stacktraceelement*)this->vmData;
167
168         /*      log_text("Java_java_lang_VMThrowable_getStackTrace");
169         utf_display(par1->header.vftbl->class->name);
170         printf("\n----------------------------------------------\n");*/
171
172         sizediff=0;
173         if (el == 0) {
174                 return generateStackTraceArray(env, el, 0,0);
175         }       
176
177         for (pos = 0; !((el[pos].method == 0) && (el[pos].linenumber ==-1)); pos++) {
178                 if (el[pos].method==0) sizediff++;
179         }
180
181         if (pos == 0) {
182                 panic("Stacktrace cannot have zero length");
183         }
184
185         pos--;
186         pos--;
187         maxpos = pos;
188         if (el[pos].method->class->name == throwable && el[pos].method->name == init) {
189                 for (; pos >= 0 && el[pos].method->name == init && el[pos].method->class->name != classname; pos--);
190                 pos--;
191                 if (pos < 0) {
192                         log_text("Invalid stack trace for Throwable.getStackTrace()");
193                 }
194         }
195         
196         /* build the result array*/
197         pos++; /*arraysize*/
198
199         return generateStackTraceArray(env,el,pos,pos-sizediff);        
200 }
201
202
203 /*
204  * These are local overrides for various environment variables in Emacs.
205  * Please do not remove this and leave it at the end of the file, where
206  * Emacs will automagically detect them.
207  * ---------------------------------------------------------------------
208  * Local variables:
209  * mode: c
210  * indent-tabs-mode: t
211  * c-basic-offset: 4
212  * tab-width: 4
213  * End:
214  */