Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/Method/IsObsolete/isobsolete002.java
41161 views
1
/*
2
* Copyright (c) 2001, 2020, 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.jdwp.Method.IsObsolete;
25
26
import java.io.*;
27
28
import nsk.share.*;
29
import nsk.share.jpda.*;
30
import nsk.share.jdwp.*;
31
32
/**
33
* Test for JDWP command: Method.IsObsolete.
34
*
35
* See isobsolete002.README for description of test execution.
36
*
37
* This class represents debugger part of the test.
38
* Test is executed by invoking method runIt().
39
* JDWP command is tested in the method testCommand().
40
*
41
* @see #runIt()
42
* @see #testCommand()
43
*/
44
public class isobsolete002 {
45
46
// exit status constants
47
static final int JCK_STATUS_BASE = 95;
48
static final int PASSED = 0;
49
static final int FAILED = 2;
50
51
// VM capability constatnts
52
static final int VM_CAPABILITY_NUMBER = JDWP.Capability.CAN_REDEFINE_CLASSES;
53
static final String VM_CAPABILITY_NAME = "canRedefineClasses";
54
55
// package and classes names
56
static final String PACKAGE_NAME = "nsk.jdwp.Method.IsObsolete";
57
static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "isobsolete002";
58
static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
59
60
// tested JDWP command
61
static final String JDWP_COMMAND_NAME = "Method.IsObsolete";
62
static final int JDWP_COMMAND_ID = JDWP.Command.Method.IsObsolete;
63
64
// tested class name and signature
65
static final String TESTED_CLASS_NAME = TEST_CLASS_NAME + "b";
66
static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";
67
68
// tested method name
69
static final String TESTED_METHOD_NAME = "testedMethod";
70
static final int BREAKPOINT_LINE = isobsolete002a.BREAKPOINT_LINE;
71
72
// filename for redefined class
73
static final String REDEFINED_CLASS_FILE_NAME = "bin" + File.separator + "newclass"
74
+ File.separator + PACKAGE_NAME.replace('.', File.separatorChar)
75
+ File.separator + "isobsolete002b.class";
76
77
// usual scaffold objects
78
ArgumentHandler argumentHandler = null;
79
Log log = null;
80
Binder binder = null;
81
Debugee debugee = null;
82
Transport transport = null;
83
int waitTime = 0; // minutes
84
long timeout = 0; // milliseconds
85
String testDir = null;
86
boolean dead = false;
87
boolean success = true;
88
89
// data obtained from debuggee
90
long testedClassID = 0;
91
long testedMethodID = 0;
92
93
// -------------------------------------------------------------------
94
95
/**
96
* Start test from command line.
97
*/
98
public static void main (String argv[]) {
99
System.exit(run(argv,System.out) + JCK_STATUS_BASE);
100
}
101
102
/**
103
* Start JCK-compilant test.
104
*/
105
public static int run(String argv[], PrintStream out) {
106
return new isobsolete002().runIt(argv, out);
107
}
108
109
// -------------------------------------------------------------------
110
111
/**
112
* Perform test execution.
113
*/
114
public int runIt(String argv[], PrintStream out) {
115
116
// make log for debugger messages
117
argumentHandler = new ArgumentHandler(argv);
118
log = new Log(out, argumentHandler);
119
waitTime = argumentHandler.getWaitTime(); // minutes
120
timeout = waitTime * 60 * 1000; // milliseconds
121
122
// get testDir as first positional parameter
123
String args[] = argumentHandler.getArguments();
124
if (args.length < 1) {
125
log.complain("Test dir required as the first positional argument");
126
return FAILED;
127
}
128
testDir = args[0];
129
130
// execute test and display results
131
try {
132
log.display("\n>>> Loading redefined class \n");
133
134
// launch debuggee
135
binder = new Binder(argumentHandler, log);
136
log.display("Launching debugee VM");
137
debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
138
transport = debugee.getTransport();
139
log.display(" ... debuggee launched");
140
141
// set timeout for debuggee responces
142
log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");
143
transport.setReadTimeout(timeout);
144
log.display(" ... timeout set");
145
146
// wait for VM_INIT event
147
log.display("Waiting for VM_INIT event");
148
debugee.waitForVMInit();
149
log.display(" ... VM_INIT event received");
150
151
// query debugee for VM-dependent ID sizes
152
log.display("Querying for IDSizes");
153
debugee.queryForIDSizes();
154
log.display(" ... size of VM-dependent types adjusted");
155
156
// check for VM capability
157
log.display("\n>>> Checking VM capability \n");
158
log.display("Getting new VM capability: " + VM_CAPABILITY_NAME);
159
boolean capable = debugee.getNewCapability(VM_CAPABILITY_NUMBER, VM_CAPABILITY_NAME);
160
log.display(" ... got VM capability: " + capable);
161
162
// exit as PASSED if this capability is not supported
163
if (!capable) {
164
out.println("TEST PASSED: unsupported VM capability: "
165
+ VM_CAPABILITY_NAME);
166
return PASSED;
167
}
168
169
// prepare debuggee for testing and obtain required data
170
log.display("\n>>> Getting prepared for testing \n");
171
prepareForTest();
172
173
// test JDWP command
174
log.display("\n>>> Testing JDWP command \n");
175
testCommand(testedMethodID, TESTED_METHOD_NAME);
176
177
// finish debuggee
178
log.display("\n>> Finishing debuggee \n");
179
180
// resume debuggee after testing command
181
log.display("Resuming debuggee");
182
debugee.resume();
183
log.display(" ... debuggee resumed");
184
185
// wait for VM_DEATH event
186
log.display("Waiting for VM_DEATH event");
187
debugee.waitForVMDeath();
188
log.display(" ... VM_DEATH event received");
189
dead = true;
190
191
} catch (Failure e) {
192
log.complain("TEST FAILED: " + e.getMessage());
193
success = false;
194
} catch (Exception e) {
195
e.printStackTrace(out);
196
log.complain("Caught unexpected exception while running the test:\n\t" + e);
197
success = false;
198
} finally {
199
log.display("\n>>> Finishing test \n");
200
201
// disconnect debugee and wait for its exit
202
if (debugee != null) {
203
quitDebugee();
204
}
205
}
206
207
// check result
208
if (!success) {
209
log.complain("TEST FAILED");
210
return FAILED;
211
}
212
out.println("TEST PASSED");
213
return PASSED;
214
}
215
216
/**
217
* Get debuggee prepared for testing and obtain required data.
218
*/
219
void prepareForTest() {
220
// wait for debuggee and tested classes loaded on debuggee startup
221
log.display("Waiting for classes loaded:"
222
+ "\n\t" + TESTED_CLASS_NAME);
223
testedClassID = debugee.waitForClassLoaded(TESTED_CLASS_NAME,
224
JDWP.SuspendPolicy.ALL);
225
log.display(" ... class loaded with classID: " + testedClassID);
226
log.display("");
227
228
/*
229
// get tested methodID by names
230
log.display("Getting methodID for method name :" + TESTED_METHOD_NAME);
231
testedMethodID = debugee.getMethodID(testedClassID, TESTED_METHOD_NAME, true);
232
log.display(" ... got methodID: " + testedMethodID);
233
log.display("");
234
*/
235
236
// wait for breakpoint reached
237
log.display("Waiting for breakpoint reached at: "
238
+ TESTED_METHOD_NAME + ":" + BREAKPOINT_LINE);
239
long threadID = debugee.waitForBreakpointReached(testedClassID,
240
TESTED_METHOD_NAME,
241
BREAKPOINT_LINE,
242
JDWP.SuspendPolicy.ALL);
243
log.display(" ... breakpoint reached with threadID: " + threadID);
244
log.display("");
245
246
// load class file for redefined class
247
log.display("Loading bytecode of redefined class from file: " +
248
REDEFINED_CLASS_FILE_NAME);
249
byte[] classBytes = loadClassBytes(REDEFINED_CLASS_FILE_NAME, testDir);
250
log.display(" ... loaded bytes: " + classBytes.length);
251
252
// redefine class
253
log.display("Redefine class by classID: " + testedClassID);
254
redefineClass(testedClassID, classBytes);
255
log.display(" ... class redefined");
256
log.display("");
257
258
// get top frameID of the thread
259
log.display("Getting top frameID of the threadID: " + threadID);
260
JDWP.Location location = queryTopFrameLocation(threadID);
261
log.display(" ... got location: " + location);
262
263
// get methodID of the top frameID
264
log.display("Getting methodID for the location :" + location);
265
testedMethodID = location.getMethodID();
266
log.display(" ... got methodID: " + testedMethodID);
267
log.display("");
268
269
}
270
271
/**
272
* Perform testing JDWP command for given methodID.
273
*/
274
void testCommand(long testedMethodID, String methodName) {
275
// create command packet and fill requred out data
276
log.display("Create command packet:");
277
log.display("Command: " + JDWP_COMMAND_NAME);
278
CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
279
log.display(" refTypeID: " + testedClassID);
280
command.addReferenceTypeID(testedClassID);
281
log.display(" methodID: " + testedMethodID);
282
command.addMethodID(testedMethodID);
283
command.setLength();
284
285
// send command packet to debugee
286
try {
287
log.display("Sending command packet:\n" + command);
288
transport.write(command);
289
} catch (IOException e) {
290
log.complain("Unable to send command packet for method " + methodName + ":\n\t" + e);
291
success = false;
292
return;
293
}
294
295
// receive reply packet from debugee
296
ReplyPacket reply = new ReplyPacket();
297
try {
298
log.display("Waiting for reply packet");
299
transport.read(reply);
300
log.display(" ... reply packet received:\n" + reply);
301
} catch (IOException e) {
302
log.complain("Unable to read reply packet for method " + methodName + ":\n\t" + e);
303
success = false;
304
return;
305
}
306
307
// check reply packet header
308
try{
309
log.display("Checking header of reply packet");
310
reply.checkHeader(command.getPacketID());
311
log.display(" ... packet header is correct");
312
} catch (BoundException e) {
313
log.complain("Wrong header of reply packet for method " + methodName + ":\n\t"
314
+ e.getMessage());
315
success = false;
316
return;
317
}
318
319
// start parsing reply packet data
320
log.display("Parsing reply packet data:");
321
reply.resetPosition();
322
323
// extract boolean isObsolete
324
byte isObsolete = 0;
325
try {
326
isObsolete = reply.getByte();
327
log.display(" isObsolete: " + isObsolete);
328
} catch (BoundException e) {
329
log.complain("Unable to extract isObsolete value from reply packet for method "
330
+ methodName + ":\n\t" + e.getMessage());
331
success = false;
332
}
333
334
// check isObsolete
335
if (isObsolete == 0) {
336
log.complain("Unexpected isObsolete value for method "
337
+ methodName + ": " + isObsolete + " (expected: not " + 0 + ")");
338
success = false;
339
}
340
341
// check for extra data in reply packet
342
if (!reply.isParsed()) {
343
log.complain("Extra trailing bytes in reply packet for "
344
+ methodName + " method at: " + reply.offsetString());
345
success = false;
346
}
347
348
log.display(" ... packed data parsed");
349
}
350
351
/**
352
* Redefine class bytes for given classID.
353
*/
354
void redefineClass(long classID, byte[] classBytes) {
355
int length = classBytes.length;
356
357
CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.RedefineClasses);
358
command.addInt(1);
359
command.addReferenceTypeID(classID);
360
command.addInt(length);
361
command.addBytes(classBytes, 0, length);
362
363
// receive reply packet from debugee
364
ReplyPacket reply = debugee.receiveReplyFor(command, "VirtualMachine.RedefineClasses");
365
}
366
367
/**
368
* Query debuggee VM for top frameID of the thread.
369
*/
370
JDWP.Location queryTopFrameLocation(long threadID) {
371
String error = "Error occured while getting top frameID for threadID: " + threadID;
372
373
CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Frames);
374
command.addObjectID(threadID);
375
command.addInt(0);
376
command.addInt(1);
377
command.setLength();
378
379
ReplyPacket reply = debugee.receiveReplyFor(command, "ThreadReference.Frames");
380
reply.resetPosition();
381
382
// extract number of frames
383
int frames = 0;
384
try {
385
frames = reply.getInt();
386
} catch (BoundException e) {
387
log.complain("Unable to extract number of frames from reply packet:\n\t"
388
+ e.getMessage());
389
throw new Failure(error);
390
}
391
392
// check frames count
393
if (frames != 1) {
394
log.complain("Unexpected number of frames returned: "
395
+ frames + " (expected: " + 1 + ")");
396
throw new Failure(error);
397
}
398
399
// extract frame ID
400
long frameID = 0;
401
try {
402
frameID = reply.getFrameID();
403
} catch (BoundException e) {
404
log.complain("Unable to extract top frameID from reply packet:\n\t"
405
+ e.getMessage());
406
throw new Failure(error);
407
}
408
409
// extract frame location
410
JDWP.Location location = null;
411
try {
412
location = reply.getLocation();
413
} catch (BoundException e) {
414
log.complain("Unable to extract location for top frame from reply packet:\n\t"
415
+ e.getMessage());
416
throw new Failure(error);
417
}
418
419
return location;
420
}
421
422
423
/**
424
* Load class bytes form the given file.
425
*/
426
byte[] loadClassBytes(String fileName, String dirName) {
427
String fileSep = System.getProperty("file.separator");
428
String filePath = dirName + fileSep + fileName;
429
430
String error = "Unable to read bytes from class file:\n\t" + filePath;
431
432
int length = 0;
433
byte bytes[] = null;
434
try {
435
File file = new File(filePath);
436
length = (int)file.length();
437
FileInputStream is = new FileInputStream(file);
438
bytes = new byte[length];
439
int number = is.read(bytes);
440
if (number < 0) {
441
log.complain("EOF reached while reading bytes from file");
442
throw new Failure(error);
443
} else if (number != length) {
444
log.complain("Unexpected number of bytes red from file: " + number
445
+ " (expected: " + length + ")");
446
throw new Failure(error);
447
}
448
is.close();
449
} catch ( IOException e ) {
450
log.complain("Caught IOException while reading bytes from file:\n\t" + e);
451
throw new Failure(error);
452
}
453
return bytes;
454
}
455
456
/**
457
* Disconnect debuggee and wait for it exited.
458
*/
459
void quitDebugee() {
460
if (debugee == null)
461
return;
462
463
// disconnect debugee
464
if (!dead) {
465
try {
466
log.display("Disconnecting debuggee");
467
debugee.dispose();
468
log.display(" ... debuggee disconnected");
469
} catch (Failure e) {
470
log.display("Failed to finally disconnect debuggee:\n\t"
471
+ e.getMessage());
472
}
473
}
474
475
// wait for debugee exited
476
log.display("Waiting for debuggee exit");
477
int code = debugee.waitFor();
478
log.display(" ... debuggee exited with exit code: " + code);
479
480
// analize debugee exit status code
481
if (code != JCK_STATUS_BASE + PASSED) {
482
log.complain("Debuggee FAILED with exit code: " + code);
483
success = false;
484
}
485
}
486
}
487
488