1 /*
   2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <ctype.h>
  27 
  28 #include "util.h"
  29 #include "utf_util.h"
  30 #include "transport.h"
  31 #include "eventHandler.h"
  32 #include "threadControl.h"
  33 #include "outStream.h"
  34 #include "inStream.h"
  35 #include "invoker.h"
  36 #include "signature.h"
  37 
  38 
  39 /* Global data area */
  40 BackendGlobalData *gdata = NULL;
  41 
  42 /* Forward declarations */
  43 static jboolean isInterface(jclass clazz);
  44 static jboolean isArrayClass(jclass clazz);
  45 static char * getPropertyUTF8(JNIEnv *env, char *propertyName);
  46 
  47 /* Save an object reference for use later (create a NewGlobalRef) */
  48 void
  49 saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj)
  50 {
  51     jobject newobj;
  52 
  53     if ( pobj == NULL ) {
  54         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"saveGlobalRef pobj");
  55     }
  56     if ( *pobj != NULL ) {
  57         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"saveGlobalRef *pobj");
  58     }
  59     if ( env == NULL ) {
  60         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"saveGlobalRef env");
  61     }
  62     if ( obj == NULL ) {
  63         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"saveGlobalRef obj");
  64     }
  65     newobj = JNI_FUNC_PTR(env,NewGlobalRef)(env, obj);
  66     if ( newobj == NULL ) {
  67         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewGlobalRef");
  68     }
  69     *pobj = newobj;
  70 }
  71 
  72 /* Toss a previously saved object reference */
  73 void
  74 tossGlobalRef(JNIEnv *env, jobject *pobj)
  75 {
  76     jobject obj;
  77 
  78     if ( pobj == NULL ) {
  79         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"tossGlobalRef pobj");
  80     }
  81     obj = *pobj;
  82     if ( env == NULL ) {
  83         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"tossGlobalRef env");
  84     }
  85     if ( obj == NULL ) {
  86         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"tossGlobalRef obj");
  87     }
  88     JNI_FUNC_PTR(env,DeleteGlobalRef)(env, obj);
  89     *pobj = NULL;
  90 }
  91 
  92 jclass
  93 findClass(JNIEnv *env, const char * name)
  94 {
  95     jclass x;
  96 
  97     if ( env == NULL ) {
  98         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"findClass env");
  99     }
 100     if ( name == NULL || name[0] == 0 ) {
 101         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"findClass name");
 102     }
 103     x = JNI_FUNC_PTR(env,FindClass)(env, name);
 104     if (x == NULL) {
 105         ERROR_MESSAGE(("JDWP Can't find class %s", name));
 106         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 107     }
 108     if ( JNI_FUNC_PTR(env,ExceptionOccurred)(env) ) {
 109         ERROR_MESSAGE(("JDWP Exception occurred finding class %s", name));
 110         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 111     }
 112     return x;
 113 }
 114 
 115 jmethodID
 116 getMethod(JNIEnv *env, jclass clazz, const char * name, const char *signature)
 117 {
 118     jmethodID method;
 119 
 120     if ( env == NULL ) {
 121         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getMethod env");
 122     }
 123     if ( clazz == NULL ) {
 124         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getMethod clazz");
 125     }
 126     if ( name == NULL || name[0] == 0 ) {
 127         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getMethod name");
 128     }
 129     if ( signature == NULL || signature[0] == 0 ) {
 130         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getMethod signature");
 131     }
 132     method = JNI_FUNC_PTR(env,GetMethodID)(env, clazz, name, signature);
 133     if (method == NULL) {
 134         ERROR_MESSAGE(("JDWP Can't find method %s with signature %s",
 135                                 name, signature));
 136         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 137     }
 138     if ( JNI_FUNC_PTR(env,ExceptionOccurred)(env) ) {
 139         ERROR_MESSAGE(("JDWP Exception occurred finding method %s with signature %s",
 140                                 name, signature));
 141         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 142     }
 143     return method;
 144 }
 145 
 146 static jmethodID
 147 getStaticMethod(JNIEnv *env, jclass clazz, const char * name, const char *signature)
 148 {
 149     jmethodID method;
 150 
 151     if ( env == NULL ) {
 152         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getStaticMethod env");
 153     }
 154     if ( clazz == NULL ) {
 155         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getStaticMethod clazz");
 156     }
 157     if ( name == NULL || name[0] == 0 ) {
 158         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getStaticMethod name");
 159     }
 160     if ( signature == NULL || signature[0] == 0 ) {
 161         EXIT_ERROR(AGENT_ERROR_ILLEGAL_ARGUMENT,"getStaticMethod signature");
 162     }
 163     method = JNI_FUNC_PTR(env,GetStaticMethodID)(env, clazz, name, signature);
 164     if (method == NULL) {
 165         ERROR_MESSAGE(("JDWP Can't find method %s with signature %s",
 166                                 name, signature));
 167         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 168     }
 169     if ( JNI_FUNC_PTR(env,ExceptionOccurred)(env) ) {
 170         ERROR_MESSAGE(("JDWP Exception occurred finding method %s with signature %s",
 171                                 name, signature));
 172         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
 173     }
 174     return method;
 175 }
 176 
 177 
 178 
 179 void
 180 util_initialize(JNIEnv *env)
 181 {
 182     WITH_LOCAL_REFS(env, 6) {
 183 
 184         jvmtiError error;
 185         jclass localClassClass;
 186         jclass localThreadClass;
 187         jclass localThreadGroupClass;
 188         jclass localClassLoaderClass;
 189         jclass localStringClass;
 190         jclass localSystemClass;
 191         jclass localPropertiesClass;
 192         jclass localVMSupportClass;
 193         jobject localAgentProperties;
 194         jmethodID getAgentProperties;
 195         jint groupCount;
 196         jthreadGroup *groups;
 197         jthreadGroup localSystemThreadGroup;
 198 
 199         /* Find some standard classes */
 200 
 201         localClassClass         = findClass(env,"java/lang/Class");
 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 
 209         /* Save references */
 210 
 211         saveGlobalRef(env, localClassClass,       &(gdata->classClass));
 212         saveGlobalRef(env, localThreadClass,      &(gdata->threadClass));
 213         saveGlobalRef(env, localThreadGroupClass, &(gdata->threadGroupClass));
 214         saveGlobalRef(env, localClassLoaderClass, &(gdata->classLoaderClass));
 215         saveGlobalRef(env, localStringClass,      &(gdata->stringClass));
 216         saveGlobalRef(env, localSystemClass,      &(gdata->systemClass));
 217 
 218         /* Find some standard methods */
 219 
 220         gdata->threadConstructor =
 221                 getMethod(env, gdata->threadClass,
 222                     "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V");
 223         gdata->threadSetDaemon =
 224                 getMethod(env, gdata->threadClass, "setDaemon", "(Z)V");
 225         gdata->systemGetProperty =
 226                 getStaticMethod(env, gdata->systemClass,
 227                     "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
 228         gdata->setProperty =
 229                 getMethod(env, localPropertiesClass,
 230                     "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
 231 
 232         /* Find the system thread group */
 233 
 234         groups = NULL;
 235         groupCount = 0;
 236         error = JVMTI_FUNC_PTR(gdata->jvmti,GetTopThreadGroups)
 237                     (gdata->jvmti, &groupCount, &groups);
 238         if (error != JVMTI_ERROR_NONE ) {
 239             EXIT_ERROR(error, "Can't get system thread group");
 240         }
 241         if ( groupCount == 0 ) {
 242             EXIT_ERROR(AGENT_ERROR_NULL_POINTER, "Can't get system thread group");
 243         }
 244         localSystemThreadGroup = groups[0];
 245         saveGlobalRef(env, localSystemThreadGroup, &(gdata->systemThreadGroup));
 246         jvmtiDeallocate(groups);
 247 
 248         /* Get some basic Java property values we will need at some point */
 249         gdata->property_java_version
 250                         = getPropertyUTF8(env, "java.version");
 251         gdata->property_java_vm_name
 252                         = getPropertyUTF8(env, "java.vm.name");
 253         gdata->property_java_vm_info
 254                         = getPropertyUTF8(env, "java.vm.info");
 255         gdata->property_java_class_path
 256                         = getPropertyUTF8(env, "java.class.path");
 257         gdata->property_sun_boot_library_path
 258                         = getPropertyUTF8(env, "sun.boot.library.path");
 259         gdata->property_path_separator
 260                         = getPropertyUTF8(env, "path.separator");
 261         gdata->property_user_dir
 262                         = getPropertyUTF8(env, "user.dir");
 263 
 264         /* Get agent properties: invoke VMSupport.getAgentProperties */
 265         localVMSupportClass = JNI_FUNC_PTR(env,FindClass)
 266                                           (env, "jdk/internal/vm/VMSupport");
 267         if (localVMSupportClass == NULL) {
 268             gdata->agent_properties = NULL;
 269             if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
 270                 JNI_FUNC_PTR(env,ExceptionClear)(env);
 271             }
 272         } else {
 273             getAgentProperties  =
 274                 getStaticMethod(env, localVMSupportClass,
 275                                 "getAgentProperties", "()Ljava/util/Properties;");
 276             localAgentProperties =
 277                 JNI_FUNC_PTR(env,CallStaticObjectMethod)
 278                             (env, localVMSupportClass, getAgentProperties);
 279             if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
 280                 JNI_FUNC_PTR(env,ExceptionClear)(env);
 281                 EXIT_ERROR(AGENT_ERROR_INTERNAL,
 282                     "Exception occurred calling VMSupport.getAgentProperties");
 283             }
 284             saveGlobalRef(env, localAgentProperties, &(gdata->agent_properties));
 285         }
 286 
 287     } END_WITH_LOCAL_REFS(env);
 288 
 289 }
 290 
 291 void
 292 util_reset(void)
 293 {
 294 }
 295 
 296 jboolean
 297 isObjectTag(jbyte tag) {
 298     return (tag == JDWP_TAG(OBJECT)) ||
 299            (tag == JDWP_TAG(STRING)) ||
 300            (tag == JDWP_TAG(THREAD)) ||
 301            (tag == JDWP_TAG(THREAD_GROUP)) ||
 302            (tag == JDWP_TAG(CLASS_LOADER)) ||
 303            (tag == JDWP_TAG(CLASS_OBJECT)) ||
 304            (tag == JDWP_TAG(ARRAY));
 305 }
 306 
 307 jbyte
 308 specificTypeKey(JNIEnv *env, jobject object)
 309 {
 310     if (object == NULL) {
 311         return JDWP_TAG(OBJECT);
 312     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->stringClass)) {
 313         return JDWP_TAG(STRING);
 314     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadClass)) {
 315         return JDWP_TAG(THREAD);
 316     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadGroupClass)) {
 317         return JDWP_TAG(THREAD_GROUP);
 318     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classLoaderClass)) {
 319         return JDWP_TAG(CLASS_LOADER);
 320     } else if (JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classClass)) {
 321         return JDWP_TAG(CLASS_OBJECT);
 322     } else {
 323         jboolean classIsArray;
 324 
 325         WITH_LOCAL_REFS(env, 1) {
 326             jclass clazz;
 327             clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object);
 328             classIsArray = isArrayClass(clazz);
 329         } END_WITH_LOCAL_REFS(env);
 330 
 331         return (classIsArray ? JDWP_TAG(ARRAY) : JDWP_TAG(OBJECT));
 332     }
 333 }
 334 
 335 static void
 336 writeFieldValue(JNIEnv *env, PacketOutputStream *out, jobject object,
 337                 jfieldID field)
 338 {
 339     jclass clazz;
 340     char *signature = NULL;
 341     jvmtiError error;
 342     jbyte typeKey;
 343 
 344     clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object);
 345     error = fieldSignature(clazz, field, NULL, &signature, NULL);
 346     if (error != JVMTI_ERROR_NONE) {
 347         outStream_setError(out, map2jdwpError(error));
 348         return;
 349     }
 350     typeKey = jdwpTag(signature);
 351     jvmtiDeallocate(signature);
 352 
 353     if (isReferenceTag(typeKey)) {
 354 
 355         jobject value = JNI_FUNC_PTR(env,GetObjectField)(env, object, field);
 356         (void)outStream_writeByte(out, specificTypeKey(env, value));
 357         (void)outStream_writeObjectRef(env, out, value);
 358         return;
 359 
 360     }
 361 
 362     /*
 363      * For primitive types, the type key is bounced back as is.
 364      */

 365     (void)outStream_writeByte(out, typeKey);
 366 
 367     switch (typeKey) {
 368         case JDWP_TAG(BYTE):
 369             (void)outStream_writeByte(out,
 370                       JNI_FUNC_PTR(env,GetByteField)(env, object, field));
 371             break;
 372 
 373         case JDWP_TAG(CHAR):
 374             (void)outStream_writeChar(out,
 375                       JNI_FUNC_PTR(env,GetCharField)(env, object, field));
 376             break;
 377 
 378         case JDWP_TAG(FLOAT):
 379             (void)outStream_writeFloat(out,
 380                       JNI_FUNC_PTR(env,GetFloatField)(env, object, field));
 381             break;
 382 
 383         case JDWP_TAG(DOUBLE):
 384             (void)outStream_writeDouble(out,
 385                       JNI_FUNC_PTR(env,GetDoubleField)(env, object, field));
 386             break;
 387 
 388         case JDWP_TAG(INT):
 389             (void)outStream_writeInt(out,
 390                       JNI_FUNC_PTR(env,GetIntField)(env, object, field));
 391             break;
 392 
 393         case JDWP_TAG(LONG):
 394             (void)outStream_writeLong(out,
 395                       JNI_FUNC_PTR(env,GetLongField)(env, object, field));
 396             break;
 397 
 398         case JDWP_TAG(SHORT):
 399             (void)outStream_writeShort(out,
 400                       JNI_FUNC_PTR(env,GetShortField)(env, object, field));
 401             break;
 402 
 403         case JDWP_TAG(BOOLEAN):
 404             (void)outStream_writeBoolean(out,
 405                       JNI_FUNC_PTR(env,GetBooleanField)(env, object, field));
 406             break;
 407     }
 408 }
 409 
 410 static void
 411 writeStaticFieldValue(JNIEnv *env, PacketOutputStream *out, jclass clazz,
 412                       jfieldID field)
 413 {
 414     jvmtiError error;
 415     char *signature = NULL;
 416     jbyte typeKey;
 417 
 418     error = fieldSignature(clazz, field, NULL, &signature, NULL);
 419     if (error != JVMTI_ERROR_NONE) {
 420         outStream_setError(out, map2jdwpError(error));
 421         return;
 422     }
 423     typeKey = jdwpTag(signature);
 424     jvmtiDeallocate(signature);
 425 
 426 
 427     if (isReferenceTag(typeKey)) {
 428 
 429         jobject value = JNI_FUNC_PTR(env,GetStaticObjectField)(env, clazz, field);
 430         (void)outStream_writeByte(out, specificTypeKey(env, value));
 431         (void)outStream_writeObjectRef(env, out, value);
 432 
 433         return;
 434     }
 435 
 436     /*
 437      * For primitive types, the type key is bounced back as is.
 438      */
 439     (void)outStream_writeByte(out, typeKey);
 440     switch (typeKey) {
 441         case JDWP_TAG(BYTE):
 442             (void)outStream_writeByte(out,
 443                       JNI_FUNC_PTR(env,GetStaticByteField)(env, clazz, field));
 444             break;
 445 
 446         case JDWP_TAG(CHAR):
 447             (void)outStream_writeChar(out,
 448                       JNI_FUNC_PTR(env,GetStaticCharField)(env, clazz, field));
 449             break;
 450 
 451         case JDWP_TAG(FLOAT):
 452             (void)outStream_writeFloat(out,
 453                       JNI_FUNC_PTR(env,GetStaticFloatField)(env, clazz, field));
 454             break;
 455 
 456         case JDWP_TAG(DOUBLE):
 457             (void)outStream_writeDouble(out,
 458                       JNI_FUNC_PTR(env,GetStaticDoubleField)(env, clazz, field));
 459             break;
 460 
 461         case JDWP_TAG(INT):
 462             (void)outStream_writeInt(out,
 463                       JNI_FUNC_PTR(env,GetStaticIntField)(env, clazz, field));
 464             break;
 465 
 466         case JDWP_TAG(LONG):
 467             (void)outStream_writeLong(out,
 468                       JNI_FUNC_PTR(env,GetStaticLongField)(env, clazz, field));
 469             break;
 470 
 471         case JDWP_TAG(SHORT):
 472             (void)outStream_writeShort(out,
 473                       JNI_FUNC_PTR(env,GetStaticShortField)(env, clazz, field));
 474             break;
 475 
 476         case JDWP_TAG(BOOLEAN):
 477             (void)outStream_writeBoolean(out,
 478                       JNI_FUNC_PTR(env,GetStaticBooleanField)(env, clazz, field));
 479             break;
 480     }
 481 }
 482 
 483 void
 484 sharedGetFieldValues(PacketInputStream *in, PacketOutputStream *out,
 485                      jboolean isStatic)
 486 {
 487     JNIEnv *env = getEnv();
 488     jint length;
 489     jobject object;
 490     jclass clazz;
 491 
 492     object = NULL;
 493     clazz  = NULL;
 494 
 495     if (isStatic) {
 496         clazz = inStream_readClassRef(env, in);
 497     } else {
 498         object = inStream_readObjectRef(env, in);
 499     }
 500 
 501     length = inStream_readInt(in);
 502     if (inStream_error(in)) {
 503         return;
 504     }
 505 
 506     WITH_LOCAL_REFS(env, length + 1) { /* +1 for class with instance fields */
 507 
 508         int i;
 509 
 510         (void)outStream_writeInt(out, length);
 511         for (i = 0; (i < length) && !outStream_error(out); i++) {
 512             jfieldID field = inStream_readFieldID(in);
 513 
 514             if (isStatic) {
 515                 writeStaticFieldValue(env, out, clazz, field);
 516             } else {
 517                 writeFieldValue(env, out, object, field);
 518             }
 519         }
 520 
 521     } END_WITH_LOCAL_REFS(env);
 522 }
 523 
 524 jboolean
 525 sharedInvoke(PacketInputStream *in, PacketOutputStream *out)
 526 {
 527     jvalue *arguments = NULL;
 528     jint options;
 529     jvmtiError error;
 530     jbyte invokeType;
 531     jclass clazz;
 532     jmethodID method;
 533     jint argumentCount;
 534     jobject instance;
 535     jthread thread;
 536     JNIEnv *env;
 537 
 538     /*
 539      * Instance methods start with the instance, thread and class,
 540      * and statics and constructors start with the class and then the
 541      * thread.
 542      */
 543     env = getEnv();
 544     if (inStream_command(in) == JDWP_COMMAND(ObjectReference, InvokeMethod)) {
 545         instance = inStream_readObjectRef(env, in);
 546         thread = inStream_readThreadRef(env, in);
 547         clazz = inStream_readClassRef(env, in);
 548     } else { /* static method or constructor */
 549         instance = NULL;
 550         clazz = inStream_readClassRef(env, in);
 551         thread = inStream_readThreadRef(env, in);
 552     }
 553 
 554     /*
 555      * ... and the rest of the packet is identical for all commands
 556      */
 557     method = inStream_readMethodID(in);
 558     argumentCount = inStream_readInt(in);
 559     if (inStream_error(in)) {
 560         return JNI_TRUE;
 561     }
 562 
 563     /* If count == 0, don't try and allocate 0 bytes, you'll get NULL */
 564     if ( argumentCount > 0 ) {
 565         int i;
 566         /*LINTED*/
 567         arguments = jvmtiAllocate(argumentCount * (jint)sizeof(*arguments));
 568         if (arguments == NULL) {
 569             outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));
 570             return JNI_TRUE;
 571         }
 572         for (i = 0; (i < argumentCount) && !inStream_error(in); i++) {
 573             arguments[i] = inStream_readValue(in);
 574         }
 575         if (inStream_error(in)) {
 576             return JNI_TRUE;
 577         }
 578     }
 579 
 580     options = inStream_readInt(in);
 581     if (inStream_error(in)) {
 582         if ( arguments != NULL ) {
 583             jvmtiDeallocate(arguments);
 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)
 632 {
 633     int i;
 634     int current;
 635 
 636     /* Squish out all of the debugger-spawned threads */
 637     for (i = 0, current = 0; i < count; i++) {
 638         jthread thread = threads[i];
 639         if (!threadControl_isDebugThread(thread)) {
 640             if (i > current) {
 641                 threads[current] = thread;
 642             }
 643             current++;
 644         }
 645     }
 646     return current;
 647 }
 648 
 649 jbyte
 650 referenceTypeTag(jclass clazz)
 651 {
 652     jbyte tag;
 653 
 654     if (isInterface(clazz)) {
 655         tag = JDWP_TYPE_TAG(INTERFACE);
 656     } else if (isArrayClass(clazz)) {
 657         tag = JDWP_TYPE_TAG(ARRAY);
 658     } else {
 659         tag = JDWP_TYPE_TAG(CLASS);
 660     }
 661 
 662     return tag;
 663 }
 664 
 665 /**
 666  * Get field modifiers
 667  */
 668 jvmtiError
 669 fieldModifiers(jclass clazz, jfieldID field, jint *pmodifiers)
 670 {
 671     jvmtiError error;
 672 
 673     *pmodifiers = 0;
 674     error = JVMTI_FUNC_PTR(gdata->jvmti,GetFieldModifiers)
 675             (gdata->jvmti, clazz, field, pmodifiers);
 676     return error;
 677 }
 678 
 679 /**
 680  * Get method modifiers
 681  */
 682 jvmtiError
 683 methodModifiers(jmethodID method, jint *pmodifiers)
 684 {
 685     jvmtiError error;
 686 
 687     *pmodifiers = 0;
 688     error = JVMTI_FUNC_PTR(gdata->jvmti,GetMethodModifiers)
 689             (gdata->jvmti, method, pmodifiers);
 690     return error;
 691 }
 692 
 693 /* Returns a local ref to the declaring class for a method, or NULL. */
 694 jvmtiError
 695 methodClass(jmethodID method, jclass *pclazz)
 696 {
 697     jvmtiError error;
 698 
 699     *pclazz = NULL;
 700     error = JVMTI_FUNC_PTR(gdata->jvmti,GetMethodDeclaringClass)
 701                                 (gdata->jvmti, method, pclazz);
 702     return error;
 703 }
 704 
 705 /* Returns the start and end locations of the specified method. */
 706 jvmtiError
 707 methodLocation(jmethodID method, jlocation *ploc1, jlocation *ploc2)
 708 {
 709     jvmtiError error;
 710 
 711     error = JVMTI_FUNC_PTR(gdata->jvmti,GetMethodLocation)
 712                                 (gdata->jvmti, method, ploc1, ploc2);
 713     return error;
 714 }
 715 
 716 /**
 717  * Get method signature
 718  */
 719 jvmtiError
 720 methodSignature(jmethodID method,
 721         char **pname, char **psignature, char **pgeneric_signature)
 722 {
 723     jvmtiError error;
 724     char *name = NULL;
 725     char *signature = NULL;
 726     char *generic_signature = NULL;
 727 
 728     error = JVMTI_FUNC_PTR(gdata->jvmti,GetMethodName)
 729             (gdata->jvmti, method, &name, &signature, &generic_signature);
 730 
 731     if ( pname != NULL ) {
 732         *pname = name;
 733     } else if ( name != NULL )  {
 734         jvmtiDeallocate(name);
 735     }
 736     if ( psignature != NULL ) {
 737         *psignature = signature;
 738     } else if ( signature != NULL ) {
 739         jvmtiDeallocate(signature);
 740     }
 741     if ( pgeneric_signature != NULL ) {
 742         *pgeneric_signature = generic_signature;
 743     } else if ( generic_signature != NULL )  {
 744         jvmtiDeallocate(generic_signature);
 745     }
 746     return error;
 747 }
 748 
 749 /*
 750  * Get the return type key of the method
 751  *     V or B C D F I J S Z L  [
 752  */
 753 jvmtiError
 754 methodReturnType(jmethodID method, char *typeKey)
 755 {
 756     char       *signature;
 757     jvmtiError  error;
 758 
 759     signature = NULL;
 760     error     = methodSignature(method, NULL, &signature, NULL);
 761     if (error == JVMTI_ERROR_NONE) {
 762         if (signature == NULL ) {
 763             error = AGENT_ERROR_INVALID_TAG;
 764         } else {
 765             char * xx;
 766 
 767             xx = strchr(signature, ')');
 768             if (xx == NULL || *(xx + 1) == 0) {
 769                 error = AGENT_ERROR_INVALID_TAG;
 770             } else {
 771                *typeKey = *(xx + 1);
 772             }
 773             jvmtiDeallocate(signature);
 774         }
 775     }
 776     return error;
 777 }
 778 
 779 
 780 /**
 781  * Return class loader for a class (must be inside a WITH_LOCAL_REFS)
 782  */
 783 jvmtiError
 784 classLoader(jclass clazz, jobject *pclazz)
 785 {
 786     jvmtiError error;
 787 
 788     *pclazz = NULL;
 789     error = JVMTI_FUNC_PTR(gdata->jvmti,GetClassLoader)
 790             (gdata->jvmti, clazz, pclazz);
 791     return error;
 792 }
 793 
 794 /**
 795  * Get field signature
 796  */
 797 jvmtiError
 798 fieldSignature(jclass clazz, jfieldID field,
 799         char **pname, char **psignature, char **pgeneric_signature)
 800 {
 801     jvmtiError error;
 802     char *name = NULL;
 803     char *signature = NULL;
 804     char *generic_signature = NULL;
 805 
 806     error = JVMTI_FUNC_PTR(gdata->jvmti,GetFieldName)
 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();
 847     jvmtiError error;
 848 
 849     LOG_MISC(("Spawning new thread: %s", name));
 850 
 851     WITH_LOCAL_REFS(env, 3) {
 852 
 853         jthread thread;
 854         jstring nameString;
 855 
 856         nameString = JNI_FUNC_PTR(env,NewStringUTF)(env, name);
 857         if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
 858             JNI_FUNC_PTR(env,ExceptionClear)(env);
 859             error = AGENT_ERROR_OUT_OF_MEMORY;
 860             goto err;
 861         }
 862 
 863         thread = JNI_FUNC_PTR(env,NewObject)
 864                         (env, gdata->threadClass, gdata->threadConstructor,
 865                                    gdata->systemThreadGroup, nameString);
 866         if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
 867             JNI_FUNC_PTR(env,ExceptionClear)(env);
 868             error = AGENT_ERROR_OUT_OF_MEMORY;
 869             goto err;
 870         }
 871 
 872         /*
 873          * Make the debugger thread a daemon
 874          */
 875         JNI_FUNC_PTR(env,CallVoidMethod)
 876                         (env, thread, gdata->threadSetDaemon, JNI_TRUE);
 877         if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
 878             JNI_FUNC_PTR(env,ExceptionClear)(env);
 879             error = AGENT_ERROR_JNI_EXCEPTION;
 880             goto err;
 881         }
 882 
 883         error = threadControl_addDebugThread(thread);
 884         if (error == JVMTI_ERROR_NONE) {
 885             /*
 886              * Debugger threads need cycles in all sorts of strange
 887              * situations (e.g. infinite cpu-bound loops), so give the
 888              * thread a high priority. Note that if the VM has an application
 889              * thread running at the max priority, there is still a chance
 890              * that debugger threads will be starved. (There needs to be
 891              * a way to give debugger threads a priority higher than any
 892              * application thread).
 893              */
 894             error = JVMTI_FUNC_PTR(gdata->jvmti,RunAgentThread)
 895                         (gdata->jvmti, thread, func, arg,
 896                                         JVMTI_THREAD_MAX_PRIORITY);
 897         }
 898 
 899         err: ;
 900 
 901     } END_WITH_LOCAL_REFS(env);
 902 
 903     return error;
 904 }
 905 
 906 jvmtiError
 907 jvmtiGetCapabilities(jvmtiCapabilities *caps)
 908 {
 909     if ( gdata->vmDead ) {
 910         return AGENT_ERROR_VM_DEAD;
 911     }
 912     if (!gdata->haveCachedJvmtiCapabilities) {
 913         jvmtiError error;
 914 
 915         error = JVMTI_FUNC_PTR(gdata->jvmti,GetCapabilities)
 916                         (gdata->jvmti, &(gdata->cachedJvmtiCapabilities));
 917         if (error != JVMTI_ERROR_NONE) {
 918             return error;
 919         }
 920         gdata->haveCachedJvmtiCapabilities = JNI_TRUE;
 921     }
 922 
 923     *caps = gdata->cachedJvmtiCapabilities;
 924 
 925     return JVMTI_ERROR_NONE;
 926 }
 927 
 928 static jint
 929 jvmtiVersion(void)
 930 {
 931     if (gdata->cachedJvmtiVersion == 0) {
 932         jvmtiError error;
 933         error = JVMTI_FUNC_PTR(gdata->jvmti,GetVersionNumber)
 934                         (gdata->jvmti, &(gdata->cachedJvmtiVersion));
 935         if (error != JVMTI_ERROR_NONE) {
 936             EXIT_ERROR(error, "on getting the JVMTI version number");
 937         }
 938     }
 939     return gdata->cachedJvmtiVersion;
 940 }
 941 
 942 jint
 943 jvmtiMajorVersion(void)
 944 {
 945     return (jvmtiVersion() & JVMTI_VERSION_MASK_MAJOR)
 946                     >> JVMTI_VERSION_SHIFT_MAJOR;
 947 }
 948 
 949 jint
 950 jvmtiMinorVersion(void)
 951 {
 952     return (jvmtiVersion() & JVMTI_VERSION_MASK_MINOR)
 953                     >> JVMTI_VERSION_SHIFT_MINOR;
 954 }
 955 
 956 jint
 957 jvmtiMicroVersion(void)
 958 {
 959     return (jvmtiVersion() & JVMTI_VERSION_MASK_MICRO)
 960                     >> JVMTI_VERSION_SHIFT_MICRO;
 961 }
 962 
 963 jvmtiError
 964 getSourceDebugExtension(jclass clazz, char **extensionPtr)
 965 {
 966     return JVMTI_FUNC_PTR(gdata->jvmti,GetSourceDebugExtension)
 967                 (gdata->jvmti, clazz, extensionPtr);
 968 }
 969 
 970 
 971 static void
 972 handleInterrupt(void)
 973 {
 974     /*
 975      * An interrupt is handled:
 976      *
 977      * 1) for running application threads by deferring the interrupt
 978      * until the current event handler has concluded.
 979      *
 980      * 2) for debugger threads by ignoring the interrupt; this is the
 981      * most robust solution since debugger threads don't use interrupts
 982      * to signal any condition.
 983      *
 984      * 3) for application threads that have not started or already
 985      * ended by ignoring the interrupt. In the former case, the application
 986      * is relying on timing to determine whether or not the thread sees
 987      * the interrupt; in the latter case, the interrupt is meaningless.
 988      */
 989     jthread thread = threadControl_currentThread();
 990     if ((thread != NULL) && (!threadControl_isDebugThread(thread))) {
 991         threadControl_setPendingInterrupt(thread);
 992     }
 993 }
 994 
 995 static jvmtiError
 996 ignore_vm_death(jvmtiError error)
 997 {
 998     if (error == JVMTI_ERROR_WRONG_PHASE) {
 999         LOG_MISC(("VM_DEAD, in debugMonitor*()?"));
1000         return JVMTI_ERROR_NONE; /* JVMTI does this, not JVMDI? */
1001     }
1002     return error;
1003 }
1004 
1005 void
1006 debugMonitorEnter(jrawMonitorID monitor)
1007 {
1008     jvmtiError error;
1009     error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorEnter)
1010             (gdata->jvmti, monitor);
1011     error = ignore_vm_death(error);
1012     if (error != JVMTI_ERROR_NONE) {
1013         EXIT_ERROR(error, "on raw monitor enter");
1014     }
1015 }
1016 
1017 void
1018 debugMonitorExit(jrawMonitorID monitor)
1019 {
1020     jvmtiError error;
1021 
1022     error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorExit)
1023                 (gdata->jvmti, monitor);
1024     error = ignore_vm_death(error);
1025     if (error != JVMTI_ERROR_NONE) {
1026         EXIT_ERROR(error, "on raw monitor exit");
1027     }
1028 }
1029 
1030 void
1031 debugMonitorWait(jrawMonitorID monitor)
1032 {
1033     jvmtiError error;
1034     error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorWait)
1035         (gdata->jvmti, monitor, ((jlong)(-1)));
1036 
1037     /*
1038      * According to the JLS (17.8), here we have
1039      * either :
1040      * a- been notified
1041      * b- gotten a spurious wakeup
1042      * c- been interrupted
1043      * If both a and c have happened, the VM must choose
1044      * which way to return - a or c.  If it chooses c
1045      * then the notify is gone - either to some other
1046      * thread that is also waiting, or it is dropped
1047      * on the floor.
1048      *
1049      * a is what we expect.  b won't hurt us any -
1050      * callers should be programmed to handle
1051      * spurious wakeups.  In case of c,
1052      * then the interrupt has been cleared, but
1053      * we don't want to consume it.  It came from
1054      * user code and is intended for user code, not us.
1055      * So, we will remember that the interrupt has
1056      * occurred and re-activate it when this thread
1057      * goes back into user code.
1058      * That being said, what do we do here?  Since
1059      * we could have been notified too, here we will
1060      * just pretend that we have been.  It won't hurt
1061      * anything to return in the same way as if
1062      * we were notified since callers have to be able to
1063      * handle spurious wakeups anyway.
1064      */
1065     if (error == JVMTI_ERROR_INTERRUPT) {
1066         handleInterrupt();
1067         error = JVMTI_ERROR_NONE;
1068     }
1069     error = ignore_vm_death(error);
1070     if (error != JVMTI_ERROR_NONE) {
1071         EXIT_ERROR(error, "on raw monitor wait");
1072     }
1073 }
1074 
1075 void
1076 debugMonitorNotify(jrawMonitorID monitor)
1077 {
1078     jvmtiError error;
1079 
1080     error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorNotify)
1081                 (gdata->jvmti, monitor);
1082     error = ignore_vm_death(error);
1083     if (error != JVMTI_ERROR_NONE) {
1084         EXIT_ERROR(error, "on raw monitor notify");
1085     }
1086 }
1087 
1088 void
1089 debugMonitorNotifyAll(jrawMonitorID monitor)
1090 {
1091     jvmtiError error;
1092 
1093     error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorNotifyAll)
1094                 (gdata->jvmti, monitor);
1095     error = ignore_vm_death(error);
1096     if (error != JVMTI_ERROR_NONE) {
1097         EXIT_ERROR(error, "on raw monitor notify all");
1098     }
1099 }
1100 
1101 jrawMonitorID
1102 debugMonitorCreate(char *name)
1103 {
1104     jrawMonitorID monitor;
1105     jvmtiError error;
1106 
1107     error = JVMTI_FUNC_PTR(gdata->jvmti,CreateRawMonitor)
1108                 (gdata->jvmti, name, &monitor);
1109     if (error != JVMTI_ERROR_NONE) {
1110         EXIT_ERROR(error, "on creation of a raw monitor");
1111     }
1112     return monitor;
1113 }
1114 
1115 void
1116 debugMonitorDestroy(jrawMonitorID monitor)
1117 {
1118     jvmtiError error;
1119 
1120     error = JVMTI_FUNC_PTR(gdata->jvmti,DestroyRawMonitor)
1121                 (gdata->jvmti, monitor);
1122     error = ignore_vm_death(error);
1123     if (error != JVMTI_ERROR_NONE) {
1124         EXIT_ERROR(error, "on destruction of raw monitor");
1125     }
1126 }
1127 
1128 /**
1129  * Return array of all threads (must be inside a WITH_LOCAL_REFS)
1130  */
1131 jthread *
1132 allThreads(jint *count)
1133 {
1134     jthread *threads;
1135     jvmtiError error;
1136 
1137     *count = 0;
1138     threads = NULL;
1139     error = JVMTI_FUNC_PTR(gdata->jvmti,GetAllThreads)
1140                 (gdata->jvmti, count, &threads);
1141     if (error == AGENT_ERROR_OUT_OF_MEMORY) {
1142         return NULL; /* Let caller deal with no memory? */
1143     }
1144     if (error != JVMTI_ERROR_NONE) {
1145         EXIT_ERROR(error, "getting all threads");
1146     }
1147     return threads;
1148 }
1149 
1150 /**
1151  * Fill the passed in structure with thread group info.
1152  * name field is JVMTI allocated.  parent is global ref.
1153  */
1154 void
1155 threadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo *info)
1156 {
1157     jvmtiError error;
1158 
1159     error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadGroupInfo)
1160                 (gdata->jvmti, group, info);
1161     if (error != JVMTI_ERROR_NONE) {
1162         EXIT_ERROR(error, "on getting thread group info");
1163     }
1164 }
1165 
1166 /**
1167  * Return class signature string
1168  */
1169 jvmtiError
1170 classSignature(jclass clazz, char **psignature, char **pgeneric_signature)
1171 {
1172     jvmtiError error;
1173     char *signature = NULL;
1174 
1175     /*
1176      * pgeneric_signature can be NULL, and GetClassSignature
1177      * accepts NULL.
1178      */
1179     error = JVMTI_FUNC_PTR(gdata->jvmti,GetClassSignature)
1180                 (gdata->jvmti, clazz, &signature, pgeneric_signature);
1181 
1182     if ( psignature != NULL ) {
1183         *psignature = signature;
1184     } else if ( signature != NULL )  {
1185         jvmtiDeallocate(signature);
1186     }
1187     return error;
1188 }
1189 
1190 /* Get class name (not signature) */
1191 char *
1192 getClassname(jclass clazz)
1193 {
1194     char *classname;
1195 
1196     classname = NULL;
1197     if ( clazz != NULL ) {
1198         if (classSignature(clazz, &classname, NULL) != JVMTI_ERROR_NONE) {
1199             classname = NULL;
1200         } else {
1201             /* Convert in place */
1202             convertSignatureToClassname(classname);
1203         }
1204     }
1205     return classname; /* Caller must free this memory */
1206 }
1207 
1208 void
1209 writeGenericSignature(PacketOutputStream *out, char *genericSignature)
1210 {
1211     if (genericSignature == NULL) {
1212         (void)outStream_writeString(out, "");
1213     } else {
1214         (void)outStream_writeString(out, genericSignature);
1215     }
1216 }
1217 
1218 jint
1219 classStatus(jclass clazz)
1220 {
1221     jint status;
1222     jvmtiError error;
1223 
1224     error = JVMTI_FUNC_PTR(gdata->jvmti,GetClassStatus)
1225                 (gdata->jvmti, clazz, &status);
1226     if (error != JVMTI_ERROR_NONE) {
1227         EXIT_ERROR(error, "on getting class status");
1228     }
1229     return status;
1230 }
1231 
1232 static jboolean
1233 isArrayClass(jclass clazz)
1234 {
1235     jboolean isArray = JNI_FALSE;
1236     jvmtiError error;
1237 
1238     error = JVMTI_FUNC_PTR(gdata->jvmti,IsArrayClass)
1239                 (gdata->jvmti, clazz, &isArray);
1240     if (error != JVMTI_ERROR_NONE) {
1241         EXIT_ERROR(error, "on checking for an array class");
1242     }
1243     return isArray;
1244 }
1245 
1246 static jboolean
1247 isInterface(jclass clazz)
1248 {
1249     jboolean isInterface = JNI_FALSE;
1250     jvmtiError error;
1251 
1252     error = JVMTI_FUNC_PTR(gdata->jvmti,IsInterface)
1253                 (gdata->jvmti, clazz, &isInterface);
1254     if (error != JVMTI_ERROR_NONE) {
1255         EXIT_ERROR(error, "on checking for an interface");
1256     }
1257     return isInterface;
1258 }
1259 
1260 jvmtiError
1261 isFieldSynthetic(jclass clazz, jfieldID field, jboolean *psynthetic)
1262 {
1263     jvmtiError error;
1264 
1265     error = JVMTI_FUNC_PTR(gdata->jvmti,IsFieldSynthetic)
1266                 (gdata->jvmti, clazz, field, psynthetic);
1267     if ( error == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) {
1268         /* If the query is not supported, we assume it is not synthetic. */
1269         *psynthetic = JNI_FALSE;
1270         return JVMTI_ERROR_NONE;
1271     }
1272     return error;
1273 }
1274 
1275 jvmtiError
1276 isMethodSynthetic(jmethodID method, jboolean *psynthetic)
1277 {
1278     jvmtiError error;
1279 
1280     error = JVMTI_FUNC_PTR(gdata->jvmti,IsMethodSynthetic)
1281                 (gdata->jvmti, method, psynthetic);
1282     if ( error == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) {
1283         /* If the query is not supported, we assume it is not synthetic. */
1284         *psynthetic = JNI_FALSE;
1285         return JVMTI_ERROR_NONE;
1286     }
1287     return error;
1288 }
1289 
1290 jboolean
1291 isMethodNative(jmethodID method)
1292 {
1293     jboolean isNative = JNI_FALSE;
1294     jvmtiError error;
1295 
1296     error = JVMTI_FUNC_PTR(gdata->jvmti,IsMethodNative)
1297                 (gdata->jvmti, method, &isNative);
1298     if (error != JVMTI_ERROR_NONE) {
1299         EXIT_ERROR(error, "on checking for a native interface");
1300     }
1301     return isNative;
1302 }
1303 
1304 jboolean
1305 isSameObject(JNIEnv *env, jobject o1, jobject o2)
1306 {
1307     if ( o1==o2 ) {
1308         return JNI_TRUE;
1309     }
1310     return FUNC_PTR(env,IsSameObject)(env, o1, o2);
1311 }
1312 
1313 jint
1314 objectHashCode(jobject object)
1315 {
1316     jint hashCode = 0;
1317     jvmtiError error;
1318 
1319     if ( object!=NULL ) {
1320         error = JVMTI_FUNC_PTR(gdata->jvmti,GetObjectHashCode)
1321                     (gdata->jvmti, object, &hashCode);
1322         if (error != JVMTI_ERROR_NONE) {
1323             EXIT_ERROR(error, "on getting an object hash code");
1324         }
1325     }
1326     return hashCode;
1327 }
1328 
1329 /* Get all implemented interfaces (must be inside a WITH_LOCAL_REFS) */
1330 jvmtiError
1331 allInterfaces(jclass clazz, jclass **ppinterfaces, jint *pcount)
1332 {
1333     jvmtiError error;
1334 
1335     *pcount = 0;
1336     *ppinterfaces = NULL;
1337     error = JVMTI_FUNC_PTR(gdata->jvmti,GetImplementedInterfaces)
1338                 (gdata->jvmti, clazz, pcount, ppinterfaces);
1339     return error;
1340 }
1341 
1342 /* Get all loaded classes (must be inside a WITH_LOCAL_REFS) */
1343 jvmtiError
1344 allLoadedClasses(jclass **ppclasses, jint *pcount)
1345 {
1346     jvmtiError error;
1347 
1348     *pcount = 0;
1349     *ppclasses = NULL;
1350     error = JVMTI_FUNC_PTR(gdata->jvmti,GetLoadedClasses)
1351                 (gdata->jvmti, pcount, ppclasses);
1352     return error;
1353 }
1354 
1355 /* Get all loaded classes for a loader (must be inside a WITH_LOCAL_REFS) */
1356 jvmtiError
1357 allClassLoaderClasses(jobject loader, jclass **ppclasses, jint *pcount)
1358 {
1359     jvmtiError error;
1360 
1361     *pcount = 0;
1362     *ppclasses = NULL;
1363     error = JVMTI_FUNC_PTR(gdata->jvmti,GetClassLoaderClasses)
1364                 (gdata->jvmti, loader, pcount, ppclasses);
1365     return error;
1366 }
1367 
1368 static jboolean
1369 is_a_nested_class(char *outer_sig, int outer_sig_len, char *sig, int sep)
1370 {
1371     char *inner;
1372 
1373     /* Assumed outer class signature is  "LOUTERCLASSNAME;"
1374      *         inner class signature is  "LOUTERCLASSNAME$INNERNAME;"
1375      *
1376      * INNERNAME can take the form:
1377      *    [0-9][1-9]*        anonymous class somewhere in the file
1378      *    [0-9][1-9]*NAME    local class somewhere in the OUTER class
1379      *    NAME               nested class in OUTER
1380      *
1381      * If NAME itself contains a $ (sep) then classname is further nested
1382      *    inside another class.
1383      *
1384      */
1385 
1386     /* Check prefix first */
1387     if ( strncmp(sig, outer_sig, outer_sig_len-1) != 0 ) {
1388         return JNI_FALSE;
1389     }
1390 
1391     /* Prefix must be followed by a $ (sep) */
1392     if ( sig[outer_sig_len-1] != sep ) {
1393         return JNI_FALSE;  /* No sep follows the match, must not be nested. */
1394     }
1395 
1396     /* Walk past any digits, if we reach the end, must be pure anonymous */
1397     inner = sig + outer_sig_len;
1398 #if 1 /* We want to return local classes */
1399     while ( *inner && isdigit(*inner) ) {
1400         inner++;
1401     }
1402     /* But anonymous class names can't be trusted. */
1403     if ( *inner == ';' ) {
1404         return JNI_FALSE;  /* A pure anonymous class */
1405     }
1406 #else
1407     if ( *inner && isdigit(*inner) ) {
1408         return JNI_FALSE;  /* A pure anonymous or local class */
1409     }
1410 #endif
1411 
1412     /* Nested deeper? */
1413     if ( strchr(inner, sep) != NULL ) {
1414         return JNI_FALSE;  /* Nested deeper than we want? */
1415     }
1416     return JNI_TRUE;
1417 }
1418 
1419 /* Get all nested classes for a class (must be inside a WITH_LOCAL_REFS) */
1420 jvmtiError
1421 allNestedClasses(jclass parent_clazz, jclass **ppnested, jint *pcount)
1422 {
1423     jvmtiError error;
1424     jobject parent_loader;
1425     jclass *classes;
1426     char *signature;
1427     size_t len;
1428     jint count;
1429     jint ncount;
1430     int i;
1431 
1432     *ppnested   = NULL;
1433     *pcount     = 0;
1434 
1435     parent_loader = NULL;
1436     classes       = NULL;
1437     signature     = NULL;
1438     count         = 0;
1439     ncount        = 0;
1440 
1441     error = classLoader(parent_clazz, &parent_loader);
1442     if (error != JVMTI_ERROR_NONE) {
1443         return error;
1444     }
1445     error = classSignature(parent_clazz, &signature, NULL);
1446     if (error != JVMTI_ERROR_NONE) {
1447         return error;
1448     }
1449     len = strlen(signature);
1450 
1451     error = allClassLoaderClasses(parent_loader, &classes, &count);
1452     if ( error != JVMTI_ERROR_NONE ) {
1453         jvmtiDeallocate(signature);
1454         return error;
1455     }
1456 
1457     for (i=0; i<count; i++) {
1458         jclass clazz;
1459         char *candidate_signature;
1460 
1461         clazz = classes[i];
1462         candidate_signature = NULL;
1463         error = classSignature(clazz, &candidate_signature, NULL);
1464         if (error != JVMTI_ERROR_NONE) {
1465             break;
1466         }
1467 
1468         if ( is_a_nested_class(signature, (int)len, candidate_signature, '$') ||
1469              is_a_nested_class(signature, (int)len, candidate_signature, '#') ) {
1470             /* Float nested classes to top */
1471             classes[i] = classes[ncount];
1472             classes[ncount++] = clazz;
1473         }
1474         jvmtiDeallocate(candidate_signature);
1475     }
1476 
1477     jvmtiDeallocate(signature);
1478 
1479     if ( count != 0 &&  ncount == 0 ) {
1480         jvmtiDeallocate(classes);
1481         classes = NULL;
1482     }
1483 
1484     *ppnested = classes;
1485     *pcount = ncount;
1486     return error;
1487 }
1488 
1489 void
1490 createLocalRefSpace(JNIEnv *env, jint capacity)
1491 {
1492     /*
1493      * Save current exception since it might get overwritten by
1494      * the calls below. Note we must depend on space in the existing
1495      * frame because asking for a new frame may generate an exception.
1496      */
1497     jobject throwable = JNI_FUNC_PTR(env,ExceptionOccurred)(env);
1498 
1499     /*
1500      * Use the current frame if necessary; otherwise create a new one
1501      */
1502     if (JNI_FUNC_PTR(env,PushLocalFrame)(env, capacity) < 0) {
1503         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"PushLocalFrame: Unable to push JNI frame");
1504     }
1505 
1506     /*
1507      * TO DO: This could be more efficient if it used EnsureLocalCapacity,
1508      * but that would not work if two functions on the call stack
1509      * use this function. We would need to either track reserved
1510      * references on a per-thread basis or come up with a convention
1511      * that would prevent two functions from depending on this function
1512      * at the same time.
1513      */
1514 
1515     /*
1516      * Restore exception state from before call
1517      */
1518     if (throwable != NULL) {
1519         JNI_FUNC_PTR(env,Throw)(env, throwable);
1520     } else {
1521         JNI_FUNC_PTR(env,ExceptionClear)(env);
1522     }
1523 }
1524 
1525 jboolean
1526 isClass(jobject object)
1527 {
1528     JNIEnv *env = getEnv();
1529     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classClass);
1530 }
1531 
1532 jboolean
1533 isVThread(jobject object)
1534 {
1535     JNIEnv *env = getEnv();
1536     return JNI_FUNC_PTR(env,IsVirtualThread)(env, object);
1537 }
1538 
1539 jboolean
1540 isThread(jobject object)
1541 {
1542     JNIEnv *env = getEnv();
1543     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadClass);
1544 }
1545 
1546 jboolean
1547 isThreadGroup(jobject object)
1548 {
1549     JNIEnv *env = getEnv();
1550     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->threadGroupClass);
1551 }
1552 
1553 jboolean
1554 isString(jobject object)
1555 {
1556     JNIEnv *env = getEnv();
1557     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->stringClass);
1558 }
1559 
1560 jboolean
1561 isClassLoader(jobject object)
1562 {
1563     JNIEnv *env = getEnv();
1564     return JNI_FUNC_PTR(env,IsInstanceOf)(env, object, gdata->classLoaderClass);
1565 }
1566 
1567 jboolean
1568 isArray(jobject object)
1569 {
1570     JNIEnv *env = getEnv();
1571     jboolean is;
1572 
1573     WITH_LOCAL_REFS(env, 1) {
1574         jclass clazz;
1575         clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object);
1576         is = isArrayClass(clazz);
1577     } END_WITH_LOCAL_REFS(env);
1578 
1579     return is;
1580 }
1581 
1582 /**
1583  * Return property value as jstring
1584  */
1585 static jstring
1586 getPropertyValue(JNIEnv *env, char *propertyName)
1587 {
1588     jstring valueString;
1589     jstring nameString;
1590 
1591     valueString = NULL;
1592 
1593     /* Create new String object to hold the property name */
1594     nameString = JNI_FUNC_PTR(env,NewStringUTF)(env, propertyName);
1595     if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
1596         JNI_FUNC_PTR(env,ExceptionClear)(env);
1597         /* NULL will be returned below */
1598     } else {
1599         /* Call valueString = System.getProperty(nameString) */
1600         valueString = JNI_FUNC_PTR(env,CallStaticObjectMethod)
1601             (env, gdata->systemClass, gdata->systemGetProperty, nameString);
1602         if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
1603             JNI_FUNC_PTR(env,ExceptionClear)(env);
1604             valueString = NULL;
1605         }
1606     }
1607     return valueString;
1608 }
1609 
1610 /**
1611  * Set an agent property
1612  */
1613 void
1614 setAgentPropertyValue(JNIEnv *env, char *propertyName, char* propertyValue)
1615 {
1616     jstring nameString;
1617     jstring valueString;
1618 
1619     if (gdata->agent_properties == NULL) {
1620         /* VMSupport doesn't exist; so ignore */
1621         return;
1622     }
1623 
1624     /* Create jstrings for property name and value */
1625     nameString = JNI_FUNC_PTR(env,NewStringUTF)(env, propertyName);
1626     if (nameString != NULL) {
1627         /* convert the value to UTF8 */
1628         int len;
1629         char *utf8value;
1630         int utf8maxSize;
1631 
1632         len = (int)strlen(propertyValue);
1633         utf8maxSize = len * 4 + 1;
1634         utf8value = (char *)jvmtiAllocate(utf8maxSize);
1635         if (utf8value != NULL) {
1636             utf8FromPlatform(propertyValue, len, (jbyte *)utf8value, utf8maxSize);
1637             valueString = JNI_FUNC_PTR(env, NewStringUTF)(env, utf8value);
1638             jvmtiDeallocate(utf8value);
1639 
1640             if (valueString != NULL) {
1641                 /* invoke Properties.setProperty */
1642                 JNI_FUNC_PTR(env,CallObjectMethod)
1643                     (env, gdata->agent_properties,
1644                      gdata->setProperty,
1645                      nameString, valueString);
1646             }
1647         }
1648     }
1649     if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {
1650         JNI_FUNC_PTR(env,ExceptionClear)(env);
1651     }
1652 }
1653 
1654 /**
1655  * Return property value as JDWP allocated string in UTF8 encoding
1656  */
1657 static char *
1658 getPropertyUTF8(JNIEnv *env, char *propertyName)
1659 {
1660     jvmtiError  error;
1661     char       *value;
1662 
1663     value = NULL;
1664     error = JVMTI_FUNC_PTR(gdata->jvmti,GetSystemProperty)
1665                 (gdata->jvmti, (const char *)propertyName, &value);
1666     if (error != JVMTI_ERROR_NONE) {
1667         jstring valueString;
1668 
1669         value = NULL;
1670         valueString = getPropertyValue(env, propertyName);
1671 
1672         if (valueString != NULL) {
1673             const char *utf;
1674 
1675             /* Get the UTF8 encoding for this property value string */
1676             utf = JNI_FUNC_PTR(env,GetStringUTFChars)(env, valueString, NULL);
1677             /* Make a copy for returning, release the JNI copy */
1678             value = jvmtiAllocate((int)strlen(utf) + 1);
1679             if (value != NULL) {
1680                 (void)strcpy(value, utf);
1681             }
1682             JNI_FUNC_PTR(env,ReleaseStringUTFChars)(env, valueString, utf);
1683         }
1684     }
1685     if ( value == NULL ) {
1686         ERROR_MESSAGE(("JDWP Can't get property value for %s", propertyName));
1687         EXIT_ERROR(AGENT_ERROR_NULL_POINTER,NULL);
1688     }
1689     return value;
1690 }
1691 
1692 jboolean
1693 isMethodObsolete(jmethodID method)
1694 {
1695     jvmtiError error;
1696     jboolean obsolete = JNI_TRUE;
1697 
1698     if ( method != NULL ) {
1699         error = JVMTI_FUNC_PTR(gdata->jvmti,IsMethodObsolete)
1700                     (gdata->jvmti, method, &obsolete);
1701         if (error != JVMTI_ERROR_NONE) {
1702             obsolete = JNI_TRUE;
1703         }
1704     }
1705     return obsolete;
1706 }
1707 
1708 /* Get the jvmti environment to be used with tags */
1709 jvmtiEnv *
1710 getSpecialJvmti(void)
1711 {
1712     jvmtiEnv  *jvmti;
1713     jvmtiError error;
1714     int        rc;
1715 
1716     /* Get one time use JVMTI Env */
1717     jvmtiCapabilities caps;
1718 
1719     rc = JVM_FUNC_PTR(gdata->jvm,GetEnv)
1720                      (gdata->jvm, (void **)&jvmti, JVMTI_VERSION);
1721     if (rc != JNI_OK) {
1722         return NULL;
1723     }
1724     (void)memset(&caps, 0, (int)sizeof(caps));
1725     caps.can_tag_objects = 1;
1726     error = JVMTI_FUNC_PTR(jvmti,AddCapabilities)(jvmti, &caps);
1727     if ( error != JVMTI_ERROR_NONE ) {
1728         return NULL;
1729     }
1730     return jvmti;
1731 }
1732 
1733 void
1734 writeCodeLocation(PacketOutputStream *out, jclass clazz,
1735                        jmethodID method, jlocation location)
1736 {
1737     jbyte tag;
1738 
1739     if (clazz != NULL) {
1740         tag = referenceTypeTag(clazz);
1741     } else {
1742         tag = JDWP_TYPE_TAG(CLASS);
1743     }
1744     (void)outStream_writeByte(out, tag);
1745     (void)outStream_writeObjectRef(getEnv(), out, clazz);
1746     (void)outStream_writeMethodID(out, isMethodObsolete(method)?NULL:method);
1747     (void)outStream_writeLocation(out, location);
1748 }
1749 
1750 void *
1751 jvmtiAllocate(jint numBytes)
1752 {
1753     void *ptr;
1754     jvmtiError error;
1755     if ( numBytes == 0 ) {
1756         return NULL;
1757     }
1758     error = JVMTI_FUNC_PTR(gdata->jvmti,Allocate)
1759                 (gdata->jvmti, numBytes, (unsigned char**)&ptr);
1760     if (error != JVMTI_ERROR_NONE ) {
1761         EXIT_ERROR(error, "Can't allocate jvmti memory");
1762     }
1763     return ptr;
1764 }
1765 
1766 void
1767 jvmtiDeallocate(void *ptr)
1768 {
1769     jvmtiError error;
1770     if ( ptr == NULL ) {
1771         return;
1772     }
1773     error = JVMTI_FUNC_PTR(gdata->jvmti,Deallocate)
1774                 (gdata->jvmti, ptr);
1775     if (error != JVMTI_ERROR_NONE ) {
1776         EXIT_ERROR(error, "Can't deallocate jvmti memory");
1777     }
1778 }
1779 
1780 /* Rarely needed, transport library uses JDWP errors, only use? */
1781 jvmtiError
1782 map2jvmtiError(jdwpError error)
1783 {
1784     switch ( error ) {
1785         case JDWP_ERROR(NONE):
1786             return JVMTI_ERROR_NONE;
1787         case JDWP_ERROR(INVALID_THREAD):
1788             return JVMTI_ERROR_INVALID_THREAD;
1789         case JDWP_ERROR(INVALID_THREAD_GROUP):
1790             return JVMTI_ERROR_INVALID_THREAD_GROUP;
1791         case JDWP_ERROR(INVALID_PRIORITY):
1792             return JVMTI_ERROR_INVALID_PRIORITY;
1793         case JDWP_ERROR(THREAD_NOT_SUSPENDED):
1794             return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1795         case JDWP_ERROR(THREAD_SUSPENDED):
1796             return JVMTI_ERROR_THREAD_SUSPENDED;
1797         case JDWP_ERROR(INVALID_OBJECT):
1798             return JVMTI_ERROR_INVALID_OBJECT;
1799         case JDWP_ERROR(INVALID_CLASS):
1800             return JVMTI_ERROR_INVALID_CLASS;
1801         case JDWP_ERROR(CLASS_NOT_PREPARED):
1802             return JVMTI_ERROR_CLASS_NOT_PREPARED;
1803         case JDWP_ERROR(INVALID_METHODID):
1804             return JVMTI_ERROR_INVALID_METHODID;
1805         case JDWP_ERROR(INVALID_LOCATION):
1806             return JVMTI_ERROR_INVALID_LOCATION;
1807         case JDWP_ERROR(INVALID_FIELDID):
1808             return JVMTI_ERROR_INVALID_FIELDID;
1809         case JDWP_ERROR(INVALID_FRAMEID):
1810             return AGENT_ERROR_INVALID_FRAMEID;
1811         case JDWP_ERROR(NO_MORE_FRAMES):
1812             return JVMTI_ERROR_NO_MORE_FRAMES;
1813         case JDWP_ERROR(OPAQUE_FRAME):
1814             return JVMTI_ERROR_OPAQUE_FRAME;
1815         case JDWP_ERROR(NOT_CURRENT_FRAME):
1816             return AGENT_ERROR_NOT_CURRENT_FRAME;
1817         case JDWP_ERROR(TYPE_MISMATCH):
1818             return JVMTI_ERROR_TYPE_MISMATCH;
1819         case JDWP_ERROR(INVALID_SLOT):
1820             return JVMTI_ERROR_INVALID_SLOT;
1821         case JDWP_ERROR(DUPLICATE):
1822             return JVMTI_ERROR_DUPLICATE;
1823         case JDWP_ERROR(NOT_FOUND):
1824             return JVMTI_ERROR_NOT_FOUND;
1825         case JDWP_ERROR(INVALID_MONITOR):
1826             return JVMTI_ERROR_INVALID_MONITOR;
1827         case JDWP_ERROR(NOT_MONITOR_OWNER):
1828             return JVMTI_ERROR_NOT_MONITOR_OWNER;
1829         case JDWP_ERROR(INTERRUPT):
1830             return JVMTI_ERROR_INTERRUPT;
1831         case JDWP_ERROR(INVALID_CLASS_FORMAT):
1832             return JVMTI_ERROR_INVALID_CLASS_FORMAT;
1833         case JDWP_ERROR(CIRCULAR_CLASS_DEFINITION):
1834             return JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION;
1835         case JDWP_ERROR(FAILS_VERIFICATION):
1836             return JVMTI_ERROR_FAILS_VERIFICATION;
1837         case JDWP_ERROR(ADD_METHOD_NOT_IMPLEMENTED):
1838             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
1839         case JDWP_ERROR(SCHEMA_CHANGE_NOT_IMPLEMENTED):
1840             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
1841         case JDWP_ERROR(INVALID_TYPESTATE):
1842             return JVMTI_ERROR_INVALID_TYPESTATE;
1843         case JDWP_ERROR(HIERARCHY_CHANGE_NOT_IMPLEMENTED):
1844             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
1845         case JDWP_ERROR(DELETE_METHOD_NOT_IMPLEMENTED):
1846             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED;
1847         case JDWP_ERROR(UNSUPPORTED_VERSION):
1848             return JVMTI_ERROR_UNSUPPORTED_VERSION;
1849         case JDWP_ERROR(NAMES_DONT_MATCH):
1850             return JVMTI_ERROR_NAMES_DONT_MATCH;
1851         case JDWP_ERROR(CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED):
1852             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED;
1853         case JDWP_ERROR(METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED):
1854             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED;
1855         case JDWP_ERROR(CLASS_ATTRIBUTE_CHANGE_NOT_IMPLEMENTED):
1856             return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
1857         case JDWP_ERROR(NOT_IMPLEMENTED):
1858             return JVMTI_ERROR_NOT_AVAILABLE;
1859         case JDWP_ERROR(NULL_POINTER):
1860             return JVMTI_ERROR_NULL_POINTER;
1861         case JDWP_ERROR(ABSENT_INFORMATION):
1862             return JVMTI_ERROR_ABSENT_INFORMATION;
1863         case JDWP_ERROR(INVALID_EVENT_TYPE):
1864             return JVMTI_ERROR_INVALID_EVENT_TYPE;
1865         case JDWP_ERROR(ILLEGAL_ARGUMENT):
1866             return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1867         case JDWP_ERROR(OUT_OF_MEMORY):
1868             return JVMTI_ERROR_OUT_OF_MEMORY;
1869         case JDWP_ERROR(ACCESS_DENIED):
1870             return JVMTI_ERROR_ACCESS_DENIED;
1871         case JDWP_ERROR(VM_DEAD):
1872             return JVMTI_ERROR_WRONG_PHASE;
1873         case JDWP_ERROR(UNATTACHED_THREAD):
1874             return JVMTI_ERROR_UNATTACHED_THREAD;
1875         case JDWP_ERROR(INVALID_TAG):
1876             return AGENT_ERROR_INVALID_TAG;
1877         case JDWP_ERROR(ALREADY_INVOKING):
1878             return AGENT_ERROR_ALREADY_INVOKING;
1879         case JDWP_ERROR(INVALID_INDEX):
1880             return AGENT_ERROR_INVALID_INDEX;
1881         case JDWP_ERROR(INVALID_LENGTH):
1882             return AGENT_ERROR_INVALID_LENGTH;
1883         case JDWP_ERROR(INVALID_STRING):
1884             return AGENT_ERROR_INVALID_STRING;
1885         case JDWP_ERROR(INVALID_CLASS_LOADER):
1886             return AGENT_ERROR_INVALID_CLASS_LOADER;
1887         case JDWP_ERROR(INVALID_ARRAY):
1888             return AGENT_ERROR_INVALID_ARRAY;
1889         case JDWP_ERROR(TRANSPORT_LOAD):
1890             return AGENT_ERROR_TRANSPORT_LOAD;
1891         case JDWP_ERROR(TRANSPORT_INIT):
1892             return AGENT_ERROR_TRANSPORT_INIT;
1893         case JDWP_ERROR(NATIVE_METHOD):
1894             return AGENT_ERROR_NATIVE_METHOD;
1895         case JDWP_ERROR(INVALID_COUNT):
1896             return AGENT_ERROR_INVALID_COUNT;
1897         case JDWP_ERROR(INTERNAL):
1898             return AGENT_ERROR_JDWP_INTERNAL;
1899     }
1900     return AGENT_ERROR_INTERNAL;
1901 }
1902 
1903 static jvmtiEvent index2jvmti[EI_max-EI_min+1];
1904 static jdwpEvent  index2jdwp [EI_max-EI_min+1];
1905 
1906 void
1907 eventIndexInit(void)
1908 {
1909     (void)memset(index2jvmti, 0, (int)sizeof(index2jvmti));
1910     (void)memset(index2jdwp,  0, (int)sizeof(index2jdwp));
1911 
1912     index2jvmti[EI_SINGLE_STEP        -EI_min] = JVMTI_EVENT_SINGLE_STEP;
1913     index2jvmti[EI_BREAKPOINT         -EI_min] = JVMTI_EVENT_BREAKPOINT;
1914     index2jvmti[EI_FRAME_POP          -EI_min] = JVMTI_EVENT_FRAME_POP;
1915     index2jvmti[EI_EXCEPTION          -EI_min] = JVMTI_EVENT_EXCEPTION;
1916     index2jvmti[EI_THREAD_START       -EI_min] = JVMTI_EVENT_THREAD_START;
1917     index2jvmti[EI_THREAD_END         -EI_min] = JVMTI_EVENT_THREAD_END;
1918     index2jvmti[EI_CLASS_PREPARE      -EI_min] = JVMTI_EVENT_CLASS_PREPARE;
1919     index2jvmti[EI_CLASS_UNLOAD       -EI_min] = 0; // No mapping to JVMTI event
1920     index2jvmti[EI_CLASS_LOAD         -EI_min] = JVMTI_EVENT_CLASS_LOAD;
1921     index2jvmti[EI_FIELD_ACCESS       -EI_min] = JVMTI_EVENT_FIELD_ACCESS;
1922     index2jvmti[EI_FIELD_MODIFICATION -EI_min] = JVMTI_EVENT_FIELD_MODIFICATION;
1923     index2jvmti[EI_EXCEPTION_CATCH    -EI_min] = JVMTI_EVENT_EXCEPTION_CATCH;
1924     index2jvmti[EI_METHOD_ENTRY       -EI_min] = JVMTI_EVENT_METHOD_ENTRY;
1925     index2jvmti[EI_METHOD_EXIT        -EI_min] = JVMTI_EVENT_METHOD_EXIT;
1926     index2jvmti[EI_MONITOR_CONTENDED_ENTER      -EI_min] = JVMTI_EVENT_MONITOR_CONTENDED_ENTER;
1927     index2jvmti[EI_MONITOR_CONTENDED_ENTERED    -EI_min] = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED;
1928     index2jvmti[EI_MONITOR_WAIT       -EI_min] = JVMTI_EVENT_MONITOR_WAIT;
1929     index2jvmti[EI_MONITOR_WAITED     -EI_min] = JVMTI_EVENT_MONITOR_WAITED;
1930     index2jvmti[EI_VM_INIT            -EI_min] = JVMTI_EVENT_VM_INIT;
1931     index2jvmti[EI_VM_DEATH           -EI_min] = JVMTI_EVENT_VM_DEATH;
1932     index2jvmti[EI_VIRTUAL_THREAD_START -EI_min] = JVMTI_EVENT_VIRTUAL_THREAD_START;
1933     index2jvmti[EI_VIRTUAL_THREAD_END   -EI_min] = JVMTI_EVENT_VIRTUAL_THREAD_END;
1934 
1935     index2jdwp[EI_SINGLE_STEP         -EI_min] = JDWP_EVENT(SINGLE_STEP);
1936     index2jdwp[EI_BREAKPOINT          -EI_min] = JDWP_EVENT(BREAKPOINT);
1937     index2jdwp[EI_FRAME_POP           -EI_min] = JDWP_EVENT(FRAME_POP);
1938     index2jdwp[EI_EXCEPTION           -EI_min] = JDWP_EVENT(EXCEPTION);
1939     index2jdwp[EI_THREAD_START        -EI_min] = JDWP_EVENT(THREAD_START);
1940     index2jdwp[EI_THREAD_END          -EI_min] = JDWP_EVENT(THREAD_END);
1941     index2jdwp[EI_CLASS_PREPARE       -EI_min] = JDWP_EVENT(CLASS_PREPARE);
1942     index2jdwp[EI_CLASS_UNLOAD        -EI_min] = JDWP_EVENT(CLASS_UNLOAD);
1943     index2jdwp[EI_CLASS_LOAD          -EI_min] = JDWP_EVENT(CLASS_LOAD);
1944     index2jdwp[EI_FIELD_ACCESS        -EI_min] = JDWP_EVENT(FIELD_ACCESS);
1945     index2jdwp[EI_FIELD_MODIFICATION  -EI_min] = JDWP_EVENT(FIELD_MODIFICATION);
1946     index2jdwp[EI_EXCEPTION_CATCH     -EI_min] = JDWP_EVENT(EXCEPTION_CATCH);
1947     index2jdwp[EI_METHOD_ENTRY        -EI_min] = JDWP_EVENT(METHOD_ENTRY);
1948     index2jdwp[EI_METHOD_EXIT         -EI_min] = JDWP_EVENT(METHOD_EXIT);
1949     index2jdwp[EI_MONITOR_CONTENDED_ENTER             -EI_min] = JDWP_EVENT(MONITOR_CONTENDED_ENTER);
1950     index2jdwp[EI_MONITOR_CONTENDED_ENTERED           -EI_min] = JDWP_EVENT(MONITOR_CONTENDED_ENTERED);
1951     index2jdwp[EI_MONITOR_WAIT        -EI_min] = JDWP_EVENT(MONITOR_WAIT);
1952     index2jdwp[EI_MONITOR_WAITED      -EI_min] = JDWP_EVENT(MONITOR_WAITED);
1953     index2jdwp[EI_VM_INIT             -EI_min] = JDWP_EVENT(VM_INIT);
1954     index2jdwp[EI_VM_DEATH            -EI_min] = JDWP_EVENT(VM_DEATH);
1955     /* Just map VIRTUAL_THREAD_START/END to THREAD_START/END. */
1956     index2jdwp[EI_VIRTUAL_THREAD_START -EI_min] = JDWP_EVENT(THREAD_START);
1957     index2jdwp[EI_VIRTUAL_THREAD_END   -EI_min] = JDWP_EVENT(THREAD_END);
1958 }
1959 
1960 jdwpEvent
1961 eventIndex2jdwp(EventIndex ei)
1962 {
1963     jdwpEvent event = 0;
1964     if (ei >= EI_min && ei <= EI_max) {
1965         event = index2jdwp[ei - EI_min];
1966     }
1967     if (event == 0) {
1968         EXIT_ERROR(AGENT_ERROR_INVALID_INDEX, "bad EventIndex");
1969     }
1970     return event;
1971 }
1972 
1973 jvmtiEvent
1974 eventIndex2jvmti(EventIndex ei)
1975 {
1976     jvmtiEvent event = 0;
1977     if (ei >= EI_min && ei <= EI_max) {
1978         event = index2jvmti[ei - EI_min];
1979     }
1980     if (event == 0) {
1981         EXIT_ERROR(AGENT_ERROR_INVALID_INDEX, "bad EventIndex");
1982     }
1983     return event;
1984 }
1985 
1986 #ifdef DEBUG
1987 
1988 char*
1989 eventIndex2EventName(EventIndex ei)
1990 {
1991     switch ( ei ) {
1992         case EI_SINGLE_STEP:
1993             return "EI_SINGLE_STEP";
1994         case EI_BREAKPOINT:
1995             return "EI_BREAKPOINT";
1996         case EI_FRAME_POP:
1997             return "EI_FRAME_POP";
1998         case EI_EXCEPTION:
1999             return "EI_EXCEPTION";
2000         case EI_THREAD_START:
2001             return "EI_THREAD_START";
2002         case EI_THREAD_END:
2003             return "EI_THREAD_END";
2004         case EI_CLASS_PREPARE:
2005             return "EI_CLASS_PREPARE";
2006         case EI_CLASS_UNLOAD:
2007             return "EI_CLASS_UNLOAD";
2008         case EI_CLASS_LOAD:
2009             return "EI_CLASS_LOAD";
2010         case EI_FIELD_ACCESS:
2011             return "EI_FIELD_ACCESS";
2012         case EI_FIELD_MODIFICATION:
2013             return "EI_FIELD_MODIFICATION";
2014         case EI_EXCEPTION_CATCH:
2015             return "EI_EXCEPTION_CATCH";
2016         case EI_METHOD_ENTRY:
2017             return "EI_METHOD_ENTRY";
2018         case EI_METHOD_EXIT:
2019             return "EI_METHOD_EXIT";
2020         case EI_MONITOR_CONTENDED_ENTER:
2021             return "EI_MONITOR_CONTENDED_ENTER";
2022         case EI_MONITOR_CONTENDED_ENTERED:
2023             return "EI_MONITOR_CONTENDED_ENTERED";
2024         case EI_MONITOR_WAIT:
2025             return "EI_MONITOR_WAIT";
2026         case EI_MONITOR_WAITED:
2027             return "EI_MONITOR_WAITED";
2028         case EI_VM_INIT:
2029             return "EI_VM_INIT";
2030         case EI_VM_DEATH:
2031             return "EI_VM_DEATH";
2032         case EI_VIRTUAL_THREAD_START:
2033             return "EI_VIRTUAL_THREAD_START";
2034         case EI_VIRTUAL_THREAD_END:
2035             return "EI_VIRTUAL_THREAD_END";
2036         default:
2037             JDI_ASSERT(JNI_FALSE);
2038             return "Bad EI";
2039     }
2040 }
2041 
2042 #endif
2043 
2044 EventIndex
2045 jdwp2EventIndex(jdwpEvent eventType)
2046 {
2047     switch ( eventType ) {
2048         case JDWP_EVENT(SINGLE_STEP):
2049             return EI_SINGLE_STEP;
2050         case JDWP_EVENT(BREAKPOINT):
2051             return EI_BREAKPOINT;
2052         case JDWP_EVENT(FRAME_POP):
2053             return EI_FRAME_POP;
2054         case JDWP_EVENT(EXCEPTION):
2055             return EI_EXCEPTION;
2056         case JDWP_EVENT(THREAD_START):
2057             return EI_THREAD_START;
2058         case JDWP_EVENT(THREAD_END):
2059             return EI_THREAD_END;
2060         case JDWP_EVENT(CLASS_PREPARE):
2061             return EI_CLASS_PREPARE;
2062         case JDWP_EVENT(CLASS_UNLOAD):
2063             return EI_CLASS_UNLOAD;
2064         case JDWP_EVENT(CLASS_LOAD):
2065             return EI_CLASS_LOAD;
2066         case JDWP_EVENT(FIELD_ACCESS):
2067             return EI_FIELD_ACCESS;
2068         case JDWP_EVENT(FIELD_MODIFICATION):
2069             return EI_FIELD_MODIFICATION;
2070         case JDWP_EVENT(EXCEPTION_CATCH):
2071             return EI_EXCEPTION_CATCH;
2072         case JDWP_EVENT(METHOD_ENTRY):
2073             return EI_METHOD_ENTRY;
2074         case JDWP_EVENT(METHOD_EXIT):
2075             return EI_METHOD_EXIT;
2076         case JDWP_EVENT(METHOD_EXIT_WITH_RETURN_VALUE):
2077             return EI_METHOD_EXIT;
2078         case JDWP_EVENT(MONITOR_CONTENDED_ENTER):
2079             return EI_MONITOR_CONTENDED_ENTER;
2080         case JDWP_EVENT(MONITOR_CONTENDED_ENTERED):
2081             return EI_MONITOR_CONTENDED_ENTERED;
2082         case JDWP_EVENT(MONITOR_WAIT):
2083             return EI_MONITOR_WAIT;
2084         case JDWP_EVENT(MONITOR_WAITED):
2085             return EI_MONITOR_WAITED;
2086         case JDWP_EVENT(VM_INIT):
2087             return EI_VM_INIT;
2088         case JDWP_EVENT(VM_DEATH):
2089             return EI_VM_DEATH;
2090         default:
2091             break;
2092     }
2093 
2094     /*
2095      * Event type not recognized - don't exit with error as caller
2096      * may wish to return error to debugger.
2097      */
2098     return (EventIndex)0;
2099 }
2100 
2101 EventIndex
2102 jvmti2EventIndex(jvmtiEvent kind)
2103 {
2104     switch ( kind ) {
2105         case JVMTI_EVENT_SINGLE_STEP:
2106             return EI_SINGLE_STEP;
2107         case JVMTI_EVENT_BREAKPOINT:
2108             return EI_BREAKPOINT;
2109         case JVMTI_EVENT_FRAME_POP:
2110             return EI_FRAME_POP;
2111         case JVMTI_EVENT_EXCEPTION:
2112             return EI_EXCEPTION;
2113         case JVMTI_EVENT_THREAD_START:
2114             return EI_THREAD_START;
2115         case JVMTI_EVENT_THREAD_END:
2116             return EI_THREAD_END;
2117         case JVMTI_EVENT_CLASS_PREPARE:
2118             return EI_CLASS_PREPARE;
2119         case JVMTI_EVENT_CLASS_LOAD:
2120             return EI_CLASS_LOAD;
2121         case JVMTI_EVENT_FIELD_ACCESS:
2122             return EI_FIELD_ACCESS;
2123         case JVMTI_EVENT_FIELD_MODIFICATION:
2124             return EI_FIELD_MODIFICATION;
2125         case JVMTI_EVENT_EXCEPTION_CATCH:
2126             return EI_EXCEPTION_CATCH;
2127         case JVMTI_EVENT_METHOD_ENTRY:
2128             return EI_METHOD_ENTRY;
2129         case JVMTI_EVENT_METHOD_EXIT:
2130             return EI_METHOD_EXIT;
2131         /*
2132          * There is no JVMTI_EVENT_METHOD_EXIT_WITH_RETURN_VALUE.
2133          * The normal JVMTI_EVENT_METHOD_EXIT always contains the return value.
2134          */
2135         case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
2136             return EI_MONITOR_CONTENDED_ENTER;
2137         case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
2138             return EI_MONITOR_CONTENDED_ENTERED;
2139         case JVMTI_EVENT_MONITOR_WAIT:
2140             return EI_MONITOR_WAIT;
2141         case JVMTI_EVENT_MONITOR_WAITED:
2142             return EI_MONITOR_WAITED;
2143         case JVMTI_EVENT_VM_INIT:
2144             return EI_VM_INIT;
2145         case JVMTI_EVENT_VM_DEATH:
2146             return EI_VM_DEATH;
2147         /* vthread events */
2148         case JVMTI_EVENT_VIRTUAL_THREAD_START:
2149             return EI_VIRTUAL_THREAD_START;
2150         case JVMTI_EVENT_VIRTUAL_THREAD_END:
2151             return EI_VIRTUAL_THREAD_END;
2152 
2153         default:
2154             EXIT_ERROR(AGENT_ERROR_INVALID_INDEX,"JVMTI to EventIndex mapping");
2155             break;
2156     }
2157     return (EventIndex)0;
2158 }
2159 
2160 /* This routine is commonly used, maps jvmti and agent errors to the best
2161  *    jdwp error code we can map to.
2162  */
2163 jdwpError
2164 map2jdwpError(jvmtiError error)
2165 {
2166     switch ( (int)error ) {
2167         case JVMTI_ERROR_NONE:
2168             return JDWP_ERROR(NONE);
2169         case AGENT_ERROR_INVALID_THREAD:
2170         case JVMTI_ERROR_INVALID_THREAD:
2171             return JDWP_ERROR(INVALID_THREAD);
2172         case JVMTI_ERROR_INVALID_THREAD_GROUP:
2173             return JDWP_ERROR(INVALID_THREAD_GROUP);
2174         case JVMTI_ERROR_INVALID_PRIORITY:
2175             return JDWP_ERROR(INVALID_PRIORITY);
2176         case JVMTI_ERROR_THREAD_NOT_SUSPENDED:
2177             return JDWP_ERROR(THREAD_NOT_SUSPENDED);
2178         case JVMTI_ERROR_THREAD_SUSPENDED:
2179             return JDWP_ERROR(THREAD_SUSPENDED);
2180         case JVMTI_ERROR_THREAD_NOT_ALIVE:
2181             return JDWP_ERROR(INVALID_THREAD);
2182         case AGENT_ERROR_INVALID_OBJECT:
2183         case JVMTI_ERROR_INVALID_OBJECT:
2184             return JDWP_ERROR(INVALID_OBJECT);
2185         case JVMTI_ERROR_INVALID_CLASS:
2186             return JDWP_ERROR(INVALID_CLASS);
2187         case JVMTI_ERROR_CLASS_NOT_PREPARED:
2188             return JDWP_ERROR(CLASS_NOT_PREPARED);
2189         case JVMTI_ERROR_INVALID_METHODID:
2190             return JDWP_ERROR(INVALID_METHODID);
2191         case JVMTI_ERROR_INVALID_LOCATION:
2192             return JDWP_ERROR(INVALID_LOCATION);
2193         case JVMTI_ERROR_INVALID_FIELDID:
2194             return JDWP_ERROR(INVALID_FIELDID);
2195         case AGENT_ERROR_NO_MORE_FRAMES:
2196         case JVMTI_ERROR_NO_MORE_FRAMES:
2197             return JDWP_ERROR(NO_MORE_FRAMES);
2198         case JVMTI_ERROR_OPAQUE_FRAME:
2199             return JDWP_ERROR(OPAQUE_FRAME);
2200         case JVMTI_ERROR_TYPE_MISMATCH:
2201             return JDWP_ERROR(TYPE_MISMATCH);
2202         case JVMTI_ERROR_INVALID_SLOT:
2203             return JDWP_ERROR(INVALID_SLOT);
2204         case JVMTI_ERROR_DUPLICATE:
2205             return JDWP_ERROR(DUPLICATE);
2206         case JVMTI_ERROR_NOT_FOUND:
2207             return JDWP_ERROR(NOT_FOUND);
2208         case JVMTI_ERROR_INVALID_MONITOR:
2209             return JDWP_ERROR(INVALID_MONITOR);
2210         case JVMTI_ERROR_NOT_MONITOR_OWNER:
2211             return JDWP_ERROR(NOT_MONITOR_OWNER);
2212         case JVMTI_ERROR_INTERRUPT:
2213             return JDWP_ERROR(INTERRUPT);
2214         case JVMTI_ERROR_INVALID_CLASS_FORMAT:
2215             return JDWP_ERROR(INVALID_CLASS_FORMAT);
2216         case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
2217             return JDWP_ERROR(CIRCULAR_CLASS_DEFINITION);
2218         case JVMTI_ERROR_FAILS_VERIFICATION:
2219             return JDWP_ERROR(FAILS_VERIFICATION);
2220         case JVMTI_ERROR_INVALID_TYPESTATE:
2221             return JDWP_ERROR(INVALID_TYPESTATE);
2222         case JVMTI_ERROR_UNSUPPORTED_VERSION:
2223             return JDWP_ERROR(UNSUPPORTED_VERSION);
2224         case JVMTI_ERROR_NAMES_DONT_MATCH:
2225             return JDWP_ERROR(NAMES_DONT_MATCH);
2226         case AGENT_ERROR_NULL_POINTER:
2227         case JVMTI_ERROR_NULL_POINTER:
2228             return JDWP_ERROR(NULL_POINTER);
2229         case JVMTI_ERROR_ABSENT_INFORMATION:
2230             return JDWP_ERROR(ABSENT_INFORMATION);
2231         case AGENT_ERROR_INVALID_EVENT_TYPE:
2232         case JVMTI_ERROR_INVALID_EVENT_TYPE:
2233             return JDWP_ERROR(INVALID_EVENT_TYPE);
2234         case AGENT_ERROR_ILLEGAL_ARGUMENT:
2235         case JVMTI_ERROR_ILLEGAL_ARGUMENT:
2236             return JDWP_ERROR(ILLEGAL_ARGUMENT);
2237         case JVMTI_ERROR_OUT_OF_MEMORY:
2238         case AGENT_ERROR_OUT_OF_MEMORY:
2239             return JDWP_ERROR(OUT_OF_MEMORY);
2240         case JVMTI_ERROR_ACCESS_DENIED:
2241             return JDWP_ERROR(ACCESS_DENIED);
2242         case JVMTI_ERROR_WRONG_PHASE:
2243         case AGENT_ERROR_VM_DEAD:
2244         case AGENT_ERROR_NO_JNI_ENV:
2245             return JDWP_ERROR(VM_DEAD);
2246         case AGENT_ERROR_JNI_EXCEPTION:
2247         case JVMTI_ERROR_UNATTACHED_THREAD:
2248             return JDWP_ERROR(UNATTACHED_THREAD);
2249         case JVMTI_ERROR_NOT_AVAILABLE:
2250         case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
2251             return JDWP_ERROR(NOT_IMPLEMENTED);
2252         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
2253             return JDWP_ERROR(HIERARCHY_CHANGE_NOT_IMPLEMENTED);
2254         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
2255             return JDWP_ERROR(DELETE_METHOD_NOT_IMPLEMENTED);
2256         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
2257             return JDWP_ERROR(ADD_METHOD_NOT_IMPLEMENTED);
2258         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
2259             return JDWP_ERROR(SCHEMA_CHANGE_NOT_IMPLEMENTED);
2260         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
2261             return JDWP_ERROR(CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED);
2262         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
2263             return JDWP_ERROR(METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED);
2264         case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED:
2265             return JDWP_ERROR(CLASS_ATTRIBUTE_CHANGE_NOT_IMPLEMENTED);
2266         case JVMTI_ERROR_UNSUPPORTED_OPERATION:
2267             return JDWP_ERROR(NOT_IMPLEMENTED);
2268         case AGENT_ERROR_NOT_CURRENT_FRAME:
2269             return JDWP_ERROR(NOT_CURRENT_FRAME);
2270         case AGENT_ERROR_INVALID_TAG:
2271             return JDWP_ERROR(INVALID_TAG);
2272         case AGENT_ERROR_ALREADY_INVOKING:
2273             return JDWP_ERROR(ALREADY_INVOKING);
2274         case AGENT_ERROR_INVALID_INDEX:
2275             return JDWP_ERROR(INVALID_INDEX);
2276         case AGENT_ERROR_INVALID_LENGTH:
2277             return JDWP_ERROR(INVALID_LENGTH);
2278         case AGENT_ERROR_INVALID_STRING:
2279             return JDWP_ERROR(INVALID_STRING);
2280         case AGENT_ERROR_INVALID_CLASS_LOADER:
2281             return JDWP_ERROR(INVALID_CLASS_LOADER);
2282         case AGENT_ERROR_INVALID_ARRAY:
2283             return JDWP_ERROR(INVALID_ARRAY);
2284         case AGENT_ERROR_TRANSPORT_LOAD:
2285             return JDWP_ERROR(TRANSPORT_LOAD);
2286         case AGENT_ERROR_TRANSPORT_INIT:
2287             return JDWP_ERROR(TRANSPORT_INIT);
2288         case AGENT_ERROR_NATIVE_METHOD:
2289             return JDWP_ERROR(NATIVE_METHOD);
2290         case AGENT_ERROR_INVALID_COUNT:
2291             return JDWP_ERROR(INVALID_COUNT);
2292         case AGENT_ERROR_INVALID_FRAMEID:
2293             return JDWP_ERROR(INVALID_FRAMEID);
2294         case JVMTI_ERROR_INTERNAL:
2295         case JVMTI_ERROR_INVALID_ENVIRONMENT:
2296         case AGENT_ERROR_INTERNAL:
2297         case AGENT_ERROR_JVMTI_INTERNAL:
2298         case AGENT_ERROR_JDWP_INTERNAL:
2299             return JDWP_ERROR(INTERNAL);
2300         default:
2301             break;
2302     }
2303     return JDWP_ERROR(INTERNAL);
2304 }
2305 
2306 jint
2307 map2jdwpSuspendStatus(jint state)
2308 {
2309     jint status = 0;
2310     if ( ( state & JVMTI_THREAD_STATE_SUSPENDED ) != 0 )  {
2311         status = JDWP_SUSPEND_STATUS(SUSPENDED);
2312     }
2313     return status;
2314 }
2315 
2316 jdwpThreadStatus
2317 map2jdwpThreadStatus(jint state)
2318 {
2319     jdwpThreadStatus status;
2320 
2321     status = (jdwpThreadStatus)(-1);
2322 
2323     if ( ! ( state & JVMTI_THREAD_STATE_ALIVE ) ) {
2324         if ( state & JVMTI_THREAD_STATE_TERMINATED ) {
2325             status = JDWP_THREAD_STATUS(ZOMBIE);
2326         } else {
2327             /* FIXUP? New JDWP #define for not started? */
2328             status = (jdwpThreadStatus)(-1);
2329         }
2330     } else {
2331         if ( state & JVMTI_THREAD_STATE_SLEEPING ) {
2332             status = JDWP_THREAD_STATUS(SLEEPING);
2333         } else if ( state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER ) {
2334             status = JDWP_THREAD_STATUS(MONITOR);
2335         } else if ( state & JVMTI_THREAD_STATE_WAITING ) {
2336             status = JDWP_THREAD_STATUS(WAIT);
2337         } else if ( state & JVMTI_THREAD_STATE_RUNNABLE ) {
2338             status = JDWP_THREAD_STATUS(RUNNING);
2339         }
2340     }
2341     return status;
2342 }
2343 
2344 jint
2345 map2jdwpClassStatus(jint classStatus)
2346 {
2347     jint status = 0;
2348     if ( ( classStatus & JVMTI_CLASS_STATUS_VERIFIED ) != 0 ) {
2349         status |= JDWP_CLASS_STATUS(VERIFIED);
2350     }
2351     if ( ( classStatus & JVMTI_CLASS_STATUS_PREPARED ) != 0 ) {
2352         status |= JDWP_CLASS_STATUS(PREPARED);
2353     }
2354     if ( ( classStatus & JVMTI_CLASS_STATUS_INITIALIZED ) != 0 ) {
2355         status |= JDWP_CLASS_STATUS(INITIALIZED);
2356     }
2357     if ( ( classStatus & JVMTI_CLASS_STATUS_ERROR ) != 0 ) {
2358         status |= JDWP_CLASS_STATUS(ERROR);
2359     }
2360     return status;
2361 }
2362 
2363 void
2364 log_debugee_location(const char *func,
2365         jthread thread, jmethodID method, jlocation location)
2366 {
2367     int logging_locations = LOG_TEST(JDWP_LOG_LOC);
2368 
2369     if ( logging_locations ) {
2370         char *method_name;
2371         char *class_sig;
2372         jvmtiError error;
2373         jvmtiThreadInfo info;
2374         jint state;
2375 
2376         /* Get thread information */
2377         info.name = NULL;
2378         error = FUNC_PTR(gdata->jvmti,GetThreadInfo)
2379                                 (gdata->jvmti, thread, &info);
2380         if ( error != JVMTI_ERROR_NONE) {
2381             info.name = NULL;
2382         }
2383         error = FUNC_PTR(gdata->jvmti,GetThreadState)
2384                                 (gdata->jvmti, thread, &state);
2385         if ( error != JVMTI_ERROR_NONE) {
2386             state = 0;
2387         }
2388 
2389         /* Get method if necessary */
2390         if ( method==NULL ) {
2391             error = FUNC_PTR(gdata->jvmti,GetFrameLocation)
2392                         (gdata->jvmti, thread, 0, &method, &location);
2393             if ( error != JVMTI_ERROR_NONE ) {
2394                 method = NULL;
2395                 location = 0;
2396             }
2397         }
2398 
2399         /* Get method name */
2400         method_name = NULL;
2401         if ( method != NULL ) {
2402             error = methodSignature(method, &method_name, NULL, NULL);
2403             if ( error != JVMTI_ERROR_NONE ) {
2404                 method_name = NULL;
2405             }
2406         }
2407 
2408         /* Get class signature */
2409         class_sig = NULL;
2410         if ( method != NULL ) {
2411             jclass clazz;
2412 
2413             error = methodClass(method, &clazz);
2414             if ( error == JVMTI_ERROR_NONE ) {
2415                 error = classSignature(clazz, &class_sig, NULL);
2416                 if ( error != JVMTI_ERROR_NONE ) {
2417                     class_sig = NULL;
2418                 }
2419             }
2420         }
2421 
2422         /* Issue log message */
2423         LOG_LOC(("%s: debuggee: thread=%p(%s:0x%x),method=%p(%s@%d;%s)",
2424                 func,
2425                 thread, info.name==NULL ? "?" : info.name, state,
2426                 method, method_name==NULL ? "?" : method_name,
2427                 (int)location, class_sig==NULL ? "?" : class_sig));
2428 
2429         /* Free memory */
2430         if ( class_sig != NULL ) {
2431             jvmtiDeallocate(class_sig);
2432         }
2433         if ( method_name != NULL ) {
2434             jvmtiDeallocate(method_name);
2435         }
2436         if ( info.name != NULL ) {
2437             jvmtiDeallocate(info.name);
2438         }
2439     }
2440 }
2441 
2442 /* ********************************************************************* */
2443 /* JDK 6.0: Use of new Heap Iteration functions */
2444 /* ********************************************************************* */
2445 
2446 /* ********************************************************************* */
2447 /* Instances */
2448 
2449 /* Structure to hold class instances heap iteration data (arg user_data) */
2450 typedef struct ClassInstancesData {
2451     jint         instCount;
2452     jint         maxInstances;
2453     jlong        objTag;
2454     jvmtiError   error;
2455 } ClassInstancesData;
2456 
2457 /* Callback for instance object tagging (heap_reference_callback). */
2458 static jint JNICALL
2459 cbObjectTagInstance(jvmtiHeapReferenceKind reference_kind,
2460      const jvmtiHeapReferenceInfo* reference_info, jlong class_tag,
2461      jlong referrer_class_tag, jlong size,
2462      jlong* tag_ptr, jlong* referrer_tag_ptr, jint length, void* user_data)
2463 {
2464     ClassInstancesData  *data;
2465 
2466     /* Check data structure */
2467     data = (ClassInstancesData*)user_data;
2468     if (data == NULL) {
2469         return JVMTI_VISIT_ABORT;
2470     }
2471 
2472     /* If we have tagged enough objects, just abort */
2473     if ( data->maxInstances != 0 && data->instCount >= data->maxInstances ) {
2474         return JVMTI_VISIT_ABORT;
2475     }
2476 
2477     /* If tagged already, just continue */
2478     if ( (*tag_ptr) != (jlong)0 ) {
2479         return JVMTI_VISIT_OBJECTS;
2480     }
2481 
2482     /* Tag the object so we don't count it again, and so we can retrieve it */
2483     (*tag_ptr) = data->objTag;
2484     data->instCount++;
2485     return JVMTI_VISIT_OBJECTS;
2486 }
2487 
2488 /* Get instances for one class */
2489 jvmtiError
2490 classInstances(jclass klass, ObjectBatch *instances, int maxInstances)
2491 {
2492     ClassInstancesData data;
2493     jvmtiHeapCallbacks heap_callbacks;
2494     jvmtiError         error;
2495     jvmtiEnv          *jvmti;
2496 
2497     /* Check interface assumptions */
2498 
2499     if (klass == NULL) {
2500         return AGENT_ERROR_INVALID_OBJECT;
2501     }
2502 
2503     if ( maxInstances < 0 || instances == NULL) {
2504         return AGENT_ERROR_ILLEGAL_ARGUMENT;
2505     }
2506 
2507     /* Initialize return information */
2508     instances->count   = 0;
2509     instances->objects = NULL;
2510 
2511     /* Get jvmti environment to use */
2512     jvmti = getSpecialJvmti();
2513     if ( jvmti == NULL ) {
2514         return AGENT_ERROR_INTERNAL;
2515     }
2516 
2517     /* Setup data to passed around the callbacks */
2518     data.instCount    = 0;
2519     data.maxInstances = maxInstances;
2520     data.objTag       = (jlong)1;
2521     data.error        = JVMTI_ERROR_NONE;
2522 
2523     /* Clear out callbacks structure */
2524     (void)memset(&heap_callbacks,0,sizeof(heap_callbacks));
2525 
2526     /* Set the callbacks we want */
2527     heap_callbacks.heap_reference_callback = &cbObjectTagInstance;
2528 
2529     /* Follow references, no initiating object, just this class, all objects */
2530     error = JVMTI_FUNC_PTR(jvmti,FollowReferences)
2531                  (jvmti, 0, klass, NULL, &heap_callbacks, &data);
2532     if ( error == JVMTI_ERROR_NONE ) {
2533         error = data.error;
2534     }
2535 
2536     /* Get all the instances now that they are tagged */
2537     if ( error == JVMTI_ERROR_NONE ) {
2538         error = JVMTI_FUNC_PTR(jvmti,GetObjectsWithTags)
2539                       (jvmti, 1, &(data.objTag), &(instances->count),
2540                        &(instances->objects), NULL);
2541         /* Verify we got the count we expected */
2542         if ( data.instCount != instances->count ) {
2543             error = AGENT_ERROR_INTERNAL;
2544         }
2545     }
2546 
2547     /* Dispose of any special jvmti environment */
2548     (void)JVMTI_FUNC_PTR(jvmti,DisposeEnvironment)(jvmti);
2549     return error;
2550 }
2551 
2552 /* ********************************************************************* */
2553 /* Instance counts. */
2554 
2555 /* Macros to convert a class or instance tag to an index and back again */
2556 #define INDEX2CLASSTAG(i)      ((jlong)((i)+1))
2557 #define CLASSTAG2INDEX(t)      (((int)(t))-1)
2558 #define JLONG_ABS(x)           (((x)<(jlong)0)?-(x):(x))
2559 
2560 /* Structure to hold class count heap traversal data (arg user_data) */
2561 typedef struct ClassCountData {
2562     int          classCount;
2563     jlong       *counts;
2564     jlong        negObjTag;
2565     jvmtiError   error;
2566 } ClassCountData;
2567 
2568 /* Two different cbObjectCounter's, one for FollowReferences, one for
2569  *    IterateThroughHeap. Pick a card, any card.
2570  */
2571 
2572 /* Callback for object count heap traversal (heap_reference_callback) */
2573 static jint JNICALL
2574 cbObjectCounterFromRef(jvmtiHeapReferenceKind reference_kind,
2575      const jvmtiHeapReferenceInfo* reference_info, jlong class_tag,
2576      jlong referrer_class_tag, jlong size,
2577      jlong* tag_ptr, jlong* referrer_tag_ptr, jint length, void* user_data)
2578 {
2579     ClassCountData  *data;
2580     int              index;
2581     jlong            jindex;
2582     jlong            tag;
2583 
2584     /* Check data structure */
2585     data = (ClassCountData*)user_data;
2586     if (data == NULL) {
2587         return JVMTI_VISIT_ABORT;
2588     }
2589 
2590     /* Classes with no class_tag should have been filtered out. */
2591     if ( class_tag == (jlong)0 ) {
2592         data->error = AGENT_ERROR_INTERNAL;
2593         return JVMTI_VISIT_ABORT;
2594     }
2595 
2596     /* Class tag not one we really want (jclass not in supplied list) */
2597     if ( class_tag == data->negObjTag ) {
2598         return JVMTI_VISIT_OBJECTS;
2599     }
2600 
2601     /* If object tag is negative, just continue, we counted it */
2602     tag = (*tag_ptr);
2603     if ( tag < (jlong)0 ) {
2604         return JVMTI_VISIT_OBJECTS;
2605     }
2606 
2607     /* Tag the object with a negative value just so we don't count it again */
2608     if ( tag == (jlong)0 ) {
2609         /* This object had no tag value, so we give it the negObjTag value */
2610         (*tag_ptr) = data->negObjTag;
2611     } else {
2612         /* If this object had a positive tag value, it must be one of the
2613          *    jclass objects we tagged. We need to preserve the value of
2614          *    this tag for later objects that might have this as a class
2615          *    tag, so we just make the existing tag value negative.
2616          */
2617         (*tag_ptr) = -tag;
2618     }
2619 
2620     /* Absolute value of class tag is an index into the counts[] array */
2621     jindex = JLONG_ABS(class_tag);
2622     index = CLASSTAG2INDEX(jindex);
2623     if (index < 0 || index >= data->classCount) {
2624         data->error = AGENT_ERROR_ILLEGAL_ARGUMENT;
2625         return JVMTI_VISIT_ABORT;
2626     }
2627 
2628     /* Bump instance count on this class */
2629     data->counts[index]++;
2630     return JVMTI_VISIT_OBJECTS;
2631 }
2632 
2633 /* Callback for instance count heap traversal (heap_iteration_callback) */
2634 static jint JNICALL
2635 cbObjectCounter(jlong class_tag, jlong size, jlong* tag_ptr, jint length,
2636                         void* user_data)
2637 {
2638     ClassCountData  *data;
2639     int              index;
2640 
2641     /* Check data structure */
2642     data = (ClassCountData*)user_data;
2643     if (data == NULL) {
2644         return JVMTI_VISIT_ABORT;
2645     }
2646 
2647     /* Classes with no tag should be filtered out. */
2648     if ( class_tag == (jlong)0 ) {
2649         data->error = AGENT_ERROR_INTERNAL;
2650         return JVMTI_VISIT_ABORT;
2651     }
2652 
2653     /* Class tag is actually an index into data arrays */
2654     index = CLASSTAG2INDEX(class_tag);
2655     if (index < 0 || index >= data->classCount) {
2656         data->error = AGENT_ERROR_ILLEGAL_ARGUMENT;
2657         return JVMTI_VISIT_ABORT;
2658     }
2659 
2660     /* Bump instance count on this class */
2661     data->counts[index]++;
2662     return JVMTI_VISIT_OBJECTS;
2663 }
2664 
2665 /* Get instance counts for a set of classes */
2666 jvmtiError
2667 classInstanceCounts(jint classCount, jclass *classes, jlong *counts)
2668 {
2669     jvmtiHeapCallbacks heap_callbacks;
2670     ClassCountData     data;
2671     jvmtiError         error;
2672     jvmtiEnv          *jvmti;
2673     int                i;
2674 
2675     /* Check interface assumptions */
2676     if ( classes == NULL || classCount <= 0 || counts == NULL ) {
2677         return AGENT_ERROR_ILLEGAL_ARGUMENT;
2678     }
2679 
2680     /* Initialize return information */
2681     for ( i = 0 ; i < classCount ; i++ ) {
2682         counts[i] = (jlong)0;
2683     }
2684 
2685     /* Get jvmti environment to use */
2686     jvmti = getSpecialJvmti();
2687     if ( jvmti == NULL ) {
2688         return AGENT_ERROR_INTERNAL;
2689     }
2690 
2691     /* Setup class data structure */
2692     data.error        = JVMTI_ERROR_NONE;
2693     data.classCount   = classCount;
2694     data.counts       = counts;
2695 
2696     error = JVMTI_ERROR_NONE;
2697     /* Set tags on classes, use index in classes[] as the tag value. */
2698     error             = JVMTI_ERROR_NONE;
2699     for ( i = 0 ; i < classCount ; i++ ) {
2700         if (classes[i] != NULL) {
2701             jlong tag;
2702 
2703             tag = INDEX2CLASSTAG(i);
2704             error = JVMTI_FUNC_PTR(jvmti,SetTag) (jvmti, classes[i], tag);
2705             if ( error != JVMTI_ERROR_NONE ) {
2706                 break;
2707             }
2708         }
2709     }
2710 
2711     /* Traverse heap, two ways to do this for instance counts. */
2712     if ( error == JVMTI_ERROR_NONE ) {
2713 
2714         /* Clear out callbacks structure */
2715         (void)memset(&heap_callbacks,0,sizeof(heap_callbacks));
2716 
2717         /* Check debug flags to see how to do this. */
2718         if ( (gdata->debugflags & USE_ITERATE_THROUGH_HEAP) == 0 ) {
2719 
2720             /* Using FollowReferences only gives us live objects, but we
2721              *   need to tag the objects to avoid counting them twice since
2722              *   the callback is per reference.
2723              *   The jclass objects have been tagged with their index in the
2724              *   supplied list, and that tag may flip to negative if it
2725              *   is also an object of interest.
2726              *   All other objects being counted that weren't in the
2727              *   supplied classes list will have a negative classCount
2728              *   tag value. So all objects counted will have negative tags.
2729              *   If the absolute tag value is an index in the supplied
2730              *   list, then it's one of the supplied classes.
2731              */
2732             data.negObjTag = -INDEX2CLASSTAG(classCount);
2733 
2734             /* Setup callbacks, only using object reference callback */
2735             heap_callbacks.heap_reference_callback = &cbObjectCounterFromRef;
2736 
2737             /* Follow references, no initiating object, tagged classes only */
2738             error = JVMTI_FUNC_PTR(jvmti,FollowReferences)
2739                           (jvmti, JVMTI_HEAP_FILTER_CLASS_UNTAGGED,
2740                            NULL, NULL, &heap_callbacks, &data);
2741 
2742         } else {
2743 
2744             /* Using IterateThroughHeap means that we will visit each object
2745              *   once, so no special tag tricks here. Just simple counting.
2746              *   However in this case the object might not be live, so we do
2747              *   a GC beforehand to make sure we minimize this.
2748              */
2749 
2750             /* FIXUP: Need some kind of trigger here to avoid excessive GC's? */
2751             error = JVMTI_FUNC_PTR(jvmti,ForceGarbageCollection)(jvmti);
2752             if ( error != JVMTI_ERROR_NONE ) {
2753 
2754                 /* Setup callbacks, just need object callback */
2755                 heap_callbacks.heap_iteration_callback = &cbObjectCounter;
2756 
2757                 /* Iterate through entire heap, tagged classes only */
2758                 error = JVMTI_FUNC_PTR(jvmti,IterateThroughHeap)
2759                               (jvmti, JVMTI_HEAP_FILTER_CLASS_UNTAGGED,
2760                                NULL, &heap_callbacks, &data);
2761 
2762             }
2763         }
2764 
2765         /* Use data error if needed */
2766         if ( error == JVMTI_ERROR_NONE ) {
2767             error = data.error;
2768         }
2769 
2770     }
2771 
2772     /* Dispose of any special jvmti environment */
2773     (void)JVMTI_FUNC_PTR(jvmti,DisposeEnvironment)(jvmti);
2774     return error;
2775 }
2776 
2777 /* ********************************************************************* */
2778 /* Referrers */
2779 
2780 /* Structure to hold object referrer heap traversal data (arg user_data) */
2781 typedef struct ReferrerData {
2782   int        refCount;
2783   int        maxObjects;
2784   jlong      refTag;
2785   jlong      objTag;
2786   jboolean   selfRef;
2787   jvmtiError error;
2788 } ReferrerData;
2789 
2790 /* Callback for referrers object tagging (heap_reference_callback). */
2791 static jint JNICALL
2792 cbObjectTagReferrer(jvmtiHeapReferenceKind reference_kind,
2793      const jvmtiHeapReferenceInfo* reference_info, jlong class_tag,
2794      jlong referrer_class_tag, jlong size,
2795      jlong* tag_ptr, jlong* referrer_tag_ptr, jint length, void* user_data)
2796 {
2797     ReferrerData  *data;
2798 
2799     /* Check data structure */
2800     data = (ReferrerData*)user_data;
2801     if (data == NULL) {
2802         return JVMTI_VISIT_ABORT;
2803     }
2804 
2805     /* If we have tagged enough objects, just abort */
2806     if ( data->maxObjects != 0 && data->refCount >= data->maxObjects ) {
2807         return JVMTI_VISIT_ABORT;
2808     }
2809 
2810     /* If not of interest, just continue */
2811     if ( (*tag_ptr) != data->objTag ) {
2812         return JVMTI_VISIT_OBJECTS;
2813     }
2814 
2815     /* Self reference that we haven't counted? */
2816     if ( tag_ptr == referrer_tag_ptr ) {
2817         if ( data->selfRef == JNI_FALSE ) {
2818             data->selfRef = JNI_TRUE;
2819             data->refCount++;
2820         }
2821         return JVMTI_VISIT_OBJECTS;
2822     }
2823 
2824     /* If the referrer can be tagged, and hasn't been tagged, tag it */
2825     if ( referrer_tag_ptr != NULL ) {
2826         if ( (*referrer_tag_ptr) == (jlong)0 ) {
2827             *referrer_tag_ptr = data->refTag;
2828             data->refCount++;
2829         }
2830     }
2831     return JVMTI_VISIT_OBJECTS;
2832 }
2833 
2834 /* Heap traversal to find referrers of an object */
2835 jvmtiError
2836 objectReferrers(jobject obj, ObjectBatch *referrers, int maxObjects)
2837 {
2838     jvmtiHeapCallbacks heap_callbacks;
2839     ReferrerData       data;
2840     jvmtiError         error;
2841     jvmtiEnv          *jvmti;
2842 
2843     /* Check interface assumptions */
2844     if (obj == NULL) {
2845         return AGENT_ERROR_INVALID_OBJECT;
2846     }
2847     if (referrers == NULL || maxObjects < 0 ) {
2848         return AGENT_ERROR_ILLEGAL_ARGUMENT;
2849     }
2850 
2851     /* Initialize return information */
2852     referrers->count = 0;
2853     referrers->objects = NULL;
2854 
2855     /* Get jvmti environment to use */
2856     jvmti = getSpecialJvmti();
2857     if ( jvmti == NULL ) {
2858         return AGENT_ERROR_INTERNAL;
2859     }
2860 
2861     /* Fill in the data structure passed around the callbacks */
2862     data.refCount   = 0;
2863     data.maxObjects = maxObjects;
2864     data.objTag     = (jlong)1;
2865     data.refTag     = (jlong)2;
2866     data.selfRef    = JNI_FALSE;
2867     data.error      = JVMTI_ERROR_NONE;
2868 
2869     /* Tag the object of interest */
2870     error = JVMTI_FUNC_PTR(jvmti,SetTag) (jvmti, obj, data.objTag);
2871 
2872     /* No need to go any further if we can't tag the object */
2873     if ( error == JVMTI_ERROR_NONE ) {
2874 
2875         /* Clear out callbacks structure */
2876         (void)memset(&heap_callbacks,0,sizeof(heap_callbacks));
2877 
2878         /* Setup callbacks we want */
2879         heap_callbacks.heap_reference_callback = &cbObjectTagReferrer;
2880 
2881         /* Follow references, no initiating object, all classes, 1 tagged objs */
2882         error = JVMTI_FUNC_PTR(jvmti,FollowReferences)
2883                       (jvmti, JVMTI_HEAP_FILTER_UNTAGGED,
2884                        NULL, NULL, &heap_callbacks, &data);
2885 
2886         /* Use data error if needed */
2887         if ( error == JVMTI_ERROR_NONE ) {
2888             error = data.error;
2889         }
2890 
2891     }
2892 
2893     /* Watch out for self-reference */
2894     if ( error == JVMTI_ERROR_NONE && data.selfRef == JNI_TRUE ) {
2895         /* Tag itself as a referer */
2896         error = JVMTI_FUNC_PTR(jvmti,SetTag) (jvmti, obj, data.refTag);
2897     }
2898 
2899     /* Get the jobjects for the tagged referrer objects.  */
2900     if ( error == JVMTI_ERROR_NONE ) {
2901         error = JVMTI_FUNC_PTR(jvmti,GetObjectsWithTags)
2902                     (jvmti, 1, &(data.refTag), &(referrers->count),
2903                           &(referrers->objects), NULL);
2904         /* Verify we got the count we expected */
2905         if ( data.refCount != referrers->count ) {
2906             error = AGENT_ERROR_INTERNAL;
2907         }
2908     }
2909 
2910     /* Dispose of any special jvmti environment */
2911     (void)JVMTI_FUNC_PTR(jvmti,DisposeEnvironment)(jvmti);
2912     return error;
2913 }
--- EOF ---