duke@435: /* duke@435: * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: import com.sun.jdi.*; duke@435: import com.sun.jdi.connect.*; duke@435: duke@435: import java.util.Map; duke@435: import java.util.List; duke@435: import java.util.Iterator; duke@435: import java.io.IOException; duke@435: duke@435: /* This class is used to test multi VM connectivity feature of duke@435: * SA/JDI. Accepts two PIDs as arguments. Connects to first VM duke@435: *, Connects to second VM and disposes them in that order. duke@435: */ duke@435: duke@435: public class multivm { duke@435: static AttachingConnector myPIDConn; duke@435: static VirtualMachine vm1; duke@435: static VirtualMachine vm2; duke@435: static VirtualMachineManager vmmgr; duke@435: duke@435: public static void println(String msg) { duke@435: System.out.println(msg); duke@435: } duke@435: duke@435: private static void usage() { duke@435: System.err.println("Usage: java multivm "); duke@435: System.exit(1); duke@435: } duke@435: duke@435: public static void main(String args[]) { duke@435: vmmgr = Bootstrap.virtualMachineManager(); duke@435: List attachingConnectors = vmmgr.attachingConnectors(); duke@435: if (attachingConnectors.isEmpty()) { duke@435: System.err.println( "ERROR: No attaching connectors"); duke@435: return; duke@435: } duke@435: Iterator myIt = attachingConnectors.iterator(); duke@435: while (myIt.hasNext()) { duke@435: AttachingConnector tmpCon = (AttachingConnector)myIt.next(); duke@435: if (tmpCon.name().equals( duke@435: "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) { duke@435: myPIDConn = tmpCon; duke@435: break; duke@435: } duke@435: } duke@435: duke@435: int pid1 = 0, pid2 = 0; duke@435: String pidText = null; duke@435: switch (args.length) { duke@435: case (2): duke@435: try { duke@435: pidText = args[0]; duke@435: pid1 = Integer.parseInt(pidText); duke@435: System.out.println( "pid1: " + pid1); duke@435: vm1 = attachPID(pid1); duke@435: pidText = args[1]; duke@435: pid2 = Integer.parseInt(pidText); duke@435: System.out.println( "pid2: " + pid2); duke@435: vm2 = attachPID(pid2); duke@435: } catch (NumberFormatException e) { duke@435: println(e.getMessage()); duke@435: usage(); duke@435: } duke@435: break; duke@435: default: duke@435: usage(); duke@435: } duke@435: duke@435: if (vm1 != null) { duke@435: System.out.println("vm1: attached ok!"); duke@435: System.out.println(vm1.version()); duke@435: sagdoit mine = new sagdoit(vm1); duke@435: mine.doAll(); duke@435: } duke@435: duke@435: if (vm2 != null) { duke@435: System.out.println("vm2: attached ok!"); duke@435: System.out.println(vm2.version()); duke@435: sagdoit mine = new sagdoit(vm2); duke@435: mine.doAll(); duke@435: } duke@435: duke@435: if (vm1 != null) { duke@435: vm1.dispose(); duke@435: } duke@435: duke@435: if (vm2 != null) { duke@435: vm2.dispose(); duke@435: } duke@435: } duke@435: duke@435: private static VirtualMachine attachPID(int pid) { duke@435: Map connArgs = myPIDConn.defaultArguments(); duke@435: System.out.println("connArgs = " + connArgs); duke@435: VirtualMachine vm; duke@435: Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid"); duke@435: connArg.setValue(Integer.toString(pid)); duke@435: duke@435: try { duke@435: vm = myPIDConn.attach(connArgs); duke@435: } catch (IOException ee) { duke@435: System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee); duke@435: vm = null; duke@435: } catch (IllegalConnectorArgumentsException ee) { duke@435: System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee); duke@435: vm = null; duke@435: } duke@435: return vm; duke@435: } duke@435: }