1 /*
   2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4997445
  26  * @summary Test that with server=y, when VM runs to System.exit() no error happens
  27  * @library /lib/testlibrary
  28  * @build jdk.testlibrary.* VMConnection RunToExit Exit0
  29  * @run driver RunToExit
  30  */
  31 import java.net.ServerSocket;
  32 import com.sun.jdi.Bootstrap;
  33 import com.sun.jdi.VirtualMachine;
  34 import com.sun.jdi.event.*;
  35 import com.sun.jdi.connect.Connector;
  36 import com.sun.jdi.connect.AttachingConnector;
  37 import java.net.ConnectException;
  38 import java.util.Map;
  39 import java.util.List;
  40 import java.util.Iterator;
  41 import java.util.concurrent.TimeUnit;
  42 import java.util.stream.Collectors;
  43 import jdk.testlibrary.ProcessTools;
  44 
  45 public class RunToExit {
  46 
  47     /* Increment this when ERROR: seen */
  48     static volatile int error_seen = 0;
  49     static volatile boolean ready = false;
  50 
  51     /*
  52      * Find a connector by name
  53      */
  54     private static Connector findConnector(String name) {
  55         List connectors = Bootstrap.virtualMachineManager().allConnectors();
  56         Iterator iter = connectors.iterator();
  57         while (iter.hasNext()) {
  58             Connector connector = (Connector)iter.next();
  59             if (connector.name().equals(name)) {
  60                 return connector;
  61             }
  62         }
  63         return null;
  64     }
  65 
  66     /*
  67      * Launch a server debuggee with the given address
  68      */
  69     private static Process launch(String address, String class_name) throws Exception {
  70         String args[] = new String[]{
  71             "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
  72                 + address,
  73             class_name
  74         };
  75         args = VMConnection.insertDebuggeeVMOptions(args);
  76 
  77         ProcessBuilder launcher = ProcessTools.createJavaProcessBuilder(args);
  78 
  79         System.out.println(launcher.command().stream().collect(Collectors.joining(" ", "Starting: ", "")));
  80 
  81         Process p = ProcessTools.startProcess(
  82             class_name,
  83             launcher,
  84             RunToExit::checkForError,
  85             RunToExit::isTransportListening,
  86             0,
  87             TimeUnit.NANOSECONDS
  88         );
  89 
  90         return p;
  91     }
  92 
  93     private static boolean isTransportListening(String line) {
  94         return line.startsWith("Listening for transport dt_socket");
  95     }
  96 
  97     private static void checkForError(String line) {
  98         if (line.contains("ERROR:")) {
  99             error_seen++;
 100         }
 101     }
 102 
 103     /*
 104      * - pick a TCP port
 105      * - Launch a server debuggee: server=y,suspend=y,address=${port}
 106      * - run it to VM death
 107      * - verify we saw no error
 108      */
 109     public static void main(String args[]) throws Exception {
 110         // find a free port
 111         ServerSocket ss = new ServerSocket(0);
 112         int port = ss.getLocalPort();
 113         ss.close();
 114 
 115         String address = String.valueOf(port);
 116 
 117         // launch the server debuggee
 118         Process process = launch(address, "Exit0");
 119 
 120         // attach to server debuggee and resume it so it can exit
 121         AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
 122         Map conn_args = conn.defaultArguments();
 123         Connector.IntegerArgument port_arg =
 124             (Connector.IntegerArgument)conn_args.get("port");
 125         port_arg.setValue(port);
 126 
 127         System.out.println("Connection arguments: " + conn_args);
 128 
 129         VirtualMachine vm = null;
 130         while (vm == null) {
 131             try {
 132                 vm = conn.attach(conn_args);
 133             } catch (ConnectException e) {
 134                 e.printStackTrace(System.out);
 135                 System.out.println("--- Debugee not ready. Retrying in 500ms. ---");
 136                 Thread.sleep(500);
 137             }
 138         }
 139 
 140         // The first event is always a VMStartEvent, and it is always in
 141         // an EventSet by itself.  Wait for it.
 142         EventSet evtSet = vm.eventQueue().remove();
 143         for (Event event: evtSet) {
 144             if (event instanceof VMStartEvent) {
 145                 break;
 146             }
 147             throw new RuntimeException("Test failed - debuggee did not start properly");
 148         }
 149         vm.eventRequestManager().deleteAllBreakpoints();
 150         vm.resume();
 151 
 152         int exitCode = process.waitFor();
 153 
 154         // if the server debuggee ran cleanly, we assume we were clean
 155         if (exitCode == 0 && error_seen == 0) {
 156             System.out.println("Test passed - server debuggee cleanly terminated");
 157         } else {
 158             throw new RuntimeException("Test failed - server debuggee generated an error when it terminated, " +
 159                 "exit code was " + exitCode + ", " + error_seen + " error(s) seen in debugee output.");
 160         }
 161     }
 162 }