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 private static String dumpfile = "heap.bin"; 75 76 public void run() { 77 Tool tool = null; 78 switch (mode) { 79 80 case MODE_HEAP_SUMMARY: 81 tool = new HeapSummary(); 82 break; 83 84 case MODE_HISTOGRAM: 85 tool = new ObjectHistogram(); 86 break; 87 88 case MODE_CLSTATS: 89 tool = new ClassLoaderStats(); 90 break; 91 92 case MODE_PMAP: 93 tool = new PMap(); 94 break; 95 96 case MODE_HEAP_GRAPH_HPROF_BIN: 97 writeHeapHprofBin(dumpfile); 98 return; 99 100 case MODE_HEAP_GRAPH_GXL: 101 writeHeapGXL(dumpfile); 102 return; 103 104 case MODE_FINALIZERINFO: 105 tool = new FinalizerInfo(); 106 break; 107 108 default: 109 usage(); 110 break; 111 } 112 113 tool.setAgent(getAgent()); 114 tool.setDebugeeType(getDebugeeType()); 115 tool.run(); 116 } 117 118 public static void main(String[] args) { 119 int mode = MODE_PMAP; 120 if (args.length > 1 ) { 121 String modeFlag = args[0]; 122 boolean copyArgs = true; 123 if (modeFlag.equals("-heap")) { 124 mode = MODE_HEAP_SUMMARY; 125 } else if (modeFlag.equals("-histo")) { 126 mode = MODE_HISTOGRAM; 127 } else if (modeFlag.equals("-clstats")) { 128 mode = MODE_CLSTATS; 129 } else if (modeFlag.equals("-finalizerinfo")) { 130 mode = MODE_FINALIZERINFO; 131 } else { 132 int index = modeFlag.indexOf("-heap:"); 133 if (index != -1) { 134 String[] options = modeFlag.substring(6).split(","); 135 for (String option : options) { 136 String[] keyValue = option.split("="); 137 if (keyValue[0].equals("format")) { 138 if (keyValue[1].equals("b")) { 139 mode = MODE_HEAP_GRAPH_HPROF_BIN; 140 } else if (keyValue[1].equals("x")) { 141 mode = MODE_HEAP_GRAPH_GXL; 142 } else { 143 System.err.println("unknown heap format:" + keyValue[0]); 144 145 // Exit with error status 146 System.exit(1); 147 } 148 } else if (keyValue[0].equals("file")) { 149 if ((keyValue[1] == null) || keyValue[1].equals("")) { 150 System.err.println("File name must be set."); 151 System.exit(1); 152 } 153 dumpfile = keyValue[1]; 154 } else { 155 System.err.println("unknown option:" + keyValue[0]); 156 157 // Exit with error status 158 System.exit(1); 159 } 160 } 161 } else { 162 copyArgs = false; 163 } 164 } 165 166 if (copyArgs) { 167 String[] newArgs = new String[args.length - 1]; 168 for (int i = 0; i < newArgs.length; i++) { 169 newArgs[i] = args[i + 1]; 170 } 171 args = newArgs; 172 } 173 } 174 175 JMap jmap = new JMap(mode); 176 jmap.execute(args); 177 } 178 179 public boolean writeHeapHprofBin(String fileName) { 180 try { 181 HeapGraphWriter hgw = new HeapHprofBinWriter(); 182 hgw.write(fileName); 183 System.out.println("heap written to " + fileName); 184 return true; 185 } catch (IOException exp) { 186 System.err.println(exp.getMessage()); 187 return false; 188 } 189 } 190 191 public boolean writeHeapHprofBin() { 192 return writeHeapHprofBin("heap.bin"); 193 } 194 195 private boolean writeHeapGXL(String fileName) { 196 try { 197 HeapGraphWriter hgw = new HeapGXLWriter(); 198 hgw.write(fileName); 199 System.out.println("heap written to " + fileName); 200 return true; 201 } catch (IOException exp) { 202 System.err.println(exp.getMessage()); 203 return false; 204 } 205 } 206 207 public boolean writeHeapGXL() { 208 return writeHeapGXL("heap.xml"); 209 } 210 211 private int mode; 212 }