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