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 /* test objects */ 41 static jthread thread = NULL; 42 43 /* event counts */ 44 static int MethodExitEventsCount = 0; 45 46 /* ========================================================================== */ 47 48 /** callback functions **/ 49 50 static void JNICALL 51 MethodExit(jvmtiEnv *jvmti_env, JNIEnv *jni_env, 52 jthread thread, jmethodID method, 53 jboolean was_poped_by_exception, jvalue return_value) { 54 char *name = NULL; 55 char *signature = NULL; 56 57 MethodExitEventsCount++; 58 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, 59 jvmti_env, method, &name, &signature, NULL))) { 60 nsk_jvmti_setFailStatus(); 61 return; 62 } 63 NSK_DISPLAY2("MethodExit event: %s%s\n", name, signature); 64 if (name != NULL) 65 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name); 66 if (signature != NULL) 67 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature); 68 } 69 70 /* ========================================================================== */ 71 72 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) { 73 const char* THREAD_NAME = "Debuggee Thread"; 74 jvmtiThreadInfo info; 75 jthread *threads = NULL; 76 jint threads_count = 0; 77 int i; 78 79 NSK_DISPLAY0("Prepare: find tested thread\n"); 80 81 /* get all live threads */ 82 if (!NSK_JVMTI_VERIFY( 83 NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads))) 84 return NSK_FALSE; 85 86 if (!NSK_VERIFY(threads_count > 0 && threads != NULL)) 87 return NSK_FALSE; 88 89 /* find tested thread */ 90 for (i = 0; i < threads_count; i++) { 91 if (!NSK_VERIFY(threads[i] != NULL)) 92 return NSK_FALSE; 93 94 /* get thread information */ 95 if (!NSK_JVMTI_VERIFY( 96 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info))) 97 return NSK_FALSE; 98 99 NSK_DISPLAY3(" thread #%d (%s): %p\n", i, info.name, threads[i]); 100 101 /* find by name */ 102 if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) { 103 thread = threads[i]; 104 } 105 } 106 107 if (!NSK_JNI_VERIFY(jni, (thread = 108 NSK_CPP_STUB2(NewGlobalRef, jni, thread)) != NULL)) 109 return NSK_FALSE; 110 111 /* deallocate threads list */ 112 if (!NSK_JVMTI_VERIFY( 113 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads))) 114 return NSK_FALSE; 115 116 return NSK_TRUE; 117 } 118 119 /* ========================================================================== */ 120 121 /** Agent algorithm. */ 122 static void JNICALL 123 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 124 125 if (!nsk_jvmti_waitForSync(timeout)) 126 return; 127 128 if (!prepare(jvmti, jni)) { 129 nsk_jvmti_setFailStatus(); 130 return; 131 } 132 133 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 134 jvmti, JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread))) 135 nsk_jvmti_setFailStatus(); 136 137 /* resume debugee and wait for sync */ 138 if (!nsk_jvmti_resumeSync()) 139 return; 140 if (!nsk_jvmti_waitForSync(timeout)) 141 return; 142 143 NSK_DISPLAY1("MethodExit events received: %d\n", 144 MethodExitEventsCount); 145 if (!NSK_VERIFY(MethodExitEventsCount != 0)) 146 nsk_jvmti_setFailStatus(); 147 148 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, thread)); 149 150 if (!nsk_jvmti_resumeSync()) 151 return; 152 } 153 154 /* ========================================================================== */ 155 156 /** Agent library initialization. */ 157 #ifdef STATIC_BUILD 158 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t003(JavaVM *jvm, char *options, void *reserved) { 159 return Agent_Initialize(jvm, options, reserved); 160 } 161 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t003(JavaVM *jvm, char *options, void *reserved) { 162 return Agent_Initialize(jvm, options, reserved); 163 } 164 JNIEXPORT jint JNI_OnLoad_ma10t003(JavaVM *jvm, char *options, void *reserved) { 165 return JNI_VERSION_1_8; 166 } 167 #endif 168 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 169 jvmtiEnv* jvmti = NULL; 170 jvmtiCapabilities caps; 171 jvmtiEventCallbacks callbacks; 172 173 NSK_DISPLAY0("Agent_OnLoad\n"); 174 175 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 176 return JNI_ERR; 177 178 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 179 180 if (!NSK_VERIFY((jvmti = 181 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 182 return JNI_ERR; 183 184 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 185 return JNI_ERR; 186 187 memset(&caps, 0, sizeof(caps)); 188 caps.can_generate_method_exit_events = 1; 189 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { 190 return JNI_ERR; 191 } 192 193 memset(&callbacks, 0, sizeof(callbacks)); 194 callbacks.MethodExit = &MethodExit; 195 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 196 return JNI_ERR; 197 198 return JNI_OK; 199 } 200 201 /* ========================================================================== */ 202 203 }