< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk004/classfloadhk004.cpp

Print this page
rev 51942 : [mq]: refactor


  44 
  45 static jint newClassSize = 0;
  46 static unsigned char* newClassBytes = NULL;
  47 
  48 static volatile int eventsCount = 0;
  49 
  50 /* ============================================================================= */
  51 
  52 /** Get classfile bytecode from a static field of given class. */
  53 static int getBytecode(jvmtiEnv* jvmti, JNIEnv* jni, jclass cls,
  54                                     const char fieldName[], const char fieldSig[],
  55                                     jint* size, unsigned char* *bytes) {
  56 
  57     jfieldID fieldID = NULL;
  58     jbyteArray array = NULL;
  59     jbyte* elements;
  60     int i;
  61 
  62     NSK_DISPLAY1("Find static field: %s\n", fieldName);
  63     if (!NSK_JNI_VERIFY(jni, (fieldID =
  64             NSK_CPP_STUB4(GetStaticFieldID, jni, cls, fieldName, fieldSig)) != NULL)) {
  65         nsk_jvmti_setFailStatus();
  66         return NSK_FALSE;
  67     }
  68     NSK_DISPLAY1("  ... got fieldID: 0x%p\n", (void*)fieldID);
  69 
  70     NSK_DISPLAY1("Get classfile bytes array from static field: %s\n", fieldName);
  71     if (!NSK_JNI_VERIFY(jni, (array = (jbyteArray)
  72             NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fieldID)) != NULL)) {
  73         nsk_jvmti_setFailStatus();
  74         return NSK_FALSE;
  75     }
  76     NSK_DISPLAY1("  ... got array object: 0x%p\n", (void*)array);
  77 
  78     if (!NSK_JNI_VERIFY(jni, (*size =
  79             NSK_CPP_STUB2(GetArrayLength, jni, array)) > 0)) {
  80         nsk_jvmti_setFailStatus();
  81         return NSK_FALSE;
  82     }
  83     NSK_DISPLAY1("  ... got array size: %d bytes\n", (int)*size);
  84 
  85     {
  86         jboolean isCopy;
  87         if (!NSK_JNI_VERIFY(jni, (elements =
  88                 NSK_CPP_STUB3(GetByteArrayElements, jni, array,
  89                                                             &isCopy)) != NULL)) {
  90             nsk_jvmti_setFailStatus();
  91         return NSK_FALSE;
  92         }
  93     }
  94     NSK_DISPLAY1("  ... got elements list: 0x%p\n", (void*)elements);
  95 
  96     if (!NSK_JVMTI_VERIFY(
  97             NSK_CPP_STUB3(Allocate, jvmti, *size, bytes))) {
  98         nsk_jvmti_setFailStatus();
  99         return NSK_FALSE;
 100     }
 101     NSK_DISPLAY1("  ... created bytes array: 0x%p\n", (void*)*bytes);
 102 
 103     for (i = 0; i < *size; i++) {
 104         (*bytes)[i] = (unsigned char)elements[i];
 105     }
 106     NSK_DISPLAY1("  ... copied bytecode: %d bytes\n", (int)*size);
 107 
 108     NSK_DISPLAY1("Release elements list: 0x%p\n", (void*)elements);
 109     NSK_TRACE(NSK_CPP_STUB4(ReleaseByteArrayElements, jni, array, elements, JNI_ABORT));
 110     NSK_DISPLAY0("  ... released\n");
 111 
 112     return NSK_TRUE;
 113 }
 114 
 115 /* ============================================================================= */
 116 
 117 /** Agent algorithm. */
 118 static void JNICALL
 119 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 120     NSK_DISPLAY0("Wait for debuggee to become ready\n");
 121     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 122         return;
 123 
 124     /* perform testing */
 125     {
 126         NSK_DISPLAY0(">>> Obtain classloader and instrumented bytecode of tested class\n");
 127         {
 128             jclass debugeeClass = NULL;
 129 
 130             NSK_DISPLAY1("Find debugee class: %s\n", DEBUGEE_CLASS_NAME);
 131             if (!NSK_JNI_VERIFY(jni, (debugeeClass =
 132                     NSK_CPP_STUB2(FindClass, jni, DEBUGEE_CLASS_NAME)) != NULL)) {
 133                 nsk_jvmti_setFailStatus();
 134                 return;
 135             }
 136             NSK_DISPLAY1("  ... found class: 0x%p\n", (void*)debugeeClass);
 137 
 138             if (!NSK_VERIFY(getBytecode(jvmti, jni, debugeeClass,
 139                                         NEW_BYTECODE_FIELD_NAME,
 140                                         BYTECODE_FIELD_SIG,
 141                                         &newClassSize, &newClassBytes)))
 142                 return;
 143         }
 144 
 145         NSK_DISPLAY0(">>> Testcase #1: Load tested class and replace bytecode in CLASS_FILE_LOAD_HOOK event\n");
 146         {
 147             jvmtiEvent event = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
 148 
 149             NSK_DISPLAY1("Enable event: %s\n", "CLASS_FILE_LOAD_HOOK");
 150             if (!NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_ENABLE, 1, &event, NULL)))
 151                 return;
 152             NSK_DISPLAY0("  ... event enabled\n");


 254 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 255     jvmtiEnv* jvmti = NULL;
 256 
 257     /* init framework and parse options */
 258     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 259         return JNI_ERR;
 260 
 261     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 262 
 263     /* create JVMTI environment */
 264     if (!NSK_VERIFY((jvmti =
 265             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 266         return JNI_ERR;
 267 
 268     NSK_DISPLAY1("Add required capability: %s\n", "can_generate_eraly_class_hook_events");
 269     {
 270         jvmtiCapabilities caps;
 271 
 272         memset(&caps, 0, sizeof(caps));
 273         caps.can_generate_all_class_hook_events = 1;
 274         if (!NSK_JVMTI_VERIFY(
 275                 NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 276             return JNI_ERR;
 277         }
 278     }
 279     NSK_DISPLAY0("  ... added\n");
 280 
 281     NSK_DISPLAY1("Set callback for event: %s\n", "CLASS_FILE_LOAD_HOOK");
 282     {
 283         jvmtiEventCallbacks callbacks;
 284         jint size = (jint)sizeof(callbacks);
 285 
 286         memset(&callbacks, 0, sizeof(callbacks));
 287         callbacks.ClassFileLoadHook = callbackClassFileLoadHook;
 288         if (!NSK_JVMTI_VERIFY(
 289                 NSK_CPP_STUB3(SetEventCallbacks, jvmti, &callbacks, size))) {
 290             return JNI_ERR;
 291         }
 292     }
 293     NSK_DISPLAY0("  ... set\n");
 294 
 295     /* register agent proc and arg */
 296     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 297         return JNI_ERR;
 298 
 299     return JNI_OK;
 300 }
 301 
 302 /* ============================================================================= */
 303 
 304 }


  44 
  45 static jint newClassSize = 0;
  46 static unsigned char* newClassBytes = NULL;
  47 
  48 static volatile int eventsCount = 0;
  49 
  50 /* ============================================================================= */
  51 
  52 /** Get classfile bytecode from a static field of given class. */
  53 static int getBytecode(jvmtiEnv* jvmti, JNIEnv* jni, jclass cls,
  54                                     const char fieldName[], const char fieldSig[],
  55                                     jint* size, unsigned char* *bytes) {
  56 
  57     jfieldID fieldID = NULL;
  58     jbyteArray array = NULL;
  59     jbyte* elements;
  60     int i;
  61 
  62     NSK_DISPLAY1("Find static field: %s\n", fieldName);
  63     if (!NSK_JNI_VERIFY(jni, (fieldID =
  64             jni->GetStaticFieldID(cls, fieldName, fieldSig)) != NULL)) {
  65         nsk_jvmti_setFailStatus();
  66         return NSK_FALSE;
  67     }
  68     NSK_DISPLAY1("  ... got fieldID: 0x%p\n", (void*)fieldID);
  69 
  70     NSK_DISPLAY1("Get classfile bytes array from static field: %s\n", fieldName);
  71     if (!NSK_JNI_VERIFY(jni, (array = (jbyteArray)
  72             jni->GetStaticObjectField(cls, fieldID)) != NULL)) {
  73         nsk_jvmti_setFailStatus();
  74         return NSK_FALSE;
  75     }
  76     NSK_DISPLAY1("  ... got array object: 0x%p\n", (void*)array);
  77 
  78     if (!NSK_JNI_VERIFY(jni, (*size = jni->GetArrayLength(array)) > 0)) {

  79         nsk_jvmti_setFailStatus();
  80         return NSK_FALSE;
  81     }
  82     NSK_DISPLAY1("  ... got array size: %d bytes\n", (int)*size);
  83 
  84     {
  85         jboolean isCopy;
  86         if (!NSK_JNI_VERIFY(jni, (elements = jni->GetByteArrayElements(array, &isCopy)) != NULL)) {


  87             nsk_jvmti_setFailStatus();
  88         return NSK_FALSE;
  89         }
  90     }
  91     NSK_DISPLAY1("  ... got elements list: 0x%p\n", (void*)elements);
  92 
  93     if (!NSK_JVMTI_VERIFY(jvmti->Allocate(*size, bytes))) {

  94         nsk_jvmti_setFailStatus();
  95         return NSK_FALSE;
  96     }
  97     NSK_DISPLAY1("  ... created bytes array: 0x%p\n", (void*)*bytes);
  98 
  99     for (i = 0; i < *size; i++) {
 100         (*bytes)[i] = (unsigned char)elements[i];
 101     }
 102     NSK_DISPLAY1("  ... copied bytecode: %d bytes\n", (int)*size);
 103 
 104     NSK_DISPLAY1("Release elements list: 0x%p\n", (void*)elements);
 105     NSK_TRACE(jni->ReleaseByteArrayElements(array, elements, JNI_ABORT));
 106     NSK_DISPLAY0("  ... released\n");
 107 
 108     return NSK_TRUE;
 109 }
 110 
 111 /* ============================================================================= */
 112 
 113 /** Agent algorithm. */
 114 static void JNICALL
 115 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 116     NSK_DISPLAY0("Wait for debuggee to become ready\n");
 117     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 118         return;
 119 
 120     /* perform testing */
 121     {
 122         NSK_DISPLAY0(">>> Obtain classloader and instrumented bytecode of tested class\n");
 123         {
 124             jclass debugeeClass = NULL;
 125 
 126             NSK_DISPLAY1("Find debugee class: %s\n", DEBUGEE_CLASS_NAME);
 127             if (!NSK_JNI_VERIFY(jni, (debugeeClass =
 128                     jni->FindClass(DEBUGEE_CLASS_NAME)) != NULL)) {
 129                 nsk_jvmti_setFailStatus();
 130                 return;
 131             }
 132             NSK_DISPLAY1("  ... found class: 0x%p\n", (void*)debugeeClass);
 133 
 134             if (!NSK_VERIFY(getBytecode(jvmti, jni, debugeeClass,
 135                                         NEW_BYTECODE_FIELD_NAME,
 136                                         BYTECODE_FIELD_SIG,
 137                                         &newClassSize, &newClassBytes)))
 138                 return;
 139         }
 140 
 141         NSK_DISPLAY0(">>> Testcase #1: Load tested class and replace bytecode in CLASS_FILE_LOAD_HOOK event\n");
 142         {
 143             jvmtiEvent event = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
 144 
 145             NSK_DISPLAY1("Enable event: %s\n", "CLASS_FILE_LOAD_HOOK");
 146             if (!NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_ENABLE, 1, &event, NULL)))
 147                 return;
 148             NSK_DISPLAY0("  ... event enabled\n");


 250 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 251     jvmtiEnv* jvmti = NULL;
 252 
 253     /* init framework and parse options */
 254     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 255         return JNI_ERR;
 256 
 257     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 258 
 259     /* create JVMTI environment */
 260     if (!NSK_VERIFY((jvmti =
 261             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 262         return JNI_ERR;
 263 
 264     NSK_DISPLAY1("Add required capability: %s\n", "can_generate_eraly_class_hook_events");
 265     {
 266         jvmtiCapabilities caps;
 267 
 268         memset(&caps, 0, sizeof(caps));
 269         caps.can_generate_all_class_hook_events = 1;
 270         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {

 271             return JNI_ERR;
 272         }
 273     }
 274     NSK_DISPLAY0("  ... added\n");
 275 
 276     NSK_DISPLAY1("Set callback for event: %s\n", "CLASS_FILE_LOAD_HOOK");
 277     {
 278         jvmtiEventCallbacks callbacks;
 279         jint size = (jint)sizeof(callbacks);
 280 
 281         memset(&callbacks, 0, sizeof(callbacks));
 282         callbacks.ClassFileLoadHook = callbackClassFileLoadHook;
 283         if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, size))) {

 284             return JNI_ERR;
 285         }
 286     }
 287     NSK_DISPLAY0("  ... set\n");
 288 
 289     /* register agent proc and arg */
 290     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 291         return JNI_ERR;
 292 
 293     return JNI_OK;
 294 }
 295 
 296 /* ============================================================================= */
 297 
 298 }
< prev index next >