1 // natVMMethod.cc -- native support for VMMethod
3 /* Copyright (C) 2006, 2007 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 #include <java-interp.h>
15 #include "jvmti-int.h"
17 #include <java/lang/reflect/Modifier.h>
18 #include <gnu/classpath/jdwp/VMMethod.h>
19 #include <gnu/classpath/jdwp/exception/AbsentInformationException.h>
20 #include <gnu/classpath/jdwp/exception/InvalidMethodException.h>
21 #include <gnu/classpath/jdwp/exception/JdwpInternalErrorException.h>
22 #include <gnu/classpath/jdwp/util/LineTable.h>
23 #include <gnu/classpath/jdwp/util/VariableTable.h>
25 using namespace java::lang;
27 #define CHECK_INTERP_CLASS() \
30 if (!_Jv_IsInterpretedClass (getDeclaringClass ())) \
32 ::java::lang::String *msg = JvNewStringLatin1 ("native class"); \
33 throw new exception::JdwpInternalErrorException (msg); \
39 gnu::classpath::jdwp::VMMethod::getName ()
41 jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
42 jmethodID method = reinterpret_cast<jmethodID> (_methodId);
44 env->GetMethodName (method, &name, NULL, NULL);
45 jstring string = JvNewStringUTF (name);
46 env->Deallocate (reinterpret_cast<unsigned char *> (name));
51 gnu::classpath::jdwp::VMMethod::getSignature ()
53 jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
54 jmethodID method = reinterpret_cast<jmethodID> (_methodId);
56 env->GetMethodName (method, NULL, &signature, NULL);
57 jstring string = JvNewStringUTF (signature);
58 env->Deallocate (reinterpret_cast<unsigned char *> (signature));
63 gnu::classpath::jdwp::VMMethod::getModifiers ()
65 jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
66 jmethodID method = reinterpret_cast<jmethodID> (_methodId);
68 env->GetMethodModifiers (method, &flags);
70 // If this class is compiled, as far as JDWP is concerned, its methods are
71 // native. This will set the native flag for these methods.
72 if (!_Jv_IsInterpretedClass (getDeclaringClass ()))
73 flags |= ::java::lang::reflect::Modifier::NATIVE;
78 gnu::classpath::jdwp::util::LineTable *
79 gnu::classpath::jdwp::VMMethod::getLineTable ()
81 CHECK_INTERP_CLASS ();
83 jmethodID desired_method = reinterpret_cast<jmethodID> (_methodId);
85 _Jv_MethodBase *theMethod
86 = _Jv_FindInterpreterMethod (getDeclaringClass (), desired_method);
88 if (theMethod == NULL)
90 // this should not happen
91 ::java::lang::String *msg
92 = JvNewStringLatin1 ("could not find method in class");
93 throw new exception::JdwpInternalErrorException (msg);
96 if (::java::lang::reflect::Modifier::isNative (desired_method->accflags))
98 jintArray lines = JvNewIntArray (0);
99 jlongArray indices = JvNewLongArray (0);
100 return new util::LineTable (-1, -1, lines, indices);
104 _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (theMethod);
109 imeth->get_line_table (start, end, lines, indices);
110 return new util::LineTable (start, end, lines, indices);
114 gnu::classpath::jdwp::util::VariableTable*
115 gnu::classpath::jdwp::VMMethod::getVariableTable ()
117 using namespace gnu::classpath::jdwp::util;
119 jvmtiEnv *env = _Jv_GetJDWP_JVMTIEnv ();
121 CHECK_INTERP_CLASS ();
123 jmethodID meth = reinterpret_cast<jmethodID> (_methodId);
124 jvmtiLocalVariableEntry *var_table;
125 jint num_slots, args_len;
127 jvmtiError jerr = env->GetLocalVariableTable (meth, &num_slots, &var_table);
129 if (jerr != JVMTI_ERROR_NONE)
132 jerr = env->GetArgumentsSize (meth, &args_len);
134 if (jerr != JVMTI_ERROR_NONE)
137 using namespace gnu::classpath::jdwp::exception;
139 env->GetErrorName (jerr, &error);
140 String *msg = JvNewStringUTF (error);
141 env->Deallocate (reinterpret_cast<unsigned char *> (error));
143 if (jerr == JVMTI_ERROR_NATIVE_METHOD)
144 throw new AbsentInformationException (msg);
145 else if (jerr == JVMTI_ERROR_INVALID_METHODID)
146 throw new InvalidMethodException (_methodId);
148 throw new JdwpInternalErrorException (msg);
151 jlongArray start_pcs = JvNewLongArray (num_slots);
152 jlong *start_pcs_ptr = elements (start_pcs);
153 jintArray lengths = JvNewIntArray (num_slots);
154 jint *lengths_ptr = elements (lengths);
155 jintArray slots = JvNewIntArray (num_slots);
156 jint *slots_ptr = elements (slots);
157 JArray<String *> *names = reinterpret_cast<JArray<String *> *>
158 (JvNewObjectArray (num_slots,
159 &String::class$, NULL));
160 jstring *names_ptr = elements (names);
161 JArray<String *> *signatures = reinterpret_cast<JArray<String *> *>
162 (JvNewObjectArray (num_slots,
163 &String::class$, NULL));
164 jstring *signatures_ptr = elements (signatures);
166 // Get the information out of the JVMTI strucutre and Deallocate the strings.
167 for (int i = 0; i < num_slots; i++)
169 start_pcs_ptr[i] = var_table[i].start_location;
170 lengths_ptr[i] = var_table[i].length;
171 slots_ptr[i] = var_table[i].slot;
172 names_ptr[i] = JvNewStringUTF (var_table[i].name);
173 env->Deallocate (reinterpret_cast<unsigned char *>
174 (var_table[i].name));
175 signatures_ptr[i] = JvNewStringUTF (var_table[i].signature);
176 env->Deallocate (reinterpret_cast<unsigned char *>
177 (var_table[i].signature));
178 env->Deallocate (reinterpret_cast<unsigned char *>
179 (var_table[i].generic_signature));
182 // Now Deallocate the table since it's strings have already been freed.
183 env->Deallocate (reinterpret_cast<unsigned char *> (var_table));
185 // Create the new JDWP VariableTable to return with the now filled arrays.
186 VariableTable* jdwp_vtable = new VariableTable (args_len, num_slots,
187 start_pcs, names, signatures,