Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/test/jdi/sagclient.java
38764 views
/*1* Copyright (c) 2002, 2004, 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;3132public class sagclient {33static AttachingConnector myCoreConn;34static AttachingConnector myPIDConn;35static AttachingConnector myDbgSvrConn;36static VirtualMachine vm;37static VirtualMachineManager vmmgr;3839public static void println(String msg) {40System.out.println("jj: " + msg);41}424344public static void main(String args[]) {45vmmgr = Bootstrap.virtualMachineManager();46List attachingConnectors = vmmgr.attachingConnectors();47if (attachingConnectors.isEmpty()) {48System.err.println( "ERROR: No attaching connectors");49return;50}51Iterator myIt = attachingConnectors.iterator();52while (myIt.hasNext()) {53AttachingConnector tmpCon = (AttachingConnector)myIt.next();54if (tmpCon.name().equals(55"sun.jvm.hotspot.jdi.SACoreAttachingConnector")) {56myCoreConn = tmpCon;57} else if (tmpCon.name().equals(58"sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) {59myPIDConn = tmpCon;60} else if (tmpCon.name().equals(61"sun.jvm.hotspot.jdi.SADebugServerAttachingConnector")) {62myDbgSvrConn = tmpCon;63}64}65String execPath = null;66String pidText = null;67String coreFilename = null;68String debugServer = null;69int pid = 0;70switch (args.length) {71case (0):72break;73case (1):74// If all numbers, it is a PID to attach to75// Else, it is a pathname to a .../bin/java for a core file.76try {77pidText = args[0];78pid = Integer.parseInt(pidText);79System.out.println( "pid: " + pid);80vm = attachPID(pid);81} catch (NumberFormatException e) {82System.out.println("trying remote server ..");83debugServer = args[0];84System.out.println( "remote server: " + debugServer);85vm = attachDebugServer(debugServer);86}87break;8889case (2):90execPath = args[0];91coreFilename = args[1];92System.out.println( "jdk: " + execPath);93System.out.println( "core: " + coreFilename);94vm = attachCore(coreFilename, execPath);95break;96}979899if (vm != null) {100System.out.println("sagclient: attached ok!");101sagdoit mine = new sagdoit(vm);102mine.doAll();103vm.dispose();104}105}106107private static VirtualMachine attachCore(String coreFilename, String execPath) {108Map connArgs = myCoreConn.defaultArguments();109System.out.println("connArgs = " + connArgs);110VirtualMachine vm;111Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("core");112connArg.setValue(coreFilename);113114connArg = (Connector.StringArgument)connArgs.get("javaExecutable");115connArg.setValue(execPath);116try {117vm = myCoreConn.attach(connArgs);118} catch (IOException ee) {119System.err.println("ERROR: myCoreConn.attach got IO Exception:" + ee);120vm = null;121} catch (IllegalConnectorArgumentsException ee) {122System.err.println("ERROR: myCoreConn.attach got illegal args exception:" + ee);123vm = null;124}125return vm;126}127128private static VirtualMachine attachPID(int pid) {129Map connArgs = myPIDConn.defaultArguments();130System.out.println("connArgs = " + connArgs);131VirtualMachine vm;132Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid");133connArg.setValue(Integer.toString(pid));134135try {136vm = myPIDConn.attach(connArgs);137} catch (IOException ee) {138System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee);139vm = null;140} catch (IllegalConnectorArgumentsException ee) {141System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee);142vm = null;143}144return vm;145}146147148private static VirtualMachine attachDebugServer(String debugServer) {149Map connArgs = myDbgSvrConn.defaultArguments();150System.out.println("connArgs = " + connArgs);151VirtualMachine vm;152Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("debugServerName");153connArg.setValue(debugServer);154155try {156vm = myDbgSvrConn.attach(connArgs);157} catch (IOException ee) {158System.err.println("ERROR: myDbgSvrConn.attach got IO Exception:" + ee);159vm = null;160} catch (IllegalConnectorArgumentsException ee) {161System.err.println("ERROR: myDbgSvrConn.attach got illegal args exception:" + ee);162vm = null;163}164return vm;165}166}167168169