< prev index next >
test/jdk/com/sun/jdi/lib/jdb/JdbTest.java
Print this page
@@ -21,24 +21,19 @@
* questions.
*/
package lib.jdb;
-import jdk.test.lib.Utils;
import jdk.test.lib.process.OutputAnalyzer;
-import jdk.test.lib.process.ProcessTools;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import java.util.stream.Collectors;
public abstract class JdbTest {
public static class LaunchOptions {
@@ -74,22 +69,21 @@
public JdbTest(String debuggeeClass, String sourceFilename) {
this(new LaunchOptions(debuggeeClass).setSourceFilename(sourceFilename));
}
protected Jdb jdb;
- protected Process debuggee;
- private final List<String> debuggeeOutput = new LinkedList<>();
+ protected Debuggee debuggee;
private final LaunchOptions launchOptions;
// returns the whole jdb output as a string
public String getJdbOutput() {
return jdb == null ? "" : jdb.getJdbOutput();
}
// returns the whole debuggee output as a string
public String getDebuggeeOutput() {
- return debuggeeOutput.stream().collect(Collectors.joining(lineSeparator));
+ return debuggee == null ? "" : debuggee.getOutput();
}
public void run() {
try {
setup();
@@ -104,49 +98,26 @@
}
}
protected void setup() {
/* run debuggee as:
- java -agentlib:jdwp=transport=dt_socket,address=0,server=n,suspend=y <debuggeeClass>
+ java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y <debuggeeClass>
it reports something like : Listening for transport dt_socket at address: 60810
after that connect jdb by:
jdb -connect com.sun.jdi.SocketAttach:port=60810
*/
// launch debuggee
- List<String> debuggeeArgs = new LinkedList<>();
- // specify address=0 to automatically select free port
- debuggeeArgs.add("-agentlib:jdwp=transport=dt_socket,address=0,server=y,suspend=y");
- debuggeeArgs.addAll(launchOptions.debuggeeOptions);
- debuggeeArgs.add(launchOptions.debuggeeClass);
- ProcessBuilder pbDebuggee = ProcessTools.createJavaProcessBuilder(true, debuggeeArgs.toArray(new String[0]));
-
- // debuggeeListen[0] - transport, debuggeeListen[1] - address
- String[] debuggeeListen = new String[2];
- Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(\\d+)\\b");
- try {
- debuggee = ProcessTools.startProcess("debuggee", pbDebuggee,
- s -> debuggeeOutput.add(s), // output consumer
- s -> { // warm-up predicate
- Matcher m = listenRegexp.matcher(s);
- if (!m.matches()) {
- return false;
- }
- debuggeeListen[0] = m.group(1);
- debuggeeListen[1] = m.group(2);
- return true;
- },
- 30, TimeUnit.SECONDS);
- } catch (IOException | InterruptedException | TimeoutException ex) {
- throw new RuntimeException("failed to launch debuggee", ex);
- }
+ debuggee = Debuggee.launcher(launchOptions.debuggeeClass)
+ .addOptions(launchOptions.debuggeeOptions)
+ .launch();
// launch jdb
try {
- jdb = new Jdb("-connect", "com.sun.jdi.SocketAttach:port=" + debuggeeListen[1]);
+ jdb = new Jdb("-connect", "com.sun.jdi.SocketAttach:port=" + debuggee.getAddress());
} catch (Throwable ex) {
// terminate debuggee if something went wrong
- debuggee.destroy();
+ debuggee.shutdown();
throw ex;
}
// wait while jdb is initialized
jdb.waitForPrompt(1, false);
}
@@ -156,19 +127,13 @@
protected void shutdown() {
if (jdb != null) {
jdb.shutdown();
}
// shutdown debuggee
- if (debuggee != null && debuggee.isAlive()) {
- try {
- debuggee.waitFor(Utils.adjustTimeout(10), TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- // ignore
- } finally {
- if (debuggee.isAlive()) {
- debuggee.destroy();
- }
+ if (debuggee != null) {
+ if (!debuggee.waitFor(10, TimeUnit.SECONDS)) {
+ debuggee.shutdown();
}
}
}
protected static final String lineSeparator = System.getProperty("line.separator");
< prev index next >