Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/sa/ClhsdbInspect.java
41152 views
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
Map<String, String> tokensMap = new HashMap<>();
61
tokensMap.put("(a java.lang.Class for LingeredAppWithLock)",
62
"instance of Oop for java/lang/Class");
63
tokensMap.put("Method*=", "Type is Method");
64
tokensMap.put("(a java.lang.ref.ReferenceQueue$Lock)",
65
"instance of Oop for java/lang/ref/ReferenceQueue\\$Lock");
66
67
String[] lines = jstackOutput.split("\\R");
68
69
for (String key: tokensMap.keySet()) {
70
cmds = new ArrayList<String>();
71
Map<String, List<String>> expStrMap = new HashMap<>();
72
73
String addressString = null;
74
for (String line : lines) {
75
if (line.contains(key)) {
76
// Escape the token "Method*=" because the split method uses
77
// a regex, not just a straight String.
78
String escapedKey = key.replace("*","\\*");
79
String[] words = line.split(escapedKey+"|[ ]");
80
for (String word : words) {
81
word = word.replace("<","").replace(">","");
82
if (word.startsWith("0x")) {
83
addressString = word;
84
break;
85
}
86
}
87
if (addressString != null)
88
break;
89
}
90
}
91
92
String cmd = "inspect " + addressString;
93
cmds.add(cmd);
94
expStrMap.put(cmd, List.of(tokensMap.get(key)));
95
test.run(theApp.getPid(), cmds, expStrMap, null);
96
}
97
98
// This part is testing JDK-8261269. When inspecting a java object, we want to make
99
// sure the address is not printed twice and that "Oop for ..." is not printed twice.
100
//
101
// The goal of this test is to dump the Class instance for java.lang.System. It contains
102
// some Oop statics, and that's where the redundant "Oop for..." was noticed. The script
103
// looks something like this:
104
//
105
// hsdb> class java.lang.System
106
// java/lang/System @0x000000080000f388
107
//
108
// hsdb> inspect 0x000000080000f388
109
// Type is InstanceKlass (size of 480)
110
// ...
111
// OopHandle Klass::_java_mirror: OopHandle @ 0x000000080000f400
112
// ...
113
//
114
// hsdb> examine 0x000000080000f400
115
// 0x000000080000f400: 0x00007fd8b812e5e8
116
//
117
// hsdb> examine 0x00007fd8b812e5e8
118
// 0x00007fd8b812e5e8: 0x00000007fef00770
119
//
120
// hsdb> inspect 0x00000007fef00770
121
// instance of Oop for java/lang/Class @ 0x00000007fef00770 @ 0x00000007fef00770 (size = 160)
122
// in: Oop for java/io/BufferedInputStream @ 0x0000000082005b08 Oop for java/io/BufferedInputStream @ 0x0000000082005b08
123
// out: Oop for java/io/PrintStream @ 0x0000000082007b60 Oop for java/io/PrintStream @ 0x0000000082007b60
124
// err: Oop for java/io/PrintStream @ 0x000000008200e0c8 Oop for java/io/PrintStream @ 0x000000008200e0c8
125
126
String cmd;
127
Map<String, List<String>> expStrMap;
128
Map<String, List<String>> unexpStrMap;
129
130
// Start with the "class java.lang.System"
131
cmd = "class java.lang.System";
132
cmds = List.of(cmd);
133
expStrMap = new HashMap<>();
134
expStrMap.put(cmd, List.of("java.lang.System @0x"));
135
String classCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
136
137
// "inspect" the address produced by the "class java.lang.System". This is the InstanceKlass.
138
String classAddress = classCmdOutput.substring(classCmdOutput.indexOf("@0x")+1);
139
lines = classAddress.split("\\R");
140
classAddress = lines[0];
141
cmd = "inspect " + classAddress;
142
cmds = List.of(cmd);
143
expStrMap = new HashMap<>();
144
expStrMap.put(cmd, List.of("Type is InstanceKlass", "Klass::_java_mirror: OopHandle @"));
145
String inspectCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
146
147
// Get the Klass::_java_mirror value from the InstanceKlass
148
String mirrorPattern = "Klass::_java_mirror: OopHandle @ ";
149
String mirrorAddress = inspectCmdOutput.substring(
150
inspectCmdOutput.indexOf(mirrorPattern) + mirrorPattern.length());
151
lines = mirrorAddress.split("\\R");
152
mirrorAddress = lines[0];
153
154
// Use "examine" to do an indirection of the _java_mirror.
155
cmd = "examine " + mirrorAddress;
156
cmds = List.of(cmd);
157
expStrMap = new HashMap<>();
158
expStrMap.put(cmd, List.of(mirrorAddress + ": 0x"));
159
String examineCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
160
String examineResult = examineCmdOutput.substring(examineCmdOutput.indexOf(": 0x")+2);
161
lines = examineResult.split("\\R");
162
examineResult = lines[0].trim(); // examine leaves a trailing space
163
164
// Do another indirection using "examine" to get to the address of the Class instance.
165
cmd = "examine " + examineResult;
166
cmds = List.of(cmd);
167
expStrMap = new HashMap<>();
168
expStrMap.put(cmd, List.of(examineResult + ": 0x"));
169
examineCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
170
examineResult = examineCmdOutput.substring(examineCmdOutput.indexOf(": 0x")+2);
171
lines = examineResult.split("\\R");
172
examineResult = lines[0].trim(); // examine leaves a trailing space
173
174
// inspect the Class instance
175
String instanceOfString = "instance of Oop for java/lang/Class @ ";
176
String staticFieldString = "Oop for java/io/BufferedInputStream @";
177
cmd = "inspect " + examineResult;
178
cmds = List.of(cmd);
179
expStrMap = new HashMap<>();
180
expStrMap.put(cmd, List.of(instanceOfString + examineResult,
181
"in: " + staticFieldString));
182
unexpStrMap = new HashMap<>();
183
// Make sure we don't see the address of the class intance twice, and make sure
184
// we don't see "Oop for ..." twice for the "in" static field.
185
unexpStrMap.put(cmd, List.of(
186
instanceOfString + examineResult + " @ " + examineResult,
187
"in: " + staticFieldString + " .* " + staticFieldString));
188
inspectCmdOutput = test.run(theApp.getPid(), cmds, expStrMap, unexpStrMap);
189
} catch (SkippedException e) {
190
throw e;
191
} catch (Exception ex) {
192
throw new RuntimeException("Test ERROR " + ex, ex);
193
} finally {
194
LingeredApp.stopApp(theApp);
195
}
196
System.out.println("Test PASSED");
197
}
198
}
199
200