private VirtualMachine connect(int port) throws IOException { VirtualMachineManager manager = Bootstrap.virtualMachineManager(); // Find appropiate connector List<AttachingConnector> connectors = manager.attachingConnectors(); AttachingConnector chosenConnector = null; for (AttachingConnector c : connectors) { if (c.transport().name().equals(TRANSPORT_NAME)) { chosenConnector = c; break; } } if (chosenConnector == null) { throw new IllegalStateException("Could not find socket connector"); } // Set port argument AttachingConnector connector = chosenConnector; Map<String, Argument> defaults = connector.defaultArguments(); Argument arg = defaults.get(PORT_ARGUMENT_NAME); if (arg == null) { throw new IllegalStateException("Could not find port argument"); } arg.setValue(Integer.toString(port)); // Attach try { System.out.println("Connector arguments: " + defaults); return connector.attach(defaults); } catch (IllegalConnectorArgumentsException e) { throw new IllegalArgumentException("Illegal connector arguments", e); } }
private static void log(Connector c, Map<String, Argument> args) { LogRecord record = new LogRecord(Level.INFO, "USG_DEBUG_ATTACH_JPDA"); record.setResourceBundle(NbBundle.getBundle(ConnectPanel.class)); record.setResourceBundleName(ConnectPanel.class.getPackage().getName() + ".Bundle"); // NOI18N record.setLoggerName(USG_LOGGER.getName()); List<Object> params = new ArrayList<Object>(); params.add(c.name()); StringBuilder arguments = new StringBuilder(); for (Map.Entry argEntry : args.entrySet()) { //arguments.append(argEntry.getKey()); //arguments.append("="); arguments.append(argEntry.getValue()); arguments.append(", "); } if (arguments.length() > 2) { arguments.delete(arguments.length() - 2, arguments.length()); } params.add(arguments); record.setParameters(params.toArray(new Object[params.size()])); USG_LOGGER.log(record); }
private static void saveArgs ( Map<String, Argument> args, Connector connector ) { Map<String, Argument> defaultValues = connector.defaultArguments (); Map<String, String> argsToSave = new HashMap<String, String>(); for(String argName : args.keySet ()) { Argument value = args.get (argName); Argument defaultValue = defaultValues.get (argName); if ( value != null && value != defaultValue && !value.equals (defaultValue) ) { argsToSave.put (argName, value.value ()); } } Map m = Properties.getDefault ().getProperties ("debugger"). getMap ("connection_settings", new HashMap ()); String name = connector.name (); m.put (name, argsToSave); Properties.getDefault ().getProperties ("debugger"). setString ("last_attaching_connector", name); Properties.getDefault ().getProperties ("debugger"). setMap ("connection_settings", m); }
/** * This utility method helps to start a new JPDA debugger session. * Its implementation use {@link ListeningDICookie} and * {@link org.netbeans.api.debugger.DebuggerManager#getDebuggerManager}. * It's identical to {@link #startListening(com.sun.jdi.connect.ListeningConnector, java.util.Map, java.lang.Object[])}, * but returns the started engines. * * @param connector The listening connector * @param args The arguments * @param services The additional services, which are added to the debugger session lookup.<br/> * See {@link #listen(com.sun.jdi.connect.ListeningConnector, java.util.Map, java.lang.Object[])} for a more detailed description of this argument. * @return A non-empty array of started engines (since 2.26) * @throws DebuggerStartException when {@link org.netbeans.api.debugger.DebuggerManager#startDebugging} * returns an empty array * @since 2.26 */ public static DebuggerEngine[] startListeningAndGetEngines ( ListeningConnector connector, Map<String, ? extends Argument> args, Object[] services ) throws DebuggerStartException { Object[] s = new Object [services.length + 1]; System.arraycopy (services, 0, s, 1, services.length); s [0] = ListeningDICookie.create ( connector, args ); DebuggerEngine[] es = DebuggerManager.getDebuggerManager (). startDebugging ( DebuggerInfo.create ( ListeningDICookie.ID, s ) ); if (es.length == 0) { throw new DebuggerStartException( NbBundle.getMessage(JPDADebugger.class, "MSG_NO_DEBUGGER")); } return es; }
/** * Return the launching connector's arguments. */ static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) { Map<String,Connector.Argument> arguments = connector.defaultArguments(); Connector.Argument mainArg = (Connector.Argument)arguments.get("main"); if (mainArg == null) { throw new Error("Bad launching connector"); } mainArg.setValue(mainArgs); Connector.Argument optionsArg = (Connector.Argument)arguments.get("options"); if (optionsArg == null) { throw new Error("Bad launching connector"); } optionsArg.setValue(ARGUMENTS); return arguments; }
public PermissionDebugger attach() throws IOException, IllegalConnectorArgumentsException { VirtualMachineManager vmm = Bootstrap.virtualMachineManager(); List<AttachingConnector> connectors = vmm.attachingConnectors(); AttachingConnector connector = connectors .stream() .filter(c -> c.transport().name().equals(transport)) .findFirst() .orElseThrow( () -> new IOException(String.format("Failed to find transport %s", transport))); Map<String, Argument> map = connector.defaultArguments(); Argument portArg = map.get(PORT_KEY); portArg.setValue(port); map.put(PORT_KEY, portArg); vm = connector.attach(map); return this; }
private ILaunchConfigurationWorkingCopy getRemoteDebugConfig(IProject activeProj) throws CoreException { ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION); ILaunchConfigurationWorkingCopy config = type.newInstance(null, "Debug "+activeProj.getName()); config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, activeProj.getName()); config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, true); config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR); IVMConnector connector = JavaRuntime.getVMConnector(IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR); Map<String, Argument> def = connector.getDefaultArguments(); Map<String, String> argMap = new HashMap<String, String>(def.size()); argMap.put("hostname", getHostname(activeProj)); argMap.put("port", "8348"); WPILibJavaPlugin.logInfo(argMap.toString()); config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, argMap); return config; }
private static String getLabel(Argument a) { String label = translate (a.name()); if (label == null) { label = a.label(); } else { int amp = Mnemonics.findMnemonicAmpersand(label); if (amp >= 0) { label = label.substring(0, amp) + label.substring(amp + 1); } } return label; }
/** * This utility method helps to start a new JPDA debugger session. * Its implementation use {@link ListeningDICookie} and * {@link org.netbeans.api.debugger.DebuggerManager#getDebuggerManager}. * * @param connector The listening connector * @param args The arguments * @param services The additional services, which are added to the debugger session lookup.<br/> * It is expected, that one element in the services array is a {@link Map} with following setup properties:<br/> * <lu> * <li><code>name</code> with the value being the session name as String,</li> * <li><code>sourcepath</code> with the {@link ClassPath} value containing class path of sources used by debugger,</li> * <li><code>jdksources</code> with the {@link ClassPath} value containing class path of platform sources,</li> * <li><code>listeningCP</code> optional String representation of source class path which is in compile-on-save mode, which we listen on for artifacts changes,</li> * <li><code>baseDir</code> with the debugging project's base directory as {@link File}.</li> * </lu> */ public static JPDADebugger listen ( ListeningConnector connector, Map<String, ? extends Argument> args, Object[] services ) throws DebuggerStartException { Object[] s = new Object [services.length + 1]; System.arraycopy (services, 0, s, 1, services.length); s [0] = ListeningDICookie.create ( connector, args ); DebuggerEngine[] es = DebuggerManager.getDebuggerManager (). startDebugging ( DebuggerInfo.create ( ListeningDICookie.ID, s ) ); int i, k = es.length; for (i = 0; i < k; i++) { JPDADebugger d = es[i].lookupFirst(null, JPDADebugger.class); if (d == null) { continue; } d.waitRunning (); return d; } throw new DebuggerStartException( NbBundle.getMessage(JPDADebugger.class, "MSG_NO_DEBUGGER")); }
private AttachingDICookie ( AttachingConnector attachingConnector, Map<String,? extends Argument> args ) { this.attachingConnector = attachingConnector; this.args = args; }
/** * Creates a new instance of AttachingDICookie for given parameters. * * @param attachingConnector a connector to be used * @param args map of arguments * @return a new instance of AttachingDICookie for given parameters */ public static AttachingDICookie create ( AttachingConnector attachingConnector, Map<String,? extends Argument> args ) { return new AttachingDICookie ( attachingConnector, args ); }
/** * Returns port number. * * @return port number */ public int getPortNumber () { Argument a = args.get ("port"); if (a == null) return -1; String pn = a.value (); if (pn == null) return -1; return Integer.parseInt (pn); }
private static Map<String,? extends Argument> getArgs ( AttachingConnector attachingConnector, String hostName, int portNumber ) { Map<String,? extends Argument> args = attachingConnector.defaultArguments (); args.get ("hostname").setValue (hostName); args.get ("port").setValue ("" + portNumber); return args; }
private static Map<String,? extends Argument> getArgs ( AttachingConnector attachingConnector, String name ) { Map<String,? extends Argument> args = attachingConnector.defaultArguments (); args.get ("name").setValue (name); return args; }
private LaunchingDICookie ( LaunchingConnector launchingConnector, Map<String, ? extends Argument> args, String mainClassName, boolean suspend ) { this.launchingConnector = launchingConnector; this.args = args; this.mainClassName = mainClassName; this.suspend = suspend; }
private static Map<String, ? extends Argument> getArgs ( String commandLine, String address ) { Map<String, ? extends Argument> args = findLaunchingConnector ().defaultArguments (); args.get ("command").setValue (commandLine); args.get ("address").setValue (address); return args; }
private ListeningDICookie ( ListeningConnector listeningConnector, Map<String, ? extends Argument> args ) { this.listeningConnector = listeningConnector; this.args = args; }
/** * Creates a new instance of ListeningDICookie for given parameters. * * @param listeningConnector a instance of ListeningConnector * @param args arguments to be used * @return a new instance of ListeningDICookie for given parameters */ public static ListeningDICookie create ( ListeningConnector listeningConnector, Map<String, ? extends Argument> args ) { return new ListeningDICookie ( listeningConnector, args ); }
private static Map<String, ? extends Argument> getArgs ( ListeningConnector listeningConnector, int portNumber ) { Map<String, ? extends Argument> args = listeningConnector.defaultArguments (); args.get ("port").setValue ("" + portNumber); return args; }
private static Map<String, ? extends Argument> getArgs ( ListeningConnector listeningConnector, String name ) { Map<String, ? extends Argument> args = listeningConnector.defaultArguments (); args.get ("name").setValue (name); return args; }
public boolean setMapArguments(String arg, String value, int i){ Connector.Argument argument = (Connector.Argument)connectionSettings.get(i).get(arg); if (argument == null) { return false; } argument.setValue(value); return true; }
private static Map<String, Argument> getEditedArgs ( JTextField[] tfParams, Connector connector ) { assert SwingUtilities.isEventDispatchThread(); // 1) get default set of args Map<String, Argument> args = connector.defaultArguments (); // 2) update values from text fields int i, k = tfParams.length; for (i = 0; i < k; i++) { JTextField tf = tfParams [i]; String paramName = tf.getName (); String paramValue = tf.getText (); Argument a = args.get (paramName); while ( ((!a.isValid (paramValue)) && (!"".equals (paramValue))) || ( "".equals (paramValue) && a.mustSpecify () ) ) { NotifyDescriptor.InputLine in; String label = getLabel(a); if ( "".equals (paramValue) && a.mustSpecify ()) { in = new NotifyDescriptor.InputLine ( label, NbBundle.getMessage ( ConnectPanel.class, "CTL_Required_value_title" ) ); } else { in = new NotifyDescriptor.InputLine ( label, NbBundle.getMessage ( ConnectPanel.class, "CTL_Invalid_value_title" ) ); } if (DialogDisplayer.getDefault ().notify (in) == NotifyDescriptor.CANCEL_OPTION ) { return null; } paramValue = in.getInputText (); } a.setValue (paramValue); } return args; }
/** * Returns name of computer. * * @return name of computer */ public String getHostName () { Argument a = args.get ("hostname"); if (a == null) return null; return a.value (); }
/** * Returns shared memory block name. * * @return shared memory block name */ public String getSharedMemoryName () { Argument a = args.get ("name"); if (a == null) return null; return a.value (); }
/** * Returns command line to be used. * * @return command line to be used */ public String getCommandLine () { Argument a = args.get ("command"); if (a == null) return null; return a.value (); }
public static void main(String args[]) throws Exception { // Launch the server debuggee int port = 0; Process process = null; while (process == null) { port = Utils.getFreePort(); String address = String.valueOf(port); LaunchResult launchResult = launch(address, "Exit0"); process = launchResult.getProcess(); if (launchResult.isBindFailed()) { System.out.println("Port " + port + " already in use. Trying to restart debuggee with a new one..."); Thread.sleep(100); } else if (process == null ) { throw new RuntimeException("Unable to start debugee"); } } // Connect to the debuggee and handshake with garbage Socket s = new Socket("localhost", port); s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8")); s.close(); // Re-connect and to a partial handshake - don't disconnect s = new Socket("localhost", port); s.getOutputStream().write("JDWP-".getBytes("UTF-8")); // Attach to server debuggee and resume it so it can exit AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); Map<String, Argument> conn_args = conn.defaultArguments(); Connector.IntegerArgument port_arg = (Connector.IntegerArgument)conn_args.get("port"); port_arg.setValue(port); VirtualMachine vm = conn.attach(conn_args); // The first event is always a VMStartEvent, and it is always in // an EventSet by itself. Wait for it. EventSet evtSet = vm.eventQueue().remove(); for (Event event: evtSet) { if (event instanceof VMStartEvent) { break; } throw new RuntimeException("Test failed - debuggee did not start properly"); } vm.eventRequestManager().deleteAllBreakpoints(); vm.resume(); process.waitFor(); }
public F3VirtualMachine accept(Map<String, ? extends Argument> args) throws IOException, IllegalConnectorArgumentsException { return F3Wrapper.wrap(underlying().accept(args)); }
public String startListening(Map<String, ? extends Argument> args) throws IOException, IllegalConnectorArgumentsException { return underlying().startListening(args); }
public void stopListening(Map<String, ? extends Argument> args) throws IOException, IllegalConnectorArgumentsException { underlying().stopListening(args); }
public F3VirtualMachine launch(Map<String, ? extends Argument> args) throws IOException, IllegalConnectorArgumentsException, VMStartException { return F3Wrapper.wrap(underlying().launch(args)); }
public F3VirtualMachine attach(Map<String, ? extends Argument> args) throws IOException, IllegalConnectorArgumentsException { return F3Wrapper.wrap(underlying().attach(args)); }
public VirtualMachine attach(Map<String, ? extends Argument> args) throws IOException, IllegalConnectorArgumentsException { return F3Wrapper.wrap(underlying().attach(args)); }