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 /* test objects */
  41 static int VMInitEventsCount = 0;
  42 static int VMInitEventsExpected = 1;
  43 static int VMDeathEventsCount = 0;
  44 static int VMDeathEventsExpected = 1;
  45 static int ThreadStartEventsCount = 0;
  46 
  47 /* ========================================================================== */
  48 
  49 /** callback functions **/
  50 
  51 void JNICALL VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
  52     NSK_DISPLAY0("VMInit event\n");
  53     VMInitEventsCount++;
  54 }
  55 
  56 void JNICALL ThreadStart(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
  57     NSK_DISPLAY0("ThreadStart event\n");
  58     ThreadStartEventsCount++;
  59 }
  60 
  61 void JNICALL
  62 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {
  63     NSK_DISPLAY0("VMDeath event\n");
  64     VMDeathEventsCount++;
  65 }
  66 
  67 /* ========================================================================== */
  68 
  69 /** Agent algorithm. */
  70 static void JNICALL
  71 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  72     if (!nsk_jvmti_waitForSync(timeout))
  73         return;
  74 
  75     if (VMInitEventsCount != VMInitEventsExpected) {
  76         NSK_COMPLAIN2("Wrong number of VM init events: %d, expected: %d\n",
  77             VMInitEventsCount, VMInitEventsExpected);
  78         nsk_jvmti_setFailStatus();
  79     }
  80 
  81     if (ThreadStartEventsCount == 0) {
  82         NSK_COMPLAIN0("No thread start events\n");
  83         nsk_jvmti_setFailStatus();
  84     }
  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_ma03t001(JavaVM *jvm, char *options, void *reserved) {
  95     return Agent_Initialize(jvm, options, reserved);
  96 }
  97 JNIEXPORT jint JNICALL Agent_OnAttach_ma03t001(JavaVM *jvm, char *options, void *reserved) {
  98     return Agent_Initialize(jvm, options, reserved);
  99 }
 100 JNIEXPORT jint JNI_OnLoad_ma03t001(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     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(&callbacks, 0, sizeof(callbacks));
 123     callbacks.VMInit = &VMInit;
 124     callbacks.ThreadStart = &ThreadStart;
 125     callbacks.VMDeath = &VMDeath;
 126     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 127         return JNI_ERR;
 128 
 129     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL)))
 130         return JNI_ERR;
 131 
 132     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))
 133         return JNI_ERR;
 134 
 135     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL)))
 136         return JNI_ERR;
 137 
 138     return JNI_OK;
 139 }
 140 
 141 /* ========================================================================== */
 142 
 143 /* agent library shutdown */
 144 JNIEXPORT void JNICALL
 145 #ifdef STATIC_BUILD
 146 Agent_OnUnload_ma03t001(JavaVM *jvm)
 147 #else
 148 Agent_OnUnload(JavaVM *jvm)
 149 #endif
 150 {
 151     if (VMDeathEventsCount != VMDeathEventsExpected) {
 152         NSK_COMPLAIN2("Wrong number of VM death events: %d, expected: %d\n",
 153             VMDeathEventsCount, VMDeathEventsExpected);
 154         exit(97);
 155     }
 156 }
 157 
 158 /* ========================================================================== */
 159 
 160 }