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 <stdlib.h> 24 #include <string.h> 25 #include "jvmti.h" 26 #include "agent_common.h" 27 #include "jni_tools.h" 28 #include "jvmti_tools.h" 29 30 extern "C" { 31 32 /* ============================================================================= */ 33 34 static jlong timeout = 0; 35 36 #define STATUS_FAIL 97 37 38 #define EVENTS_COUNT 2 39 40 static jvmtiEvent events[EVENTS_COUNT] = { 41 JVMTI_EVENT_VM_INIT, 42 JVMTI_EVENT_VM_DEATH 43 }; 44 45 static jvmtiCapabilities initCaps; 46 47 /* ============================================================================= */ 48 49 /** 50 * Get and check current capabilities. 51 * @returns NSK_FALSE if any error occured. 52 */ 53 static int checkCapabilities(jvmtiEnv* jvmti, jvmtiCapabilities* initCaps, const char where[]) { 54 int success = NSK_TRUE; 55 jvmtiCapabilities caps; 56 57 memset(&caps, 0, sizeof(jvmtiCapabilities)); 58 59 NSK_DISPLAY0("GetCapabilities() for current JVMTI env\n"); 60 if (!NSK_JVMTI_VERIFY( 61 NSK_CPP_STUB2(GetCapabilities, jvmti, &caps))) { 62 return NSK_FALSE; 63 } 64 65 return success; 66 } 67 68 /** 69 * Add given capabilities list. 70 * @returns NSK_FALSE if any error occured. 71 */ 72 static int addCapabilities(jvmtiEnv* jvmti, jvmtiCapabilities* caps, const char where[]) { 73 NSK_DISPLAY0("AddCapabilities() for current JVMTI env\n"); 74 if (!NSK_JVMTI_VERIFY( 75 NSK_CPP_STUB2(AddCapabilities, jvmti, caps))) { 76 return NSK_FALSE; 77 } 78 NSK_DISPLAY0(" ... set\n"); 79 80 return NSK_TRUE; 81 } 82 83 /** 84 * Get potential capabilities to the given list. 85 * @returns NSK_FALSE if any error occured. 86 */ 87 static int getPotentialCapabilities(jvmtiEnv* jvmti, jvmtiCapabilities* caps) { 88 NSK_DISPLAY0("GetPotentialCapabilities() for current JVMTI env\n"); 89 if (!NSK_JVMTI_VERIFY( 90 NSK_CPP_STUB2(GetPotentialCapabilities, jvmti, caps))) { 91 return NSK_FALSE; 92 } 93 94 return NSK_TRUE; 95 } 96 97 /* ============================================================================= */ 98 99 /** Agent algorithm. */ 100 static void JNICALL 101 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 102 NSK_DISPLAY0("Wait for debugee to become ready\n"); 103 if (!nsk_jvmti_waitForSync(timeout)) 104 return; 105 106 NSK_DISPLAY0(">>> Testcase #3: Check capabilities in agent thread\n"); 107 if (!checkCapabilities(jvmti, &initCaps, "agent thread")) { 108 nsk_jvmti_setFailStatus(); 109 } 110 111 NSK_DISPLAY0("Let debugee to finish\n"); 112 if (!nsk_jvmti_resumeSync()) 113 return; 114 } 115 116 /* ============================================================================= */ 117 118 /** 119 * Callback for VM_INIT event. 120 */ 121 JNIEXPORT void JNICALL 122 callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) { 123 124 NSK_DISPLAY0(">>> Testcase #2: Check capabilities in VM_INIT callback\n"); 125 if (!checkCapabilities(jvmti, &initCaps, "VM_INIT callback")) { 126 nsk_jvmti_setFailStatus(); 127 } 128 129 } 130 131 /** 132 * Callback for VM_DEATH event. 133 */ 134 JNIEXPORT void JNICALL 135 callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) { 136 int success = NSK_TRUE; 137 138 NSK_DISPLAY0(">>> Testcase #4: Check capabilities in VM_DEATH callback\n"); 139 success = checkCapabilities(jvmti, &initCaps, "VM_DEATH callback"); 140 141 NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT); 142 if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) { 143 success = NSK_FALSE; 144 } else { 145 NSK_DISPLAY0(" ... disabled\n"); 146 } 147 148 if (!success) { 149 NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL); 150 NSK_BEFORE_TRACE(exit(STATUS_FAIL)); 151 } 152 } 153 154 /* ============================================================================= */ 155 156 /** Agent library initialization. */ 157 #ifdef STATIC_BUILD 158 JNIEXPORT jint JNICALL Agent_OnLoad_addcaps003(JavaVM *jvm, char *options, void *reserved) { 159 return Agent_Initialize(jvm, options, reserved); 160 } 161 JNIEXPORT jint JNICALL Agent_OnAttach_addcaps003(JavaVM *jvm, char *options, void *reserved) { 162 return Agent_Initialize(jvm, options, reserved); 163 } 164 JNIEXPORT jint JNI_OnLoad_addcaps003(JavaVM *jvm, char *options, void *reserved) { 165 return JNI_VERSION_1_8; 166 } 167 #endif 168 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 169 jvmtiEnv* jvmti = NULL; 170 171 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 172 return JNI_ERR; 173 174 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 175 176 if (!NSK_VERIFY((jvmti = 177 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 178 return JNI_ERR; 179 180 { 181 jvmtiEventCallbacks eventCallbacks; 182 183 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 184 eventCallbacks.VMInit = callbackVMInit; 185 eventCallbacks.VMDeath = callbackVMDeath; 186 if (!NSK_JVMTI_VERIFY( 187 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 188 &eventCallbacks, sizeof(eventCallbacks)))) { 189 return JNI_ERR; 190 } 191 192 } 193 194 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 195 return JNI_ERR; 196 197 memset(&initCaps, 0, sizeof(jvmtiCapabilities)); 198 if (!getPotentialCapabilities(jvmti, &initCaps)) { 199 return JNI_ERR; 200 } 201 202 NSK_DISPLAY0(">>> Testcase #0: Add all potential capabilities in Agent_OnLoad()\n"); 203 if (!addCapabilities(jvmti, &initCaps, "Agent_OnLoad()")) { 204 nsk_jvmti_setFailStatus(); 205 } 206 207 NSK_DISPLAY0(">>> Testcase #1: Check capabilities in Agent_OnLoad()\n"); 208 if (!checkCapabilities(jvmti, &initCaps, "Agent_OnLoad()")) { 209 nsk_jvmti_setFailStatus(); 210 } 211 212 NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT); 213 if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) { 214 NSK_DISPLAY0(" ... enabled\n"); 215 } 216 217 return JNI_OK; 218 } 219 220 /* ============================================================================= */ 221 222 }