diff -r acaf894a5b5d -r f113d1ef7bed test/hotspot/jtreg/serviceability/sa/ClhsdbLauncher.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/serviceability/sa/ClhsdbLauncher.java Thu Nov 16 09:26:48 2017 +0530 @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + +import jdk.test.lib.apps.LingeredApp; +import jdk.test.lib.Platform; +import jdk.test.lib.JDKToolLauncher; +import jdk.test.lib.process.OutputAnalyzer; + +/** + * This is a framework to run 'jhsdb clhsdb' commands. + * See open/test/hotspot/jtreg/serviceability/sa/ClhsdbLongConstant.java for + * an example of how to write a test. + */ + +public class ClhsdbLauncher { + + private Process toolProcess; + + public void ClhsdbLauncher() { + toolProcess = null; + } + + /** + * + * Launches 'jhsdb clhsdb' and attaches to the Lingered App process. + * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. + */ + private void attach(long lingeredAppPid) + throws IOException { + + System.out.println("Starting clhsdb against " + lingeredAppPid); + JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); + launcher.addToolArg("clhsdb"); + launcher.addToolArg("--pid=" + Long.toString(lingeredAppPid)); + + ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); + processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); + + toolProcess = processBuilder.start(); + } + + /** + * + * Runs 'jhsdb clhsdb' commands and checks for expected and unexpected strings. + * @param commands - clhsdb commands to execute. + * @param expectedStrMap - Map of expected strings per command which need to + * be checked in the output of the command. + * @param unExpectedStrMap - Map of unexpected strings per command which should + * not be present in the output of the command. + * @return Output of the commands as a String. + */ + private String runCmd(List commands, + Map> expectedStrMap, + Map> unExpectedStrMap) + throws IOException, InterruptedException { + String output; + + if (commands == null) { + throw new RuntimeException("CLHSDB command must be provided\n"); + } + + try (OutputStream out = toolProcess.getOutputStream()) { + for (String cmd : commands) { + out.write((cmd + "\n").getBytes()); + } + out.write("quit\n".getBytes()); + out.flush(); + } + + OutputAnalyzer oa = new OutputAnalyzer(toolProcess); + try { + toolProcess.waitFor(); + } catch (InterruptedException ie) { + toolProcess.destroyForcibly(); + throw new Error("Problem awaiting the child process: " + ie); + } + + oa.shouldHaveExitValue(0); + output = oa.getOutput(); + System.out.println(output); + + String[] parts = output.split("hsdb>"); + for (String cmd : commands) { + int index = commands.indexOf(cmd) + 1; + OutputAnalyzer out = new OutputAnalyzer(parts[index]); + + if (expectedStrMap != null) { + List expectedStr = expectedStrMap.get(cmd); + if (expectedStr != null) { + for (String exp : expectedStr) { + out.shouldContain(exp); + } + } + } + + if (unExpectedStrMap != null) { + List unExpectedStr = unExpectedStrMap.get(cmd); + if (unExpectedStr != null) { + for (String unExp : unExpectedStr) { + out.shouldNotContain(unExp); + } + } + } + } + return output; + } + + /** + * + * Launches 'jhsdb clhsdb', attaches to the Lingered App, executes the commands, + * checks for expected and unexpected strings. + * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. + * @param commands - clhsdb commands to execute. + * @param expectedStrMap - Map of expected strings per command which need to + * be checked in the output of the command. + * @param unExpectedStrMap - Map of unexpected strings per command which should + * not be present in the output of the command. + * @return Output of the commands as a String. + */ + public String run(long lingeredAppPid, + List commands, + Map> expectedStrMap, + Map> unExpectedStrMap) + throws IOException, InterruptedException { + + if (!Platform.shouldSAAttach()) { + // Silently skip the test if we don't have enough permissions to attach + System.out.println("SA attach not expected to work - test skipped."); + return null; + } + + attach(lingeredAppPid); + return runCmd(commands, expectedStrMap, unExpectedStrMap); + } +}