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 ExceptionEventsCount = 0; 42 static int ExceptionCatchEventsCount = 0; 43 44 /* ========================================================================== */ 45 46 /** callback functions **/ 47 48 static void JNICALL 49 Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread, 50 jmethodID method, jlocation location, jobject exception, 51 jmethodID catch_method, jlocation catch_location) { 52 jclass klass = NULL; 53 char *signature = NULL; 54 55 ExceptionEventsCount++; 56 57 if (!NSK_JNI_VERIFY(jni_env, (klass = jni_env->GetObjectClass(exception)) != NULL)) { 58 nsk_jvmti_setFailStatus(); 59 return; 60 } 61 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &signature, NULL))) { 62 nsk_jvmti_setFailStatus(); 63 return; 64 } 65 NSK_DISPLAY1("Exception event: %s\n", signature); 66 if (signature != NULL) 67 jvmti_env->Deallocate((unsigned char*)signature); 68 } 69 70 void JNICALL 71 ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread, 72 jmethodID method, jlocation location, jobject exception) { 73 jclass klass = NULL; 74 char *signature = NULL; 75 76 ExceptionCatchEventsCount++; 77 78 if (!NSK_JNI_VERIFY(jni_env, (klass = jni_env->GetObjectClass(exception)) != NULL)) { 79 nsk_jvmti_setFailStatus(); 80 return; 81 } 82 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &signature, NULL))) { 83 nsk_jvmti_setFailStatus(); 84 return; 85 } 86 NSK_DISPLAY1("ExceptionCatch event: %s\n", signature); 87 if (signature != NULL) 88 jvmti_env->Deallocate((unsigned char*)signature); 89 } 90 91 /* ========================================================================== */ 92 93 /** Agent algorithm. */ 94 static void JNICALL 95 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 96 97 if (!nsk_jvmti_waitForSync(timeout)) 98 return; 99 100 /* resume debugee and wait for sync */ 101 if (!nsk_jvmti_resumeSync()) 102 return; 103 if (!nsk_jvmti_waitForSync(timeout)) 104 return; 105 106 NSK_DISPLAY1("Exception events received: %d\n", 107 ExceptionEventsCount); 108 if (!NSK_VERIFY(ExceptionEventsCount == 0)) 109 nsk_jvmti_setFailStatus(); 110 111 NSK_DISPLAY1("ExceptionCatch events received: %d\n", 112 ExceptionCatchEventsCount); 113 if (!NSK_VERIFY(ExceptionCatchEventsCount == 0)) 114 nsk_jvmti_setFailStatus(); 115 116 if (!nsk_jvmti_resumeSync()) 117 return; 118 } 119 120 /* ========================================================================== */ 121 122 /** Agent library initialization. */ 123 #ifdef STATIC_BUILD 124 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t001a(JavaVM *jvm, char *options, void *reserved) { 125 return Agent_Initialize(jvm, options, reserved); 126 } 127 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t001a(JavaVM *jvm, char *options, void *reserved) { 128 return Agent_Initialize(jvm, options, reserved); 129 } 130 JNIEXPORT jint JNI_OnLoad_ma10t001a(JavaVM *jvm, char *options, void *reserved) { 131 return JNI_VERSION_1_8; 132 } 133 #endif 134 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 135 jvmtiEnv* jvmti = NULL; 136 jvmtiCapabilities caps; 137 jvmtiEventCallbacks callbacks; 138 139 NSK_DISPLAY0("Agent_OnLoad\n"); 140 141 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 142 return JNI_ERR; 143 144 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 145 146 if (!NSK_VERIFY((jvmti = 147 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 148 return JNI_ERR; 149 150 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 151 return JNI_ERR; 152 153 memset(&caps, 0, sizeof(caps)); 154 caps.can_generate_exception_events = 1; 155 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 156 return JNI_ERR; 157 } 158 159 memset(&callbacks, 0, sizeof(callbacks)); 160 callbacks.Exception = &Exception; 161 callbacks.ExceptionCatch = &ExceptionCatch; 162 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 163 return JNI_ERR; 164 165 return JNI_OK; 166 } 167 168 /* ========================================================================== */ 169 170 }