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