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 'type' 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 TestType
43  */
44 
45 public class TestType {
46 
testClhsdbForType( long lingeredAppPid, String commandString, String[] expectedOutputStrings)47     private static void testClhsdbForType(
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         System.out.println(
61             pb.command().stream().collect(Collectors.joining(" ")));
62 
63         try {
64             p = pb.start();
65         } catch (Exception attachE) {
66             throw new Error("Couldn't start jhsdb or attach to LingeredApp : " + attachE);
67         }
68 
69         // Issue the 'type' commands at the clhsdb prompt.
70         OutputStream input = p.getOutputStream();
71         try {
72             input.write((commandString + "\n").getBytes());
73             input.write("quit\n".getBytes());
74             input.flush();
75         } catch (IOException ioe) {
76             throw new Error("Problem issuing the 'type' command ", ioe);
77         }
78 
79         OutputAnalyzer output = new OutputAnalyzer(p);
80 
81         try {
82             p.waitFor();
83         } catch (InterruptedException ie) {
84             p.destroyForcibly();
85             throw new Error("Problem awaiting the child process: " + ie);
86         }
87 
88         output.shouldHaveExitValue(0);
89         System.out.println(output.getOutput());
90 
91         for (String expectedOutputString: expectedOutputStrings) {
92             output.shouldContain(expectedOutputString);
93         }
94     }
95 
main(String... args)96     public static void main (String... args) throws Exception {
97         LingeredApp app = null;
98 
99         try {
100             List<String> vmArgs = new ArrayList<String>();
101             vmArgs.addAll(Utils.getVmOptions());
102             // Strings to check for in the output of 'type'. The 'type'
103             // command prints out entries from 'gHotSpotVMTypes', which
104             // is a table containing the hotspot types, their supertypes,
105             // sizes, etc, which are of interest to the SA.
106             String[] defaultOutputStrings =
107                 {"type G1CollectedHeap CollectedHeap",
108                  "type ConstantPoolCache MetaspaceObj",
109                  "type ConstantPool Metadata",
110                  "type CompilerThread JavaThread",
111                  "type CardGeneration Generation",
112                  "type ArrayKlass Klass",
113                  "type InstanceKlass Klass"};
114             // String to check for in the output of "type InstanceKlass"
115             String[] instanceKlassOutputString = {"type InstanceKlass Klass"};
116 
117             app = LingeredApp.startApp(vmArgs);
118             System.out.println ("Started LingeredApp with pid " + app.getPid());
119             testClhsdbForType(app.getPid(), "type", defaultOutputStrings);
120             testClhsdbForType(app.getPid(),
121                               "type InstanceKlass",
122                               instanceKlassOutputString);
123         } finally {
124             LingeredApp.stopApp(app);
125         }
126     }
127 }
128