jdk/test/demo/jvmti/DemoRun.java
author anthony
Mon, 23 Jun 2008 16:03:25 +0400
changeset 1178 4734cf2a22bf
parent 2 90ce3da70b43
child 4229 b09f9b9f75c5
permissions -rw-r--r--
6704896: FD_SET usage can cause stack corruption (sol) Summary: Using poll() instead of select() Reviewed-by: yan, denis
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/* DemoRun:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Support classes for java jvmti demo tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Helper class to direct process output to a StringBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class MyInputStream implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private String              name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private BufferedInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private StringBuffer        buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* Create MyInputStream that saves all output to a StringBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    MyInputStream(String name, InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this.in = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        buffer = new StringBuffer(4096);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Thread thr = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* Dump the buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    void dump(PrintStream x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        String str = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        x.println("<beginning of " + name + " buffer>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        x.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        x.println("<end of buffer>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* Check to see if a pattern is inside the output. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    boolean contains(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String str = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return str.contains(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* Runs as a separate thread capturing all output in a StringBuffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                int n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                str = new String(b, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                buffer.append(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                System.out.print(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } catch (IOException ioe) { /* skip */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * Main JVMTI Demo Run class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
public class DemoRun {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String        demo_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String        demo_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private MyInputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private MyInputStream error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /* Create a Demo run process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public DemoRun(String name, String options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        demo_name    = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        demo_options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Execute a process with an -agentpath or -agentlib command option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void runit(String class_name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        runit(class_name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Execute a process with an -agentpath or -agentlib command option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *    plus any set of other java options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void runit(String class_name, String vm_options[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String jre_home  = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String sdk_home  = (jre_home.endsWith("jre") ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            (jre_home + File.separator + "..") :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                            jre_home );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        String cdir      = System.getProperty("test.classes", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String os_arch   = System.getProperty("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String os_name   = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String libprefix = os_name.contains("Windows")?"":"lib";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String libsuffix = os_name.contains("Windows")?".dll":".so";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        boolean d64      =    ( os_name.contains("Solaris") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                os_name.contains("SunOS") )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                           && ( os_arch.equals("sparcv9") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                os_arch.equals("amd64"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        boolean hprof    = demo_name.equals("hprof");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String isa_dir   = d64?(File.separator+os_arch):"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String java      = jre_home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                             + File.separator + "bin" + isa_dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                             + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /* Array of strings to be passed in for exec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         *   1. java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         *   2. -Dtest.classes=.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         *   3. -d64                 (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         *   4. -Xcheck:jni          (Just because it finds bugs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         *   5. -Xverify:all         (Make sure verification is on full blast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         *   6. -agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         *       vm_options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         *   7+i. classname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int nvm_options = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if ( vm_options != null ) nvm_options = vm_options.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        String cmd[]     = new String[1 + (d64?1:0) + 5 + nvm_options];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        String cmdLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int exitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        cmdLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        cmdLine += (cmd[i++] = java);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        cmdLine += (cmd[i++] = "-Dtest.classes=" + cdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if ( d64 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            cmdLine += (cmd[i++] = "-d64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        cmdLine += (cmd[i++] = "-Xcheck:jni");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        cmdLine += (cmd[i++] = "-Xverify:all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ( hprof ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            /* Load hprof with -agentlib since it's part of jre */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            cmdLine += (cmd[i++] = "-agentlib:" + demo_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                     + (demo_options.equals("")?"":("="+demo_options)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            String libname  = sdk_home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                         + File.separator + "demo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                         + File.separator + "jvmti"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                         + File.separator + demo_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                         + File.separator + "lib" + isa_dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                         + File.separator + libprefix + demo_name + libsuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            cmdLine += (cmd[i++] = "-agentpath:" + libname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                     + (demo_options.equals("")?"":("="+demo_options)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /* Add any special VM options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        for ( j = 0; j < nvm_options; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            cmdLine += (cmd[i++] = vm_options[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /* Add classname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        cmdLine += (cmd[i++] = class_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /* Begin process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Process p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        System.out.println("Starting: " + cmdLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new RuntimeException("Test failed - exec got IO exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /* Save process output in StringBuffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        output = new MyInputStream("Input Stream", p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        error  = new MyInputStream("Error Stream", p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /* Wait for process to complete, and if exit code is non-zero we fail */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            exitStatus = p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if ( exitStatus != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                System.out.println("Exit code is " + exitStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                error.dump(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                output.dump(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                throw new RuntimeException("Test failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                    "exit return code non-zero " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                    "(exitStatus==" + exitStatus + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } catch ( InterruptedException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new RuntimeException("Test failed - process interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        System.out.println("Completed: " + cmdLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /* Does the pattern appear in the output of this process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public boolean output_contains(String pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return output.contains(pattern) || error.contains(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}