0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * The Original Code is HAT. The Initial Developer of the
0N/A * Original Code is Bill Foote, with contributions from others
245N/A * at JavaSoft/Sun.
0N/A */
0N/A
0N/Apackage com.sun.tools.hat;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.File;
0N/A
0N/Aimport com.sun.tools.hat.internal.model.Snapshot;
0N/Aimport com.sun.tools.hat.internal.model.ReachableExcludesImpl;
0N/Aimport com.sun.tools.hat.internal.server.QueryListener;
0N/A
0N/A/**
0N/A *
0N/A * @author Bill Foote
0N/A */
0N/A
0N/A
0N/Apublic class Main {
0N/A
0N/A private static String VERSION_STRING = "jhat version 2.0";
0N/A
0N/A private static void usage(String message) {
0N/A if ( message != null ) {
0N/A System.err.println("ERROR: " + message);
0N/A }
0N/A System.err.println("Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>");
0N/A System.err.println();
0N/A System.err.println("\t-J<flag> Pass <flag> directly to the runtime system. For");
0N/A System.err.println("\t\t\t example, -J-mx512m to use a maximum heap size of 512MB");
0N/A System.err.println("\t-stack false: Turn off tracking object allocation call stack.");
0N/A System.err.println("\t-refs false: Turn off tracking of references to objects");
0N/A System.err.println("\t-port <port>: Set the port for the HTTP server. Defaults to 7000");
0N/A System.err.println("\t-exclude <file>: Specify a file that lists data members that should");
0N/A System.err.println("\t\t\t be excluded from the reachableFrom query.");
0N/A System.err.println("\t-baseline <file>: Specify a baseline object dump. Objects in");
0N/A System.err.println("\t\t\t both heap dumps with the same ID and same class will");
0N/A System.err.println("\t\t\t be marked as not being \"new\".");
0N/A System.err.println("\t-debug <int>: Set debug level.");
0N/A System.err.println("\t\t\t 0: No debug output");
0N/A System.err.println("\t\t\t 1: Debug hprof file parsing");
0N/A System.err.println("\t\t\t 2: Debug hprof file parsing, no server");
0N/A System.err.println("\t-version Report version number");
0N/A System.err.println("\t-h|-help Print this help and exit");
0N/A System.err.println("\t<file> The file to read");
0N/A System.err.println();
0N/A System.err.println("For a dump file that contains multiple heap dumps,");
0N/A System.err.println("you may specify which dump in the file");
0N/A System.err.println("by appending \"#<number>\" to the file name, i.e. \"foo.hprof#3\".");
0N/A System.err.println();
0N/A System.err.println("All boolean options default to \"true\"");
0N/A System.exit(1);
0N/A }
0N/A
0N/A //
0N/A // Convert s to a boolean. If it's invalid, abort the program.
0N/A //
0N/A private static boolean booleanValue(String s) {
0N/A if ("true".equalsIgnoreCase(s)) {
0N/A return true;
0N/A } else if ("false".equalsIgnoreCase(s)) {
0N/A return false;
0N/A } else {
0N/A usage("Boolean value must be true or false");
0N/A return false; // Never happens
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A if (args.length < 1) {
0N/A usage("No arguments supplied");
0N/A }
0N/A
0N/A boolean parseonly = false;
0N/A int portNumber = 7000;
0N/A boolean callStack = true;
0N/A boolean calculateRefs = true;
0N/A String baselineDump = null;
0N/A String excludeFileName = null;
0N/A int debugLevel = 0;
0N/A for (int i = 0; ; i += 2) {
0N/A if (i > (args.length - 1)) {
0N/A usage("Option parsing error");
0N/A }
0N/A if ("-version".equals(args[i])) {
0N/A System.out.print(VERSION_STRING);
0N/A System.out.println(" (java version " + System.getProperty("java.version") + ")");
0N/A System.exit(0);
0N/A }
0N/A
0N/A if ("-h".equals(args[i]) || "-help".equals(args[i])) {
0N/A usage(null);
0N/A }
0N/A
0N/A if (i == (args.length - 1)) {
0N/A break;
0N/A }
0N/A String key = args[i];
0N/A String value = args[i+1];
0N/A if ("-stack".equals(key)) {
0N/A callStack = booleanValue(value);
0N/A } else if ("-refs".equals(key)) {
0N/A calculateRefs = booleanValue(value);
0N/A } else if ("-port".equals(key)) {
0N/A portNumber = Integer.parseInt(value, 10);
0N/A } else if ("-exclude".equals(key)) {
0N/A excludeFileName = value;
0N/A } else if ("-baseline".equals(key)) {
0N/A baselineDump = value;
0N/A } else if ("-debug".equals(key)) {
0N/A debugLevel = Integer.parseInt(value, 10);
0N/A } else if ("-parseonly".equals(key)) {
0N/A // Undocumented option. To be used for testing purpose only
0N/A parseonly = booleanValue(value);
0N/A }
0N/A }
0N/A String fileName = args[args.length - 1];
0N/A Snapshot model = null;
0N/A File excludeFile = null;
0N/A if (excludeFileName != null) {
0N/A excludeFile = new File(excludeFileName);
0N/A if (!excludeFile.exists()) {
0N/A System.out.println("Exclude file " + excludeFile
0N/A + " does not exist. Aborting.");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A System.out.println("Reading from " + fileName + "...");
0N/A try {
0N/A model = com.sun.tools.hat.internal.parser.Reader.readFile(fileName, callStack, debugLevel);
0N/A } catch (IOException ex) {
0N/A ex.printStackTrace();
0N/A System.exit(1);
0N/A } catch (RuntimeException ex) {
0N/A ex.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A System.out.println("Snapshot read, resolving...");
0N/A model.resolve(calculateRefs);
0N/A System.out.println("Snapshot resolved.");
0N/A
0N/A if (excludeFile != null) {
0N/A model.setReachableExcludes(new ReachableExcludesImpl(excludeFile));
0N/A }
0N/A
0N/A if (baselineDump != null) {
0N/A System.out.println("Reading baseline snapshot...");
0N/A Snapshot baseline = null;
0N/A try {
0N/A baseline = com.sun.tools.hat.internal.parser.Reader.readFile(baselineDump, false,
0N/A debugLevel);
0N/A } catch (IOException ex) {
0N/A ex.printStackTrace();
0N/A System.exit(1);
0N/A } catch (RuntimeException ex) {
0N/A ex.printStackTrace();
0N/A System.exit(1);
0N/A }
0N/A baseline.resolve(false);
0N/A System.out.println("Discovering new objects...");
0N/A model.markNewRelativeTo(baseline);
0N/A baseline = null; // Guard against conservative GC
0N/A }
0N/A if ( debugLevel == 2 ) {
0N/A System.out.println("No server, -debug 2 was used.");
0N/A System.exit(0);
0N/A }
0N/A
0N/A if (parseonly) {
0N/A // do not start web server.
0N/A System.out.println("-parseonly is true, exiting..");
0N/A System.exit(0);
0N/A }
0N/A
0N/A QueryListener listener = new QueryListener(portNumber);
0N/A listener.setModel(model);
0N/A Thread t = new Thread(listener, "Query Listener");
0N/A t.setPriority(Thread.NORM_PRIORITY+1);
0N/A t.start();
0N/A System.out.println("Started HTTP server on port " + portNumber);
0N/A System.out.println("Server is ready.");
0N/A }
0N/A}