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(jvmti->GetPhase(&phase)))
  52         exit(95 + STATUS_FAILED);
  53 
  54     if (phase != JVMTI_PHASE_LIVE) {
  55         NSK_COMPLAIN1("TEST FAILED: VMDeath event received during non-live phase %s\n",
  56             TranslatePhase(phase));
  57         exit(95 + STATUS_FAILED);
  58     }
  59     else
  60         NSK_DISPLAY0("CHECK PASSED: VMDeath event received during the live phase as expected\n");
  61 
  62     exit(95 + PASSED);
  63 }
  64 /************************/
  65 
  66 #ifdef STATIC_BUILD
  67 JNIEXPORT jint JNICALL Agent_OnLoad_vmdeath001(JavaVM *jvm, char *options, void *reserved) {
  68     return Agent_Initialize(jvm, options, reserved);
  69 }
  70 JNIEXPORT jint JNICALL Agent_OnAttach_vmdeath001(JavaVM *jvm, char *options, void *reserved) {
  71     return Agent_Initialize(jvm, options, reserved);
  72 }
  73 JNIEXPORT jint JNI_OnLoad_vmdeath001(JavaVM *jvm, char *options, void *reserved) {
  74     return JNI_VERSION_1_8;
  75 }
  76 #endif
  77 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  78     /* init framework and parse options */
  79     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
  80         return JNI_ERR;
  81 
  82     /* create JVMTI environment */
  83     if (!NSK_VERIFY((jvmti =
  84             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
  85         return JNI_ERR;
  86 
  87     NSK_DISPLAY0("setting event callbacks ...\n");
  88     (void) memset(&callbacks, 0, sizeof(callbacks));
  89     callbacks.VMDeath = &VMDeath;
  90     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
  91         return JNI_ERR;
  92 
  93     NSK_DISPLAY0("setting event callbacks done\nenabling VMDeath event ...\n");
  94     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))
  95         return JNI_ERR;
  96     NSK_DISPLAY0("enabling VMDeath event done\n");
  97 
  98     return JNI_OK;
  99 }
 100 
 101 }