src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java
author jdv
Wed, 18 Apr 2018 13:22:53 +0530
changeset 49996 39dc39093c5e
parent 48543 7067fe4e054e
child 50294 16be361f4320
permissions -rw-r--r--
6574555: PNGImageWriter incorrectly sets bKGD chunk Reviewed-by: prr, pnarayanan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     4
 *
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    10
 *
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    15
 * accompanied this code).
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    16
 *
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    20
 *
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    23
 * questions.
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    24
 */
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    25
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    26
package sun.tools.jcmd;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    27
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    28
import java.io.BufferedReader;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    29
import java.io.FileReader;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    30
import java.io.IOException;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    31
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    32
class Arguments {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    33
    private boolean listProcesses = false;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    34
    private boolean listCounters  = false;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    35
    private boolean showUsage     = false;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    36
    private String  command       = null;
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 35967
diff changeset
    37
    private String  processString = null;
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    38
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    39
    public boolean isListProcesses() { return listProcesses; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    40
    public boolean isListCounters() { return listCounters; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    41
    public boolean isShowUsage() { return showUsage; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    42
    public String getCommand() { return command; }
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 35967
diff changeset
    43
    public String getProcessString() { return processString; }
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    44
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    45
    public Arguments(String[] args) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    46
        if (args.length == 0 || args[0].equals("-l")) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    47
            listProcesses = true;
38956
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    48
            /* list all processes */
cf2c4bfba78a 8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents: 38361
diff changeset
    49
            processString = "0";
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    50
            return;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    51
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    52
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    53
        if (args[0].equals("-?") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    54
            args[0].equals("-h") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    55
            args[0].equals("--help") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    56
            // -help: legacy.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    57
            args[0].equals("-help")) {
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    58
            showUsage = true;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    59
            return;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    60
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    61
38361
8ea2d56bfdf3 8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents: 35967
diff changeset
    62
        processString = args[0];
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    63
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    64
        StringBuilder sb = new StringBuilder();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    65
        for (int i = 1; i < args.length; i++) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    66
            if (args[i].equals("-f")) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    67
                if (args.length == i + 1) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    68
                    throw new IllegalArgumentException(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    69
                        "No file specified for parameter -f");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    70
                } else if (args.length == i + 2) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    71
                    try {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    72
                        readCommandFile(args[i + 1]);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    73
                    } catch(IOException e) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    74
                        throw new IllegalArgumentException(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    75
                            "Could not read from file specified with -f option: "
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    76
                            + args[i + 1]);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    77
                    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    78
                    return;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    79
                } else {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    80
                    throw new IllegalArgumentException(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    81
                        "Options after -f are not allowed");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    82
                }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    83
            } else if (args[i].equals("PerfCounter.print")) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    84
                listCounters = true;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    85
            } else {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    86
                sb.append(args[i]).append(" ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    87
            }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    88
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    89
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    90
        if (listCounters != true && sb.length() == 0) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    91
            throw new IllegalArgumentException("No command specified");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    92
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    93
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    94
        command = sb.toString().trim();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    95
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    96
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    97
    private void readCommandFile(String path) throws IOException {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    98
        try (BufferedReader bf = new BufferedReader(new FileReader(path));) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    99
                StringBuilder sb = new StringBuilder();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   100
                String s;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   101
                while ((s = bf.readLine()) != null) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   102
                    sb.append(s).append("\n");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   103
                }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   104
                command = sb.toString();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   105
            }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   106
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   107
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   108
    public static void usage() {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   109
        System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   110
        System.out.println("   or: jcmd -l                                                    ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   111
        System.out.println("   or: jcmd -h                                                    ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   112
        System.out.println("                                                                  ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   113
        System.out.println("  command must be a valid jcmd command for the selected jvm.      ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   114
        System.out.println("  Use the command \"help\" to see which commands are available.   ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   115
        System.out.println("  If the pid is 0, commands will be sent to all Java processes.   ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   116
        System.out.println("  The main class argument will be used to match (either partially ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   117
        System.out.println("  or fully) the class used to start Java.                         ");
35967
31cdfa4bf870 8149099: jcmd -help mention non-existent option
dsamersoff
parents: 25859
diff changeset
   118
        System.out.println("  If no options are given, lists Java processes (same as -l).     ");
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   119
        System.out.println("                                                                  ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   120
        System.out.println("  PerfCounter.print display the counters exposed by this process  ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   121
        System.out.println("  -f  read and execute commands from the file                     ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   122
        System.out.println("  -l  list JVM processes on the local machine                     ");
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   123
        System.out.println("  -? -h --help print this help message                            ");
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   124
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   125
}