1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Vitaly A. Provodin
21  */
22 
23 /**
24  * Created on 09.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34 
35 
36 /**
37  * JDWP Unit test for VirtualMachine.AllThreads command.
38  */
39 public class AllThreadsTest extends JDWPSyncTestCase {
40 
getDebuggeeClassName()41     protected String getDebuggeeClassName() {
42         return "org.apache.harmony.jpda.tests.jdwp.VirtualMachine.AllThreadsDebuggee";
43     }
44 
45     /**
46      * This testcase exercises VirtualMachine.AllThreads command.
47      * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread
48      * which starts and finishes.
49      * <BR> Then the test performs VirtualMachine.AllThreads command and checks that
50      * the tested thread is not returned by command;
51      */
testAllThreads003()52     public void testAllThreads003() {
53         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
54 
55         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
56         logWriter.println("waiting for finishing thread");
57         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
58 
59         logWriter.println("send AllThreads cmd");
60         CommandPacket packet = new CommandPacket(
61                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
62                 JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
63 
64         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
65         checkReplyPacket(reply, "VirtualMachine::AllThreads command");
66 
67         long threadID;
68         int threadStatus, suspendStatus;
69         String threadName;
70         ReplyPacket replyName;
71 
72         int threads = reply.getNextValueAsInt();
73         logWriter.println("Number of threads = " + threads);
74         assertTrue("Number of threads must be > 0", threads > 0);
75 
76         for (int i = 0; i < threads; i++) {
77 
78             threadID = reply.getNextValueAsThreadID();
79             threadName = debuggeeWrapper.vmMirror.getThreadName(threadID);
80 
81             packet = new CommandPacket(
82                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
83                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
84             packet.setNextValueAsReferenceTypeID(threadID);
85 
86             replyName = debuggeeWrapper.vmMirror.performCommand(packet);
87             checkReplyPacket(replyName, "ThreadReference::Status command");
88 
89             threadStatus = replyName.getNextValueAsInt();
90             suspendStatus = replyName.getNextValueAsInt();
91 
92             logWriter.println("\t" + threadID + " "
93                     + "\"" + threadName + "\" "
94                     + JDWPConstants.ThreadStatus.getName(threadStatus) + " "
95                     + JDWPConstants.SuspendStatus.getName(suspendStatus));
96 
97             if (threadName.equals(AllThreadsDebuggee.TESTED_THREAD)) {
98                 printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD
99                         + " must not be in all_thread list");
100             }
101         }
102         assertAllDataRead(reply);
103         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
104     }
105 
106     /**
107      * This testcase exercises VirtualMachine.AllThreads command.
108      * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread
109      * which starts but does not finish.
110      * <BR> Then the test performs VirtualMachine.AllThreads command and checks that
111      * all threads returned by command have only valid thread status:
112      * <BR>'RUNNING' or 'MONITOR' or 'SLEEPING' or 'ZOMBIE' or 'WAIT' status;
113      */
testAllThreads002()114     public void testAllThreads002() {
115         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
116 
117         logWriter.println("send AllThreads cmd");
118         CommandPacket packet = new CommandPacket(
119                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
120                 JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
121 
122         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
123         checkReplyPacket(reply, "VirtualMachine::AllThreads command");
124 
125         long threadID;
126         int threadStatus, suspendStatus;
127         String threadName;
128         int count = 0;
129         ReplyPacket replyName;
130 
131         int threads = reply.getNextValueAsInt();
132         logWriter.println("Number of threads = " + threads);
133         assertTrue("Number of threads must be > 0", threads > 0);
134 
135         boolean threadStatusFailed = false;
136         for (int i = 0; i < threads; i++) {
137 
138             threadID = reply.getNextValueAsThreadID();
139             threadName = debuggeeWrapper.vmMirror.getThreadName(threadID);
140 
141             packet = new CommandPacket(
142                     JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
143                     JDWPCommands.ThreadReferenceCommandSet.StatusCommand);
144             packet.setNextValueAsReferenceTypeID(threadID);
145 
146             replyName = debuggeeWrapper.vmMirror.performCommand(packet);
147             checkReplyPacket(replyName, "ThreadReference::Status command");
148 
149             threadStatus = replyName.getNextValueAsInt();
150             suspendStatus = replyName.getNextValueAsInt();
151 
152             logWriter.println("\t" + threadID + " "
153                     + "\"" + threadName + "\" "
154                     + JDWPConstants.ThreadStatus.getName(threadStatus) + " "
155                     + JDWPConstants.SuspendStatus.getName(suspendStatus));
156             if (threadStatus == JDWPConstants.ThreadStatus.RUNNING
157                     || threadStatus == JDWPConstants.ThreadStatus.MONITOR
158                     || threadStatus == JDWPConstants.ThreadStatus.SLEEPING
159                     || threadStatus == JDWPConstants.ThreadStatus.ZOMBIE
160                     || threadStatus == JDWPConstants.ThreadStatus.WAIT) {
161             } else {
162                 logWriter.println
163                 ("## FAILURE: Unknown thread status is found out!");
164                 threadStatusFailed = true;
165                 count++;
166             }
167         }
168         if (threadStatusFailed) {
169             printErrorAndFail("\nThreads with unknown thread status found out!\n" +
170                     "Number of such threads = " + count +"\n");
171         }
172         assertAllDataRead(reply);
173 
174         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
175         logWriter.println("waiting for finishing thread");
176         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
177         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
178     }
179 
180     /**
181      * This testcase exercises VirtualMachine.AllThreads command.
182      * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread
183      * which starts but does not finish.
184      * <BR> Then the test performs VirtualMachine.AllThreads command and checks that
185      * the tested thread is returned by command in the list of threads;
186      */
testAllThreads001()187     public void testAllThreads001() {
188         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
189 
190         logWriter.println("send AllThreads cmd");
191         CommandPacket packet = new CommandPacket(
192                 JDWPCommands.VirtualMachineCommandSet.CommandSetID,
193                 JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
194 
195         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
196         checkReplyPacket(reply, "VirtualMachine::AllThreads command");
197 
198         long threadID;
199         String threadName;
200         int count = 0;
201 
202         int threads = reply.getNextValueAsInt();
203         logWriter.println("Number of threads = " + threads);
204         assertTrue("Number of threads must be > 0", threads > 0);
205 
206         for (int i = 0; i < threads; i++) {
207             threadID = reply.getNextValueAsThreadID();
208             threadName = debuggeeWrapper.vmMirror.getThreadName(threadID);
209 
210             if (threadName.equals(AllThreadsDebuggee.TESTED_THREAD)) {
211                 count++;
212                 logWriter.println("\t" + threadID + " "
213                         + "\"" + threadName + "\" found");
214             }
215         }
216         if (count != 1) {
217             if (count == 0) {
218                 printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD + " not found");
219             }
220             if (count > 1 || count < 0) {
221                 printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD
222                         + " unexpected amount: " + count);
223             }
224         }
225         assertAllDataRead(reply);
226         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
227         logWriter.println("waiting for finishing thread");
228         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
229         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
230     }
231 }
232