Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/jvmti/DemoRun.java
38833 views
/*1* Copyright (c) 2004, 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/* DemoRun:25*26* Support classes for java jvmti demo 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 JVMTI Demo Run class.88*/89public class DemoRun {9091private String demo_name;92private String demo_options;93private MyInputStream output;94private MyInputStream error;9596/* Create a Demo run process */97public DemoRun(String name, String options)98{99demo_name = name;100demo_options = options;101}102103/*104* Execute a process with an -agentpath or -agentlib command option105*/106public void runit(String class_name)107{108runit(class_name, null);109}110111/*112* Execute a process with an -agentpath or -agentlib command option113* plus any set of other java options.114*/115public void runit(String class_name, String vm_options[])116{117String jre_home = System.getProperty("java.home");118String sdk_home = (jre_home.endsWith("jre") ?119(jre_home + File.separator + "..") :120jre_home );121String cdir = System.getProperty("test.classes", ".");122String os_arch = System.getProperty("os.arch");123String os_name = System.getProperty("os.name");124String libprefix = os_name.contains("Windows")?"":"lib";125String libsuffix = os_name.contains("Windows")?".dll":126os_name.contains("OS X")?".dylib":".so";127boolean hprof = demo_name.equals("hprof");128String java = jre_home129+ File.separator + "bin"130+ File.separator + "java";131/* Array of strings to be passed in for exec:132* 1. java133* 2. -Dtest.classes=.134* 3. -Xcheck:jni (Just because it finds bugs)135* 4. -Xverify:all (Make sure verification is on full blast)136* 5. -agent137* vm_options138* 6+i. classname139*/140int nvm_options = 0;141if ( vm_options != null ) nvm_options = vm_options.length;142String cmd[] = new String[1 + 7 + nvm_options];143String cmdLine;144int exitStatus;145int i,j;146147i = 0;148cmdLine = "";149cmdLine += (cmd[i++] = java);150cmdLine += " ";151cmdLine += (cmd[i++] = "-cp");152cmdLine += " ";153cmdLine += (cmd[i++] = cdir);154cmdLine += " ";155cmdLine += (cmd[i++] = "-Dtest.classes=" + cdir);156cmdLine += " ";157cmdLine += (cmd[i++] = "-Xcheck:jni");158cmdLine += " ";159cmdLine += (cmd[i++] = "-Xverify:all");160if ( hprof ) {161/* Load hprof with -agentlib since it's part of jre */162cmdLine += " ";163cmdLine += (cmd[i++] = "-agentlib:" + demo_name164+ (demo_options.equals("")?"":("="+demo_options)));165} else {166String libname = sdk_home167+ File.separator + "demo"168+ File.separator + "jvmti"169+ File.separator + demo_name170+ File.separator + "lib"171+ File.separator + libprefix + demo_name + libsuffix;172cmdLine += " ";173cmdLine += (cmd[i++] = "-agentpath:" + libname174+ (demo_options.equals("")?"":("="+demo_options)));175}176/* Add any special VM options */177for ( j = 0; j < nvm_options; j++ ) {178cmdLine += " ";179cmdLine += (cmd[i++] = vm_options[j]);180}181/* Add classname */182cmdLine += " ";183cmdLine += (cmd[i++] = class_name);184185/* Begin process */186Process p;187188System.out.println("Starting: " + cmdLine);189try {190p = Runtime.getRuntime().exec(cmd);191} catch ( IOException e ) {192throw new RuntimeException("Test failed - exec got IO exception");193}194195/* Save process output in StringBuffers */196output = new MyInputStream("Input Stream", p.getInputStream());197error = new MyInputStream("Error Stream", p.getErrorStream());198199/* Wait for process to complete, and if exit code is non-zero we fail */200try {201exitStatus = p.waitFor();202if ( exitStatus != 0) {203System.out.println("Exit code is " + exitStatus);204error.dump(System.out);205output.dump(System.out);206throw new RuntimeException("Test failed - " +207"exit return code non-zero " +208"(exitStatus==" + exitStatus + ")");209}210} catch ( InterruptedException e ) {211throw new RuntimeException("Test failed - process interrupted");212}213System.out.println("Completed: " + cmdLine);214}215216/* Does the pattern appear in the output of this process */217public boolean output_contains(String pattern)218{219return output.contains(pattern) || error.contains(pattern);220}221}222223224