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(NSK_CPP_STUB2(GetPhase, 54 jvmti_env, &phase))) { 55 result = STATUS_FAILED; 56 NSK_COMPLAIN0("TEST FAILED: unable to obtain phase of the VM execution during SingleStep callback\n\n"); 57 } 58 else { 59 if (phase != JVMTI_PHASE_LIVE) { 60 wrongStepEv++; 61 result = STATUS_FAILED; 62 NSK_COMPLAIN1("TEST FAILED: SingleStep event received during non-live phase %s\n", 63 TranslatePhase(phase)); 64 } 65 } 66 } 67 68 void JNICALL 69 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 70 NSK_DISPLAY0("VMDeath event received\n"); 71 72 if (wrongStepEv != 0) { 73 NSK_COMPLAIN1( 74 "TEST FAILED: there are %d SingleStep events\n" 75 "sent during non-live phase of the VM execution\n", 76 wrongStepEv); 77 } 78 79 if (result == STATUS_FAILED) 80 exit(95 + STATUS_FAILED); 81 } 82 /************************/ 83 84 #ifdef STATIC_BUILD 85 JNIEXPORT jint JNICALL Agent_OnLoad_singlestep002(JavaVM *jvm, char *options, void *reserved) { 86 return Agent_Initialize(jvm, options, reserved); 87 } 88 JNIEXPORT jint JNICALL Agent_OnAttach_singlestep002(JavaVM *jvm, char *options, void *reserved) { 89 return Agent_Initialize(jvm, options, reserved); 90 } 91 JNIEXPORT jint JNI_OnLoad_singlestep002(JavaVM *jvm, char *options, void *reserved) { 92 return JNI_VERSION_1_8; 93 } 94 #endif 95 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 96 /* init framework and parse options */ 97 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 98 return JNI_ERR; 99 100 /* create JVMTI environment */ 101 if (!NSK_VERIFY((jvmti = 102 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 103 return JNI_ERR; 104 105 /* add capability to generate compiled method events */ 106 memset(&caps, 0, sizeof(jvmtiCapabilities)); 107 caps.can_generate_single_step_events = 1; 108 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, 109 jvmti, &caps))) 110 return JNI_ERR; 111 112 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities, 113 jvmti, &caps))) 114 return JNI_ERR; 115 116 if (!caps.can_generate_single_step_events) 117 NSK_DISPLAY0("Warning: generation of single step events is not implemented\n"); 118 119 /* set event callback */ 120 NSK_DISPLAY0("setting event callbacks ...\n"); 121 (void) memset(&callbacks, 0, sizeof(callbacks)); 122 callbacks.SingleStep = &SingleStep; 123 callbacks.VMDeath = &VMDeath; 124 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, 125 jvmti, &callbacks, sizeof(callbacks)))) 126 return JNI_ERR; 127 128 NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n"); 129 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 130 jvmti, JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL))) 131 return JNI_ERR; 132 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 133 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 134 return JNI_ERR; 135 NSK_DISPLAY0("enabling the events done\n\n"); 136 137 return JNI_OK; 138 } 139 140 }