2
+ − 1
/*
+ − 2
* Copyright 2004-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ − 20
* CA 95054 USA or visit www.sun.com if you need additional information or
+ − 21
* have any questions.
+ − 22
*/
+ − 23
+ − 24
+ − 25
/* DemoRun:
+ − 26
*
+ − 27
* Support classes for java jvmti demo tests
+ − 28
*
+ − 29
*/
+ − 30
+ − 31
import java.io.InputStream;
+ − 32
import java.io.IOException;
+ − 33
import java.io.File;
+ − 34
import java.io.BufferedInputStream;
+ − 35
import java.io.PrintStream;
+ − 36
+ − 37
/*
+ − 38
* Helper class to direct process output to a StringBuffer
+ − 39
*/
+ − 40
class MyInputStream implements Runnable {
+ − 41
private String name;
+ − 42
private BufferedInputStream in;
+ − 43
private StringBuffer buffer;
+ − 44
+ − 45
/* Create MyInputStream that saves all output to a StringBuffer */
+ − 46
MyInputStream(String name, InputStream in) {
+ − 47
this.name = name;
+ − 48
this.in = new BufferedInputStream(in);
+ − 49
buffer = new StringBuffer(4096);
+ − 50
Thread thr = new Thread(this);
+ − 51
thr.setDaemon(true);
+ − 52
thr.start();
+ − 53
}
+ − 54
+ − 55
/* Dump the buffer */
+ − 56
void dump(PrintStream x) {
+ − 57
String str = buffer.toString();
+ − 58
x.println("<beginning of " + name + " buffer>");
+ − 59
x.println(str);
+ − 60
x.println("<end of buffer>");
+ − 61
}
+ − 62
+ − 63
/* Check to see if a pattern is inside the output. */
+ − 64
boolean contains(String pattern) {
+ − 65
String str = buffer.toString();
+ − 66
return str.contains(pattern);
+ − 67
}
+ − 68
+ − 69
/* Runs as a separate thread capturing all output in a StringBuffer */
+ − 70
public void run() {
+ − 71
try {
+ − 72
byte b[] = new byte[100];
+ − 73
for (;;) {
+ − 74
int n = in.read(b);
+ − 75
String str;
+ − 76
if (n < 0) {
+ − 77
break;
+ − 78
}
+ − 79
str = new String(b, 0, n);
+ − 80
buffer.append(str);
+ − 81
System.out.print(str);
+ − 82
}
+ − 83
} catch (IOException ioe) { /* skip */ }
+ − 84
}
+ − 85
}
+ − 86
+ − 87
/*
+ − 88
* Main JVMTI Demo Run class.
+ − 89
*/
+ − 90
public class DemoRun {
+ − 91
+ − 92
private String demo_name;
+ − 93
private String demo_options;
+ − 94
private MyInputStream output;
+ − 95
private MyInputStream error;
+ − 96
+ − 97
/* Create a Demo run process */
+ − 98
public DemoRun(String name, String options)
+ − 99
{
+ − 100
demo_name = name;
+ − 101
demo_options = options;
+ − 102
}
+ − 103
+ − 104
/*
+ − 105
* Execute a process with an -agentpath or -agentlib command option
+ − 106
*/
+ − 107
public void runit(String class_name)
+ − 108
{
+ − 109
runit(class_name, null);
+ − 110
}
+ − 111
+ − 112
/*
+ − 113
* Execute a process with an -agentpath or -agentlib command option
+ − 114
* plus any set of other java options.
+ − 115
*/
+ − 116
public void runit(String class_name, String vm_options[])
+ − 117
{
+ − 118
String jre_home = System.getProperty("java.home");
+ − 119
String sdk_home = (jre_home.endsWith("jre") ?
+ − 120
(jre_home + File.separator + "..") :
+ − 121
jre_home );
+ − 122
String cdir = System.getProperty("test.classes", ".");
+ − 123
String os_arch = System.getProperty("os.arch");
+ − 124
String os_name = System.getProperty("os.name");
+ − 125
String libprefix = os_name.contains("Windows")?"":"lib";
+ − 126
String libsuffix = os_name.contains("Windows")?".dll":".so";
+ − 127
boolean d64 = ( os_name.contains("Solaris") ||
+ − 128
os_name.contains("SunOS") )
+ − 129
&& ( os_arch.equals("sparcv9") ||
+ − 130
os_arch.equals("amd64"));
+ − 131
boolean hprof = demo_name.equals("hprof");
+ − 132
String isa_dir = d64?(File.separator+os_arch):"";
+ − 133
String java = jre_home
+ − 134
+ File.separator + "bin" + isa_dir
+ − 135
+ File.separator + "java";
+ − 136
/* Array of strings to be passed in for exec:
+ − 137
* 1. java
+ − 138
* 2. -Dtest.classes=.
+ − 139
* 3. -d64 (optional)
+ − 140
* 4. -Xcheck:jni (Just because it finds bugs)
+ − 141
* 5. -Xverify:all (Make sure verification is on full blast)
+ − 142
* 6. -agent
+ − 143
* vm_options
+ − 144
* 7+i. classname
+ − 145
*/
+ − 146
int nvm_options = 0;
+ − 147
if ( vm_options != null ) nvm_options = vm_options.length;
+ − 148
String cmd[] = new String[1 + (d64?1:0) + 5 + nvm_options];
+ − 149
String cmdLine;
+ − 150
int exitStatus;
+ − 151
int i,j;
+ − 152
+ − 153
i = 0;
+ − 154
cmdLine = "";
+ − 155
cmdLine += (cmd[i++] = java);
+ − 156
cmdLine += " ";
+ − 157
cmdLine += (cmd[i++] = "-Dtest.classes=" + cdir);
+ − 158
if ( d64 ) {
+ − 159
cmdLine += " ";
+ − 160
cmdLine += (cmd[i++] = "-d64");
+ − 161
}
+ − 162
cmdLine += " ";
+ − 163
cmdLine += (cmd[i++] = "-Xcheck:jni");
+ − 164
cmdLine += " ";
+ − 165
cmdLine += (cmd[i++] = "-Xverify:all");
+ − 166
if ( hprof ) {
+ − 167
/* Load hprof with -agentlib since it's part of jre */
+ − 168
cmdLine += " ";
+ − 169
cmdLine += (cmd[i++] = "-agentlib:" + demo_name
+ − 170
+ (demo_options.equals("")?"":("="+demo_options)));
+ − 171
} else {
+ − 172
String libname = sdk_home
+ − 173
+ File.separator + "demo"
+ − 174
+ File.separator + "jvmti"
+ − 175
+ File.separator + demo_name
+ − 176
+ File.separator + "lib" + isa_dir
+ − 177
+ File.separator + libprefix + demo_name + libsuffix;
+ − 178
cmdLine += " ";
+ − 179
cmdLine += (cmd[i++] = "-agentpath:" + libname
+ − 180
+ (demo_options.equals("")?"":("="+demo_options)));
+ − 181
}
+ − 182
/* Add any special VM options */
+ − 183
for ( j = 0; j < nvm_options; j++ ) {
+ − 184
cmdLine += " ";
+ − 185
cmdLine += (cmd[i++] = vm_options[j]);
+ − 186
}
+ − 187
/* Add classname */
+ − 188
cmdLine += " ";
+ − 189
cmdLine += (cmd[i++] = class_name);
+ − 190
+ − 191
/* Begin process */
+ − 192
Process p;
+ − 193
+ − 194
System.out.println("Starting: " + cmdLine);
+ − 195
try {
+ − 196
p = Runtime.getRuntime().exec(cmd);
+ − 197
} catch ( IOException e ) {
+ − 198
throw new RuntimeException("Test failed - exec got IO exception");
+ − 199
}
+ − 200
+ − 201
/* Save process output in StringBuffers */
+ − 202
output = new MyInputStream("Input Stream", p.getInputStream());
+ − 203
error = new MyInputStream("Error Stream", p.getErrorStream());
+ − 204
+ − 205
/* Wait for process to complete, and if exit code is non-zero we fail */
+ − 206
try {
+ − 207
exitStatus = p.waitFor();
+ − 208
if ( exitStatus != 0) {
+ − 209
System.out.println("Exit code is " + exitStatus);
+ − 210
error.dump(System.out);
+ − 211
output.dump(System.out);
+ − 212
throw new RuntimeException("Test failed - " +
+ − 213
"exit return code non-zero " +
+ − 214
"(exitStatus==" + exitStatus + ")");
+ − 215
}
+ − 216
} catch ( InterruptedException e ) {
+ − 217
throw new RuntimeException("Test failed - process interrupted");
+ − 218
}
+ − 219
System.out.println("Completed: " + cmdLine);
+ − 220
}
+ − 221
+ − 222
/* Does the pattern appear in the output of this process */
+ − 223
public boolean output_contains(String pattern)
+ − 224
{
+ − 225
return output.contains(pattern) || error.contains(pattern);
+ − 226
}
+ − 227
}