1 /*
   2  * Copyright (c) 2007, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 
  31 extern "C" {
  32 
  33 /*
  34  * Expected agent work scenario:
  35  *  - during initialization agent enables events VMObjectAlloc
  36  *  - agent receives VMObjectAlloc event for instance of InterruptedException,
  37  *    redefines class InterruptedException and finishes work
  38  */
  39 
  40 #define REDEFINED_CLASS_NAME "Ljava/lang/InterruptedException;"
  41 #define REDEFINED_CLASS_FILE_NAME "java/lang/InterruptedException"
  42 
  43 static Options* options = NULL;
  44 static const char* agentName;
  45 
  46 static jvmtiEvent testEvents[] = {JVMTI_EVENT_VM_OBJECT_ALLOC};
  47 
  48 static const int testEventsNumber = 1;
  49 
  50 void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
  51         JNIEnv * jni,
  52         jthread thread,
  53         jobject object,
  54         jclass object_class,
  55         jlong size) {
  56     char className[MAX_STRING_LENGTH];
  57 
  58     if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
  59         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  60         return;
  61     }
  62 
  63     NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
  64 
  65     if (!strcmp(className, REDEFINED_CLASS_NAME)) {
  66         int success = 1;
  67 
  68         if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, object_class, REDEFINED_CLASS_FILE_NAME))) {
  69             NSK_COMPLAIN1("%s: failed to redefine class\n", agentName);
  70             success = 0;
  71         }
  72 
  73         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
  74     }
  75 }
  76 
  77 #ifdef STATIC_BUILD
  78 JNIEXPORT jint JNI_OnLoad_attach002aAgent00(JavaVM *jvm, char *options, void *reserved) {
  79     return JNI_VERSION_1_8;
  80 }
  81 #endif
  82 
  83 JNIEXPORT jint JNICALL
  84 #ifdef STATIC_BUILD
  85 Agent_OnAttach_attach002aAgent00(JavaVM *vm, char *optionsString, void *reserved)
  86 #else
  87 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  88 #endif
  89 {
  90     jvmtiEventCallbacks eventCallbacks;
  91     jvmtiCapabilities caps;
  92     jvmtiEnv* jvmti = NULL;
  93     JNIEnv* jni = NULL;
  94 
  95     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
  96         return JNI_ERR;
  97 
  98     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  99 
 100     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 101         return NSK_FALSE;
 102 
 103     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 104         return JNI_ERR;
 105 
 106     memset(&caps, 0, sizeof(caps));
 107     caps.can_redefine_classes = 1;
 108     caps.can_generate_vm_object_alloc_events = 1;
 109     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)) ) {
 110         return JNI_ERR;
 111     }
 112 
 113     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 114     eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
 115     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 116         return JNI_ERR;
 117     }
 118 
 119     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 120         return JNI_ERR;
 121     }
 122 
 123     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 124 
 125     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 126         return JNI_ERR;
 127 
 128     return JNI_OK;
 129 }
 130 
 131 }