|
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.StackFrame.ThisObject; |
|
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: StackFrame.ThisObject. |
|
34 * |
|
35 * See thisobject001.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 thisobject001 { |
|
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.StackFrame.ThisObject"; |
|
58 static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "thisobject001"; |
|
59 static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a"; |
|
60 |
|
61 // tested JDWP command constants |
|
62 static final String JDWP_COMMAND_NAME = "StackFrame.ThisObject"; |
|
63 static final int JDWP_COMMAND_ID = JDWP.Command.StackFrame.ThisObject; |
|
64 |
|
65 // tested class name and signature constants |
|
66 static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + thisobject001a.OBJECT_CLASS_NAME; |
|
67 static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";"; |
|
68 |
|
69 // names of the static fields with the tested thread and object values |
|
70 static final String TESTED_OBJECT_FIELD_NAME = thisobject001a.OBJECT_FIELD_NAME; |
|
71 static final String TESTED_THREAD_FIELD_NAME = thisobject001a.THREAD_FIELD_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 thisobject001().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 // work with prepared debuggee |
|
126 long threadID = 0; |
|
127 try { |
|
128 log.display("\n>>> Obtaining requred data from debugee \n"); |
|
129 |
|
130 // query debuggee for classID of tested class |
|
131 log.display("Getting classID by signature:\n" |
|
132 + " " + TESTED_CLASS_SIGNATURE); |
|
133 long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE); |
|
134 log.display(" got classID: " + classID); |
|
135 |
|
136 // query debuggee for objectID value from static field |
|
137 log.display("Getting objectID value from static field: " |
|
138 + TESTED_OBJECT_FIELD_NAME); |
|
139 long objectID = queryObjectID(classID, TESTED_OBJECT_FIELD_NAME, JDWP.Tag.OBJECT); |
|
140 log.display(" got objectID: " + objectID); |
|
141 |
|
142 // query debuggee for threadID value from static field |
|
143 log.display("Getting threadID value from static field: " |
|
144 + TESTED_THREAD_FIELD_NAME); |
|
145 threadID = queryObjectID(classID, TESTED_THREAD_FIELD_NAME, JDWP.Tag.THREAD); |
|
146 log.display(" got threadID: " + threadID); |
|
147 |
|
148 // suspend tested thread into debuggee |
|
149 log.display("Suspending thread into debuggee for threadID: " + threadID); |
|
150 debugee.suspendThread(threadID); |
|
151 |
|
152 // query debuggee for current frameID of the tested thread |
|
153 log.display("Getting current frameID for the threadID: " |
|
154 + threadID); |
|
155 long frameID = debugee.getCurrentFrameID(threadID); |
|
156 log.display(" got frameID: " + frameID); |
|
157 |
|
158 // perform testing JDWP command |
|
159 log.display("\n>>> Testing JDWP command \n"); |
|
160 testCommand(frameID, threadID, objectID); |
|
161 |
|
162 } finally { |
|
163 log.display("\n>>> Finishing test \n"); |
|
164 |
|
165 // resume suspended thread |
|
166 if (threadID != 0) { |
|
167 log.display("Resuming suspended thread"); |
|
168 debugee.resumeThread(threadID); |
|
169 } |
|
170 |
|
171 // quit debugee |
|
172 quitDebugee(); |
|
173 } |
|
174 |
|
175 } catch (Failure e) { |
|
176 log.complain("TEST FAILED: " + e.getMessage()); |
|
177 success = false; |
|
178 } catch (Exception e) { |
|
179 e.printStackTrace(out); |
|
180 log.complain("Caught unexpected exception while running the test:\n\t" + e); |
|
181 success = false; |
|
182 } |
|
183 |
|
184 if (!success) { |
|
185 log.complain("TEST FAILED"); |
|
186 return FAILED; |
|
187 } |
|
188 |
|
189 out.println("TEST PASSED"); |
|
190 return PASSED; |
|
191 |
|
192 } |
|
193 |
|
194 /** |
|
195 * Prepare debugee for testing and waiting for ready signal. |
|
196 */ |
|
197 void prepareDebugee() { |
|
198 // wait for VM_INIT event from debugee |
|
199 log.display("Waiting for VM_INIT event"); |
|
200 debugee.waitForVMInit(); |
|
201 |
|
202 // query debugee for VM-dependent ID sizes |
|
203 log.display("Querying for IDSizes"); |
|
204 debugee.queryForIDSizes(); |
|
205 |
|
206 // resume initially suspended debugee |
|
207 log.display("Resuming debugee VM"); |
|
208 debugee.resume(); |
|
209 |
|
210 // wait for READY signal from debugee |
|
211 log.display("Waiting for signal from debugee: " + READY); |
|
212 String signal = pipe.readln(); |
|
213 log.display("Received signal from debugee: " + signal); |
|
214 if (signal == null) { |
|
215 throw new TestBug("Null signal received from debugee: " + signal |
|
216 + " (expected: " + READY + ")"); |
|
217 } else if (signal.equals(ERROR)) { |
|
218 throw new TestBug("Debugee was not able to start tested thread" |
|
219 + " (received signal: " + signal + ")"); |
|
220 } else if (!signal.equals(READY)) { |
|
221 throw new TestBug("Unexpected signal received from debugee: " + signal |
|
222 + " (expected: " + READY + ")"); |
|
223 } |
|
224 } |
|
225 |
|
226 /** |
|
227 * Sending debugee signal to quit and waiting for it exits. |
|
228 */ |
|
229 void quitDebugee() { |
|
230 // send debugee signal to quit |
|
231 log.display("Sending signal to debugee: " + QUIT); |
|
232 pipe.println(QUIT); |
|
233 |
|
234 // wait for debugee exits |
|
235 log.display("Waiting for debugee exits"); |
|
236 int code = debugee.waitFor(); |
|
237 |
|
238 // analize debugee exit status code |
|
239 if (code == JCK_STATUS_BASE + PASSED) { |
|
240 log.display("Debugee PASSED with exit code: " + code); |
|
241 } else { |
|
242 log.complain("Debugee FAILED with exit code: " + code); |
|
243 success = false; |
|
244 } |
|
245 } |
|
246 |
|
247 /** |
|
248 * Query debuggee for objectID value of static field of the class. |
|
249 */ |
|
250 long queryObjectID(long classID, String fieldName, byte tag) { |
|
251 // get fieledID for static field (declared in the class) |
|
252 long fieldID = debugee.getClassFieldID(classID, fieldName, true); |
|
253 // get value of the field |
|
254 JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID); |
|
255 |
|
256 // check that value has expected tag |
|
257 if (value.getTag() != tag) { |
|
258 throw new Failure("Unexpected tag for object value returned: " |
|
259 + value.getTag() + " (expected: " + tag + ")"); |
|
260 } |
|
261 |
|
262 // extract objectID from the value |
|
263 long objectID = ((Long)value.getValue()).longValue(); |
|
264 return objectID; |
|
265 } |
|
266 |
|
267 /** |
|
268 * Perform testing JDWP command for specified frameID. |
|
269 */ |
|
270 void testCommand(long frameID, long threadID, long expectedObjectID) { |
|
271 // create command packet and fill requred out data |
|
272 log.display("Create command packet:"); |
|
273 log.display("Command: " + JDWP_COMMAND_NAME); |
|
274 CommandPacket command = new CommandPacket(JDWP_COMMAND_ID); |
|
275 log.display(" threadID: " + threadID); |
|
276 command.addObjectID(threadID); |
|
277 log.display(" frameID: " + frameID); |
|
278 command.addFrameID(frameID); |
|
279 command.setLength(); |
|
280 |
|
281 // send command packet to debugee |
|
282 try { |
|
283 log.display("Sending command packet:\n" + command); |
|
284 transport.write(command); |
|
285 } catch (IOException e) { |
|
286 log.complain("Unable to send command packet:\n\t" + e); |
|
287 success = false; |
|
288 return; |
|
289 } |
|
290 |
|
291 ReplyPacket reply = new ReplyPacket(); |
|
292 |
|
293 // receive reply packet from debugee |
|
294 try { |
|
295 log.display("Waiting for reply packet"); |
|
296 transport.read(reply); |
|
297 log.display("Reply packet received:\n" + reply); |
|
298 } catch (IOException e) { |
|
299 log.complain("Unable to read reply packet:\n\t" + e); |
|
300 success = false; |
|
301 return; |
|
302 } |
|
303 |
|
304 // check reply packet header |
|
305 try{ |
|
306 log.display("Checking reply packet header"); |
|
307 reply.checkHeader(command.getPacketID()); |
|
308 } catch (BoundException e) { |
|
309 log.complain("Bad header of reply packet:\n\t" + e.getMessage()); |
|
310 success = false; |
|
311 return; |
|
312 } |
|
313 |
|
314 // start parsing reply packet data |
|
315 log.display("Parsing reply packet:"); |
|
316 reply.resetPosition(); |
|
317 |
|
318 // extract tag of object value |
|
319 byte tag = (byte)0; |
|
320 try { |
|
321 tag = reply.getByte(); |
|
322 log.display(" tag: " + tag); |
|
323 } catch (BoundException e) { |
|
324 log.complain("Unable to extract object tag from reply packet:\n\t" |
|
325 + e.getMessage()); |
|
326 success = false; |
|
327 return; |
|
328 } |
|
329 |
|
330 // extract objectID of object value |
|
331 long objectID = 0; |
|
332 try { |
|
333 objectID = reply.getObjectID(); |
|
334 log.display(" objectID: " + objectID); |
|
335 } catch (BoundException e) { |
|
336 log.complain("Unable to extract objectID from reply packet:\n\t" |
|
337 + e.getMessage()); |
|
338 success = false; |
|
339 return; |
|
340 } |
|
341 |
|
342 // check for extra data in reply packet |
|
343 if (!reply.isParsed()) { |
|
344 log.complain("Extra trailing bytes found in reply packet at: " |
|
345 + reply.offsetString()); |
|
346 success = false; |
|
347 } |
|
348 |
|
349 // check that object tag is as expected |
|
350 if (tag != JDWP.Tag.OBJECT) { |
|
351 log.complain("Unexpected object tag returned: " |
|
352 + tag + " (expected: " + JDWP.Tag.OBJECT + ")"); |
|
353 success = false; |
|
354 } |
|
355 |
|
356 // check that objectID is as expected |
|
357 if (objectID < 0) { |
|
358 log.complain("Negative value of objectID retured: " |
|
359 + objectID + " (expected: " + expectedObjectID + ")"); |
|
360 success = false; |
|
361 } else if (objectID != expectedObjectID) { |
|
362 log.complain(" Unexpected objectID retured: " |
|
363 + objectID + " (expected: " + expectedObjectID + ")"); |
|
364 success = false; |
|
365 } |
|
366 |
|
367 } |
|
368 |
|
369 } |