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/tc02x002.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: TC2
42
* Description: Before 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 line before point of
47
* execution: System.err.println("foo");
48
* 4.Smart Swap
49
* 5.Set Smart PopFrame to beginging of
50
* method a()
51
* 6.Resume
52
* X. Prints out "foo" and hits breakpoint
53
* on line 24
54
* 7.Resume
55
* X. Prints numbers
56
* The description was drown up according to steps under JBuilder.
57
*
58
* Of course, the test has own line numbers and method/class names and
59
* works as follow:
60
* When the test is starting debugee, debugger sets breakpoint at
61
* the 38th line (method <code>method_A</code>).
62
* After the breakpoint is reached, debugger: <br>
63
* - redefines debugee adding a new line into "method_A", <br>
64
* - pops current frame, <br>
65
* - sets breakpoint at the 38th line again <br>
66
* and resumes debugee.
67
* It is expected the new code will be actual after the redefining and
68
* it doesn't hit breakpoint according to JVMDI Redefine Classes spec :
69
* "...All breakpoints in the class are cleared."
70
*/
71
72
public class tc02x002 {
73
74
public final static String UNEXPECTED_STRING = "***Unexpected exception ";
75
76
private final static String prefix = "nsk.jdi.BScenarios.hotswap.";
77
private final static String debuggerName = prefix + "tc02x002";
78
private final static String debugeeName = debuggerName + "a";
79
80
private final static String newClassFile = "newclass" + File.separator
81
+ debugeeName.replace('.',File.separatorChar)
82
+ ".class";
83
84
private static int exitStatus;
85
private static Log log;
86
private static Debugee debugee;
87
private static long waitTime;
88
private static String classDir;
89
90
private int expectedEventCount = 6;
91
private int eventCount = 0;
92
93
private ReferenceType debugeeClass;
94
95
private static void display(String msg) {
96
log.display(msg);
97
}
98
99
private static void complain(String msg) {
100
log.complain("debugger FAILURE> " + msg + "\n");
101
}
102
103
public static void main(String argv[]) {
104
System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
105
}
106
107
public static int run(String argv[], PrintStream out) {
108
109
exitStatus = Consts.TEST_PASSED;
110
111
tc02x002 thisTest = new tc02x002();
112
113
ArgumentHandler argHandler = new ArgumentHandler(argv);
114
log = new Log(out, argHandler);
115
116
classDir = argv[0];
117
waitTime = argHandler.getWaitTime() * 60000;
118
119
Binder binder = new Binder(argHandler, log);
120
debugee = binder.bindToDebugee(debugeeName);
121
122
try {
123
thisTest.execTest();
124
} catch (Throwable e) {
125
exitStatus = Consts.TEST_FAILED;
126
e.printStackTrace();
127
} finally {
128
debugee.endDebugee();
129
}
130
display("Test finished. exitStatus = " + exitStatus);
131
132
return exitStatus;
133
}
134
135
private void execTest() throws Failure {
136
137
if (!debugee.VM().canRedefineClasses()) {
138
display("\n>>>canRedefineClasses() is false<<< test canceled.\n");
139
return;
140
}
141
142
display("\nTEST BEGINS");
143
display("===========");
144
145
EventSet eventSet = null;
146
EventIterator eventIterator = null;
147
Event event;
148
long totalTime = waitTime;
149
long tmp, begin = System.currentTimeMillis(),
150
delta = 0;
151
boolean exit = false;
152
153
EventRequestManager evm = debugee.getEventRequestManager();
154
ClassPrepareRequest req = evm.createClassPrepareRequest();
155
req.addClassFilter(debugeeName);
156
req.enable();
157
debugee.resume();
158
159
while (totalTime > 0 && !exit) {
160
if (eventIterator == null || !eventIterator.hasNext()) {
161
try {
162
eventSet = debugee.VM().eventQueue().remove(totalTime);
163
} catch (InterruptedException e) {
164
new Failure(e);
165
}
166
if (eventSet != null) {
167
eventIterator = eventSet.eventIterator();
168
} else {
169
eventIterator = null;
170
}
171
}
172
if (eventIterator != null) {
173
while (eventIterator.hasNext()) {
174
event = eventIterator.nextEvent();
175
// display("\n event ===>>> " + event);
176
177
if (event instanceof ClassPrepareEvent) {
178
display("\n event ===>>> " + event);
179
debugeeClass = ((ClassPrepareEvent )event).referenceType();
180
display("Tested class\t:" + debugeeClass.name());
181
debugee.setBreakpoint(debugeeClass,
182
tc02x002a.brkpMethodName,
183
tc02x002a.brkpLineNumber);
184
185
debugee.resume();
186
eventCount++;
187
188
} else if (event instanceof BreakpointEvent) {
189
display("\n event ===>>> " + event);
190
switch (eventCount) {
191
case 1:
192
display("redefining...");
193
redefineDebugee();
194
popFrames(((BreakpointEvent )event).thread());
195
196
debugee.setBreakpoint(debugeeClass,
197
tc02x002a.brkpMethodName,
198
tc02x002a.brkpLineNumber);
199
break;
200
201
case 2:
202
display("!!!Expected second breakpoint event!!!");
203
createMethodExitRequest(debugeeClass);
204
break;
205
206
default:
207
complain("Unexpected breakpoint event");
208
exitStatus = Consts.TEST_FAILED;
209
}
210
eventCount++;
211
hitBreakpoint((BreakpointEvent )event);
212
debugee.resume();
213
214
} else if (event instanceof MethodExitEvent) {
215
display("\n event ===>>> " + event);
216
hitMethodExit((MethodExitEvent)event);
217
eventCount++;
218
debugee.resume();
219
220
} else if (event instanceof VMDeathEvent) {
221
exit = true;
222
break;
223
} else if (event instanceof VMDisconnectEvent) {
224
exit = true;
225
break;
226
} // if
227
} // while
228
} // if
229
tmp = System.currentTimeMillis();
230
delta = tmp - begin;
231
totalTime -= delta;
232
begin = tmp;
233
}
234
235
if (eventCount != expectedEventCount) {
236
if (totalTime <= 0) {
237
complain("out of wait time...");
238
}
239
complain("expecting " + expectedEventCount
240
+ " events, but "
241
+ eventCount + " events arrived.");
242
exitStatus = Consts.TEST_FAILED;
243
}
244
245
display("=============");
246
display("TEST FINISHES\n");
247
}
248
249
private void redefineDebugee() {
250
Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;
251
boolean alreadyComplained = false;
252
mapBytes = mapClassToBytes(newClassFile);
253
try {
254
debugee.VM().redefineClasses(mapBytes);
255
} catch (Exception e) {
256
throw new Failure(UNEXPECTED_STRING + e);
257
}
258
}
259
260
private void popFrames(ThreadReference thread) {
261
try {
262
StackFrame frame = thread.frame(0);
263
thread.popFrames(frame);
264
} catch (IncompatibleThreadStateException e) {
265
throw new Failure(UNEXPECTED_STRING + e);
266
}
267
}
268
269
private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {
270
display("class-file\t:" + fileName);
271
File fileToBeRedefined = new File(classDir + File.separator + fileName);
272
int fileToRedefineLength = (int )fileToBeRedefined.length();
273
byte[] arrayToRedefine = new byte[fileToRedefineLength];
274
275
FileInputStream inputFile;
276
try {
277
inputFile = new FileInputStream(fileToBeRedefined);
278
} catch (FileNotFoundException e) {
279
throw new Failure(UNEXPECTED_STRING + e);
280
}
281
282
try {
283
inputFile.read(arrayToRedefine);
284
inputFile.close();
285
} catch (IOException e) {
286
throw new Failure(UNEXPECTED_STRING + e);
287
}
288
HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();
289
mapForClass.put(debugeeClass, arrayToRedefine);
290
return mapForClass;
291
}
292
293
private MethodExitRequest createMethodExitRequest(ReferenceType refType) {
294
EventRequestManager evm = debugee.getEventRequestManager();
295
MethodExitRequest request = evm.createMethodExitRequest();
296
request.addClassFilter(refType);
297
request.enable();
298
return request;
299
}
300
301
private void hitBreakpoint(BreakpointEvent event) {
302
locationInfo(event);
303
if (event.location().lineNumber() != tc02x002a.checkLastLine) {
304
complain("BreakpointEvent steps to line " + event.location().lineNumber()
305
+ ", expected line number is "
306
+ tc02x002a.checkLastLine);
307
exitStatus = Consts.TEST_FAILED;
308
} else {
309
display("!!!BreakpointEvent steps to the expected line "
310
+ event.location().lineNumber() + "!!!");
311
}
312
display("");
313
}
314
315
private void hitMethodExit(MethodExitEvent event) {
316
317
locationInfo(event);
318
319
Method mthd = ((MethodExitEvent )event).method();
320
if (mthd.name().compareTo(tc02x002a.brkpMethodName) == 0) {
321
Field fld = debugeeClass.fieldByName(tc02x002a.fieldToCheckName);
322
Value val = debugeeClass.getValue(fld);
323
if (((IntegerValue )val).value() != tc02x002a.CHANGED_VALUE) {
324
complain("Unexpected: new code is not actual "
325
+ "after resetting frames");
326
complain("Unexpected value of checked field: "
327
+ val);
328
exitStatus = Consts.TEST_FAILED;
329
} else {
330
display("!!!Expected: new code is actual "
331
+ "after resetting frames!!!");
332
}
333
}
334
display("");
335
}
336
337
private void locationInfo(LocatableEvent event) {
338
display("event info:");
339
display("\tthread\t- " + event.thread().name());
340
try {
341
display("\tsource\t- " + event.location().sourceName());
342
} catch (AbsentInformationException e) {
343
}
344
display("\tmethod\t- " + event.location().method().name());
345
display("\tline\t- " + event.location().lineNumber());
346
}
347
}
348
349