Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/Suspend/suspend001.java
41161 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jdwp.ThreadReference.Suspend;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.Suspend.33*34* See suspend001.README for description of test execution.35*36* This class represents debugger part of the test.37* Test is executed by invoking method runIt().38* JDWP command is tested in the method testCommand().39*40* @see #runIt()41* @see #testCommand()42*/43public class suspend001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// communication signals constants51static final String READY = "ready";52static final String ERROR = "error";53static final String QUIT = "quit";5455// package and classes names constants56static final String PACKAGE_NAME = "nsk.jdwp.ThreadReference.Suspend";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "suspend001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "ThreadReference.Suspend";62static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Suspend;6364// tested class name and signature constants65static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";66static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6768// name of the tested thread and statioc field with thread value69static final String TESTED_CLASS_FIELD_NAME = suspend001a.FIELD_NAME;70static final String TESTED_THREAD_NAME = suspend001a.THREAD_NAME;7172// usual scaffold objects73ArgumentHandler argumentHandler = null;74Log log = null;75Binder binder = null;76Debugee debugee = null;77Transport transport = null;78IOPipe pipe = null;7980// test passed or not81boolean success = true;8283// -------------------------------------------------------------------8485/**86* Start test from command line.87*/88public static void main (String argv[]) {89System.exit(run(argv,System.out) + JCK_STATUS_BASE);90}9192/**93* Start JCK-compilant test.94*/95public static int run(String argv[], PrintStream out) {96return new suspend001().runIt(argv, out);97}9899// -------------------------------------------------------------------100101/**102* Perform test execution.103*/104public int runIt(String argv[], PrintStream out) {105106// make log for debugger messages107argumentHandler = new ArgumentHandler(argv);108log = new Log(out, argumentHandler);109110// execute test and display results111try {112log.display("\n>>> Preparing debugee for testing \n");113114// launch debuggee115binder = new Binder(argumentHandler, log);116log.display("Launching debugee");117debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);118transport = debugee.getTransport();119pipe = debugee.createIOPipe();120121// make debuggee ready for testing122prepareDebugee();123124long threadID = 0;125126// work with prepared debuggee127try {128log.display("\n>>> Obtaining requred data from debugee \n");129130// query debuggee for classID of tested class131log.display("Getting classID by signature:\n"132+ " " + TESTED_CLASS_SIGNATURE);133long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);134log.display(" got classID: " + classID);135136// query debuggee for threadID value from a static field137log.display("Getting threadID value from static field: "138+ TESTED_CLASS_FIELD_NAME);139threadID = queryThreadID(classID, TESTED_CLASS_FIELD_NAME);140log.display(" got threadID: " + threadID);141142// perform testing JDWP command143log.display("\n>>> Testing JDWP command \n");144testCommand(threadID);145146} finally {147148log.display("\n>>> Finishing test \n");149150// resumme suspended thread151if (threadID != 0) {152log.display("Resuming suspended thread");153debugee.resumeThread(threadID);154}155156// quit debugee157quitDebugee();158}159160} catch (Failure e) {161log.complain("TEST FAILED: " + e.getMessage());162success = false;163} catch (Exception e) {164e.printStackTrace(out);165log.complain("Caught unexpected exception while running the test:\n\t" + e);166success = false;167}168169if (!success) {170log.complain("TEST FAILED");171return FAILED;172}173174out.println("TEST PASSED");175return PASSED;176177}178179/**180* Prepare debugee for testing and waiting for ready signal.181*/182void prepareDebugee() {183// wait for VM_INIT event from debugee184log.display("Waiting for VM_INIT event");185debugee.waitForVMInit();186187// query debugee for VM-dependent ID sizes188log.display("Querying for IDSizes");189debugee.queryForIDSizes();190191// resume initially suspended debugee192log.display("Resuming debugee VM");193debugee.resume();194195// wait for READY signal from debugee196log.display("Waiting for signal from debugee: " + READY);197String signal = pipe.readln();198log.display("Received signal from debugee: " + signal);199if (signal == null) {200throw new TestBug("Null signal received from debugee: " + signal201+ " (expected: " + READY + ")");202} else if (signal.equals(ERROR)) {203throw new TestBug("Debugee was not able to start tested thread"204+ " (received signal: " + signal + ")");205} else if (!signal.equals(READY)) {206throw new TestBug("Unexpected signal received from debugee: " + signal207+ " (expected: " + READY + ")");208}209}210211/**212* Sending debugee signal to quit and waiting for it exits.213*/214void quitDebugee() {215// send debugee signal to quit216log.display("Sending signal to debugee: " + QUIT);217pipe.println(QUIT);218219// wait for debugee exits220log.display("Waiting for debugee exits");221int code = debugee.waitFor();222223// analize debugee exit status code224if (code == JCK_STATUS_BASE + PASSED) {225log.display("Debugee PASSED with exit code: " + code);226} else {227log.complain("Debugee FAILED with exit code: " + code);228success = false;229}230}231232/**233* Query debuggee for threadID value of statuic field of the class.234*/235long queryThreadID(long classID, String fieldName) {236// get fieledID for static field (declared in the class)237long fieldID = debugee.getClassFieldID(classID, fieldName, true);238// get value of the field239JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);240241// check that value has THREAD tag242if (value.getTag() != JDWP.Tag.THREAD) {243throw new Failure("Not threadID value returned from debuggee: " + value);244}245246// extract threadID from the value247long threadID = ((Long)value.getValue()).longValue();248return threadID;249}250251/**252* Query debuggee for suspend status of the thread.253*/254int querySuspendStatus(long threadID) {255log.display("Getting suspend status for threadID: " + threadID);256CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Status);257command.addObjectID(threadID);258ReplyPacket reply = debugee.receiveReplyFor(command);259260try {261reply.resetPosition();262263int threadStatus = reply.getInt();264int suspendStatus = reply.getInt();265log.display(" got suspendStatus: " + suspendStatusString(suspendStatus));266return suspendStatus;267} catch (BoundException e) {268throw new Failure("Caught BoundException while parsing reply for ThreadReference.Status:\n\t"269+ e);270}271}272273/**274* Perform testing JDWP command for specified threadID.275*/276void testCommand(long threadID) {277// check that tested thread is not suspended before command sent278int suspendStatus = querySuspendStatus(threadID);279if (suspendStatus == JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {280throw new Failure("SuspendStatus reports thread is suspended before sending Suspend command: "281+ suspendStatusString(suspendStatus));282} else {283log.display("Thread is not suspended");284}285286// create command packet and fill requred out data287log.display("Create command packet:");288log.display("Command: " + JDWP_COMMAND_NAME);289CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);290log.display(" threadID: " + threadID);291command.addObjectID(threadID);292command.setLength();293294// send command packet to debugee295try {296log.display("Sending command packet:\n" + command);297transport.write(command);298} catch (IOException e) {299log.complain("Unable to send command packet:\n\t" + e);300success = false;301return;302}303304ReplyPacket reply = new ReplyPacket();305306// receive reply packet from debugee307try {308log.display("Waiting for reply packet");309transport.read(reply);310log.display("Reply packet received:\n" + reply);311} catch (IOException e) {312log.complain("Unable to read reply packet:\n\t" + e);313success = false;314return;315}316317// check reply packet header318try{319log.display("Checking reply packet header");320reply.checkHeader(command.getPacketID());321} catch (BoundException e) {322log.complain("Bad header of reply packet:\n\t" + e.getMessage());323success = false;324return;325}326327// start parsing reply packet data328log.display("Parsing reply packet:");329reply.resetPosition();330331// check for extra data in reply packet332if (!reply.isParsed()) {333log.complain("Extra trailing bytes found in reply packet at: "334+ reply.offsetString());335success = false;336}337338// check that tested thread is suspended after Suspend command sent339suspendStatus = querySuspendStatus(threadID);340if ((suspendStatus & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) == 0) {341log.complain("SuspendStatus reports thread is not suspended after Suspend command sent: "342+ suspendStatusString(suspendStatus));343} else {344log.display("Thread is suspended");345}346347}348349/**350* Return string representation of thread suspend status.351*/352private static String suspendStatusString(int status) {353String s = null;354if ((status & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) != 0) {355s = "SUSPEND_STATUS_SUSPENDED";356} else if (status == 0) {357s = "NONE";358} else {359s = "unknown";360}361return status + "=" + s;362}363}364365366