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 static jvmtiEnv *jvmti = NULL;
34 static frame_info expected_virtual_frames[] = {
35     {"LGetStackTraceCurrentThreadTest;", "check", "(Ljava/lang/Thread;)V"},
36     {"LGetStackTraceCurrentThreadTest;", "dummy", "()V"},
37     {"LGetStackTraceCurrentThreadTest;", "chain", "()V"},
38     {"LTask;", "run", "()V"},
39     {"Ljava/lang/VirtualThread;", "run", "(Ljava/lang/Runnable;)V"}
40 };
41 
42 static frame_info expected_platform_frames[] = {
43     {"LGetStackTraceCurrentThreadTest;", "check", "(Ljava/lang/Thread;)V"},
44     {"LGetStackTraceCurrentThreadTest;", "dummy", "()V"},
45     {"LGetStackTraceCurrentThreadTest;", "chain", "()V"},
46     {"LTask;", "run", "()V"},
47     {"Ljava/lang/Thread;", "run", "()V"}
48 };
49 
50 JNIEXPORT jint JNICALL
51 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
52   jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
53   if (res != JNI_OK || jvmti == NULL) {
54     printf("Wrong result of a valid call to GetEnv!\n");
55     return JNI_ERR;
56   }
57   return JNI_OK;
58 }
59 
60 JNIEXPORT void JNICALL
61 Java_GetStackTraceCurrentThreadTest_chain(JNIEnv *env, jclass cls) {
62   jmethodID mid = env->GetStaticMethodID(cls, "dummy", "()V");
63   env->CallStaticVoidMethod(cls, mid);
64 }
65 
66 JNIEXPORT void JNICALL
67 Java_GetStackTraceCurrentThreadTest_check(JNIEnv *jni, jclass cls, jthread thread) {
68 
69   frame_info *expected_frames = jni->IsVirtualThread(thread)
70       ? expected_virtual_frames
71       : expected_platform_frames;
72   int expected_number_of_stack_frames = jni->IsVirtualThread(thread)
73       ? ((int) (sizeof(expected_virtual_frames) / sizeof(frame_info)))
74       : ((int) (sizeof(expected_platform_frames) / sizeof(frame_info)));
75 
76   if (!compare_stack_trace(jvmti, jni, thread, expected_frames, expected_number_of_stack_frames)) {
77     jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), "Stacktrace differs from expected.");
78   }
79 }
80 }