1 /* 2 * Copyright (c) 2003, 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 <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <jvmti.h> 28 #include "agent_common.h" 29 30 #include "nsk_tools.h" 31 #include "JVMTITools.h" 32 #include "jvmti_tools.h" 33 #include "jni_tools.h" 34 35 extern "C" { 36 37 #define STATUS_FAILED 2 38 #define PASSED 0 39 40 static volatile jint result = PASSED; 41 static volatile long wrongStepEv = 0; 42 43 static jvmtiEnv *jvmti = NULL; 44 static jvmtiEventCallbacks callbacks; 45 static jvmtiCapabilities caps; 46 47 /** callback functions **/ 48 void JNICALL 49 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread, 50 jmethodID method, jlocation location) { 51 jvmtiPhase phase; 52 53 if (!NSK_JVMTI_VERIFY(jvmti_env->GetPhase(&phase))) { 54 result = STATUS_FAILED; 55 NSK_COMPLAIN0("TEST FAILED: unable to obtain phase of the VM execution during SingleStep callback\n\n"); 56 } 57 else { 58 if (phase != JVMTI_PHASE_LIVE) { 59 wrongStepEv++; 60 result = STATUS_FAILED; 61 NSK_COMPLAIN1("TEST FAILED: SingleStep event received during non-live phase %s\n", 62 TranslatePhase(phase)); 63 } 64 } 65 } 66 67 void JNICALL 68 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 69 NSK_DISPLAY0("VMDeath event received\n"); 70 71 if (wrongStepEv != 0) { 72 NSK_COMPLAIN1( 73 "TEST FAILED: there are %d SingleStep events\n" 74 "sent during non-live phase of the VM execution\n", 75 wrongStepEv); 76 } 77 78 if (result == STATUS_FAILED) 79 exit(95 + STATUS_FAILED); 80 } 81 /************************/ 82 83 #ifdef STATIC_BUILD 84 JNIEXPORT jint JNICALL Agent_OnLoad_singlestep002(JavaVM *jvm, char *options, void *reserved) { 85 return Agent_Initialize(jvm, options, reserved); 86 } 87 JNIEXPORT jint JNICALL Agent_OnAttach_singlestep002(JavaVM *jvm, char *options, void *reserved) { 88 return Agent_Initialize(jvm, options, reserved); 89 } 90 JNIEXPORT jint JNI_OnLoad_singlestep002(JavaVM *jvm, char *options, void *reserved) { 91 return JNI_VERSION_1_8; 92 } 93 #endif 94 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 95 /* init framework and parse options */ 96 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 97 return JNI_ERR; 98 99 /* create JVMTI environment */ 100 if (!NSK_VERIFY((jvmti = 101 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 102 return JNI_ERR; 103 104 /* add capability to generate compiled method events */ 105 memset(&caps, 0, sizeof(jvmtiCapabilities)); 106 caps.can_generate_single_step_events = 1; 107 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) 108 return JNI_ERR; 109 110 if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps))) 111 return JNI_ERR; 112 113 if (!caps.can_generate_single_step_events) 114 NSK_DISPLAY0("Warning: generation of single step events is not implemented\n"); 115 116 /* set event callback */ 117 NSK_DISPLAY0("setting event callbacks ...\n"); 118 (void) memset(&callbacks, 0, sizeof(callbacks)); 119 callbacks.SingleStep = &SingleStep; 120 callbacks.VMDeath = &VMDeath; 121 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)))) 122 return JNI_ERR; 123 124 NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n"); 125 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL))) 126 return JNI_ERR; 127 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 128 return JNI_ERR; 129 NSK_DISPLAY0("enabling the events done\n\n"); 130 131 return JNI_OK; 132 } 133 134 }