duke@435: /* duke@435: * Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: import com.sun.jdi.*; duke@435: import com.sun.jdi.connect.*; duke@435: duke@435: import java.util.Map; duke@435: import java.util.List; duke@435: import java.util.Iterator; duke@435: import java.io.IOException; duke@435: duke@435: public class sagclient { duke@435: static AttachingConnector myCoreConn; duke@435: static AttachingConnector myPIDConn; duke@435: static AttachingConnector myDbgSvrConn; duke@435: static VirtualMachine vm; duke@435: static VirtualMachineManager vmmgr; duke@435: duke@435: public static void println(String msg) { duke@435: System.out.println("jj: " + msg); duke@435: } duke@435: duke@435: duke@435: public static void main(String args[]) { duke@435: vmmgr = Bootstrap.virtualMachineManager(); duke@435: List attachingConnectors = vmmgr.attachingConnectors(); duke@435: if (attachingConnectors.isEmpty()) { duke@435: System.err.println( "ERROR: No attaching connectors"); duke@435: return; duke@435: } duke@435: Iterator myIt = attachingConnectors.iterator(); duke@435: while (myIt.hasNext()) { duke@435: AttachingConnector tmpCon = (AttachingConnector)myIt.next(); duke@435: if (tmpCon.name().equals( duke@435: "sun.jvm.hotspot.jdi.SACoreAttachingConnector")) { duke@435: myCoreConn = tmpCon; duke@435: } else if (tmpCon.name().equals( duke@435: "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) { duke@435: myPIDConn = tmpCon; duke@435: } else if (tmpCon.name().equals( duke@435: "sun.jvm.hotspot.jdi.SADebugServerAttachingConnector")) { duke@435: myDbgSvrConn = tmpCon; duke@435: } duke@435: } duke@435: String execPath = null; duke@435: String pidText = null; duke@435: String coreFilename = null; duke@435: String debugServer = null; duke@435: int pid = 0; duke@435: switch (args.length) { duke@435: case (0): duke@435: break; duke@435: case (1): duke@435: // If all numbers, it is a PID to attach to duke@435: // Else, it is a pathname to a .../bin/java for a core file. duke@435: try { duke@435: pidText = args[0]; duke@435: pid = Integer.parseInt(pidText); duke@435: System.out.println( "pid: " + pid); duke@435: vm = attachPID(pid); duke@435: } catch (NumberFormatException e) { duke@435: System.out.println("trying remote server .."); duke@435: debugServer = args[0]; duke@435: System.out.println( "remote server: " + debugServer); duke@435: vm = attachDebugServer(debugServer); duke@435: } duke@435: break; duke@435: duke@435: case (2): duke@435: execPath = args[0]; duke@435: coreFilename = args[1]; duke@435: System.out.println( "jdk: " + execPath); duke@435: System.out.println( "core: " + coreFilename); duke@435: vm = attachCore(coreFilename, execPath); duke@435: break; duke@435: } duke@435: duke@435: duke@435: if (vm != null) { duke@435: System.out.println("sagclient: attached ok!"); duke@435: sagdoit mine = new sagdoit(vm); duke@435: mine.doAll(); duke@435: vm.dispose(); duke@435: } duke@435: } duke@435: duke@435: private static VirtualMachine attachCore(String coreFilename, String execPath) { duke@435: Map connArgs = myCoreConn.defaultArguments(); duke@435: System.out.println("connArgs = " + connArgs); duke@435: VirtualMachine vm; duke@435: Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("core"); duke@435: connArg.setValue(coreFilename); duke@435: duke@435: connArg = (Connector.StringArgument)connArgs.get("javaExecutable"); duke@435: connArg.setValue(execPath); duke@435: try { duke@435: vm = myCoreConn.attach(connArgs); duke@435: } catch (IOException ee) { duke@435: System.err.println("ERROR: myCoreConn.attach got IO Exception:" + ee); duke@435: vm = null; duke@435: } catch (IllegalConnectorArgumentsException ee) { duke@435: System.err.println("ERROR: myCoreConn.attach got illegal args exception:" + ee); duke@435: vm = null; duke@435: } duke@435: return vm; duke@435: } duke@435: duke@435: private static VirtualMachine attachPID(int pid) { duke@435: Map connArgs = myPIDConn.defaultArguments(); duke@435: System.out.println("connArgs = " + connArgs); duke@435: VirtualMachine vm; duke@435: Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid"); duke@435: connArg.setValue(Integer.toString(pid)); duke@435: duke@435: try { duke@435: vm = myPIDConn.attach(connArgs); duke@435: } catch (IOException ee) { duke@435: System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee); duke@435: vm = null; duke@435: } catch (IllegalConnectorArgumentsException ee) { duke@435: System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee); duke@435: vm = null; duke@435: } duke@435: return vm; duke@435: } duke@435: duke@435: duke@435: private static VirtualMachine attachDebugServer(String debugServer) { duke@435: Map connArgs = myDbgSvrConn.defaultArguments(); duke@435: System.out.println("connArgs = " + connArgs); duke@435: VirtualMachine vm; duke@435: Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("debugServerName"); duke@435: connArg.setValue(debugServer); duke@435: duke@435: try { duke@435: vm = myDbgSvrConn.attach(connArgs); duke@435: } catch (IOException ee) { duke@435: System.err.println("ERROR: myDbgSvrConn.attach got IO Exception:" + ee); duke@435: vm = null; duke@435: } catch (IllegalConnectorArgumentsException ee) { duke@435: System.err.println("ERROR: myDbgSvrConn.attach got illegal args exception:" + ee); duke@435: vm = null; duke@435: } duke@435: return vm; duke@435: } duke@435: }