diff --git a/jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java b/jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java index 4a9562b069f..a5e965e5ec3 100644 --- a/jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java +++ b/jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java @@ -258,7 +258,7 @@ public abstract class HotSpotVirtualMachine extends VirtualMachine { /* * Convenience method for simple commands */ - private InputStream executeCommand(String cmd, Object ... args) throws IOException { + public InputStream executeCommand(String cmd, Object ... args) throws IOException { try { return execute(cmd, args); } catch (AgentLoadException x) { diff --git a/jdk/src/jdk.jcmd/share/classes/jdk/internal/vm/agent/spi/ToolProvider.java b/jdk/src/jdk.jcmd/share/classes/jdk/internal/vm/agent/spi/ToolProvider.java deleted file mode 100644 index 55be308348a..00000000000 --- a/jdk/src/jdk.jcmd/share/classes/jdk/internal/vm/agent/spi/ToolProvider.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.internal.vm.agent.spi; - -/** - * Service interface for jdk.hotspot.agent to provide the tools that - * jstack, jmap, jinfo will invoke, if present. - */ -public interface ToolProvider { - /** - * Returns the name of the tool provider - */ - String getName(); - - /** - * Invoke the tool provider with the given arguments - */ - void run(String... arguments); -} diff --git a/jdk/src/jdk.jcmd/share/classes/jdk/internal/vm/agent/spi/ToolProviderFinder.java b/jdk/src/jdk.jcmd/share/classes/jdk/internal/vm/agent/spi/ToolProviderFinder.java deleted file mode 100644 index b96e6ae6239..00000000000 --- a/jdk/src/jdk.jcmd/share/classes/jdk/internal/vm/agent/spi/ToolProviderFinder.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.internal.vm.agent.spi; - -import java.lang.reflect.Layer; -import java.util.HashMap; -import java.util.Map; -import java.util.ServiceLoader; - -public final class ToolProviderFinder { - private static final Map providers = init(); - - public static ToolProvider find(String name) { - return providers.get(name); - } - - private static Map init() { - Map providers = new HashMap<>(); - ServiceLoader.load(Layer.boot(), ToolProvider.class) - .forEach(p -> providers.putIfAbsent(p.getName(), p)); - return providers; - } -} diff --git a/jdk/src/jdk.jcmd/share/classes/module-info.java b/jdk/src/jdk.jcmd/share/classes/module-info.java index 4b8dbce1304..c78803b774c 100644 --- a/jdk/src/jdk.jcmd/share/classes/module-info.java +++ b/jdk/src/jdk.jcmd/share/classes/module-info.java @@ -26,9 +26,4 @@ module jdk.jcmd { requires jdk.attach; requires jdk.jvmstat; - - exports jdk.internal.vm.agent.spi to jdk.hotspot.agent; - - uses jdk.internal.vm.agent.spi.ToolProvider; } - diff --git a/jdk/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java b/jdk/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java index f19b6d159f0..dbfbaf2e25b 100644 --- a/jdk/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java +++ b/jdk/src/jdk.jcmd/share/classes/sun/tools/jinfo/JInfo.java @@ -32,8 +32,6 @@ import java.io.InputStream; import com.sun.tools.attach.VirtualMachine; import sun.tools.attach.HotSpotVirtualMachine; -import jdk.internal.vm.agent.spi.ToolProvider; -import jdk.internal.vm.agent.spi.ToolProviderFinder; /* * This class is the main class for the JInfo utility. It parses its arguments @@ -41,155 +39,73 @@ import jdk.internal.vm.agent.spi.ToolProviderFinder; * or an SA tool. */ final public class JInfo { - private static final String SA_JINFO_TOOL_NAME = "jinfo"; - private boolean useSA = false; - private String[] args = null; - - private JInfo(String[] args) throws IllegalArgumentException { - if (args.length == 0) { - throw new IllegalArgumentException(); - } - - int argCopyIndex = 0; - // First determine if we should launch SA or not - if (args[0].equals("-F")) { - // delete the -F - argCopyIndex = 1; - useSA = true; - } else if (args[0].equals("-flags") - || args[0].equals("-sysprops")) - { - if (args.length == 2) { - if (!isPid(args[1])) { - // If args[1] doesn't parse to a number then - // it must be the SA debug server - // (otherwise it is the pid) - useSA = true; - } - } else if (args.length == 3) { - // arguments include an executable and a core file - useSA = true; - } else { - throw new IllegalArgumentException(); - } - } else if (!args[0].startsWith("-")) { - if (args.length == 2) { - // the only arguments are an executable and a core file - useSA = true; - } else if (args.length == 1) { - if (!isPid(args[0])) { - // The only argument is not a PID; it must be SA debug - // server - useSA = true; - } - } else { - throw new IllegalArgumentException(); - } - } else if (args[0].equals("-h") || args[0].equals("-help")) { - if (args.length > 1) { - throw new IllegalArgumentException(); - } - } else if (args[0].equals("-flag")) { - if (args.length == 3) { - if (!isPid(args[2])) { - throw new IllegalArgumentException(); - } - } else { - throw new IllegalArgumentException(); - } - } else { - throw new IllegalArgumentException(); - } - - this.args = Arrays.copyOfRange(args, argCopyIndex, args.length); - } - - @SuppressWarnings("fallthrough") - private void execute() throws Exception { - if (args[0].equals("-h") - || args[0].equals("-help")) { - usage(0); - } - - if (useSA) { - // SA only supports -flags or -sysprops - if (args[0].startsWith("-")) { - if (!(args[0].equals("-flags") || args[0].equals("-sysprops"))) { - usage(1); - } - } - - // invoke SA which does it's own argument parsing - runTool(); - - } else { - // Now we can parse arguments for the non-SA case - String pid = null; - - switch(args[0]) { - case "-flag": - if (args.length != 3) { - usage(1); - } - String option = args[1]; - pid = args[2]; - flag(pid, option); - break; - case "-flags": - if (args.length != 2) { - usage(1); - } - pid = args[1]; - flags(pid); - break; - case "-sysprops": - if (args.length != 2) { - usage(1); - } - pid = args[1]; - sysprops(pid); - break; - case "-help": - case "-h": - usage(0); - // Fall through - default: - if (args.length == 1) { - // no flags specified, we do -sysprops and -flags - pid = args[0]; - sysprops(pid); - System.out.println(); - flags(pid); - System.out.println(); - commandLine(pid); - } else { - usage(1); - } - } - } - } public static void main(String[] args) throws Exception { - JInfo jinfo = null; - try { - jinfo = new JInfo(args); - jinfo.execute(); - } catch (IllegalArgumentException e) { + if (args.length == 0) { + usage(1); // no arguments + } + checkForUnsupportedOptions(args); + + boolean doFlag = false; + boolean doFlags = false; + boolean doSysprops = false; + + // Parse the options (arguments starting with "-" ) + int optionCount = 0; + while (optionCount < args.length) { + String arg = args[optionCount]; + if (!arg.startsWith("-")) { + break; + } + + optionCount++; + + if (arg.equals("-help") || arg.equals("-h")) { + usage(0); + } + + if (arg.equals("-flag")) { + doFlag = true; + continue; + } + + if (arg.equals("-flags")) { + doFlags = true; + continue; + } + + if (arg.equals("-sysprops")) { + doSysprops = true; + continue; + } + } + + // Next we check the parameter count. -flag allows extra parameters + int paramCount = args.length - optionCount; + if ((doFlag && paramCount != 2) || (paramCount != 1)) { usage(1); } - } - private static boolean isPid(String arg) { - return arg.matches("[0-9]+"); - } - - // Invoke SA tool with the given arguments - private void runTool() throws Exception { - ToolProvider tool = ToolProviderFinder.find(SA_JINFO_TOOL_NAME); - if (tool == null) { - usage(1); + if (!doFlag && !doFlags && !doSysprops) { + // Print flags and sysporps if no options given + sysprops(args[optionCount]); + System.out.println(); + flags(args[optionCount]); + System.out.println(); + commandLine(args[optionCount]); + } + + if (doFlag) { + flag(args[optionCount+1], args[optionCount]); + } + else { + if (doFlags) { + flags(args[optionCount]); + } + else if (doSysprops) { + sysprops(args[optionCount]); + } } - tool.run(args); } private static void flag(String pid, String option) throws IOException { @@ -274,46 +190,50 @@ final public class JInfo { vm.detach(); } + private static void checkForUnsupportedOptions(String[] args) { + // Check arguments for -F, and non-numeric value + // and warn the user that SA is not supported anymore - // print usage message - private static void usage(int exit) { - boolean usageSA = ToolProviderFinder.find(SA_JINFO_TOOL_NAME) != null; + int paramCount = 0; - System.err.println("Usage:"); - if (usageSA) { - System.err.println(" jinfo [option] "); - System.err.println(" (to connect to a running process)"); - System.err.println(" jinfo -F [option] "); - System.err.println(" (to connect to a hung process)"); - System.err.println(" jinfo [option] "); - System.err.println(" (to connect to a core file)"); - System.err.println(" jinfo [option] [server_id@]"); - System.err.println(" (to connect to remote debug server)"); - System.err.println(""); - System.err.println("where