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