1 /* src/native/vm/gnuclasspath/java_lang_VMThrowable.cpp
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
33 #include "native/jni.hpp"
34 #include "native/llni.h"
35 #include "native/native.hpp"
37 #if defined(ENABLE_JNI_HEADERS)
38 # include "native/vm/include/java_lang_VMThrowable.h"
41 #include "vm/array.hpp"
42 #include "vm/jit/builtin.hpp"
43 #include "vm/exceptions.hpp"
44 #include "vm/globals.hpp"
45 #include "vm/javaobjects.hpp"
46 #include "vm/loader.hpp"
47 #include "vm/string.hpp"
49 #include "vm/jit/code.hpp"
50 #include "vm/jit/linenumbertable.hpp"
51 #include "vm/jit/stacktrace.hpp"
55 // Native functions are exported as C functions.
59 * Class: java/lang/VMThrowable
60 * Method: fillInStackTrace
61 * Signature: (Ljava/lang/Throwable;)Ljava/lang/VMThrowable;
63 JNIEXPORT jobject JNICALL Java_java_lang_VMThrowable_fillInStackTrace(JNIEnv *env, jclass clazz, jobject t)
66 java_handle_bytearray_t* ba;
68 h = native_new_and_init(class_java_lang_VMThrowable);
73 java_lang_VMThrowable vmt(h);
75 ba = stacktrace_get_current();
82 return (jobject) vmt.get_handle();
87 * Class: java/lang/VMThrowable
88 * Method: getStackTrace
89 * Signature: (Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;
91 JNIEXPORT jobjectArray JNICALL Java_java_lang_VMThrowable_getStackTrace(JNIEnv *env, jobject _this, jobject t)
93 java_lang_VMThrowable vmt(_this);
95 // Get the stacktrace from the VMThrowable object.
97 java_handle_bytearray_t* ba = vmt.get_vmdata();
99 // XXX Critical GC section?
100 stacktrace_t* st = (stacktrace_t*) LLNI_array_data(ba);
104 stacktrace_entry_t* ste = st->entries;
106 /* Create the stacktrace element array. */
108 java_handle_objectarray_t* oa = builtin_anewarray(st->length, class_java_lang_StackTraceElement);
113 for (int i = 0; i < st->length; i++, ste++) {
114 /* Get the codeinfo and methodinfo. */
116 codeinfo* code = ste->code;
117 methodinfo* m = code->m;
121 java_handle_t* filename;
123 if (!(m->flags & ACC_NATIVE)) {
124 if (m->clazz->sourcefile)
125 filename = javastring_new(m->clazz->sourcefile);
132 /* get line number */
136 if (m->flags & ACC_NATIVE) {
140 /* FIXME linenumbertable->find could change the methodinfo
141 pointer when hitting an inlined method. */
143 linenumber = code->linenumbertable->find(&m, ste->pc);
144 linenumber = (linenumber == 0) ? -1 : linenumber;
147 /* get declaring class name */
149 java_handle_t* declaringclass = class_get_classname(m->clazz);
151 /* allocate a new stacktrace element */
153 java_handle_t* h = builtin_new(class_java_lang_StackTraceElement);
158 java_lang_StackTraceElement ste(h, filename, linenumber, declaringclass, javastring_new(m->name), ((m->flags & ACC_NATIVE) ? 1 : 0));
160 array_objectarray_element_set(oa, i, ste.get_handle());
163 return (jobjectArray) oa;
169 /* native methods implemented by this file ************************************/
171 static JNINativeMethod methods[] = {
172 { (char*) "fillInStackTrace", (char*) "(Ljava/lang/Throwable;)Ljava/lang/VMThrowable;", (void*) (uintptr_t) &Java_java_lang_VMThrowable_fillInStackTrace },
173 { (char*) "getStackTrace", (char*) "(Ljava/lang/Throwable;)[Ljava/lang/StackTraceElement;", (void*) (uintptr_t) &Java_java_lang_VMThrowable_getStackTrace },
177 /* _Jv_java_lang_VMThrowable_init **********************************************
179 Register native functions.
181 *******************************************************************************/
183 void _Jv_java_lang_VMThrowable_init(void)
185 utf* u = utf_new_char("java/lang/VMThrowable");
187 NativeMethods& nm = VM::get_current()->get_nativemethods();
188 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
193 * These are local overrides for various environment variables in Emacs.
194 * Please do not remove this and leave it at the end of the file, where
195 * Emacs will automagically detect them.
196 * ---------------------------------------------------------------------
199 * indent-tabs-mode: t