1 /* 2 * Copyright (c) 2006, 2014, 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 if (arg.equals("-help") || arg.equals("-h")) { 67 usage(0); 68 } 69 70 if (arg.equals("-flag")) { 71 doFlag = true; 72 // Consume the flag 73 if (optionCount < args.length) { 74 flag = optionCount++; 75 break; 76 } 77 usage(1); 78 } 79 80 if (arg.equals("-flags")) { 81 doFlags = true; 82 break; 83 } 84 85 if (arg.equals("-sysprops")) { 86 doSysprops = true; 87 break; 88 } 89 } 90 91 int paramCount = args.length - optionCount; 92 if (paramCount != 1) { 93 usage(1); 94 } 95 96 String parg = args[optionCount]; 97 98 ProcessArgumentMatcher ap = new ProcessArgumentMatcher(parg); 99 Collection<String> pids = ap.getVirtualMachinePids(JInfo.class); 100 101 if (pids.isEmpty()) { 102 System.err.println("Could not find any processes matching : '" + parg + "'"); 103 System.exit(1); 104 } 105 106 for (String pid : pids) { 107 if (pids.size() > 1) { 108 System.out.println("Pid:" + pid); 109 } 110 if (!doFlag && !doFlags && !doSysprops) { 111 // Print flags and sysporps if no options given 112 sysprops(pid); 113 System.out.println(); 114 flags(pid); 115 System.out.println(); 116 commandLine(pid); 117 } 118 if (doFlag) { 119 if (flag < 0) { 120 System.err.println("Missing flag"); 121 usage(1); 122 } 123 flag(pid, args[flag]); 124 } 125 if (doFlags) { 126 flags(pid); 127 } 128 if (doSysprops) { 129 sysprops(pid); 130 } 131 } 132 } 133 134 private static void flag(String pid, String option) throws IOException { 135 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid); 136 String flag; 137 InputStream in; 138 int index = option.indexOf('='); 139 if (index != -1) { 140 flag = option.substring(0, index); 141 String value = option.substring(index + 1); 142 in = vm.setFlag(flag, value); 143 } else { 144 char c = option.charAt(0); 145 switch (c) { 146 case '+': 147 flag = option.substring(1); 148 in = vm.setFlag(flag, "1"); 149 break; 150 case '-': 151 flag = option.substring(1); 152 in = vm.setFlag(flag, "0"); 153 break; 154 default: 155 flag = option; 156 in = vm.printFlag(flag); 157 break; 158 } 159 } 160 161 drain(vm, in); 162 } 163 164 private static void flags(String pid) throws IOException { 165 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid); 166 InputStream in = vm.executeJCmd("VM.flags"); 167 System.out.println("VM Flags:"); 168 drain(vm, in); 169 } 170 171 private static void commandLine(String pid) throws IOException { 172 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid); 173 InputStream in = vm.executeJCmd("VM.command_line"); 174 drain(vm, in); 175 } 176 177 private static void sysprops(String pid) throws IOException { 178 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) attach(pid); 179 InputStream in = vm.executeJCmd("VM.system_properties"); 180 System.out.println("Java System Properties:"); 181 drain(vm, in); 182 } 183 184 // Attach to <pid>, exiting if we fail to attach 185 private static VirtualMachine attach(String pid) { 186 try { 187 return VirtualMachine.attach(pid); 188 } catch (Exception x) { 189 String msg = x.getMessage(); 190 if (msg != null) { 191 System.err.println(pid + ": " + msg); 192 } else { 193 x.printStackTrace(); 194 } 195 System.exit(1); 196 return null; // keep compiler happy 197 } 198 } 199 200 // Read the stream from the target VM until EOF, then detach 201 private static void drain(VirtualMachine vm, InputStream in) throws IOException { 202 // read to EOF and just print output 203 byte b[] = new byte[256]; 204 int n; 205 do { 206 n = in.read(b); 207 if (n > 0) { 208 String s = new String(b, 0, n, "UTF-8"); 209 System.out.print(s); 210 } 211 } while (n > 0); 212 in.close(); 213 vm.detach(); 214 } 215 216 private static void checkForUnsupportedOptions(String[] args) { 217 // Check arguments for -F, and non-numeric value 218 // and warn the user that SA is not supported anymore 219 int maxCount = 1; 220 int paramCount = 0; 221 222 for (String s : args) { 223 if (s.equals("-F")) { 224 SAOptionError("-F option used"); 225 } 226 if (s.equals("-flag")) { 227 maxCount = 2; 228 } 229 if (! s.startsWith("-")) { 230 paramCount += 1; 231 } 232 } 233 234 if (paramCount > maxCount) { 235 SAOptionError("More than " + maxCount + " non-option argument"); 236 } 237 } 238 239 private static void SAOptionError(String msg) { 240 System.err.println("Error: " + msg); 241 System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jinfo instead"); 242 System.exit(1); 243 } 244 245 // print usage message 246 private static void usage(int exit) { 247 System.err.println("Usage:"); 248 System.err.println(" jinfo <option> <pid>"); 249 System.err.println(" (to connect to a running process)"); 250 System.err.println(""); 251 System.err.println("where <option> is one of:"); 252 System.err.println(" -flag <name> to print the value of the named VM flag"); 253 System.err.println(" -flag [+|-]<name> to enable or disable the named VM flag"); 254 System.err.println(" -flag <name>=<value> to set the named VM flag to the given value"); 255 System.err.println(" -flags to print VM flags"); 256 System.err.println(" -sysprops to print Java system properties"); 257 System.err.println(" <no option> to print both VM flags and system properties"); 258 System.err.println(" -h | -help to print this help message"); 259 System.exit(exit); 260 } 261 }