1 /* 2 * Copyright (c) 2019, 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 * @bug 8209790 27 * @summary Checks ability for connecting to debug server (jstack, jmap, jinfo, jsnap) 28 * @requires vm.hasSAandCanAttach 29 * @requires os.family != "windows" 30 * @modules java.base/jdk.internal.misc 31 * @library /test/lib 32 * 33 * @run main/othervm DebugdConnectTest 34 */ 35 36 import java.io.IOException; 37 38 import jdk.test.lib.JDKToolLauncher; 39 import jdk.test.lib.apps.LingeredApp; 40 import jdk.test.lib.process.OutputAnalyzer; 41 42 43 public class DebugdConnectTest { 44 45 private static OutputAnalyzer runJHSDB(String command, String id) throws IOException, InterruptedException { 46 JDKToolLauncher jhsdbLauncher = JDKToolLauncher.createUsingTestJDK("jhsdb"); 47 jhsdbLauncher.addToolArg(command); 48 jhsdbLauncher.addToolArg("--connect"); 49 if (id != null) { 50 jhsdbLauncher.addToolArg(id + "@localhost"); 51 } else { 52 jhsdbLauncher.addToolArg("localhost"); 53 } 54 55 Process jhsdb = (new ProcessBuilder(jhsdbLauncher.getCommand())).start(); 56 OutputAnalyzer out = new OutputAnalyzer(jhsdb); 57 58 jhsdb.waitFor(); 59 60 System.out.println(out.getStdout()); 61 System.err.println(out.getStderr()); 62 63 return out; 64 } 65 66 private static void runJSTACK(String id) throws IOException, InterruptedException { 67 OutputAnalyzer out = runJHSDB("jstack", id); 68 69 out.shouldContain("LingeredApp"); 70 out.stderrShouldBeEmpty(); 71 out.shouldHaveExitValue(0); 72 } 73 74 private static void runJMAP(String id) throws IOException, InterruptedException { 75 OutputAnalyzer out = runJHSDB("jmap", id); 76 77 out.shouldContain("JVM version is"); 78 out.stderrShouldBeEmpty(); 79 out.shouldHaveExitValue(0); 80 } 81 82 private static void runJINFO(String id) throws IOException, InterruptedException { 83 OutputAnalyzer out = runJHSDB("jinfo", id); 84 85 out.shouldContain("Java System Properties:"); 86 out.stderrShouldBeEmpty(); 87 out.shouldHaveExitValue(0); 88 } 89 90 private static void runJSNAP(String id) throws IOException, InterruptedException { 91 OutputAnalyzer out = runJHSDB("jsnap", id); 92 93 out.shouldContain("java.vm.name="); 94 out.stderrShouldBeEmpty(); 95 out.shouldHaveExitValue(0); 96 } 97 98 private static void runTests(String id, long debuggeePid) throws IOException, InterruptedException { 99 DebugdUtils debugd = new DebugdUtils(id); 100 debugd.attach(debuggeePid); 101 102 try { 103 runJSTACK(id); 104 runJMAP(id); 105 runJINFO(id); 106 runJSNAP(id); 107 } finally { 108 debugd.detach(); 109 } 110 } 111 112 public static void main(String[] args) throws Exception { 113 LingeredApp app = null; 114 115 try { 116 app = LingeredApp.startApp(); 117 System.out.println("Started LingeredApp with pid " + app.getPid()); 118 119 System.out.println("debugd connection test with server id:"); 120 runTests("test", app.getPid()); 121 122 System.out.println("debugd connection test without server id:"); 123 runTests(null, app.getPid()); 124 } finally { 125 LingeredApp.stopApp(app); 126 } 127 128 } 129 130 }