1 /*
   2  * Copyright (c) 2005, 2020, 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("-?") ||
  68                 arg.equals("-h") ||
  69                 arg.equals("--help") ||
  70                 // -help: legacy. Undocumented.
  71                 arg.equals("-help")) {
  72                 usage(0);
  73             } else {
  74                 if (option != null) {
  75                     usage(1);  // option already specified
  76                 }
  77                 option = arg;
  78             }
  79             optionCount++;
  80         }
  81 
  82         // if no option provided then use default.
  83         if (option == null) {
  84             usage(0);
  85         }
  86 
  87         // Next we check the parameter count.
  88         int paramCount = args.length - optionCount;
  89         if (paramCount != 1) {
  90             usage(1);
  91         }
  92 
  93         String pidArg = args[1];
  94         // Here we handle the built-in options
  95         // As more options are added we should create an abstract tool class and
  96         // have a table to map the options
  97         ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
  98         Collection<String> pids = ap.getVirtualMachinePids(JMap.class);
  99 
 100         if (pids.isEmpty()) {
 101             System.err.println("Could not find any processes matching : '" + pidArg + "'");
 102             System.exit(1);
 103         }
 104 
 105         for (String pid : pids) {
 106             if (pids.size() > 1) {
 107                 System.out.println("Pid:" + pid);
 108             }
 109             if (option.equals("-histo")) {
 110                 histo(pid, "");
 111             } else if (option.startsWith("-histo:")) {
 112                 histo(pid, option.substring("-histo:".length()));
 113             } else if (option.startsWith("-dump:")) {
 114                 dump(pid, option.substring("-dump:".length()));
 115             } else if (option.equals("-finalizerinfo")) {
 116                 executeCommandForPid(pid, "jcmd", "GC.finalizer_info");
 117             } else if (option.equals("-clstats")) {
 118                 executeCommandForPid(pid, "jcmd", "VM.classloader_stats");
 119             } else {
 120               usage(1);
 121             }
 122         }
 123     }
 124 
 125     private static void executeCommandForPid(String pid, String command, Object ... args)
 126         throws AttachNotSupportedException, IOException,
 127                UnsupportedEncodingException {
 128         VirtualMachine vm = VirtualMachine.attach(pid);
 129 
 130         // Cast to HotSpotVirtualMachine as this is an
 131         // implementation specific method.
 132         HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
 133         try (InputStream in = hvm.executeCommand(command, args)) {
 134           // read to EOF and just print output
 135           byte b[] = new byte[256];
 136           int n;
 137           do {
 138               n = in.read(b);
 139               if (n > 0) {
 140                   String s = new String(b, 0, n, "UTF-8");
 141                   System.out.print(s);
 142               }
 143           } while (n > 0);
 144         }
 145         vm.detach();
 146     }
 147 
 148     private static String parseFileName(String opt) {
 149         // opt starts with "file="
 150         if (opt.length() > 5) {
 151             //  pass whole "file=" string
 152             String filename = opt.substring(5);
 153             try {
 154                 // Get the canonical path - important to avoid just
 155                 // passing a "heap.bin" and having the dump created
 156                 // in the target VM working directory rather than the
 157                 // directory where jmap is executed.
 158                 return new File(filename).getCanonicalPath();
 159             } catch (IOException ioe) {
 160               return null;
 161             }
 162         }
 163         // no filename
 164         return null;
 165     }
 166 
 167     private static String add_option(String cmd, String opt) {
 168        if (cmd.isEmpty()) {
 169            return opt;
 170        }
 171        return cmd + "," + opt;
 172     }
 173 
 174     private static void histo(String pid, String options)
 175         throws AttachNotSupportedException, IOException,
 176                UnsupportedEncodingException {
 177         String liveopt = "-all";
 178         String filename = null;
 179         String parallel = null;
 180         String subopts[] = options.split(",");
 181         boolean set_all = false;
 182         boolean set_live = false;
 183         String cmdline = "";
 184 
 185         for (int i = 0; i < subopts.length; i++) {
 186             String subopt = subopts[i];
 187             if (subopt.equals("") || subopt.equals("all")) {
 188                 cmdline = add_option(cmdline, "-all");
 189                 set_all = true;
 190             } else if (subopt.equals("live")) {
 191                 // Add '-' for compatibility.
 192                 cmdline = add_option(cmdline, "-live");
 193                 set_live = true;
 194             } else if (subopt.startsWith("file=")) {
 195                 filename = parseFileName(subopt);
 196                 if (filename == null) {
 197                     usage(1); // invalid options or no filename
 198                 }
 199                 cmdline = add_option(cmdline, filename);
 200             } else if (subopt.startsWith("parallel=")) {
 201                parallel = subopt.substring("parallel=".length());
 202                if (parallel == null) {
 203                     usage(1);
 204                }
 205                // Add "parallelThreadsNum=<>" for later check
 206                cmdline = add_option(cmdline, subopt);
 207             } else {
 208                 usage(1);
 209             }
 210         }
 211         if (set_live && set_all) {
 212             usage(1);
 213         }
 214 
 215         System.out.flush();
 216         // inspectHeap is not the same as jcmd GC.class_histogram
 217         executeCommandForPid(pid, "inspectheap", cmdline);
 218     }
 219 
 220     private static void dump(String pid, String options)
 221         throws AttachNotSupportedException, IOException,
 222                UnsupportedEncodingException {
 223 
 224         String subopts[] = options.split(",");
 225         String filename = null;
 226         String liveopt = "-all";
 227 
 228         for (int i = 0; i < subopts.length; i++) {
 229             String subopt = subopts[i];
 230             if (subopt.equals("live")) {
 231                 liveopt = "-live";
 232             } else if (subopt.startsWith("file=")) {
 233                 filename = parseFileName(subopt);
 234             }
 235         }
 236 
 237         if (filename == null) {
 238             usage(1);  // invalid options or no filename
 239         }
 240 
 241         // dumpHeap is not the same as jcmd GC.heap_dump
 242         executeCommandForPid(pid, "dumpheap", filename, liveopt);
 243     }
 244 
 245     private static void checkForUnsupportedOptions(String[] args) {
 246         // Check arguments for -F, -m, and non-numeric value
 247         // and warn the user that SA is not supported anymore
 248 
 249         int paramCount = 0;
 250 
 251         for (String s : args) {
 252             if (s.equals("-F")) {
 253                 SAOptionError("-F option used");
 254             }
 255 
 256             if (s.equals("-heap")) {
 257                 SAOptionError("-heap option used");
 258             }
 259 
 260             /* Reimplemented using jcmd, output format is different
 261                from original one
 262 
 263             if (s.equals("-clstats")) {
 264                 warnSA("-clstats option used");
 265             }
 266 
 267             if (s.equals("-finalizerinfo")) {
 268                 warnSA("-finalizerinfo option used");
 269             }
 270             */
 271 
 272             if (! s.startsWith("-")) {
 273                 paramCount += 1;
 274             }
 275         }
 276 
 277         if (paramCount > 1) {
 278             SAOptionError("More than one non-option argument");
 279         }
 280     }
 281 
 282     private static void SAOptionError(String msg) {
 283         System.err.println("Error: " + msg);
 284         System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jmap instead");
 285         System.exit(1);
 286     }
 287 
 288     // print usage message
 289     private static void usage(int exit) {
 290         System.err.println("Usage:");
 291         System.err.println("    jmap -clstats <pid>");
 292         System.err.println("        to connect to running process and print class loader statistics");
 293         System.err.println("    jmap -finalizerinfo <pid>");
 294         System.err.println("        to connect to running process and print information on objects awaiting finalization");
 295         System.err.println("    jmap -histo[:[<histo-options>]] <pid>");
 296         System.err.println("        to connect to running process and print histogram of java object heap");
 297         System.err.println("    jmap -dump:<dump-options> <pid>");
 298         System.err.println("        to connect to running process and dump java heap");
 299         System.err.println("    jmap -? -h --help");
 300         System.err.println("        to print this help message");
 301         System.err.println("");
 302         System.err.println("    dump-options:");
 303         System.err.println("      live         dump only live objects");
 304         System.err.println("      all          dump all objects in the heap (default if one of \"live\" or \"all\" is not specified");
 305         System.err.println("      format=b     binary format");
 306         System.err.println("      file=<file>  dump heap to <file>");
 307         System.err.println("");
 308         System.err.println("    Example: jmap -dump:live,format=b,file=heap.bin <pid>");
 309         System.err.println("");
 310         System.err.println("    histo-options:");
 311         System.err.println("      live         count only live objects");
 312         System.err.println("      all          count all objects in the heap (default if one of \"live\" or \"all\" is not specified)");
 313         System.err.println("      file=<file>  dump data to <file>");
 314         System.err.println("      parallel=<number>  parallel threads number for heap iteration:");
 315         System.err.println("                                  parallel=0 default behavior, use predefined number of threads");
 316         System.err.println("                                  parallel=1 disable parallel heap iteration");
 317         System.err.println("                                  parallel=<N> use N threads for parallel heap iteration");
 318         System.err.println("");
 319         System.err.println("    Example: jmap -histo:live,file=/tmp/histo.data <pid>");
 320         System.exit(exit);
 321     }
 322 }