1 /*
   2  * Copyright (c) 2003, 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 <stdio.h>
  25 #include <stdarg.h>
  26 #include <stdlib.h>
  27 #include <string.h>
  28 
  29 #include <jvmti.h>
  30 #include "agent_common.h"
  31 
  32 #include "nsk_tools.h"
  33 #include "JVMTITools.h"
  34 #include "jvmti_tools.h"
  35 
  36 extern "C" {
  37 
  38 #define PASSED  0
  39 #define STATUS_FAILED  2
  40 
  41 static jvmtiEnv *jvmti = NULL;
  42 static jvmtiEventCallbacks callbacks;
  43 
  44 /** callback functions **/
  45 void JNICALL
  46 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {
  47     jvmtiPhase phase;
  48 
  49     NSK_DISPLAY0("CHECK PASSED: VMDeath event received\n");
  50 
  51     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase,
  52             jvmti, &phase)))
  53         exit(95 + STATUS_FAILED);
  54 
  55     if (phase != JVMTI_PHASE_LIVE) {
  56         NSK_COMPLAIN1("TEST FAILED: VMDeath event received during non-live phase %s\n",
  57             TranslatePhase(phase));
  58         exit(95 + STATUS_FAILED);
  59     }
  60     else
  61         NSK_DISPLAY0("CHECK PASSED: VMDeath event received during the live phase as expected\n");
  62 
  63     exit(95 + PASSED);
  64 }
  65 /************************/
  66 
  67 #ifdef STATIC_BUILD
  68 JNIEXPORT jint JNICALL Agent_OnLoad_vmdeath001(JavaVM *jvm, char *options, void *reserved) {
  69     return Agent_Initialize(jvm, options, reserved);
  70 }
  71 JNIEXPORT jint JNICALL Agent_OnAttach_vmdeath001(JavaVM *jvm, char *options, void *reserved) {
  72     return Agent_Initialize(jvm, options, reserved);
  73 }
  74 JNIEXPORT jint JNI_OnLoad_vmdeath001(JavaVM *jvm, char *options, void *reserved) {
  75     return JNI_VERSION_1_8;
  76 }
  77 #endif
  78 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  79     /* init framework and parse options */
  80     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
  81         return JNI_ERR;
  82 
  83     /* create JVMTI environment */
  84     if (!NSK_VERIFY((jvmti =
  85             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
  86         return JNI_ERR;
  87 
  88     NSK_DISPLAY0("setting event callbacks ...\n");
  89     (void) memset(&callbacks, 0, sizeof(callbacks));
  90     callbacks.VMDeath = &VMDeath;
  91     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks,
  92             jvmti, &callbacks, sizeof(callbacks))))
  93         return JNI_ERR;
  94 
  95     NSK_DISPLAY0("setting event callbacks done\nenabling VMDeath event ...\n");
  96     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
  97             jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))
  98         return JNI_ERR;
  99     NSK_DISPLAY0("enabling VMDeath event done\n");
 100 
 101     return JNI_OK;
 102 }
 103 
 104 }