Skip to content

Commit

Permalink
Add an option to specify the debug address and display info when star…
Browse files Browse the repository at this point in the history
…ting (fixes #737) (#753)
  • Loading branch information
gnodet committed Jan 6, 2023
1 parent 740f914 commit 5eebb89
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -122,7 +124,7 @@ public DaemonClientConnection connect(ClientOutput output) {
final String daemonId = newId();
String message = handleStopEvents(daemonId, idleDaemons, busyDaemons);
output.accept(Message.buildStatus(message));
return startDaemon(daemonId);
return startDaemon(daemonId, output);
}

private DaemonClientConnection connectNoDaemon() {
Expand Down Expand Up @@ -297,8 +299,8 @@ private DaemonClientConnection findConnection(List<DaemonInfo> compatibleDaemons
return null;
}

public DaemonClientConnection startDaemon(String daemonId) {
final Process process = startDaemonProcess(daemonId);
public DaemonClientConnection startDaemon(String daemonId, ClientOutput output) {
final Process process = startDaemonProcess(daemonId, output);
LOGGER.debug("Started Maven daemon {}", daemonId);
long start = System.currentTimeMillis();
do {
Expand All @@ -321,7 +323,7 @@ static String newId() {
return String.format("%08x", new Random().nextInt());
}

private Process startDaemonProcess(String daemonId) {
private Process startDaemonProcess(String daemonId, ClientOutput output) {
final Path mvndHome = parameters.mvndHome();
final Path workingDir = parameters.userDir();
String command = "";
Expand Down Expand Up @@ -354,7 +356,32 @@ private Process startDaemonProcess(String daemonId) {
args.add("-javaagent:" + mvndAgentPath);
// debug options
if (parameters.property(Environment.MVND_DEBUG).asBoolean()) {
args.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000");
String address =
parameters.property(Environment.MVND_DEBUG_ADDRESS).asString();
String host;
String port;
int column = address.indexOf(':');
if (column >= 0) {
host = address.substring(0, column);
port = address.substring(column + 1);
} else {
host = "localhost";
port = address;
}
if (!port.matches("[0-9]+")) {
throw new IllegalArgumentException("Wrong debug address syntax: " + address);
}
int iPort = Integer.parseInt(port);
if (iPort == 0) {
try (ServerSocketChannel channel = SocketFamily.inet.openServerSocket()) {
iPort = ((InetSocketAddress) channel.getLocalAddress()).getPort();
} catch (IOException e) {
throw new IllegalStateException("Unable to find a free debug port", e);
}
}
address = host + ":" + iPort;
output.accept(Message.buildStatus("Daemon listening for debugger on address: " + address));
args.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + address);
}
// jvm args
String jvmArgs = parameters.jvmArgs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ public enum Environment {
* not passed to the daemon.
*/
MVND_DEBUG("mvnd.debug", null, Boolean.FALSE, OptionType.BOOLEAN, Flags.DISCRIMINATING),
/**
* The tcp address used to launch the debug mode. Defaults to <code>8000</code>, which is similar to
* <code>localhost:8000</code>. In order to remote debug from a different computer, you need to allow
* remote connections using <code>*:8000</code> for example. Use a port with a value of <code>0</code>
* to have <code>mvnd</code> to choose one.
*/
MVND_DEBUG_ADDRESS("mvnd.debug.address", null, "8000", OptionType.STRING, Flags.DISCRIMINATING),
/**
* A time period after which an unused daemon will terminate by itself.
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/src/main/distro/bin/mvnd-bash-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ _mvnd()

local mvnd_opts="-1"
local mvnd_long_opts="--color|--completion|--purge|--serial|--status|--stop"
local mvnd_properties="-Djava.home|-Djdk.java.options|-Dmaven.multiModuleProjectDirectory|-Dmaven.repo.local|-Dmaven.settings|-Dmvnd.buildTime|-Dmvnd.builder|-Dmvnd.daemonStorage|-Dmvnd.debug|-Dmvnd.duplicateDaemonGracePeriod|-Dmvnd.enableAssertions|-Dmvnd.expirationCheckDelay|-Dmvnd.home|-Dmvnd.idleTimeout|-Dmvnd.jvmArgs|-Dmvnd.keepAlive|-Dmvnd.logPurgePeriod|-Dmvnd.logback|-Dmvnd.maxHeapSize|-Dmvnd.maxLostKeepAlive|-Dmvnd.minHeapSize|-Dmvnd.minThreads|-Dmvnd.noBuffering|-Dmvnd.noDaemon|-Dmvnd.pluginRealmEvictPattern|-Dmvnd.propertiesPath|-Dmvnd.registry|-Dmvnd.rollingWindowSize|-Dmvnd.serial|-Dmvnd.socketFamily|-Dmvnd.threadStackSize|-Dmvnd.threads|-Dstyle.color|-Duser.dir|-Duser.home"
local mvnd_properties="-Djava.home|-Djdk.java.options|-Dmaven.multiModuleProjectDirectory|-Dmaven.repo.local|-Dmaven.settings|-Dmvnd.buildTime|-Dmvnd.builder|-Dmvnd.daemonStorage|-Dmvnd.debug|-Dmvnd.debug.address|-Dmvnd.duplicateDaemonGracePeriod|-Dmvnd.enableAssertions|-Dmvnd.expirationCheckDelay|-Dmvnd.home|-Dmvnd.idleTimeout|-Dmvnd.jvmArgs|-Dmvnd.keepAlive|-Dmvnd.logPurgePeriod|-Dmvnd.logback|-Dmvnd.maxHeapSize|-Dmvnd.maxLostKeepAlive|-Dmvnd.minHeapSize|-Dmvnd.minThreads|-Dmvnd.noBuffering|-Dmvnd.noDaemon|-Dmvnd.pluginRealmEvictPattern|-Dmvnd.propertiesPath|-Dmvnd.registry|-Dmvnd.rollingWindowSize|-Dmvnd.serial|-Dmvnd.socketFamily|-Dmvnd.threadStackSize|-Dmvnd.threads|-Dstyle.color|-Duser.dir|-Duser.home"
local opts="-am|-amd|-B|-C|-c|-cpu|-D|-e|-emp|-ep|-f|-fae|-ff|-fn|-gs|-h|-l|-N|-npr|-npu|-nsu|-o|-P|-pl|-q|-rf|-s|-T|-t|-U|-up|-V|-v|-X|${mvnd_opts}"
local long_opts="--also-make|--also-make-dependents|--batch-mode|--strict-checksums|--lax-checksums|--check-plugin-updates|--define|--errors|--encrypt-master-password|--encrypt-password|--file|--fail-at-end|--fail-fast|--fail-never|--global-settings|--help|--log-file|--non-recursive|--no-plugin-registry|--no-plugin-updates|--no-snapshot-updates|--offline|--activate-profiles|--projects|--quiet|--resume-from|--settings|--threads|--toolchains|--update-snapshots|--update-plugins|--show-version|--version|--debug|${mvnd_long_opts}"

Expand Down

0 comments on commit 5eebb89

Please sign in to comment.