1 /*
   2  * Copyright (c) 2007, 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 <string.h>
  25 #include "jvmti.h"
  26 #include <jvmti_tools.h>
  27 #include "JVMTITools.h"
  28 #include "agent_common.h"
  29 
  30 extern "C" {
  31 
  32 /* ============================================================================= */
  33 
  34 static jvmtiEnv *jvmti = NULL;
  35 static jvmtiCapabilities caps;
  36 static jvmtiEventCallbacks callbacks;
  37 
  38 /* ============================================================================= */
  39 
  40 JNIEXPORT jboolean JNICALL
  41 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_suspendThread (
  42         JNIEnv *env
  43         , jclass cls
  44         , jobject earlyReturnThread
  45         )
  46 {
  47     if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(earlyReturnThread)))
  48          return JNI_FALSE;
  49 
  50     return JNI_TRUE;
  51 }
  52 
  53 /* ============================================================================= */
  54 
  55 JNIEXPORT jboolean JNICALL
  56 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_resumeThread (
  57         JNIEnv *env
  58         , jclass klass
  59         , jobject earlyReturnThread
  60         )
  61 {
  62     if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(earlyReturnThread)))
  63          return JNI_FALSE;
  64 
  65     return JNI_TRUE;
  66 }
  67 
  68 /* ============================================================================= */
  69 
  70 JNIEXPORT jboolean JNICALL
  71 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnObject (
  72         JNIEnv *env
  73         , jclass klass
  74         , jthread earlyReturnThread
  75         , jobject valueToReturn
  76         )
  77 {
  78     if (!NSK_JVMTI_VERIFY(jvmti->ForceEarlyReturnObject(earlyReturnThread, valueToReturn)))
  79         return JNI_FALSE;
  80 
  81     return JNI_TRUE;
  82 }
  83 
  84 /* ============================================================================= */
  85 
  86 JNIEXPORT jboolean JNICALL
  87 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnInt(
  88         JNIEnv *env
  89         , jclass klass
  90         , jthread earlyReturnThread
  91         , jint valueToReturn
  92         )
  93 {
  94     if (!NSK_JVMTI_VERIFY(jvmti->ForceEarlyReturnInt(earlyReturnThread, valueToReturn)))
  95         return JNI_FALSE;
  96 
  97     return JNI_TRUE;
  98 }
  99 
 100 /* ============================================================================= */
 101 
 102 JNIEXPORT jboolean JNICALL
 103 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnLong (
 104         JNIEnv *env
 105         , jclass klass
 106         , jthread earlyReturnThread
 107         , jlong valueToReturn
 108         )
 109 {
 110     if (!NSK_JVMTI_VERIFY(jvmti->ForceEarlyReturnLong(earlyReturnThread, valueToReturn)))
 111         return JNI_FALSE;
 112 
 113     return JNI_TRUE;
 114 }
 115 
 116 /* ============================================================================= */
 117 
 118 JNIEXPORT jboolean JNICALL
 119 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnFloat (
 120         JNIEnv *env
 121         , jclass klass
 122         , jthread earlyReturnThread
 123         , jfloat valueToReturn
 124         )
 125 {
 126     if (!NSK_JVMTI_VERIFY(jvmti->ForceEarlyReturnFloat(earlyReturnThread, valueToReturn)))
 127         return JNI_FALSE;
 128 
 129     return JNI_TRUE;
 130 }
 131 
 132 /* ============================================================================= */
 133 
 134 JNIEXPORT jboolean JNICALL
 135 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnDouble (
 136         JNIEnv *env
 137         , jclass klass
 138         , jthread earlyReturnThread
 139         , jdouble valueToReturn
 140         )
 141 {
 142     if (!NSK_JVMTI_VERIFY(jvmti->ForceEarlyReturnDouble(earlyReturnThread, valueToReturn)))
 143         return JNI_FALSE;
 144 
 145     return JNI_TRUE;
 146 }
 147 
 148 /* ============================================================================= */
 149 
 150 JNIEXPORT jboolean JNICALL
 151 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnVoid (
 152         JNIEnv *env
 153         , jclass klass
 154         , jthread earlyReturnThread
 155         )
 156 {
 157     if (!NSK_JVMTI_VERIFY(jvmti->ForceEarlyReturnVoid(earlyReturnThread)))
 158         return JNI_FALSE;
 159 
 160     return JNI_TRUE;
 161 }
 162 
 163 /* ============================================================================= */
 164 
 165 /* Agent initialization procedure */
 166 #ifdef STATIC_BUILD
 167 JNIEXPORT jint JNICALL Agent_OnLoad_ForceEarlyReturn001(JavaVM *jvm, char *options, void *reserved) {
 168     return Agent_Initialize(jvm, options, reserved);
 169 }
 170 JNIEXPORT jint JNICALL Agent_OnAttach_ForceEarlyReturn001(JavaVM *jvm, char *options, void *reserved) {
 171     return Agent_Initialize(jvm, options, reserved);
 172 }
 173 JNIEXPORT jint JNI_OnLoad_ForceEarlyReturn001(JavaVM *jvm, char *options, void *reserved) {
 174     return JNI_VERSION_1_8;
 175 }
 176 #endif
 177 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved)
 178 {
 179     jvmtiCapabilities caps;
 180 
 181     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 182         return JNI_ERR;
 183 
 184     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
 185         return JNI_ERR;
 186 
 187     // Register all necessary JVM capabilities
 188     caps.can_force_early_return = 1;
 189     caps.can_suspend = 1;
 190 
 191     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 192         return JNI_ERR;
 193 
 194     return JNI_OK;
 195 }
 196 
 197 }