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 41 @Override 42 protected String getDebuggeeClassName() { 43 return "org.apache.harmony.jpda.tests.jdwp.VirtualMachine.AllThreadsDebuggee"; 44 } 45 46 /** 47 * This testcase exercises VirtualMachine.AllThreads command. 48 * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread 49 * which starts and finishes. 50 * <BR> Then the test performs VirtualMachine.AllThreads command and checks that 51 * the tested thread is not returned by command; 52 */ 53 public void testAllThreads003() { 54 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 55 56 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 57 logWriter.println("waiting for finishing thread"); 58 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 59 60 logWriter.println("send AllThreads cmd"); 61 CommandPacket packet = new CommandPacket( 62 JDWPCommands.VirtualMachineCommandSet.CommandSetID, 63 JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand); 64 65 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 66 checkReplyPacket(reply, "VirtualMachine::AllThreads command"); 67 68 long threadID; 69 int threadStatus, suspendStatus; 70 String threadName; 71 ReplyPacket replyName; 72 73 int threads = reply.getNextValueAsInt(); 74 logWriter.println("Number of threads = " + threads); 75 assertTrue("Number of threads must be > 0", threads > 0); 76 77 for (int i = 0; i < threads; i++) { 78 79 threadID = reply.getNextValueAsThreadID(); 80 threadName = debuggeeWrapper.vmMirror.getThreadName(threadID); 81 82 packet = new CommandPacket( 83 JDWPCommands.ThreadReferenceCommandSet.CommandSetID, 84 JDWPCommands.ThreadReferenceCommandSet.StatusCommand); 85 packet.setNextValueAsReferenceTypeID(threadID); 86 87 replyName = debuggeeWrapper.vmMirror.performCommand(packet); 88 checkReplyPacket(replyName, "ThreadReference::Status command"); 89 90 threadStatus = replyName.getNextValueAsInt(); 91 suspendStatus = replyName.getNextValueAsInt(); 92 93 logWriter.println("\t" + threadID + " " 94 + "\"" + threadName + "\" " 95 + JDWPConstants.ThreadStatus.getName(threadStatus) + " " 96 + JDWPConstants.SuspendStatus.getName(suspendStatus)); 97 98 if (threadName.equals(AllThreadsDebuggee.TESTED_THREAD)) { 99 printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD 100 + " must not be in all_thread list"); 101 } 102 } 103 assertAllDataRead(reply); 104 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 105 } 106 107 /** 108 * This testcase exercises VirtualMachine.AllThreads command. 109 * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread 110 * which starts but does not finish. 111 * <BR> Then the test performs VirtualMachine.AllThreads command and checks that 112 * all threads returned by command have only valid thread status: 113 * <BR>'RUNNING' or 'MONITOR' or 'SLEEPING' or 'ZOMBIE' or 'WAIT' status; 114 */ 115 public void testAllThreads002() { 116 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 117 118 logWriter.println("send AllThreads cmd"); 119 CommandPacket packet = new CommandPacket( 120 JDWPCommands.VirtualMachineCommandSet.CommandSetID, 121 JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand); 122 123 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 124 checkReplyPacket(reply, "VirtualMachine::AllThreads command"); 125 126 long threadID; 127 int threadStatus, suspendStatus; 128 String threadName; 129 int count = 0; 130 ReplyPacket replyName; 131 132 int threads = reply.getNextValueAsInt(); 133 logWriter.println("Number of threads = " + threads); 134 assertTrue("Number of threads must be > 0", threads > 0); 135 136 boolean threadStatusFailed = false; 137 for (int i = 0; i < threads; i++) { 138 139 threadID = reply.getNextValueAsThreadID(); 140 threadName = debuggeeWrapper.vmMirror.getThreadName(threadID); 141 142 packet = new CommandPacket( 143 JDWPCommands.ThreadReferenceCommandSet.CommandSetID, 144 JDWPCommands.ThreadReferenceCommandSet.StatusCommand); 145 packet.setNextValueAsReferenceTypeID(threadID); 146 147 replyName = debuggeeWrapper.vmMirror.performCommand(packet); 148 checkReplyPacket(replyName, "ThreadReference::Status command"); 149 150 threadStatus = replyName.getNextValueAsInt(); 151 suspendStatus = replyName.getNextValueAsInt(); 152 153 logWriter.println("\t" + threadID + " " 154 + "\"" + threadName + "\" " 155 + JDWPConstants.ThreadStatus.getName(threadStatus) + " " 156 + JDWPConstants.SuspendStatus.getName(suspendStatus)); 157 if (threadStatus == JDWPConstants.ThreadStatus.RUNNING 158 || threadStatus == JDWPConstants.ThreadStatus.MONITOR 159 || threadStatus == JDWPConstants.ThreadStatus.SLEEPING 160 || threadStatus == JDWPConstants.ThreadStatus.ZOMBIE 161 || threadStatus == JDWPConstants.ThreadStatus.WAIT) { 162 } else { 163 logWriter.println 164 ("## FAILURE: Unknown thread status is found out!"); 165 threadStatusFailed = true; 166 count++; 167 } 168 } 169 if (threadStatusFailed) { 170 printErrorAndFail("\nThreads with unknown thread status found out!\n" + 171 "Number of such threads = " + count +"\n"); 172 } 173 assertAllDataRead(reply); 174 175 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 176 logWriter.println("waiting for finishing thread"); 177 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 178 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 179 } 180 181 /** 182 * This testcase exercises VirtualMachine.AllThreads command. 183 * <BR>At first the test starts AllThreadsDebuggee which runs the TESTED_THREAD thread 184 * which starts but does not finish. 185 * <BR> Then the test performs VirtualMachine.AllThreads command and checks that 186 * the tested thread is returned by command in the list of threads; 187 */ 188 public void testAllThreads001() { 189 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 190 191 logWriter.println("send AllThreads cmd"); 192 CommandPacket packet = new CommandPacket( 193 JDWPCommands.VirtualMachineCommandSet.CommandSetID, 194 JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand); 195 196 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 197 checkReplyPacket(reply, "VirtualMachine::AllThreads command"); 198 199 long threadID; 200 String threadName; 201 int count = 0; 202 203 int threads = reply.getNextValueAsInt(); 204 logWriter.println("Number of threads = " + threads); 205 assertTrue("Number of threads must be > 0", threads > 0); 206 207 for (int i = 0; i < threads; i++) { 208 threadID = reply.getNextValueAsThreadID(); 209 threadName = debuggeeWrapper.vmMirror.getThreadName(threadID); 210 211 if (threadName.equals(AllThreadsDebuggee.TESTED_THREAD)) { 212 count++; 213 logWriter.println("\t" + threadID + " " 214 + "\"" + threadName + "\" found"); 215 } 216 } 217 if (count != 1) { 218 if (count == 0) { 219 printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD + " not found"); 220 } 221 if (count > 1 || count < 0) { 222 printErrorAndFail(AllThreadsDebuggee.TESTED_THREAD 223 + " unexpected amount: " + count); 224 } 225 } 226 assertAllDataRead(reply); 227 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 228 logWriter.println("waiting for finishing thread"); 229 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 230 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 231 } 232 } 233