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/tc02x001.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
* and resumes debugee.
66
* It is expected the new code will be actual after the redefining and
67
* it doesn't hit breakpoint according to JVMDI Redefine Classes spec :
68
* "...All breakpoints in the class are cleared."
69
*/
70
71
public class tc02x001 {
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 + "tc02x001";
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 = 5;
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
tc02x001 thisTest = new tc02x001();
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
tc02x001a.brkpMethodName,
182
tc02x001a.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
createMethodExitRequest(debugeeClass);
195
196
break;
197
198
default:
199
complain("Unexpected breakpoint event");
200
exitStatus = Consts.TEST_FAILED;
201
}
202
eventCount++;
203
hitBreakpoint((BreakpointEvent )event);
204
debugee.resume();
205
206
} else if (event instanceof MethodExitEvent) {
207
display("\n event ===>>> " + event);
208
hitMethodExit((MethodExitEvent)event);
209
eventCount++;
210
debugee.resume();
211
212
} else if (event instanceof VMDeathEvent) {
213
exit = true;
214
break;
215
} else if (event instanceof VMDisconnectEvent) {
216
exit = true;
217
break;
218
} // if
219
} // while
220
} // if
221
tmp = System.currentTimeMillis();
222
delta = tmp - begin;
223
totalTime -= delta;
224
begin = tmp;
225
}
226
227
if (eventCount != expectedEventCount) {
228
if (totalTime <= 0) {
229
complain("out of wait time...");
230
}
231
complain("expecting " + expectedEventCount
232
+ " events, but "
233
+ eventCount + " events arrived.");
234
exitStatus = Consts.TEST_FAILED;
235
}
236
237
display("=============");
238
display("TEST FINISHES\n");
239
}
240
241
private void redefineDebugee() {
242
Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;
243
boolean alreadyComplained = false;
244
mapBytes = mapClassToBytes(newClassFile);
245
try {
246
debugee.VM().redefineClasses(mapBytes);
247
} catch (Exception e) {
248
throw new Failure(UNEXPECTED_STRING + e);
249
}
250
}
251
252
private void popFrames(ThreadReference thread) {
253
try {
254
StackFrame frame = thread.frame(0);
255
thread.popFrames(frame);
256
} catch (IncompatibleThreadStateException e) {
257
throw new Failure(UNEXPECTED_STRING + e);
258
}
259
}
260
261
private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {
262
display("class-file\t:" + fileName);
263
File fileToBeRedefined = new File(classDir + File.separator + fileName);
264
int fileToRedefineLength = (int )fileToBeRedefined.length();
265
byte[] arrayToRedefine = new byte[fileToRedefineLength];
266
267
FileInputStream inputFile;
268
try {
269
inputFile = new FileInputStream(fileToBeRedefined);
270
} catch (FileNotFoundException e) {
271
throw new Failure(UNEXPECTED_STRING + e);
272
}
273
274
try {
275
inputFile.read(arrayToRedefine);
276
inputFile.close();
277
} catch (IOException e) {
278
throw new Failure(UNEXPECTED_STRING + e);
279
}
280
HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();
281
mapForClass.put(debugeeClass, arrayToRedefine);
282
return mapForClass;
283
}
284
285
private MethodExitRequest createMethodExitRequest(ReferenceType refType) {
286
EventRequestManager evm = debugee.getEventRequestManager();
287
MethodExitRequest request = evm.createMethodExitRequest();
288
request.addClassFilter(refType);
289
request.enable();
290
return request;
291
}
292
293
private void hitBreakpoint(BreakpointEvent event) {
294
locationInfo(event);
295
if (event.location().lineNumber() != tc02x001a.checkLastLine) {
296
complain("BreakpointEvent steps to line " + event.location().lineNumber()
297
+ ", expected line number is "
298
+ tc02x001a.checkLastLine);
299
exitStatus = Consts.TEST_FAILED;
300
} else {
301
display("!!!BreakpointEvent steps to the expected line "
302
+ event.location().lineNumber() + "!!!");
303
}
304
display("");
305
}
306
307
private void hitMethodExit(MethodExitEvent event) {
308
309
locationInfo(event);
310
311
Method mthd = ((MethodExitEvent )event).method();
312
if (mthd.name().compareTo(tc02x001a.brkpMethodName) == 0) {
313
Field fld = debugeeClass.fieldByName(tc02x001a.fieldToCheckName);
314
Value val = debugeeClass.getValue(fld);
315
if (((IntegerValue )val).value() != tc02x001a.CHANGED_VALUE) {
316
complain("Unexpected: new code is not actual "
317
+ "after resetting frames");
318
complain("Unexpected value of checked field: "
319
+ val);
320
exitStatus = Consts.TEST_FAILED;
321
} else {
322
display("!!!Expected: new code is actual "
323
+ "after resetting frames!!!");
324
}
325
}
326
display("");
327
}
328
329
private void locationInfo(LocatableEvent event) {
330
display("event info:");
331
display("\tthread\t- " + event.thread().name());
332
try {
333
display("\tsource\t- " + event.location().sourceName());
334
} catch (AbsentInformationException e) {
335
}
336
display("\tmethod\t- " + event.location().method().name());
337
display("\tline\t- " + event.location().lineNumber());
338
}
339
}
340
341