|
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.jdi.ThreadReference.stop; |
|
25 |
|
26 import nsk.share.*; |
|
27 import nsk.share.jpda.*; |
|
28 import nsk.share.jdi.*; |
|
29 |
|
30 import com.sun.jdi.*; |
|
31 import java.util.*; |
|
32 import java.io.*; |
|
33 |
|
34 import com.sun.jdi.event.*; |
|
35 import com.sun.jdi.request.*; |
|
36 |
|
37 /** |
|
38 * The test for the implementation of an object of the type <BR> |
|
39 * ThreadReference. <BR> |
|
40 * <BR> |
|
41 * The test checks up that results of the method <BR> |
|
42 * <code>com.sun.jdi.ThreadReference.stop()</code> <BR> |
|
43 * complies with its spec. <BR> |
|
44 * <BR> |
|
45 * The test works as follows. After being started up, <BR> <BR> |
|
46 * the debuggee creates a 'lockingObject' for synchronizing threads, <BR> |
|
47 * enters a synchronized block in which it creates new thread, thread2, <BR> |
|
48 * informs the debugger of the thread2 creation, <BR> |
|
49 * and is waiting for reply. <BR> |
|
50 * Since the thread2 uses the same locking object in its 'run' method <BR> |
|
51 * it is locked up until getting Exception from the debugger. <BR> |
|
52 * Upon the receiption a message from the debuggee, <BR> |
|
53 * the debugger gets a ThreadReference thread2 mirroring the thread2 and <BR> |
|
54 * an ObjectReference throwableObj mirroring a special Throwable object <BR> |
|
55 * prepared by the main thread in the debuggee, <BR> |
|
56 * (note: for identification, throwableObj.getMessage() returns <BR> |
|
57 * the string "testException") <BR> |
|
58 * and invokes thread2.stop(throwableObj) which should throw exception <BR> |
|
59 * in the locked thread2 in the debugger. <BR> |
|
60 * In catch subclouse, the thread2 assigns Exception it got to a special<BR> |
|
61 * variable tObj of the Throwable type and ends its running. <BR> |
|
62 * The main thread reads tObj.message(), compares to "testException", <BR> |
|
63 * and informs the debugger of strings match or mismatch. <BR> |
|
64 */ |
|
65 |
|
66 public class stop001 { |
|
67 |
|
68 //----------------------------------------------------- templete section |
|
69 static final int PASSED = 0; |
|
70 static final int FAILED = 2; |
|
71 static final int PASS_BASE = 95; |
|
72 |
|
73 //----------------------------------------------------- templete parameters |
|
74 static final String |
|
75 sHeader1 = "\n==> nsk/jdi/ThreadReference/stop/stop001 ", |
|
76 sHeader2 = "--> debugger: ", |
|
77 sHeader3 = "##> debugger: "; |
|
78 |
|
79 //----------------------------------------------------- main method |
|
80 |
|
81 public static void main (String argv[]) { |
|
82 int result = run(argv, System.out); |
|
83 System.exit(result + PASS_BASE); |
|
84 } |
|
85 |
|
86 public static int run (String argv[], PrintStream out) { |
|
87 return new stop001().runThis(argv, out); |
|
88 } |
|
89 |
|
90 //-------------------------------------------------- log procedures |
|
91 |
|
92 private static Log logHandler; |
|
93 |
|
94 private static void log1(String message) { |
|
95 logHandler.display(sHeader1 + message); |
|
96 } |
|
97 private static void log2(String message) { |
|
98 logHandler.display(sHeader2 + message); |
|
99 } |
|
100 private static void log3(String message) { |
|
101 logHandler.complain(sHeader3 + message); |
|
102 } |
|
103 |
|
104 // ************************************************ test parameters |
|
105 |
|
106 private String debuggeeName = |
|
107 "nsk.jdi.ThreadReference.stop.stop001a"; |
|
108 |
|
109 private String testedClassName = |
|
110 "nsk.jdi.ThreadReference.stop.Threadstop001a"; |
|
111 |
|
112 //String mName = "nsk.jdi.ThreadReference.stop"; |
|
113 |
|
114 //====================================================== test program |
|
115 //------------------------------------------------------ common section |
|
116 |
|
117 static ArgumentHandler argsHandler; |
|
118 |
|
119 static int waitTime; |
|
120 |
|
121 static VirtualMachine vm = null; |
|
122 |
|
123 ReferenceType testedclass = null; |
|
124 ThreadReference thread2 = null; |
|
125 ThreadReference mainThread = null; |
|
126 |
|
127 static int testExitCode = PASSED; |
|
128 |
|
129 static final int returnCode0 = 0; |
|
130 static final int returnCode1 = 1; |
|
131 static final int returnCode2 = 2; |
|
132 static final int returnCode3 = 3; |
|
133 static final int returnCode4 = 4; |
|
134 |
|
135 //------------------------------------------------------ methods |
|
136 |
|
137 private int runThis (String argv[], PrintStream out) { |
|
138 |
|
139 Debugee debuggee; |
|
140 |
|
141 argsHandler = new ArgumentHandler(argv); |
|
142 logHandler = new Log(out, argsHandler); |
|
143 Binder binder = new Binder(argsHandler, logHandler); |
|
144 |
|
145 if (argsHandler.verbose()) { |
|
146 debuggee = binder.bindToDebugee(debuggeeName + " -vbs"); |
|
147 } else { |
|
148 debuggee = binder.bindToDebugee(debuggeeName); |
|
149 } |
|
150 |
|
151 waitTime = argsHandler.getWaitTime(); |
|
152 |
|
153 |
|
154 IOPipe pipe = new IOPipe(debuggee); |
|
155 |
|
156 debuggee.redirectStderr(out); |
|
157 log2("issuspended002a debuggee launched"); |
|
158 debuggee.resume(); |
|
159 |
|
160 String line = pipe.readln(); |
|
161 if ((line == null) || !line.equals("ready")) { |
|
162 log3("signal received is not 'ready' but: " + line); |
|
163 return FAILED; |
|
164 } else { |
|
165 log2("'ready' recieved"); |
|
166 } |
|
167 |
|
168 vm = debuggee.VM(); |
|
169 |
|
170 //------------------------------------------------------ testing section |
|
171 |
|
172 log1(" TESTING BEGINS"); |
|
173 |
|
174 for (int i = 0; ; i++) { |
|
175 |
|
176 pipe.println("newcheck"); |
|
177 line = pipe.readln(); |
|
178 |
|
179 if (line.equals("checkend")) { |
|
180 log2(" : returned string is 'checkend'"); |
|
181 break ; |
|
182 } else if (!line.equals("checkready")) { |
|
183 log3("ERROR: returned string is not 'checkready'"); |
|
184 testExitCode = FAILED; |
|
185 break ; |
|
186 } |
|
187 |
|
188 log1("new checkready: #" + i); |
|
189 |
|
190 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part |
|
191 |
|
192 int expresult = returnCode0; |
|
193 |
|
194 String threadName = "Thread2"; |
|
195 |
|
196 List allThreads = null; |
|
197 ListIterator listIterator = null; |
|
198 List classes = null; |
|
199 ReferenceType mainthreadClass = null; |
|
200 ObjectReference throwableObj = null; |
|
201 |
|
202 |
|
203 label0: { |
|
204 |
|
205 log2("getting ThreadReference objects and setting up breakponts"); |
|
206 try { |
|
207 allThreads = vm.allThreads(); |
|
208 classes = vm.classesByName(debuggeeName); |
|
209 mainthreadClass = (ReferenceType) classes.get(0); |
|
210 } catch ( Exception e) { |
|
211 log3("ERROR: Exception at very beginning !? : " + e); |
|
212 expresult = returnCode1; |
|
213 break label0; |
|
214 } |
|
215 |
|
216 listIterator = allThreads.listIterator(); |
|
217 for (;;) { |
|
218 try { |
|
219 thread2 = (ThreadReference) listIterator.next(); |
|
220 if (thread2.name().equals(threadName)) |
|
221 break ; |
|
222 } catch ( NoSuchElementException e ) { |
|
223 log3("ERROR: NoSuchElementException for listIterator.next()"); |
|
224 log3("ERROR: NO THREAD2 ?????????!!!!!!!"); |
|
225 expresult = returnCode1; |
|
226 break label0; |
|
227 } |
|
228 } |
|
229 } |
|
230 |
|
231 |
|
232 label1: { |
|
233 if (expresult != returnCode0) |
|
234 break label1; |
|
235 |
|
236 |
|
237 try { |
|
238 log2(" getting a mirror of the throwableObj"); |
|
239 throwableObj = (ObjectReference) |
|
240 mainthreadClass.getValue(mainthreadClass.fieldByName("throwableObj")); |
|
241 |
|
242 log2(" stopping the thread2"); |
|
243 thread2.stop(throwableObj); |
|
244 |
|
245 } catch ( InvalidTypeException e1 ) { |
|
246 log3("ERROR: InvalidTypeException ???"); |
|
247 expresult = returnCode1; |
|
248 } catch ( Exception e2 ) { |
|
249 log3("ERROR: unexpected exception: " + e2); |
|
250 expresult = returnCode1; |
|
251 } |
|
252 |
|
253 log2("......instructing mainThread to leave synchronized block"); |
|
254 pipe.println("continue"); |
|
255 |
|
256 log2("......getting result from mainThread:"); |
|
257 line = pipe.readln(); |
|
258 log2(" returned string is: " + line); |
|
259 if (line.equals("null")) { |
|
260 log3("ERROR: 'stop001a.tObj = e1;' was not assigned"); |
|
261 expresult = returnCode1; |
|
262 } else if (line.equals("equal")) { |
|
263 } else if (line.equals("NOT_equal")) { |
|
264 log3("ERROR: in the debugee, e1 is not 'LineUnavailableException'"); |
|
265 expresult = returnCode1; |
|
266 } else { |
|
267 log3("ERROR: returned string is unexpected"); |
|
268 expresult = returnCode4; |
|
269 } |
|
270 } |
|
271 |
|
272 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
273 log2(" the end of testing"); |
|
274 if (expresult != returnCode0) |
|
275 testExitCode = FAILED; |
|
276 } |
|
277 log1(" TESTING ENDS"); |
|
278 |
|
279 //-------------------------------------------------- test summary section |
|
280 //------------------------------------------------- standard end section |
|
281 |
|
282 pipe.println("quit"); |
|
283 log2("waiting for the debuggee to finish ..."); |
|
284 debuggee.waitFor(); |
|
285 |
|
286 int status = debuggee.getStatus(); |
|
287 if (status != PASSED + PASS_BASE) { |
|
288 log3("debuggee returned UNEXPECTED exit status: " + |
|
289 status + " != PASS_BASE"); |
|
290 testExitCode = FAILED; |
|
291 } else { |
|
292 log2("debuggee returned expected exit status: " + |
|
293 status + " == PASS_BASE"); |
|
294 } |
|
295 |
|
296 if (testExitCode != PASSED) { |
|
297 logHandler.complain("TEST FAILED"); |
|
298 } |
|
299 return testExitCode; |
|
300 } |
|
301 |
|
302 } |