sdedic commented on a change in pull request #2978: URL: https://github.com/apache/netbeans/pull/2978#discussion_r641809053
########## File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/attach/AttachConfigurations.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.lsp.server.debugging.attach; + +import com.sun.jdi.Bootstrap; +import com.sun.jdi.VirtualMachineManager; +import com.sun.jdi.connect.AttachingConnector; +import com.sun.jdi.connect.Connector; +import com.sun.tools.attach.AttachNotSupportedException; +import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.VirtualMachineDescriptor; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +import org.eclipse.lsp4j.MessageParams; +import org.eclipse.lsp4j.MessageType; + +import org.netbeans.modules.java.lsp.server.protocol.DebugConnector; +import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient; +import org.netbeans.modules.java.lsp.server.protocol.QuickPickItem; +import org.netbeans.modules.java.lsp.server.protocol.ShowQuickPickParams; +import org.openide.util.RequestProcessor; + +/** + * Debugger attach configurations provider. + * + * @author Martin Entlicher + */ +public final class AttachConfigurations { + + static final String NAME_ATTACH_PROCESS = "Attach to Process"; // NOI18N + static final String NAME_ATTACH_SOCKET = "Attach to Port"; // NOI18N + static final String NAME_ATTACH_SHMEM = "Attach to Shared Memory"; // NOI18N + static final String NAME_ATTACH_BY = "Attach by "; // NOI18N + + static final String CONNECTOR_PROCESS = "com.sun.jdi.ProcessAttach"; // NOI18N + static final String CONNECTOR_SOCKET = "com.sun.jdi.SocketAttach"; // NOI18N + static final String CONNECTOR_SHMEM = "com.sun.jdi.SharedMemoryAttach"; // NOI18N + + static final String PROCESS_ARG_PID = "processId"; // NOI18N + static final String SOCKET_ARG_HOST = "hostName"; // NOI18N + static final String SOCKET_ARG_PORT = "port"; // NOI18N + static final String SHMEM_ARG_NAME = "sharedMemoryName"; // NOI18N + + private static final RequestProcessor RP = new RequestProcessor(AttachConfigurations.class); + + private AttachConfigurations() {} + + public static CompletableFuture<Object> findConnectors() { + return CompletableFuture.supplyAsync(() -> { + return listAttachingConnectors(); + }, RP); + } + + private static List<DebugConnector> listAttachingConnectors() { + VirtualMachineManager vmm = Bootstrap.virtualMachineManager (); + List<AttachingConnector> attachingConnectors = vmm.attachingConnectors(); + List<DebugConnector> connectors = new ArrayList<>(5); + String type = "java8+"; // NOI18N + for (AttachingConnector ac : attachingConnectors) { + String connectorName = ac.name(); + Map<String, Connector.Argument> defaultArguments = ac.defaultArguments(); + DebugConnector connector; + switch (connectorName) { + case CONNECTOR_PROCESS: + connector = new DebugConnector(NAME_ATTACH_PROCESS, type, + Collections.singletonList(PROCESS_ARG_PID), + Collections.singletonList("${command:java.attachDebugger.pickProcess}")); // NOI18N + break; + case CONNECTOR_SOCKET: { + String hostName = getArgumentOrDefault(defaultArguments.get("hostname"), "localhost"); // NOI18N + String port = getArgumentOrDefault(defaultArguments.get("port"), "<port of the debuggee JVM>"); // NOI18N + connector = new DebugConnector(NAME_ATTACH_SOCKET, type, + Arrays.asList(SOCKET_ARG_HOST, SOCKET_ARG_PORT), + Arrays.asList(hostName, port)); + break; + } + case CONNECTOR_SHMEM: { + String name = getArgumentOrDefault(defaultArguments.get("name"), "<shared memory name>"); // NOI18N + connector = new DebugConnector(NAME_ATTACH_SHMEM, type, + Collections.singletonList(SHMEM_ARG_NAME), + Collections.singletonList(name)); + break; + } + default: { + List<String> names = new ArrayList<>(); + List<String> values = new ArrayList<>(); + for (Connector.Argument arg : defaultArguments.values()) { + if (arg.mustSpecify()) { + names.add(arg.name()); + String value = arg.value(); + if (value.isEmpty()) { + value = "<" + arg.description()+ ">"; // NOI18N + } + values.add(value); + } + } + connector = new DebugConnector(NAME_ATTACH_BY + connectorName, type, + names, values); + } + } + connectors.add(connector); + } + connectors.sort((c1, c2) -> c1.getName().compareToIgnoreCase(c2.getName())); + return connectors; + } + + private static String getArgumentOrDefault(Connector.Argument arg, String def) { + if (arg != null) { + String value = arg.value(); + if (!value.isEmpty()) { + return value; + } + } + return def; + } + + public static CompletableFuture<Object> findProcessAttachTo(NbCodeLanguageClient client) { + return CompletableFuture.supplyAsync(() -> { + return listProcessesToAttachTo(client); + }, RP).thenCompose(params -> client.showQuickPick(params)).thenApply(itemsList -> { + if (itemsList == null || itemsList.isEmpty()) { + return null; + } else { + return itemsList.get(0).getUserData(); + } + }); + } + + private static void notifyNoProcessesError(NbCodeLanguageClient client) { + MessageParams params = new MessageParams(); + params.setMessage("No debuggable JVM process found.\nPlease be sure to use `-agentlib:jdwp=transport=dt_socket,server=y` option."); Review comment: NbBundle localization ? More general question to other reviewers: is the LSP server code expected to be l10n the same way as framework / IDE ? ########## File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/attach/AttachConfigurations.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.lsp.server.debugging.attach; + +import com.sun.jdi.Bootstrap; +import com.sun.jdi.VirtualMachineManager; +import com.sun.jdi.connect.AttachingConnector; +import com.sun.jdi.connect.Connector; +import com.sun.tools.attach.AttachNotSupportedException; +import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.VirtualMachineDescriptor; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +import org.eclipse.lsp4j.MessageParams; +import org.eclipse.lsp4j.MessageType; + +import org.netbeans.modules.java.lsp.server.protocol.DebugConnector; +import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient; +import org.netbeans.modules.java.lsp.server.protocol.QuickPickItem; +import org.netbeans.modules.java.lsp.server.protocol.ShowQuickPickParams; +import org.openide.util.RequestProcessor; + +/** + * Debugger attach configurations provider. + * + * @author Martin Entlicher + */ +public final class AttachConfigurations { + + static final String NAME_ATTACH_PROCESS = "Attach to Process"; // NOI18N + static final String NAME_ATTACH_SOCKET = "Attach to Port"; // NOI18N + static final String NAME_ATTACH_SHMEM = "Attach to Shared Memory"; // NOI18N + static final String NAME_ATTACH_BY = "Attach by "; // NOI18N + + static final String CONNECTOR_PROCESS = "com.sun.jdi.ProcessAttach"; // NOI18N + static final String CONNECTOR_SOCKET = "com.sun.jdi.SocketAttach"; // NOI18N + static final String CONNECTOR_SHMEM = "com.sun.jdi.SharedMemoryAttach"; // NOI18N + + static final String PROCESS_ARG_PID = "processId"; // NOI18N + static final String SOCKET_ARG_HOST = "hostName"; // NOI18N + static final String SOCKET_ARG_PORT = "port"; // NOI18N + static final String SHMEM_ARG_NAME = "sharedMemoryName"; // NOI18N + + private static final RequestProcessor RP = new RequestProcessor(AttachConfigurations.class); + + private AttachConfigurations() {} + + public static CompletableFuture<Object> findConnectors() { + return CompletableFuture.supplyAsync(() -> { + return listAttachingConnectors(); + }, RP); + } + + private static List<DebugConnector> listAttachingConnectors() { + VirtualMachineManager vmm = Bootstrap.virtualMachineManager (); + List<AttachingConnector> attachingConnectors = vmm.attachingConnectors(); + List<DebugConnector> connectors = new ArrayList<>(5); + String type = "java8+"; // NOI18N + for (AttachingConnector ac : attachingConnectors) { + String connectorName = ac.name(); + Map<String, Connector.Argument> defaultArguments = ac.defaultArguments(); + DebugConnector connector; + switch (connectorName) { + case CONNECTOR_PROCESS: + connector = new DebugConnector(NAME_ATTACH_PROCESS, type, + Collections.singletonList(PROCESS_ARG_PID), + Collections.singletonList("${command:java.attachDebugger.pickProcess}")); // NOI18N Review comment: This could eventually use `JAVA_FIND_DEBUG_PROCESS_TO_ATTACH` constant ########## File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/attach/NbAttachRequestHandler.java ########## @@ -0,0 +1,217 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.lsp.server.debugging.attach; + +import com.sun.jdi.Bootstrap; +import com.sun.jdi.VirtualMachineManager; +import com.sun.jdi.connect.AttachingConnector; +import com.sun.jdi.connect.Connector.Argument; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.eclipse.lsp4j.MessageParams; +import org.eclipse.lsp4j.MessageType; +import org.eclipse.lsp4j.debug.TerminatedEventArguments; +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode; + +import org.netbeans.api.debugger.DebuggerEngine; +import org.netbeans.api.debugger.DebuggerInfo; +import org.netbeans.api.debugger.DebuggerManager; +import org.netbeans.api.debugger.Session; +import org.netbeans.api.debugger.jpda.AttachingDICookie; +import org.netbeans.api.debugger.jpda.DebuggerStartException; +import org.netbeans.api.debugger.jpda.JPDADebugger; +import org.netbeans.modules.java.lsp.server.debugging.DebugAdapterContext; +import org.netbeans.modules.java.lsp.server.debugging.launch.NbDebugSession; +import org.netbeans.modules.java.lsp.server.debugging.utils.ErrorUtilities; +import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient; +import org.openide.util.RequestProcessor; + +/** + * + * @author Martin Entlicher + */ +public final class NbAttachRequestHandler { + + private static final String CONNECTOR_ARG_PID = "pid"; // NOI18N + private static final String CONNECTOR_ARG_HOST = "hostname"; // NOI18N + private static final String CONNECTOR_ARG_PORT = "port"; // NOI18N + private static final String CONNECTOR_ARG_NAME = "name"; // NOI18N + // The default attributes of DebugConfiguration + private static final Set<String> CONFIG_ATTRIBUTES = new HashSet<>(Arrays.asList("type", "name", "request", "classPaths", "console")); // NOI18N + + private static final RequestProcessor RP = new RequestProcessor(AttachConfigurations.class); + + public CompletableFuture<Void> attach(Map<String, Object> attachArguments, DebugAdapterContext context) { + boolean isNative = "nativeimage".equals(attachArguments.get("type")); // NOI18N + if (isNative) { + return attachToNative(attachArguments, context); + } else { + return attachToJVM(attachArguments, context); + } + } + + private CompletableFuture<Void> attachToNative(Map<String, Object> attachArguments, DebugAdapterContext context) { + CompletableFuture<Void> resultFuture = new CompletableFuture<>(); + // TODO + ErrorUtilities.completeExceptionally(resultFuture, + "Attach to native image is not implemented yet", ResponseErrorCode.serverErrorStart); + return resultFuture; + } + + private CompletableFuture<Void> attachToJVM(Map<String, Object> attachArguments, DebugAdapterContext context) { + String name = (String) attachArguments.get("name"); // NOI18N + AttachingDICookie attachingCookie; + String connectorName; + Map<String, String> translatedArguments = new HashMap<>(); + CompletableFuture<Void> resultFuture = new CompletableFuture<>(); + switch (name) { + case AttachConfigurations.NAME_ATTACH_PROCESS: + Object pid = attachArguments.get(AttachConfigurations.PROCESS_ARG_PID); + connectorName = AttachConfigurations.CONNECTOR_PROCESS; + translatedArguments.put(AttachConfigurations.PROCESS_ARG_PID, CONNECTOR_ARG_PID); + break; + case AttachConfigurations.NAME_ATTACH_SOCKET: + connectorName = AttachConfigurations.CONNECTOR_SOCKET; + translatedArguments.put(AttachConfigurations.SOCKET_ARG_HOST, CONNECTOR_ARG_HOST); + translatedArguments.put(AttachConfigurations.SOCKET_ARG_PORT, CONNECTOR_ARG_PORT); + break; + case AttachConfigurations.NAME_ATTACH_SHMEM: + connectorName = AttachConfigurations.CONNECTOR_SHMEM; + translatedArguments.put(AttachConfigurations.SHMEM_ARG_NAME, CONNECTOR_ARG_NAME); + break; + default: + if (name.startsWith(AttachConfigurations.NAME_ATTACH_BY)) { + connectorName = name.substring(AttachConfigurations.NAME_ATTACH_BY.length()); + } else { + ErrorUtilities.completeExceptionally(resultFuture, + "Invalid connector name: " + name, + ResponseErrorCode.serverErrorStart); + connectorName = null; + } + } + if (connectorName != null) { + context.setDebugMode(true); + RP.post(() -> attachTo(connectorName, attachArguments, translatedArguments, context, resultFuture)); + } else { + assert resultFuture.isCompletedExceptionally(); + } + return resultFuture; + } + + private void attachTo(String connectorName, Map<String, Object> arguments, Map<String, String> translatedArguments, DebugAdapterContext context, CompletableFuture<Void> resultFuture) { + VirtualMachineManager vmm = Bootstrap.virtualMachineManager (); + List<AttachingConnector> attachingConnectors = vmm.attachingConnectors(); + for (AttachingConnector connector : attachingConnectors) { + if (connector.name().equals(connectorName)) { + Map<String, Argument> args = connector.defaultArguments(); + for (String argName : arguments.keySet()) { + if (CONFIG_ATTRIBUTES.contains(argName) || argName.startsWith("__")) { + continue; + } + String argNameTranslated = translatedArguments.getOrDefault(argName, argName); + Argument arg = args.get(argNameTranslated); + if (arg == null) { + ErrorUtilities.completeExceptionally(resultFuture, + "Argument " + argNameTranslated + " of " + connectorName + " was not found.", + ResponseErrorCode.serverErrorStart); + return ; + } + String value = arguments.get(argName).toString(); + if (!arg.isValid(value)) { + ErrorUtilities.completeExceptionally(resultFuture, + "Invalid value of " + argName + ": " + value, + ResponseErrorCode.serverErrorStart); + return ; + } + arg.setValue(value); + } + AttachingDICookie attachingCookie = AttachingDICookie.create(connector, args); + resultFuture.complete(null); + startAttaching(attachingCookie, context); + return ; + } + } + ErrorUtilities.completeExceptionally(resultFuture, + "Connector " + connectorName + " was not found.", + ResponseErrorCode.serverErrorStart); + } + + private void startAttaching(AttachingDICookie attachingCookie, DebugAdapterContext context) { + DebuggerEngine[] es = DebuggerManager.getDebuggerManager ().startDebugging( + DebuggerInfo.create(AttachingDICookie.ID, new Object [] { attachingCookie }) + ); + if (es.length > 0) { + JPDADebugger debugger = es[0].lookupFirst(null, JPDADebugger.class); + if (debugger != null) { + Session session = es[0].lookupFirst(null, Session.class); + NbDebugSession debugSession = new NbDebugSession(session); + context.setDebugSession(debugSession); + AtomicBoolean finished = new AtomicBoolean(false); + debugger.addPropertyChangeListener(JPDADebugger.PROP_STATE, new PropertyChangeListener() { + private final AtomicBoolean initialized = new AtomicBoolean(false); Review comment: field seems to be unused. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
