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