annotate agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java @ 6972:bd7a7ce2e264

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