test/jdk/com/sun/jdi/RunToExit.java
author iignatyev
Fri, 14 Sep 2018 14:02:57 -0700
changeset 51754 594919232b8f
parent 51675 b487c1e914d0
child 53929 ca23d3475af0
permissions -rw-r--r--
8210732: remove jdk.testlibrary.Utils Reviewed-by: alanb, jcbeyler
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2018, 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
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 47216
diff changeset
    27
 * @library /test/lib
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 29496
diff changeset
    28
 * @modules java.management
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 29496
diff changeset
    29
 *          jdk.jdi
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 47216
diff changeset
    30
 * @build VMConnection RunToExit Exit0
24973
8c4bc3fa4c4e 6622468: TEST_BUG: Time to retire the @debuggeeVMOptions mechanism used in the com.sun.jdi infrastructure
sla
parents: 24514
diff changeset
    31
 * @run driver RunToExit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.connect.AttachingConnector;
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    39
import java.net.ConnectException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    43
import java.util.concurrent.TimeUnit;
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    44
import java.util.stream.Collectors;
51675
b487c1e914d0 8210112: remove jdk.testlibrary.ProcessTools
iignatyev
parents: 47216
diff changeset
    45
import jdk.test.lib.process.ProcessTools;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class RunToExit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /* Increment this when ERROR: seen */
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    50
    static volatile int error_seen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static volatile boolean ready = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Find a connector by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Launch a server debuggee with the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    71
    private static Process launch(String address, String class_name) throws Exception {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    72
        String args[] = new String[]{
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    73
            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    74
                + address,
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    75
            class_name
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    76
        };
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    77
        args = VMConnection.insertDebuggeeVMOptions(args);
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    78
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    79
        ProcessBuilder launcher = ProcessTools.createJavaProcessBuilder(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    81
        System.out.println(launcher.command().stream().collect(Collectors.joining(" ", "Starting: ", "")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    83
        Process p = ProcessTools.startProcess(
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    84
            class_name,
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    85
            launcher,
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    86
            RunToExit::checkForError,
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    87
            RunToExit::isTransportListening,
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    88
            0,
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    89
            TimeUnit.NANOSECONDS
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    90
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    95
    private static boolean isTransportListening(String line) {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    96
        return line.startsWith("Listening for transport dt_socket");
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    97
    }
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    98
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
    99
    private static void checkForError(String line) {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   100
        if (line.contains("ERROR:")) {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   101
            error_seen++;
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   102
        }
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   103
    }
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * - pick a TCP port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * - Launch a server debuggee: server=y,suspend=y,address=${port}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * - run it to VM death
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * - verify we saw no error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // find a free port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String address = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // launch the server debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Process process = launch(address, "Exit0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // attach to server debuggee and resume it so it can exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Map conn_args = conn.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Connector.IntegerArgument port_arg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            (Connector.IntegerArgument)conn_args.get("port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        port_arg.setValue(port);
24514
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 20201
diff changeset
   128
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 20201
diff changeset
   129
        System.out.println("Connection arguments: " + conn_args);
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 20201
diff changeset
   130
29496
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   131
        VirtualMachine vm = null;
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   132
        while (vm == null) {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   133
            try {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   134
                vm = conn.attach(conn_args);
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   135
            } catch (ConnectException e) {
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   136
                e.printStackTrace(System.out);
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   137
                System.out.println("--- Debugee not ready. Retrying in 500ms. ---");
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   138
                Thread.sleep(500);
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   139
            }
728ed9492e63 8049696: com/sun/jdi/RunToExit fails with "ConnectException: Connection refused"
jbachorik
parents: 26963
diff changeset
   140
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // The first event is always a VMStartEvent, and it is always in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // an EventSet by itself.  Wait for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        EventSet evtSet = vm.eventQueue().remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (Event event: evtSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (event instanceof VMStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new RuntimeException("Test failed - debuggee did not start properly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        vm.eventRequestManager().deleteAllBreakpoints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int exitCode = process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // if the server debuggee ran cleanly, we assume we were clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (exitCode == 0 && error_seen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.println("Test passed - server debuggee cleanly terminated");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
26963
a2a52f380463 8059105: Add better failure reporting to com/sun/jdi/RunToExit.java test
farvidsson
parents: 24973
diff changeset
   160
            throw new RuntimeException("Test failed - server debuggee generated an error when it terminated, " +
a2a52f380463 8059105: Add better failure reporting to com/sun/jdi/RunToExit.java test
farvidsson
parents: 24973
diff changeset
   161
                "exit code was " + exitCode + ", " + error_seen + " error(s) seen in debugee output.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}