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