1 /* 2 * Copyright (c) 2003, 2023, 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 frame_info expected_platform_frames[] = { 34 {"Ljava/lang/Object;", "wait", "()V"}, 35 {"Lgetstacktr03;", "dummy", "()V"}, 36 {"Lgetstacktr03;", "chain", "()V"}, 37 {"Lgetstacktr03$Task;", "run", "()V"}, 38 {"Ljava/lang/Thread;", "runWith", "(Ljava/lang/Object;Ljava/lang/Runnable;)V"}, 39 {"Ljava/lang/Thread;", "run", "()V"}, 40 }; 41 42 static frame_info expected_virtual_frames[] = { 43 {"Ljava/lang/Object;", "wait", "()V"}, 44 {"Lgetstacktr03;", "dummy", "()V"}, 45 {"Lgetstacktr03;", "chain", "()V"}, 46 {"Lgetstacktr03$Task;", "run", "()V"}, 47 {"Ljava/lang/VirtualThread;", "runWith", "(Ljava/lang/Object;Ljava/lang/Runnable;)V"}, 48 {"Ljava/lang/VirtualThread;", "run", "(Ljava/lang/Runnable;)V"}, 49 {"Ljava/lang/VirtualThread$VThreadContinuation;", "lambda$new$0", "(Ljava/lang/VirtualThread;Ljava/lang/Runnable;)V"}, 50 {"Ljava/lang/VirtualThread$VThreadContinuation$$Lambda.0x0000000800098810;", "run", "()V"}, 51 {"Ljdk/internal/vm/Continuation;", "enter0", "()V"}, 52 {"Ljdk/internal/vm/Continuation;", "enter", "(Ljdk/internal/vm/Continuation;Z)V"} 53 }; 54 55 JNIEXPORT jint JNICALL 56 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { 57 jvmtiError err; 58 jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); 59 if (res != JNI_OK || jvmti == NULL) { 60 LOG("Wrong result of a valid call to GetEnv!\n"); 61 return JNI_ERR; 62 } 63 64 jvmtiCapabilities caps; 65 memset(&caps, 0, sizeof(caps)); 66 caps.can_suspend = 1; 67 68 err = jvmti->AddCapabilities(&caps); 69 if (err != JVMTI_ERROR_NONE) { 70 LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err); 71 return JNI_ERR; 72 } 73 74 return JNI_OK; 75 } 76 77 JNIEXPORT void JNICALL 78 Java_getstacktr03_chain(JNIEnv *env, jclass cls) { 79 jmethodID mid = env->GetStaticMethodID(cls, "dummy", "()V"); 80 env->CallStaticVoidMethod(cls, mid); 81 } 82 83 JNIEXPORT int JNICALL 84 Java_getstacktr03_check(JNIEnv *jni, jclass cls, jthread thread) { 85 suspend_thread(jvmti, jni, thread); 86 frame_info *expected_frames = jni->IsVirtualThread(thread) 87 ? expected_virtual_frames 88 : expected_platform_frames; 89 int expected_number_of_stack_frames = jni->IsVirtualThread(thread) 90 ? ((int) (sizeof(expected_virtual_frames) / sizeof(frame_info))) 91 : ((int) (sizeof(expected_platform_frames) / sizeof(frame_info))); 92 93 if (!compare_stack_trace(jvmti, jni, thread, expected_frames, expected_number_of_stack_frames)) { 94 jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected."); 95 } 96 resume_thread(jvmti, jni, thread); 97 return 0; 98 } 99 100 }