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