Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jcmd/Arguments.java
38918 views
/*1* Copyright (c) 2011, 2012, 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 int pid = -1;36private String command = null;37private String processSubstring;3839public boolean isListProcesses() { return listProcesses; }40public boolean isListCounters() { return listCounters; }41public boolean isShowUsage() { return showUsage; }42public int getPid() { return pid; }43public String getCommand() { return command; }44public String getProcessSubstring() { return processSubstring; }4546public Arguments(String[] args) {47if (args.length == 0 || args[0].equals("-l")) {48listProcesses = true;49return;50}5152if (args[0].equals("-h") || args[0].equals("-help") ) {53showUsage = true;54return;55}5657try {58pid = Integer.parseInt(args[0]);59} catch (NumberFormatException ex) {60// use as a partial class-name instead61if (args[0].charAt(0) != '-') {62// unless it starts with a '-'63processSubstring = args[0];64}65}6667StringBuilder sb = new StringBuilder();68for (int i = 1; i < args.length; i++) {69if (args[i].equals("-f")) {70if (args.length == i + 1) {71throw new IllegalArgumentException(72"No file specified for parameter -f");73} else if (args.length == i + 2) {74try {75readCommandFile(args[i + 1]);76} catch(IOException e) {77throw new IllegalArgumentException(78"Could not read from file specified with -f option: "79+ args[i + 1]);80}81return;82} else {83throw new IllegalArgumentException(84"Options after -f are not allowed");85}86} else if (args[i].equals("PerfCounter.print")) {87listCounters = true;88} else {89sb.append(args[i]).append(" ");90}91}9293if (listCounters != true && sb.length() == 0) {94throw new IllegalArgumentException("No command specified");95}9697command = sb.toString().trim();98}99100private void readCommandFile(String path) throws IOException {101try (BufferedReader bf = new BufferedReader(new FileReader(path));) {102StringBuilder sb = new StringBuilder();103String s;104while ((s = bf.readLine()) != null) {105sb.append(s).append("\n");106}107command = sb.toString();108}109}110111public static void usage() {112System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>");113System.out.println(" or: jcmd -l ");114System.out.println(" or: jcmd -h ");115System.out.println(" ");116System.out.println(" command must be a valid jcmd command for the selected jvm. ");117System.out.println(" Use the command \"help\" to see which commands are available. ");118System.out.println(" If the pid is 0, commands will be sent to all Java processes. ");119System.out.println(" The main class argument will be used to match (either partially ");120System.out.println(" or fully) the class used to start Java. ");121System.out.println(" If no options are given, lists Java processes (same as -p). ");122System.out.println(" ");123System.out.println(" PerfCounter.print display the counters exposed by this process ");124System.out.println(" -f read and execute commands from the file ");125System.out.println(" -l list JVM processes on the local machine ");126System.out.println(" -h this help ");127}128}129130131