Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/test/jdi/serialvm.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;313233/* This class is used to test multi VM connectivity feature of34* SA/JDI. Accepts two PIDs as arguments. Connects to first VM35*, disposes it, connects to second VM, disposes second VM.36*/373839public class serialvm {40static AttachingConnector myPIDConn;41static VirtualMachine vm1;42static VirtualMachine vm2;43static VirtualMachineManager vmmgr;4445public static void println(String msg) {46System.out.println(msg);47}4849private static void usage() {50System.err.println("Usage: java serialvm <pid1> <pid2>");51System.exit(1);52}5354public static void main(String args[]) {55vmmgr = Bootstrap.virtualMachineManager();56List attachingConnectors = vmmgr.attachingConnectors();57if (attachingConnectors.isEmpty()) {58System.err.println( "ERROR: No attaching connectors");59return;60}61Iterator myIt = attachingConnectors.iterator();62while (myIt.hasNext()) {63AttachingConnector tmpCon = (AttachingConnector)myIt.next();64if (tmpCon.name().equals(65"sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {66myPIDConn = tmpCon;67break;68}69}7071int pid1 = 0, pid2 = 0;72String pidText = null;73switch (args.length) {74case (2):75try {76pidText = args[0];77pid1 = Integer.parseInt(pidText);78System.out.println( "pid1: " + pid1);79pidText = args[1];80pid2 = Integer.parseInt(pidText);81System.out.println( "pid2: " + pid2);82} catch (NumberFormatException e) {83println(e.getMessage());84usage();85}86break;87default:88usage();89}9091// attach, dispose, attach2, dispose2 pattern92// as opposed to attach1, attach2, dispose1, dispose293vm1 = attachPID(pid1);94if (vm1 != null) {95System.out.println("vm1: attached ok!");96System.out.println(vm1.version());97sagdoit mine = new sagdoit(vm1);98mine.doAll();99}100if (vm1 != null) {101vm1.dispose();102}103104vm2 = attachPID(pid2);105if (vm2 != null) {106System.out.println("vm2: attached ok!");107System.out.println(vm2.version());108sagdoit mine = new sagdoit(vm2);109mine.doAll();110}111112113if (vm2 != null) {114vm2.dispose();115}116}117118private static VirtualMachine attachPID(int pid) {119Map connArgs = myPIDConn.defaultArguments();120System.out.println("connArgs = " + connArgs);121VirtualMachine vm;122Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");123connArg.setValue(Integer.toString(pid));124125try {126vm = myPIDConn.attach(connArgs);127} catch (IOException ee) {128System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);129vm = null;130} catch (IllegalConnectorArgumentsException ee) {131System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);132vm = null;133}134return vm;135}136}137138139