Skip to content

Commit

Permalink
8260540: serviceability/jdwp/AllModulesCommandTest.java failed with "…
Browse files Browse the repository at this point in the history
…Debuggee error: 'ERROR: transport error 202: bind failed: Address already in use'"

Reviewed-by: lucy
Backport-of: 7a23c9cbb7d09c4e6c4e8d9b2f912e41d60fd05e
  • Loading branch information
GoeLin committed Mar 25, 2024
1 parent b4d22f6 commit d74ef51
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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
Expand Down Expand Up @@ -82,7 +82,7 @@ private void doJdwp() throws Exception {
try {
// Establish JDWP socket connection
channel = new JdwpChannel();
channel.connect();
channel.connect(launcher.getJdwpPort());
// Send out ALLMODULES JDWP command
// and verify the reply
JdwpAllModulesReply reply = new JdwpAllModulesCmd().send(channel);
Expand Down
33 changes: 13 additions & 20 deletions test/hotspot/jtreg/serviceability/jdwp/DebuggeeLauncher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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
Expand All @@ -21,11 +21,9 @@
* questions.
*/

import java.io.IOException;
import java.net.ServerSocket;
import java.util.StringTokenizer;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.Utils;
import jdk.test.lib.JDWP;
import static jdk.test.lib.Asserts.assertFalse;

/**
Expand Down Expand Up @@ -55,7 +53,7 @@ public interface Listener {
void onDebuggeeError(String line);
}

private static int jdwpPort = -1;
private int jdwpPort = -1;
private static final String CLS_DIR = System.getProperty("test.classes", "").trim();
private static final String DEBUGGEE = "AllModulesCommandTestDebuggee";
private Process p;
Expand Down Expand Up @@ -117,30 +115,19 @@ public void terminateDebuggee() {
* @return the JDWP options to start the debuggee with
*/
private static String getJdwpOptions() {
return "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + getJdwpPort();
return "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0";
}

/**
* Find an available port for the JDWP session
* Gets JDWP port debuggee is listening on.
*
* @return JDWP port
*/
public static int getJdwpPort() {
if (jdwpPort == -1) {
jdwpPort = findFreePort();
assertFalse(jdwpPort == -1, "Can not find vailbale port for JDWP");
}
public int getJdwpPort() {
assertFalse(jdwpPort == -1, "JDWP port is not detected");
return jdwpPort;
}

private static int findFreePort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
}
return -1;
}

@Override
public void onStringRead(StreamHandler handler, String line) {
if (handler.equals(errorHandler)) {
Expand All @@ -152,6 +139,12 @@ public void onStringRead(StreamHandler handler, String line) {
}

private void processDebuggeeOutput(String line) {
if (jdwpPort == -1) {
JDWP.ListenAddress addr = JDWP.parseListenAddress(line);
if (addr != null) {
jdwpPort = Integer.parseInt(addr.address());
}
}
StringTokenizer st = new StringTokenizer(line);
String token = st.nextToken();
switch (token) {
Expand Down
6 changes: 3 additions & 3 deletions test/hotspot/jtreg/serviceability/jdwp/JdwpChannel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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
Expand Down Expand Up @@ -33,8 +33,8 @@ public class JdwpChannel {

private Socket sock;

public void connect() throws IOException {
sock = new Socket("localhost", DebuggeeLauncher.getJdwpPort());
public void connect(int jdwpPort) throws IOException {
sock = new Socket("localhost", jdwpPort);
handshake();
}

Expand Down
14 changes: 5 additions & 9 deletions test/jdk/com/sun/jdi/JdwpAllowTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, 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
Expand Down Expand Up @@ -34,16 +34,14 @@
import java.net.Socket;
import java.net.SocketException;

import jdk.test.lib.JDWP;
import jdk.test.lib.Utils;
import jdk.test.lib.apps.LingeredApp;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class JdwpAllowTest {
Expand Down Expand Up @@ -76,16 +74,14 @@ public static String[] prepareCmd(String allowOpt) {
return new String[] { jdwpArgs };
}

private static Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(\\d+)\\b");
private static int detectPort(LingeredApp app) {
long maxWaitTime = System.currentTimeMillis()
+ Utils.adjustTimeout(10000); // 10 seconds adjusted for TIMEOUT_FACTOR
while (true) {
String s = app.getProcessStdout();
Matcher m = listenRegexp.matcher(s);
if (m.find()) {
// m.group(1) is transport, m.group(2) is port
return Integer.parseInt(m.group(2));
JDWP.ListenAddress addr = JDWP.parseListenAddress(s);
if (addr != null) {
return Integer.parseInt(addr.address());
}
if (System.currentTimeMillis() > maxWaitTime) {
throw new RuntimeException("Could not detect port from '" + s + "' (timeout)");
Expand Down
16 changes: 6 additions & 10 deletions test/jdk/com/sun/jdi/RunToExit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2021, 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
Expand Down Expand Up @@ -40,9 +40,9 @@
import java.util.List;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import jdk.test.lib.JDWP;
import jdk.test.lib.process.ProcessTools;

public class RunToExit {
Expand Down Expand Up @@ -95,16 +95,12 @@ private static Process launch(String class_name) throws Exception {
return p;
}

/* warm-up predicate for debuggee */
private static Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");

private static boolean isTransportListening(String line) {
Matcher m = listenRegexp.matcher(line);
if (!m.matches()) {
JDWP.ListenAddress addr = JDWP.parseListenAddress(line);
if (addr == null) {
return false;
}
// address is 2nd group
address = m.group(2);
address = addr.address();
return true;
}

Expand Down
38 changes: 10 additions & 28 deletions test/jdk/com/sun/jdi/lib/jdb/Debuggee.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

package lib.jdb;

import jdk.test.lib.JDWP;
import jdk.test.lib.Utils;
import jdk.test.lib.util.Pair;
import jdk.test.lib.process.ProcessTools;

import java.io.Closeable;
Expand Down Expand Up @@ -128,7 +128,7 @@ public ProcessBuilder prepare() {
public Debuggee launch(String name) {
return new Debuggee(prepare(), name,
onthrow.isEmpty() ?
Launcher::parseListenAddress :
JDWP::parseListenAddress :
Launcher::parseLaunchEchoListenAddress
);
}
Expand All @@ -140,47 +140,29 @@ public Debuggee launch() {
* Parses debuggee output to get listening transport and address, printed by `launch=echo`.
* Returns null if the string specified does not contain required info.
*/
private static Pair<String, String> parseLaunchEchoListenAddress(String debuggeeOutput) {
private static JDWP.ListenAddress parseLaunchEchoListenAddress(String debuggeeOutput) {
Pattern listenRegexp = Pattern.compile(LAUNCH_ECHO_STRING + " \\b(.+)\\b \\b(.+)\\b");
Matcher m = listenRegexp.matcher(debuggeeOutput);
if (m.find()) {
return new Pair<String, String>(m.group(1), m.group(2));
}
return null;
}

/**
* Parses debuggee output to get listening transport and address, printed by `launch=echo`.
* Returns null if the string specified does not contain required info.
*/
private static Pair<String, String> parseListenAddress(String debuggeeOutput) {
Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");
Matcher m = listenRegexp.matcher(debuggeeOutput);
if (m.find()) {
return new Pair<String, String>(m.group(1), m.group(2));
return new JDWP.ListenAddress(m.group(1), m.group(2));
}
return null;
}
}

// starts the process, waits until the provided addressDetector detects transport/address from the process output
private Debuggee(ProcessBuilder pb, String name, Function<String, Pair<String, String>> addressDetector) {
String[] debuggeeListen = new String[2];
private Debuggee(ProcessBuilder pb, String name, Function<String, JDWP.ListenAddress> addressDetector) {
JDWP.ListenAddress[] listenAddress = new JDWP.ListenAddress[1];
try {
p = ProcessTools.startProcess(name, pb,
s -> output.add(s), // output consumer
s -> {
Pair<String, String> addr = addressDetector.apply(s);
if (addr != null) {
debuggeeListen[0] = addr.first;
debuggeeListen[1] = addr.second;
return true;
}
return false;
listenAddress[0] = addressDetector.apply(s);
return listenAddress[0] != null;
},
30, TimeUnit.SECONDS);
transport = debuggeeListen[0];
address = debuggeeListen[1];
transport = listenAddress[0].transport();
address = listenAddress[0].address();
} catch (IOException | InterruptedException | TimeoutException ex) {
throw new RuntimeException("failed to launch debuggee", ex);
}
Expand Down
52 changes: 52 additions & 0 deletions test/lib/jdk/test/lib/JDWP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2021, 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.
*/

package jdk.test.lib;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JDWP {

public record ListenAddress(String transport, String address) {
}

// lazy initialized
private static Pattern listenRegexp;

/**
* Parses debuggee output to get listening transport and address.
* Returns null if the string specified does not contain required info.
*/
public static ListenAddress parseListenAddress(String debuggeeOutput) {
if (listenRegexp == null) {
listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");
}
Matcher m = listenRegexp.matcher(debuggeeOutput);
if (m.find()) {
return new ListenAddress(m.group(1), m.group(2));
}
return null;
}

}

1 comment on commit d74ef51

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.