Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jinfo/JInfo.java
38918 views
/*1* Copyright (c) 2006, 2013, 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.lang.reflect.Method;28import java.io.IOException;29import java.io.InputStream;3031import com.sun.tools.attach.VirtualMachine;32import sun.tools.attach.HotSpotVirtualMachine;3334/*35* This class is the main class for the JInfo utility. It parses its arguments36* and decides if the command should be satisfied using the VM attach mechanism37* or an SA tool. At this time the only option that uses the VM attach38* mechanism is the -flag option to set or print a command line option of a39* running application. All other options are mapped to SA tools.40*/41public class JInfo {4243public static void main(String[] args) throws Exception {44if (args.length == 0) {45usage(1); // no arguments46}4748boolean useSA = true;49String arg1 = args[0];50if (arg1.startsWith("-")) {51if (arg1.equals("-flags") ||52arg1.equals("-sysprops")) {53// SA JInfo needs <pid> or <server> or54// (<executable> and <code file>). So, total55// argument count including option has to 2 or 3.56if (args.length != 2 && args.length != 3) {57usage(1);58}59} else if (arg1.equals("-flag")) {60// do not use SA, use attach-on-demand61useSA = false;62} else {63// unknown option or -h or -help, print help64int exit;65if (arg1.equals("-help") || arg1.equals("-h")) {66exit = 0;67} else {68exit = 1;69}70usage(exit);71}72}7374if (useSA) {75runTool(args);76} else {77if (args.length == 3) {78String pid = args[2];79String option = args[1];80flag(pid, option);81} else {82int exit;83if (arg1.equals("-help") || arg1.equals("-h")) {84exit = 0;85} else {86exit = 1;87}88usage(exit);89}90}91}9293// Invoke SA tool with the given arguments94private static void runTool(String args[]) throws Exception {95String tool = "sun.jvm.hotspot.tools.JInfo";96// Tool not available on this platform.97Class<?> c = loadClass(tool);98if (c == null) {99usage(1);100}101102// invoke the main method with the arguments103Class[] argTypes = { String[].class } ;104Method m = c.getDeclaredMethod("main", argTypes);105106Object[] invokeArgs = { args };107m.invoke(null, invokeArgs);108}109110// loads the given class using the system class loader111private static Class<?> loadClass(String name) {112//113// We specify the system clas loader so as to cater for development114// environments where this class is on the boot class path but sa-jdi.jar115// is on the system class path. Once the JDK is deployed then both116// tools.jar and sa-jdi.jar are on the system class path.117//118try {119return Class.forName(name, true,120ClassLoader.getSystemClassLoader());121} catch (Exception x) { }122return null;123}124125private static void flag(String pid, String option) throws IOException {126VirtualMachine vm = attach(pid);127String flag;128InputStream in;129int index = option.indexOf('=');130if (index != -1) {131flag = option.substring(0, index);132String value = option.substring(index + 1);133in = ((HotSpotVirtualMachine)vm).setFlag(flag, value);134} else {135char c = option.charAt(0);136switch (c) {137case '+':138flag = option.substring(1);139in = ((HotSpotVirtualMachine)vm).setFlag(flag, "1");140break;141case '-':142flag = option.substring(1);143in = ((HotSpotVirtualMachine)vm).setFlag(flag, "0");144break;145default:146flag = option;147in = ((HotSpotVirtualMachine)vm).printFlag(flag);148break;149}150}151152drain(vm, in);153}154155// Attach to <pid>, exiting if we fail to attach156private static VirtualMachine attach(String pid) {157try {158return VirtualMachine.attach(pid);159} catch (Exception x) {160String msg = x.getMessage();161if (msg != null) {162System.err.println(pid + ": " + msg);163} else {164x.printStackTrace();165}166System.exit(1);167return null; // keep compiler happy168}169}170171// Read the stream from the target VM until EOF, then detach172private static void drain(VirtualMachine vm, InputStream in) throws IOException {173// read to EOF and just print output174byte b[] = new byte[256];175int n;176do {177n = in.read(b);178if (n > 0) {179String s = new String(b, 0, n, "UTF-8");180System.out.print(s);181}182} while (n > 0);183in.close();184vm.detach();185}186187188// print usage message189private static void usage(int exit) {190191Class<?> c = loadClass("sun.jvm.hotspot.tools.JInfo");192boolean usageSA = (c != null);193194System.err.println("Usage:");195if (usageSA) {196System.err.println(" jinfo [option] <pid>");197System.err.println(" (to connect to running process)");198System.err.println(" jinfo [option] <executable <core>");199System.err.println(" (to connect to a core file)");200System.err.println(" jinfo [option] [server_id@]<remote server IP or hostname>");201System.err.println(" (to connect to remote debug server)");202System.err.println("");203System.err.println("where <option> is one of:");204System.err.println(" -flag <name> to print the value of the named VM flag");205System.err.println(" -flag [+|-]<name> to enable or disable the named VM flag");206System.err.println(" -flag <name>=<value> to set the named VM flag to the given value");207System.err.println(" -flags to print VM flags");208System.err.println(" -sysprops to print Java system properties");209System.err.println(" <no option> to print both of the above");210System.err.println(" -h | -help to print this help message");211} else {212System.err.println(" jinfo <option> <pid>");213System.err.println(" (to connect to a running process)");214System.err.println("");215System.err.println("where <option> is one of:");216System.err.println(" -flag <name> to print the value of the named VM flag");217System.err.println(" -flag [+|-]<name> to enable or disable the named VM flag");218System.err.println(" -flag <name>=<value> to set the named VM flag to the given value");219System.err.println(" -h | -help to print this help message");220}221222System.exit(exit);223}224}225226227