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