1 /* 2 * Copyright (c) 2017, 2018, 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 import java.io.IOException; 25 import java.io.OutputStream; 26 import java.util.List; 27 import java.util.Map; 28 29 import jdk.test.lib.apps.LingeredApp; 30 import jdk.test.lib.Platform; 31 import jdk.test.lib.JDKToolLauncher; 32 import jdk.test.lib.JDKToolFinder; 33 import jdk.test.lib.process.OutputAnalyzer; 34 35 /** 36 * This is a framework to run 'jhsdb clhsdb' commands. 37 * See open/test/hotspot/jtreg/serviceability/sa/ClhsdbLongConstant.java for 38 * an example of how to write a test. 39 */ 40 41 public class ClhsdbLauncher { 42 43 private Process toolProcess; 44 45 public ClhsdbLauncher() { 46 toolProcess = null; 47 } 48 49 /** 50 * 51 * Launches 'jhsdb clhsdb' and attaches to the Lingered App process. 52 * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. 53 */ 54 private void attach(long lingeredAppPid) 55 throws IOException { 56 57 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); 58 launcher.addToolArg("clhsdb"); 59 if (lingeredAppPid != -1) { 60 launcher.addToolArg("--pid=" + Long.toString(lingeredAppPid)); 61 System.out.println("Starting clhsdb against " + lingeredAppPid); 62 } 63 64 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); 65 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); 66 67 toolProcess = processBuilder.start(); 68 } 69 70 /** 71 * 72 * Launches 'jhsdb clhsdb' and loads a core file. 73 * @param coreFileName - Name of the corefile to be loaded. 74 */ 75 private void loadCore(String coreFileName) 76 throws IOException { 77 78 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); 79 launcher.addToolArg("clhsdb"); 80 launcher.addToolArg("--core=" + coreFileName); 81 launcher.addToolArg("--exe=" + JDKToolFinder.getTestJDKTool("java")); 82 System.out.println("Starting clhsdb against corefile " + coreFileName + 83 " and exe " + JDKToolFinder.getTestJDKTool("java")); 84 85 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); 86 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); 87 88 toolProcess = processBuilder.start(); 89 } 90 91 /** 92 * 93 * Runs 'jhsdb clhsdb' commands and checks for expected and unexpected strings. 94 * @param commands - clhsdb commands to execute. 95 * @param expectedStrMap - Map of expected strings per command which need to 96 * be checked in the output of the command. 97 * @param unExpectedStrMap - Map of unexpected strings per command which should 98 * not be present in the output of the command. 99 * @return Output of the commands as a String. 100 */ 101 private String runCmd(List<String> commands, 102 Map<String, List<String>> expectedStrMap, 103 Map<String, List<String>> unExpectedStrMap) 104 throws IOException, InterruptedException { 105 String output; 106 107 if (commands == null) { 108 throw new RuntimeException("CLHSDB command must be provided\n"); 109 } 110 111 try (OutputStream out = toolProcess.getOutputStream()) { 112 for (String cmd : commands) { 113 out.write((cmd + "\n").getBytes()); 114 } 115 out.write("quit\n".getBytes()); 116 out.flush(); 117 } 118 119 OutputAnalyzer oa = new OutputAnalyzer(toolProcess); 120 try { 121 toolProcess.waitFor(); 122 } catch (InterruptedException ie) { 123 toolProcess.destroyForcibly(); 124 throw new Error("Problem awaiting the child process: " + ie); 125 } 126 127 oa.shouldHaveExitValue(0); 128 output = oa.getOutput(); 129 System.out.println(output); 130 131 String[] parts = output.split("hsdb>"); 132 for (String cmd : commands) { 133 int index = commands.indexOf(cmd) + 1; 134 OutputAnalyzer out = new OutputAnalyzer(parts[index]); 135 136 if (expectedStrMap != null) { 137 List<String> expectedStr = expectedStrMap.get(cmd); 138 if (expectedStr != null) { 139 for (String exp : expectedStr) { 140 out.shouldContain(exp); 141 } 142 } 143 } 144 145 if (unExpectedStrMap != null) { 146 List<String> unExpectedStr = unExpectedStrMap.get(cmd); 147 if (unExpectedStr != null) { 148 for (String unExp : unExpectedStr) { 149 out.shouldNotContain(unExp); 150 } 151 } 152 } 153 } 154 return output; 155 } 156 157 /** 158 * 159 * Launches 'jhsdb clhsdb', attaches to the Lingered App, executes the commands, 160 * checks for expected and unexpected strings. 161 * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. 162 * @param commands - clhsdb commands to execute. 163 * @param expectedStrMap - Map of expected strings per command which need to 164 * be checked in the output of the command. 165 * @param unExpectedStrMap - Map of unexpected strings per command which should 166 * not be present in the output of the command. 167 * @return Output of the commands as a String. 168 */ 169 public String run(long lingeredAppPid, 170 List<String> commands, 171 Map<String, List<String>> expectedStrMap, 172 Map<String, List<String>> unExpectedStrMap) 173 throws IOException, InterruptedException { 174 175 if (!Platform.shouldSAAttach()) { 176 // Silently skip the test if we don't have enough permissions to attach 177 System.out.println("SA attach not expected to work - test skipped."); 178 return null; 179 } 180 181 attach(lingeredAppPid); 182 return runCmd(commands, expectedStrMap, unExpectedStrMap); 183 } 184 185 /** 186 * 187 * Launches 'jhsdb clhsdb', loads a core file, executes the commands, 188 * checks for expected and unexpected strings. 189 * @param coreFileName - Name of the core file to be debugged. 190 * @param commands - clhsdb commands to execute. 191 * @param expectedStrMap - Map of expected strings per command which need to 192 * be checked in the output of the command. 193 * @param unExpectedStrMap - Map of unexpected strings per command which should 194 * not be present in the output of the command. 195 * @return Output of the commands as a String. 196 */ 197 public String runOnCore(String coreFileName, 198 List<String> commands, 199 Map<String, List<String>> expectedStrMap, 200 Map<String, List<String>> unExpectedStrMap) 201 throws IOException, InterruptedException { 202 203 if (!Platform.shouldSAAttach()) { 204 // Silently skip the test if we don't have enough permissions to attach 205 System.out.println("SA attach not expected to work - test skipped."); 206 return null; 207 } 208 209 loadCore(coreFileName); 210 return runCmd(commands, expectedStrMap, unExpectedStrMap); 211 } 212 }