|
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.ThreadReference.Suspend; |
|
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: ThreadReference.Suspend. |
|
34 * |
|
35 * See suspend001.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 suspend001 { |
|
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 ERROR = "error"; |
|
54 static final String QUIT = "quit"; |
|
55 |
|
56 // package and classes names constants |
|
57 static final String PACKAGE_NAME = "nsk.jdwp.ThreadReference.Suspend"; |
|
58 static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "suspend001"; |
|
59 static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a"; |
|
60 |
|
61 // tested JDWP command constants |
|
62 static final String JDWP_COMMAND_NAME = "ThreadReference.Suspend"; |
|
63 static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Suspend; |
|
64 |
|
65 // tested class name and signature constants |
|
66 static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass"; |
|
67 static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";"; |
|
68 |
|
69 // name of the tested thread and statioc field with thread value |
|
70 static final String TESTED_CLASS_FIELD_NAME = suspend001a.FIELD_NAME; |
|
71 static final String TESTED_THREAD_NAME = suspend001a.THREAD_NAME; |
|
72 |
|
73 // usual scaffold objects |
|
74 ArgumentHandler argumentHandler = null; |
|
75 Log log = null; |
|
76 Binder binder = null; |
|
77 Debugee debugee = null; |
|
78 Transport transport = null; |
|
79 IOPipe pipe = null; |
|
80 |
|
81 // test passed or not |
|
82 boolean success = true; |
|
83 |
|
84 // ------------------------------------------------------------------- |
|
85 |
|
86 /** |
|
87 * Start test from command line. |
|
88 */ |
|
89 public static void main (String argv[]) { |
|
90 System.exit(run(argv,System.out) + JCK_STATUS_BASE); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Start JCK-compilant test. |
|
95 */ |
|
96 public static int run(String argv[], PrintStream out) { |
|
97 return new suspend001().runIt(argv, out); |
|
98 } |
|
99 |
|
100 // ------------------------------------------------------------------- |
|
101 |
|
102 /** |
|
103 * Perform test execution. |
|
104 */ |
|
105 public int runIt(String argv[], PrintStream out) { |
|
106 |
|
107 // make log for debugger messages |
|
108 argumentHandler = new ArgumentHandler(argv); |
|
109 log = new Log(out, argumentHandler); |
|
110 |
|
111 // execute test and display results |
|
112 try { |
|
113 log.display("\n>>> Preparing debugee for testing \n"); |
|
114 |
|
115 // launch debuggee |
|
116 binder = new Binder(argumentHandler, log); |
|
117 log.display("Launching debugee"); |
|
118 debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME); |
|
119 transport = debugee.getTransport(); |
|
120 pipe = debugee.createIOPipe(); |
|
121 |
|
122 // make debuggee ready for testing |
|
123 prepareDebugee(); |
|
124 |
|
125 long threadID = 0; |
|
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 threadID value from a static field |
|
138 log.display("Getting threadID value from static field: " |
|
139 + TESTED_CLASS_FIELD_NAME); |
|
140 threadID = queryThreadID(classID, TESTED_CLASS_FIELD_NAME); |
|
141 log.display(" got threadID: " + threadID); |
|
142 |
|
143 // perform testing JDWP command |
|
144 log.display("\n>>> Testing JDWP command \n"); |
|
145 testCommand(threadID); |
|
146 |
|
147 } finally { |
|
148 |
|
149 log.display("\n>>> Finishing test \n"); |
|
150 |
|
151 // resumme suspended thread |
|
152 if (threadID != 0) { |
|
153 log.display("Resuming suspended thread"); |
|
154 debugee.resumeThread(threadID); |
|
155 } |
|
156 |
|
157 // quit debugee |
|
158 quitDebugee(); |
|
159 } |
|
160 |
|
161 } catch (Failure e) { |
|
162 log.complain("TEST FAILED: " + e.getMessage()); |
|
163 success = false; |
|
164 } catch (Exception e) { |
|
165 e.printStackTrace(out); |
|
166 log.complain("Caught unexpected exception while running the test:\n\t" + e); |
|
167 success = false; |
|
168 } |
|
169 |
|
170 if (!success) { |
|
171 log.complain("TEST FAILED"); |
|
172 return FAILED; |
|
173 } |
|
174 |
|
175 out.println("TEST PASSED"); |
|
176 return PASSED; |
|
177 |
|
178 } |
|
179 |
|
180 /** |
|
181 * Prepare debugee for testing and waiting for ready signal. |
|
182 */ |
|
183 void prepareDebugee() { |
|
184 // wait for VM_INIT event from debugee |
|
185 log.display("Waiting for VM_INIT event"); |
|
186 debugee.waitForVMInit(); |
|
187 |
|
188 // query debugee for VM-dependent ID sizes |
|
189 log.display("Querying for IDSizes"); |
|
190 debugee.queryForIDSizes(); |
|
191 |
|
192 // resume initially suspended debugee |
|
193 log.display("Resuming debugee VM"); |
|
194 debugee.resume(); |
|
195 |
|
196 // wait for READY signal from debugee |
|
197 log.display("Waiting for signal from debugee: " + READY); |
|
198 String signal = pipe.readln(); |
|
199 log.display("Received signal from debugee: " + signal); |
|
200 if (signal == null) { |
|
201 throw new TestBug("Null signal received from debugee: " + signal |
|
202 + " (expected: " + READY + ")"); |
|
203 } else if (signal.equals(ERROR)) { |
|
204 throw new TestBug("Debugee was not able to start tested thread" |
|
205 + " (received signal: " + signal + ")"); |
|
206 } else if (!signal.equals(READY)) { |
|
207 throw new TestBug("Unexpected signal received from debugee: " + signal |
|
208 + " (expected: " + READY + ")"); |
|
209 } |
|
210 } |
|
211 |
|
212 /** |
|
213 * Sending debugee signal to quit and waiting for it exits. |
|
214 */ |
|
215 void quitDebugee() { |
|
216 // send debugee signal to quit |
|
217 log.display("Sending signal to debugee: " + QUIT); |
|
218 pipe.println(QUIT); |
|
219 |
|
220 // wait for debugee exits |
|
221 log.display("Waiting for debugee exits"); |
|
222 int code = debugee.waitFor(); |
|
223 |
|
224 // analize debugee exit status code |
|
225 if (code == JCK_STATUS_BASE + PASSED) { |
|
226 log.display("Debugee PASSED with exit code: " + code); |
|
227 } else { |
|
228 log.complain("Debugee FAILED with exit code: " + code); |
|
229 success = false; |
|
230 } |
|
231 } |
|
232 |
|
233 /** |
|
234 * Query debuggee for threadID value of statuic field of the class. |
|
235 */ |
|
236 long queryThreadID(long classID, String fieldName) { |
|
237 // get fieledID for static field (declared in the class) |
|
238 long fieldID = debugee.getClassFieldID(classID, fieldName, true); |
|
239 // get value of the field |
|
240 JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID); |
|
241 |
|
242 // check that value has THREAD tag |
|
243 if (value.getTag() != JDWP.Tag.THREAD) { |
|
244 throw new Failure("Not threadID value returned from debuggee: " + value); |
|
245 } |
|
246 |
|
247 // extract threadID from the value |
|
248 long threadID = ((Long)value.getValue()).longValue(); |
|
249 return threadID; |
|
250 } |
|
251 |
|
252 /** |
|
253 * Query debuggee for suspend status of the thread. |
|
254 */ |
|
255 int querySuspendStatus(long threadID) { |
|
256 log.display("Getting suspend status for threadID: " + threadID); |
|
257 CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Status); |
|
258 command.addObjectID(threadID); |
|
259 ReplyPacket reply = debugee.receiveReplyFor(command); |
|
260 |
|
261 try { |
|
262 reply.resetPosition(); |
|
263 |
|
264 int threadStatus = reply.getInt(); |
|
265 int suspendStatus = reply.getInt(); |
|
266 log.display(" got suspendStatus: " + suspendStatusString(suspendStatus)); |
|
267 return suspendStatus; |
|
268 } catch (BoundException e) { |
|
269 throw new Failure("Caught BoundException while parsing reply for ThreadReference.Status:\n\t" |
|
270 + e); |
|
271 } |
|
272 } |
|
273 |
|
274 /** |
|
275 * Perform testing JDWP command for specified threadID. |
|
276 */ |
|
277 void testCommand(long threadID) { |
|
278 // check that tested thread is not suspended before command sent |
|
279 int suspendStatus = querySuspendStatus(threadID); |
|
280 if (suspendStatus == JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) { |
|
281 throw new Failure("SuspendStatus reports thread is suspended before sending Suspend command: " |
|
282 + suspendStatusString(suspendStatus)); |
|
283 } else { |
|
284 log.display("Thread is not suspended"); |
|
285 } |
|
286 |
|
287 // create command packet and fill requred out data |
|
288 log.display("Create command packet:"); |
|
289 log.display("Command: " + JDWP_COMMAND_NAME); |
|
290 CommandPacket command = new CommandPacket(JDWP_COMMAND_ID); |
|
291 log.display(" threadID: " + threadID); |
|
292 command.addObjectID(threadID); |
|
293 command.setLength(); |
|
294 |
|
295 // send command packet to debugee |
|
296 try { |
|
297 log.display("Sending command packet:\n" + command); |
|
298 transport.write(command); |
|
299 } catch (IOException e) { |
|
300 log.complain("Unable to send command packet:\n\t" + e); |
|
301 success = false; |
|
302 return; |
|
303 } |
|
304 |
|
305 ReplyPacket reply = new ReplyPacket(); |
|
306 |
|
307 // receive reply packet from debugee |
|
308 try { |
|
309 log.display("Waiting for reply packet"); |
|
310 transport.read(reply); |
|
311 log.display("Reply packet received:\n" + reply); |
|
312 } catch (IOException e) { |
|
313 log.complain("Unable to read reply packet:\n\t" + e); |
|
314 success = false; |
|
315 return; |
|
316 } |
|
317 |
|
318 // check reply packet header |
|
319 try{ |
|
320 log.display("Checking reply packet header"); |
|
321 reply.checkHeader(command.getPacketID()); |
|
322 } catch (BoundException e) { |
|
323 log.complain("Bad header of reply packet:\n\t" + e.getMessage()); |
|
324 success = false; |
|
325 return; |
|
326 } |
|
327 |
|
328 // start parsing reply packet data |
|
329 log.display("Parsing reply packet:"); |
|
330 reply.resetPosition(); |
|
331 |
|
332 // check for extra data in reply packet |
|
333 if (!reply.isParsed()) { |
|
334 log.complain("Extra trailing bytes found in reply packet at: " |
|
335 + reply.offsetString()); |
|
336 success = false; |
|
337 } |
|
338 |
|
339 // check that tested thread is suspended after Suspend command sent |
|
340 suspendStatus = querySuspendStatus(threadID); |
|
341 if ((suspendStatus & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) == 0) { |
|
342 log.complain("SuspendStatus reports thread is not suspended after Suspend command sent: " |
|
343 + suspendStatusString(suspendStatus)); |
|
344 } else { |
|
345 log.display("Thread is suspended"); |
|
346 } |
|
347 |
|
348 } |
|
349 |
|
350 /** |
|
351 * Return string representation of thread suspend status. |
|
352 */ |
|
353 private static String suspendStatusString(int status) { |
|
354 String s = null; |
|
355 if ((status & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) != 0) { |
|
356 s = "SUSPEND_STATUS_SUSPENDED"; |
|
357 } else if (status == 0) { |
|
358 s = "NONE"; |
|
359 } else { |
|
360 s = "unknown"; |
|
361 } |
|
362 return status + "=" + s; |
|
363 } |
|
364 } |