1 /*
   2  * Copyright (c) 2005, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.tools;
  26 
  27 import sun.jvm.hotspot.utilities.HeapHprofBinWriter;
  28 import sun.jvm.hotspot.debugger.JVMDebugger;
  29 import java.io.IOException;
  30 
  31 /*
  32  * This tool is used by the JDK jmap utility to dump the heap of the target
  33  * process/core as a HPROF binary file. It can also be used as a standalone
  34  * tool if required.
  35  */
  36 public class HeapDumper extends Tool {
  37 
  38     private static String DEFAULT_DUMP_FILE = "heap.bin";
  39 
  40     private String dumpFile;
  41 
  42     public HeapDumper(String dumpFile) {
  43         this.dumpFile = dumpFile;
  44     }
  45 
  46     public HeapDumper(String dumpFile, JVMDebugger d) {
  47         super(d);
  48         this.dumpFile = dumpFile;
  49     }
  50 
  51     protected void printFlagsUsage() {
  52         System.out.println("    <no option>\tto dump heap to " +
  53             DEFAULT_DUMP_FILE);
  54         System.out.println("    -f <file>\tto dump heap to <file>");
  55         super.printFlagsUsage();
  56     }
  57 
  58     // use HeapHprofBinWriter to write the heap dump
  59     public void run() {
  60         System.out.println("Dumping heap to " + dumpFile + " ...");
  61         try {
  62             new HeapHprofBinWriter().write(dumpFile);
  63             System.out.println("Heap dump file created");
  64         } catch (IOException ioe) {
  65             System.err.println(ioe.getMessage());
  66         }
  67     }
  68 
  69     // JDK jmap utility will always invoke this tool as:
  70     //   HeapDumper -f <file> <args...>
  71     public static void main(String args[]) {
  72         String file = DEFAULT_DUMP_FILE;
  73         if (args.length > 2) {
  74             if (args[0].equals("-f")) {
  75                 file = args[1];
  76                 String[] newargs = new String[args.length-2];
  77                 System.arraycopy(args, 2, newargs, 0, args.length-2);
  78                 args = newargs;
  79             }
  80         }
  81 
  82         HeapDumper dumper = new HeapDumper(file);
  83         dumper.start(args);
  84         dumper.stop();
  85     }
  86 
  87 }