1 /*
  2  * Copyright (c) 2003, 2022, 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 <string.h>
 26 #include "jvmti.h"
 27 #include "jvmti_common.h"
 28 #include "../get_stack_trace.h"
 29 
 30 extern "C" {
 31 
 32 #define PASSED 0
 33 #define STATUS_FAILED 2
 34 
 35 static jvmtiEnv *jvmti = NULL;
 36 static jvmtiCapabilities caps;
 37 static jvmtiEventCallbacks callbacks;
 38 static jint result = PASSED;
 39 static jmethodID mid;
 40 static jbyteArray classBytes;
 41 static frame_info expected_platform_frames[] = {
 42     {"Lgetstacktr07$TestThread;", "checkPoint", "()V"},
 43     {"Lgetstacktr07$TestThread;", "chain4", "()V"},
 44     {"Lgetstacktr07$TestThread;", "chain3", "()V"},
 45     {"Lgetstacktr07$TestThread;", "chain2", "()V"},
 46     {"Lgetstacktr07$TestThread;", "chain1", "()V"},
 47     {"Lgetstacktr07$TestThread;", "run", "()V"},
 48     {"Ljava/lang/Thread;", "run", "()V"},
 49 };
 50 
 51 static frame_info expected_virtual_frames[] = {
 52     {"Lgetstacktr07$TestThread;", "checkPoint", "()V"},
 53     {"Lgetstacktr07$TestThread;", "chain4", "()V"},
 54     {"Lgetstacktr07$TestThread;", "chain3", "()V"},
 55     {"Lgetstacktr07$TestThread;", "chain2", "()V"},
 56     {"Lgetstacktr07$TestThread;", "chain1", "()V"},
 57     {"Lgetstacktr07$TestThread;", "run", "()V"},
 58     {"Ljava/lang/VirtualThread;", "run", "(Ljava/lang/Runnable;)V"},
 59     {"Ljava/lang/VirtualThread$VThreadContinuation;", "lambda$new$0", "(Ljava/lang/VirtualThread;Ljava/lang/Runnable;)V"},
 60     {"Ljava/lang/VirtualThread$VThreadContinuation$$Lambda;", "run", "()V"},
 61     {"Ljdk/internal/vm/Continuation;", "enter0", "()V"},
 62     {"Ljdk/internal/vm/Continuation;", "enter", "(Ljdk/internal/vm/Continuation;Z)V"},
 63 };
 64 
 65 
 66 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni, jthread thread, jmethodID method, jlocation location) {
 67   jclass klass;
 68   jvmtiClassDefinition class_def;
 69 
 70   if (mid != method) {
 71     jni->FatalError("ERROR: don't know where we get called from");
 72   }
 73 
 74   if (classBytes == NULL) {
 75     jni->FatalError("ERROR: don't have any bytes");
 76   }
 77 
 78   check_jvmti_status(jni, jvmti->GetMethodDeclaringClass(method, &klass), "GetMethodDeclaringClass failed.");
 79 
 80   LOG(">>> redefining class ...\n");
 81 
 82   class_def.klass = klass;
 83   class_def.class_byte_count = jni->GetArrayLength(classBytes);
 84   class_def.class_bytes = (unsigned char *) jni->GetByteArrayElements(classBytes, NULL);
 85 
 86   check_jvmti_status(jni, jvmti->RedefineClasses(1, &class_def), "RedefineClasses failed.");
 87   jni->DeleteGlobalRef(classBytes);
 88   classBytes = NULL;
 89 
 90   frame_info *expected_frames = jni->IsVirtualThread(thread)
 91       ? expected_virtual_frames
 92       : expected_platform_frames;
 93   int expected_number_of_stack_frames = jni->IsVirtualThread(thread)
 94       ? ((int) (sizeof(expected_virtual_frames) / sizeof(frame_info)))
 95       : ((int) (sizeof(expected_platform_frames) / sizeof(frame_info)));
 96 
 97   if (!compare_stack_trace(jvmti_env, jni, thread, expected_frames, expected_number_of_stack_frames)) {
 98     jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
 99   }
100 }
101 
102 JNIEXPORT jint JNICALL
103 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
104   jvmtiError err;
105   jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
106   if (res != JNI_OK || jvmti == NULL) {
107     LOG("Wrong result of a valid call to GetEnv!\n");
108     return JNI_ERR;
109   }
110 
111   jvmtiCapabilities caps;
112   memset(&caps, 0, sizeof(caps));
113   caps.can_generate_breakpoint_events = 1;
114   caps.can_generate_single_step_events = 1;
115   caps.can_redefine_classes = 1;
116 
117   err = jvmti->AddCapabilities(&caps);
118   if (err != JVMTI_ERROR_NONE) {
119    LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err);
120     return JNI_ERR;
121   }
122 
123   callbacks.Breakpoint = &Breakpoint;
124   err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
125   if (err != JVMTI_ERROR_NONE) {
126     LOG("(SetEventCallbacks) unexpected error: %s (%d)\n", TranslateError(err), err);
127     return JNI_ERR;
128   }
129 
130   return JNI_OK;
131 }
132 
133 JNIEXPORT void JNICALL
134 Java_getstacktr07_getReady(JNIEnv *jni, jclass cls, jclass clazz, jbyteArray bytes) {
135   classBytes = (jbyteArray) jni->NewGlobalRef(bytes);
136 
137   mid = jni->GetMethodID(clazz, "checkPoint", "()V");
138   if (mid == NULL) {
139     jni->FatalError("Cannot find Method ID for method checkPoint\n");
140   }
141 
142   check_jvmti_status(jni, jvmti->SetBreakpoint(mid, 0), "SetBreakpoint failed.");
143   set_event_notification_mode(jvmti, jni, JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL);
144 
145 }
146 
147 }