Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jhat/HatRun.java
38841 views
/*1* Copyright (c) 2005, 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.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*/222324/*25*26* Support classes for running jhat tests27*28*/2930import java.io.InputStream;31import java.io.IOException;32import java.io.File;33import java.io.BufferedInputStream;34import java.io.PrintStream;3536/*37* Helper class to direct process output to a StringBuffer38*/39class MyInputStream implements Runnable {40private String name;41private BufferedInputStream in;42private StringBuffer buffer;4344/* Create MyInputStream that saves all output to a StringBuffer */45MyInputStream(String name, InputStream in) {46this.name = name;47this.in = new BufferedInputStream(in);48buffer = new StringBuffer(4096);49Thread thr = new Thread(this);50thr.setDaemon(true);51thr.start();52}5354/* Dump the buffer */55void dump(PrintStream x) {56String str = buffer.toString();57x.println("<beginning of " + name + " buffer>");58x.println(str);59x.println("<end of buffer>");60}6162/* Check to see if a pattern is inside the output. */63boolean contains(String pattern) {64String str = buffer.toString();65return str.contains(pattern);66}6768/* Runs as a separate thread capturing all output in a StringBuffer */69public void run() {70try {71byte b[] = new byte[100];72for (;;) {73int n = in.read(b);74String str;75if (n < 0) {76break;77}78str = new String(b, 0, n);79buffer.append(str);80System.out.print(str);81}82} catch (IOException ioe) { /* skip */ }83}84}8586/*87* Main jhat run88*/89public class HatRun {9091private String all_hprof_options;92private String all_hat_options;93private String dumpfile;94private MyInputStream output;95private MyInputStream error;9697/* Create a Hat run process */98public HatRun(String hprof_options, String hat_options)99{100all_hprof_options = hprof_options;101all_hat_options = hat_options;102}103104/*105* Execute a process with an -agentpath or -agentlib command option106*/107public void runit(String class_name)108{109runit(class_name, null);110}111112/*113* Execute a command.114*/115private void execute(String cmd[])116{117/* Begin process */118Process p;119String cmdLine = "";120int i;121122for ( i = 0 ; i < cmd.length; i++ ) {123cmdLine += cmd[i];124cmdLine += " ";125}126System.out.println("Starting: " + cmdLine);127128try {129p = Runtime.getRuntime().exec(cmd);130} catch ( IOException e ) {131throw new RuntimeException("Test failed - exec got IO exception");132}133134/* Save process output in StringBuffers */135output = new MyInputStream("Input Stream", p.getInputStream());136error = new MyInputStream("Error Stream", p.getErrorStream());137138/* Wait for process to complete, and if exit code is non-zero we fail */139try {140int exitStatus;141exitStatus = p.waitFor();142if ( exitStatus != 0) {143System.out.println("Exit code is " + exitStatus);144error.dump(System.out);145output.dump(System.out);146throw new RuntimeException("Test failed - " +147"exit return code non-zero " +148"(exitStatus==" + exitStatus + ")");149}150} catch ( InterruptedException e ) {151throw new RuntimeException("Test failed - process interrupted");152}153System.out.println("Completed: " + cmdLine);154}155156/*157* Execute a process with an -agentpath or -agentlib command option158* plus any set of other java options.159*/160public void runit(String class_name, String vm_options[])161{162String jre_home = System.getProperty("java.home");163String sdk_home = (jre_home.endsWith("jre") ?164(jre_home + File.separator + "..") :165jre_home );166String cdir = System.getProperty("test.classes", ".");167String os_arch = System.getProperty("os.arch");168String os_name = System.getProperty("os.name");169String java = jre_home170+ File.separator + "bin"171+ File.separator + "java";172String jhat = sdk_home + File.separator + "bin"173+ File.separator + "jhat";174/* Array of strings to be passed in for exec:175* 1. java176* 2. -Dtest.classes=.177* 3. -Xcheck:jni (Just because it finds bugs)178* 4. -Xverify:all (Make sure verification is on full blast)179* 5. -agent180* vm_options181* 6+i. classname182*/183int nvm_options = 0;184if ( vm_options != null ) nvm_options = vm_options.length;185String cmd[] = new String[1 + 7 + nvm_options];186int i,j;187188i = 0;189cmd[i++] = java;190cmd[i++] = "-cp";191cmd[i++] = cdir;192cmd[i++] = "-Dtest.classes=" + cdir;193cmd[i++] = "-Xcheck:jni";194cmd[i++] = "-Xverify:all";195dumpfile= cdir + File.separator + class_name + ".hdump";196cmd[i++] = "-agentlib:hprof=" + all_hprof_options197+ ",format=b,file=" + dumpfile;198/* Add any special VM options */199for ( j = 0; j < nvm_options; j++ ) {200cmd[i++] = vm_options[j];201}202/* Add classname */203cmd[i++] = class_name;204205/* Execute process */206execute(cmd);207208/* Run jhat */209String jhat_cmd[] = new String[4];210jhat_cmd[0] = jhat;211jhat_cmd[1] = "-debug";212jhat_cmd[2] = "2";213jhat_cmd[3] = dumpfile;214215/* Execute process */216execute(jhat_cmd);217218}219220/* Does the pattern appear in the output of this process */221public boolean output_contains(String pattern)222{223return output.contains(pattern) || error.contains(pattern);224}225}226227228