jdk/src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 14342 jdk/src/share/classes/sun/tools/jcmd/Arguments.java@8435a30053c1
child 35967 31cdfa4bf870
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11365
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11365
diff changeset
     2
 * Copyright (c) 2011, 2012, 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 int     pid           = -1;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    37
    private String  command       = null;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    38
    private String  processSubstring;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    39
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    40
    public boolean isListProcesses() { return listProcesses; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    41
    public boolean isListCounters() { return listCounters; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    42
    public boolean isShowUsage() { return showUsage; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    43
    public int getPid() { return pid; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    44
    public String getCommand() { return command; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    45
    public String getProcessSubstring() { return processSubstring; }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    46
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    47
    public Arguments(String[] args) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    48
        if (args.length == 0 || args[0].equals("-l")) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    49
            listProcesses = true;
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
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    53
        if (args[0].equals("-h") || args[0].equals("-help") ) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    54
            showUsage = true;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    55
            return;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    56
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    57
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    58
        try {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    59
            pid = Integer.parseInt(args[0]);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    60
        } catch (NumberFormatException ex) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    61
            // use as a partial class-name instead
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    62
            if (args[0].charAt(0) != '-') {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    63
                // unless it starts with a '-'
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    64
                processSubstring = args[0];
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    65
            }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    66
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    67
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    68
        StringBuilder sb = new StringBuilder();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    69
        for (int i = 1; i < args.length; i++) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    70
            if (args[i].equals("-f")) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    71
                if (args.length == i + 1) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    72
                    throw new IllegalArgumentException(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    73
                        "No file specified for parameter -f");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    74
                } else if (args.length == i + 2) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    75
                    try {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    76
                        readCommandFile(args[i + 1]);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    77
                    } catch(IOException e) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    78
                        throw new IllegalArgumentException(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    79
                            "Could not read from file specified with -f option: "
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    80
                            + args[i + 1]);
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    81
                    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    82
                    return;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    83
                } else {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    84
                    throw new IllegalArgumentException(
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    85
                        "Options after -f are not allowed");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    86
                }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    87
            } else if (args[i].equals("PerfCounter.print")) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    88
                listCounters = true;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    89
            } else {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    90
                sb.append(args[i]).append(" ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    91
            }
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
        if (listCounters != true && sb.length() == 0) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    95
            throw new IllegalArgumentException("No command specified");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    96
        }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    97
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    98
        command = sb.toString().trim();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
    99
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   100
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   101
    private void readCommandFile(String path) throws IOException {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   102
        try (BufferedReader bf = new BufferedReader(new FileReader(path));) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   103
                StringBuilder sb = new StringBuilder();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   104
                String s;
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   105
                while ((s = bf.readLine()) != null) {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   106
                    sb.append(s).append("\n");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   107
                }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   108
                command = sb.toString();
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   109
            }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   110
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   111
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   112
    public static void usage() {
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   113
        System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   114
        System.out.println("   or: jcmd -l                                                    ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   115
        System.out.println("   or: jcmd -h                                                    ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   116
        System.out.println("                                                                  ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   117
        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
   118
        System.out.println("  Use the command \"help\" to see which commands are available.   ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   119
        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
   120
        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
   121
        System.out.println("  or fully) the class used to start Java.                         ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   122
        System.out.println("  If no options are given, lists Java processes (same as -p).     ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   123
        System.out.println("                                                                  ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   124
        System.out.println("  PerfCounter.print display the counters exposed by this process  ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   125
        System.out.println("  -f  read and execute commands from the file                     ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   126
        System.out.println("  -l  list JVM processes on the local machine                     ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   127
        System.out.println("  -h  this help                                                   ");
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   128
    }
05d995976571 7104647: Adding a diagnostic command framework
fparain
parents:
diff changeset
   129
}