< prev index next >

src/jdk.jdwp.agent/share/native/libjdwp/util.c

Print this page




 161     if (method == NULL) {
 162         ERROR_MESSAGE(("JDWP Can't find method %s with signature %s",
 163                                 name, signature));
 164         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 165     }
 166     if ( JNI_FUNC_PTR(env,ExceptionOccurred)(env) ) {
 167         ERROR_MESSAGE(("JDWP Exception occurred finding method %s with signature %s",
 168                                 name, signature));
 169         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 170     }
 171     return method;
 172 }
 173 
 174 void
 175 util_initialize(JNIEnv *env)
 176 {
 177     WITH_LOCAL_REFS(env, 6) {
 178 
 179         jvmtiError error;
 180         jclass localClassClass;

 181         jclass localThreadClass;
 182         jclass localThreadGroupClass;
 183         jclass localClassLoaderClass;
 184         jclass localStringClass;
 185         jclass localSystemClass;

 186         jclass localPropertiesClass;
 187         jclass localVMSupportClass;
 188         jobject localAgentProperties;
 189         jmethodID getAgentProperties;
 190         jint groupCount;
 191         jthreadGroup *groups;
 192         jthreadGroup localSystemThreadGroup;
 193 


 194         /* Find some standard classes */
 195 
 196         localClassClass         = findClass(env,"java/lang/Class");

 197         localThreadClass        = findClass(env,"java/lang/Thread");
 198         localThreadGroupClass   = findClass(env,"java/lang/ThreadGroup");
 199         localClassLoaderClass   = findClass(env,"java/lang/ClassLoader");
 200         localStringClass        = findClass(env,"java/lang/String");
 201         localSystemClass        = findClass(env,"java/lang/System");
 202         localPropertiesClass    = findClass(env,"java/util/Properties");

 203 
 204         /* Save references */
 205 
 206         saveGlobalRef(env, localClassClass,       &(gdata->classClass));

 207         saveGlobalRef(env, localThreadClass,      &(gdata->threadClass));
 208         saveGlobalRef(env, localThreadGroupClass, &(gdata->threadGroupClass));
 209         saveGlobalRef(env, localClassLoaderClass, &(gdata->classLoaderClass));
 210         saveGlobalRef(env, localStringClass,      &(gdata->stringClass));
 211         saveGlobalRef(env, localSystemClass,      &(gdata->systemClass));

 212 
 213         /* Find some standard methods */
 214 




 215         gdata->threadConstructor =
 216                 getMethod(env, gdata->threadClass,
 217                     "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V");
 218         gdata->threadSetDaemon =
 219                 getMethod(env, gdata->threadClass, "setDaemon", "(Z)V");
 220         gdata->threadResume =
 221                 getMethod(env, gdata->threadClass, "resume", "()V");
 222         gdata->systemGetProperty =
 223                 getStaticMethod(env, gdata->systemClass,
 224                     "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
 225         gdata->setProperty =
 226                 getMethod(env, localPropertiesClass,
 227                     "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
 228 
 229         /* Find the system thread group */
 230 
 231         groups = NULL;
 232         groupCount = 0;
 233         error = JVMTI_FUNC_PTR(gdata->jvmti,GetTopThreadGroups)
 234                     (gdata->jvmti, &groupCount, &groups);


 290 }
 291 
 292 jboolean
 293 isObjectTag(jbyte tag) {
 294     return (tag == JDWP_TAG(OBJECT)) ||
 295            (tag == JDWP_TAG(STRING)) ||
 296            (tag == JDWP_TAG(THREAD)) ||
 297            (tag == JDWP_TAG(THREAD_GROUP)) ||
 298            (tag == JDWP_TAG(CLASS_LOADER)) ||
 299            (tag == JDWP_TAG(CLASS_OBJECT)) ||
 300            (tag == JDWP_TAG(ARRAY));
 301 }
 302 
 303 jbyte
 304 specificTypeKey(JNIEnv *env, jobject object)
 305 {
 306     if (object == NULL) {
 307         return JDWP_TAG(OBJECT);
 308     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->stringClass)) {
 309         return JDWP_TAG(STRING);






 310     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadClass)) {
 311         return JDWP_TAG(THREAD);
 312     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadGroupClass)) {
 313         return JDWP_TAG(THREAD_GROUP);
 314     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classLoaderClass)) {
 315         return JDWP_TAG(CLASS_LOADER);
 316     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classClass)) {
 317         return JDWP_TAG(CLASS_OBJECT);
 318     } else {
 319         jboolean classIsArray;
 320 
 321         WITH_LOCAL_REFS(env, 1) {
 322             jclass clazz;
 323             clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object);
 324             classIsArray = isArrayClass(clazz);
 325         } END_WITH_LOCAL_REFS(env);
 326 
 327         return (classIsArray ? JDWP_TAG(ARRAY) : JDWP_TAG(OBJECT));
 328     }
 329 }


 584         }
 585         return JNI_TRUE;
 586     }
 587 
 588     if (inStream_command(in) == JDWP_COMMAND(ClassType, NewInstance)) {
 589         invokeType = INVOKE_CONSTRUCTOR;
 590     } else if (inStream_command(in) == JDWP_COMMAND(ClassType, InvokeMethod)) {
 591         invokeType = INVOKE_STATIC;
 592     } else if (inStream_command(in) == JDWP_COMMAND(InterfaceType, InvokeMethod)) {
 593         invokeType = INVOKE_STATIC;
 594     } else if (inStream_command(in) == JDWP_COMMAND(ObjectReference, InvokeMethod)) {
 595         invokeType = INVOKE_INSTANCE;
 596     } else {
 597         outStream_setError(out, JDWP_ERROR(INTERNAL));
 598         if ( arguments != NULL ) {
 599             jvmtiDeallocate(arguments);
 600         }
 601         return JNI_TRUE;
 602     }
 603 
 604     /*
 605      * Request the invoke. If there are no errors in the request,
 606      * the interrupting thread will actually do the invoke and a
 607      * reply will be generated subsequently, so we don't reply here.
 608      */
 609     error = invoker_requestInvoke(invokeType, (jbyte)options, inStream_id(in),
 610                                   thread, clazz, method,
 611                                   instance, arguments, argumentCount);








 612     if (error != JVMTI_ERROR_NONE) {
 613         outStream_setError(out, map2jdwpError(error));
 614         if ( arguments != NULL ) {
 615             jvmtiDeallocate(arguments);
 616         }
 617         return JNI_TRUE;
 618     }
 619 
 620     return JNI_FALSE;   /* Don't reply */
 621 }
 622 
 623 jint
 624 uniqueID(void)
 625 {
 626     static jint currentID = 0;
 627     return currentID++;
 628 }
 629 
 630 int
 631 filterDebugThreads(jthread *threads, int count)


 807             (gdata->jvmti, clazz, field, &name, &signature, &generic_signature);
 808 
 809     if ( pname != NULL ) {
 810         *pname = name;
 811     } else if ( name != NULL )  {
 812         jvmtiDeallocate(name);
 813     }
 814     if ( psignature != NULL ) {
 815         *psignature = signature;
 816     } else if ( signature != NULL )  {
 817         jvmtiDeallocate(signature);
 818     }
 819     if ( pgeneric_signature != NULL ) {
 820         *pgeneric_signature = generic_signature;
 821     } else if ( generic_signature != NULL )  {
 822         jvmtiDeallocate(generic_signature);
 823     }
 824     return error;
 825 }
 826 





























































 827 JNIEnv *
 828 getEnv(void)
 829 {
 830     JNIEnv *env = NULL;
 831     jint rc;
 832 
 833     rc = FUNC_PTR(gdata->jvm,GetEnv)
 834                 (gdata->jvm, (void **)&env, JNI_VERSION_1_2);
 835     if (rc != JNI_OK) {
 836         ERROR_MESSAGE(("JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = %d",
 837                 rc));
 838         EXIT_ERROR(AGENT_ERROR_NO_JNI_ENV,NULL);
 839     }
 840     return env;
 841 }
 842 
 843 jvmtiError
 844 spawnNewThread(jvmtiStartFunction func, void *arg, char *name)
 845 {
 846     JNIEnv *env = getEnv();


1569      */
1570 
1571     /*
1572      * Restore exception state from before call
1573      */
1574     if (throwable != NULL) {
1575         JNI_FUNC_PTR(env,Throw)(env, throwable);
1576     } else {
1577         JNI_FUNC_PTR(env,ExceptionClear)(env);
1578     }
1579 }
1580 
1581 jboolean
1582 isClass(jobject object)
1583 {
1584     JNIEnv *env = getEnv();
1585     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classClass);
1586 }
1587 
1588 jboolean







