|
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.LineTable; |
|
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.LineTable. |
|
34 * |
|
35 * See linetable001.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 linetable001 { |
|
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 // communication signals constants |
|
52 static final String READY = "ready"; |
|
53 static final String QUIT = "quit"; |
|
54 |
|
55 // package and classes names constants |
|
56 static final String PACKAGE_NAME = "nsk.jdwp.Method.LineTable"; |
|
57 static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "linetable001"; |
|
58 static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a"; |
|
59 |
|
60 // tested JDWP command constants |
|
61 static final String JDWP_COMMAND_NAME = "Method.LineTable"; |
|
62 static final int JDWP_COMMAND_ID = JDWP.Command.Method.LineTable; |
|
63 |
|
64 // tested class name and signature constants |
|
65 static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass"; |
|
66 static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";"; |
|
67 |
|
68 // tested method name constant |
|
69 static final String TESTED_METHOD_NAME = "testedMethod"; |
|
70 |
|
71 // expected values for bound line numbers |
|
72 static final int FIRST_LINE_NUMBER = linetable001a.FIRST_LINE_NUMBER; |
|
73 static final int LAST_LINE_NUMBER = linetable001a.LAST_LINE_NUMBER; |
|
74 |
|
75 // usual scaffold objects |
|
76 ArgumentHandler argumentHandler = null; |
|
77 Log log = null; |
|
78 Binder binder = null; |
|
79 Debugee debugee = null; |
|
80 Transport transport = null; |
|
81 IOPipe pipe = null; |
|
82 |
|
83 // test passed or not |
|
84 boolean success = true; |
|
85 |
|
86 // ------------------------------------------------------------------- |
|
87 |
|
88 /** |
|
89 * Start test from command line. |
|
90 */ |
|
91 public static void main (String argv[]) { |
|
92 System.exit(run(argv,System.out) + JCK_STATUS_BASE); |
|
93 } |
|
94 |
|
95 /** |
|
96 * Start JCK-compilant test. |
|
97 */ |
|
98 public static int run(String argv[], PrintStream out) { |
|
99 return new linetable001().runIt(argv, out); |
|
100 } |
|
101 |
|
102 // ------------------------------------------------------------------- |
|
103 |
|
104 /** |
|
105 * Perform test execution. |
|
106 */ |
|
107 public int runIt(String argv[], PrintStream out) { |
|
108 |
|
109 // make log for debugger messages |
|
110 argumentHandler = new ArgumentHandler(argv); |
|
111 log = new Log(out, argumentHandler); |
|
112 |
|
113 // execute test and display results |
|
114 try { |
|
115 log.display("\n>>> Preparing debugee for testing \n"); |
|
116 |
|
117 // launch debuggee |
|
118 binder = new Binder(argumentHandler, log); |
|
119 log.display("Launching debugee"); |
|
120 debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME); |
|
121 transport = debugee.getTransport(); |
|
122 pipe = debugee.createIOPipe(); |
|
123 |
|
124 // make debuggee ready for testing |
|
125 prepareDebugee(); |
|
126 |
|
127 // work with prepared debuggee |
|
128 try { |
|
129 log.display("\n>>> Obtaining requred data from debugee \n"); |
|
130 |
|
131 // query debuggee for classID of tested class |
|
132 log.display("Getting classID by signature:\n" |
|
133 + " " + TESTED_CLASS_SIGNATURE); |
|
134 long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE); |
|
135 log.display(" got classID: " + classID); |
|
136 |
|
137 // query debuggee for methodID of tested method (declared in the class) |
|
138 log.display("Getting methodID by name: " + TESTED_METHOD_NAME); |
|
139 long methodID = debugee.getMethodID(classID, TESTED_METHOD_NAME, true); |
|
140 log.display(" got methodID: " + methodID); |
|
141 |
|
142 // perform testing JDWP command |
|
143 log.display("\n>>> Testing JDWP command \n"); |
|
144 testCommand(classID, methodID); |
|
145 |
|
146 } finally { |
|
147 // quit debugee |
|
148 log.display("\n>>> Finishing test \n"); |
|
149 quitDebugee(); |
|
150 } |
|
151 |
|
152 } catch (Failure e) { |
|
153 log.complain("TEST FAILED: " + e.getMessage()); |
|
154 success = false; |
|
155 } catch (Exception e) { |
|
156 e.printStackTrace(out); |
|
157 log.complain("Caught unexpected exception while running the test:\n\t" + e); |
|
158 success = false; |
|
159 } |
|
160 |
|
161 if (!success) { |
|
162 log.complain("TEST FAILED"); |
|
163 return FAILED; |
|
164 } |
|
165 |
|
166 out.println("TEST PASSED"); |
|
167 return PASSED; |
|
168 |
|
169 } |
|
170 |
|
171 /** |
|
172 * Prepare debugee for testing and waiting for ready signal. |
|
173 */ |
|
174 void prepareDebugee() { |
|
175 // wait for VM_INIT event from debugee |
|
176 log.display("Waiting for VM_INIT event"); |
|
177 debugee.waitForVMInit(); |
|
178 |
|
179 // query debugee for VM-dependent ID sizes |
|
180 log.display("Querying for IDSizes"); |
|
181 debugee.queryForIDSizes(); |
|
182 |
|
183 // resume initially suspended debugee |
|
184 log.display("Resuming debugee VM"); |
|
185 debugee.resume(); |
|
186 |
|
187 // wait for READY signal from debugee |
|
188 log.display("Waiting for signal from debugee: " + READY); |
|
189 String signal = pipe.readln(); |
|
190 log.display("Received signal from debugee: " + signal); |
|
191 if (! signal.equals(READY)) { |
|
192 throw new TestBug("Unexpected signal received from debugee: " + signal |
|
193 + " (expected: " + READY + ")"); |
|
194 } |
|
195 } |
|
196 |
|
197 /** |
|
198 * Sending debugee signal to quit and waiting for it exits. |
|
199 */ |
|
200 void quitDebugee() { |
|
201 // send debugee signal to quit |
|
202 log.display("Sending signal to debugee: " + QUIT); |
|
203 pipe.println(QUIT); |
|
204 |
|
205 // wait for debugee exits |
|
206 log.display("Waiting for debugee exits"); |
|
207 int code = debugee.waitFor(); |
|
208 |
|
209 // analize debugee exit status code |
|
210 if (code == JCK_STATUS_BASE + PASSED) { |
|
211 log.display("Debugee PASSED with exit code: " + code); |
|
212 } else { |
|
213 log.complain("Debugee FAILED with exit code: " + code); |
|
214 success = false; |
|
215 } |
|
216 } |
|
217 |
|
218 /** |
|
219 * Perform testing JDWP command for specified TypeID. |
|
220 */ |
|
221 void testCommand(long classID, long methodID) { |
|
222 // create command packet and fill requred out data |
|
223 log.display("Create command packet:"); |
|
224 log.display("Command: " + JDWP_COMMAND_NAME); |
|
225 CommandPacket command = new CommandPacket(JDWP_COMMAND_ID); |
|
226 log.display(" referenceTypeID: " + classID); |
|
227 command.addReferenceTypeID(classID); |
|
228 log.display(" methodID: " + methodID); |
|
229 command.addMethodID(methodID); |
|
230 command.setLength(); |
|
231 |
|
232 // send command packet to debugee |
|
233 try { |
|
234 log.display("Sending command packet:\n" + command); |
|
235 transport.write(command); |
|
236 } catch (IOException e) { |
|
237 log.complain("Unable to send command packet:\n\t" + e); |
|
238 success = false; |
|
239 return; |
|
240 } |
|
241 |
|
242 ReplyPacket reply = new ReplyPacket(); |
|
243 |
|
244 // receive reply packet from debugee |
|
245 try { |
|
246 log.display("Waiting for reply packet"); |
|
247 transport.read(reply); |
|
248 log.display("Reply packet received:\n" + reply); |
|
249 } catch (IOException e) { |
|
250 log.complain("Unable to read reply packet:\n\t" + e); |
|
251 success = false; |
|
252 return; |
|
253 } |
|
254 |
|
255 // check reply packet header |
|
256 try{ |
|
257 log.display("Checking reply packet header"); |
|
258 reply.checkHeader(command.getPacketID()); |
|
259 } catch (BoundException e) { |
|
260 log.complain("Bad header of reply packet:\n\t" + e.getMessage()); |
|
261 success = false; |
|
262 } |
|
263 |
|
264 // start parsing reply packet data |
|
265 log.display("Parsing reply packet:"); |
|
266 reply.resetPosition(); |
|
267 |
|
268 // extract and check reply data |
|
269 |
|
270 // extract start code index |
|
271 long start = 0; |
|
272 try { |
|
273 start = reply.getLong(); |
|
274 log.display(" start: " + start); |
|
275 } catch (BoundException e) { |
|
276 log.complain("Unable to extract start line index from reply packet:\n\t" |
|
277 + e.getMessage()); |
|
278 success = false; |
|
279 return; |
|
280 } |
|
281 |
|
282 // check that start code index is not negative |
|
283 if (start < 0) { |
|
284 log.complain("Negative value of start code index in reply packet: " + start); |
|
285 success = false; |
|
286 } |
|
287 |
|
288 // extract end code index |
|
289 long end = 0; |
|
290 try { |
|
291 end = reply.getLong(); |
|
292 log.display(" end: " + end); |
|
293 } catch (BoundException e) { |
|
294 log.complain("Unable to extract end line index from reply packet:\n\t" |
|
295 + e.getMessage()); |
|
296 success = false; |
|
297 return; |
|
298 } |
|
299 |
|
300 // check that end code index is not negative |
|
301 if (start < 0) { |
|
302 log.complain("Negative value of end code index in reply packet: " + end); |
|
303 success = false; |
|
304 } |
|
305 |
|
306 // check that start code is strongly less than end code index |
|
307 if (start > end) { |
|
308 log.complain("Start code index (" + start |
|
309 + ") is greater than end code index (" + end + ")"); |
|
310 success = false; |
|
311 } else if (start == end) { |
|
312 log.complain("Start code index (" + start |
|
313 + ") is equal to end code index (" + end + ")"); |
|
314 success = false; |
|
315 } |
|
316 |
|
317 // extract number of lines |
|
318 int lines = 0; |
|
319 try { |
|
320 lines = reply.getInt(); |
|
321 log.display(" lines: " + lines); |
|
322 } catch (BoundException e) { |
|
323 log.complain("Unable to extract number of lines from reply packet:\n\t" |
|
324 + e.getMessage()); |
|
325 success = false; |
|
326 return; |
|
327 } |
|
328 |
|
329 if (lines < 0) { |
|
330 log.complain("Negative number of lines in reply packet: " + lines); |
|
331 success = false; |
|
332 return; |
|
333 } |
|
334 |
|
335 if (lines == 0) { |
|
336 log.complain("Zero number of lines in reply packet: " + lines); |
|
337 success = false; |
|
338 return; |
|
339 } |
|
340 |
|
341 // extract and check each line attributes |
|
342 long lineCodeIndex = 0, prevLineCodeIndex = 0; |
|
343 int lineNumber = 0, prevLineNumber = 0; |
|
344 |
|
345 for (int i = 0; i < lines; i++) { |
|
346 log.display(" line #" + i + ":"); |
|
347 |
|
348 // extract code index of a line |
|
349 try { |
|
350 lineCodeIndex = reply.getLong(); |
|
351 log.display(" lineCodeIndex: " + lineCodeIndex); |
|
352 } catch (BoundException e) { |
|
353 log.complain("Unable to extract code index of line #" + i |
|
354 + " from reply packet:\n\t" |
|
355 + e.getMessage()); |
|
356 success = false; |
|
357 return; |
|
358 } |
|
359 |
|
360 // check that code index is between start and end values |
|
361 if (lineCodeIndex < start) { |
|
362 log.complain("Code index of line #" + i + " (" + lineCodeIndex |
|
363 + ") is less than start code index (" + start + ")"); |
|
364 success = false; |
|
365 } |
|
366 |
|
367 if (lineCodeIndex > end) { |
|
368 log.complain("Code index of line #" + i + " (" + lineCodeIndex |
|
369 + ") is greater than end code index (" + end + ")"); |
|
370 success = false; |
|
371 } |
|
372 |
|
373 // check that code index ot the first line is equal to start value |
|
374 if (i == 0) { |
|
375 if (lineCodeIndex != start) { |
|
376 log.complain("Code index of first line (" + lineCodeIndex |
|
377 + ") is not equal to start code index (" + start + ")"); |
|
378 success = false; |
|
379 } |
|
380 } |
|
381 |
|
382 // check that code index of a line is strongly greater than for previous line |
|
383 if (i > 0) { |
|
384 if (lineCodeIndex < prevLineCodeIndex) { |
|
385 log.complain("Code index of line #" + i + " (" + lineCodeIndex |
|
386 + ") is less than code index of previous line (" |
|
387 + prevLineCodeIndex + ")"); |
|
388 success = false; |
|
389 } else if (lineCodeIndex == prevLineCodeIndex) { |
|
390 log.complain("Code index of line #" + i + " (" + lineCodeIndex |
|
391 + ") is equal to code index of previous line (" |
|
392 + prevLineCodeIndex + ")"); |
|
393 success = false; |
|
394 } |
|
395 } |
|
396 |
|
397 // extract number of a line |
|
398 try { |
|
399 lineNumber = reply.getInt(); |
|
400 log.display(" lineNumber: " + lineNumber); |
|
401 } catch (BoundException e) { |
|
402 log.complain("Unable to extract number of line #" + i |
|
403 + " from reply packet:\n\t" |
|
404 + e.getMessage()); |
|
405 success = false; |
|
406 return; |
|
407 } |
|
408 |
|
409 // check that code index ot the line is not negative |
|
410 if (lineNumber < 0) { |
|
411 log.complain("Number of line #" + i + " (" + lineNumber |
|
412 + ") is negative"); |
|
413 success = false; |
|
414 } |
|
415 |
|
416 // check that code index ot the line is not less than expected |
|
417 if (lineNumber < FIRST_LINE_NUMBER) { |
|
418 log.complain("Number of line #" + i + " (" + lineNumber |
|
419 + ") is less than expected (" + FIRST_LINE_NUMBER + ")"); |
|
420 success = false; |
|
421 } |
|
422 |
|
423 // check that code index ot the line is not greater than expected |
|
424 if (lineNumber > LAST_LINE_NUMBER) { |
|
425 log.complain("Number of line #" + i + " (" + lineNumber |
|
426 + ") is greater than expected (" + LAST_LINE_NUMBER + ")"); |
|
427 success = false; |
|
428 } |
|
429 |
|
430 // check that line number follows directly to the number of previous line |
|
431 if (i > 0) { |
|
432 if (lineNumber < prevLineNumber) { |
|
433 log.complain("Number of line #" + i + " (" + lineCodeIndex |
|
434 + ") is less than number of previous line (" |
|
435 + prevLineNumber + ")"); |
|
436 success = false; |
|
437 } else if (lineNumber == prevLineNumber) { |
|
438 log.complain("Number of line #" + i + " (" + lineCodeIndex |
|
439 + ") is equal to number of previous line (" |
|
440 + prevLineNumber + ")"); |
|
441 success = false; |
|
442 } else if (lineNumber != prevLineNumber + 1) { |
|
443 log.complain("Number of line #" + i + " (" + lineCodeIndex |
|
444 + ") does not follows to number of previous line (" |
|
445 + prevLineNumber + ")"); |
|
446 success = false; |
|
447 } |
|
448 } |
|
449 |
|
450 // save values to use them as previous line attributes |
|
451 prevLineCodeIndex = lineCodeIndex; |
|
452 prevLineNumber = lineNumber; |
|
453 } |
|
454 |
|
455 // check for extra data in reply packet |
|
456 if (!reply.isParsed()) { |
|
457 log.complain("Extra trailing bytes found in reply packet at: " |
|
458 + reply.offsetString()); |
|
459 success = false; |
|
460 } |
|
461 } |
|
462 |
|
463 } |