Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,7 +41,9 @@
public final class BTraceClient {
private static final Logger log = LoggerFactory.getLogger(BTraceClient.class);
private static final String JAR_PATH_PROPERTY = "btrace.jar.path";
private static final Pattern PORT_UNAVAILABLE = Pattern.compile("Port \\d+ unavailable.*");
// Older clients say "unavailable"; newer ones say "is in use" and advise the CLI's -p flag
private static final Pattern PORT_UNAVAILABLE =
Pattern.compile("Port (\\d+) (?:unavailable|is in use by another process).*");

@FunctionalInterface
public interface CommandListener {
Expand Down Expand Up @@ -236,12 +239,16 @@ private static Object call(Method method, Object target, Object... args) throws
*/
private static Exception unwrap(InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException
&& cause.getMessage() != null
&& PORT_UNAVAILABLE.matcher(cause.getMessage()).matches()) {
Matcher portUnavailable =
cause instanceof IOException && cause.getMessage() != null
? PORT_UNAVAILABLE.matcher(cause.getMessage())
: null;
if (portUnavailable != null && portUnavailable.matches()) {
return new IOException(
cause.getMessage()
+ " Another BTrace agent or process is using it; pass a different 'port' argument.",
"Port "
+ portUnavailable.group(1)
+ " unavailable. Another BTrace agent or process is using it; pass a different"
+ " 'port' argument.",
cause);
}
if (cause instanceof Exception && cause.getMessage() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void attach(String pid, String systemClassPath, String bootstrapClassPath
if (pid.equals("port-busy")) {
throw new java.io.IOException("Port " + port + " unavailable.");
}
if (pid.equals("port-in-use")) {
// The wording of the current BTrace client
throw new java.io.IOException(
"Port "
+ port
+ " is in use by another process (often another BTrace agent); choose a free"
+ " agent port with -p <port>.");
}
if (pid.equals("no-message")) {
throw new java.io.IOException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ void aBusyPortNamesThePortAndTheArgumentThatAvoidsIt() throws Exception {
assertTrue(e.getMessage().contains("'port' argument"), e.getMessage());
}

@Test
void aBusyPortInTheCurrentClientWordingNamesTheArgumentNotTheCliFlag() throws Exception {
BTraceClient client = BTraceClient.create(2020);

IOException e = assertThrows(IOException.class, () -> client.attach("port-in-use", null, "."));

assertTrue(e.getMessage().startsWith("Port 2020 unavailable. Another"), e.getMessage());
assertTrue(e.getMessage().contains("'port' argument"), e.getMessage());
assertFalse(e.getMessage().contains("-p"), e.getMessage());
}

@Test
void aFailureWithoutAMessageStillDescribesItself() throws Exception {
BTraceClient client = BTraceClient.create(2020);
Expand Down
Loading