1 /*
   2  * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jinfo;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.util.Collection;
  31 
  32 import com.sun.tools.attach.VirtualMachine;
  33 import com.sun.tools.attach.VirtualMachineDescriptor;
  34 
  35 import sun.tools.attach.HotSpotVirtualMachine;
  36 import sun.tools.common.ProcessArgumentMatcher;
  37 
  38 /*
  39  * This class is the main class for the JInfo utility. It parses its arguments
  40  * and decides if the command should be satisfied using the VM attach mechanism
  41  * or an SA tool.
  42  */
  43 final public class JInfo {
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (args.length == 0) {
  47             usage(1); // no arguments
  48         }
  49         checkForUnsupportedOptions(args);
  50 
  51         boolean doFlag = false;
  52         boolean doFlags = false;
  53         boolean doSysprops = false;
  54         int flag = -1;
  55 
  56         // Parse the options (arguments starting with "-" )
  57         int optionCount = 0;
  58         while (optionCount < args.length) {
  59             String arg = args[optionCount];
  60             if (!arg.startsWith("-")) {
  61                 break;
  62             }
  63 
  64             optionCount++;
  65 
  66             // -help: legacy. Undocumented.
  67             if (arg.equals("-?") || arg.equals("-h") || arg.equals("--help") || arg.equals("-help")) {
  68                 usage(0);
  69             }
  70 
  71             if (arg.equals("-flag")) {
  72                 doFlag = true;
  73                 // Consume the flag
  74                 if (optionCount < args.length) {
  75                     flag = optionCount++;
  76                     break;
  77                 }
  78                 usage(1);
  79             }
  80 
  81             if (arg.equals("-flags")) {
  82                 doFlags = true;
  83                 break;
  84             }
  85 
  86             if (arg.equals("-sysprops")) {
  87                 doSysprops = true;
  88                 break;
  89             }
  90         }
  91 
  92         int paramCount = args.length - optionCount;
  93         if (paramCount != 1) {
  94             usage(1);
  95         }
  96 
  97         String parg = args[optionCount];
  98 
  99         ProcessArgumentMatcher ap = new ProcessArgumentMatcher(parg);
 100         Collection<String> pids = ap.getVirtualMachinePids(JInfo.class);
 101 
 102         if (pids.isEmpty()) {
 103             System.err.println("Could not find any processes matching : '" + parg + "'");
 104             System.exit(1);
 105         }
 106 
 107         for (String pid : pids) {
 108             if (pids.size() > 1) {
 109                 System.out.println("Pid:" + pid);
 110             }
 111             if (!doFlag && !doFlags && !doSysprops) {
 112                 // Print flags and sysporps if no options given
 113                 sysprops(pid);
 114                 System.out.println();
 115                 flags(pid);
 116                 System.out.println();
 117                 commandLine(pid);
 118             }
 119             if (doFlag) {
 120                 if (flag < 0) {
 121                     System.err.println("Missing flag");
 122                     usage(1);
 123                 }
 124                 flag(pid, args[flag]);
 125             }
 126             if (doFlags) {
 127                 flags(pid);
 128             }
 129             if (doSysprops) {
 130                 sysprops(pid);
 131             }
 132         }
 133     }
 134 
 135     private static void flag(String pid, String option) throws IOException {
 136         HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
 137         String flag;
 138         InputStream in;
 139         int index = option.indexOf('=');
 140         if (index != -1) {
 141             flag = option.substring(0, index);
 142             String value = option.substring(index + 1);
 143             in = vm.setFlag(flag, value);
 144         } else {
 145             char c = option.charAt(0);
 146             switch (c) {
 147                 case '+':
 148                     flag = option.substring(1);
 149                     in = vm.setFlag(flag, "1");
 150                     break;
 151                 case '-':
 152                     flag = option.substring(1);
 153                     in = vm.setFlag(flag, "0");
 154                     break;
 155                 default:
 156                     flag = option;
 157                     in = vm.printFlag(flag);
 158                     break;
 159             }
 160         }
 161 
 162         drain(vm, in);
 163     }
 164 
 165     private static void flags(String pid) throws IOException {
 166         HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
 167         InputStream in = vm.executeJCmd("VM.flags");
 168         System.out.println("VM Flags:");
 169         drain(vm, in);
 170     }
 171 
 172     private static void commandLine(String pid) throws IOException {
 173         HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
 174         InputStream in = vm.executeJCmd("VM.command_line");
 175         drain(vm, in);
 176     }
 177 
 178     private static void sysprops(String pid) throws IOException {
 179         HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid);
 180         InputStream in = vm.executeJCmd("VM.system_properties");
 181         System.out.println("Java System Properties:");
 182         drain(vm, in);
 183     }
 184 
 185     // Attach to <pid>, exiting if we fail to attach
 186     private static VirtualMachine attach(String pid) {
 187         try {
 188             return VirtualMachine.attach(pid);
 189         } catch (Exception x) {
 190             String msg = x.getMessage();
 191             if (msg != null) {
 192                 System.err.println(pid + ": " + msg);
 193             } else {
 194                 x.printStackTrace();
 195             }
 196             System.exit(1);
 197             return null; // keep compiler happy
 198         }
 199     }
 200 
 201     // Read the stream from the target VM until EOF, then detach
 202     private static void drain(VirtualMachine vm, InputStream in) throws IOException {
 203         // read to EOF and just print output
 204         byte b[] = new byte[256];
 205         int n;
 206         do {
 207             n = in.read(b);
 208             if (n > 0) {
 209                 String s = new String(b, 0, n, "UTF-8");
 210                 System.out.print(s);
 211             }
 212         } while (n > 0);
 213         in.close();
 214         vm.detach();
 215     }
 216 
 217     private static void checkForUnsupportedOptions(String[] args) {
 218         // Check arguments for -F, and non-numeric value
 219         // and warn the user that SA is not supported anymore
 220         int maxCount = 1;
 221         int paramCount = 0;
 222 
 223         for (String s : args) {
 224             if (s.equals("-F")) {
 225                 SAOptionError("-F option used");
 226             }
 227             if (s.equals("-flag")) {
 228                 maxCount = 2;
 229             }
 230             if (! s.startsWith("-")) {
 231                 paramCount += 1;
 232             }
 233         }
 234 
 235         if (paramCount > maxCount) {
 236             SAOptionError("More than " + maxCount + " non-option argument");
 237         }
 238     }
 239 
 240     private static void SAOptionError(String msg) {
 241         System.err.println("Error: " + msg);
 242         System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jinfo instead");
 243         System.exit(1);
 244     }
 245 
 246      // print usage message
 247     private static void usage(int exit) {
 248         System.err.println("Usage:");
 249         System.err.println("    jinfo <option> <pid>");
 250         System.err.println("       (to connect to a running process)");
 251         System.err.println("");
 252         System.err.println("where <option> is one of:");
 253         System.err.println("    -flag <name>         to print the value of the named VM flag");
 254         System.err.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
 255         System.err.println("    -flag <name>=<value> to set the named VM flag to the given value");
 256         System.err.println("    -flags               to print VM flags");
 257         System.err.println("    -sysprops            to print Java system properties");
 258         System.err.println("    <no option>          to print both VM flags and system properties");
 259         System.err.println("    -? | -h | --help     to print this help message");
 260         System.exit(exit);
 261     }
 262 }