Skip to content

Commit 035eaee

Browse files
author
Serguei Spitsyn
committed
8296324: JVMTI GetStackTrace truncates vthread stack trace for agents loaded into running VM
Reviewed-by: cjplummer, lmesnik
1 parent 59a308b commit 035eaee

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

src/hotspot/share/prims/jvmtiExport.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
383383
if (Continuations::enabled()) {
384384
// Virtual threads support. There is a performance impact when VTMS transitions are enabled.
385385
java_lang_VirtualThread::set_notify_jvmti_events(true);
386+
if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) {
387+
ThreadInVMfromNative __tiv(JavaThread::current());
388+
java_lang_VirtualThread::init_static_notify_jvmti_events();
389+
}
386390
}
387391

388392
if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 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+
/**
25+
* @test
26+
* @summary Verifies JVMTI GetStackTrace does not truncate virtual thread stack trace with agent attach
27+
* @requires vm.jvmti
28+
* @requires vm.continuations
29+
* @enablePreview
30+
* @run main/othervm/native -Djdk.attach.allowAttachSelf=true VirtualStackTraceTest
31+
*/
32+
33+
import com.sun.tools.attach.VirtualMachine;
34+
35+
import java.util.Arrays;
36+
import java.util.List;
37+
import java.util.Objects;
38+
39+
public class VirtualStackTraceTest {
40+
private static final String AGENT_LIB = "VirtualStackTraceTest";
41+
42+
public static native String[] getStackTrace();
43+
44+
public static void main(String[] args) throws Exception {
45+
VirtualMachine vm = VirtualMachine.attach(String.valueOf(ProcessHandle.current().pid()));
46+
vm.loadAgentLibrary(AGENT_LIB);
47+
VirtualStackTraceTest t = new VirtualStackTraceTest();
48+
t.runTest();
49+
}
50+
51+
void runTest() throws Exception {
52+
Thread thr = Thread.ofVirtual().name("VT").start(VirtualStackTraceTest::test);
53+
thr.join();
54+
}
55+
56+
private static void test() {
57+
work();
58+
}
59+
60+
private static void work() {
61+
inner();
62+
}
63+
64+
private static void inner() {
65+
checkCurrentThread();
66+
}
67+
68+
private static void checkCurrentThread() {
69+
System.out.println("Stack trace for " + Thread.currentThread() + ": ");
70+
var javaStackTrace = Arrays.stream(Thread.currentThread().getStackTrace()).map(StackTraceElement::getMethodName).toList();
71+
var jvmtiStackTrace = List.of(getStackTrace());
72+
73+
System.out.println("JVMTI: " + jvmtiStackTrace);
74+
System.out.println("Java : " + javaStackTrace);
75+
76+
if (!Objects.equals(jvmtiStackTrace, javaStackTrace)) {
77+
throw new RuntimeException("VirtualStackTraceTest failed: stack traces do not match");
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 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 <cstdlib>
25+
#include <cstring>
26+
#include <jvmti.h>
27+
#include "jvmti_common.h"
28+
29+
extern "C" {
30+
31+
const int MAX_COUNT = 50;
32+
static jvmtiEnv *jvmti;
33+
34+
JNIEXPORT jobjectArray JNICALL
35+
Java_VirtualStackTraceTest_getStackTrace(JNIEnv* jni, jclass clazz) {
36+
jvmtiError err;
37+
jint count = 0;
38+
jint skipped = 0;
39+
40+
jobject visibleFrames[MAX_COUNT];
41+
jvmtiFrameInfo frameInfo[MAX_COUNT];
42+
43+
err = jvmti->GetStackTrace(NULL, 0, MAX_COUNT, frameInfo, &count);
44+
check_jvmti_status(jni, err, "event handler: error in JVMTI GetStackTrace call");
45+
46+
for (int idx = 0; idx < count; idx++) {
47+
jclass declaringClass = NULL;
48+
char *clasSignature = NULL;
49+
char *methodName = NULL;
50+
51+
err = jvmti->GetMethodDeclaringClass(frameInfo[idx].method, &declaringClass);
52+
check_jvmti_status(jni, err, "event handler: error in JVMTI GetMethodDeclaringClass call");
53+
54+
err = jvmti->GetClassSignature(declaringClass, &clasSignature, NULL);
55+
check_jvmti_status(jni, err, "event handler: error in JVMTI GetClassSignature call");
56+
57+
err = jvmti->GetMethodName(frameInfo[idx].method, &methodName, NULL, NULL);
58+
check_jvmti_status(jni, err, "event handler: error in JVMTI GetMethodName call");
59+
60+
if (strchr(clasSignature, '.')) {
61+
skipped++;
62+
continue;
63+
}
64+
visibleFrames[idx - skipped] = jni->NewStringUTF(methodName);
65+
66+
jvmti->Deallocate(reinterpret_cast<unsigned char*>(methodName));
67+
jvmti->Deallocate(reinterpret_cast<unsigned char*>(clasSignature));
68+
}
69+
jobjectArray methodNames = jni->NewObjectArray(count - skipped, jni->FindClass("java/lang/String"), NULL);
70+
for (int idx = 0; idx < count - skipped; idx++) {
71+
jni->SetObjectArrayElement(methodNames, idx, visibleFrames[idx]);
72+
}
73+
print_stack_trace(jvmti, jni, NULL);
74+
75+
return methodNames;
76+
}
77+
78+
JNIEXPORT jint JNICALL
79+
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
80+
LOG("Agent_OnLoad started\n");
81+
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
82+
return JNI_ERR;
83+
}
84+
return JNI_OK;
85+
}
86+
87+
JNIEXPORT jint JNICALL
88+
Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
89+
LOG("Agent_OnAttach started\n");
90+
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
91+
return JNI_ERR;
92+
}
93+
return JNI_OK;
94+
}
95+
96+
} // extern "C"

0 commit comments

Comments
 (0)