1589 isThread(jobject object)
1590 {
1591     JNIEnv *env = getEnv();
1592     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadClass);
1593 }
1594 
1595 jboolean
1596 isThreadGroup(jobject object)
1597 {
1598     JNIEnv *env = getEnv();
1599     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadGroupClass);
1600 }
1601 
1602 jboolean
1603 isString(jobject object)
1604 {
1605     JNIEnv *env = getEnv();
1606     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->stringClass);
1607 }
1608 


1948     index2jvmti[EI_SINGLE_STEP        -EI_min] = JVMTI_EVENT_SINGLE_STEP;
1949     index2jvmti[EI_BREAKPOINT         -EI_min] = JVMTI_EVENT_BREAKPOINT;
1950     index2jvmti[EI_FRAME_POP          -EI_min] = JVMTI_EVENT_FRAME_POP;
1951     index2jvmti[EI_EXCEPTION          -EI_min] = JVMTI_EVENT_EXCEPTION;
1952     index2jvmti[EI_THREAD_START       -EI_min] = JVMTI_EVENT_THREAD_START;
1953     index2jvmti[EI_THREAD_END         -EI_min] = JVMTI_EVENT_THREAD_END;
1954     index2jvmti[EI_CLASS_PREPARE      -EI_min] = JVMTI_EVENT_CLASS_PREPARE;
1955     index2jvmti[EI_GC_FINISH          -EI_min] = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH;
1956     index2jvmti[EI_CLASS_LOAD         -EI_min] = JVMTI_EVENT_CLASS_LOAD;
1957     index2jvmti[EI_FIELD_ACCESS       -EI_min] = JVMTI_EVENT_FIELD_ACCESS;
1958     index2jvmti[EI_FIELD_MODIFICATION -EI_min] = JVMTI_EVENT_FIELD_MODIFICATION;
1959     index2jvmti[EI_EXCEPTION_CATCH    -EI_min] = JVMTI_EVENT_EXCEPTION_CATCH;
1960     index2jvmti[EI_METHOD_ENTRY       -EI_min] = JVMTI_EVENT_METHOD_ENTRY;
1961     index2jvmti[EI_METHOD_EXIT        -EI_min] = JVMTI_EVENT_METHOD_EXIT;
1962     index2jvmti[EI_MONITOR_CONTENDED_ENTER      -EI_min] = JVMTI_EVENT_MONITOR_CONTENDED_ENTER;
1963     index2jvmti[EI_MONITOR_CONTENDED_ENTERED    -EI_min] = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED;
1964     index2jvmti[EI_MONITOR_WAIT       -EI_min] = JVMTI_EVENT_MONITOR_WAIT;
1965     index2jvmti[EI_MONITOR_WAITED     -EI_min] = JVMTI_EVENT_MONITOR_WAITED;
1966     index2jvmti[EI_VM_INIT            -EI_min] = JVMTI_EVENT_VM_INIT;
1967     index2jvmti[EI_VM_DEATH           -EI_min] = JVMTI_EVENT_VM_DEATH;






