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