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.process.OutputAnalyzer; 33 34 /** 35 * This is a framework to run 'jhsdb clhsdb' commands. 36 * See open/test/hotspot/jtreg/serviceability/sa/ClhsdbLongConstant.java for 37 * an example of how to write a test. 38 */ 39 40 public class ClhsdbLauncher { 41 42 private Process toolProcess; 43 44 public ClhsdbLauncher() { 45 toolProcess = null; 46 } 47 48 /** 49 * 50 * Launches 'jhsdb clhsdb' and attaches to the Lingered App process. 51 * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. 52 */ 53 private void attach(long lingeredAppPid) 54 throws IOException { 55 56 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); 57 launcher.addToolArg("clhsdb"); 58 if (lingeredAppPid != -1) { 59 launcher.addToolArg("--pid=" + Long.toString(lingeredAppPid)); 60 System.out.println("Starting clhsdb against " + lingeredAppPid); 61 } 62 63 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); 64 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); 65 66 toolProcess = processBuilder.start(); 67 } 68 69 /** 70 * 71 * Runs 'jhsdb clhsdb' commands and checks for expected and unexpected strings. 72 * @param commands - clhsdb commands to execute. 73 * @param expectedStrMap - Map of expected strings per command which need to 74 * be checked in the output of the command. 75 * @param unExpectedStrMap - Map of unexpected strings per command which should 76 * not be present in the output of the command. 77 * @return Output of the commands as a String. 78 */ 79 private String runCmd(List<String> commands, 80 Map<String, List<String>> expectedStrMap, 81 Map<String, List<String>> unExpectedStrMap) 82 throws IOException, InterruptedException { 83 String output; 84 85 if (commands == null) { 86 throw new RuntimeException("CLHSDB command must be provided\n"); 87 } 88 89 try (OutputStream out = toolProcess.getOutputStream()) { 90 for (String cmd : commands) { 91 out.write((cmd + "\n").getBytes()); 92 } 93 out.write("quit\n".getBytes()); 94 out.flush(); 95 } 96 97 OutputAnalyzer oa = new OutputAnalyzer(toolProcess); 98 try { 99 toolProcess.waitFor(); 100 } catch (InterruptedException ie) { 101 toolProcess.destroyForcibly(); 102 throw new Error("Problem awaiting the child process: " + ie); 103 } 104 105 oa.shouldHaveExitValue(0); 106 output = oa.getOutput(); 107 System.out.println(output); 108 109 String[] parts = output.split("hsdb>"); 110 for (String cmd : commands) { 111 int index = commands.indexOf(cmd) + 1; 112 OutputAnalyzer out = new OutputAnalyzer(parts[index]); 113 114 if (expectedStrMap != null) { 115 List<String> expectedStr = expectedStrMap.get(cmd); 116 if (expectedStr != null) { 117 for (String exp : expectedStr) { 118 out.shouldContain(exp); 119 } 120 } 121 } 122 123 if (unExpectedStrMap != null) { 124 List<String> unExpectedStr = unExpectedStrMap.get(cmd); 125 if (unExpectedStr != null) { 126 for (String unExp : unExpectedStr) { 127 out.shouldNotContain(unExp); 128 } 129 } 130 } 131 } 132 return output; 133 } 134 135 /** 136 * 137 * Launches 'jhsdb clhsdb', attaches to the Lingered App, executes the commands, 138 * checks for expected and unexpected strings. 139 * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. 140 * @param commands - clhsdb commands to execute. 141 * @param expectedStrMap - Map of expected strings per command which need to 142 * be checked in the output of the command. 143 * @param unExpectedStrMap - Map of unexpected strings per command which should 144 * not be present in the output of the command. 145 * @return Output of the commands as a String. 146 */ 147 public String run(long lingeredAppPid, 148 List<String> commands, 149 Map<String, List<String>> expectedStrMap, 150 Map<String, List<String>> unExpectedStrMap) 151 throws IOException, InterruptedException { 152 153 if (!Platform.shouldSAAttach()) { 154 // Silently skip the test if we don't have enough permissions to attach 155 System.out.println("SA attach not expected to work - test skipped."); 156 return null; 157 } 158 159 attach(lingeredAppPid); 160 return runCmd(commands, expectedStrMap, unExpectedStrMap); 161 } 162 }