0N/A/*
3261N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.jinfo;
0N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/A
0N/Aimport com.sun.tools.attach.VirtualMachine;
0N/Aimport sun.tools.attach.HotSpotVirtualMachine;
0N/A
0N/A/*
0N/A * This class is the main class for the JInfo utility. It parses its arguments
0N/A * and decides if the command should be satisifed using the VM attach mechanism
0N/A * or an SA tool. At this time the only option that uses the VM attach
0N/A * mechanism is the -flag option to set or print a command line option of a
0N/A * running application. All other options are mapped to SA tools.
0N/A */
0N/Apublic class JInfo {
0N/A
2507N/A public static void main(String[] args) throws Exception {
0N/A if (args.length == 0) {
0N/A usage(); // no arguments
0N/A }
0N/A
0N/A boolean useSA = true;
0N/A String arg1 = args[0];
0N/A if (arg1.startsWith("-")) {
0N/A if (arg1.equals("-flags") ||
0N/A arg1.equals("-sysprops")) {
0N/A // SA JInfo needs <pid> or <server> or
0N/A // (<executable> and <code file>). So, total
0N/A // argument count including option has to 2 or 3.
0N/A if (args.length != 2 && args.length != 3) {
0N/A usage();
0N/A }
0N/A } else if (arg1.equals("-flag")) {
0N/A // do not use SA, use attach-on-demand
0N/A useSA = false;
0N/A } else {
0N/A // unknown option or -h or -help, print help
0N/A usage();
0N/A }
0N/A }
0N/A
0N/A if (useSA) {
0N/A runTool(args);
0N/A } else {
0N/A if (args.length == 3) {
0N/A String pid = args[2];
0N/A String option = args[1];
0N/A flag(pid, option);
0N/A } else {
0N/A usage();
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Invoke SA tool with the given arguments
0N/A private static void runTool(String args[]) throws Exception {
0N/A String tool = "sun.jvm.hotspot.tools.JInfo";
0N/A // Tool not available on this platform.
0N/A Class<?> c = loadClass(tool);
0N/A if (c == null) {
0N/A usage();
0N/A }
0N/A
0N/A // invoke the main method with the arguments
0N/A Class[] argTypes = { String[].class } ;
0N/A Method m = c.getDeclaredMethod("main", argTypes);
0N/A
0N/A Object[] invokeArgs = { args };
0N/A m.invoke(null, invokeArgs);
0N/A }
0N/A
0N/A // loads the given class using the system class loader
0N/A private static Class loadClass(String name) {
0N/A //
0N/A // We specify the system clas loader so as to cater for development
0N/A // environments where this class is on the boot class path but sa-jdi.jar
0N/A // is on the system class path. Once the JDK is deployed then both
0N/A // tools.jar and sa-jdi.jar are on the system class path.
0N/A //
0N/A try {
0N/A return Class.forName(name, true,
0N/A ClassLoader.getSystemClassLoader());
0N/A } catch (Exception x) { }
0N/A return null;
0N/A }
0N/A
0N/A private static void flag(String pid, String option) throws IOException {
0N/A VirtualMachine vm = attach(pid);
0N/A String flag;
2507N/A InputStream in;
0N/A int index = option.indexOf('=');
0N/A if (index != -1) {
0N/A flag = option.substring(0, index);
0N/A String value = option.substring(index + 1);
0N/A in = ((HotSpotVirtualMachine)vm).setFlag(flag, value);
0N/A } else {
0N/A char c = option.charAt(0);
0N/A switch (c) {
0N/A case '+':
0N/A flag = option.substring(1);
0N/A in = ((HotSpotVirtualMachine)vm).setFlag(flag, "1");
0N/A break;
0N/A case '-':
0N/A flag = option.substring(1);
0N/A in = ((HotSpotVirtualMachine)vm).setFlag(flag, "0");
0N/A break;
0N/A default:
0N/A flag = option;
0N/A in = ((HotSpotVirtualMachine)vm).printFlag(flag);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A drain(vm, in);
0N/A }
0N/A
0N/A // Attach to <pid>, exiting if we fail to attach
0N/A private static VirtualMachine attach(String pid) {
0N/A try {
0N/A return VirtualMachine.attach(pid);
0N/A } catch (Exception x) {
0N/A String msg = x.getMessage();
0N/A if (msg != null) {
0N/A System.err.println(pid + ": " + msg);
0N/A } else {
0N/A x.printStackTrace();
0N/A }
0N/A System.exit(1);
0N/A return null; // keep compiler happy
0N/A }
0N/A }
0N/A
0N/A // Read the stream from the target VM until EOF, then detach
0N/A private static void drain(VirtualMachine vm, InputStream in) throws IOException {
0N/A // read to EOF and just print output
0N/A byte b[] = new byte[256];
0N/A int n;
0N/A do {
0N/A n = in.read(b);
0N/A if (n > 0) {
0N/A String s = new String(b, 0, n, "UTF-8");
0N/A System.out.print(s);
0N/A }
0N/A } while (n > 0);
0N/A in.close();
0N/A vm.detach();
0N/A }
0N/A
0N/A
0N/A // print usage message
0N/A private static void usage() {
0N/A
0N/A Class c = loadClass("sun.jvm.hotspot.tools.JInfo");
0N/A boolean usageSA = (c != null);
0N/A
0N/A System.out.println("Usage:");
0N/A if (usageSA) {
0N/A System.out.println(" jinfo [option] <pid>");
0N/A System.out.println(" (to connect to running process)");
0N/A System.out.println(" jinfo [option] <executable <core>");
0N/A System.out.println(" (to connect to a core file)");
0N/A System.out.println(" jinfo [option] [server_id@]<remote server IP or hostname>");
0N/A System.out.println(" (to connect to remote debug server)");
0N/A System.out.println("");
0N/A System.out.println("where <option> is one of:");
0N/A System.out.println(" -flag <name> to print the value of the named VM flag");
0N/A System.out.println(" -flag [+|-]<name> to enable or disable the named VM flag");
0N/A System.out.println(" -flag <name>=<value> to set the named VM flag to the given value");
0N/A System.out.println(" -flags to print VM flags");
0N/A System.out.println(" -sysprops to print Java system properties");
0N/A System.out.println(" <no option> to print both of the above");
0N/A System.out.println(" -h | -help to print this help message");
0N/A } else {
0N/A System.out.println(" jinfo <option> <pid>");
0N/A System.out.println(" (to connect to a running process)");
0N/A System.out.println("");
0N/A System.out.println("where <option> is one of:");
0N/A System.out.println(" -flag <name> to print the value of the named VM flag");
0N/A System.out.println(" -flag [+|-]<name> to enable or disable the named VM flag");
0N/A System.out.println(" -flag <name>=<value> to set the named VM flag to the given value");
0N/A System.out.println(" -h | -help to print this help message");
0N/A }
0N/A
0N/A System.exit(1);
0N/A }
0N/A}
0N/A