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