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.util.ArrayList;
25 import java.util.List;
26 import java.io.IOException;
27 import java.util.stream.Collectors;
28 import java.io.OutputStream;
29 import jdk.test.lib.apps.LingeredApp;
30 import jdk.test.lib.JDKToolLauncher;
31 import jdk.test.lib.Platform;
32 import jdk.test.lib.process.OutputAnalyzer;
33 import jdk.test.lib.Utils;
34 
35 /**
36  * @test
37  * @summary Test the 'intConstant' command of jhsdb clhsdb.
38  * @bug 8190307
39  * @requires vm.hasSAandCanAttach
40  * @library /test/lib
41  * @build jdk.test.lib.apps.*
42  * @run main/othervm TestIntConstant
43  */
44 
45 public class TestIntConstant {
46 
testClhsdbForIntConstant( long lingeredAppPid, String commandString, String[] expectedOutputStrings)47     private static void testClhsdbForIntConstant(
48                         long lingeredAppPid,
49                         String commandString,
50                         String[] expectedOutputStrings) throws Exception {
51 
52         Process p;
53         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
54         launcher.addToolArg("clhsdb");
55         launcher.addToolArg("--pid");
56         launcher.addToolArg(Long.toString(lingeredAppPid));
57 
58         ProcessBuilder pb = new ProcessBuilder();
59         pb.command(launcher.getCommand());
60         pb.redirectError(ProcessBuilder.Redirect.INHERIT);
61         System.out.println(
62             pb.command().stream().collect(Collectors.joining(" ")));
63 
64         try {
65             p = pb.start();
66         } catch (Exception attachE) {
67             throw new Error("Couldn't start jhsdb or attach to LingeredApp : " + attachE);
68         }
69 
70         // Issue the 'intConstant' inputs at the clhsdb prompt.
71         OutputStream input = p.getOutputStream();
72         try {
73             input.write((commandString + "\n").getBytes());
74             input.write("quit\n".getBytes());
75             input.flush();
76         } catch (IOException ioe) {
77             throw new Error("Problem issuing the intConstant command: " +
78                             commandString + ioe);
79         }
80 
81         OutputAnalyzer output = new OutputAnalyzer(p);
82 
83         System.out.println("Awaiting process completion");
84         try {
85             p.waitFor();
86         } catch (InterruptedException ie) {
87             p.destroyForcibly();
88             throw new Error("Problem awaiting the child process: " + ie);
89         }
90 
91         output.shouldHaveExitValue(0);
92         System.out.println(output.getOutput());
93         for (String expectedOutputString: expectedOutputStrings) {
94             output.shouldContain(expectedOutputString);
95         }
96     }
97 
testIntConstant()98     public static void testIntConstant() throws Exception {
99         LingeredApp app = null;
100 
101         try {
102             List<String> vmArgs = new ArrayList<String>();
103             vmArgs.addAll(Utils.getVmOptions());
104 
105             app = LingeredApp.startApp(vmArgs);
106             System.out.println ("Started LingeredApp with pid " + app.getPid());
107 
108             // Strings to check for in the output of 'intConstant'. The
109             // 'intConstant' command prints out entries from the
110             // 'gHotSpotVMIntConstants', which is a table of integer constants,
111             // with names and the values derived from enums and #define preprocessor
112             // macros in hotspot.
113             String[] defaultOutputStrings =
114                 {"CollectedHeap::G1 4",
115                  "RUNNABLE 2",
116                  "Deoptimization::Reason_class_check 4",
117                  "InstanceKlass::_misc_is_anonymous 32",
118                  "Generation::ParNew 1",
119                  "_thread_uninitialized 0"};
120             String[] tempConstantString = {"intConstant _temp_constant 45"};
121             testClhsdbForIntConstant(app.getPid(), "intConstant", defaultOutputStrings);
122             testClhsdbForIntConstant(
123                 app.getPid(),
124                 "intConstant _temp_constant 45\nintConstant _temp_constant",
125                 tempConstantString);
126           } finally {
127               LingeredApp.stopApp(app);
128           }
129     }
130 
main(String... args)131     public static void main (String... args) throws Exception {
132 
133         try {
134             testIntConstant();
135         } catch (Exception e) {
136             throw new Error("Test failed with " + e);
137         }
138     }
139 }
140