Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestJhsdbJstackLineNumbers.java
64478 views
1
/*
2
* Copyright (c) 2020, 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
import java.io.OutputStream;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
27
import java.util.stream.Collectors;
28
import java.util.TreeSet;
29
30
import jdk.test.lib.apps.LingeredApp;
31
import jdk.test.lib.JDKToolLauncher;
32
import jdk.test.lib.process.OutputAnalyzer;
33
import jdk.test.lib.process.ProcessTools;
34
import jdk.test.lib.SA.SATestUtils;
35
36
/**
37
* @test
38
* @requires vm.hasSA
39
* @requires os.arch=="amd64" | os.arch=="x86_64"
40
* @requires os.family=="windows" | os.family == "linux" | os.family == "mac"
41
* @requires vm.flagless
42
* @library /test/lib
43
* @run driver TestJhsdbJstackLineNumbers
44
*/
45
46
/*
47
* This test makes sure that SA gets the most accurate value for the line number of
48
* the current (topmost) frame. Many SA ports just rely on frame->bcp, but it is
49
* usually out of date since the current BCP is cached in a register and only flushed
50
* to frame->bcp when the register is needed for something else. Therefore SA ports
51
* need to fetch the register that the BCP is stored in and see if it is valid,
52
* and only defer to frame->bcp if it is not valid.
53
*
54
* The test works by spawning a process that sits in a 10 line loop in the busywork() method,
55
* all while the main test does repeated jstacks on the process. The expectation is
56
* that at least 5 of the lines in the busywork() loop will eventually show up in at
57
* least one of the jstack runs.
58
*/
59
60
class LingeredAppWithBusyWork extends LingeredApp {
61
static volatile boolean stop = false;
62
63
private static int busywork(int[] x) {
64
int i = 0;
65
while (!stop) {
66
i = x[0];
67
i += x[1];
68
i += x[2];
69
i += x[3];
70
i += x[4];
71
i += x[5];
72
i += x[6];
73
i += x[7];
74
}
75
return i;
76
}
77
78
public static void main(String... args) {
79
Thread t = new Thread(() -> {
80
busywork(new int[]{0,1,2,3,4,5,6,7});
81
});
82
83
try {
84
t.setName("BusyWorkThread");
85
t.start();
86
LingeredApp.main(args);
87
stop = true;
88
t.join();
89
} catch (InterruptedException e) {
90
}
91
}
92
}
93
94
public class TestJhsdbJstackLineNumbers {
95
// This is the number of lines in the busywork main loop
96
static final int TOTAL_BUSYWORK_LOOP_LINES = 10;
97
// The minimum number of lines that we must at some point see in the jstack output
98
static final int MIN_BUSYWORK_LOOP_LINES = 5;
99
100
static final int MAX_NUMBER_OF_JSTACK_RUNS = 25;
101
102
private static OutputAnalyzer runJstack(String... toolArgs) throws Exception {
103
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
104
launcher.addToolArg("jstack");
105
if (toolArgs != null) {
106
for (String toolArg : toolArgs) {
107
launcher.addToolArg(toolArg);
108
}
109
}
110
111
ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);
112
System.out.println(processBuilder.command().stream().collect(Collectors.joining(" ")));
113
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
114
115
return output;
116
}
117
118
public static void runTest(long pid) throws Exception {
119
// Keep running jstack until the target app is in the "busywork" method.
120
String output;
121
int maxRetries = 5;
122
do {
123
if (maxRetries-- == 0) {
124
throw new RuntimeException("Failed: LingeredAppWithBusyWork never entered busywork() method.");
125
}
126
OutputAnalyzer jstackOut = runJstack("--pid", Long.toString(pid));
127
output = jstackOut.getOutput();
128
System.out.println(output);
129
} while (!output.contains("busywork"));
130
131
// This is for tracking all the line numbers in busywork() that we've seen.
132
// Since it is a TreeSet, it will always be sorted and have no duplicates.
133
TreeSet<Integer> lineNumbersSeen = new TreeSet<Integer>();
134
135
// Keep running jstack until we see a sufficient number of different line
136
// numbers in the busywork() loop.
137
for (int x = 0; x < MAX_NUMBER_OF_JSTACK_RUNS; x++) {
138
OutputAnalyzer jstackOut = runJstack("--pid", Long.toString(pid));
139
output = jstackOut.getOutput();
140
// The stack dump will have a line that looks like:
141
// - LingeredAppWithBusyWork.busywork(int[]) @bci=32, line=74 (Interpreted frame)
142
// We want to match on the line number, "74" in this example. We also match on the
143
// full line just so we can print it out.
144
Pattern LINE_PATTERN = Pattern.compile(
145
".+(- LingeredAppWithBusyWork.busywork\\(int\\[\\]\\) \\@bci\\=[0-9]+, line\\=([0-9]+) \\(Interpreted frame\\)).+", Pattern.DOTALL);
146
Matcher matcher = LINE_PATTERN.matcher(output);
147
if (matcher.matches()) {
148
System.out.println(matcher.group(1)); // print matching stack trace line
149
int lineNum = Integer.valueOf(matcher.group(2)); // get matching line number
150
lineNumbersSeen.add(lineNum);
151
if (lineNumbersSeen.size() == MIN_BUSYWORK_LOOP_LINES) {
152
// We're done!
153
System.out.println("Found needed line numbers after " + (x+1) + " iterations");
154
break;
155
}
156
} else {
157
System.out.println("failed to match");
158
System.out.println(output);
159
continue; // Keep trying. This can happen on rare occasions when the stack cannot be determined.
160
}
161
}
162
System.out.println("Found Line Numbers: " + lineNumbersSeen);
163
164
// Make sure we saw the minimum required number of lines in busywork().
165
if (lineNumbersSeen.size() < MIN_BUSYWORK_LOOP_LINES) {
166
throw new RuntimeException("Failed: Didn't find enough line numbers: " + lineNumbersSeen);
167
}
168
169
// Make sure the distance between the lowest and highest line numbers seen
170
// is not more than the number of lines in the busywork() loop.
171
if (lineNumbersSeen.last() - lineNumbersSeen.first() > TOTAL_BUSYWORK_LOOP_LINES) {
172
throw new RuntimeException("Failed: lowest and highest line numbers are too far apart: " + lineNumbersSeen);
173
}
174
175
}
176
177
public static void main(String... args) throws Exception {
178
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
179
180
LingeredApp theApp = null;
181
try {
182
// Launch the LingeredAppWithBusyWork process with the busywork() loop
183
theApp = new LingeredAppWithBusyWork();
184
LingeredApp.startAppExactJvmOpts(theApp, "-Xint");
185
System.out.println("Started LingeredApp with pid " + theApp.getPid());
186
187
runTest(theApp.getPid());
188
} finally {
189
LingeredApp.stopApp(theApp);
190
System.out.println("LingeredAppWithBusyWork finished");
191
}
192
}
193
}
194
195