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