Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/BScenarios/hotswap/tc01x002.java
41161 views
1
/*
2
* Copyright (c) 2002, 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
package nsk.jdi.BScenarios.hotswap;
25
26
import nsk.share.*;
27
import nsk.share.jpda.*;
28
import nsk.share.jdi.*;
29
30
import com.sun.jdi.*;
31
import com.sun.jdi.request.*;
32
import com.sun.jdi.event.*;
33
34
import java.util.*;
35
import java.io.*;
36
37
/**
38
* This test is from the group of so-called Borland's scenarios and
39
* implements the following test case:
40
* Suite 3 - Hot Swap
41
* Test case: TC1
42
* Description: After point of execution, same method
43
* Steps: 1.Set breakpoint at line 24 (call to b()
44
* from a())
45
* 2.Debug Main
46
* 3.Insert as next line after point of
47
* execution: System.err.println("foo");
48
* 4.Smart Swap
49
* 5.Resume
50
* X. Prints out "foo" after printing the
51
* numbers
52
* The description was drown up according to steps under JBuilder.
53
*
54
* Of course, the test has own line numbers and method/class names and
55
* works as follow:
56
* When the test is starting debugee, debugger sets breakpoint at
57
* the 37th line (method <code>method_A</code>).
58
* After the breakpoint is reached, debugger redefines debugee adding
59
* a new line into <code>method_A</code>, pops current frame and resumes debugee.
60
* It is expected the new code will be actual after the redefining
61
*
62
* Step 5 of Borland's scenario is wrong due to redefineClasses spec says:
63
* "The redefined method will be used on new invokes. If resetting these
64
* frames is desired, use <code>ThreadReference.popFrames()</code> with
65
* <code>Method.isObsolete()</code>."
66
*/
67
68
public class tc01x002 {
69
70
public final static String UNEXPECTED_STRING = "***Unexpected exception ";
71
72
private final static String prefix = "nsk.jdi.BScenarios.hotswap.";
73
private final static String debuggerName = prefix + "tc01x002";
74
private final static String debugeeName = debuggerName + "a";
75
76
private final static String newClassFile = "newclass" + File.separator
77
+ debugeeName.replace('.',File.separatorChar)
78
+ ".class";
79
80
private static int exitStatus;
81
private static Log log;
82
private static Debugee debugee;
83
private static long waitTime;
84
private static String classDir;
85
86
private int expectedEventCount = 5;
87
88
private ReferenceType debugeeClass;
89
90
private static void display(String msg) {
91
log.display(msg);
92
}
93
94
private static void complain(String msg) {
95
log.complain("debugger FAILURE> " + msg + "\n");
96
}
97
98
public static void main(String argv[]) {
99
System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
100
}
101
102
public static int run(String argv[], PrintStream out) {
103
104
exitStatus = Consts.TEST_PASSED;
105
106
tc01x002 thisTest = new tc01x002();
107
108
ArgumentHandler argHandler = new ArgumentHandler(argv);
109
log = new Log(out, argHandler);
110
111
classDir = argv[0];
112
waitTime = argHandler.getWaitTime() * 60000;
113
114
Binder binder = new Binder(argHandler, log);
115
debugee = binder.bindToDebugee(debugeeName);
116
117
try {
118
thisTest.execTest();
119
} catch (Throwable e) {
120
exitStatus = Consts.TEST_FAILED;
121
e.printStackTrace();
122
} finally {
123
debugee.endDebugee();
124
}
125
display("Test finished. exitStatus = " + exitStatus);
126
127
return exitStatus;
128
}
129
130
private void execTest() throws Failure {
131
132
if (!debugee.VM().canRedefineClasses()) {
133
display("\n>>>canRedefineClasses() is false<<< test canceled.\n");
134
return;
135
}
136
137
display("\nTEST BEGINS");
138
display("===========");
139
140
EventSet eventSet = null;
141
EventIterator eventIterator = null;
142
Event event;
143
long totalTime = waitTime;
144
long tmp, begin = System.currentTimeMillis(),
145
delta = 0;
146
boolean exit = false;
147
148
int eventCount = 0;
149
EventRequestManager evm = debugee.getEventRequestManager();
150
ClassPrepareRequest req = evm.createClassPrepareRequest();
151
req.addClassFilter(debugeeName);
152
req.enable();
153
debugee.resume();
154
155
while (totalTime > 0 && !exit) {
156
if (eventIterator == null || !eventIterator.hasNext()) {
157
try {
158
eventSet = debugee.VM().eventQueue().remove(totalTime);
159
} catch (InterruptedException e) {
160
new Failure(e);
161
}
162
if (eventSet != null) {
163
eventIterator = eventSet.eventIterator();
164
} else {
165
eventIterator = null;
166
}
167
}
168
if (eventIterator != null) {
169
while (eventIterator.hasNext()) {
170
event = eventIterator.nextEvent();
171
// display("\n event ===>>> " + event);
172
173
if (event instanceof ClassPrepareEvent) {
174
display("\n event ===>>> " + event);
175
debugeeClass = ((ClassPrepareEvent )event).referenceType();
176
display("Tested class\t:" + debugeeClass.name());
177
debugee.setBreakpoint(debugeeClass,
178
tc01x002a.brkpMethodName,
179
tc01x002a.brkpLineNumber);
180
181
debugee.resume();
182
eventCount++;
183
184
} else if (event instanceof BreakpointEvent) {
185
display("\n event ===>>> " + event);
186
display("redefining...");
187
redefineDebugee();
188
popFrames(((BreakpointEvent )event).thread());
189
190
createMethodExitRequest(debugeeClass);
191
debugee.resume();
192
eventCount++;
193
194
} else if (event instanceof MethodExitEvent) {
195
display("\n event ===>>> " + event);
196
Method mthd = ((MethodExitEvent )event).method();
197
eventCount++;
198
display("exiting from \"" + mthd.name() + "\" method");
199
if (mthd.name().compareTo(tc01x002a.brkpMethodName) == 0) {
200
Field fld = debugeeClass.fieldByName(tc01x002a.fieldToCheckName);
201
Value val = debugeeClass.getValue(fld);
202
if (((IntegerValue )val).value() != tc01x002a.CHANGED_VALUE) {
203
complain("Unexpected: new code is not actual "
204
+ "after resetting frames");
205
complain("Unexpected value of checked field: "
206
+ val);
207
exitStatus = Consts.TEST_FAILED;
208
} else {
209
display("!!!Expected: new code is actual "
210
+ "after resetting frames!!!");
211
}
212
}
213
debugee.resume();
214
215
} else if (event instanceof VMDeathEvent) {
216
exit = true;
217
break;
218
} else if (event instanceof VMDisconnectEvent) {
219
exit = true;
220
break;
221
} // if
222
} // while
223
} // if
224
tmp = System.currentTimeMillis();
225
delta = tmp - begin;
226
totalTime -= delta;
227
begin = tmp;
228
}
229
230
if (eventCount != expectedEventCount) {
231
if (totalTime <= 0) {
232
complain("out of wait time...");
233
}
234
complain("expecting " + expectedEventCount
235
+ " events, but "
236
+ eventCount + " events arrived.");
237
exitStatus = Consts.TEST_FAILED;
238
}
239
240
display("=============");
241
display("TEST FINISHES\n");
242
}
243
244
private void redefineDebugee() {
245
Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;
246
boolean alreadyComplained = false;
247
mapBytes = mapClassToBytes(newClassFile);
248
try {
249
debugee.VM().redefineClasses(mapBytes);
250
} catch (Exception e) {
251
throw new Failure(UNEXPECTED_STRING + e);
252
}
253
}
254
255
private void popFrames(ThreadReference thread) {
256
try {
257
StackFrame frame = thread.frame(0);
258
thread.popFrames(frame);
259
} catch (IncompatibleThreadStateException e) {
260
throw new Failure(UNEXPECTED_STRING + e);
261
}
262
}
263
264
private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {
265
display("class-file\t:" + fileName);
266
File fileToBeRedefined = new File(classDir + File.separator + fileName);
267
int fileToRedefineLength = (int )fileToBeRedefined.length();
268
byte[] arrayToRedefine = new byte[fileToRedefineLength];
269
270
FileInputStream inputFile;
271
try {
272
inputFile = new FileInputStream(fileToBeRedefined);
273
} catch (FileNotFoundException e) {
274
throw new Failure(UNEXPECTED_STRING + e);
275
}
276
277
try {
278
inputFile.read(arrayToRedefine);
279
inputFile.close();
280
} catch (IOException e) {
281
throw new Failure(UNEXPECTED_STRING + e);
282
}
283
HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();
284
mapForClass.put(debugeeClass, arrayToRedefine);
285
return mapForClass;
286
}
287
288
private MethodExitRequest createMethodExitRequest(ReferenceType refType) {
289
EventRequestManager evm = debugee.getEventRequestManager();
290
MethodExitRequest request = evm.createMethodExitRequest();
291
request.addClassFilter(refType);
292
request.enable();
293
return request;
294
}
295
}
296
297