1968 
1969     index2jdwp[EI_SINGLE_STEP         -EI_min] = JDWP_EVENT(SINGLE_STEP);
1970     index2jdwp[EI_BREAKPOINT          -EI_min] = JDWP_EVENT(BREAKPOINT);
1971     index2jdwp[EI_FRAME_POP           -EI_min] = JDWP_EVENT(FRAME_POP);
1972     index2jdwp[EI_EXCEPTION           -EI_min] = JDWP_EVENT(EXCEPTION);
1973     index2jdwp[EI_THREAD_START        -EI_min] = JDWP_EVENT(THREAD_START);
1974     index2jdwp[EI_THREAD_END          -EI_min] = JDWP_EVENT(THREAD_END);
1975     index2jdwp[EI_CLASS_PREPARE       -EI_min] = JDWP_EVENT(CLASS_PREPARE);
1976     index2jdwp[EI_GC_FINISH           -EI_min] = JDWP_EVENT(CLASS_UNLOAD);
1977     index2jdwp[EI_CLASS_LOAD          -EI_min] = JDWP_EVENT(CLASS_LOAD);
1978     index2jdwp[EI_FIELD_ACCESS        -EI_min] = JDWP_EVENT(FIELD_ACCESS);
1979     index2jdwp[EI_FIELD_MODIFICATION  -EI_min] = JDWP_EVENT(FIELD_MODIFICATION);
1980     index2jdwp[EI_EXCEPTION_CATCH     -EI_min] = JDWP_EVENT(EXCEPTION_CATCH);
1981     index2jdwp[EI_METHOD_ENTRY        -EI_min] = JDWP_EVENT(METHOD_ENTRY);
1982     index2jdwp[EI_METHOD_EXIT         -EI_min] = JDWP_EVENT(METHOD_EXIT);
1983     index2jdwp[EI_MONITOR_CONTENDED_ENTER             -EI_min] = JDWP_EVENT(MONITOR_CONTENDED_ENTER);
1984     index2jdwp[EI_MONITOR_CONTENDED_ENTERED           -EI_min] = JDWP_EVENT(MONITOR_CONTENDED_ENTERED);
1985     index2jdwp[EI_MONITOR_WAIT        -EI_min] = JDWP_EVENT(MONITOR_WAIT);
1986     index2jdwp[EI_MONITOR_WAITED      -EI_min] = JDWP_EVENT(MONITOR_WAITED);
1987     index2jdwp[EI_VM_INIT             -EI_min] = JDWP_EVENT(VM_INIT);
1988     index2jdwp[EI_VM_DEATH            -EI_min] = JDWP_EVENT(VM_DEATH);









1989 }
1990 
1991 jdwpEvent
1992 eventIndex2jdwp(EventIndex i)
1993 {
1994     if ( i < EI_min || i > EI_max ) {
1995         EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"bad EventIndex");
1996     }
1997     return index2jdwp[i-EI_min];
1998 }
1999 
2000 jvmtiEvent
2001 eventIndex2jvmti(EventIndex i)
2002 {
2003     if ( i < EI_min || i > EI_max ) {
2004         EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"bad EventIndex");
2005     }
2006     return index2jvmti[i-EI_min];
2007 }
2008 































































