Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/test/jdi/multivm.java
38764 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324import com.sun.jdi.*;25import com.sun.jdi.connect.*;2627import java.util.Map;28import java.util.List;29import java.util.Iterator;30import java.io.IOException;3132/* This class is used to test multi VM connectivity feature of33* SA/JDI. Accepts two PIDs as arguments. Connects to first VM34*, Connects to second VM and disposes them in that order.35*/3637public class multivm {38static AttachingConnector myPIDConn;39static VirtualMachine vm1;40static VirtualMachine vm2;41static VirtualMachineManager vmmgr;4243public static void println(String msg) {44System.out.println(msg);45}4647private static void usage() {48System.err.println("Usage: java multivm <pid1> <pid2>");49System.exit(1);50}5152public static void main(String args[]) {53vmmgr = Bootstrap.virtualMachineManager();54List attachingConnectors = vmmgr.attachingConnectors();55if (attachingConnectors.isEmpty()) {56System.err.println( "ERROR: No attaching connectors");57return;58}59Iterator myIt = attachingConnectors.iterator();60while (myIt.hasNext()) {61AttachingConnector tmpCon = (AttachingConnector)myIt.next();62if (tmpCon.name().equals(63"sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {64myPIDConn = tmpCon;65break;66}67}6869int pid1 = 0, pid2 = 0;70String pidText = null;71switch (args.length) {72case (2):73try {74pidText = args[0];75pid1 = Integer.parseInt(pidText);76System.out.println( "pid1: " + pid1);77vm1 = attachPID(pid1);78pidText = args[1];79pid2 = Integer.parseInt(pidText);80System.out.println( "pid2: " + pid2);81vm2 = attachPID(pid2);82} catch (NumberFormatException e) {83println(e.getMessage());84usage();85}86break;87default:88usage();89}9091if (vm1 != null) {92System.out.println("vm1: attached ok!");93System.out.println(vm1.version());94sagdoit mine = new sagdoit(vm1);95mine.doAll();96}9798if (vm2 != null) {99System.out.println("vm2: attached ok!");100System.out.println(vm2.version());101sagdoit mine = new sagdoit(vm2);102mine.doAll();103}104105if (vm1 != null) {106vm1.dispose();107}108109if (vm2 != null) {110vm2.dispose();111}112}113114private static VirtualMachine attachPID(int pid) {115Map connArgs = myPIDConn.defaultArguments();116System.out.println("connArgs = " + connArgs);117VirtualMachine vm;118Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");119connArg.setValue(Integer.toString(pid));120121try {122vm = myPIDConn.attach(connArgs);123} catch (IOException ee) {124System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);125vm = null;126} catch (IllegalConnectorArgumentsException ee) {127System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);128vm = null;129}130return vm;131}132}133134135