1 /*
   2  * Copyright (c) 2004, 2013, 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 java.io.*;
  28 import sun.jvm.hotspot.debugger.JVMDebugger;
  29 import sun.jvm.hotspot.utilities.*;
  30 
  31 public class JMap extends Tool {
  32     public JMap(int m) {
  33         mode = m;
  34     }
  35 
  36     public JMap() {
  37         this(MODE_PMAP);
  38     }
  39 
  40     public JMap(JVMDebugger d) {
  41         super(d);
  42     }
  43 
  44     protected boolean needsJavaPrefix() {
  45         return false;
  46     }
  47 
  48     public String getName() {
  49         return "jmap";
  50     }
  51 
  52     protected String getCommandFlags() {
  53         return "-heap|-heap:format=b|-histo|-clstats|-finalizerinfo";
  54     }
  55 
  56     protected void printFlagsUsage() {
  57         System.out.println("    <no option>\tto print same info as Solaris pmap");
  58         System.out.println("    -heap\tto print java heap summary");
  59         System.out.println("    -heap:format=b\tto dump java heap in hprof binary format");
  60         System.out.println("    -histo\tto print histogram of java object heap");
  61         System.out.println("    -clstats\tto print class loader statistics");
  62         System.out.println("    -finalizerinfo\tto print information on objects awaiting finalization");
  63         super.printFlagsUsage();
  64     }
  65 
  66     public static final int MODE_HEAP_SUMMARY = 0;
  67     public static final int MODE_HISTOGRAM = 1;
  68     public static final int MODE_CLSTATS = 2;
  69     public static final int MODE_PMAP = 3;
  70     public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;
  71     public static final int MODE_HEAP_GRAPH_GXL = 5;
  72     public static final int MODE_FINALIZERINFO = 6;
  73 
  74     public void run() {
  75         Tool tool = null;
  76         switch (mode) {
  77 
  78         case MODE_HEAP_SUMMARY:
  79             tool = new HeapSummary();
  80             break;
  81 
  82         case MODE_HISTOGRAM:
  83             tool = new ObjectHistogram();
  84             break;
  85 
  86         case MODE_CLSTATS:
  87             tool = new ClassLoaderStats();
  88             break;
  89 
  90         case MODE_PMAP:
  91             tool = new PMap();
  92             break;
  93 
  94         case MODE_HEAP_GRAPH_HPROF_BIN:
  95             writeHeapHprofBin();
  96             return;
  97 
  98         case MODE_HEAP_GRAPH_GXL:
  99             writeHeapGXL();
 100             return;
 101 
 102         case MODE_FINALIZERINFO:
 103             tool = new FinalizerInfo();
 104             break;
 105 
 106         default:
 107             usage();
 108             break;
 109        }
 110 
 111        tool.setAgent(getAgent());
 112        tool.setDebugeeType(getDebugeeType());
 113        tool.run();
 114     }
 115 
 116     public static void main(String[] args) {
 117         int mode = MODE_PMAP;
 118         if (args.length > 1 ) {
 119             String modeFlag = args[0];
 120             boolean copyArgs = true;
 121             if (modeFlag.equals("-heap")) {
 122                 mode = MODE_HEAP_SUMMARY;
 123             } else if (modeFlag.equals("-histo")) {
 124                 mode = MODE_HISTOGRAM;
 125             } else if (modeFlag.equals("-clstats")) {
 126                 mode = MODE_CLSTATS;
 127             } else if (modeFlag.equals("-finalizerinfo")) {
 128                 mode = MODE_FINALIZERINFO;
 129             } else {
 130                 int index = modeFlag.indexOf("-heap:format=");
 131                 if (index != -1) {
 132                     String format = modeFlag.substring(1 + modeFlag.indexOf('='));
 133                     if (format.equals("b")) {
 134                         mode = MODE_HEAP_GRAPH_HPROF_BIN;
 135                     } else if (format.equals("x")) {
 136                         mode = MODE_HEAP_GRAPH_GXL;
 137                     } else {
 138                         System.err.println("unknown heap format:" + format);
 139 
 140                         // Exit with error status
 141                         System.exit(1);
 142                     }
 143                 } else {
 144                     copyArgs = false;
 145                 }
 146             }
 147 
 148             if (copyArgs) {
 149                 String[] newArgs = new String[args.length - 1];
 150                 for (int i = 0; i < newArgs.length; i++) {
 151                     newArgs[i] = args[i + 1];
 152                 }
 153                 args = newArgs;
 154             }
 155         }
 156 
 157         JMap jmap = new JMap(mode);
 158         jmap.execute(args);
 159     }
 160 
 161     public boolean writeHeapHprofBin(String fileName) {
 162         try {
 163             HeapGraphWriter hgw = new HeapHprofBinWriter();
 164             hgw.write(fileName);
 165             System.out.println("heap written to " + fileName);
 166             return true;
 167         } catch (IOException exp) {
 168             System.err.println(exp.getMessage());
 169             return false;
 170         }
 171     }
 172 
 173     public boolean writeHeapHprofBin() {
 174         return writeHeapHprofBin("heap.bin");
 175     }
 176 
 177     private boolean writeHeapGXL(String fileName) {
 178         try {
 179             HeapGraphWriter hgw = new HeapGXLWriter();
 180             hgw.write(fileName);
 181             System.out.println("heap written to " + fileName);
 182             return true;
 183         } catch (IOException exp) {
 184             System.err.println(exp.getMessage());
 185             return false;
 186         }
 187     }
 188 
 189     public boolean writeHeapGXL() {
 190         return writeHeapGXL("heap.xml");
 191     }
 192 
 193     private int mode;
 194 }