1 /* 2 * Copyright (c) 2004, 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 24 #include <stdlib.h> 25 #include <string.h> 26 #include "jni_tools.h" 27 #include "agent_common.h" 28 #include "jvmti_tools.h" 29 30 #define PASSED 0 31 #define STATUS_FAILED 2 32 #define SAMPLE_TAG ((jlong) 222222) 33 34 extern "C" { 35 36 /* ========================================================================== */ 37 38 /* scaffold objects */ 39 static jlong timeout = 0; 40 41 /* test objects */ 42 static jobject testedObject = NULL; 43 static int ObjectFreeEventsCount = 0; 44 45 /* ========================================================================== */ 46 47 /** callback functions **/ 48 49 void JNICALL ObjectFree(jvmtiEnv *jvmti_env, jlong tag) { 50 char buffer[32], buffer2[32]; 51 52 ObjectFreeEventsCount++; 53 54 NSK_DISPLAY1("ObjectFree event, tag = %s\n", jlong_to_string(tag, buffer)); 55 if (tag != SAMPLE_TAG) { 56 if (tag == 0) { 57 NSK_COMPLAIN0("testedObject not tagged\n"); 58 } else { 59 NSK_COMPLAIN2("testedObject tagged incorrectly, expected=%s, got=%s\n", 60 jlong_to_string(SAMPLE_TAG, buffer), 61 jlong_to_string(tag, buffer2)); 62 } 63 nsk_jvmti_setFailStatus(); 64 } 65 } 66 67 void JNICALL VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 68 NSK_DISPLAY0("VMDeath event\n"); 69 70 if (ObjectFreeEventsCount == 0) { 71 NSK_DISPLAY0("Warning: no object free events\n"); 72 } 73 74 if (nsk_jvmti_isFailStatus()) { 75 exit(NSK_STATUS_BASE + NSK_STATUS_FAILED); 76 } 77 } 78 79 /* ========================================================================== */ 80 81 static int prepare(JNIEnv* jni) { 82 const char* CLASS_NAME = "nsk/jvmti/scenarios/multienv/MA04/ma04t003"; 83 const char* FIELD_NAME = "testedObject2"; 84 const char* FIELD_SIGNATURE = "Ljava/lang/Object;"; 85 jclass cls = NULL; 86 jfieldID fid = NULL; 87 88 NSK_DISPLAY0("Obtain tested object from a static field of debugee class\n"); 89 90 NSK_DISPLAY1("Find class: %s\n", CLASS_NAME); 91 if (!NSK_JNI_VERIFY(jni, (cls = 92 NSK_CPP_STUB2(FindClass, jni, CLASS_NAME)) != NULL)) 93 return NSK_FALSE; 94 95 NSK_DISPLAY2("Find field: %s:%s\n", FIELD_NAME, FIELD_SIGNATURE); 96 if (!NSK_JNI_VERIFY(jni, (fid = 97 NSK_CPP_STUB4(GetStaticFieldID, jni, cls, 98 FIELD_NAME, FIELD_SIGNATURE)) != NULL)) 99 return NSK_FALSE; 100 101 if (!NSK_JNI_VERIFY(jni, (testedObject = 102 NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fid)) != NULL)) 103 return NSK_FALSE; 104 105 if (!NSK_JNI_VERIFY(jni, (testedObject = 106 NSK_CPP_STUB2(NewGlobalRef, jni, testedObject)) != NULL)) 107 return NSK_FALSE; 108 109 return NSK_TRUE; 110 } 111 112 /* ========================================================================== */ 113 114 /** Agent algorithm. */ 115 static void JNICALL 116 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 117 118 if (!nsk_jvmti_waitForSync(timeout)) 119 return; 120 121 if (!prepare(jni)) { 122 nsk_jvmti_setFailStatus(); 123 return; 124 } 125 126 NSK_DISPLAY0("Set tag on testedObject\n"); 127 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject, 128 SAMPLE_TAG))) { 129 nsk_jvmti_setFailStatus(); 130 return; 131 } 132 133 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedObject)); 134 135 if (!nsk_jvmti_resumeSync()) 136 return; 137 } 138 139 /* ========================================================================== */ 140 141 /** Agent library initialization. */ 142 #ifdef STATIC_BUILD 143 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t003a(JavaVM *jvm, char *options, void *reserved) { 144 return Agent_Initialize(jvm, options, reserved); 145 } 146 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t003a(JavaVM *jvm, char *options, void *reserved) { 147 return Agent_Initialize(jvm, options, reserved); 148 } 149 JNIEXPORT jint JNI_OnLoad_ma04t003a(JavaVM *jvm, char *options, void *reserved) { 150 return JNI_VERSION_1_8; 151 } 152 #endif 153 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 154 jvmtiEnv* jvmti = NULL; 155 jvmtiEventCallbacks callbacks; 156 jvmtiCapabilities caps; 157 158 NSK_DISPLAY0("Agent_OnLoad\n"); 159 160 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 161 return JNI_ERR; 162 163 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 164 165 if (!NSK_VERIFY((jvmti = 166 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 167 return JNI_ERR; 168 169 memset(&caps, 0, sizeof(caps)); 170 caps.can_tag_objects = 1; 171 caps.can_generate_object_free_events = 1; 172 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { 173 return JNI_ERR; 174 } 175 176 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 177 return JNI_ERR; 178 179 memset(&callbacks, 0, sizeof(callbacks)); 180 callbacks.ObjectFree = &ObjectFree; 181 callbacks.VMDeath = &VMDeath; 182 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 183 return JNI_ERR; 184 185 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 186 jvmti, JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, NULL))) 187 return JNI_ERR; 188 189 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 190 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 191 return JNI_ERR; 192 193 return JNI_OK; 194 } 195 196 /* ========================================================================== */ 197 198 }