1 /* src/native/vm/VMVirtualMachine.c - jdwp->jvmti interface
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Martin Platter
29 Changes: Samuel Vinson
32 $Id: gnu_classpath_jdwp_VMVirtualMachine.c 6213 2006-12-18 17:36:06Z twisti $
36 #include "toolbox/logging.h"
37 #include "native/jni.h"
38 #include "native/include/java_lang_Thread.h"
39 #include "native/include/java_nio_ByteBuffer.h"
40 #include "native/include/java_lang_Class.h"
41 #include "native/include/java_lang_ClassLoader.h"
42 #include "native/include/java_lang_reflect_Method.h"
43 #include "native/include/gnu_classpath_jdwp_event_EventRequest.h"
44 #include "native/include/gnu_classpath_jdwp_VMVirtualMachine.h"
45 #include "native/jvmti/jvmti.h"
46 #include "native/jvmti/VMjdwp.h"
51 * Class: gnu_classpath_jdwp_VMVirtualMachine
52 * Method: suspendThread
53 * Signature: (Ljava/lang/Thread;)V
55 JNIEXPORT void JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_suspendThread(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1)
59 err = (*jvmtienv)->SuspendThread(jvmtienv, (jthread) par1);
60 printjvmtierror("VMVirtualMachine.suspendThread SuspendThread", err);
64 * Class: gnu_classpath_jdwp_VMVirtualMachine
65 * Method: resumeThread
66 * Signature: (Ljava/lang/Thread;)V
68 JNIEXPORT void JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_resumeThread(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1)
72 err = (*jvmtienv)->ResumeThread(jvmtienv, (jthread) par1);
73 printjvmtierror("VMVirtualMachine.resumethread ResumeThread", err);
78 * Class: gnu_classpath_jdwp_VMVirtualMachine
79 * Method: getSuspendCount
80 * Signature: (Ljava/lang/Thread;)I
82 JNIEXPORT s4 JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getSuspendCount(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1) {
83 log_text ("VMVirtualMachine_getSuspendCount: not supported");
88 * Class: gnu_classpath_jdwp_VMVirtualMachine
89 * Method: getAllLoadedClassesCount
92 JNIEXPORT s4 JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClassesCount(JNIEnv *env, jclass clazz) {
97 if (JVMTI_ERROR_NONE != (err= (*jvmtienv)->
98 GetLoadedClasses(jvmtienv, &count, &classes))) {
99 printjvmtierror("VMVirtualMachine_getAllLoadedClassCount GetLoadedClasses",err);
102 (*jvmtienv)->Deallocate(jvmtienv,(unsigned char*)classes);
107 * Class: gnu_classpath_jdwp_VMVirtualMachine
108 * Method: getAllLoadedClasses
109 * Signature: ()Ljava/util/Iterator
111 JNIEXPORT struct java_util_Iterator* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getAllLoadedClasses(JNIEnv *env, jclass clazz) {
112 jclass *classes, *cl;
121 if (JVMTI_ERROR_NONE != (err= (*jvmtienv)->
122 GetLoadedClasses(jvmtienv, &classcount, &classes))) {
123 printjvmtierror("VMVirtualMachine_getAllLoadedClasses GetLoadedClasses",err);
125 /* we should throw JDWP Exception INTERNAL = 113;*/
126 /* env->ThrowNew(env,ec,"jvmti error occoured"); */
130 cl = (*env)->FindClass(env,"java.lang.Class");
131 if (!cl) return NULL;
133 /* Arrays.asList(Object[] classes)->List.Iterator()->Iterator */
134 joa = (*env)->NewObjectArray(env, (jsize)classcount, cl , NULL);
135 if (!joa) return NULL;
137 for (i = 0; i < classcount; i++)
138 (*env)->SetObjectArrayElement(env,joa,(jsize)i, (jobject)classes[i]);
139 (*jvmtienv)->Deallocate(jvmtienv, (unsigned char*)classes);
141 cl = (*env)->FindClass(env,"java.util.Arrays");
142 if (!cl) return NULL;
144 m = (*env)->GetStaticMethodID(env, cl, "asList", "([Ljava/lang/Object;)Ljava/util/List;");
147 ol = (*env)->CallStaticObjectMethod(env,(jclass)cl,m,joa);
148 if (!ol) return NULL;
150 cl = (*env)->FindClass(env,"java.util.List");
151 if (!cl) return NULL;
152 m = (*env)->GetMethodID(env,cl,"iterator","()Ljava/util/Iterator;");
154 oi = (*env)->CallObjectMethod(env,ol,m);
156 return (struct java_util_Iterator*)oi;
159 /* Class: gnu/classpath/jdwp/VMVirtualMachine
160 * Method: getClassStatus
161 * Signature: (Ljava/lang/Class;)I
163 JNIEXPORT s4 JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getClassStatus(JNIEnv *env, jclass clazz, struct java_lang_Class* par1) {
167 err = (*jvmtienv)->GetClassStatus(jvmtienv, (jclass) par1, &status);
168 printjvmtierror("VMVirtualMachine_getClassStatus GetClassStatus", err);
174 * Class: gnu/classpath/jdwp/VMVirtualMachine
175 * Method: getAllClassMethods
176 * Signature: (Ljava/lang/Class;)[Lgnu/classpath/jdwp/VMMethod;
178 JNIEXPORT java_objectarray* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getAllClassMethods(JNIEnv *env, jclass clazz, struct java_lang_Class* par1) {
180 jmethodID* methodID, m;
188 if (JVMTI_ERROR_NONE != (err= (*jvmtienv)->
189 GetClassMethods(jvmtienv, (jclass) par1,
190 &count, &methodID))) {
191 printjvmtierror("VMVirtualMachine_getAllClassMethods GetClassMethods", err);
196 GetStaticMethodID(env, clazz, "getClassMethod",
197 "(Ljava/lang/Class;J)Lgnu/classpath/jdwp/VMMethod;");
200 cl = (*env)->FindClass(env,"gnu.classpath.jdwp.VMMethod");
201 if (!cl) return NULL;
203 joa = (*env)->NewObjectArray(env, (jsize)count, cl , NULL);
204 if (!joa) return NULL;
206 for (i = 0; i < count; i++) {
208 CallStaticObjectMethod(env,clazz,m,(jobject)par1, methodID[i]);
209 if (!ol) return NULL;
210 (*env)->SetObjectArrayElement(env,joa,(jsize)i, ol);
217 * Class: gnu/classpath/jdwp/VMVirtualMachine
218 * Method: getClassMethod
219 * Signature: (Ljava/lang/Class;J)Lgnu/classpath/jdwp/VMMethod;
221 JNIEXPORT struct gnu_classpath_jdwp_VMMethod* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getClassMethod(JNIEnv *env, jclass clazz, struct java_lang_Class* par1, s8 par2) {
226 cl = (*env)->FindClass(env,"gnu.classpath.jdwp.VMMethod");
227 if (!cl) return NULL;
229 m = (*env)->GetMethodID(env, cl, "<init>", "(Ljava/lang/Class;J)V");
232 ol = (*env)->NewObject(env, cl, m, par1, par2);
234 return (struct gnu_classpath_jdwp_VMMethod*)ol;
239 * Class: gnu/classpath/jdwp/VMVirtualMachine
241 * Signature: (Ljava/lang/Thread;II)Ljava/util/ArrayList;
243 JNIEXPORT struct java_util_ArrayList* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getFrames(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1, s4 par2, s4 par3) {
244 log_text ("VMVirtualMachine_getFrames - IMPLEMENT ME!!!");
245 /* jclass ec = (*env)->FindClass(env,"gnu/classpath/jdwp/JdwpInternalErrorException");
246 if (JVMTI_ERROR_NONE != (*jvmtienv)->GetClassStatus(jvmtienv, par1, &status))
247 env->ThrowNew(env,ec,"jvmti error occoured");*/
253 * Class: gnu/classpath/jdwp/VMVirtualMachine
255 * Signature: (Ljava/lang/Thread;Ljava/nio/ByteBuffer;)Lgnu/classpath/jdwp/VMFrame;
257 JNIEXPORT struct gnu_classpath_jdwp_VMFrame* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getFrame(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1, struct java_nio_ByteBuffer* par2) {
258 log_text ("VMVirtualMachine_getFrame - IMPLEMENT ME!!!");
264 * Class: gnu/classpath/jdwp/VMVirtualMachine
265 * Method: getFrameCount
266 * Signature: (Ljava/lang/Thread;)I
268 JNIEXPORT s4 JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getFrameCount(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1) {
271 err = (*jvmtienv)->GetFrameCount(jvmtienv, (jthread)par1, &count);
272 printjvmtierror("VMVirtualMachine_getFrameCount GetFrameCount", err);
278 * Class: gnu/classpath/jdwp/VMVirtualMachine
279 * Method: getThreadStatus
280 * Signature: (Ljava/lang/Thread;)I
282 JNIEXPORT s4 JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getThreadStatus(JNIEnv *env, jclass clazz, struct java_lang_Thread* par1) {
285 if (JVMTI_ERROR_NONE != (err = (*jvmtienv)->GetThreadState(jvmtienv, (jthread)par1, &status))) {
286 printjvmtierror("VMVirtualMachine_getThreadStatus GetThreadState", err);
289 if (status && JVMTI_THREAD_STATE_ALIVE) {
290 if (status && JVMTI_THREAD_STATE_WAITING) {
291 return 4; /* WAIT - see JdwpConstants */
293 if (status && JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) {
294 return 3; /* MONITOR - see JdwpConstants */
296 if (status && JVMTI_THREAD_STATE_SLEEPING) {
297 return 2; /* SLEEPING - see JdwpConstants */
299 return 1; /* RUNNING - see JdwpConstants */
301 return 0; /* ZOMBIE - see JdwpConstants */
302 return -1; /* some error */
307 * Class: gnu/classpath/jdwp/VMVirtualMachine
308 * Method: getLoadRequests
309 * Signature: (Ljava/lang/ClassLoader;)Ljava/util/ArrayList;
311 JNIEXPORT struct java_util_ArrayList* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getLoadRequests(JNIEnv *env, jclass clazz, struct java_lang_ClassLoader* par1) {
312 log_text ("VMVirtualMachine_getLoadRequests(");
318 * Class: gnu/classpath/jdwp/VMVirtualMachine
319 * Method: executeMethod
320 * Signature: (Ljava/lang/Object;Ljava/lang/Thread;Ljava/lang/Class;Ljava/lang/reflect/Method;[Ljava/lang/Object;Z)Lgnu/classpath/jdwp/util/MethodResult;
322 JNIEXPORT struct gnu_classpath_jdwp_util_MethodResult* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_executeMethod(JNIEnv *env, jclass clazz, struct java_lang_Object* par1, struct java_lang_Thread* par2, struct java_lang_Class* par3, struct java_lang_reflect_Method* par4, java_objectarray* par5, s4 par6) {
323 log_text ("VMVirtualMachine_executeMethod");
329 * Class: gnu/classpath/jdwp/VMVirtualMachine
330 * Method: getSourceFile
331 * Signature: (Ljava/lang/Class;)Ljava/lang/String;
333 JNIEXPORT struct java_lang_String* JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_getSourceFile(JNIEnv *env, jclass clazz, struct java_lang_Class* par1) {
338 if (JVMTI_ERROR_NONE !=(err=(*jvmtienv)->
339 GetSourceFileName(jvmtienv, (jclass)par1, &srcname))) {
340 printjvmtierror("VMVirtualMachine.getSourceFile GetSourceFileName", err);
344 str = (*env)->NewString(env,(jchar*)srcname,(jsize)strlen(srcname));
346 return (struct java_lang_String*)str;
349 /* match JdwpConstants.EventKind to jvmtiEvent constants */
350 static jvmtiEvent EventKind2jvmtiEvent(jbyte kind){
352 case /* SINGLE_STEP */ 1: return JVMTI_EVENT_SINGLE_STEP;
353 case /* BREAKPOINT */ 2: return JVMTI_EVENT_BREAKPOINT;
354 case /* FRAME_POP */ 3: return JVMTI_EVENT_FRAME_POP;
355 case /* EXCEPTION */ 4: return JVMTI_EVENT_EXCEPTION;
356 case /* USER_DEFINED */ 5: return -1; /* can this be matched ? */
357 case /* THREAD_START */ 6: return JVMTI_EVENT_THREAD_START;
358 case /* THREAD_END */ 7: return JVMTI_EVENT_THREAD_END;
359 case /* CLASS_PREPARE */ 8: return JVMTI_EVENT_CLASS_PREPARE;
360 case /* CLASS_UNLOAD */ 9: return -1; /* can this be matched ? */
361 case /* CLASS_LOAD */ 10: return JVMTI_EVENT_CLASS_LOAD;
362 case /* FIELD_ACCESS */ 20: return JVMTI_EVENT_FIELD_ACCESS;
363 case /* FIELD_MODIFICATION */ 21: return JVMTI_EVENT_FIELD_MODIFICATION;
364 case /* EXCEPTION_CATCH */ 30: return JVMTI_EVENT_EXCEPTION_CATCH;
365 case /* METHOD_ENTRY */ 40: return JVMTI_EVENT_METHOD_ENTRY;
366 case /* METHOD_EXIT */ 41: return JVMTI_EVENT_METHOD_EXIT;
367 case /* VM_INIT */ 90: return JVMTI_EVENT_VM_INIT;
368 case /* VM_DEATH */ 99: return JVMTI_EVENT_VM_DEATH;
369 case /* VM_DISCONNECTED */ 100: return -1; /* can this be matched ? */
375 * Class: gnu/classpath/jdwp/VMVirtualMachine
376 * Method: registerEvent
377 * Signature: (Lgnu/classpath/jdwp/event/EventRequest;)V
379 JNIEXPORT void JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_registerEvent(JNIEnv *env, jclass clazz, struct gnu_classpath_jdwp_event_EventRequest* par1) {
385 erc = (*env)->FindClass(env,"gnu.classpath.jdwp.event.EventRequest");
387 kindid = (*env)->GetFieldID(env, erc, "_kind", "B");
388 kind = (*env)->GetByteField(env, (jobject)par1, kindid);
390 if (JVMTI_ERROR_NONE != (err= (*jvmtienv)->
391 SetEventNotificationMode(jvmtienv, JVMTI_ENABLE,
392 EventKind2jvmtiEvent(kind), NULL)))
393 printjvmtierror("VMVirtualMachine_registerEvent SetEventNotificationMode",err);
399 * Class: gnu/classpath/jdwp/VMVirtualMachine
400 * Method: unregisterEvent
401 * Signature: (Lgnu/classpath/jdwp/event/EventRequest;)V
403 JNIEXPORT void JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_unregisterEvent(JNIEnv *env, jclass clazz, struct gnu_classpath_jdwp_event_EventRequest* par1) {
409 erc = (*env)->FindClass(env,"gnu.classpath.jdwp.event.EventRequest");
411 kindid = (*env)->GetFieldID(env, erc, "_kind", "B");
412 kind = (*env)->GetByteField(env, (jobject)par1, kindid);
414 if (JVMTI_ERROR_NONE != (err= (*jvmtienv)->
415 SetEventNotificationMode(jvmtienv, JVMTI_DISABLE,
416 EventKind2jvmtiEvent(kind), NULL)))
417 printjvmtierror("VMVirtualMachine_registerEvent SetEventNotificationMode",err);
423 * Class: gnu/classpath/jdwp/VMVirtualMachine
424 * Method: clearEvents
427 JNIEXPORT void JNICALL Java_gnu_classpath_jdwp_VMVirtualMachine_clearEvents(JNIEnv *env, jclass clazz, s4 par1) {
428 /* jvmti events are not saved - there is nothing to clear */
433 * These are local overrides for various environment variables in Emacs.
434 * Please do not remove this and leave it at the end of the file, where
435 * Emacs will automagically detect them.
436 * ---------------------------------------------------------------------
439 * indent-tabs-mode: t