jdk/test/demo/jvmti/DemoRun.java
author pchelko
Fri, 18 Apr 2014 10:47:23 +0400
changeset 24520 e8afd90fcb69
parent 20201 50cc2d25a60b
child 28859 6c9e357aff59
permissions -rw-r--r--
8035169: Move ThreadGroupUtils from the sun.misc package Reviewed-by: anthony, serb, azvegint
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
20201
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
     2
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4229
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4229
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4229
diff changeset
    21
 * questions.
2
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";
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   126
        String libsuffix = os_name.contains("Windows")?".dll":
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
   127
                                os_name.contains("OS X")?".dylib":".so";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        boolean hprof    = demo_name.equals("hprof");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        String java      = jre_home
20201
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   130
                             + File.separator + "bin"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                             + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /* Array of strings to be passed in for exec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         *   1. java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         *   2. -Dtest.classes=.
20201
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   135
         *   3. -Xcheck:jni          (Just because it finds bugs)
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   136
         *   4. -Xverify:all         (Make sure verification is on full blast)
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   137
         *   5. -agent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         *       vm_options
20201
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   139
         *   6+i. classname
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int nvm_options = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if ( vm_options != null ) nvm_options = vm_options.length;
20201
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   143
        String cmd[]     = new String[1 + 7 + nvm_options];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String cmdLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int exitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        cmdLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        cmdLine += (cmd[i++] = java);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        cmdLine += " ";
4229
b09f9b9f75c5 6899444: Fix demo/jvmti tests so they can run in jtreg samevm mode, cleanup problemlist
ohair
parents: 2
diff changeset
   152
        cmdLine += (cmd[i++] = "-cp");
b09f9b9f75c5 6899444: Fix demo/jvmti tests so they can run in jtreg samevm mode, cleanup problemlist
ohair
parents: 2
diff changeset
   153
        cmdLine += " ";
b09f9b9f75c5 6899444: Fix demo/jvmti tests so they can run in jtreg samevm mode, cleanup problemlist
ohair
parents: 2
diff changeset
   154
        cmdLine += (cmd[i++] = cdir);
b09f9b9f75c5 6899444: Fix demo/jvmti tests so they can run in jtreg samevm mode, cleanup problemlist
ohair
parents: 2
diff changeset
   155
        cmdLine += " ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        cmdLine += (cmd[i++] = "-Dtest.classes=" + cdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        cmdLine += (cmd[i++] = "-Xcheck:jni");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        cmdLine += (cmd[i++] = "-Xverify:all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if ( hprof ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            /* Load hprof with -agentlib since it's part of jre */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            cmdLine += (cmd[i++] = "-agentlib:" + demo_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                     + (demo_options.equals("")?"":("="+demo_options)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            String libname  = sdk_home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                         + File.separator + "demo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                         + File.separator + "jvmti"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                         + File.separator + demo_name
20201
50cc2d25a60b 8020552: [launcher] changes to support removal of Solaris 32-bit distribution
ksrini
parents: 12538
diff changeset
   171
                         + File.separator + "lib"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                         + File.separator + libprefix + demo_name + libsuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            cmdLine += (cmd[i++] = "-agentpath:" + libname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                     + (demo_options.equals("")?"":("="+demo_options)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        /* Add any special VM options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for ( j = 0; j < nvm_options; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            cmdLine += (cmd[i++] = vm_options[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /* Add classname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        cmdLine += (cmd[i++] = class_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /* Begin process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Process p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        System.out.println("Starting: " + cmdLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new RuntimeException("Test failed - exec got IO exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /* Save process output in StringBuffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        output = new MyInputStream("Input Stream", p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        error  = new MyInputStream("Error Stream", p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /* Wait for process to complete, and if exit code is non-zero we fail */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            exitStatus = p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if ( exitStatus != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                System.out.println("Exit code is " + exitStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                error.dump(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                output.dump(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                throw new RuntimeException("Test failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                    "exit return code non-zero " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                    "(exitStatus==" + exitStatus + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch ( InterruptedException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new RuntimeException("Test failed - process interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        System.out.println("Completed: " + cmdLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /* Does the pattern appear in the output of this process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public boolean output_contains(String pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return output.contains(pattern) || error.contains(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}