jdk/test/sun/tools/jhat/HatRun.java
author weijun
Fri, 20 Jun 2008 12:05:02 +0800
changeset 793 8183c2b0985f
parent 2 90ce3da70b43
child 4319 47bb128ae7b3
permissions -rw-r--r--
6716534: Krb5LoginModule has not cleaned temp info between authentication attempts Reviewed-by: valeriep
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 2005 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Support classes for running jhat 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 jhat run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
public class HatRun {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String        all_hprof_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String        all_hat_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private String        dumpfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private MyInputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private MyInputStream error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* Create a Hat run process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public HatRun(String hprof_options, String hat_options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        all_hprof_options = hprof_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        all_hat_options   = hat_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Execute a process with an -agentpath or -agentlib command option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void runit(String class_name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        runit(class_name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Execute a command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private void execute(String cmd[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /* Begin process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Process p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String cmdLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for ( i = 0 ; i < cmd.length; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
          cmdLine += cmd[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
          cmdLine += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("Starting: " + cmdLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new RuntimeException("Test failed - exec got IO exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /* Save process output in StringBuffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        output = new MyInputStream("Input Stream", p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        error  = new MyInputStream("Error Stream", p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /* Wait for process to complete, and if exit code is non-zero we fail */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int exitStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            exitStatus = p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if ( exitStatus != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                System.out.println("Exit code is " + exitStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                error.dump(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                output.dump(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new RuntimeException("Test failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                    "exit return code non-zero " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                    "(exitStatus==" + exitStatus + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch ( InterruptedException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new RuntimeException("Test failed - process interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        System.out.println("Completed: " + cmdLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Execute a process with an -agentpath or -agentlib command option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *    plus any set of other java options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void runit(String class_name, String vm_options[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String jre_home  = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String sdk_home  = (jre_home.endsWith("jre") ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            (jre_home + File.separator + "..") :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            jre_home );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        String cdir      = System.getProperty("test.classes", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String os_arch   = System.getProperty("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        boolean d64      = os_arch.equals("sparcv9") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                           os_arch.equals("amd64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        String isa_dir   = d64?(File.separator+os_arch):"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String java      = jre_home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                             + File.separator + "bin" + isa_dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                             + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        String jhat      = sdk_home + File.separator + "bin"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                           + File.separator + "jhat";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        /* Array of strings to be passed in for exec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *   1. java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         *   2. -Dtest.classes=.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         *   3. -d64                 (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         *   4. -Xcheck:jni          (Just because it finds bugs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         *   5. -Xverify:all         (Make sure verification is on full blast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         *   6. -agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         *       vm_options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         *   7+i. classname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int nvm_options = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if ( vm_options != null ) nvm_options = vm_options.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String cmd[]     = new String[1 + (d64?1:0) + 5 + nvm_options];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        cmd[i++] = java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        cmd[i++] = "-Dtest.classes=" + cdir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ( d64 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            cmd[i++] = "-d64";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        cmd[i++] = "-Xcheck:jni";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        cmd[i++] = "-Xverify:all";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        dumpfile= cdir + File.separator + class_name + ".hdump";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        cmd[i++] = "-agentlib:hprof=" + all_hprof_options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    + ",format=b,file=" + dumpfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /* Add any special VM options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for ( j = 0; j < nvm_options; j++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            cmd[i++] = vm_options[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /* Add classname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        cmd[i++] = class_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        /* Execute process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        execute(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /* Run jhat */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        String jhat_cmd[] = new String[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        jhat_cmd[0] = jhat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        jhat_cmd[1] = "-debug";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        jhat_cmd[2] = "2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        jhat_cmd[3] = dumpfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        /* Execute process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        execute(jhat_cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /* Does the pattern appear in the output of this process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public boolean output_contains(String pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return output.contains(pattern) || error.contains(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}