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 
  24 #include <stdio.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <aod.h>
  28 
  29 extern "C" {
  30 
  31 /*
  32  * Test checks following spec clause: "Agent_OnAttach function is invoked even if the agent library was loaded
  33  * prior to invoking this method"
  34  *
  35  * This agent is loaded as static agent via 'agentlib:' VM option and also dynamically attached, so this agent
  36  * has both Agent_OnLoad and Agent_OnAttach.
  37  */
  38 
  39 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *optionsString, void *reserved) {
  40     // can't use NSK_DISPLAY since needed for nsk_ functions initialization isn't done here
  41     printf("Agent_OnLoad: agent is loaded\n");
  42     fflush(stdout);
  43     return JNI_OK;
  44 }
  45 
  46 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) {
  47     JNIEnv* jni = NULL;
  48     Options* options = NULL;
  49     const char* agentName;
  50 
  51     options = (Options*) nsk_aod_createOptions(optionsString);
  52     if (!NSK_VERIFY(options != NULL))
  53         return JNI_ERR;
  54 
  55     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  56 
  57     jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
  58     if (jni == NULL)
  59         return JNI_ERR;
  60 
  61     NSK_DISPLAY1("%s: initialization was done\n", agentName);
  62 
  63     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
  64         return JNI_ERR;
  65 
  66     nsk_aod_agentFinished(jni, agentName, 1);
  67 
  68     return JNI_OK;
  69 }
  70 
  71 }