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     if (!isThreadExpected(jvmti_env, thread)) {
  56         return;
  57     }
  58 
  59     ExceptionEventsCount++;
  60 
  61     if (!NSK_JNI_VERIFY(jni_env, (klass =
  62             NSK_CPP_STUB2(GetObjectClass, jni_env, exception)) != NULL)) {
  63         nsk_jvmti_setFailStatus();
  64         return;
  65     }
  66     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env,
  67             klass, &signature, NULL))) {
  68         nsk_jvmti_setFailStatus();
  69         return;
  70     }
  71     NSK_DISPLAY1("Exception event: %s\n", signature);
  72     if (signature != NULL)
  73         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
  74 }
  75 
  76 void JNICALL
  77 ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
  78         jmethodID method, jlocation location, jobject exception) {
  79     jclass klass = NULL;
  80     char *signature = NULL;
  81 
  82     if (!isThreadExpected(jvmti_env, thread)) {
  83         return;
  84     }
  85 
  86     ExceptionCatchEventsCount++;
  87 
  88     if (!NSK_JNI_VERIFY(jni_env, (klass =
  89             NSK_CPP_STUB2(GetObjectClass, jni_env, exception)) != NULL)) {
  90         nsk_jvmti_setFailStatus();
  91         return;
  92     }
  93     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env,
  94             klass, &signature, NULL))) {
  95         nsk_jvmti_setFailStatus();
  96         return;
  97     }
  98     NSK_DISPLAY1("ExceptionCatch event: %s\n", signature);
  99     if (signature != NULL)
 100         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
 101 }
 102 
 103 /* ========================================================================== */
 104 
 105 /** Agent algorithm. */
 106 static void JNICALL
 107 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 108 
 109     if (!nsk_jvmti_waitForSync(timeout))
 110         return;
 111 
 112     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 113             jvmti, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
 114         nsk_jvmti_setFailStatus();
 115     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 116             jvmti, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION_CATCH, NULL)))
 117         nsk_jvmti_setFailStatus();
 118 
 119     /* resume debugee and wait for sync */
 120     if (!nsk_jvmti_resumeSync())
 121         return;
 122     if (!nsk_jvmti_waitForSync(timeout))
 123         return;
 124 
 125     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 126             jvmti, JVMTI_DISABLE, JVMTI_EVENT_EXCEPTION, NULL)))
 127         nsk_jvmti_setFailStatus();
 128     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 129             jvmti, JVMTI_DISABLE, JVMTI_EVENT_EXCEPTION_CATCH, NULL)))
 130         nsk_jvmti_setFailStatus();
 131 
 132     NSK_DISPLAY1("Exception events received: %d\n",
 133         ExceptionEventsCount);
 134     if (!NSK_VERIFY(ExceptionEventsCount == 3))
 135         nsk_jvmti_setFailStatus();
 136 
 137     NSK_DISPLAY1("ExceptionCatch events received: %d\n",
 138         ExceptionCatchEventsCount);
 139     if (!NSK_VERIFY(ExceptionCatchEventsCount == 3))
 140         nsk_jvmti_setFailStatus();
 141 
 142     if (!nsk_jvmti_resumeSync())
 143         return;
 144 }
 145 
 146 /* ========================================================================== */
 147 
 148 /** Agent library initialization. */
 149 #ifdef STATIC_BUILD
 150 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t001(JavaVM *jvm, char *options, void *reserved) {
 151     return Agent_Initialize(jvm, options, reserved);
 152 }
 153 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t001(JavaVM *jvm, char *options, void *reserved) {
 154     return Agent_Initialize(jvm, options, reserved);
 155 }
 156 JNIEXPORT jint JNI_OnLoad_ma10t001(JavaVM *jvm, char *options, void *reserved) {
 157     return JNI_VERSION_1_8;
 158 }
 159 #endif
 160 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 161     jvmtiEnv* jvmti = NULL;
 162     jvmtiCapabilities caps;
 163     jvmtiEventCallbacks callbacks;
 164 
 165     NSK_DISPLAY0("Agent_OnLoad\n");
 166 
 167     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 168         return JNI_ERR;
 169 
 170     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 171 
 172     if (!NSK_VERIFY((jvmti =
 173             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 174         return JNI_ERR;
 175 
 176     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 177         return JNI_ERR;
 178 
 179     memset(&caps, 0, sizeof(caps));
 180     caps.can_generate_exception_events = 1;
 181     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 182         return JNI_ERR;
 183     }
 184 
 185     memset(&callbacks, 0, sizeof(callbacks));
 186     callbacks.Exception = &Exception;
 187     callbacks.ExceptionCatch = &ExceptionCatch;
 188     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 189         return JNI_ERR;
 190 
 191     return JNI_OK;
 192 }
 193 
 194 /* ========================================================================== */
 195 
 196 }