1 /*
   2  * Copyright (c) 2008, 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 #define ON_UNLOAD_MARKER "attach050.on_unload"
  32 
  33 extern "C" {
  34 
  35 /*
  36  * This agent gets JVMTI environment and calls nsk_aod_agentLoaded and
  37  * nsk_aod_agentFinished from Agent_OnAttach. It also has
  38  * Agent_OnUnload function that prints marker string to the stdout,
  39  * providing a means for the test runner to check whether
  40  * Agent_OnUnload is called on agent unload during VM shutdown.
  41  */
  42 
  43 #ifdef STATIC_BUILD
  44 JNIEXPORT jint JNI_OnLoad_attach050Agent00(JavaVM *jvm, char *options, void *reserved) {
  45     return JNI_VERSION_1_8;
  46 }
  47 #endif
  48 
  49 JNIEXPORT jint JNICALL
  50 #ifdef STATIC_BUILD
  51 Agent_OnAttach_attach050Agent00(JavaVM *vm, char *optionsString, void *reserved)
  52 #else
  53 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  54 #endif
  55 {
  56     jvmtiEnv* jvmti;
  57     JNIEnv* jni = NULL;
  58     Options* options = NULL;
  59     const char* agentName;
  60 
  61     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
  62         return JNI_ERR;
  63 
  64     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  65 
  66     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
  67         return JNI_ERR;
  68 
  69     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
  70         return JNI_ERR;
  71 
  72     NSK_DISPLAY1("%s: initialization was done\n", agentName);
  73 
  74     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
  75         return JNI_ERR;
  76 
  77     nsk_aod_agentFinished(jni, agentName, 1);
  78 
  79     return JNI_OK;
  80 }
  81 
  82 
  83 /* agent library shutdown */
  84 JNIEXPORT void JNICALL
  85 #ifdef STATIC_BUILD
  86 Agent_OnUnload_attach050Agent00(JavaVM *jvm)
  87 #else
  88 Agent_OnUnload(JavaVM *jvm)
  89 #endif
  90 {
  91     fprintf(stdout, "%s%s", ON_UNLOAD_MARKER, "\n");
  92     fflush(stdout);
  93 }
  94 
  95 }