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