Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java
41159 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jcmd;2627import java.io.BufferedReader;28import java.io.FileReader;29import java.io.IOException;3031class Arguments {32private boolean listProcesses = false;33private boolean listCounters = false;34private boolean showUsage = false;35private String command = null;36private String processString = null;3738public boolean isListProcesses() { return listProcesses; }39public boolean isListCounters() { return listCounters; }40public boolean isShowUsage() { return showUsage; }41public String getCommand() { return command; }42public String getProcessString() { return processString; }4344public Arguments(String[] args) {45if (args.length == 0 || args[0].equals("-l")) {46listProcesses = true;47/* list all processes */48processString = "0";49return;50}5152if (args[0].equals("-?") ||53args[0].equals("-h") ||54args[0].equals("--help") ||55// -help: legacy.56args[0].equals("-help")) {57showUsage = true;58return;59}6061processString = args[0];6263StringBuilder sb = new StringBuilder();64for (int i = 1; i < args.length; i++) {65if (args[i].equals("-f")) {66if (args.length == i + 1) {67throw new IllegalArgumentException(68"No file specified for parameter -f");69} else if (args.length == i + 2) {70try {71readCommandFile(args[i + 1]);72} catch(IOException e) {73throw new IllegalArgumentException(74"Could not read from file specified with -f option: "75+ args[i + 1]);76}77return;78} else {79throw new IllegalArgumentException(80"Options after -f are not allowed");81}82} else if (args[i].equals("PerfCounter.print")) {83listCounters = true;84} else {85sb.append(args[i]).append(" ");86}87}8889if (listCounters != true && sb.length() == 0) {90// Omitting the command shall cause the target VM to print out a list91// of available commands.92sb.append("help");93}9495command = sb.toString().trim();96}9798private void readCommandFile(String path) throws IOException {99try (BufferedReader bf = new BufferedReader(new FileReader(path));) {100StringBuilder sb = new StringBuilder();101String s;102while ((s = bf.readLine()) != null) {103sb.append(s).append("\n");104}105command = sb.toString();106}107}108109public static void usage() {110System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>");111System.out.println(" or: jcmd -l ");112System.out.println(" or: jcmd -h ");113System.out.println(" ");114System.out.println(" command must be a valid jcmd command for the selected jvm. ");115System.out.println(" Use the command \"help\" to see which commands are available. ");116System.out.println(" If the pid is 0, commands will be sent to all Java processes. ");117System.out.println(" The main class argument will be used to match (either partially ");118System.out.println(" or fully) the class used to start Java. ");119System.out.println(" If no options are given, lists Java processes (same as -l). ");120System.out.println(" ");121System.out.println(" PerfCounter.print display the counters exposed by this process ");122System.out.println(" -f read and execute commands from the file ");123System.out.println(" -l list JVM processes on the local machine ");124System.out.println(" -? -h --help print this help message ");125}126}127128129