jdk/test/com/sun/jdi/RunToExit.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 20201 50cc2d25a60b
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
     2
 * Copyright (c) 2004, 2006, 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: 4342
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4997445
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that with server=y, when VM runs to System.exit() no error happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build VMConnection RunToExit Exit0
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
    29
 * @run main/othervm RunToExit
2
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.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jdi.connect.AttachingConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class RunToExit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* Increment this when ERROR: seen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static int error_seen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static volatile boolean ready = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Helper class to direct process output to a StringBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static class IOHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        private String              name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        private BufferedInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        private StringBuffer        buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        IOHandler(String name, InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.in = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            this.buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        static void handle(String name, InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            IOHandler handler = new IOHandler(name, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            Thread thr = new Thread(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    int n = in.read(b, 0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    // The first thing that will get read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    //    Listening for transport dt_socket at address: xxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    // which shows the debuggee is ready to accept connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    ready = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    buffer.append(new String(b, 0, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            String str = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if ( str.contains("ERROR:") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                error_seen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println(name + ": " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Find a connector by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Launch a server debuggee with the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static Process launch(String address, String class_name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        String exe =   System.getProperty("java.home")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                     + File.separator + "bin" + File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String arch = System.getProperty("os.arch");
4342
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
   118
        String osname = System.getProperty("os.name");
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
   119
        if (osname.equals("SunOS") && arch.equals("sparcv9")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            exe += "sparcv9/java";
4342
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
   121
        } else if (osname.equals("SunOS") && arch.equals("amd64")) {
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   122
            exe += "amd64/java";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            exe += "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            " -agentlib:jdwp=transport=dt_socket" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ",server=y" + ",suspend=y" + ",address=" + address +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            " " + class_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("Starting: " + cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Process p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        IOHandler.handle("Input Stream", p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        IOHandler.handle("Error Stream", p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * - pick a TCP port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * - Launch a server debuggee: server=y,suspend=y,address=${port}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * - run it to VM death
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * - verify we saw no error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // find a free port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String address = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // launch the server debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Process process = launch(address, "Exit0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // wait for the debugge to be ready
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        while (!ready) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            } catch(Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw ee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // attach to server debuggee and resume it so it can exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Map conn_args = conn.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Connector.IntegerArgument port_arg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            (Connector.IntegerArgument)conn_args.get("port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        port_arg.setValue(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        VirtualMachine vm = conn.attach(conn_args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // The first event is always a VMStartEvent, and it is always in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // an EventSet by itself.  Wait for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        EventSet evtSet = vm.eventQueue().remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (Event event: evtSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (event instanceof VMStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new RuntimeException("Test failed - debuggee did not start properly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        vm.eventRequestManager().deleteAllBreakpoints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int exitCode = process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // if the server debuggee ran cleanly, we assume we were clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (exitCode == 0 && error_seen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.out.println("Test passed - server debuggee cleanly terminated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new RuntimeException("Test failed - server debuggee generated an error when it terminated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}