test/hotspot/jtreg/serviceability/sa/sadebugd/SADebugDTest.java
changeset 47216 71c04702a3d5
parent 44636 a50cc126180c
child 50791 b1e90a8a876c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, 2017, 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 /*
       
    25  * @test
       
    26  * @summary Checks that the jshdb debugd utility sucessfully starts
       
    27  *          and tries to attach to a running process
       
    28  * @modules java.base/jdk.internal.misc
       
    29  * @library /test/lib
       
    30  *
       
    31  * @run main/othervm SADebugDTest
       
    32  */
       
    33 import java.io.File;
       
    34 import java.util.concurrent.CountDownLatch;
       
    35 import java.io.InputStreamReader;
       
    36 import java.io.BufferedReader;
       
    37 import java.io.Reader;
       
    38 import java.util.concurrent.TimeUnit;
       
    39 import java.util.function.Predicate;
       
    40 import static jdk.test.lib.Asserts.assertTrue;
       
    41 import static jdk.test.lib.Platform.shouldSAAttach;
       
    42 import static jdk.test.lib.process.ProcessTools.startProcess;
       
    43 
       
    44 public class SADebugDTest {
       
    45 
       
    46     private static final String GOLDEN = "Attaching to process ID %d and starting RMI services, please wait...";
       
    47 
       
    48     private static final String JAVA_HOME = (System.getProperty("test.jdk") != null)
       
    49             ? System.getProperty("test.jdk") : System.getProperty("java.home");
       
    50 
       
    51     private static final String JAVA_BIN_DIR
       
    52             = JAVA_HOME + File.separator + "bin" + File.separator;
       
    53 
       
    54     private static final String JHSDB = JAVA_BIN_DIR + "jhsdb";
       
    55 
       
    56     public static void main(String[] args) throws Exception {
       
    57 
       
    58         if (!shouldSAAttach()) {
       
    59             log("Not possible to attach the SA. Skipping the test");
       
    60             return;
       
    61         }
       
    62 
       
    63         long ourPid = ProcessHandle.current().pid();
       
    64 
       
    65         // The string we are expecting in the debugd ouput
       
    66         String golden = String.format(GOLDEN, ourPid);
       
    67 
       
    68         // We are going to run 'jhsdb debugd <our pid>'
       
    69         // The startProcess will block untl the 'golden' string appears in either process' stdout or stderr
       
    70         // In case of timeout startProcess kills the debugd process
       
    71         ProcessBuilder pb = new ProcessBuilder();
       
    72         pb.command(JHSDB, "debugd", String.valueOf(ourPid));
       
    73         Process debugd = startProcess("debugd", pb, null, (line) -> line.trim().contains(golden), 0, TimeUnit.SECONDS);
       
    74 
       
    75         // If we are here, this means we have received the golden line and the test has passed
       
    76         // The debugd remains running, we have to kill it
       
    77         debugd.destroy();
       
    78 
       
    79     }
       
    80 
       
    81     private static void log(String string) {
       
    82         System.out.println(string);
       
    83     }
       
    84 
       
    85 }