Path: blob/master/test/jdk/sun/tools/jcmd/JcmdBase.java
41152 views
/*1* Copyright (c) 2013, 2020, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Arrays;2425import jdk.test.lib.Utils;26import jdk.test.lib.process.OutputAnalyzer;27import jdk.test.lib.process.ProcessTools;28import jdk.test.lib.JDKToolLauncher;2930/**31* Helper class for starting jcmd process.32* <pre>33* - jcmd will send diagnostic requests to the current java process:34* jcmd pid_to_current_process PerfCounter.print35* - jcmd will be run without sending request to any JVM36* jcmd -h37* </pre>38*/39public final class JcmdBase {4041private static ProcessBuilder processBuilder = new ProcessBuilder();4243private JcmdBase() {44// Private constructor to prevent class instantiation45}4647/**48* Sends the diagnostic command request to the current process49*50* @see #jcmd(boolean, String[], String[])51*/52public final static OutputAnalyzer jcmd(String... jcmdArgs)53throws Exception {54return jcmd(true, null, jcmdArgs);55}5657/**58* Sends the diagnostic command request to the current process.59* jcmd will be run with specified {@code vmArgs}.60*61* @see #jcmd(boolean, String[], String[])62*/63public final static OutputAnalyzer jcmd(String[] vmArgs,64String[] jcmdArgs) throws Exception {65return jcmd(true, vmArgs, jcmdArgs);66}6768/**69* Runs jcmd without sending request to any JVM70*71* @see #jcmd(boolean, String[], String[])72*/73public final static OutputAnalyzer jcmdNoPid(String[] vmArgs,74String[] jcmdArgs) throws Exception {75return jcmd(false, vmArgs, jcmdArgs);76}7778/**79* If {@code requestToCurrentProcess} is {@code true}80* sends a diagnostic command request to the current process.81* If {@code requestToCurrentProcess} is {@code false}82* runs jcmd without sending request to any JVM.83*84* @param requestToCurrentProcess85* Defines if jcmd will send request to the current process86* @param vmArgs87* jcmd will be run with VM arguments specified,88* e.g. -XX:+UsePerfData89* @param jcmdArgs90* jcmd will be run with option or command and its arguments91* specified, e.g. VM.flags92* @return The output from {@link OutputAnalyzer} object93* @throws Exception94*/95private static final OutputAnalyzer jcmd(boolean requestToCurrentProcess,96String[] vmArgs, String[] jcmdArgs) throws Exception {97JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jcmd");98launcher.addVMArgs(Utils.getTestJavaOpts());99if (vmArgs != null) {100for (String vmArg : vmArgs) {101launcher.addVMArg(vmArg);102}103}104if (requestToCurrentProcess) {105launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));106}107if (jcmdArgs != null) {108for (String toolArg : jcmdArgs) {109launcher.addToolArg(toolArg);110}111}112processBuilder.command(launcher.getCommand());113System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));114OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);115System.out.println(output.getOutput());116117return output;118}119120}121122123