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