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