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(jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, NULL));
  57 
  58     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
  59         nsk_jvmti_setFailStatus();
  60         return;
  61     }
  62     NSK_DISPLAY3("SingleStep event: %s%s, location=%s\n", name, signature,
  63         jlong_to_string(location, buffer));
  64     if (name != NULL)
  65         jvmti_env->Deallocate((unsigned char*)name);
  66     if (signature != NULL)
  67         jvmti_env->Deallocate((unsigned char*)signature);
  68 }
  69 
  70 /* ========================================================================== */
  71 
  72 /** Agent algorithm. */
  73 static void JNICALL
  74 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  75 
  76     if (!nsk_jvmti_waitForSync(timeout))
  77         return;
  78 
  79     /* resume debugee and wait for sync */
  80     if (!nsk_jvmti_resumeSync())
  81         return;
  82     if (!nsk_jvmti_waitForSync(timeout))
  83         return;
  84 
  85     NSK_DISPLAY1("SingleStep events received: %d\n",
  86         SingleStepEventsCount);
  87     if (!NSK_VERIFY(SingleStepEventsCount == 0))
  88         nsk_jvmti_setFailStatus();
  89 
  90     if (!nsk_jvmti_resumeSync())
  91         return;
  92 }
  93 
  94 /* ========================================================================== */
  95 
  96 /** Agent library initialization. */
  97 #ifdef STATIC_BUILD
  98 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t004a(JavaVM *jvm, char *options, void *reserved) {
  99     return Agent_Initialize(jvm, options, reserved);
 100 }
 101 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t004a(JavaVM *jvm, char *options, void *reserved) {
 102     return Agent_Initialize(jvm, options, reserved);
 103 }
 104 JNIEXPORT jint JNI_OnLoad_ma10t004a(JavaVM *jvm, char *options, void *reserved) {
 105     return JNI_VERSION_1_8;
 106 }
 107 #endif
 108 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 109     jvmtiEnv* jvmti = NULL;
 110     jvmtiCapabilities caps;
 111     jvmtiEventCallbacks callbacks;
 112 
 113     NSK_DISPLAY0("Agent_OnLoad\n");
 114 
 115     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 116         return JNI_ERR;
 117 
 118     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 119 
 120     if (!NSK_VERIFY((jvmti =
 121             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 122         return JNI_ERR;
 123 
 124     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 125         return JNI_ERR;
 126 
 127     memset(&caps, 0, sizeof(caps));
 128     caps.can_generate_single_step_events = 1;
 129     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 130         return JNI_ERR;
 131     }
 132 
 133     memset(&callbacks, 0, sizeof(callbacks));
 134     callbacks.SingleStep = &SingleStep;
 135     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 136         return JNI_ERR;
 137 
 138     return JNI_OK;
 139 }
 140 
 141 /* ========================================================================== */
 142 
 143 }