* src/vm/jit/i386/darwin/md-os.c (md_replace_executionstate_read):
[cacao.git] / src / native / vm / gnu / java_lang_VMThrowable.c
1 /* src/native/vm/gnu/java_lang_VMThrowable.c
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29
30 #include "vm/types.h"
31
32 #include "native/jni.h"
33 #include "native/llni.h"
34 #include "native/native.h"
35
36 #include "native/include/gnu_classpath_Pointer.h"
37 #include "native/include/java_lang_Class.h"
38 #include "native/include/java_lang_StackTraceElement.h"
39 #include "native/include/java_lang_Throwable.h"
40
41 #include "native/include/java_lang_VMThrowable.h"
42
43 #include "native/vm/java_lang_Class.h"
44
45 #include "vm/array.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/stringlocal.h"
49
50 #include "vm/jit/code.h"
51 #include "vm/jit/linenumbertable.h"
52 #include "vm/jit/stacktrace.h"
53
54 #include "vmcore/class.h"
55 #include "vmcore/loader.h"
56
57
58 /* native methods implemented by this file ************************************/
59
60 static JNINativeMethod methods[] = {
61         { "fillInStackTrace", "(Ljava/lang/Throwable;)Ljava/lang/VMThrowable;",        (void *) (ptrint) &Java_java_lang_VMThrowable_fillInStackTrace },
62         { "getStackTrace",    "(Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;", (void *) (ptrint) &Java_java_lang_VMThrowable_getStackTrace    },
63 };
64
65
66 /* _Jv_java_lang_VMThrowable_init **********************************************
67
68    Register native functions.
69
70 *******************************************************************************/
71
72 void _Jv_java_lang_VMThrowable_init(void)
73 {
74         utf *u;
75
76         u = utf_new_char("java/lang/VMThrowable");
77
78         native_method_register(u, methods, NATIVE_METHODS_COUNT);
79 }
80
81
82 /*
83  * Class:     java/lang/VMThrowable
84  * Method:    fillInStackTrace
85  * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
86  */
87 JNIEXPORT java_lang_VMThrowable* JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, java_lang_Throwable *t)
88 {
89         java_lang_VMThrowable   *o;
90         java_handle_bytearray_t *ba;
91
92         o = (java_lang_VMThrowable *)
93                 native_new_and_init(class_java_lang_VMThrowable);
94
95         if (o == NULL)
96                 return NULL;
97
98         ba = stacktrace_get_current();
99
100         if (ba == NULL)
101                 return NULL;
102
103         LLNI_field_set_ref(o, vmData, (gnu_classpath_Pointer *) ba);
104
105         return o;
106 }
107
108
109 /*
110  * Class:     java/lang/VMThrowable
111  * Method:    getStackTrace
112  * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
113  */
114 JNIEXPORT java_handle_objectarray_t* JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, java_lang_VMThrowable *this, java_lang_Throwable *t)
115 {
116         java_handle_bytearray_t     *ba;
117         stacktrace_t                *st;
118         stacktrace_entry_t          *ste;
119         java_handle_objectarray_t   *oa;
120         java_lang_StackTraceElement *o;
121         codeinfo                    *code;
122         methodinfo                  *m;
123         java_lang_String            *filename;
124         s4                           linenumber;
125         java_lang_String            *declaringclass;
126         int                          i;
127
128         /* get the stacktrace buffer from the VMThrowable object */
129
130         LLNI_field_get_ref(this, vmData, ba);
131
132         st = (stacktrace_t *) LLNI_array_data(ba);
133
134         assert(st != NULL);
135
136         ste = st->entries;
137
138         /* Create the stacktrace element array. */
139
140         oa = builtin_anewarray(st->length, class_java_lang_StackTraceElement);
141
142         if (oa == NULL)
143                 return NULL;
144
145         for (i = 0; i < st->length; i++, ste++) {
146                 /* allocate a new stacktrace element */
147
148                 o = (java_lang_StackTraceElement *)
149                         builtin_new(class_java_lang_StackTraceElement);
150
151                 if (o == NULL)
152                         return NULL;
153
154                 /* Get the codeinfo and methodinfo. */
155
156                 code = ste->code;
157                 m    = code->m;
158
159                 /* Get filename. */
160
161                 if (!(m->flags & ACC_NATIVE)) {
162                         if (m->class->sourcefile)
163                                 filename = (java_lang_String *) javastring_new(m->class->sourcefile);
164                         else
165                                 filename = NULL;
166                 }
167                 else
168                         filename = NULL;
169
170                 /* get line number */
171
172                 if (m->flags & ACC_NATIVE) {
173                         linenumber = -1;
174                 }
175                 else {
176                         /* FIXME The linenumbertable_linenumber_for_pc could
177                            change the methodinfo pointer when hitting an inlined
178                            method. */
179
180                         linenumber = linenumbertable_linenumber_for_pc(&m, code, ste->pc);
181                         linenumber = (linenumber == 0) ? -1 : linenumber;
182                 }
183
184                 /* get declaring class name */
185
186                 declaringclass =
187                         _Jv_java_lang_Class_getName(LLNI_classinfo_wrap(m->class));
188
189                 /* Fill the java.lang.StackTraceElement object. */
190
191                 LLNI_field_set_ref(o, fileName      , filename);
192                 LLNI_field_set_val(o, lineNumber    , linenumber);
193                 LLNI_field_set_ref(o, declaringClass, declaringclass);
194                 LLNI_field_set_ref(o, methodName    , (java_lang_String *) javastring_new(m->name));
195                 LLNI_field_set_val(o, isNative      , (m->flags & ACC_NATIVE) ? 1 : 0);
196
197                 array_objectarray_element_set(oa, i, (java_handle_t *) o);
198         }
199
200         return oa;
201 }
202
203
204 /*
205  * These are local overrides for various environment variables in Emacs.
206  * Please do not remove this and leave it at the end of the file, where
207  * Emacs will automagically detect them.
208  * ---------------------------------------------------------------------
209  * Local variables:
210  * mode: c
211  * indent-tabs-mode: t
212  * c-basic-offset: 4
213  * tab-width: 4
214  * End:
215  */