2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
/*
|
|
28 |
* The contents of this file are subject to the Sun Public License
|
|
29 |
* Version 1.0 (the "License"); you may not use this file except in
|
|
30 |
* compliance with the License. A copy of the License is available at
|
|
31 |
* http://www.sun.com/, and in the file LICENSE.html in the
|
|
32 |
* doc directory.
|
|
33 |
*
|
|
34 |
* The Original Code is HAT. The Initial Developer of the
|
|
35 |
* Original Code is Bill Foote, with contributions from others
|
|
36 |
* at JavaSoft/Sun. Portions created by Bill Foote and others
|
|
37 |
* at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved.
|
|
38 |
*/
|
|
39 |
|
|
40 |
package com.sun.tools.hat;
|
|
41 |
|
|
42 |
import java.io.IOException;
|
|
43 |
import java.io.File;
|
|
44 |
|
|
45 |
import com.sun.tools.hat.internal.model.Snapshot;
|
|
46 |
import com.sun.tools.hat.internal.model.ReachableExcludesImpl;
|
|
47 |
import com.sun.tools.hat.internal.server.QueryListener;
|
|
48 |
|
|
49 |
/**
|
|
50 |
*
|
|
51 |
* @author Bill Foote
|
|
52 |
*/
|
|
53 |
|
|
54 |
|
|
55 |
public class Main {
|
|
56 |
|
|
57 |
private static String VERSION_STRING = "jhat version 2.0";
|
|
58 |
|
|
59 |
private static void usage(String message) {
|
|
60 |
if ( message != null ) {
|
|
61 |
System.err.println("ERROR: " + message);
|
|
62 |
}
|
|
63 |
System.err.println("Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>");
|
|
64 |
System.err.println();
|
|
65 |
System.err.println("\t-J<flag> Pass <flag> directly to the runtime system. For");
|
|
66 |
System.err.println("\t\t\t example, -J-mx512m to use a maximum heap size of 512MB");
|
|
67 |
System.err.println("\t-stack false: Turn off tracking object allocation call stack.");
|
|
68 |
System.err.println("\t-refs false: Turn off tracking of references to objects");
|
|
69 |
System.err.println("\t-port <port>: Set the port for the HTTP server. Defaults to 7000");
|
|
70 |
System.err.println("\t-exclude <file>: Specify a file that lists data members that should");
|
|
71 |
System.err.println("\t\t\t be excluded from the reachableFrom query.");
|
|
72 |
System.err.println("\t-baseline <file>: Specify a baseline object dump. Objects in");
|
|
73 |
System.err.println("\t\t\t both heap dumps with the same ID and same class will");
|
|
74 |
System.err.println("\t\t\t be marked as not being \"new\".");
|
|
75 |
System.err.println("\t-debug <int>: Set debug level.");
|
|
76 |
System.err.println("\t\t\t 0: No debug output");
|
|
77 |
System.err.println("\t\t\t 1: Debug hprof file parsing");
|
|
78 |
System.err.println("\t\t\t 2: Debug hprof file parsing, no server");
|
|
79 |
System.err.println("\t-version Report version number");
|
|
80 |
System.err.println("\t-h|-help Print this help and exit");
|
|
81 |
System.err.println("\t<file> The file to read");
|
|
82 |
System.err.println();
|
|
83 |
System.err.println("For a dump file that contains multiple heap dumps,");
|
|
84 |
System.err.println("you may specify which dump in the file");
|
|
85 |
System.err.println("by appending \"#<number>\" to the file name, i.e. \"foo.hprof#3\".");
|
|
86 |
System.err.println();
|
|
87 |
System.err.println("All boolean options default to \"true\"");
|
|
88 |
System.exit(1);
|
|
89 |
}
|
|
90 |
|
|
91 |
//
|
|
92 |
// Convert s to a boolean. If it's invalid, abort the program.
|
|
93 |
//
|
|
94 |
private static boolean booleanValue(String s) {
|
|
95 |
if ("true".equalsIgnoreCase(s)) {
|
|
96 |
return true;
|
|
97 |
} else if ("false".equalsIgnoreCase(s)) {
|
|
98 |
return false;
|
|
99 |
} else {
|
|
100 |
usage("Boolean value must be true or false");
|
|
101 |
return false; // Never happens
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
public static void main(String[] args) {
|
|
106 |
if (args.length < 1) {
|
|
107 |
usage("No arguments supplied");
|
|
108 |
}
|
|
109 |
|
|
110 |
boolean parseonly = false;
|
|
111 |
int portNumber = 7000;
|
|
112 |
boolean callStack = true;
|
|
113 |
boolean calculateRefs = true;
|
|
114 |
String baselineDump = null;
|
|
115 |
String excludeFileName = null;
|
|
116 |
int debugLevel = 0;
|
|
117 |
for (int i = 0; ; i += 2) {
|
|
118 |
if (i > (args.length - 1)) {
|
|
119 |
usage("Option parsing error");
|
|
120 |
}
|
|
121 |
if ("-version".equals(args[i])) {
|
|
122 |
System.out.print(VERSION_STRING);
|
|
123 |
System.out.println(" (java version " + System.getProperty("java.version") + ")");
|
|
124 |
System.exit(0);
|
|
125 |
}
|
|
126 |
|
|
127 |
if ("-h".equals(args[i]) || "-help".equals(args[i])) {
|
|
128 |
usage(null);
|
|
129 |
}
|
|
130 |
|
|
131 |
if (i == (args.length - 1)) {
|
|
132 |
break;
|
|
133 |
}
|
|
134 |
String key = args[i];
|
|
135 |
String value = args[i+1];
|
|
136 |
if ("-stack".equals(key)) {
|
|
137 |
callStack = booleanValue(value);
|
|
138 |
} else if ("-refs".equals(key)) {
|
|
139 |
calculateRefs = booleanValue(value);
|
|
140 |
} else if ("-port".equals(key)) {
|
|
141 |
portNumber = Integer.parseInt(value, 10);
|
|
142 |
} else if ("-exclude".equals(key)) {
|
|
143 |
excludeFileName = value;
|
|
144 |
} else if ("-baseline".equals(key)) {
|
|
145 |
baselineDump = value;
|
|
146 |
} else if ("-debug".equals(key)) {
|
|
147 |
debugLevel = Integer.parseInt(value, 10);
|
|
148 |
} else if ("-parseonly".equals(key)) {
|
|
149 |
// Undocumented option. To be used for testing purpose only
|
|
150 |
parseonly = booleanValue(value);
|
|
151 |
}
|
|
152 |
}
|
|
153 |
String fileName = args[args.length - 1];
|
|
154 |
Snapshot model = null;
|
|
155 |
File excludeFile = null;
|
|
156 |
if (excludeFileName != null) {
|
|
157 |
excludeFile = new File(excludeFileName);
|
|
158 |
if (!excludeFile.exists()) {
|
|
159 |
System.out.println("Exclude file " + excludeFile
|
|
160 |
+ " does not exist. Aborting.");
|
|
161 |
System.exit(1);
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
System.out.println("Reading from " + fileName + "...");
|
|
166 |
try {
|
|
167 |
model = com.sun.tools.hat.internal.parser.Reader.readFile(fileName, callStack, debugLevel);
|
|
168 |
} catch (IOException ex) {
|
|
169 |
ex.printStackTrace();
|
|
170 |
System.exit(1);
|
|
171 |
} catch (RuntimeException ex) {
|
|
172 |
ex.printStackTrace();
|
|
173 |
System.exit(1);
|
|
174 |
}
|
|
175 |
System.out.println("Snapshot read, resolving...");
|
|
176 |
model.resolve(calculateRefs);
|
|
177 |
System.out.println("Snapshot resolved.");
|
|
178 |
|
|
179 |
if (excludeFile != null) {
|
|
180 |
model.setReachableExcludes(new ReachableExcludesImpl(excludeFile));
|
|
181 |
}
|
|
182 |
|
|
183 |
if (baselineDump != null) {
|
|
184 |
System.out.println("Reading baseline snapshot...");
|
|
185 |
Snapshot baseline = null;
|
|
186 |
try {
|
|
187 |
baseline = com.sun.tools.hat.internal.parser.Reader.readFile(baselineDump, false,
|
|
188 |
debugLevel);
|
|
189 |
} catch (IOException ex) {
|
|
190 |
ex.printStackTrace();
|
|
191 |
System.exit(1);
|
|
192 |
} catch (RuntimeException ex) {
|
|
193 |
ex.printStackTrace();
|
|
194 |
System.exit(1);
|
|
195 |
}
|
|
196 |
baseline.resolve(false);
|
|
197 |
System.out.println("Discovering new objects...");
|
|
198 |
model.markNewRelativeTo(baseline);
|
|
199 |
baseline = null; // Guard against conservative GC
|
|
200 |
}
|
|
201 |
if ( debugLevel == 2 ) {
|
|
202 |
System.out.println("No server, -debug 2 was used.");
|
|
203 |
System.exit(0);
|
|
204 |
}
|
|
205 |
|
|
206 |
if (parseonly) {
|
|
207 |
// do not start web server.
|
|
208 |
System.out.println("-parseonly is true, exiting..");
|
|
209 |
System.exit(0);
|
|
210 |
}
|
|
211 |
|
|
212 |
QueryListener listener = new QueryListener(portNumber);
|
|
213 |
listener.setModel(model);
|
|
214 |
Thread t = new Thread(listener, "Query Listener");
|
|
215 |
t.setPriority(Thread.NORM_PRIORITY+1);
|
|
216 |
t.start();
|
|
217 |
System.out.println("Started HTTP server on port " + portNumber);
|
|
218 |
System.out.println("Server is ready.");
|
|
219 |
}
|
|
220 |
}
|