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