1 /*
   2  * Copyright (c) 2004, 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 <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 
  33 extern "C" {
  34 
  35 /* ========================================================================== */
  36 
  37 /* scaffold objects */
  38 static jlong timeout = 0;
  39 
  40 /* test objects */
  41 static jthread thread = NULL;
  42 static jmethodID method = NULL;
  43 static int BreakpointEventsCount = 0;
  44 static int FramePopEventsCount = 0;
  45 
  46 /* ========================================================================== */
  47 
  48 /** callback functions **/
  49 
  50 static void JNICALL
  51 Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
  52         jthread thread, jmethodID method, jlocation location) {
  53     char *name = NULL;
  54     char *signature = NULL;
  55 
  56     BreakpointEventsCount++;
  57     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
  58         nsk_jvmti_setFailStatus();
  59     }
  60 
  61     NSK_DISPLAY2("Breakpoint event: %s%s\n", name, signature);
  62 
  63     if (name != NULL)
  64         jvmti_env->Deallocate((unsigned char*)name);
  65     if (signature != NULL)
  66         jvmti_env->Deallocate((unsigned char*)signature);
  67 
  68     switch(BreakpointEventsCount) {
  69     case 1:
  70         NSK_DISPLAY0("Testcase #1: FramePop in both agents\n");
  71         break;
  72 
  73     case 2:
  74         NSK_DISPLAY0("Testcase #2: w/o NotifyFramePop in 2nd agent\n");
  75         break;
  76 
  77     case 3:
  78         NSK_DISPLAY0("Testcase #3: FramePop disabled in 2nd agent\n");
  79         break;
  80 
  81     default:
  82         NSK_COMPLAIN0("Should no reach here");
  83         nsk_jvmti_setFailStatus();
  84         break;
  85     }
  86 
  87     if (!NSK_JVMTI_VERIFY(jvmti_env->NotifyFramePop(thread, 0))) {
  88         nsk_jvmti_setFailStatus();
  89     }
  90 }
  91 
  92 static void JNICALL
  93 FramePop(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
  94         jthread thread, jmethodID method,
  95         jboolean wasPopedByException) {
  96     char *name = NULL;
  97     char *signature = NULL;
  98 
  99     FramePopEventsCount++;
 100     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
 101         nsk_jvmti_setFailStatus();
 102         return;
 103     }
 104 
 105     NSK_DISPLAY2("FramePop event: %s%s\n", name, signature);
 106 
 107     if (name != NULL)
 108         jvmti_env->Deallocate((unsigned char*)name);
 109     if (signature != NULL)
 110         jvmti_env->Deallocate((unsigned char*)signature);
 111 }
 112 
 113 /* ========================================================================== */
 114 
 115 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
 116     const char* THREAD_NAME = "Debuggee Thread";
 117     jvmtiThreadInfo info;
 118     jthread *threads = NULL;
 119     jclass klass = NULL;
 120     jint threads_count = 0;
 121     int i;
 122 
 123     NSK_DISPLAY0("Prepare: find tested thread\n");
 124 
 125     /* get all live threads */
 126     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))
 127         return NSK_FALSE;
 128 
 129     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
 130         return NSK_FALSE;
 131 
 132     /* find tested thread */
 133     for (i = 0; i < threads_count; i++) {
 134         if (!NSK_VERIFY(threads[i] != NULL))
 135             return NSK_FALSE;
 136 
 137         /* get thread information */
 138         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
 139             return NSK_FALSE;
 140 
 141         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
 142 
 143         /* find by name */
 144         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
 145             thread = threads[i];
 146         }
 147 
 148         if (info.name != NULL) {
 149             if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)info.name)))
 150                 return NSK_FALSE;
 151         }
 152     }
 153 
 154     /* deallocate threads list */
 155     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
 156         return NSK_FALSE;
 157 
 158     if (thread == NULL) {
 159         NSK_COMPLAIN0("Debuggee thread not found");
 160         return NSK_FALSE;
 161     }
 162 
 163     if (!NSK_JNI_VERIFY(jni, (thread = jni->NewGlobalRef(thread)) != NULL))
 164         return NSK_FALSE;
 165 
 166     /* get tested thread class */
 167     if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
 168         return NSK_FALSE;
 169 
 170     /* get tested thread method 'checkPoint' */
 171     if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "checkPoint", "()V")) != NULL))
 172         return NSK_FALSE;
 173 
 174     if (!NSK_JVMTI_VERIFY(jvmti->SetBreakpoint(method, 0)))
 175         return NSK_FALSE;
 176 
 177     /* enable events */
 178     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
 179         return NSK_FALSE;
 180     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL)))
 181         return NSK_FALSE;
 182 
 183     return NSK_TRUE;
 184 }
 185 
 186 /* ========================================================================== */
 187 
 188 /** Agent algorithm. */
 189 static void JNICALL
 190 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 191 
 192     if (!nsk_jvmti_waitForSync(timeout))
 193         return;
 194 
 195     if (!prepare(jvmti, jni)) {
 196         nsk_jvmti_setFailStatus();
 197         return;
 198     }
 199 
 200     /* resume debugee and wait for sync */
 201     if (!nsk_jvmti_resumeSync())
 202         return;
 203     if (!nsk_jvmti_waitForSync(timeout))
 204         return;
 205 
 206     if (FramePopEventsCount == 0) {
 207         NSK_COMPLAIN0("No FramePop events\n");
 208         nsk_jvmti_setFailStatus();
 209     } else if (FramePopEventsCount != 3) {
 210         NSK_COMPLAIN1("Some of FramePop events were missed: %d\n",
 211             FramePopEventsCount);
 212         nsk_jvmti_setFailStatus();
 213     }
 214 
 215     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_FRAME_POP, NULL)))
 216         nsk_jvmti_setFailStatus();
 217 
 218     NSK_TRACE(jvmti->ClearBreakpoint(method, 0));
 219     NSK_TRACE(jni->DeleteGlobalRef(thread));
 220 
 221     if (!nsk_jvmti_resumeSync())
 222         return;
 223 }
 224 
 225 /* ========================================================================== */
 226 
 227 /** Agent library initialization. */
 228 #ifdef STATIC_BUILD
 229 JNIEXPORT jint JNICALL Agent_OnLoad_ma05t001(JavaVM *jvm, char *options, void *reserved) {
 230     return Agent_Initialize(jvm, options, reserved);
 231 }
 232 JNIEXPORT jint JNICALL Agent_OnAttach_ma05t001(JavaVM *jvm, char *options, void *reserved) {
 233     return Agent_Initialize(jvm, options, reserved);
 234 }
 235 JNIEXPORT jint JNI_OnLoad_ma05t001(JavaVM *jvm, char *options, void *reserved) {
 236     return JNI_VERSION_1_8;
 237 }
 238 #endif
 239 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 240     jvmtiEnv* jvmti = NULL;
 241     jvmtiEventCallbacks callbacks;
 242     jvmtiCapabilities caps;
 243 
 244     NSK_DISPLAY0("Agent_OnLoad\n");
 245 
 246     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 247         return JNI_ERR;
 248 
 249     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 250 
 251     if (!NSK_VERIFY((jvmti =
 252             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 253         return JNI_ERR;
 254 
 255     memset(&caps, 0, sizeof(caps));
 256     caps.can_generate_breakpoint_events = 1;
 257     caps.can_generate_frame_pop_events = 1;
 258     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 259         return JNI_ERR;
 260 
 261     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 262         return JNI_ERR;
 263 
 264     memset(&callbacks, 0, sizeof(callbacks));
 265     callbacks.Breakpoint = &Breakpoint;
 266     callbacks.FramePop = &FramePop;
 267     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 268         return JNI_ERR;
 269 
 270     return JNI_OK;
 271 }
 272 
 273 /* ========================================================================== */
 274 
 275 }