1 /* 2 * Copyright (c) 2005, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jmap; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.io.UnsupportedEncodingException; 32 import java.util.Collection; 33 34 import com.sun.tools.attach.VirtualMachine; 35 import com.sun.tools.attach.VirtualMachineDescriptor; 36 import com.sun.tools.attach.AttachNotSupportedException; 37 import sun.tools.attach.HotSpotVirtualMachine; 38 import sun.tools.common.ProcessArgumentMatcher; 39 40 /* 41 * This class is the main class for the JMap utility. It parses its arguments 42 * and decides if the command should be satisfied using the VM attach mechanism 43 * or an SA tool. At this time the only option that uses the VM attach mechanism 44 * is the -dump option to get a heap dump of a running application. All other 45 * options are mapped to SA tools. 46 */ 47 public class JMap { 48 49 public static void main(String[] args) throws Exception { 50 if (args.length == 0) { 51 usage(1); // no arguments 52 } 53 54 checkForUnsupportedOptions(args); 55 56 // the chosen option 57 String option = null; 58 59 // First iterate over the options (arguments starting with -). There should be 60 // one. 61 int optionCount = 0; 62 while (optionCount < args.length) { 63 String arg = args[optionCount]; 64 if (!arg.startsWith("-")) { 65 break; 66 } 67 if (arg.equals("-help") || arg.equals("-h")) { 68 usage(0); 69 } else { 70 if (option != null) { 71 usage(1); // option already specified 72 } 73 option = arg; 74 } 75 optionCount++; 76 } 77 78 // if no option provided then use default. 79 if (option == null) { 80 usage(0); 81 } 82 83 // Next we check the parameter count. 84 int paramCount = args.length - optionCount; 85 if (paramCount != 1) { 86 usage(1); 87 } 88 89 String pidArg = args[1]; 90 // Here we handle the built-in options 91 // As more options are added we should create an abstract tool class and 92 // have a table to map the options 93 ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg); 94 Collection<String> pids = ap.getVirtualMachinePids(JMap.class); 95 96 if (pids.isEmpty()) { 97 System.err.println("Could not find any processes matching : '" + pidArg + "'"); 98 System.exit(1); 99 } 100 101 for (String pid : pids) { 102 if (pids.size() > 1) { 103 System.out.println("Pid:" + pid); 104 } 105 if (option.equals("-histo")) { 106 histo(pid, ""); 107 } else if (option.startsWith("-histo:")) { 108 histo(pid, option.substring("-histo:".length())); 109 } else if (option.startsWith("-dump:")) { 110 dump(pid, option.substring("-dump:".length())); 111 } else if (option.equals("-finalizerinfo")) { 112 executeCommandForPid(pid, "jcmd", "GC.finalizer_info"); 113 } else if (option.equals("-clstats")) { 114 executeCommandForPid(pid, "jcmd", "GC.class_stats"); 115 } else { 116 usage(1); 117 } 118 } 119 } 120 121 private static void executeCommandForPid(String pid, String command, Object ... args) 122 throws AttachNotSupportedException, IOException, 123 UnsupportedEncodingException { 124 VirtualMachine vm = VirtualMachine.attach(pid); 125 126 // Cast to HotSpotVirtualMachine as this is an 127 // implementation specific method. 128 HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm; 129 try (InputStream in = hvm.executeCommand(command, args)) { 130 // read to EOF and just print output 131 byte b[] = new byte[256]; 132 int n; 133 do { 134 n = in.read(b); 135 if (n > 0) { 136 String s = new String(b, 0, n, "UTF-8"); 137 System.out.print(s); 138 } 139 } while (n > 0); 140 } 141 vm.detach(); 142 } 143 144 private static void histo(String pid, String options) 145 throws AttachNotSupportedException, IOException, 146 UnsupportedEncodingException { 147 String liveopt = "-all"; 148 if (options.equals("") || options.equals("all")) { 149 // pass 150 } 151 else if (options.equals("live")) { 152 liveopt = "-live"; 153 } 154 else { 155 usage(1); 156 } 157 158 // inspectHeap is not the same as jcmd GC.class_histogram 159 executeCommandForPid(pid, "inspectheap", liveopt); 160 } 161 162 private static void dump(String pid, String options) 163 throws AttachNotSupportedException, IOException, 164 UnsupportedEncodingException { 165 166 String subopts[] = options.split(","); 167 String filename = null; 168 String liveopt = "-all"; 169 170 for (int i = 0; i < subopts.length; i++) { 171 String subopt = subopts[i]; 172 if (subopt.equals("live")) { 173 liveopt = "-live"; 174 } else if (subopt.startsWith("file=")) { 175 // file=<file> - check that <file> is specified 176 if (subopt.length() > 5) { 177 filename = subopt.substring(5); 178 } 179 } 180 } 181 182 if (filename == null) { 183 usage(1); // invalid options or no filename 184 } 185 186 // get the canonical path - important to avoid just passing 187 // a "heap.bin" and having the dump created in the target VM 188 // working directory rather than the directory where jmap 189 // is executed. 190 filename = new File(filename).getCanonicalPath(); 191 // dumpHeap is not the same as jcmd GC.heap_dump 192 executeCommandForPid(pid, "dumpheap", filename, liveopt); 193 } 194 195 private static void checkForUnsupportedOptions(String[] args) { 196 // Check arguments for -F, -m, and non-numeric value 197 // and warn the user that SA is not supported anymore 198 199 int paramCount = 0; 200 201 for (String s : args) { 202 if (s.equals("-F")) { 203 SAOptionError("-F option used"); 204 } 205 206 if (s.equals("-heap")) { 207 SAOptionError("-heap option used"); 208 } 209 210 /* Reimplemented using jcmd, output format is different 211 from original one 212 213 if (s.equals("-clstats")) { 214 warnSA("-clstats option used"); 215 } 216 217 if (s.equals("-finalizerinfo")) { 218 warnSA("-finalizerinfo option used"); 219 } 220 */ 221 222 if (! s.startsWith("-")) { 223 paramCount += 1; 224 } 225 } 226 227 if (paramCount > 1) { 228 SAOptionError("More than one non-option argument"); 229 } 230 } 231 232 private static void SAOptionError(String msg) { 233 System.err.println("Error: " + msg); 234 System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jmap instead"); 235 System.exit(1); 236 } 237 238 // print usage message 239 private static void usage(int exit) { 240 System.err.println("Usage:"); 241 System.err.println(" jmap -clstats <pid>"); 242 System.err.println(" to connect to running process and print class loader statistics"); 243 System.err.println(" jmap -finalizerinfo <pid>"); 244 System.err.println(" to connect to running process and print information on objects awaiting finalization"); 245 System.err.println(" jmap -histo[:live] <pid>"); 246 System.err.println(" to connect to running process and print histogram of java object heap"); 247 System.err.println(" if the \"live\" suboption is specified, only count live objects"); 248 System.err.println(" jmap -dump:<dump-options> <pid>"); 249 System.err.println(" to connect to running process and dump java heap"); 250 System.err.println(""); 251 System.err.println(" dump-options:"); 252 System.err.println(" live dump only live objects; if not specified,"); 253 System.err.println(" all objects in the heap are dumped."); 254 System.err.println(" format=b binary format"); 255 System.err.println(" file=<file> dump heap to <file>"); 256 System.err.println(""); 257 System.err.println(" Example: jmap -dump:live,format=b,file=heap.bin <pid>"); 258 System.exit(exit); 259 } 260 }