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 <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 /* ============================================================================= */
  33 
  34 static jlong timeout = 0;
  35 
  36 /* ============================================================================= */
  37 
  38 static void JNICALL
  39 callbackExtensionEvent(jvmtiEnv* jvmti, ... ) {
  40     NSK_DISPLAY0("    event: callbackExtensionEvent\n");
  41 }
  42 
  43 /** Check extension events. */
  44 static int checkExtensions(jvmtiEnv* jvmti, const char phase[]) {
  45     int success = NSK_TRUE;
  46     jint extCount = 0;
  47     jvmtiExtensionEventInfo* extList = NULL;
  48     int i;
  49 
  50     NSK_DISPLAY0("Get extension events list\n");
  51     if (!NSK_JVMTI_VERIFY(
  52             NSK_CPP_STUB3(GetExtensionEvents, jvmti, &extCount, &extList))) {
  53         return NSK_FALSE;
  54     }
  55     NSK_DISPLAY1("  ... got count: %d\n", (int)extCount);
  56     NSK_DISPLAY1("  ... got list:  0x%p\n", (void*)extList);
  57 
  58     if (extCount <= 0) {
  59         NSK_DISPLAY1("# WARNING: No extension events implemented to check: %d\n", extCount);
  60     } else {
  61 
  62         if (!NSK_VERIFY(extList != NULL))
  63             return NSK_FALSE;
  64 
  65         NSK_DISPLAY1("Set/clear callback for each extension event: %d events\n", (int)extCount);
  66         for (i = 0; i < extCount; i++) {
  67             NSK_DISPLAY1("  event #%d:\n", i);
  68             NSK_DISPLAY1("    event_index: %d\n", (int)extList[i].extension_event_index);
  69             NSK_DISPLAY1("    id:          \"%s\"\n", nsk_null_string(extList[i].id));
  70             NSK_DISPLAY1("    short_desc:  \"%s\"\n", nsk_null_string(extList[i].short_description));
  71             NSK_DISPLAY1("    param_count: %d\n", (int)extList[i].param_count);
  72 
  73             NSK_DISPLAY1("    ... setting callback: 0x%p\n", (void*)callbackExtensionEvent);
  74             if (!NSK_JVMTI_VERIFY(
  75                     NSK_CPP_STUB3(SetExtensionEventCallback, jvmti,
  76                                         extList[i].extension_event_index,
  77                                         callbackExtensionEvent))) {
  78                 success = NSK_FALSE;
  79             }
  80             NSK_DISPLAY0("    ... done\n");
  81 
  82             NSK_DISPLAY1("    ... clearing callback: 0x%p\n", (void*)NULL);
  83             if (!NSK_JVMTI_VERIFY(
  84                     NSK_CPP_STUB3(SetExtensionEventCallback, jvmti,
  85                                         extList[i].extension_event_index,
  86                                         NULL))) {
  87                 success = NSK_FALSE;
  88             }
  89             NSK_DISPLAY0("    ... done\n");
  90         }
  91     }
  92 
  93     NSK_DISPLAY1("Deallocate extension events list: 0x%p\n", (void*)extList);
  94     if (!NSK_JVMTI_VERIFY(
  95             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)extList))) {
  96         return NSK_FALSE;
  97     }
  98     NSK_DISPLAY0("  ... deallocated\n");
  99 
 100     return success;
 101 }
 102 
 103 /* ============================================================================= */
 104 
 105 /** Agent algorithm. */
 106 static void JNICALL
 107 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 108     NSK_DISPLAY0("Wait for debugee class ready\n");
 109     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 110         return;
 111 
 112     NSK_DISPLAY0(">>> Testcase #2: Check setting extension event callbacks in live phase\n");
 113     {
 114         if (!checkExtensions(jvmti, "live")) {
 115             nsk_jvmti_setFailStatus();
 116         }
 117     }
 118 
 119     NSK_DISPLAY0("Let debugee to finish\n");
 120     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 121         return;
 122 }
 123 
 124 /* ============================================================================= */
 125 
 126 /** Agent library initialization. */
 127 #ifdef STATIC_BUILD
 128 JNIEXPORT jint JNICALL Agent_OnLoad_setextevent001(JavaVM *jvm, char *options, void *reserved) {
 129     return Agent_Initialize(jvm, options, reserved);
 130 }
 131 JNIEXPORT jint JNICALL Agent_OnAttach_setextevent001(JavaVM *jvm, char *options, void *reserved) {
 132     return Agent_Initialize(jvm, options, reserved);
 133 }
 134 JNIEXPORT jint JNI_OnLoad_setextevent001(JavaVM *jvm, char *options, void *reserved) {
 135     return JNI_VERSION_1_8;
 136 }
 137 #endif
 138 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 139     jvmtiEnv* jvmti = NULL;
 140 
 141     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 142         return JNI_ERR;
 143 
 144     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 145 
 146     if (!NSK_VERIFY((jvmti =
 147             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 148         return JNI_ERR;
 149 
 150     NSK_DISPLAY0(">>> Testcase #1: Check setting extension event callbacks in OnLoad phase\n");
 151     {
 152         if (!checkExtensions(jvmti, "OnLoad")) {
 153             nsk_jvmti_setFailStatus();
 154         }
 155     }
 156 
 157     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 158         return JNI_ERR;
 159 
 160     return JNI_OK;
 161 }
 162 
 163 /* ============================================================================= */
 164 
 165 }