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