Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/Resume/resume001.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.Resume;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.Resume.33*34* See resume001.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 resume001 {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.Resume";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "resume001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "ThreadReference.Resume";62static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Resume;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 = resume001a.FIELD_NAME;70static final String TESTED_THREAD_NAME = resume001a.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 resume001().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// request debuggee to suspend tested thread143log.display("Suspendig thread into debuggee for threadID: " + threadID);144debugee.suspendThread(threadID);145146// perform testing JDWP command147log.display("\n>>> Testing JDWP command \n");148testCommand(threadID);149150} finally {151152log.display("\n>>> Finishing test \n");153154// resume suspended thread155if (threadID != 0) {156log.display("Resuming potentially suspended thread");157debugee.resumeThread(threadID);158}159160// quit debugee161quitDebugee();162}163164} catch (Failure e) {165log.complain("TEST FAILED: " + e.getMessage());166success = false;167} catch (Exception e) {168e.printStackTrace(out);169log.complain("Caught unexpected exception while running the test:\n\t" + e);170success = false;171}172173if (!success) {174log.complain("TEST FAILED");175return FAILED;176}177178out.println("TEST PASSED");179return PASSED;180181}182183/**184* Prepare debugee for testing and waiting for ready signal.185*/186void prepareDebugee() {187// wait for VM_INIT event from debugee188log.display("Waiting for VM_INIT event");189debugee.waitForVMInit();190191// query debugee for VM-dependent ID sizes192log.display("Querying for IDSizes");193debugee.queryForIDSizes();194195// resume initially suspended debugee196log.display("Resuming debugee VM");197debugee.resume();198199// wait for READY signal from debugee200log.display("Waiting for signal from debugee: " + READY);201String signal = pipe.readln();202log.display("Received signal from debugee: " + signal);203if (signal == null) {204throw new TestBug("Null signal received from debugee: " + signal205+ " (expected: " + READY + ")");206} else if (signal.equals(ERROR)) {207throw new TestBug("Debugee was not able to start tested thread"208+ " (received signal: " + signal + ")");209} else if (!signal.equals(READY)) {210throw new TestBug("Unexpected signal received from debugee: " + signal211+ " (expected: " + READY + ")");212}213}214215/**216* Sending debugee signal to quit and waiting for it exits.217*/218void quitDebugee() {219// send debugee signal to quit220log.display("Sending signal to debugee: " + QUIT);221pipe.println(QUIT);222223// wait for debugee exits224log.display("Waiting for debugee exits");225int code = debugee.waitFor();226227// analize debugee exit status code228if (code == JCK_STATUS_BASE + PASSED) {229log.display("Debugee PASSED with exit code: " + code);230} else {231log.complain("Debugee FAILED with exit code: " + code);232success = false;233}234}235236/**237* Query debuggee for threadID value of statuic field of the class.238*/239long queryThreadID(long classID, String fieldName) {240// get fieledID for static field (declared in the class)241long fieldID = debugee.getClassFieldID(classID, fieldName, true);242// get value of the field243JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);244245// check that value has THREAD tag246if (value.getTag() != JDWP.Tag.THREAD) {247throw new Failure("Not threadID value returned from debuggee: " + value);248}249250// extract threadID from the value251long threadID = ((Long)value.getValue()).longValue();252return threadID;253}254255/**256* Query debuggee for suspend status of the thread.257*/258int querySuspendStatus(long threadID) {259log.display("Getting suspend status for threadID: " + threadID);260CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Status);261command.addObjectID(threadID);262ReplyPacket reply = debugee.receiveReplyFor(command);263264try {265reply.resetPosition();266267int threadStatus = reply.getInt();268int suspendStatus = reply.getInt();269log.display(" got suspendStatus: " + suspendStatusString(suspendStatus));270return suspendStatus;271} catch (BoundException e) {272throw new Failure("Caught BoundException while parsing reply for ThreadReference.Status:\n\t"273+ e);274}275}276277/**278* Perform testing JDWP command for specified threadID.279*/280void testCommand(long threadID) {281// check that tested thread is not suspended before command sent282int suspendStatus = querySuspendStatus(threadID);283if (suspendStatus != JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) {284throw new Failure("SuspendStatus reports thread is not suspended before sending Resume command: "285+ suspendStatusString(suspendStatus));286} else {287log.display("Thread is suspended");288}289290// create command packet and fill requred out data291log.display("Create command packet:");292log.display("Command: " + JDWP_COMMAND_NAME);293CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);294log.display(" threadID: " + threadID);295command.addObjectID(threadID);296command.setLength();297298// send command packet to debugee299try {300log.display("Sending command packet:\n" + command);301transport.write(command);302} catch (IOException e) {303log.complain("Unable to send command packet:\n\t" + e);304success = false;305return;306}307308ReplyPacket reply = new ReplyPacket();309310// receive reply packet from debugee311try {312log.display("Waiting for reply packet");313transport.read(reply);314log.display("Reply packet received:\n" + reply);315} catch (IOException e) {316log.complain("Unable to read reply packet:\n\t" + e);317success = false;318return;319}320321// check reply packet header322try{323log.display("Checking reply packet header");324reply.checkHeader(command.getPacketID());325} catch (BoundException e) {326log.complain("Bad header of reply packet:\n\t" + e.getMessage());327success = false;328return;329}330331// start parsing reply packet data332log.display("Parsing reply packet:");333reply.resetPosition();334335// check for extra data in reply packet336if (!reply.isParsed()) {337log.complain("Extra trailing bytes found in reply packet at: "338+ reply.offsetString());339success = false;340}341342// check that tested thread is suspended after Resume command sent343suspendStatus = querySuspendStatus(threadID);344if ((suspendStatus & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) != 0) {345log.complain("SuspendStatus reports thread is suspended after Resume command sent: "346+ suspendStatusString(suspendStatus));347} else {348log.display("Thread is not suspended");349}350351}352353/**354* Return string representation of thread suspend status.355*/356private static String suspendStatusString(int status) {357String s = null;358if ((status & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) != 0) {359s = "SUSPEND_STATUS_SUSPENDED";360} else if (status == 0) {361s = "NONE";362} else {363s = "unknown";364}365return status + "=" + s;366}367}368369370