annotate agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java @ 8103:5ed317b25e23

7165259: Remove BugSpot Reviewed-by: coleenp, mgronlun
author sla
date Fri, 22 Feb 2013 10:03:02 +0100
parents c18cbe5936b8
children 38ea2efa32a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.tools;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.PrintStream;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.util.Hashtable;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // generic command line or GUI tool.
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // override run & code main as shown below.
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 public abstract class Tool implements Runnable {
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
37 private HotSpotAgent agent;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
38 private int debugeeType;
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 // debugeeType is one of constants below
a61af66fc99e Initial load
duke
parents:
diff changeset
41 protected static final int DEBUGEE_PID = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 protected static final int DEBUGEE_CORE = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 protected static final int DEBUGEE_REMOTE = 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 return getClass().getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
47 }
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 protected boolean needsJavaPrefix() {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 }
a61af66fc99e Initial load
duke
parents:
diff changeset
52
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
53 protected void setAgent(HotSpotAgent a) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
54 agent = a;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 protected void setDebugeeType(int dt) {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 debugeeType = dt;
a61af66fc99e Initial load
duke
parents:
diff changeset
59 }
a61af66fc99e Initial load
duke
parents:
diff changeset
60
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
61 protected HotSpotAgent getAgent() {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
62 return agent;
a61af66fc99e Initial load
duke
parents:
diff changeset
63 }
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 protected int getDebugeeType() {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 return debugeeType;
a61af66fc99e Initial load
duke
parents:
diff changeset
67 }
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 protected void printUsage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 String name = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 if (needsJavaPrefix()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 name = "java " + getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 name = getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76 System.out.println("Usage: " + name + " [option] <pid>");
a61af66fc99e Initial load
duke
parents:
diff changeset
77 System.out.println("\t\t(to connect to a live java process)");
a61af66fc99e Initial load
duke
parents:
diff changeset
78 System.out.println(" or " + name + " [option] <executable> <core>");
a61af66fc99e Initial load
duke
parents:
diff changeset
79 System.out.println("\t\t(to connect to a core file)");
a61af66fc99e Initial load
duke
parents:
diff changeset
80 System.out.println(" or " + name + " [option] [server_id@]<remote server IP or hostname>");
a61af66fc99e Initial load
duke
parents:
diff changeset
81 System.out.println("\t\t(to connect to a remote debug server)");
a61af66fc99e Initial load
duke
parents:
diff changeset
82 System.out.println();
a61af66fc99e Initial load
duke
parents:
diff changeset
83 System.out.println("where option must be one of:");
a61af66fc99e Initial load
duke
parents:
diff changeset
84 printFlagsUsage();
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 protected void printFlagsUsage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 System.out.println(" -h | -help\tto print this help message");
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90
a61af66fc99e Initial load
duke
parents:
diff changeset
91 protected void usage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 printUsage();
a61af66fc99e Initial load
duke
parents:
diff changeset
93 System.exit(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
97 Derived class main should be of the following form:
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 <derived class> obj = new <derived class>;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 obj.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
102 }
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 */
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 protected void stop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if (agent != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 agent.detach();
a61af66fc99e Initial load
duke
parents:
diff changeset
109 System.exit(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 protected void start(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 if ((args.length < 1) || (args.length > 2)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // Attempt to handle -h or -help or some invalid flag
a61af66fc99e Initial load
duke
parents:
diff changeset
119 if (args[0].startsWith("-")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 PrintStream err = System.err;
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125 int pid = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
126 String coreFileName = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
127 String executableName = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
128 String remoteServer = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 switch (args.length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 case 1:
a61af66fc99e Initial load
duke
parents:
diff changeset
132 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 pid = Integer.parseInt(args[0]);
a61af66fc99e Initial load
duke
parents:
diff changeset
134 debugeeType = DEBUGEE_PID;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 } catch (NumberFormatException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // try remote server
a61af66fc99e Initial load
duke
parents:
diff changeset
137 remoteServer = args[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
138 debugeeType = DEBUGEE_REMOTE;
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 case 2:
a61af66fc99e Initial load
duke
parents:
diff changeset
143 executableName = args[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
144 coreFileName = args[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
145 debugeeType = DEBUGEE_CORE;
a61af66fc99e Initial load
duke
parents:
diff changeset
146 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
147
a61af66fc99e Initial load
duke
parents:
diff changeset
148 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
149 usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
152 agent = new HotSpotAgent();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
153 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 switch (debugeeType) {
a61af66fc99e Initial load
duke
parents:
diff changeset
155 case DEBUGEE_PID:
a61af66fc99e Initial load
duke
parents:
diff changeset
156 err.println("Attaching to process ID " + pid + ", please wait...");
a61af66fc99e Initial load
duke
parents:
diff changeset
157 agent.attach(pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 case DEBUGEE_CORE:
a61af66fc99e Initial load
duke
parents:
diff changeset
161 err.println("Attaching to core " + coreFileName +
a61af66fc99e Initial load
duke
parents:
diff changeset
162 " from executable " + executableName + ", please wait...");
a61af66fc99e Initial load
duke
parents:
diff changeset
163 agent.attach(executableName, coreFileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
165
a61af66fc99e Initial load
duke
parents:
diff changeset
166 case DEBUGEE_REMOTE:
a61af66fc99e Initial load
duke
parents:
diff changeset
167 err.println("Attaching to remote server " + remoteServer + ", please wait...");
a61af66fc99e Initial load
duke
parents:
diff changeset
168 agent.attach(remoteServer);
a61af66fc99e Initial load
duke
parents:
diff changeset
169 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172 catch (DebuggerException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 switch (debugeeType) {
a61af66fc99e Initial load
duke
parents:
diff changeset
174 case DEBUGEE_PID:
a61af66fc99e Initial load
duke
parents:
diff changeset
175 err.print("Error attaching to process: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
176 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
177
a61af66fc99e Initial load
duke
parents:
diff changeset
178 case DEBUGEE_CORE:
a61af66fc99e Initial load
duke
parents:
diff changeset
179 err.print("Error attaching to core file: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
180 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 case DEBUGEE_REMOTE:
a61af66fc99e Initial load
duke
parents:
diff changeset
183 err.print("Error attaching to remote server: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
184 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186 if (e.getMessage() != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
187 err.print(e.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
188 }
a61af66fc99e Initial load
duke
parents:
diff changeset
189 err.println();
a61af66fc99e Initial load
duke
parents:
diff changeset
190 System.exit(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 err.println("Debugger attached successfully.");
a61af66fc99e Initial load
duke
parents:
diff changeset
194
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
195 VM vm = VM.getVM();
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
196 if (vm.isCore()) {
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
197 err.println("Core build detected.");
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
198 } else if (vm.isClientCompiler()) {
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
199 err.println("Client compiler detected.");
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
200 } else if (vm.isServerCompiler()) {
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
201 err.println("Server compiler detected.");
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
202 } else {
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
203 throw new RuntimeException("Fatal error: "
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
204 + "should have been able to detect core/C1/C2 build");
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
205 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
206
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
207 String version = vm.getVMRelease();
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
208 if (version != null) {
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
209 err.print("JVM version is ");
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
210 err.println(version);
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
211 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
212
8103
5ed317b25e23 7165259: Remove BugSpot
sla
parents: 1552
diff changeset
213 run();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }