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