< prev index next >

src/jdk.jcmd/share/classes/sun/tools/jmap/JMap.java

Print this page
rev 47855 : 8189102: All tools should support -?, -h and --help
   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


  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         }


 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 }
   1 /*
   2  * Copyright (c) 2005, 2017, 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


  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         }


 234     }
 235 
 236     private static void SAOptionError(String msg) {
 237         System.err.println("Error: " + msg);
 238         System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jmap instead");
 239         System.exit(1);
 240     }
 241 
 242     // print usage message
 243     private static void usage(int exit) {
 244         System.err.println("Usage:");
 245         System.err.println("    jmap -clstats <pid>");
 246         System.err.println("        to connect to running process and print class loader statistics");
 247         System.err.println("    jmap -finalizerinfo <pid>");
 248         System.err.println("        to connect to running process and print information on objects awaiting finalization");
 249         System.err.println("    jmap -histo[:live] <pid>");
 250         System.err.println("        to connect to running process and print histogram of java object heap");
 251         System.err.println("        if the \"live\" suboption is specified, only count live objects");
 252         System.err.println("    jmap -dump:<dump-options> <pid>");
 253         System.err.println("        to connect to running process and dump java heap");
 254         System.err.println("    jmap -? -h --help");
 255         System.err.println("        to print this help message");
 256         System.err.println("");
 257         System.err.println("    dump-options:");
 258         System.err.println("      live         dump only live objects; if not specified,");
 259         System.err.println("                   all objects in the heap are dumped.");
 260         System.err.println("      format=b     binary format");
 261         System.err.println("      file=<file>  dump heap to <file>");
 262         System.err.println("");
 263         System.err.println("    Example: jmap -dump:live,format=b,file=heap.bin <pid>");
 264         System.exit(exit);
 265     }
 266 }
< prev index next >