2009 EventIndex
2010 jdwp2EventIndex(jdwpEvent eventType)
2011 {
2012     switch ( eventType ) {
2013         case JDWP_EVENT(SINGLE_STEP):
2014             return EI_SINGLE_STEP;
2015         case JDWP_EVENT(BREAKPOINT):
2016             return EI_BREAKPOINT;
2017         case JDWP_EVENT(FRAME_POP):
2018             return EI_FRAME_POP;
2019         case JDWP_EVENT(EXCEPTION):
2020             return EI_EXCEPTION;
2021         case JDWP_EVENT(THREAD_START):
2022             return EI_THREAD_START;
2023         case JDWP_EVENT(THREAD_END):
2024             return EI_THREAD_END;
2025         case JDWP_EVENT(CLASS_PREPARE):
2026             return EI_CLASS_PREPARE;
2027         case JDWP_EVENT(CLASS_UNLOAD):
2028             return EI_GC_FINISH;


2094         case JVMTI_EVENT_METHOD_ENTRY:
2095             return EI_METHOD_ENTRY;
2096         case JVMTI_EVENT_METHOD_EXIT:
2097             return EI_METHOD_EXIT;
2098         /*
2099          * There is no JVMTI_EVENT_METHOD_EXIT_WITH_RETURN_VALUE.
2100          * The normal JVMTI_EVENT_METHOD_EXIT always contains the return value.
2101          */
2102         case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
2103             return EI_MONITOR_CONTENDED_ENTER;
2104         case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
2105             return EI_MONITOR_CONTENDED_ENTERED;
2106         case JVMTI_EVENT_MONITOR_WAIT:
2107             return EI_MONITOR_WAIT;
2108         case JVMTI_EVENT_MONITOR_WAITED:
2109             return EI_MONITOR_WAITED;
2110         case JVMTI_EVENT_VM_INIT:
2111             return EI_VM_INIT;
2112         case JVMTI_EVENT_VM_DEATH:
2113             return EI_VM_DEATH;















2114         default:
2115             EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"JVMTI to EventIndex mapping");
2116             break;
2117     }
2118     return (EventIndex)0;
2119 }
2120 
2121 /* This routine is commonly used, maps jvmti and agent errors to the best
2122  *    jdwp error code we can map to.
2123  */
2124 jdwpError
2125 map2jdwpError(jvmtiError error)
2126 {
2127     switch ( (int)error ) {
2128         case JVMTI_ERROR_NONE:
2129             return JDWP_ERROR(NONE);
2130         case AGENT_ERROR_INVALID_THREAD:
2131         case JVMTI_ERROR_INVALID_THREAD:
2132             return JDWP_ERROR(INVALID_THREAD);
2133         case JVMTI_ERROR_INVALID_THREAD_GROUP:




 161     if (method == NULL) {
 162         ERROR_MESSAGE(("JDWP Can't find method %s with signature %s",
 163                                 name, signature));
 164         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 165     }
 166     if ( JNI_FUNC_PTR(env,ExceptionOccurred)(env) ) {
 167         ERROR_MESSAGE(("JDWP Exception occurred finding method %s with signature %s",
 168                                 name, signature));
 169         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 170     }
 171     return method;
 172 }
 173 
 174 void
 175 util_initialize(JNIEnv *env)
 176 {
 177     WITH_LOCAL_REFS(env, 6) {
 178 
 179         jvmtiError error;
 180         jclass localClassClass;
 181         jclass localFiberClass;
 182         jclass localThreadClass;
 183         jclass localThreadGroupClass;
 184         jclass localClassLoaderClass;
 185         jclass localStringClass;
 186         jclass localSystemClass;
 187         jclass localInnocuousThreadClass;
 188         jclass localPropertiesClass;
 189         jclass localVMSupportClass;
 190         jobject localAgentProperties;
 191         jmethodID getAgentProperties;
 192         jint groupCount;
 193         jthreadGroup *groups;
 194         jthreadGroup localSystemThreadGroup;
 195 
 196         gdata->ignoreEvents = JNI_FALSE;
 197 
 198         /* Find some standard classes */
 199 
 200         localClassClass         = findClass(env,"java/lang/Class");
 201         localFiberClass         = findClass(env,"java/lang/Fiber");
 202         localThreadClass        = findClass(env,"java/lang/Thread");
 203         localThreadGroupClass   = findClass(env,"java/lang/ThreadGroup");
 204         localClassLoaderClass   = findClass(env,"java/lang/ClassLoader");
 205         localStringClass        = findClass(env,"java/lang/String");
 206         localSystemClass        = findClass(env,"java/lang/System");
 207         localPropertiesClass    = findClass(env,"java/util/Properties");
 208         localInnocuousThreadClass = findClass(env, "jdk/internal/misc/InnocuousThread");
 209 
 210         /* Save references */
 211 
 212         saveGlobalRef(env, localClassClass,       &(gdata->classClass));
 213         saveGlobalRef(env, localFiberClass,       &(gdata->fiberClass));
 214         saveGlobalRef(env, localThreadClass,      &(gdata->threadClass));
 215         saveGlobalRef(env, localThreadGroupClass, &(gdata->threadGroupClass));
 216         saveGlobalRef(env, localClassLoaderClass, &(gdata->classLoaderClass));
 217         saveGlobalRef(env, localStringClass,      &(gdata->stringClass));
 218         saveGlobalRef(env, localSystemClass,      &(gdata->systemClass));
 219         saveGlobalRef(env, localInnocuousThreadClass, &(gdata->innocuousThreadClass));
 220 
 221         /* Find some standard methods */
 222 
 223         gdata->fiberToString =
 224                 getMethod(env, gdata->fiberClass, "toString", "()Ljava/lang/String;");
 225         gdata->fiberTryMountAndSuspend =
 226                 getMethod(env, gdata->fiberClass, "tryMountAndSuspend", "()Ljava/lang/Thread;");
 227         gdata->threadConstructor =
 228                 getMethod(env, gdata->threadClass,
 229                     "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V");
 230         gdata->threadSetDaemon =
 231                 getMethod(env, gdata->threadClass, "setDaemon", "(Z)V");
 232         gdata->threadResume =
 233                 getMethod(env, gdata->threadClass, "resume", "()V");
 234         gdata->systemGetProperty =
 235                 getStaticMethod(env, gdata->systemClass,
 236                     "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
 237         gdata->setProperty =
 238                 getMethod(env, localPropertiesClass,
 239                     "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
 240 
 241         /* Find the system thread group */
 242 
 243         groups = NULL;
 244         groupCount = 0;
 245         error = JVMTI_FUNC_PTR(gdata->jvmti,GetTopThreadGroups)
 246                     (gdata->jvmti, &groupCount, &groups);


 302 }
 303 
 304 jboolean
 305 isObjectTag(jbyte tag) {
 306     return (tag == JDWP_TAG(OBJECT)) ||
 307            (tag == JDWP_TAG(STRING)) ||
 308            (tag == JDWP_TAG(THREAD)) ||
 309            (tag == JDWP_TAG(THREAD_GROUP)) ||
 310            (tag == JDWP_TAG(CLASS_LOADER)) ||
 311            (tag == JDWP_TAG(CLASS_OBJECT)) ||
 312            (tag == JDWP_TAG(ARRAY));
 313 }
 314 
 315 jbyte
 316 specificTypeKey(JNIEnv *env, jobject object)
 317 {
 318     if (object == NULL) {
 319         return JDWP_TAG(OBJECT);
 320     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->stringClass)) {
 321         return JDWP_TAG(STRING);
 322     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->fiberClass)) {
 323         /* We don't really need to check if it's an instance of a Fiber class since
 324          * that would get detected below, but this is a bit faster. At one point
 325          * it was thought that we would need to return THREAD here instead of OBJECT,
 326          * but that's not the case. */
 327         return JDWP_TAG(OBJECT);
 328     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadClass)) {
 329         return JDWP_TAG(THREAD);
 330     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadGroupClass)) {
 331         return JDWP_TAG(THREAD_GROUP);
 332     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classLoaderClass)) {
 333         return JDWP_TAG(CLASS_LOADER);
 334     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classClass)) {
 335         return JDWP_TAG(CLASS_OBJECT);
 336     } else {
 337         jboolean classIsArray;
 338 
 339         WITH_LOCAL_REFS(env, 1) {
 340             jclass clazz;
 341             clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object);
 342             classIsArray = isArrayClass(clazz);
 343         } END_WITH_LOCAL_REFS(env);
 344 
 345         return (classIsArray ? JDWP_TAG(ARRAY) : JDWP_TAG(OBJECT));
 346     }
 347 }


 602         }
 603         return JNI_TRUE;
 604     }
 605 
 606     if (inStream_command(in) == JDWP_COMMAND(ClassType, NewInstance)) {
 607         invokeType = INVOKE_CONSTRUCTOR;
 608     } else if (inStream_command(in) == JDWP_COMMAND(ClassType, InvokeMethod)) {
 609         invokeType = INVOKE_STATIC;
 610     } else if (inStream_command(in) == JDWP_COMMAND(InterfaceType, InvokeMethod)) {
 611         invokeType = INVOKE_STATIC;
 612     } else if (inStream_command(in) == JDWP_COMMAND(ObjectReference, InvokeMethod)) {
 613         invokeType = INVOKE_INSTANCE;
 614     } else {
 615         outStream_setError(out, JDWP_ERROR(INTERNAL));
 616         if ( arguments != NULL ) {
 617             jvmtiDeallocate(arguments);
 618         }
 619         return JNI_TRUE;
 620     }
 621 
 622     /* Don't try this with unmounted fibers. */
 623     if (isFiber(thread)) {
 624         thread = getFiberThread(thread);
 625     }
 626     if (thread == NULL) {
 627         error = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
 628     } else {
 629         /*
 630          * Request the invoke. If there are no errors in the request,
 631          * the interrupting thread will actually do the invoke and a
 632          * reply will be generated subsequently, so we don't reply here.
 633          */
 634         error = invoker_requestInvoke(invokeType, (jbyte)options, inStream_id(in),
 635                                       thread, clazz, method,
 636                                       instance, arguments, argumentCount);
 637     }
 638     if (error != JVMTI_ERROR_NONE) {
 639         outStream_setError(out, map2jdwpError(error));
 640         if ( arguments != NULL ) {
 641             jvmtiDeallocate(arguments);
 642         }
 643         return JNI_TRUE;
 644     }
 645 
 646     return JNI_FALSE;   /* Don't reply */
 647 }
 648 
 649 jint
 650 uniqueID(void)
 651 {
 652     static jint currentID = 0;
 653     return currentID++;
 654 }
 655 
 656 int
 657 filterDebugThreads(jthread *threads, int count)


 833             (gdata->jvmti, clazz, field, &name, &signature, &generic_signature);
 834 
 835     if ( pname != NULL ) {
 836         *pname = name;
 837     } else if ( name != NULL )  {
 838         jvmtiDeallocate(name);
 839     }
 840     if ( psignature != NULL ) {
 841         *psignature = signature;
 842     } else if ( signature != NULL )  {
 843         jvmtiDeallocate(signature);
 844     }
 845     if ( pgeneric_signature != NULL ) {
 846         *pgeneric_signature = generic_signature;
 847     } else if ( generic_signature != NULL )  {
 848         jvmtiDeallocate(generic_signature);
 849     }
 850     return error;
 851 }
 852 
 853 /**
 854  * Return fiber that is running on specified thread (must be inside a WITH_LOCAL_REFS)
 855  */
 856 jthread
 857 getThreadFiber(jthread thread)
 858 {
 859     jthread fiber;
 860     jvmtiError error;
 861 
 862     JDI_ASSERT(gdata->fibersSupported);
 863     if ( thread == NULL ) {
 864         return NULL;
 865     }
 866     error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadFiber)
 867         (gdata->jvmti, thread, &fiber);
 868     if (error == JVMTI_ERROR_THREAD_NOT_ALIVE) {
 869         /* fiber fixme: get rid of this once we get rid of helperThreads. It should never happen then. */
 870         return NULL;
 871     } else if ( error != JVMTI_ERROR_NONE ) {
 872         EXIT_ERROR(error,"Error calling GetThreadFiber()");
 873         return JNI_FALSE;
 874     }
 875     return fiber;
 876 }
 877 
 878 /**
 879  * Return thread that specified fiber is running on (must be inside a WITH_LOCAL_REFS)
 880  */
 881 jthread
 882 getFiberThread(jthread fiber)
 883 {
 884     jthread thread;
 885     jvmtiError error;
 886 
 887     JDI_ASSERT(gdata->fibersSupported);
 888     if ( fiber == NULL ) {
 889         return NULL;
 890     }
 891     error = JVMTI_FUNC_PTR(gdata->jvmti,GetFiberThread)
 892         (gdata->jvmti, fiber, &thread);
 893     if ( error != JVMTI_ERROR_NONE ) {
 894         EXIT_ERROR(error,"Error calling GetFiberThread()");
 895         return JNI_FALSE;
 896     }
 897     return thread;
 898 }
 899 
 900 jint
 901 getThreadFrameCount(jthread thread)
 902 {
 903     jint count = 0;
 904     jvmtiError error;
 905 
 906     error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameCount)
 907                     (gdata->jvmti, thread, &count);
 908     if (error != JVMTI_ERROR_NONE) {
 909         EXIT_ERROR(error, "getting frame count");
 910     }
 911     return count;
 912 }
 913 
 914 JNIEnv *
 915 getEnv(void)
 916 {
 917     JNIEnv *env = NULL;
 918     jint rc;
 919 
 920     rc = FUNC_PTR(gdata->jvm,GetEnv)
 921                 (gdata->jvm, (void **)&env, JNI_VERSION_1_2);
 922     if (rc != JNI_OK) {
 923         ERROR_MESSAGE(("JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = %d",
 924                 rc));
 925         EXIT_ERROR(AGENT_ERROR_NO_JNI_ENV,NULL);
 926     }
 927     return env;
 928 }
 929 
 930 jvmtiError
 931 spawnNewThread(jvmtiStartFunction func, void *arg, char *name)
 932 {
 933     JNIEnv *env = getEnv();


1656      */
1657 
1658     /*
1659      * Restore exception state from before call
1660      */
1661     if (throwable != NULL) {
1662         JNI_FUNC_PTR(env,Throw)(env, throwable);
1663     } else {
1664         JNI_FUNC_PTR(env,ExceptionClear)(env);
1665     }
1666 }
1667 
1668 jboolean
1669 isClass(jobject object)
1670 {
1671     JNIEnv *env = getEnv();
1672     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classClass);
1673 }
1674 
1675 jboolean
1676 isFiber(jobject object)
1677 {
1678     JNIEnv *env = getEnv();
1679     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->fiberClass);
1680 }
1681 
1682 jboolean
1683 isThread(jobject object)
1684 {
1685     JNIEnv *env = getEnv();
1686     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadClass);
1687 }
1688 
1689 jboolean
1690 isThreadGroup(jobject object)
1691 {
1692     JNIEnv *env = getEnv();
1693     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadGroupClass);
1694 }
1695 
1696 jboolean
1697 isString(jobject object)
1698 {
1699     JNIEnv *env = getEnv();
1700     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->stringClass);
1701 }
1702 


