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