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