1 /*
  2  * Copyright (c) 2017, 2021, 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 /**
 25  * @test
 26  * @bug 8192985
 27  * @summary Test the clhsdb 'inspect' command
 28  * @requires vm.hasSA
 29  * @library /test/lib
 30  * @run main/othervm/timeout=480 ClhsdbInspect
 31  */
 32 
 33 import java.util.HashMap;
 34 import java.util.List;
 35 import java.util.Map;
 36 import java.util.ArrayList;
 37 import jdk.test.lib.apps.LingeredApp;
 38 import jtreg.SkippedException;
 39 
 40 public class ClhsdbInspect {
 41 
 42     public static void main(String[] args) throws Exception {
 43         System.out.println("Starting the ClhsdbInspect test");
 44 
 45         LingeredAppWithLock theApp = null;
 46         try {
 47             ClhsdbLauncher test = new ClhsdbLauncher();
 48 
 49             theApp = new LingeredAppWithLock();
 50             LingeredApp.startApp(theApp);
 51             System.out.println("Started LingeredApp with pid " + theApp.getPid());
 52 
 53             // Run the 'jstack -v' command to get the address of a Method*,
 54             // the oop address of a java.lang.ref.ReferenceQueue$Lock
 55             // and the oop address of a java.lang.Class object
 56             List<String> cmds = List.of("jstack -v");
 57 
 58             String jstackOutput = test.run(theApp.getPid(), cmds, null, null);
 59 
 60             // the key is a literal, searched in the jstack output; the value is a regex searched in the clhsdb output
 61             Map<String, String> tokensMap = new HashMap<>();
 62             tokensMap.put("(a java.lang.Class for LingeredAppWithLock)",
 63                           "instance of Oop for java/lang/Class");
 64             tokensMap.put("Method*=", "Type is Method");
 65             tokensMap.put("(a java/util/concurrent/locks/AbstractQueuedSynchronizer$ConditionObject)",
 66                           "instance of Oop for java/util/concurrent/locks/AbstractQueuedSynchronizer\\$ConditionObject");
 67             
 68             // System.out.println("======>>>>>\n" + jstackOutput + "\n<<<<<========");
 69 
 70             String[] lines = jstackOutput.split("\\R");
 71 
 72             for (String key: tokensMap.keySet()) {
 73                 cmds = new ArrayList<String>();
 74                 Map<String, List<String>> expStrMap = new HashMap<>();
 75 
 76                 String addressString = null;
 77                 for (String line : lines) {
 78                     if (line.contains(key)) {
 79                         // Escape the token "Method*=" because the split method uses
 80                         // a regex, not just a straight String.
 81                         String escapedKey = key.replace("*","\\*");
 82                         String[] words = line.split(escapedKey+"|[ ]");
 83                         for (String word : words) {
 84                             word = word.replace("<","").replace(">","");
 85                             if (word.startsWith("0x")) {
 86                                 addressString = word;
 87                                 break;
 88                             }
 89                         }
 90                         if (addressString != null)
 91                             break;
 92                       }
 93                   }
 94 
 95                 if (addressString == null) throw new NullPointerException("Token '" + key + "' not found in jstack output");
 96                 String cmd = "inspect " + addressString;
 97                 cmds.add(cmd);
 98                 expStrMap.put(cmd, List.of(tokensMap.get(key)));
 99                 test.run(theApp.getPid(), cmds, expStrMap, null);
100             }
101 
102             // This part is testing JDK-8261269. When inspecting a java object, we want to make
103             // sure the address is not printed twice and that "Oop for ..." is not printed twice.
104             //
105             // The goal of this test is to dump the Class instance for java.lang.System. It contains
106             // some Oop statics, and that's where the redundant "Oop for..." was noticed. The script
107             // looks something like this:
108             //
109             // hsdb> class java.lang.System
110             // java/lang/System @0x000000080000f388
111             //
112             // hsdb> inspect 0x000000080000f388
113             // Type is InstanceKlass (size of 480)
114             // ...
115             // OopHandle Klass::_java_mirror: OopHandle @ 0x000000080000f400
116             // ...
117             //
118             // hsdb> examine 0x000000080000f400
119             // 0x000000080000f400: 0x00007fd8b812e5e8
120             //
121             // hsdb> examine 0x00007fd8b812e5e8
122             // 0x00007fd8b812e5e8: 0x00000007fef00770
123             //
124             // hsdb> inspect 0x00000007fef00770
125             // instance of Oop for java/lang/Class @ 0x00000007fef00770 @ 0x00000007fef00770 (size = 160)
126             // in: Oop for java/io/BufferedInputStream @ 0x0000000082005b08 Oop for java/io/BufferedInputStream @ 0x0000000082005b08
127             // out: Oop for java/io/PrintStream @ 0x0000000082007b60 Oop for java/io/PrintStream @ 0x0000000082007b60
128             // err: Oop for java/io/PrintStream @ 0x000000008200e0c8 Oop for java/io/PrintStream @ 0x000000008200e0c8
129 
130             String cmd;
131             Map<String, List<String>> expStrMap;
132             Map<String, List<String>> unexpStrMap;
133 
134             // Start with the "class java.lang.System"
135             cmd = "class java.lang.System";
136             cmds = List.of(cmd);
137             expStrMap = new HashMap<>();
138             expStrMap.put(cmd, List.of("java.lang.System @0x"));
139             String classCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
140 
141             // "inspect" the address produced by the "class java.lang.System". This is the InstanceKlass.
142             String classAddress = classCmdOutput.substring(classCmdOutput.indexOf("@0x")+1);
143             lines = classAddress.split("\\R");
144             classAddress = lines[0];
145             cmd = "inspect " + classAddress;
146             cmds = List.of(cmd);
147             expStrMap = new HashMap<>();
148             expStrMap.put(cmd, List.of("Type is InstanceKlass", "Klass::_java_mirror: OopHandle @"));
149             String inspectCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
150 
151             // Get the Klass::_java_mirror value from the InstanceKlass
152             String mirrorPattern = "Klass::_java_mirror: OopHandle @ ";
153             String mirrorAddress = inspectCmdOutput.substring(
154                      inspectCmdOutput.indexOf(mirrorPattern) + mirrorPattern.length());
155             lines = mirrorAddress.split("\\R");
156             mirrorAddress = lines[0];
157 
158             // Use "examine" to do an indirection of the _java_mirror.
159             cmd = "examine " + mirrorAddress;
160             cmds = List.of(cmd);
161             expStrMap = new HashMap<>();
162             expStrMap.put(cmd, List.of(mirrorAddress + ": 0x"));
163             String examineCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
164             String examineResult = examineCmdOutput.substring(examineCmdOutput.indexOf(": 0x")+2);
165             lines = examineResult.split("\\R");
166             examineResult = lines[0].trim(); // examine leaves a trailing space
167 
168             // Do another indirection using "examine" to get to the address of the Class instance.
169             cmd = "examine " + examineResult;
170             cmds = List.of(cmd);
171             expStrMap = new HashMap<>();
172             expStrMap.put(cmd, List.of(examineResult + ": 0x"));
173             examineCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
174             examineResult = examineCmdOutput.substring(examineCmdOutput.indexOf(": 0x")+2);
175             lines = examineResult.split("\\R");
176             examineResult = lines[0].trim(); // examine leaves a trailing space
177 
178             // inspect the Class instance
179             String instanceOfString = "instance of Oop for java/lang/Class @ ";
180             String staticFieldString = "Oop for java/io/BufferedInputStream @";
181             cmd = "inspect " + examineResult;
182             cmds = List.of(cmd);
183             expStrMap = new HashMap<>();
184             expStrMap.put(cmd, List.of(instanceOfString + examineResult,
185                                        "in: " + staticFieldString));
186             unexpStrMap = new HashMap<>();
187             // Make sure we don't see the address of the class intance twice, and make sure
188             // we don't see "Oop for ..." twice for the "in" static field.
189             unexpStrMap.put(cmd, List.of(
190                     instanceOfString  + examineResult + " @ " + examineResult,
191                     "in: " + staticFieldString + " .* " + staticFieldString));
192             inspectCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, unexpStrMap);
193         } catch (SkippedException e) {
194             throw e;
195         } catch (Exception ex) {
196             throw new RuntimeException("Test ERROR " + ex, ex);
197         } finally {
198             LingeredApp.stopApp(theApp);
199         }
200         System.out.println("Test PASSED");
201     }
202 }