-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathFinalizerTest.java
More file actions
202 lines (182 loc) · 7.24 KB
/
Copy pathFinalizerTest.java
File metadata and controls
202 lines (182 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4272800 4274208 4392010
* @summary Test debugger operations in finalize() methods
* @author Gordon Hirsch (modified for HotSpot by tbell & rfield)
*
* @run build TestScaffold VMConnection TargetListener TargetAdapter
* @run compile -g FinalizerTest.java
*
* @run driver FinalizerTest -Xmx256M
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.sun.jdi.StackFrame;
import com.sun.jdi.event.BreakpointEvent;
import com.sun.jdi.event.StepEvent;
/*
* Debuggee which exercises a finalize() method. There's no guarantee
* that this will work, but we need some way of attempting to test
* the debugging of finalizers.
* @author Gordon Hirsch (modified for HotSpot by tbell & rfield)
*/
class FinalizerTarg {
static final CountDownLatch finalizerDone = new CountDownLatch(1);
static class BigObject {
String name;
byte[] foo = new byte[300000];
public BigObject (String _name) {
super();
this.name = _name;
}
protected void finalize() throws Throwable {
/*
* JLS 2nd Ed. section 12.6 "Finalization of Class Instances" "[...]
* invoke the finalize method for its superclass, [...] usually good
* practice [...]"
*/
super.finalize();
//Thread.dumpStack();
finalizerDone.countDown();
}
}
static void waitForAFinalizer() {
String s = Integer.toString(1);
BigObject b = new BigObject (s);
b = null; // Drop the object, creating garbage...
System.gc();
System.runFinalization();
try {
// finalize() is run in another lower priority Finalizer thread.
// Wait for it to finish.
finalizerDone.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// If System.gc() and System.runFinalization() did not trigger the
// finalizer, then finalizerDone.await() will time out and the code
// below will be needed as a second attempt to trigger finalization.
List holdAlot = new ArrayList();
for (int chunk=10000000; chunk > 10000; chunk = chunk / 2) {
if (finalizerDone.getCount() == 0) {
return;
}
try {
while (finalizerDone.getCount() > 0) {
holdAlot.add(new byte[chunk]);
System.err.println("Allocated " + chunk);
}
} catch ( Throwable thrown ) { // OutOfMemoryError
} finally {
holdAlot.clear();
System.gc();
}
System.runFinalization();
}
System.out.println("The Debuggee should not reach here in theory!");
return;
}
public static void main(String[] args) throws Exception {
/*
* Spin in waitForAFinalizer() while waiting for
* another thread to run the finalizer on one of the
* BigObjects ...
*/
waitForAFinalizer();
}
}
///// End of debuggee
public class FinalizerTest extends TestScaffold {
public static void main(String args[])
throws Exception {
new FinalizerTest (args).startTests();
}
public FinalizerTest (String args[]) {
super(args);
}
protected void runTests() throws Exception {
try {
BreakpointEvent event0 = startToMain("FinalizerTarg");
BreakpointEvent event1 = resumeTo("FinalizerTarg$BigObject",
"finalize", "()V");
println("Breakpoint at " +
event1.location().method().name() + ":" +
event1.location().lineNumber() + " (" +
event1.location().codeIndex() + ")");
/*
* Record information about the current location
*/
List frames = event1.thread().frames();
List methodStack = new ArrayList(frames.size());
Iterator iter = frames.iterator();
while (iter.hasNext()) {
StackFrame frame = (StackFrame) iter.next();
methodStack.add(frame.location().declaringType().name() +
"." + frame.location().method().name());
}
println("Try a stepOverLine()...");
StepEvent stepEvent = stepOverLine(event1.thread());
println("Step Complete at " +
stepEvent.location().method().name() + ":" +
stepEvent.location().lineNumber() + " (" +
stepEvent.location().codeIndex() + ")");
/*
* Compare current location with recorded location
*/
if (stepEvent.thread().frameCount() != methodStack.size()) {
throw new Exception("Stack depths do not match: original=" +
methodStack.size() +
", current=" +
stepEvent.thread().frameCount());
}
iter = stepEvent.thread().frames().iterator();
Iterator iter2 = methodStack.iterator();
while (iter.hasNext()) {
StackFrame frame = (StackFrame) iter.next();
String name = (String) iter2.next();
String currentName = frame.location().declaringType().name() +
"." + frame.location().method().name();
if (!name.equals(currentName)) {
throw new Exception("Stacks do not match at: original=" +
name + ", current=" + currentName);
}
}
} catch(Exception ex) {
ex.printStackTrace();
testFailed = true;
} finally {
// Allow application to complete and shut down
listenUntilVMDisconnect();
}
if (!testFailed) {
println("FinalizerTest: passed");
} else {
throw new Exception("FinalizerTest: failed");
}
}
}