0N/A/*
2362N/A * Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4997445
0N/A * @summary Test that with server=y, when VM runs to System.exit() no error happens
0N/A *
0N/A * @build VMConnection RunToExit Exit0
1940N/A * @run main/othervm RunToExit
0N/A */
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.File;
0N/Aimport java.io.BufferedInputStream;
0N/Aimport java.net.ServerSocket;
0N/Aimport com.sun.jdi.Bootstrap;
0N/Aimport com.sun.jdi.VirtualMachine;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport com.sun.jdi.connect.Connector;
0N/Aimport com.sun.jdi.connect.AttachingConnector;
0N/Aimport java.util.Map;
0N/Aimport java.util.List;
0N/Aimport java.util.Iterator;
0N/A
0N/Apublic class RunToExit {
0N/A
0N/A /* Increment this when ERROR: seen */
0N/A static int error_seen = 0;
0N/A static volatile boolean ready = false;
0N/A /*
0N/A * Helper class to direct process output to a StringBuffer
0N/A */
0N/A static class IOHandler implements Runnable {
0N/A private String name;
0N/A private BufferedInputStream in;
0N/A private StringBuffer buffer;
0N/A
0N/A IOHandler(String name, InputStream in) {
0N/A this.name = name;
0N/A this.in = new BufferedInputStream(in);
0N/A this.buffer = new StringBuffer();
0N/A }
0N/A
0N/A static void handle(String name, InputStream in) {
0N/A IOHandler handler = new IOHandler(name, in);
0N/A Thread thr = new Thread(handler);
0N/A thr.setDaemon(true);
0N/A thr.start();
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A byte b[] = new byte[100];
0N/A for (;;) {
0N/A int n = in.read(b, 0, 100);
0N/A // The first thing that will get read is
0N/A // Listening for transport dt_socket at address: xxxxx
0N/A // which shows the debuggee is ready to accept connections.
0N/A ready = true;
0N/A if (n < 0) {
0N/A break;
0N/A }
0N/A buffer.append(new String(b, 0, n));
0N/A }
0N/A } catch (IOException ioe) { }
0N/A
0N/A String str = buffer.toString();
0N/A if ( str.contains("ERROR:") ) {
0N/A error_seen++;
0N/A }
0N/A System.out.println(name + ": " + str);
0N/A }
0N/A
0N/A }
0N/A
0N/A /*
0N/A * Find a connector by name
0N/A */
0N/A private static Connector findConnector(String name) {
0N/A List connectors = Bootstrap.virtualMachineManager().allConnectors();
0N/A Iterator iter = connectors.iterator();
0N/A while (iter.hasNext()) {
0N/A Connector connector = (Connector)iter.next();
0N/A if (connector.name().equals(name)) {
0N/A return connector;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Launch a server debuggee with the given address
0N/A */
0N/A private static Process launch(String address, String class_name) throws IOException {
0N/A String exe = System.getProperty("java.home")
0N/A + File.separator + "bin" + File.separator;
0N/A String arch = System.getProperty("os.arch");
1947N/A String osname = System.getProperty("os.name");
1947N/A if (osname.equals("SunOS") && arch.equals("sparcv9")) {
0N/A exe += "sparcv9/java";
1947N/A } else if (osname.equals("SunOS") && arch.equals("amd64")) {
1940N/A exe += "amd64/java";
0N/A } else {
0N/A exe += "java";
0N/A }
0N/A String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
0N/A " -agentlib:jdwp=transport=dt_socket" +
0N/A ",server=y" + ",suspend=y" + ",address=" + address +
0N/A " " + class_name;
0N/A
0N/A System.out.println("Starting: " + cmd);
0N/A
0N/A Process p = Runtime.getRuntime().exec(cmd);
0N/A
0N/A IOHandler.handle("Input Stream", p.getInputStream());
0N/A IOHandler.handle("Error Stream", p.getErrorStream());
0N/A
0N/A return p;
0N/A }
0N/A
0N/A /*
0N/A * - pick a TCP port
0N/A * - Launch a server debuggee: server=y,suspend=y,address=${port}
0N/A * - run it to VM death
0N/A * - verify we saw no error
0N/A */
0N/A public static void main(String args[]) throws Exception {
0N/A // find a free port
0N/A ServerSocket ss = new ServerSocket(0);
0N/A int port = ss.getLocalPort();
0N/A ss.close();
0N/A
0N/A String address = String.valueOf(port);
0N/A
0N/A // launch the server debuggee
0N/A Process process = launch(address, "Exit0");
0N/A
0N/A // wait for the debugge to be ready
0N/A while (!ready) {
0N/A try {
0N/A Thread.sleep(1000);
0N/A } catch(Exception ee) {
0N/A throw ee;
0N/A }
0N/A }
0N/A
0N/A // attach to server debuggee and resume it so it can exit
0N/A AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
0N/A Map conn_args = conn.defaultArguments();
0N/A Connector.IntegerArgument port_arg =
0N/A (Connector.IntegerArgument)conn_args.get("port");
0N/A port_arg.setValue(port);
0N/A VirtualMachine vm = conn.attach(conn_args);
0N/A
0N/A // The first event is always a VMStartEvent, and it is always in
0N/A // an EventSet by itself. Wait for it.
0N/A EventSet evtSet = vm.eventQueue().remove();
0N/A for (Event event: evtSet) {
0N/A if (event instanceof VMStartEvent) {
0N/A break;
0N/A }
0N/A throw new RuntimeException("Test failed - debuggee did not start properly");
0N/A }
0N/A vm.eventRequestManager().deleteAllBreakpoints();
0N/A vm.resume();
0N/A
0N/A int exitCode = process.waitFor();
0N/A
0N/A // if the server debuggee ran cleanly, we assume we were clean
0N/A if (exitCode == 0 && error_seen == 0) {
0N/A System.out.println("Test passed - server debuggee cleanly terminated");
0N/A } else {
0N/A throw new RuntimeException("Test failed - server debuggee generated an error when it terminated");
0N/A }
0N/A }
0N/A}