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