Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/Status/status001.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.Status;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.Status.33*34* See status001.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 status001 {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.Status";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "status001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "ThreadReference.Status";62static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Status;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 = status001a.FIELD_NAME;70static final String TESTED_THREAD_NAME = status001a.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 status001().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();123124// work with prepared debuggee125try {126log.display("\n>>> Obtaining requred data from debugee \n");127128// query debuggee for classID of tested class129log.display("Getting classID by signature:\n"130+ " " + TESTED_CLASS_SIGNATURE);131long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);132log.display(" got classID: " + classID);133134// query debuggee for threadID value from a static field135log.display("Getting threadID value from static field: "136+ TESTED_CLASS_FIELD_NAME);137long threadID = queryThreadID(classID, TESTED_CLASS_FIELD_NAME);138log.display(" got threadID: " + threadID);139140// perform testing JDWP command141log.display("\n>>> Testing JDWP command \n");142testCommand(threadID);143144} finally {145// quit debugee146log.display("\n>>> Finishing test \n");147quitDebugee();148}149150} catch (Failure e) {151log.complain("TEST FAILED: " + e.getMessage());152success = false;153} catch (Exception e) {154e.printStackTrace(out);155log.complain("Caught unexpected exception while running the test:\n\t" + e);156success = false;157}158159if (!success) {160log.complain("TEST FAILED");161return FAILED;162}163164out.println("TEST PASSED");165return PASSED;166167}168169/**170* Prepare debugee for testing and waiting for ready signal.171*/172void prepareDebugee() {173// wait for VM_INIT event from debugee174log.display("Waiting for VM_INIT event");175debugee.waitForVMInit();176177// query debugee for VM-dependent ID sizes178log.display("Querying for IDSizes");179debugee.queryForIDSizes();180181// resume initially suspended debugee182log.display("Resuming debugee VM");183debugee.resume();184185// wait for READY signal from debugee186log.display("Waiting for signal from debugee: " + READY);187String signal = pipe.readln();188log.display("Received signal from debugee: " + signal);189if (signal == null) {190throw new TestBug("Null signal received from debugee: " + signal191+ " (expected: " + READY + ")");192} else if (signal.equals(ERROR)) {193throw new TestBug("Debugee was not able to start tested thread"194+ " (received signal: " + signal + ")");195} else if (!signal.equals(READY)) {196throw new TestBug("Unexpected signal received from debugee: " + signal197+ " (expected: " + READY + ")");198}199}200201/**202* Sending debugee signal to quit and waiting for it exits.203*/204void quitDebugee() {205// send debugee signal to quit206log.display("Sending signal to debugee: " + QUIT);207pipe.println(QUIT);208209// wait for debugee exits210log.display("Waiting for debugee exits");211int code = debugee.waitFor();212213// analize debugee exit status code214if (code == JCK_STATUS_BASE + PASSED) {215log.display("Debugee PASSED with exit code: " + code);216} else {217log.complain("Debugee FAILED with exit code: " + code);218success = false;219}220}221222/**223* Query debuggee for threadID value of statuic field of the class.224*/225long queryThreadID(long classID, String fieldName) {226// get fieledID for static field (declared in the class)227long fieldID = debugee.getClassFieldID(classID, fieldName, true);228// get value of the field229JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);230231// check that value has THREAD tag232if (value.getTag() != JDWP.Tag.THREAD) {233throw new Failure("Not threadID value returned from debuggee: " + value);234}235236// extract threadID from the value237long threadID = ((Long)value.getValue()).longValue();238return threadID;239}240241/**242* Perform testing JDWP command for specified threadID.243*/244void testCommand(long threadID) {245// create command packet and fill requred out data246log.display("Create command packet:");247log.display("Command: " + JDWP_COMMAND_NAME);248CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);249log.display(" threadID: " + threadID);250command.addObjectID(threadID);251command.setLength();252253// send command packet to debugee254try {255log.display("Sending command packet:\n" + command);256transport.write(command);257} catch (IOException e) {258log.complain("Unable to send command packet:\n\t" + e);259success = false;260return;261}262263ReplyPacket reply = new ReplyPacket();264265// receive reply packet from debugee266try {267log.display("Waiting for reply packet");268transport.read(reply);269log.display("Reply packet received:\n" + reply);270} catch (IOException e) {271log.complain("Unable to read reply packet:\n\t" + e);272success = false;273return;274}275276// check reply packet header277try{278log.display("Checking reply packet header");279reply.checkHeader(command.getPacketID());280} catch (BoundException e) {281log.complain("Bad header of reply packet:\n\t" + e.getMessage());282success = false;283return;284}285286// start parsing reply packet data287log.display("Parsing reply packet:");288reply.resetPosition();289290// extract thread status291int threadStatus = 0;292try {293threadStatus = reply.getInt();294log.display(" threadStatus: " + threadStatusString(threadStatus));295} catch (BoundException e) {296log.complain("Unable to extract thread status from reply packet:\n\t"297+ e.getMessage());298success = false;299return;300}301302// extract suspend status303int suspendStatus = 0;304try {305suspendStatus = reply.getInt();306log.display(" suspendStatus: " + suspendStatusString(suspendStatus));307} catch (BoundException e) {308log.complain("Unable to extract thread status from reply packet:\n\t"309+ e.getMessage());310success = false;311return;312}313314// check that both status code are not negative values315if (threadStatus < 0) {316log.complain("Negative value of thread status in reply packet: "317+ threadStatusString(threadStatus));318success = false;319}320if (suspendStatus < 0) {321log.complain("Negative value of suspend status in reply packet: "322+ suspendStatusString(suspendStatus));323success = false;324}325326// check that thread has an expected state327if (!(threadStatus == JDWP.ThreadStatus.RUNNING328|| threadStatus == JDWP.ThreadStatus.MONITOR)) {329log.complain("Unexpected thread status returned in the reply packet: "330+ threadStatusString(threadStatus)331+ " (expected: " + threadStatusString(JDWP.ThreadStatus.RUNNING)332+ " or " + threadStatusString(JDWP.ThreadStatus.MONITOR) + ")");333success = false;334}335336// check that thread was not suspended337if ((suspendStatus & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) != 0) {338log.complain("Unexpected suspend status returned in the reply packet: "339+ threadStatusString(threadStatus)340+ " (expected: " + "not suspended" + ")");341success = false;342}343344// check for extra data in reply packet345if (!reply.isParsed()) {346log.complain("Extra trailing bytes found in reply packet at: "347+ reply.offsetString());348success = false;349}350}351352/**353* Return string representation of thread status code.354*/355private static String threadStatusString(int status) {356String s = null;357switch (status) {358case JDWP.ThreadStatus.MONITOR:359s = "MONITOR";360break;361case JDWP.ThreadStatus.RUNNING:362s = "RUNNING";363break;364case JDWP.ThreadStatus.SLEEPING:365s = "SLEEPING";366break;367case JDWP.ThreadStatus.WAIT:368s = "WAIT";369break;370case JDWP.ThreadStatus.ZOMBIE:371s = "ZOMBIE";372break;373default:374s = "unknown";375break;376}377return status + "=" + s;378}379380/**381* Return string representation of thread suspend status.382*/383private static String suspendStatusString(int status) {384String s = null;385if ((status & JDWP.SuspendStatus.SUSPEND_STATUS_SUSPENDED) != 0) {386s = "SUSPEND_STATUS_SUSPENDED";387} else if (status == 0) {388s = "NONE";389} else {390s = "unknown";391}392return status + "=" + s;393}394}395396397