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(
  48                 jvmti                    ->SuspendThread                    (earlyReturnThread                )
  49             )
  50         )
  51          return JNI_FALSE;
  52 
  53     return JNI_TRUE;
  54 }
  55 
  56 /* ============================================================================= */
  57 
  58 JNIEXPORT jboolean JNICALL
  59 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_resumeThread (
  60         JNIEnv *env
  61         , jclass klass
  62         , jobject earlyReturnThread
  63         )
  64 {
  65     if (!NSK_JVMTI_VERIFY(
  66                 jvmti                    ->ResumeThread                    (earlyReturnThread                )
  67             )
  68         )
  69          return JNI_FALSE;
  70 
  71     return JNI_TRUE;
  72 }
  73 
  74 /* ============================================================================= */
  75 
  76 JNIEXPORT jboolean JNICALL
  77 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnObject (
  78         JNIEnv *env
  79         , jclass klass
  80         , jthread earlyReturnThread
  81         , jobject valueToReturn
  82         )
  83 {
  84     if (!NSK_JVMTI_VERIFY(
  85                 jvmti                    ->ForceEarlyReturnObject                    (earlyReturnThread                    , valueToReturn                    )
  86                 )
  87        )
  88         return JNI_FALSE;
  89 
  90     return JNI_TRUE;
  91 }
  92 
  93 /* ============================================================================= */
  94 
  95 JNIEXPORT jboolean JNICALL
  96 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnInt(
  97         JNIEnv *env
  98         , jclass klass
  99         , jthread earlyReturnThread
 100         , jint valueToReturn
 101         )
 102 {
 103     if (!NSK_JVMTI_VERIFY(
 104                 jvmti                    ->ForceEarlyReturnInt                    (earlyReturnThread                    , valueToReturn                    )
 105                 )
 106        )
 107         return JNI_FALSE;
 108 
 109     return JNI_TRUE;
 110 }
 111 
 112 /* ============================================================================= */
 113 
 114 JNIEXPORT jboolean JNICALL
 115 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnLong (
 116         JNIEnv *env
 117         , jclass klass
 118         , jthread earlyReturnThread
 119         , jlong valueToReturn
 120         )
 121 {
 122     if (!NSK_JVMTI_VERIFY(
 123                 jvmti                    ->ForceEarlyReturnLong                    (earlyReturnThread                    , valueToReturn                    )
 124                 )
 125        )
 126         return JNI_FALSE;
 127 
 128     return JNI_TRUE;
 129 }
 130 
 131 /* ============================================================================= */
 132 
 133 JNIEXPORT jboolean JNICALL
 134 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnFloat (
 135         JNIEnv *env
 136         , jclass klass
 137         , jthread earlyReturnThread
 138         , jfloat valueToReturn
 139         )
 140 {
 141     if (!NSK_JVMTI_VERIFY(
 142                 jvmti                    ->ForceEarlyReturnFloat                    (earlyReturnThread                    , valueToReturn                    )
 143                 )
 144        )
 145         return JNI_FALSE;
 146 
 147     return JNI_TRUE;
 148 }
 149 
 150 /* ============================================================================= */
 151 
 152 JNIEXPORT jboolean JNICALL
 153 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnDouble (
 154         JNIEnv *env
 155         , jclass klass
 156         , jthread earlyReturnThread
 157         , jdouble valueToReturn
 158         )
 159 {
 160     if (!NSK_JVMTI_VERIFY(
 161                 jvmti                    ->ForceEarlyReturnDouble                    (earlyReturnThread                    , valueToReturn                    )
 162                 )
 163        )
 164         return JNI_FALSE;
 165 
 166     return JNI_TRUE;
 167 }
 168 
 169 /* ============================================================================= */
 170 
 171 JNIEXPORT jboolean JNICALL
 172 Java_nsk_jvmti_ForceEarlyReturn_ForceEarlyReturn001_doForceEarlyReturnVoid (
 173         JNIEnv *env
 174         , jclass klass
 175         , jthread earlyReturnThread
 176         )
 177 {
 178     if (!NSK_JVMTI_VERIFY(
 179                 jvmti                    ->ForceEarlyReturnVoid                    (earlyReturnThread                    )
 180                 )
 181        )
 182         return JNI_FALSE;
 183 
 184     return JNI_TRUE;
 185 }
 186 
 187 /* ============================================================================= */
 188 
 189 /* Agent initialization procedure */
 190 #ifdef STATIC_BUILD
 191 JNIEXPORT jint JNICALL Agent_OnLoad_ForceEarlyReturn001(JavaVM *jvm, char *options, void *reserved) {
 192     return Agent_Initialize(jvm, options, reserved);
 193 }
 194 JNIEXPORT jint JNICALL Agent_OnAttach_ForceEarlyReturn001(JavaVM *jvm, char *options, void *reserved) {
 195     return Agent_Initialize(jvm, options, reserved);
 196 }
 197 JNIEXPORT jint JNI_OnLoad_ForceEarlyReturn001(JavaVM *jvm, char *options, void *reserved) {
 198     return JNI_VERSION_1_8;
 199 }
 200 #endif
 201 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved)
 202 {
 203     jvmtiCapabilities caps;
 204 
 205     if (!NSK_VERIFY(
 206                 (jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL
 207                 )
 208        )
 209         return JNI_ERR;
 210 
 211     if (!NSK_JVMTI_VERIFY(jvmti                    ->GetCapabilities                    (&caps)
 212                 )
 213        )
 214         return JNI_ERR;
 215 
 216     // Register all necessary JVM capabilities
 217     caps.can_force_early_return = 1;
 218     caps.can_suspend = 1;
 219 
 220     if (!NSK_JVMTI_VERIFY(jvmti                    ->AddCapabilities                    (&caps)
 221                 )
 222        )
 223         return JNI_ERR;
 224 
 225     return JNI_OK;
 226 }
 227 
 228 }