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: sspitsyn, kevinw
  • Loading branch information
Alex Menkov committed Jun 29, 2021
1 parent c1e2a29 commit 7a23c9c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 59 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
22 changes: 7 additions & 15 deletions test/jdk/com/sun/jdi/lib/jdb/Debuggee.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 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 @@ -23,6 +23,7 @@

package lib.jdb;

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

Expand All @@ -32,8 +33,6 @@
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;

/**
Expand Down Expand Up @@ -124,24 +123,17 @@ public Debuggee launch() {

// starts the process, waits for "Listening for transport" output and detects transport/address
private Debuggee(ProcessBuilder pb, String name) {
// debuggeeListen[0] - transport, debuggeeListen[1] - address
String[] debuggeeListen = new String[2];
Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(.+)\\b");
JDWP.ListenAddress[] listenAddress = new JDWP.ListenAddress[1];
try {
p = ProcessTools.startProcess(name, pb,
s -> output.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;
listenAddress[0] = JDWP.parseListenAddress(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 7a23c9c

@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.