1 /* 2 * Copyright (c) 2004, 20013, 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("-clstats")) { 121 mode = MODE_CLSTATS; 122 } else if (modeFlag.equals("-finalizerinfo")) { 123 mode = MODE_FINALIZERINFO; 124 } else { 125 int index = modeFlag.indexOf("-heap:format="); 126 if (index != -1) { 127 String format = modeFlag.substring(1 + modeFlag.indexOf('=')); 128 if (format.equals("b")) { 129 mode = MODE_HEAP_GRAPH_HPROF_BIN; 130 } else if (format.equals("x")) { 131 mode = MODE_HEAP_GRAPH_GXL; 132 } else { 133 System.err.println("unknown heap format:" + format); 134 return; 135 } 136 } else { 137 copyArgs = false; 138 } 139 } 140 141 if (copyArgs) { 142 String[] newArgs = new String[args.length - 1]; 143 for (int i = 0; i < newArgs.length; i++) { 144 newArgs[i] = args[i + 1]; 145 } 146 args = newArgs; 147 } 148 } 149 150 JMap jmap = new JMap(mode); 151 jmap.start(args); 152 jmap.stop(); 153 } 154 155 public boolean writeHeapHprofBin(String fileName) { 156 try { 157 HeapGraphWriter hgw = new HeapHprofBinWriter(); 158 hgw.write(fileName); 159 System.out.println("heap written to " + fileName); 160 return true; 161 } catch (IOException exp) { 162 System.err.println(exp.getMessage()); 163 return false; 164 } 165 } 166 167 public boolean writeHeapHprofBin() { 168 return writeHeapHprofBin("heap.bin"); 169 } 170 171 private boolean writeHeapGXL(String fileName) { 172 try { 173 HeapGraphWriter hgw = new HeapGXLWriter(); 174 hgw.write(fileName); 175 System.out.println("heap written to " + fileName); 176 return true; 177 } catch (IOException exp) { 178 System.err.println(exp.getMessage()); 179 return false; 180 } 181 } 182 183 public boolean writeHeapGXL() { 184 return writeHeapGXL("heap.xml"); 185 } 186 187 private int mode; 188 }