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