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 extern "C" {
  32 
  33 /*
  34  * Simple agent, doesn't perform any specific checks,
  35  * just gets JVMTI environment and calls nsk_aod_agentLoaded and nsk_aod_agentFinished
  36  * from Agent_OnAttach
  37  */
  38 
  39 #ifdef STATIC_BUILD
  40 JNIEXPORT jint JNI_OnLoad_simpleAgent00(JavaVM *jvm, char *options, void *reserved) {
  41     return JNI_VERSION_1_8;
  42 }
  43 #endif
  44 
  45 JNIEXPORT jint JNICALL
  46 #ifdef STATIC_BUILD
  47 Agent_OnAttach_simpleAgent00(JavaVM *vm, char *optionsString, void *reserved)
  48 #else
  49 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  50 #endif
  51 {
  52     jvmtiEnv* jvmti;
  53     JNIEnv* jni = NULL;
  54     Options* options = NULL;
  55     const char* agentName;
  56 
  57     options = (Options*) nsk_aod_createOptions(optionsString);
  58     if (!NSK_VERIFY(options != NULL))
  59         return JNI_ERR;
  60 
  61     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  62 
  63     jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
  64     if (jni == NULL)
  65         return JNI_ERR;
  66 
  67     jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
  68     if (!NSK_VERIFY(jvmti != NULL))
  69         return JNI_ERR;
  70 
  71     NSK_DISPLAY1("%s: initialization was done\n", agentName);
  72 
  73     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
  74         return JNI_ERR;
  75 
  76     nsk_aod_agentFinished(jni, agentName, 1);
  77 
  78     return JNI_OK;
  79 }
  80 
  81 }