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/tc03x001.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: TC3
42
* Description: After point of execution, different method
43
* Steps: 1.Set breakpoint at line 24 (call from a()
44
* to b())
45
* 2.Debug Main
46
* 3.Insert as first line in b():
47
* System.err.println("foo");
48
* 4.Smart Swap
49
* 5.Resume
50
* X. Prints out "foo" before printing the
51
* numbers
52
* The description was drown up according to steps under JBuilder.
53
*
54
* Of course, the test has own line numbers and method/class names and
55
* works as follow:
56
* When the test is starting debugee, debugger sets breakpoint at
57
* the 37th line (method <code>method_A</code>).
58
* After the breakpoint is reached, debugger redefines debugee adding
59
* first line into <code>method_B</code> and resumes debugee. It is expected the
60
* new code will be actual before calling <code>method_C</code>
61
*/
62
63
public class tc03x001 {
64
65
public final static String UNEXPECTED_STRING = "***Unexpected exception ";
66
67
private final static String prefix = "nsk.jdi.BScenarios.hotswap.";
68
private final static String debuggerName = prefix + "tc03x001";
69
private final static String debugeeName = debuggerName + "a";
70
71
private final static String newClassFile = "newclass" + File.separator
72
+ debugeeName.replace('.',File.separatorChar)
73
+ ".class";
74
75
private static int exitStatus;
76
private static Log log;
77
private static Debugee debugee;
78
private static long waitTime;
79
private static String classDir;
80
81
private int expectedEventCount = 4;
82
private static final String methodNameToCheck = "method_C";
83
84
private ReferenceType debugeeClass;
85
86
private static void display(String msg) {
87
log.display(msg);
88
}
89
90
private static void complain(String msg) {
91
log.complain("debugger FAILURE> " + msg + "\n");
92
}
93
94
public static void main(String argv[]) {
95
System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
96
}
97
98
public static int run(String argv[], PrintStream out) {
99
100
exitStatus = Consts.TEST_PASSED;
101
102
tc03x001 thisTest = new tc03x001();
103
104
ArgumentHandler argHandler = new ArgumentHandler(argv);
105
log = new Log(out, argHandler);
106
107
classDir = argv[0];
108
waitTime = argHandler.getWaitTime() * 60000;
109
110
Binder binder = new Binder(argHandler, log);
111
debugee = binder.bindToDebugee(debugeeName);
112
113
try {
114
thisTest.execTest();
115
} catch (Throwable e) {
116
exitStatus = Consts.TEST_FAILED;
117
e.printStackTrace();
118
} finally {
119
debugee.endDebugee();
120
}
121
display("Test finished. exitStatus = " + exitStatus);
122
123
return exitStatus;
124
}
125
126
private void execTest() throws Failure {
127
128
if (!debugee.VM().canRedefineClasses()) {
129
display("\n>>>canRedefineClasses() is false<<< test canceled.\n");
130
return;
131
}
132
133
display("\nTEST BEGINS");
134
display("===========");
135
136
EventSet eventSet = null;
137
EventIterator eventIterator = null;
138
Event event;
139
long totalTime = waitTime;
140
long tmp, begin = System.currentTimeMillis(),
141
delta = 0;
142
boolean exit = false;
143
144
int eventCount = 0;
145
EventRequestManager evm = debugee.getEventRequestManager();
146
ClassPrepareRequest req = evm.createClassPrepareRequest();
147
req.addClassFilter(debugeeName);
148
req.enable();
149
debugee.resume();
150
151
while (totalTime > 0 && !exit) {
152
if (eventIterator == null || !eventIterator.hasNext()) {
153
try {
154
eventSet = debugee.VM().eventQueue().remove(totalTime);
155
} catch (InterruptedException e) {
156
new Failure(e);
157
}
158
if (eventSet != null) {
159
eventIterator = eventSet.eventIterator();
160
} else {
161
eventIterator = null;
162
}
163
}
164
if (eventIterator != null) {
165
while (eventIterator.hasNext()) {
166
event = eventIterator.nextEvent();
167
// display("\n event ===>>> " + event);
168
169
if (event instanceof ClassPrepareEvent) {
170
display("\n event ===>>> " + event);
171
debugeeClass = ((ClassPrepareEvent )event).referenceType();
172
display("Tested class\t:" + debugeeClass.name());
173
debugee.setBreakpoint(debugeeClass,
174
tc03x001a.brkpMethodName,
175
tc03x001a.brkpLineNumber);
176
177
debugee.resume();
178
eventCount++;
179
180
} else if (event instanceof BreakpointEvent) {
181
display("\n event ===>>> " + event);
182
display("redefining...");
183
redefineDebugee();
184
185
createMethodEntryRequest(debugeeClass);
186
debugee.resume();
187
eventCount++;
188
189
} else if (event instanceof MethodEntryEvent) {
190
display("\n event ===>>> " + event);
191
Method mthd = ((MethodEntryEvent )event).method();
192
eventCount++;
193
display("exiting from \"" + mthd.name() + "\" method");
194
if (mthd.name().compareTo(methodNameToCheck) == 0) {
195
Field fld = debugeeClass.fieldByName(tc03x001a.fieldToCheckName);
196
Value val = debugeeClass.getValue(fld);
197
if (((IntegerValue )val).value() == tc03x001a.CHANGED_VALUE) {
198
display("!!!Expected: new code is actual "
199
+ "before calling " + methodNameToCheck
200
+ "!!!");
201
} else {
202
complain("Unexpected: new code is not actual "
203
+ "before calling " + methodNameToCheck);
204
complain("Unexpected value of checked field: "
205
+ val);
206
exitStatus = Consts.TEST_FAILED;
207
}
208
}
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<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 Map<com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {
252
display("class-file\t:" + fileName);
253
File fileToBeRedefined = new File(classDir + File.separator + fileName);
254
int fileToRedefineLength = (int )fileToBeRedefined.length();
255
byte[] arrayToRedefine = new byte[fileToRedefineLength];
256
257
FileInputStream inputFile;
258
try {
259
inputFile = new FileInputStream(fileToBeRedefined);
260
} catch (FileNotFoundException e) {
261
throw new Failure(UNEXPECTED_STRING + e);
262
}
263
264
try {
265
inputFile.read(arrayToRedefine);
266
inputFile.close();
267
} catch (IOException e) {
268
throw new Failure(UNEXPECTED_STRING + e);
269
}
270
HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();
271
mapForClass.put(debugeeClass, arrayToRedefine);
272
return mapForClass;
273
}
274
275
private MethodEntryRequest createMethodEntryRequest(ReferenceType refType) {
276
EventRequestManager evm = debugee.getEventRequestManager();
277
MethodEntryRequest request = evm.createMethodEntryRequest();
278
request.addClassFilter(refType);
279
request.enable();
280
return request;
281
}
282
}
283
284