1 /*
   2  * Copyright (c) 2004, 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 <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 
  33 extern "C" {
  34 
  35 /* ========================================================================== */
  36 
  37 /* scaffold objects */
  38 static jlong timeout = 0;
  39 
  40 /* event counts */
  41 static int SingleStepEventsCount = 0;
  42 
  43 /* ========================================================================== */
  44 
  45 /** callback functions **/
  46 
  47 static void JNICALL
  48 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
  49         jmethodID method, jlocation location) {
  50     char *name = NULL;
  51     char *signature = NULL;
  52     char buffer[32];
  53 
  54     SingleStepEventsCount++;
  55 
  56     NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
  57         jvmti_env, JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, NULL));
  58 
  59     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
  60             jvmti_env, method, &name, &signature, NULL))) {
  61         nsk_jvmti_setFailStatus();
  62         return;
  63     }
  64     NSK_DISPLAY3("SingleStep event: %s%s, location=%s\n", name, signature,
  65         jlong_to_string(location, buffer));
  66     if (name != NULL)
  67         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
  68     if (signature != NULL)
  69         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
  70 }
  71 
  72 /* ========================================================================== */
  73 
  74 /** Agent algorithm. */
  75 static void JNICALL
  76 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  77 
  78     if (!nsk_jvmti_waitForSync(timeout))
  79         return;
  80 
  81     /* resume debugee and wait for sync */
  82     if (!nsk_jvmti_resumeSync())
  83         return;
  84     if (!nsk_jvmti_waitForSync(timeout))
  85         return;
  86 
  87     NSK_DISPLAY1("SingleStep events received: %d\n",
  88         SingleStepEventsCount);
  89     if (!NSK_VERIFY(SingleStepEventsCount == 0))
  90         nsk_jvmti_setFailStatus();
  91 
  92     if (!nsk_jvmti_resumeSync())
  93         return;
  94 }
  95 
  96 /* ========================================================================== */
  97 
  98 /** Agent library initialization. */
  99 #ifdef STATIC_BUILD
 100 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t004a(JavaVM *jvm, char *options, void *reserved) {
 101     return Agent_Initialize(jvm, options, reserved);
 102 }
 103 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t004a(JavaVM *jvm, char *options, void *reserved) {
 104     return Agent_Initialize(jvm, options, reserved);
 105 }
 106 JNIEXPORT jint JNI_OnLoad_ma10t004a(JavaVM *jvm, char *options, void *reserved) {
 107     return JNI_VERSION_1_8;
 108 }
 109 #endif
 110 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 111     jvmtiEnv* jvmti = NULL;
 112     jvmtiCapabilities caps;
 113     jvmtiEventCallbacks callbacks;
 114 
 115     NSK_DISPLAY0("Agent_OnLoad\n");
 116 
 117     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 118         return JNI_ERR;
 119 
 120     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 121 
 122     if (!NSK_VERIFY((jvmti =
 123             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 124         return JNI_ERR;
 125 
 126     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 127         return JNI_ERR;
 128 
 129     memset(&caps, 0, sizeof(caps));
 130     caps.can_generate_single_step_events = 1;
 131     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 132         return JNI_ERR;
 133     }
 134 
 135     memset(&callbacks, 0, sizeof(callbacks));
 136     callbacks.SingleStep = &SingleStep;
 137     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 138         return JNI_ERR;
 139 
 140     return JNI_OK;
 141 }
 142 
 143 /* ========================================================================== */
 144 
 145 }