0
|
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 package sun.jvm.hotspot.tools;
|
|
26
|
|
27 import java.io.PrintStream;
|
|
28 import java.util.Hashtable;
|
|
29 import sun.jvm.hotspot.*;
|
|
30 import sun.jvm.hotspot.bugspot.*;
|
|
31 import sun.jvm.hotspot.runtime.*;
|
|
32 import sun.jvm.hotspot.debugger.*;
|
|
33
|
|
34 // generic command line or GUI tool.
|
|
35 // override run & code main as shown below.
|
|
36
|
|
37 public abstract class Tool implements Runnable {
|
|
38 private BugSpotAgent agent;
|
|
39 private int debugeeType;
|
|
40
|
|
41 // debugeeType is one of constants below
|
|
42 protected static final int DEBUGEE_PID = 0;
|
|
43 protected static final int DEBUGEE_CORE = 1;
|
|
44 protected static final int DEBUGEE_REMOTE = 2;
|
|
45
|
|
46 public String getName() {
|
|
47 return getClass().getName();
|
|
48 }
|
|
49
|
|
50 protected boolean needsJavaPrefix() {
|
|
51 return true;
|
|
52 }
|
|
53
|
|
54 // whether this tool requires debuggee to be java process or core?
|
|
55 protected boolean requiresVM() {
|
|
56 return true;
|
|
57 }
|
|
58
|
|
59 protected void setAgent(BugSpotAgent a) {
|
|
60 agent = a;
|
|
61 }
|
|
62
|
|
63 protected void setDebugeeType(int dt) {
|
|
64 debugeeType = dt;
|
|
65 }
|
|
66
|
|
67 protected BugSpotAgent getAgent() {
|
|
68 return agent;
|
|
69 }
|
|
70
|
|
71 protected int getDebugeeType() {
|
|
72 return debugeeType;
|
|
73 }
|
|
74
|
|
75 protected void printUsage() {
|
|
76 String name = null;
|
|
77 if (needsJavaPrefix()) {
|
|
78 name = "java " + getName();
|
|
79 } else {
|
|
80 name = getName();
|
|
81 }
|
|
82 System.out.println("Usage: " + name + " [option] <pid>");
|
|
83 System.out.println("\t\t(to connect to a live java process)");
|
|
84 System.out.println(" or " + name + " [option] <executable> <core>");
|
|
85 System.out.println("\t\t(to connect to a core file)");
|
|
86 System.out.println(" or " + name + " [option] [server_id@]<remote server IP or hostname>");
|
|
87 System.out.println("\t\t(to connect to a remote debug server)");
|
|
88 System.out.println();
|
|
89 System.out.println("where option must be one of:");
|
|
90 printFlagsUsage();
|
|
91 }
|
|
92
|
|
93 protected void printFlagsUsage() {
|
|
94 System.out.println(" -h | -help\tto print this help message");
|
|
95 }
|
|
96
|
|
97 protected void usage() {
|
|
98 printUsage();
|
|
99 System.exit(1);
|
|
100 }
|
|
101
|
|
102 /*
|
|
103 Derived class main should be of the following form:
|
|
104
|
|
105 public static void main(String[] args) {
|
|
106 <derived class> obj = new <derived class>;
|
|
107 obj.start(args);
|
|
108 }
|
|
109
|
|
110 */
|
|
111
|
|
112 protected void stop() {
|
|
113 if (agent != null) {
|
|
114 agent.detach();
|
|
115 System.exit(0);
|
|
116 }
|
|
117 }
|
|
118
|
|
119 protected void start(String[] args) {
|
|
120 if ((args.length < 1) || (args.length > 2)) {
|
|
121 usage();
|
|
122 }
|
|
123
|
|
124 // Attempt to handle -h or -help or some invalid flag
|
|
125 if (args[0].startsWith("-")) {
|
|
126 usage();
|
|
127 }
|
|
128
|
|
129 PrintStream err = System.err;
|
|
130
|
|
131 int pid = 0;
|
|
132 String coreFileName = null;
|
|
133 String executableName = null;
|
|
134 String remoteServer = null;
|
|
135
|
|
136 switch (args.length) {
|
|
137 case 1:
|
|
138 try {
|
|
139 pid = Integer.parseInt(args[0]);
|
|
140 debugeeType = DEBUGEE_PID;
|
|
141 } catch (NumberFormatException e) {
|
|
142 // try remote server
|
|
143 remoteServer = args[0];
|
|
144 debugeeType = DEBUGEE_REMOTE;
|
|
145 }
|
|
146 break;
|
|
147
|
|
148 case 2:
|
|
149 executableName = args[0];
|
|
150 coreFileName = args[1];
|
|
151 debugeeType = DEBUGEE_CORE;
|
|
152 break;
|
|
153
|
|
154 default:
|
|
155 usage();
|
|
156 }
|
|
157
|
|
158 agent = new BugSpotAgent();
|
|
159 try {
|
|
160 switch (debugeeType) {
|
|
161 case DEBUGEE_PID:
|
|
162 err.println("Attaching to process ID " + pid + ", please wait...");
|
|
163 agent.attach(pid);
|
|
164 break;
|
|
165
|
|
166 case DEBUGEE_CORE:
|
|
167 err.println("Attaching to core " + coreFileName +
|
|
168 " from executable " + executableName + ", please wait...");
|
|
169 agent.attach(executableName, coreFileName);
|
|
170 break;
|
|
171
|
|
172 case DEBUGEE_REMOTE:
|
|
173 err.println("Attaching to remote server " + remoteServer + ", please wait...");
|
|
174 agent.attach(remoteServer);
|
|
175 break;
|
|
176 }
|
|
177 }
|
|
178 catch (DebuggerException e) {
|
|
179 switch (debugeeType) {
|
|
180 case DEBUGEE_PID:
|
|
181 err.print("Error attaching to process: ");
|
|
182 break;
|
|
183
|
|
184 case DEBUGEE_CORE:
|
|
185 err.print("Error attaching to core file: ");
|
|
186 break;
|
|
187
|
|
188 case DEBUGEE_REMOTE:
|
|
189 err.print("Error attaching to remote server: ");
|
|
190 break;
|
|
191 }
|
|
192 if (e.getMessage() != null) {
|
|
193 err.print(e.getMessage());
|
|
194 }
|
|
195 err.println();
|
|
196 System.exit(1);
|
|
197 }
|
|
198
|
|
199 err.println("Debugger attached successfully.");
|
|
200
|
|
201 boolean isJava = agent.isJavaMode();
|
|
202 if (isJava) {
|
|
203 VM vm = VM.getVM();
|
|
204 if (vm.isCore()) {
|
|
205 err.println("Core build detected.");
|
|
206 } else if (vm.isClientCompiler()) {
|
|
207 err.println("Client compiler detected.");
|
|
208 } else if (vm.isServerCompiler()) {
|
|
209 err.println("Server compiler detected.");
|
|
210 } else {
|
|
211 throw new RuntimeException("Fatal error: " +
|
|
212 "should have been able to detect core/C1/C2 build");
|
|
213 }
|
|
214
|
|
215 String version = vm.getVMRelease();
|
|
216 if (version != null) {
|
|
217 err.print("JVM version is ");
|
|
218 err.println(version);
|
|
219 }
|
|
220
|
|
221 run();
|
|
222 } else { // not a java process or core
|
|
223 if (requiresVM()) {
|
|
224 err.println(getName() + " requires a java VM process/core!");
|
|
225 } else {
|
|
226 run();
|
|
227 }
|
|
228 }
|
|
229 }
|
|
230 }
|