Skip to content

Commit

Permalink
8304839: Move TestScaffold.main() to the separate class DebugeeWrapper
Browse files Browse the repository at this point in the history
Backport-of: ee9776f
  • Loading branch information
GoeLin committed Apr 4, 2024
1 parent 74dcc30 commit 702d337
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 121 deletions.
6 changes: 3 additions & 3 deletions test/jdk/com/sun/jdi/ClassesByName2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public static void main(String[] args){
System.out.println("Howdy!");
try {

Thread zero = TestScaffold.newThread (() -> {
Thread zero = DebuggeeWrapper.newThread (() -> {
System.setProperty("java.awt.headless", "true");
java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
}, "ZERO");

Thread one = TestScaffold.newThread (() -> {
Thread one = DebuggeeWrapper.newThread (() -> {
try {
java.security.KeyPairGenerator keyGen =
java.security.KeyPairGenerator.getInstance("DSA", "SUN");
Expand All @@ -64,7 +64,7 @@ public static void main(String[] args){
}
}, "ONE");

Thread two = TestScaffold.newThread (() -> {
Thread two = DebuggeeWrapper.newThread (() -> {
try {
String s = String.format("%02x", 0xff);
} catch (Exception e) {
Expand Down
123 changes: 123 additions & 0 deletions test/jdk/com/sun/jdi/DebuggeeWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ThreadFactory;

public class DebuggeeWrapper {

public static String PROPERTY_NAME = "main.wrapper";

private static final String OLD_MAIN_THREAD_NAME = "old-m-a-i-n";

private static ThreadFactory threadFactory = r -> new Thread(r);

private static final String wrapperName = System.getProperty(PROPERTY_NAME);

public static String getWrapperName() {
return wrapperName;
}

public static boolean isVirtual() {
return "Virtual".equals(wrapperName);
}

public static Thread newThread(Runnable task) {
return threadFactory.newThread(task);
}

public static Thread newThread(Runnable task, String name) {
Thread t = newThread(task);
t.setName(name);
return t;
}

public static void main(String[] args) throws Throwable {
String className = args[0];
String[] classArgs = new String[args.length - 1];
System.arraycopy(args, 1, classArgs, 0, args.length - 1);
Class c = Class.forName(className);
java.lang.reflect.Method mainMethod = c.getMethod("main", new Class[] { String[].class });
mainMethod.setAccessible(true);

if (isVirtual()) {
threadFactory = Thread.ofVirtual().factory();
MainThreadGroup tg = new MainThreadGroup();
Thread vthread = Thread.ofVirtual().unstarted(() -> {
try {
mainMethod.invoke(null, new Object[] { classArgs });
} catch (InvocationTargetException e) {
tg.uncaughtThrowable = e.getCause();
} catch (Throwable error) {
tg.uncaughtThrowable = error;
}
});
Thread.currentThread().setName(OLD_MAIN_THREAD_NAME);
vthread.setName("main");
vthread.start();
vthread.join();
if (tg.uncaughtThrowable != null) {
// Note we cant just rethrow tg.uncaughtThrowable because there are tests
// that track ExceptionEvents, and they will complain about the extra
// exception. So instead mimic what happens when the main thread exits
// with an exception.
System.out.println("Uncaught Exception: " + tg.uncaughtThrowable);
tg.uncaughtThrowable.printStackTrace(System.out);
System.exit(1);
}
} else if (getWrapperName().equals("Kernel")) {
MainThreadGroup tg = new MainThreadGroup();
Thread t = new Thread(tg, () -> {
try {
mainMethod.invoke(null, new Object[] { classArgs });
} catch (InvocationTargetException e) {
tg.uncaughtThrowable = e.getCause();
} catch (Throwable error) {
tg.uncaughtThrowable = error;
}
});
t.start();
t.join();
if (tg.uncaughtThrowable != null) {
throw new RuntimeException(tg.uncaughtThrowable);
}
} else {
mainMethod.invoke(null, new Object[] { classArgs });
}
}

static class MainThreadGroup extends ThreadGroup {
MainThreadGroup() {
super("MainThreadGroup");
}

public void uncaughtException(Thread t, Throwable e) {
if (e instanceof ThreadDeath) {
return;
}
e.printStackTrace(System.err);
uncaughtThrowable = e;
}
Throwable uncaughtThrowable = null;
}
}
4 changes: 2 additions & 2 deletions test/jdk/com/sun/jdi/DeferredStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static void main(String argv[]) {

jj1 obj1 = new jj1();
jj2 obj2 = new jj2();
Thread thread1 = TestScaffold.newThread(obj1, "jj1");
Thread thread2 = TestScaffold.newThread(obj2, "jj2");
Thread thread1 = DebuggeeWrapper.newThread(obj1, "jj1");
Thread thread2 = DebuggeeWrapper.newThread(obj2, "jj2");
thread1.start();
thread2.start();
// Threads might be deamon threads, so wait here for them to complete.
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/EATests.java
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ public void run() {
public static void staticSetUp() {
inflatedLock = new XYVal(1, 1);
synchronized (inflatedLock) {
inflatorThread = TestScaffold.newThread(() -> {
inflatorThread = DebuggeeWrapper.newThread(() -> {
synchronized (inflatedLock) {
inflatedLockIsPermanentlyInflated = true;
inflatedLock.notify(); // main thread
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/ForceEarlyReturnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void printStack(ThreadReference thread, String msg) throws Exception {
protected void runTests() throws Exception {
BreakpointEvent bpe = startTo("ForceEarlyReturnTestTarg", "loopOrSleep", "()V");
ThreadReference mainThread = bpe.thread();
boolean is_vthread_mode = "Virtual".equals(System.getProperty("main.wrapper"));
boolean is_vthread_mode = DebuggeeWrapper.isVirtual();

// Resume main thread until it is in Thread.sleep() or the infinite loop.
mainThread.resume();
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/InterruptHangTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class InterruptHangTarg {
public static void main(String[] args){
int answer = 0;
System.out.println("Howdy!");
Thread interruptorThread = TestScaffold.newThread(new Interruptor(Thread.currentThread()));
Thread interruptorThread = DebuggeeWrapper.newThread(new Interruptor(Thread.currentThread()));

synchronized(sync) {
interruptorThread.start();
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/jdi/InvokeHangTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class InvokeHangTarg implements Runnable {

public static void main(String[] args) {
System.out.println("Howdy!");
Thread t1 = TestScaffold.newThread(new InvokeHangTarg(), name1);
Thread t2 = TestScaffold.newThread(new InvokeHangTarg(), name2);
Thread t1 = DebuggeeWrapper.newThread(new InvokeHangTarg(), name1);
Thread t2 = DebuggeeWrapper.newThread(new InvokeHangTarg(), name2);

t1.start();
t2.start();
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/JdbLockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JdbLockTestTarg {
static String jj = "jj";
public static void main(String args[]) {
synchronized(jj) {
Thread xx = TestScaffold.newThread(new Sleeper());
Thread xx = DebuggeeWrapper.newThread(new Sleeper());
xx.start();
// Give the sleeper a chance to run and get to
// the synchronized statement.
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/com/sun/jdi/JdbStopThreadidTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ private static void test() {
MyTask myTask1 = test.new MyTask();
MyTask myTask2 = test.new MyTask();
MyTask myTask3 = test.new MyTask();
Thread myThread1 = TestScaffold.newThread(myTask1, "MYTHREAD-1");
Thread myThread2 = TestScaffold.newThread(myTask2, "MYTHREAD-2");
Thread myThread3 = TestScaffold.newThread(myTask3, "MYTHREAD-3");
Thread myThread1 = DebuggeeWrapper.newThread(myTask1, "MYTHREAD-1");
Thread myThread2 = DebuggeeWrapper.newThread(myTask2, "MYTHREAD-2");
Thread myThread3 = DebuggeeWrapper.newThread(myTask3, "MYTHREAD-3");

synchronized (lockObj) {
myThread1.start();
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/MonitorEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void main(String[] args){
endingMonitor = new Object();
startingMonitor = new Object();

Thread t1 = TestScaffold.newThread(new MyTask());
Thread t1 = DebuggeeWrapper.newThread(new MyTask());
foo();
aboutEnterLock = false;

Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/MultiBreakpointsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Thread console(final int num, final int nhits) {
//
//final String threadName = "DebuggeeThread: " + num;
final String threadName = "" + num;
Thread thrd = TestScaffold.newThread(() -> {
Thread thrd = DebuggeeWrapper.newThread(() -> {
synchronized( isr ) {
boolean done = false;
try {
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/PopAsynchronousTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected void runTests() throws Exception {
/*
* start popping wildly away
*/
TestScaffold.newThread(new HarassThread()).start();
DebuggeeWrapper.newThread(new HarassThread()).start();

/*
* resume the target listening for events
Expand Down
3 changes: 1 addition & 2 deletions test/jdk/com/sun/jdi/PopFramesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ protected void runTests() throws Exception {
* works. So you have an unmounted virtual thread with no native frames, which
* results in OpaqueFrameException being thrown.
*/
String mainWrapper = System.getProperty("main.wrapper");
if ("Virtual".equals(mainWrapper)) {
if (DebuggeeWrapper.isVirtual()) {
expected_exception = OpaqueFrameException.class;
} else {
expected_exception = NativeMethodException.class;
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/jdi/ResumeOneThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class ResumeOneThreadTarg implements Runnable {

public static void main(String[] args) {
System.out.println(" Debuggee: Howdy!");
Thread t1 = TestScaffold.newThread(new ResumeOneThreadTarg(), name1);
Thread t2 = TestScaffold.newThread(new ResumeOneThreadTarg(), name2);
Thread t1 = DebuggeeWrapper.newThread(new ResumeOneThreadTarg(), name1);
Thread t2 = DebuggeeWrapper.newThread(new ResumeOneThreadTarg(), name2);
t1.start();
t2.start();
// We must block until these threads exit. Otherwise for virtual threads
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/com/sun/jdi/SetLocalWhileThreadInNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected void runTests()
}
}
}
boolean isVirtualThread = "Virtual".equals(System.getProperty("main.wrapper"));
boolean isVirtualThread = DebuggeeWrapper.isVirtual();
Asserts.assertTrue(caughtOFE == isVirtualThread);
Asserts.assertTrue(changedLocal == !isVirtualThread);

Expand Down
4 changes: 2 additions & 2 deletions test/jdk/com/sun/jdi/SimulResumerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class SimulResumerTarg implements Runnable {
static int count = 10000;
public static void main(String[] args) {
System.out.println("Howdy!");
Thread t1 = TestScaffold.newThread(new SimulResumerTarg(), name1);
Thread t2 = TestScaffold.newThread(new SimulResumerTarg(), name2);
Thread t1 = DebuggeeWrapper.newThread(new SimulResumerTarg(), name1);
Thread t2 = DebuggeeWrapper.newThread(new SimulResumerTarg(), name2);

t1.start();
t2.start();
Expand Down
Loading

0 comments on commit 702d337

Please sign in to comment.