test/jdk/com/sun/jdi/JITDebug.java
changeset 51987 c4010f88ea68
parent 47216 71c04702a3d5
equal deleted inserted replaced
51986:c1db377f6300 51987:c4010f88ea68
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  *  Note 1: JITDebug.java is no longer a standalone regression test,
    25  * Note: What seems to be an excessive use of System.xxx.flush();
    26  *  due to chronic test failures on win32 platforms.  When testing,
    26  * is actually necessary to combat lost output on win32 systems.
    27  *  use the wrapper script (JITDebug.sh) instead, which will in turn
       
    28  *  invoke this program.
       
    29  *
    27  *
    30  *  The problems are related to inconsistent use of "SystemRoot"
    28  * @test
    31  *  versus "SYSTEMROOT" environment variables in different win32 O/S
    29  * @bug 4291701 4376819 4422312 4522770
    32  *  installations.  Refer to the Comments and Evaluation on bugs
    30  * @summary Test JIT debugging -
    33  *  4522770 and 4461673 for more information.
    31  * assure that launching on uncaught exception works
    34  *
    32  *
    35  *  Undefined SystemRoot in a win32 environment causes the O/S socket()
    33  * @library /test/lib
    36  *  layer to fail with WSAEPROVIDERFAILEDINIT.  The workaround used by
       
    37  *  JITDebug.sh and JITDebug.java is to select the dt_shmem transport
       
    38  *  on any win32 platform where SystemRoot is not found.
       
    39  *
    34  *
    40  *  Note 2: What seems to be an excessive use of System.xxx.flush();
    35  * @author Robert Field
    41  *  is actually necessary to combat lost output on win32 systems.
    36  * @run main/othervm JITDebug
    42  *
       
    43  *  @t e s t
       
    44  *  @bug 4291701 4376819 4422312 4522770
       
    45  *  @summary Test JIT debugging -
       
    46  *  assure that launching on uncaught exception works
       
    47  *
       
    48  *  @author Robert Field
       
    49  *  @run driver JITDebug
       
    50  */
    37  */
    51 
    38 
    52 import com.sun.jdi.*;
    39 import com.sun.jdi.*;
    53 import com.sun.jdi.connect.*;
    40 import com.sun.jdi.connect.*;
       
    41 import jdk.test.lib.JDKToolFinder;
       
    42 import jdk.test.lib.Utils;
       
    43 import jdk.test.lib.process.ProcessTools;
       
    44 
    54 import java.util.*;
    45 import java.util.*;
    55 import java.io.*;
       
    56 
    46 
    57 /*
    47 /*
    58  * This class implements three separate small programs, each
    48  * This class implements three separate small programs, each
    59  * of which (directly or indirectly) invokes the next.  These
    49  * of which (directly or indirectly) invokes the next.  These
    60  * programs are:
    50  * programs are:
    95             } else {
    85             } else {
    96                 return false;
    86                 return false;
    97             }
    87             }
    98         case 3:
    88         case 3:
    99             if (args[0].equals("DEBUGGER")) {
    89             if (args[0].equals("DEBUGGER")) {
   100                 trivialDebugger(args[2]);
    90                 // launched by using "-agentlib:" "launch" sub-option:
       
    91                 // The following strings are appended to the string given in this argument (space-delimited).
       
    92                 // They can aid the launched debugger in establishing a connection with this VM.
       
    93                 // The resulting string is executed.
       
    94                 // - The value of the transport sub-option.
       
    95                 // - The value of the address sub-option (or the generated address if one is not given)
       
    96                 trivialDebugger(args[1], args[2]);
   101                 return true;
    97                 return true;
   102             } else {
    98             } else {
   103                 return false;
    99                 return false;
   104             }
   100             }
   105         default:
   101         default:
   106             return false;
   102             return false;
   107         }
   103         }
   108     }
   104     }
   109 
   105 
   110     void testLaunch() {
   106     void testLaunch() {
   111         class DisplayOutput extends Thread {
   107         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true);
   112             InputStream in;
   108         List largs = pb.command();
   113 
   109         largs.add("-classpath");
   114             DisplayOutput(InputStream in) {
   110         largs.add(Utils.TEST_CLASSES);
   115                 this.in = in;
   111         String javaExe = JDKToolFinder.getJDKTool("java");
   116             }
   112         largs.add("-agentlib:jdwp=transport=dt_socket,server=y,onuncaught=y," +
   117 
   113                   "launch=" + javaExe + " " + this.getClass().getName() + " DEBUGGER ");
   118             public void run() {
   114         largs.add(this.getClass().getName());
   119                 try {
       
   120                     transfer();
       
   121                 } catch (IOException exc) {
       
   122                     new RuntimeException("Unexpected exception: " + exc);
       
   123                 }
       
   124             }
       
   125 
       
   126             void transfer() throws IOException {
       
   127                 int ch;
       
   128                 while ((ch = in.read()) != -1) {
       
   129                     System.out.print((char)ch);
       
   130                 }
       
   131                 in.close();
       
   132             }
       
   133         }
       
   134         String transportMethod = System.getProperty("TRANSPORT_METHOD");
       
   135         if (transportMethod == null) {
       
   136             transportMethod = "dt_socket"; //Default to socket transport.
       
   137         }
       
   138         String javaExe = System.getProperty("java.home") +
       
   139                          File.separator + "bin" + File.separator +"java";
       
   140         List largs = new ArrayList();
       
   141         largs.add(javaExe);
       
   142         largs.add("-agentlib:jdwp=transport=" + transportMethod + ",server=y,onuncaught=y," +
       
   143                   "launch=" +
       
   144                   javaExe + " -DTRANSPORT_METHOD=" + transportMethod + " " +
       
   145                   this.getClass().getName() + " DEBUGGER ");
       
   146         largs.add("JITDebug");
       
   147         largs.add("TARGET");
   115         largs.add("TARGET");
   148         System.out.println("Launching: " + largs);
       
   149         String[] sargs = (String[])largs.toArray(new String[largs.size()]);
       
   150         Runtime rt = Runtime.getRuntime();
       
   151         try {
   116         try {
   152             Process proc = rt.exec(VMConnection.insertDebuggeeVMOptions(sargs));
   117             ProcessTools.executeCommand(pb)
   153             DisplayOutput inThread = new DisplayOutput(proc.getInputStream());
   118                     .shouldHaveExitValue(0);
   154             DisplayOutput erThread = new DisplayOutput(proc.getErrorStream());
   119         } catch (Throwable exc) {
   155             inThread.start();  // transfer all in&err
       
   156             erThread.start();
       
   157             inThread.join();  // make sure they are done
       
   158             erThread.join();
       
   159             int exitValue = proc.waitFor();
       
   160             if (exitValue != 0) {
       
   161                 throw new RuntimeException("Failure exit status: " +
       
   162                                            exitValue);
       
   163             }
       
   164         } catch (Exception exc) {
       
   165             throw new RuntimeException("Unexpected exception: " + exc);
   120             throw new RuntimeException("Unexpected exception: " + exc);
   166         }
   121         }
   167         System.out.println("JIT Debugging test PASSED");
   122         System.out.println("JIT Debugging test PASSED");
   168     }
   123     }
   169 
       
   170     void displayOutput(InputStream in) throws IOException {
       
   171 
       
   172     }
       
   173 
       
   174 
   124 
   175     // Target VM code
   125     // Target VM code
   176     void debugTarget() {
   126     void debugTarget() {
   177         System.out.flush();
   127         System.out.flush();
   178         System.out.println("trigger onuncaught launch");
   128         System.out.println("trigger onuncaught launch");
   179         System.out.flush();
   129         System.out.flush();
   180         throw new RuntimeException("Start-up onuncaught handling");
   130         throw new RuntimeException("Start-up onuncaught handling");
   181     }
   131     }
   182 
   132 
   183     void trivialDebugger(String transportAddress) {
   133     void trivialDebugger(String transportMethod, String transportAddress) {
   184         System.out.println("trivial debugger started");
   134         System.out.println("trivial debugger started");
   185         String transportMethod = System.getProperty("TRANSPORT_METHOD");
       
   186         String connectorName = null;
   135         String connectorName = null;
   187         if ("dt_shmem".equals(transportMethod)) {
   136         if ("dt_shmem".equals(transportMethod)) {
   188             connectorName = "com.sun.jdi.SharedMemoryAttach";
   137             connectorName = "com.sun.jdi.SharedMemoryAttach";
   189         } else if ("dt_socket".equals(transportMethod)) {
   138         } else if ("dt_socket".equals(transportMethod)) {
   190             connectorName = "com.sun.jdi.SocketAttach";
   139             connectorName = "com.sun.jdi.SocketAttach";
   207         System.err.flush();
   156         System.err.flush();
   208         hang();
   157         hang();
   209     }
   158     }
   210 
   159 
   211     void doAttach(String connectorName, AttachingConnector conn, String transportAddress) {
   160     void doAttach(String connectorName, AttachingConnector conn, String transportAddress) {
   212         Map connArgs = conn.defaultArguments();
   161         Map<String, Connector.Argument> connArgs = conn.defaultArguments();
   213         if ("com.sun.jdi.SharedMemoryAttach".equals(connectorName)) {
   162         if ("com.sun.jdi.SharedMemoryAttach".equals(connectorName)) {
   214             Connector.Argument portArg = (Connector.Argument)connArgs.get("name");
   163             Connector.Argument portArg = (Connector.Argument)connArgs.get("name");
   215             portArg.setValue(transportAddress);
   164             portArg.setValue(transportAddress);
   216         } else {
   165         } else {
   217             Connector.Argument portArg = (Connector.Argument)connArgs.get("port");
   166             Connector.Argument portArg = (Connector.Argument)connArgs.get("port");
   232 
   181 
   233     /** Hang so that test fails */
   182     /** Hang so that test fails */
   234     void hang() {
   183     void hang() {
   235         try {
   184         try {
   236             // ten minute nap
   185             // ten minute nap
   237             Thread.currentThread().sleep(10 * 60 * 1000);
   186             Thread.sleep(10 * 60 * 1000);
   238         } catch (InterruptedException exc) {
   187         } catch (InterruptedException exc) {
   239             // shouldn't happen
   188             // shouldn't happen
   240         }
   189         }
   241     }
   190     }
   242 }
   191 }