Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java
41159 views
/*1* Copyright (c) 2006, 2019, 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.jinfo;2627import java.io.IOException;28import java.io.InputStream;29import java.util.Collection;3031import com.sun.tools.attach.VirtualMachine;3233import sun.tools.attach.HotSpotVirtualMachine;34import sun.tools.common.ProcessArgumentMatcher;35import sun.tools.common.PrintStreamPrinter;3637/*38* This class is the main class for the JInfo utility. It parses its arguments39* and decides if the command should be satisfied using the VM attach mechanism40* or an SA tool.41*/42final public class JInfo {4344public static void main(String[] args) throws Exception {45if (args.length == 0) {46usage(1); // no arguments47}48checkForUnsupportedOptions(args);4950boolean doFlag = false;51boolean doFlags = false;52boolean doSysprops = false;53int flag = -1;5455// Parse the options (arguments starting with "-" )56int optionCount = 0;57while (optionCount < args.length) {58String arg = args[optionCount];59if (!arg.startsWith("-")) {60break;61}6263optionCount++;6465if (arg.equals("-?") ||66arg.equals("-h") ||67arg.equals("--help") ||68// -help: legacy.69arg.equals("-help")) {70usage(0);71}7273if (arg.equals("-flag")) {74doFlag = true;75// Consume the flag76if (optionCount < args.length) {77flag = optionCount++;78break;79}80usage(1);81}8283if (arg.equals("-flags")) {84doFlags = true;85break;86}8788if (arg.equals("-sysprops")) {89doSysprops = true;90break;91}92}9394int paramCount = args.length - optionCount;95if (paramCount != 1) {96usage(1);97}9899String parg = args[optionCount];100101ProcessArgumentMatcher ap = new ProcessArgumentMatcher(parg);102Collection<String> pids = ap.getVirtualMachinePids(JInfo.class);103104if (pids.isEmpty()) {105System.err.println("Could not find any processes matching : '" + parg + "'");106System.exit(1);107}108109for (String pid : pids) {110if (pids.size() > 1) {111System.out.println("Pid:" + pid);112}113if (!doFlag && !doFlags && !doSysprops) {114// Print flags and sysporps if no options given115sysprops(pid);116System.out.println();117flags(pid);118System.out.println();119commandLine(pid);120}121if (doFlag) {122if (flag < 0) {123System.err.println("Missing flag");124usage(1);125}126flag(pid, args[flag]);127}128if (doFlags) {129flags(pid);130}131if (doSysprops) {132sysprops(pid);133}134}135}136137private static void flag(String pid, String option) throws IOException {138HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);139String flag;140InputStream in;141int index = option.indexOf('=');142if (index != -1) {143flag = option.substring(0, index);144String value = option.substring(index + 1);145in = vm.setFlag(flag, value);146} else {147char c = option.charAt(0);148switch (c) {149case '+':150flag = option.substring(1);151in = vm.setFlag(flag, "1");152break;153case '-':154flag = option.substring(1);155in = vm.setFlag(flag, "0");156break;157default:158flag = option;159in = vm.printFlag(flag);160break;161}162}163164drain(vm, in);165}166167private static void flags(String pid) throws IOException {168HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);169InputStream in = vm.executeJCmd("VM.flags");170System.out.println("VM Flags:");171drain(vm, in);172}173174private static void commandLine(String pid) throws IOException {175HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);176InputStream in = vm.executeJCmd("VM.command_line");177drain(vm, in);178}179180private static void sysprops(String pid) throws IOException {181HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);182InputStream in = vm.executeJCmd("VM.system_properties");183System.out.println("Java System Properties:");184drain(vm, in);185}186187// Attach to <pid>, exiting if we fail to attach188private static VirtualMachine attach(String pid) {189try {190return VirtualMachine.attach(pid);191} catch (Exception x) {192String msg = x.getMessage();193if (msg != null) {194System.err.println(pid + ": " + msg);195} else {196x.printStackTrace();197}198System.exit(1);199return null; // keep compiler happy200}201}202203// Read the stream from the target VM until EOF, then detach204private static void drain(VirtualMachine vm, InputStream in) throws IOException {205PrintStreamPrinter.drainUTF8(in, System.out);206vm.detach();207}208209private static void checkForUnsupportedOptions(String[] args) {210// Check arguments for -F, and non-numeric value211// and warn the user that SA is not supported anymore212int maxCount = 1;213int paramCount = 0;214215for (String s : args) {216if (s.equals("-F")) {217SAOptionError("-F option used");218}219if (s.equals("-flag")) {220maxCount = 2;221}222if (! s.startsWith("-")) {223paramCount += 1;224}225}226227if (paramCount > maxCount) {228SAOptionError("More than " + maxCount + " non-option argument");229}230}231232private static void SAOptionError(String msg) {233System.err.println("Error: " + msg);234System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jinfo instead");235System.exit(1);236}237238// print usage message239private static void usage(int exit) {240System.err.println("Usage:");241System.err.println(" jinfo <option> <pid>");242System.err.println(" (to connect to a running process)");243System.err.println("");244System.err.println("where <option> is one of:");245System.err.println(" -flag <name> to print the value of the named VM flag");246System.err.println(" -flag [+|-]<name> to enable or disable the named VM flag");247System.err.println(" -flag <name>=<value> to set the named VM flag to the given value");248System.err.println(" -flags to print VM flags");249System.err.println(" -sysprops to print Java system properties");250System.err.println(" <no option> to print both VM flags and system properties");251System.err.println(" -? | -h | --help | -help to print this help message");252System.exit(exit);253}254}255256257