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 33 extern "C" { 34 35 /* ========================================================================== */ 36 37 /* scaffold objects */ 38 static jlong timeout = 0; 39 40 /* test objects */ 41 static int VMInitEventsCount = 0; 42 static int VMInitEventsExpected = 1; 43 static int VMDeathEventsCount = 0; 44 static int VMDeathEventsExpected = 1; 45 46 /* ========================================================================== */ 47 48 /** callback functions **/ 49 50 void JNICALL VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) { 51 NSK_DISPLAY0("VMInit event\n"); 52 VMInitEventsCount++; 53 } 54 55 void JNICALL 56 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 57 NSK_DISPLAY0("VMDeath event\n"); 58 VMDeathEventsCount++; 59 } 60 61 /* ========================================================================== */ 62 63 /** Agent algorithm. */ 64 static void JNICALL 65 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 66 if (!nsk_jvmti_waitForSync(timeout)) 67 return; 68 69 if (VMInitEventsCount != VMInitEventsExpected) { 70 NSK_COMPLAIN2("Wrong number of VM init events: %d, expected: %d\n", 71 VMInitEventsCount, VMInitEventsExpected); 72 nsk_jvmti_setFailStatus(); 73 } 74 75 if (!nsk_jvmti_resumeSync()) 76 return; 77 } 78 79 /* ========================================================================== */ 80 81 /** Agent library initialization. */ 82 #ifdef STATIC_BUILD 83 JNIEXPORT jint JNICALL Agent_OnLoad_ma02t001(JavaVM *jvm, char *options, void *reserved) { 84 return Agent_Initialize(jvm, options, reserved); 85 } 86 JNIEXPORT jint JNICALL Agent_OnAttach_ma02t001(JavaVM *jvm, char *options, void *reserved) { 87 return Agent_Initialize(jvm, options, reserved); 88 } 89 JNIEXPORT jint JNI_OnLoad_ma02t001(JavaVM *jvm, char *options, void *reserved) { 90 return JNI_VERSION_1_8; 91 } 92 #endif 93 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 94 jvmtiEnv* jvmti = NULL; 95 jvmtiEventCallbacks callbacks; 96 97 NSK_DISPLAY0("Agent_OnLoad\n"); 98 99 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 100 return JNI_ERR; 101 102 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 103 104 if (!NSK_VERIFY((jvmti = 105 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 106 return JNI_ERR; 107 108 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 109 return JNI_ERR; 110 111 memset(&callbacks, 0, sizeof(callbacks)); 112 callbacks.VMInit = &VMInit; 113 callbacks.VMDeath = &VMDeath; 114 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 115 return JNI_ERR; 116 117 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 118 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL))) 119 return JNI_ERR; 120 121 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 122 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 123 return JNI_ERR; 124 125 return JNI_OK; 126 } 127 128 /* ========================================================================== */ 129 130 /* agent library shutdown */ 131 JNIEXPORT void JNICALL 132 #ifdef STATIC_BUILD 133 Agent_OnUnload_ma02t001(JavaVM *jvm) 134 #else 135 Agent_OnUnload(JavaVM *jvm) 136 #endif 137 { 138 const char* KEY_PHRASE = "Agent_OnUnload() of the 1st agent"; 139 140 fprintf(stdout, "%s\n", KEY_PHRASE); 141 fflush(stdout); 142 143 if (VMDeathEventsCount != VMDeathEventsExpected) { 144 NSK_COMPLAIN2("Wrong number of VM death events: %d, expected: %d\n", 145 VMDeathEventsCount, VMDeathEventsExpected); 146 exit(97); 147 } 148 } 149 150 /* ========================================================================== */ 151 152 }