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 @@ -40,10 +40,39 @@
import com.sun.jdi.request.StepRequest;

public class DebugUtility {
public static final String HOME = "home";
public static final String OPTIONS = "options";
public static final String MAIN = "main";
public static final String SUSPEND = "suspend";
public static final String QUOTE = "quote";
public static final String EXEC = "vmexec";
public static final String CWD = "cwd";
public static final String ENV = "env";
public static final String HOSTNAME = "hostname";
public static final String PORT = "port";
public static final String TIMEOUT = "timeout";

public static IDebugSession launch(VirtualMachineManager vmManager, String mainClass, String programArguments, String vmArguments, List<String> classPaths,
String cwd, String[] envVars) throws IOException, IllegalConnectorArgumentsException, VMStartException {
return DebugUtility.launch(vmManager, mainClass, programArguments, vmArguments, String.join(File.pathSeparator, classPaths), cwd, envVars);
/**
* Launch a debuggee in suspend mode.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suspend ==> suspended

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suspend Mode is OK, you can google it, there are many places calling it Suspend Mode.

* @see {@link #launch(VirtualMachineManager, String, String, String, String, String)}
*/
public static IDebugSession launch(VirtualMachineManager vmManager,
String mainClass,
String programArguments,
String vmArguments,
List<String> modulePaths,
List<String> classPaths,
String cwd,
String[] envVars)
throws IOException, IllegalConnectorArgumentsException, VMStartException {
return DebugUtility.launch(vmManager,
mainClass,
programArguments,
vmArguments,
String.join(File.pathSeparator, modulePaths),
String.join(File.pathSeparator, classPaths),
cwd,
envVars);
}

/**
Expand All @@ -57,6 +86,8 @@ public static IDebugSession launch(VirtualMachineManager vmManager, String mainC
* the program arguments.
* @param vmArguments
* the vm arguments.
* @param modulePaths
* the module paths.
* @param classPaths
* the class paths.
* @param cwd
Expand All @@ -73,30 +104,49 @@ public static IDebugSession launch(VirtualMachineManager vmManager, String mainC
* when the debuggee was successfully launched, but terminated
* with an error before a connection could be established.
*/
public static IDebugSession launch(VirtualMachineManager vmManager, String mainClass, String programArguments, String vmArguments, String classPaths,
String cwd, String[] envVars) throws IOException, IllegalConnectorArgumentsException, VMStartException {
public static IDebugSession launch(VirtualMachineManager vmManager,
String mainClass,
String programArguments,
String vmArguments,
String modulePaths,
String classPaths,
String cwd,
String[] envVars)
throws IOException, IllegalConnectorArgumentsException, VMStartException {
List<LaunchingConnector> connectors = vmManager.launchingConnectors();
LaunchingConnector connector = connectors.get(0);

Map<String, Argument> arguments = connector.defaultArguments();
arguments.get("suspend").setValue("true");
arguments.get(SUSPEND).setValue("true");

String options = "";
if (StringUtils.isNotBlank(vmArguments)) {
arguments.get("options").setValue(vmArguments + " -cp \"" + classPaths + "\"");
} else {
arguments.get("options").setValue("-cp \"" + classPaths + "\"");
options = vmArguments;
}
if (StringUtils.isNotBlank(modulePaths)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just curious, if the vmArguments already have the --module-path or -cp values, what would happen here?

@testforstephen Jinbo Wang (testforstephen) Nov 6, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It would be overided. We don't claim supporting --classpath in vmArguments.

options += " --module-path \"" + modulePaths + "\"";
}
if (StringUtils.isNotBlank(classPaths)) {
options += " -cp \"" + classPaths + "\"";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would suggest StringBuilder for options

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For simple string concatenation operation, i would prefer the concatenation because the code is shorter. For loop or large string operations, StringBuilder is preferable.

}
arguments.get(OPTIONS).setValue(options);

// For java 9 project, should specify "-m $MainClass".
String[] mainClasses = mainClass.split("/");
if (StringUtils.isNotBlank(modulePaths) || mainClasses.length == 2) {
mainClass = "-m " + mainClass;
}
if (StringUtils.isNotBlank(programArguments)) {
arguments.get("main").setValue(mainClass + " " + programArguments);
} else {
arguments.get("main").setValue(mainClass);
mainClass += " " + programArguments;
}
arguments.get(MAIN).setValue(mainClass);

if (arguments.get("cwd") != null) {
arguments.get("cwd").setValue(cwd);
if (arguments.get(CWD) != null) {
arguments.get(CWD).setValue(cwd);
}

if (arguments.get("env") != null) {
arguments.get("env").setValue(encodeArrayArgument(envVars));
if (arguments.get(ENV) != null) {
arguments.get(ENV).setValue(encodeArrayArgument(envVars));
}

VirtualMachine vm = connector.launch(arguments);
Expand Down Expand Up @@ -131,9 +181,9 @@ public static IDebugSession attach(VirtualMachineManager vmManager, String hostN
List<AttachingConnector> connectors = vmManager.attachingConnectors();
AttachingConnector connector = connectors.get(0);
Map<String, Argument> arguments = connector.defaultArguments();
arguments.get("hostname").setValue(hostName);
arguments.get("port").setValue(String.valueOf(port));
arguments.get("timeout").setValue(String.valueOf(attachTimeout));
arguments.get(HOSTNAME).setValue(hostName);
arguments.get(PORT).setValue(String.valueOf(port));
arguments.get(TIMEOUT).setValue(String.valueOf(attachTimeout));
return new DebugSession(connector.attach(arguments));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map.Entry;
import java.util.logging.Logger;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.microsoft.java.debug.core.Configuration;
Expand Down Expand Up @@ -55,10 +56,10 @@ public List<Command> getTargetCommands() {
@Override
public void handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) {
LaunchArguments launchArguments = (LaunchArguments) arguments;
if (StringUtils.isBlank(launchArguments.mainClass) || launchArguments.classPaths == null
|| launchArguments.classPaths.length == 0) {
if (StringUtils.isBlank(launchArguments.mainClass)
|| (ArrayUtils.isEmpty(launchArguments.modulePaths) && ArrayUtils.isEmpty(launchArguments.classPaths))) {
AdapterUtils.setErrorResponse(response, ErrorCode.ARGUMENT_MISSING,
String.format("Failed to launch debuggee VM. Missing mainClass or classPath options in launch configuration"));
String.format("Failed to launch debuggee VM. Missing mainClass or modulePaths/classPaths options in launch configuration"));
return;
}

Expand All @@ -70,7 +71,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
} else {
if (!Charset.isSupported(launchArguments.encoding)) {
AdapterUtils.setErrorResponse(response, ErrorCode.INVALID_ENCODING,
String.format("Failed to launch debuggee VM. 'encoding' options in launch configuration is not recognized."));
String.format("Failed to launch debuggee VM. 'encoding' options in the launch configuration is not recognized."));
return;
}

Expand Down Expand Up @@ -114,11 +115,26 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
}

try {
logger.info(String.format("Trying to launch Java Program with options \"%s -cp %s %s %s\" .",
launchArguments.vmArgs, StringUtils.join(launchArguments.classPaths, File.pathSeparator), launchArguments.mainClass, launchArguments.args));
IDebugSession debugSession = DebugUtility.launch(vmProvider.getVirtualMachineManager(), launchArguments.mainClass, launchArguments.args,
launchArguments.vmArgs, Arrays.asList(launchArguments.classPaths), launchArguments.cwd, envVars);
StringBuilder launchLogs = new StringBuilder();
launchLogs.append("Trying to launch Java Program with options:\n");
launchLogs.append(String.format("main-class: %s\n", launchArguments.mainClass));
launchLogs.append(String.format("args: %s\n", launchArguments.args));
launchLogs.append(String.format("module-path: %s\n", StringUtils.join(launchArguments.modulePaths, File.pathSeparator)));
launchLogs.append(String.format("class-path: %s\n", StringUtils.join(launchArguments.classPaths, File.pathSeparator)));
launchLogs.append(String.format("vmArgs: %s", launchArguments.vmArgs));
logger.info(launchLogs.toString());

IDebugSession debugSession = DebugUtility.launch(
vmProvider.getVirtualMachineManager(),
launchArguments.mainClass,
launchArguments.args,
launchArguments.vmArgs,
Arrays.asList(launchArguments.modulePaths),
Arrays.asList(launchArguments.classPaths),
launchArguments.cwd,
envVars);
context.setDebugSession(debugSession);

logger.info("Launching debuggee VM succeeded.");

ProcessConsole debuggeeConsole = new ProcessConsole(debugSession.process(), "Debuggee", context.getDebuggeeEncoding());
Expand All @@ -145,4 +161,5 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
}
sourceProvider.initialize(context.getDebugSession(), options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static class LaunchArguments extends Arguments {
public String vmArgs = "";
public String encoding = "";
public String[] classPaths = new String[0];
public String[] modulePaths = new String[0];
public String[] sourcePaths = new String[0];
public String cwd;
public Map<String, String> env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static IDebugSession getDebugSession(String projectName, String mainClass
String projectRoot = new File(rootPath, name).getAbsolutePath();
try {
final IDebugSession debugSession = DebugUtility.launch(Bootstrap.virtualMachineManager(), mainClass, "", "",
new File(projectRoot, "bin").getAbsolutePath(), null, null);
null, new File(projectRoot, "bin").getAbsolutePath(), null, null);
debugSession.getEventHub().events().subscribe(debugEvent -> {
if (debugEvent.event instanceof VMDisconnectEvent) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@
*
*/
public class AdvancedLaunchingConnector extends SocketLaunchingConnectorImpl implements LaunchingConnector {
public static final String HOME = "home";
public static final String OPTIONS = "options";
public static final String MAIN = "main";
public static final String SUSPEND = "suspend";
public static final String QUOTE = "quote";
public static final String EXEC = "vmexec";
private static final String CWD = "cwd";
private static final String ENV = "env";
private static final int ACCEPT_TIMEOUT = 10 * 1000;

public AdvancedLaunchingConnector(VirtualMachineManagerImpl virtualMachineManager) {
Expand All @@ -53,13 +45,13 @@ public AdvancedLaunchingConnector(VirtualMachineManagerImpl virtualMachineManage
public Map<String, Argument> defaultArguments() {
Map<String, Argument> defaultArgs = super.defaultArguments();

Argument cwdArg = new AdvancedStringArgumentImpl(CWD, "Current working directory", CWD, false);
Argument cwdArg = new AdvancedStringArgumentImpl(DebugUtility.CWD, "Current working directory", DebugUtility.CWD, false);
cwdArg.setValue(null);
defaultArgs.put(CWD, cwdArg);
defaultArgs.put(DebugUtility.CWD, cwdArg);

Argument envArg = new AdvancedStringArgumentImpl(ENV, "Environment variables", ENV, false);
Argument envArg = new AdvancedStringArgumentImpl(DebugUtility.ENV, "Environment variables", DebugUtility.ENV, false);
envArg.setValue(null);
defaultArgs.put(ENV, envArg);
defaultArgs.put(DebugUtility.ENV, envArg);

return defaultArgs;
}
Expand All @@ -72,15 +64,15 @@ public String name() {
@Override
public VirtualMachine launch(Map<String, ? extends Argument> connectionArgs)
throws IOException, IllegalConnectorArgumentsException, VMStartException {
String cwd = connectionArgs.get(CWD).value();
String cwd = connectionArgs.get(DebugUtility.CWD).value();
File workingDir = null;
if (cwd != null && Files.isDirectory(Paths.get(cwd))) {
workingDir = new File(cwd);
}

String[] envVars = null;
try {
envVars = DebugUtility.decodeArrayArgument(connectionArgs.get(ENV).value());
envVars = DebugUtility.decodeArrayArgument(connectionArgs.get(DebugUtility.ENV).value());
} catch (IllegalArgumentException e) {
// do nothing.
}
Expand All @@ -107,12 +99,12 @@ public VirtualMachine launch(Map<String, ? extends Argument> connectionArgs)
}

private static String[] constructLaunchCommand(Map<String, ? extends Argument> launchingOptions, String address) {
final String javaHome = launchingOptions.get(HOME).value();
final String javaExec = launchingOptions.get(EXEC).value();
final String javaHome = launchingOptions.get(DebugUtility.HOME).value();
final String javaExec = launchingOptions.get(DebugUtility.EXEC).value();
final String slash = System.getProperty("file.separator");
boolean suspend = Boolean.valueOf(launchingOptions.get(SUSPEND).value());
final String javaOptions = launchingOptions.get(OPTIONS).value();
final String main = launchingOptions.get(MAIN).value();
boolean suspend = Boolean.valueOf(launchingOptions.get(DebugUtility.SUSPEND).value());
final String javaOptions = launchingOptions.get(DebugUtility.OPTIONS).value();
final String main = launchingOptions.get(DebugUtility.MAIN).value();

StringBuilder execString = new StringBuilder();
execString.append("\"" + javaHome + slash + "bin" + slash + javaExec + "\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler

public static String CONFIG_LOG_LEVEL = "vscode.java.configLogLevel";


@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
if (DEBUG_STARTSESSION.equals(commandId)) {
Expand Down
Loading