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 MethodExitEventsCount = 0;
  42 
  43 /* ========================================================================== */
  44 
  45 /** callback functions **/
  46 
  47 static void JNICALL
  48 MethodExit(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
  49         jthread thread, jmethodID method,
  50         jboolean was_poped_by_exception, jvalue return_value) {
  51     char *name = NULL;
  52     char *signature = NULL;
  53 
  54     MethodExitEventsCount++;
  55     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
  56             jvmti_env, method, &name, &signature, NULL))) {
  57         nsk_jvmti_setFailStatus();
  58         return;
  59     }
  60     NSK_DISPLAY2("MethodExit event: %s%s\n", name, signature);
  61     if (name != NULL)
  62         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
  63     if (signature != NULL)
  64         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
  65 }
  66 
  67 /* ========================================================================== */
  68 
  69 /** Agent algorithm. */
  70 static void JNICALL
  71 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  72 
  73     if (!nsk_jvmti_waitForSync(timeout))
  74         return;
  75 
  76     /* resume debugee and wait for sync */
  77     if (!nsk_jvmti_resumeSync())
  78         return;
  79     if (!nsk_jvmti_waitForSync(timeout))
  80         return;
  81 
  82     NSK_DISPLAY1("MethodExit events received: %d\n",
  83         MethodExitEventsCount);
  84     if (!NSK_VERIFY(MethodExitEventsCount == 0))
  85         nsk_jvmti_setFailStatus();
  86 
  87     if (!nsk_jvmti_resumeSync())
  88         return;
  89 }
  90 
  91 /* ========================================================================== */
  92 
  93 /** Agent library initialization. */
  94 #ifdef STATIC_BUILD
  95 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t003a(JavaVM *jvm, char *options, void *reserved) {
  96     return Agent_Initialize(jvm, options, reserved);
  97 }
  98 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t003a(JavaVM *jvm, char *options, void *reserved) {
  99     return Agent_Initialize(jvm, options, reserved);
 100 }
 101 JNIEXPORT jint JNI_OnLoad_ma10t003a(JavaVM *jvm, char *options, void *reserved) {
 102     return JNI_VERSION_1_8;
 103 }
 104 #endif
 105 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 106     jvmtiEnv* jvmti = NULL;
 107     jvmtiCapabilities caps;
 108     jvmtiEventCallbacks callbacks;
 109 
 110     NSK_DISPLAY0("Agent_OnLoad\n");
 111 
 112     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 113         return JNI_ERR;
 114 
 115     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 116 
 117     if (!NSK_VERIFY((jvmti =
 118             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 119         return JNI_ERR;
 120 
 121     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 122         return JNI_ERR;
 123 
 124     memset(&caps, 0, sizeof(caps));
 125     caps.can_generate_method_exit_events = 1;
 126     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 127         return JNI_ERR;
 128     }
 129 
 130     memset(&callbacks, 0, sizeof(callbacks));
 131     callbacks.MethodExit = &MethodExit;
 132     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 133         return JNI_ERR;
 134 
 135     return JNI_OK;
 136 }
 137 
 138 /* ========================================================================== */
 139 
 140 }