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/tc01x001.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: TC1
42
* Description: After 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 next line after point of
47
* execution: System.err.println("foo");
48
* 4.Smart Swap
49
* 5.Resume
50
* X. Prints out "foo" after 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
* a new line into <code>ethod_A</code> and resumes debugee. It is expected
60
* the added line will be not actual after the redefining according to
61
* redefineClasses spec:
62
* "The redefined method will be used on new invokes. If resetting these
63
* frames is desired, use <code>ThreadReference.popFrames()</code> with
64
* <code>Method.isObsolete()</code>."
65
*/
66
67
public class tc01x001 {
68
69
public final static String UNEXPECTED_STRING = "***Unexpected exception ";
70
71
private final static String prefix = "nsk.jdi.BScenarios.hotswap.";
72
private final static String debuggerName = prefix + "tc01x001";
73
private final static String debugeeName = debuggerName + "a";
74
75
private final static String newClassFile = "newclass" + File.separator
76
+ debugeeName.replace('.',File.separatorChar)
77
+ ".class";
78
79
private static int exitStatus;
80
private static Log log;
81
private static Debugee debugee;
82
private static long waitTime;
83
private static String classDir;
84
85
private int expectedEventCount = 5;
86
87
private ReferenceType debugeeClass;
88
89
private static void display(String msg) {
90
log.display(msg);
91
}
92
93
private static void complain(String msg) {
94
log.complain("debugger FAILURE> " + msg + "\n");
95
}
96
97
public static void main(String argv[]) {
98
System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out));
99
}
100
101
public static int run(String argv[], PrintStream out) {
102
103
exitStatus = Consts.TEST_PASSED;
104
105
tc01x001 thisTest = new tc01x001();
106
107
ArgumentHandler argHandler = new ArgumentHandler(argv);
108
log = new Log(out, argHandler);
109
110
classDir = argv[0];
111
waitTime = argHandler.getWaitTime() * 60000;
112
113
Binder binder = new Binder(argHandler, log);
114
debugee = binder.bindToDebugee(debugeeName);
115
116
try {
117
thisTest.execTest();
118
} catch (Throwable e) {
119
exitStatus = Consts.TEST_FAILED;
120
e.printStackTrace();
121
} finally {
122
debugee.endDebugee();
123
}
124
display("Test finished. exitStatus = " + exitStatus);
125
126
return exitStatus;
127
}
128
129
private void execTest() throws Failure {
130
131
if (!debugee.VM().canRedefineClasses()) {
132
display("\n>>>canRedefineClasses() is false<<< test canceled.\n");
133
return;
134
}
135
136
display("\nTEST BEGINS");
137
display("===========");
138
139
EventSet eventSet = null;
140
EventIterator eventIterator = null;
141
Event event;
142
long totalTime = waitTime;
143
long tmp, begin = System.currentTimeMillis(),
144
delta = 0;
145
boolean exit = false;
146
147
int eventCount = 0;
148
EventRequestManager evm = debugee.getEventRequestManager();
149
ClassPrepareRequest req = evm.createClassPrepareRequest();
150
req.addClassFilter(debugeeName);
151
req.enable();
152
debugee.resume();
153
154
while (totalTime > 0 && !exit) {
155
if (eventIterator == null || !eventIterator.hasNext()) {
156
try {
157
eventSet = debugee.VM().eventQueue().remove(totalTime);
158
} catch (InterruptedException e) {
159
new Failure(e);
160
}
161
if (eventSet != null) {
162
eventIterator = eventSet.eventIterator();
163
} else {
164
eventIterator = null;
165
}
166
}
167
if (eventIterator != null) {
168
while (eventIterator.hasNext()) {
169
event = eventIterator.nextEvent();
170
// display("\n event ===>>> " + event);
171
172
if (event instanceof ClassPrepareEvent) {
173
display("\n event ===>>> " + event);
174
debugeeClass = ((ClassPrepareEvent )event).referenceType();
175
display("Tested class\t:" + debugeeClass.name());
176
debugee.setBreakpoint(debugeeClass,
177
tc01x001a.brkpMethodName,
178
tc01x001a.brkpLineNumber);
179
180
debugee.resume();
181
eventCount++;
182
183
} else if (event instanceof BreakpointEvent) {
184
display("\n event ===>>> " + event);
185
display("redefining...");
186
redefineDebugee();
187
188
createMethodExitRequest(debugeeClass);
189
debugee.resume();
190
eventCount++;
191
192
} else if (event instanceof MethodExitEvent) {
193
display("\n event ===>>> " + event);
194
Method mthd = ((MethodExitEvent )event).method();
195
eventCount++;
196
display("exiting from \"" + mthd.name() + "\" method");
197
if (mthd.isObsolete()) {
198
Field fld = debugeeClass.fieldByName(tc01x001a.fieldToCheckName);
199
Value val = debugeeClass.getValue(fld);
200
if (((IntegerValue )val).value() == tc01x001a.CHANGED_VALUE) {
201
complain("Unexpected: new code is actual "
202
+ "without resetting frames");
203
complain("Unexpected value of checked field: "
204
+ val);
205
exitStatus = Consts.TEST_FAILED;
206
} else {
207
display("!!!Expected: new code is not actual "
208
+ "without resetting frames!!!");
209
}
210
}
211
debugee.resume();
212
213
} else if (event instanceof VMDeathEvent) {
214
exit = true;
215
break;
216
} else if (event instanceof VMDisconnectEvent) {
217
exit = true;
218
break;
219
} // if
220
} // while
221
} // if
222
tmp = System.currentTimeMillis();
223
delta = tmp - begin;
224
totalTime -= delta;
225
begin = tmp;
226
}
227
228
if (eventCount != expectedEventCount) {
229
if (totalTime <= 0) {
230
complain("out of wait time...");
231
}
232
complain("expecting " + expectedEventCount
233
+ " events, but "
234
+ eventCount + " events arrived.");
235
exitStatus = Consts.TEST_FAILED;
236
}
237
238
display("=============");
239
display("TEST FINISHES\n");
240
}
241
242
private void redefineDebugee() {
243
Map<? extends com.sun.jdi.ReferenceType,byte[]> mapBytes;
244
boolean alreadyComplained = false;
245
mapBytes = mapClassToBytes(newClassFile);
246
try {
247
debugee.VM().redefineClasses(mapBytes);
248
} catch (Exception e) {
249
throw new Failure(UNEXPECTED_STRING + e);
250
}
251
}
252
253
private Map<? extends com.sun.jdi.ReferenceType,byte[]> mapClassToBytes(String fileName) {
254
display("class-file\t:" + fileName);
255
File fileToBeRedefined = new File(classDir + File.separator + fileName);
256
int fileToRedefineLength = (int )fileToBeRedefined.length();
257
byte[] arrayToRedefine = new byte[fileToRedefineLength];
258
259
FileInputStream inputFile;
260
try {
261
inputFile = new FileInputStream(fileToBeRedefined);
262
} catch (FileNotFoundException e) {
263
throw new Failure(UNEXPECTED_STRING + e);
264
}
265
266
try {
267
inputFile.read(arrayToRedefine);
268
inputFile.close();
269
} catch (IOException e) {
270
throw new Failure(UNEXPECTED_STRING + e);
271
}
272
HashMap<com.sun.jdi.ReferenceType,byte[]> mapForClass = new HashMap<com.sun.jdi.ReferenceType,byte[]>();
273
mapForClass.put(debugeeClass, arrayToRedefine);
274
return mapForClass;
275
}
276
277
private MethodExitRequest createMethodExitRequest(ReferenceType refType) {
278
EventRequestManager evm = debugee.getEventRequestManager();
279
MethodExitRequest request = evm.createMethodExitRequest();
280
request.addClassFilter(refType);
281
request.enable();
282
return request;
283
}
284
}
285
286