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  * Agent tries to get all potential capabilities
  35  * (capabilities returned by the JVMTI function GetPotentialCapabilities)
  36  */
  37 
  38 #ifdef STATIC_BUILD
  39 JNIEXPORT jint JNI_OnLoad_attach012Agent00(JavaVM *jvm, char *options, void *reserved) {
  40     return JNI_VERSION_1_8;
  41 }
  42 #endif
  43 
  44 JNIEXPORT jint JNICALL
  45 #ifdef STATIC_BUILD
  46 Agent_OnAttach_attach012Agent00(JavaVM *vm, char *optionsString, void *reserved)
  47 #else
  48 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  49 #endif
  50 {
  51     jvmtiEnv* jvmti;
  52     JNIEnv* jni = NULL;
  53     Options* options = NULL;
  54     const char* agentName;
  55     jvmtiCapabilities caps;
  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 NSK_FALSE;
  66 
  67     jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
  68     if (!NSK_VERIFY(jvmti != NULL))
  69         return JNI_ERR;
  70 
  71     if (!NSK_JVMTI_VERIFY(jvmti->GetPotentialCapabilities(&caps))) {
  72         return JNI_ERR;
  73     }
  74 
  75     NSK_DISPLAY1("%s: potential capabilities:\n", agentName);
  76 
  77     printCapabilities(caps);
  78 
  79     NSK_DISPLAY1("%s: trying to get all potential capabilities:\n", agentName);
  80 
  81     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
  82         return JNI_ERR;
  83     }
  84 
  85     NSK_DISPLAY1("%s: initialization was done\n", agentName);
  86 
  87     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
  88         return JNI_ERR;
  89 
  90     nsk_aod_agentFinished(jni, agentName, 1);
  91 
  92     return JNI_OK;
  93 }
  94 
  95 }