|
1 /* |
|
2 * Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 import com.sun.jdi.*; |
|
26 import com.sun.jdi.connect.*; |
|
27 |
|
28 import java.util.Map; |
|
29 import java.util.List; |
|
30 import java.util.Iterator; |
|
31 import java.io.IOException; |
|
32 |
|
33 public class sagclient { |
|
34 static AttachingConnector myCoreConn; |
|
35 static AttachingConnector myPIDConn; |
|
36 static AttachingConnector myDbgSvrConn; |
|
37 static VirtualMachine vm; |
|
38 static VirtualMachineManager vmmgr; |
|
39 |
|
40 public static void println(String msg) { |
|
41 System.out.println("jj: " + msg); |
|
42 } |
|
43 |
|
44 |
|
45 public static void main(String args[]) { |
|
46 vmmgr = Bootstrap.virtualMachineManager(); |
|
47 List attachingConnectors = vmmgr.attachingConnectors(); |
|
48 if (attachingConnectors.isEmpty()) { |
|
49 System.err.println( "ERROR: No attaching connectors"); |
|
50 return; |
|
51 } |
|
52 Iterator myIt = attachingConnectors.iterator(); |
|
53 while (myIt.hasNext()) { |
|
54 AttachingConnector tmpCon = (AttachingConnector)myIt.next(); |
|
55 if (tmpCon.name().equals( |
|
56 "sun.jvm.hotspot.jdi.SACoreAttachingConnector")) { |
|
57 myCoreConn = tmpCon; |
|
58 } else if (tmpCon.name().equals( |
|
59 "sun.jvm.hotspot.jdi.SAPIDAttachingConnector")) { |
|
60 myPIDConn = tmpCon; |
|
61 } else if (tmpCon.name().equals( |
|
62 "sun.jvm.hotspot.jdi.SADebugServerAttachingConnector")) { |
|
63 myDbgSvrConn = tmpCon; |
|
64 } |
|
65 } |
|
66 String execPath = null; |
|
67 String pidText = null; |
|
68 String coreFilename = null; |
|
69 String debugServer = null; |
|
70 int pid = 0; |
|
71 switch (args.length) { |
|
72 case (0): |
|
73 break; |
|
74 case (1): |
|
75 // If all numbers, it is a PID to attach to |
|
76 // Else, it is a pathname to a .../bin/java for a core file. |
|
77 try { |
|
78 pidText = args[0]; |
|
79 pid = Integer.parseInt(pidText); |
|
80 System.out.println( "pid: " + pid); |
|
81 vm = attachPID(pid); |
|
82 } catch (NumberFormatException e) { |
|
83 System.out.println("trying remote server .."); |
|
84 debugServer = args[0]; |
|
85 System.out.println( "remote server: " + debugServer); |
|
86 vm = attachDebugServer(debugServer); |
|
87 } |
|
88 break; |
|
89 |
|
90 case (2): |
|
91 execPath = args[0]; |
|
92 coreFilename = args[1]; |
|
93 System.out.println( "jdk: " + execPath); |
|
94 System.out.println( "core: " + coreFilename); |
|
95 vm = attachCore(coreFilename, execPath); |
|
96 break; |
|
97 } |
|
98 |
|
99 |
|
100 if (vm != null) { |
|
101 System.out.println("sagclient: attached ok!"); |
|
102 sagdoit mine = new sagdoit(vm); |
|
103 mine.doAll(); |
|
104 vm.dispose(); |
|
105 } |
|
106 } |
|
107 |
|
108 private static VirtualMachine attachCore(String coreFilename, String execPath) { |
|
109 Map connArgs = myCoreConn.defaultArguments(); |
|
110 System.out.println("connArgs = " + connArgs); |
|
111 VirtualMachine vm; |
|
112 Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("core"); |
|
113 connArg.setValue(coreFilename); |
|
114 |
|
115 connArg = (Connector.StringArgument)connArgs.get("javaExecutable"); |
|
116 connArg.setValue(execPath); |
|
117 try { |
|
118 vm = myCoreConn.attach(connArgs); |
|
119 } catch (IOException ee) { |
|
120 System.err.println("ERROR: myCoreConn.attach got IO Exception:" + ee); |
|
121 vm = null; |
|
122 } catch (IllegalConnectorArgumentsException ee) { |
|
123 System.err.println("ERROR: myCoreConn.attach got illegal args exception:" + ee); |
|
124 vm = null; |
|
125 } |
|
126 return vm; |
|
127 } |
|
128 |
|
129 private static VirtualMachine attachPID(int pid) { |
|
130 Map connArgs = myPIDConn.defaultArguments(); |
|
131 System.out.println("connArgs = " + connArgs); |
|
132 VirtualMachine vm; |
|
133 Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("pid"); |
|
134 connArg.setValue(Integer.toString(pid)); |
|
135 |
|
136 try { |
|
137 vm = myPIDConn.attach(connArgs); |
|
138 } catch (IOException ee) { |
|
139 System.err.println("ERROR: myPIDConn.attach got IO Exception:" + ee); |
|
140 vm = null; |
|
141 } catch (IllegalConnectorArgumentsException ee) { |
|
142 System.err.println("ERROR: myPIDConn.attach got illegal args exception:" + ee); |
|
143 vm = null; |
|
144 } |
|
145 return vm; |
|
146 } |
|
147 |
|
148 |
|
149 private static VirtualMachine attachDebugServer(String debugServer) { |
|
150 Map connArgs = myDbgSvrConn.defaultArguments(); |
|
151 System.out.println("connArgs = " + connArgs); |
|
152 VirtualMachine vm; |
|
153 Connector.StringArgument connArg = (Connector.StringArgument)connArgs.get("debugServerName"); |
|
154 connArg.setValue(debugServer); |
|
155 |
|
156 try { |
|
157 vm = myDbgSvrConn.attach(connArgs); |
|
158 } catch (IOException ee) { |
|
159 System.err.println("ERROR: myDbgSvrConn.attach got IO Exception:" + ee); |
|
160 vm = null; |
|
161 } catch (IllegalConnectorArgumentsException ee) { |
|
162 System.err.println("ERROR: myDbgSvrConn.attach got illegal args exception:" + ee); |
|
163 vm = null; |
|
164 } |
|
165 return vm; |
|
166 } |
|
167 } |