2042     index2jvmti[EI_SINGLE_STEP        -EI_min] = JVMTI_EVENT_SINGLE_STEP;
2043     index2jvmti[EI_BREAKPOINT         -EI_min] = JVMTI_EVENT_BREAKPOINT;
2044     index2jvmti[EI_FRAME_POP          -EI_min] = JVMTI_EVENT_FRAME_POP;
2045     index2jvmti[EI_EXCEPTION          -EI_min] = JVMTI_EVENT_EXCEPTION;
2046     index2jvmti[EI_THREAD_START       -EI_min] = JVMTI_EVENT_THREAD_START;
2047     index2jvmti[EI_THREAD_END         -EI_min] = JVMTI_EVENT_THREAD_END;
2048     index2jvmti[EI_CLASS_PREPARE      -EI_min] = JVMTI_EVENT_CLASS_PREPARE;
2049     index2jvmti[EI_GC_FINISH          -EI_min] = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH;
2050     index2jvmti[EI_CLASS_LOAD         -EI_min] = JVMTI_EVENT_CLASS_LOAD;
2051     index2jvmti[EI_FIELD_ACCESS       -EI_min] = JVMTI_EVENT_FIELD_ACCESS;
2052     index2jvmti[EI_FIELD_MODIFICATION -EI_min] = JVMTI_EVENT_FIELD_MODIFICATION;
2053     index2jvmti[EI_EXCEPTION_CATCH    -EI_min] = JVMTI_EVENT_EXCEPTION_CATCH;
2054     index2jvmti[EI_METHOD_ENTRY       -EI_min] = JVMTI_EVENT_METHOD_ENTRY;
2055     index2jvmti[EI_METHOD_EXIT        -EI_min] = JVMTI_EVENT_METHOD_EXIT;
2056     index2jvmti[EI_MONITOR_CONTENDED_ENTER      -EI_min] = JVMTI_EVENT_MONITOR_CONTENDED_ENTER;
2057     index2jvmti[EI_MONITOR_CONTENDED_ENTERED    -EI_min] = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED;
2058     index2jvmti[EI_MONITOR_WAIT       -EI_min] = JVMTI_EVENT_MONITOR_WAIT;
2059     index2jvmti[EI_MONITOR_WAITED     -EI_min] = JVMTI_EVENT_MONITOR_WAITED;
2060     index2jvmti[EI_VM_INIT            -EI_min] = JVMTI_EVENT_VM_INIT;
2061     index2jvmti[EI_VM_DEATH           -EI_min] = JVMTI_EVENT_VM_DEATH;
2062     index2jvmti[EI_FIBER_SCHEDULED    -EI_min] = JVMTI_EVENT_FIBER_SCHEDULED;
2063     index2jvmti[EI_FIBER_TERMINATED   -EI_min] = JVMTI_EVENT_FIBER_TERMINATED;
2064     index2jvmti[EI_FIBER_MOUNT        -EI_min] = JVMTI_EVENT_FIBER_MOUNT;
2065     index2jvmti[EI_FIBER_UNMOUNT      -EI_min] = JVMTI_EVENT_FIBER_UNMOUNT;
2066     index2jvmti[EI_CONTINUATION_RUN   -EI_min] = JVMTI_EVENT_CONTINUATION_RUN;
2067     index2jvmti[EI_CONTINUATION_YIELD -EI_min] = JVMTI_EVENT_CONTINUATION_YIELD;
2068 
2069     index2jdwp[EI_SINGLE_STEP         -EI_min] = JDWP_EVENT(SINGLE_STEP);
2070     index2jdwp[EI_BREAKPOINT          -EI_min] = JDWP_EVENT(BREAKPOINT);
2071     index2jdwp[EI_FRAME_POP           -EI_min] = JDWP_EVENT(FRAME_POP);
2072     index2jdwp[EI_EXCEPTION           -EI_min] = JDWP_EVENT(EXCEPTION);
2073     index2jdwp[EI_THREAD_START        -EI_min] = JDWP_EVENT(THREAD_START);
2074     index2jdwp[EI_THREAD_END          -EI_min] = JDWP_EVENT(THREAD_END);
2075     index2jdwp[EI_CLASS_PREPARE       -EI_min] = JDWP_EVENT(CLASS_PREPARE);
2076     index2jdwp[EI_GC_FINISH           -EI_min] = JDWP_EVENT(CLASS_UNLOAD);
2077     index2jdwp[EI_CLASS_LOAD          -EI_min] = JDWP_EVENT(CLASS_LOAD);
2078     index2jdwp[EI_FIELD_ACCESS        -EI_min] = JDWP_EVENT(FIELD_ACCESS);
2079     index2jdwp[EI_FIELD_MODIFICATION  -EI_min] = JDWP_EVENT(FIELD_MODIFICATION);
2080     index2jdwp[EI_EXCEPTION_CATCH     -EI_min] = JDWP_EVENT(EXCEPTION_CATCH);
2081     index2jdwp[EI_METHOD_ENTRY        -EI_min] = JDWP_EVENT(METHOD_ENTRY);
2082     index2jdwp[EI_METHOD_EXIT         -EI_min] = JDWP_EVENT(METHOD_EXIT);
2083     index2jdwp[EI_MONITOR_CONTENDED_ENTER             -EI_min] = JDWP_EVENT(MONITOR_CONTENDED_ENTER);
2084     index2jdwp[EI_MONITOR_CONTENDED_ENTERED           -EI_min] = JDWP_EVENT(MONITOR_CONTENDED_ENTERED);
2085     index2jdwp[EI_MONITOR_WAIT        -EI_min] = JDWP_EVENT(MONITOR_WAIT);
2086     index2jdwp[EI_MONITOR_WAITED      -EI_min] = JDWP_EVENT(MONITOR_WAITED);
2087     index2jdwp[EI_VM_INIT             -EI_min] = JDWP_EVENT(VM_INIT);
2088     index2jdwp[EI_VM_DEATH            -EI_min] = JDWP_EVENT(VM_DEATH);
2089     /* Just map FIBER_SCHEDULED/TERMINATED to THREAD_START/END. */
2090     index2jdwp[EI_FIBER_SCHEDULED     -EI_min] = JDWP_EVENT(THREAD_START);
2091     index2jdwp[EI_FIBER_TERMINATED    -EI_min] = JDWP_EVENT(THREAD_END);
2092     /* fiber fixme: these don't actually map to anything in JDWP. Need a way to make them
2093      * produce an error if referenced. */
2094     index2jdwp[EI_FIBER_MOUNT         -EI_min] = -1;
2095     index2jdwp[EI_FIBER_UNMOUNT       -EI_min] = -1;
2096     index2jdwp[EI_CONTINUATION_RUN    -EI_min] = -1;
2097     index2jdwp[EI_CONTINUATION_YIELD  -EI_min] = -1;
2098 }
2099 
2100 jdwpEvent
2101 eventIndex2jdwp(EventIndex i)
2102 {
2103     if ( i < EI_min || i > EI_max ) {
2104         EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"bad EventIndex");
2105     }
2106     return index2jdwp[i-EI_min];
2107 }
2108 
2109 jvmtiEvent
2110 eventIndex2jvmti(EventIndex i)
2111 {
2112     if ( i < EI_min || i > EI_max ) {
2113         EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"bad EventIndex");
2114     }
2115     return index2jvmti[i-EI_min];
2116 }
2117 
2118 
2119 char*
2120 eventIndex2EventName(EventIndex ei)
2121 {
2122     switch ( ei ) {
2123         case EI_SINGLE_STEP:
2124             return "EI_SINGLE_STEP";
2125         case EI_BREAKPOINT:
2126             return "EI_BREAKPOINT";
2127         case EI_FRAME_POP:
2128             return "EI_FRAME_POP";
2129         case EI_EXCEPTION:
2130             return "EI_EXCEPTION";
2131         case EI_THREAD_START:
2132             return "EI_THREAD_START";
2133         case EI_THREAD_END:
2134             return "EI_THREAD_END";
2135         case EI_CLASS_PREPARE:
2136             return "EI_CLASS_PREPARE";
2137         case EI_GC_FINISH:
2138             return "EI_GC_FINISH";
2139         case EI_CLASS_LOAD:
2140             return "EI_CLASS_LOAD";
2141         case EI_FIELD_ACCESS:
2142             return "EI_FIELD_ACCESS";
2143         case EI_FIELD_MODIFICATION:
2144             return "EI_FIELD_MODIFICATION";
2145         case EI_EXCEPTION_CATCH:
2146             return "EI_EXCEPTION_CATCH";
2147         case EI_METHOD_ENTRY:
2148             return "EI_METHOD_ENTRY";
2149         case EI_METHOD_EXIT:
2150             return "EI_METHOD_EXIT";
2151         case EI_MONITOR_CONTENDED_ENTER:
2152             return "EI_MONITOR_CONTENDED_ENTER";
2153         case EI_MONITOR_CONTENDED_ENTERED:
2154             return "EI_MONITOR_CONTENDED_ENTERED";
2155         case EI_MONITOR_WAIT:
2156             return "EI_MONITOR_WAIT";
2157         case EI_MONITOR_WAITED:
2158             return "EI_MONITOR_WAITED";
2159         case EI_VM_INIT:
2160             return "EI_VM_INIT";
2161         case EI_VM_DEATH:
2162             return "EI_VM_DEATH";
2163         case EI_FIBER_SCHEDULED:
2164             return "EI_FIBER_SCHEDULED";
2165         case EI_FIBER_TERMINATED:
2166             return "EI_FIBER_TERMINATED";
2167         case EI_FIBER_MOUNT:
2168             return "EI_FIBER_MOUNT";
2169         case EI_FIBER_UNMOUNT:
2170             return "EI_FIBER_UNMOUNT";
2171         case EI_CONTINUATION_RUN:
2172             return "EI_CONTINUATION_RUN";
2173         case EI_CONTINUATION_YIELD:
2174             return "EI_CONTINUATION_YIELD";
2175         default:
2176             JDI_ASSERT(JNI_FALSE);
2177             return "Bad EI";
2178     }
2179 }
2180 
2181 EventIndex
2182 jdwp2EventIndex(jdwpEvent eventType)
2183 {
2184     switch ( eventType ) {
2185         case JDWP_EVENT(SINGLE_STEP):
2186             return EI_SINGLE_STEP;
2187         case JDWP_EVENT(BREAKPOINT):
2188             return EI_BREAKPOINT;
2189         case JDWP_EVENT(FRAME_POP):
2190             return EI_FRAME_POP;
2191         case JDWP_EVENT(EXCEPTION):
2192             return EI_EXCEPTION;
2193         case JDWP_EVENT(THREAD_START):
2194             return EI_THREAD_START;
2195         case JDWP_EVENT(THREAD_END):
2196             return EI_THREAD_END;
2197         case JDWP_EVENT(CLASS_PREPARE):
2198             return EI_CLASS_PREPARE;
2199         case JDWP_EVENT(CLASS_UNLOAD):
2200             return EI_GC_FINISH;


2266         case JVMTI_EVENT_METHOD_ENTRY:
2267             return EI_METHOD_ENTRY;
2268         case JVMTI_EVENT_METHOD_EXIT:
2269             return EI_METHOD_EXIT;
2270         /*
2271          * There is no JVMTI_EVENT_METHOD_EXIT_WITH_RETURN_VALUE.
2272          * The normal JVMTI_EVENT_METHOD_EXIT always contains the return value.
2273          */
2274         case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
2275             return EI_MONITOR_CONTENDED_ENTER;
2276         case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
2277             return EI_MONITOR_CONTENDED_ENTERED;
2278         case JVMTI_EVENT_MONITOR_WAIT:
2279             return EI_MONITOR_WAIT;
2280         case JVMTI_EVENT_MONITOR_WAITED:
2281             return EI_MONITOR_WAITED;
2282         case JVMTI_EVENT_VM_INIT:
2283             return EI_VM_INIT;
2284         case JVMTI_EVENT_VM_DEATH:
2285             return EI_VM_DEATH;
2286         /* fiber events */
2287         case JVMTI_EVENT_FIBER_SCHEDULED:
2288             return EI_FIBER_SCHEDULED;
2289         case JVMTI_EVENT_FIBER_TERMINATED:
2290             return EI_FIBER_TERMINATED;
2291         case JVMTI_EVENT_FIBER_MOUNT:
2292             return EI_FIBER_MOUNT;
2293         case JVMTI_EVENT_FIBER_UNMOUNT:
2294             return EI_FIBER_UNMOUNT;
2295         /* continuation events */
2296         case JVMTI_EVENT_CONTINUATION_RUN:
2297             return EI_CONTINUATION_RUN;
2298         case JVMTI_EVENT_CONTINUATION_YIELD:
2299             return EI_CONTINUATION_YIELD;
2300 
2301         default:
2302             EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"JVMTI to EventIndex mapping");
2303             break;
2304     }
2305     return (EventIndex)0;
2306 }
2307 
2308 /* This routine is commonly used, maps jvmti and agent errors to the best
2309  *    jdwp error code we can map to.
2310  */
2311 jdwpError
2312 map2jdwpError(jvmtiError error)
2313 {
2314     switch ( (int)error ) {
2315         case JVMTI_ERROR_NONE:
2316             return JDWP_ERROR(NONE);
2317         case AGENT_ERROR_INVALID_THREAD:
2318         case JVMTI_ERROR_INVALID_THREAD:
2319             return JDWP_ERROR(INVALID_THREAD);
2320         case JVMTI_ERROR_INVALID_THREAD_GROUP:


< prev index next >