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