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 <stdlib.h>
  26 #include <string.h>
  27 #include <jvmti.h>
  28 #include "agent_common.h"
  29 
  30 #include "nsk_tools.h"
  31 #include "JVMTITools.h"
  32 #include "jvmti_tools.h"
  33 #include "jni_tools.h"
  34 
  35 extern "C" {
  36 
  37 #define STATUS_FAILED 2
  38 #define PASSED 0
  39 
  40 static jint result = STATUS_FAILED;
  41 static jvmtiEnv *jvmti = NULL;
  42 static jvmtiEventCallbacks callbacks;
  43 static jboolean eventReceived = JNI_FALSE;
  44 
  45 static jrawMonitorID dataDumpRequestMonitor;
  46 
  47 /** callback functions **/
  48 static void JNICALL DataDumpRequest(jvmtiEnv *env) {
  49     jvmtiPhase phase;
  50 
  51     rawMonitorEnter(env, dataDumpRequestMonitor);
  52 
  53     NSK_DISPLAY0(">>>> DataDumpRequest event received\n");
  54     eventReceived = JNI_TRUE;
  55 
  56     getPhase(jvmti, &phase);
  57 
  58     if (phase != JVMTI_PHASE_LIVE) {
  59         result = STATUS_FAILED;
  60         NSK_COMPLAIN1("TEST FAILED: DataDumpRequest event received during non-live phase %s\n",
  61                       TranslatePhase(phase));
  62     } else {
  63         result = PASSED;
  64         NSK_DISPLAY0("CHECK PASSED: DataDumpRequest event received during the live phase as expected\n");
  65     }
  66 
  67     NSK_DISPLAY0("<<<<\n\n");
  68 
  69     rawMonitorNotify(env, dataDumpRequestMonitor);
  70     rawMonitorExit(env, dataDumpRequestMonitor);
  71 }
  72 
  73 static void waitDumpRequestReceived(jvmtiEnv *env) {
  74     rawMonitorEnter(env, dataDumpRequestMonitor);
  75 
  76     while (eventReceived == JNI_FALSE) {
  77         NSK_DISPLAY0("waiting for DataDumpRequest event...\n");
  78         rawMonitorWait(env, dataDumpRequestMonitor, 0);
  79     }
  80 
  81     rawMonitorExit(env, dataDumpRequestMonitor);
  82 }
  83 
  84 JNIEXPORT jint JNICALL
  85 Java_nsk_jvmti_DataDumpRequest_datadumpreq001_waitForResult(JNIEnv *env, jclass cls) {
  86     waitDumpRequestReceived(jvmti);
  87     return result;
  88 }
  89 
  90 #ifdef STATIC_BUILD
  91 JNIEXPORT jint JNICALL Agent_OnLoad_datadumpreq001(JavaVM *jvm, char *options, void *reserved) {
  92     return Agent_Initialize(jvm, options, reserved);
  93 }
  94 JNIEXPORT jint JNICALL Agent_OnAttach_datadumpreq001(JavaVM *jvm, char *options, void *reserved) {
  95     return Agent_Initialize(jvm, options, reserved);
  96 }
  97 JNIEXPORT jint JNI_OnLoad_datadumpreq001(JavaVM *jvm, char *options, void *reserved) {
  98     return JNI_VERSION_1_8;
  99 }
 100 #endif
 101 
 102 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 103     /* init framework and parse options */
 104     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 105         return JNI_ERR;
 106 
 107     /* create JVMTI environment */
 108     jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved);
 109     if (!NSK_VERIFY(jvmti != NULL)) {
 110         return JNI_ERR;
 111     }
 112 
 113     if (createRawMonitor(jvmti, "data dump request monitor", &dataDumpRequestMonitor) != JNI_OK) {
 114         return JNI_ERR;
 115     }
 116 
 117     /* set event callbacks */
 118     NSK_DISPLAY0("setting event callbacks ...\n");
 119     (void) memset(&callbacks, 0, sizeof(callbacks));
 120     callbacks.DataDumpRequest = &DataDumpRequest;
 121     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks,
 122             jvmti, &callbacks, sizeof(callbacks))))
 123         return JNI_ERR;
 124 
 125     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
 126     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 127             jvmti, JVMTI_ENABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, NULL)))
 128         return JNI_ERR;
 129     NSK_DISPLAY0("enabling the events done\n\n");
 130 
 131     return JNI_OK;
 132 }
 133 
 134 }