1 /* 2 * Copyright (c) 2013, 2018, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.util.Arrays; 25 26 import jdk.testlibrary.OutputAnalyzer; 27 import jdk.testlibrary.ProcessTools; 28 import jdk.testlibrary.JDKToolLauncher; 29 30 /** 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.print 35 * - jcmd will be run without sending request to any JVM 36 * jcmd -h 37 * </pre> 38 */ 39 public final class JcmdBase { 40 41 private static ProcessBuilder processBuilder = new ProcessBuilder(); 42 43 private JcmdBase() { 44 // Private constructor to prevent class instantiation 45 } 46 47 /** 48 * Sends the diagnostic command request to the current process 49 * 50 * @see #jcmd(boolean, String[], String[]) 51 */ 52 public final static OutputAnalyzer jcmd(String... jcmdArgs) 53 throws Exception { 54 return jcmd(true, null, jcmdArgs); 55 } 56 57 /** 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 */ 63 public final static OutputAnalyzer jcmd(String[] vmArgs, 64 String[] jcmdArgs) throws Exception { 65 return jcmd(true, vmArgs, jcmdArgs); 66 } 67 68 /** 69 * Runs jcmd without sending request to any JVM 70 * 71 * @see #jcmd(boolean, String[], String[]) 72 */ 73 public final static OutputAnalyzer jcmdNoPid(String[] vmArgs, 74 String[] jcmdArgs) throws Exception { 75 return jcmd(false, vmArgs, jcmdArgs); 76 } 77 78 /** 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 requestToCurrentProcess 85 * Defines if jcmd will send request to the current process 86 * @param vmArgs 87 * jcmd will be run with VM arguments specified, 88 * e.g. -XX:+UsePerfData 89 * @param jcmdArgs 90 * jcmd will be run with option or command and its arguments 91 * specified, e.g. VM.flags 92 * @return The output from {@link OutputAnalyzer} object 93 * @throws Exception 94 */ 95 private static final OutputAnalyzer jcmd(boolean requestToCurrentProcess, 96 String[] vmArgs, String[] jcmdArgs) throws Exception { 97 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jcmd"); 98 if (vmArgs != null) { 99 for (String vmArg : vmArgs) { 100 launcher.addVMArg(vmArg); 101 } 102 } 103 if (requestToCurrentProcess) { 104 launcher.addToolArg(Long.toString(ProcessTools.getProcessId())); 105 } 106 if (jcmdArgs != null) { 107 for (String toolArg : jcmdArgs) { 108 launcher.addToolArg(toolArg); 109 } 110 } 111 processBuilder.command(launcher.getCommand()); 112 System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); 113 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); 114 System.out.println(output.getOutput()); 115 116 return output; 117 } 118 119 }