Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/Stop/stop001.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.Stop;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.Stop.33*34* See stop001.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 stop001 {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 RUN = "run";54static final String STOPPED = "stopped";55static final String NOT_STOPPED = "not_stopped";56static final String QUIT = "quit";5758// package and classes names constants59static final String PACKAGE_NAME = "nsk.jdwp.ThreadReference.Stop";60static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "stop001";61static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";6263// tested JDWP command constants64static final String JDWP_COMMAND_NAME = "ThreadReference.Stop";65static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Stop;6667// tested class name and signature constants68static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";69static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";7071// name of the tested thread and statioc field with thread value72static final String TESTED_THREAD_NAME = stop001a.THREAD_NAME;73static final String THREAD_FIELD_NAME = stop001a.THREAD_FIELD_NAME;74static final String THROWABLE_FIELD_NAME = stop001a.THROWABLE_FIELD_NAME;7576// usual scaffold objects77ArgumentHandler argumentHandler = null;78Log log = null;79Binder binder = null;80Debugee debugee = null;81Transport transport = null;82IOPipe pipe = null;8384// test passed or not85boolean success = true;8687// -------------------------------------------------------------------8889/**90* Start test from command line.91*/92public static void main (String argv[]) {93System.exit(run(argv,System.out) + JCK_STATUS_BASE);94}9596/**97* Start JCK-compilant test.98*/99public static int run(String argv[], PrintStream out) {100return new stop001().runIt(argv, out);101}102103// -------------------------------------------------------------------104105/**106* Perform test execution.107*/108public int runIt(String argv[], PrintStream out) {109110// make log for debugger messages111argumentHandler = new ArgumentHandler(argv);112log = new Log(out, argumentHandler);113114// execute test and display results115try {116log.display("\n>>> Preparing debugee for testing \n");117118// launch debuggee119binder = new Binder(argumentHandler, log);120log.display("Launching debugee");121debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);122transport = debugee.getTransport();123pipe = debugee.createIOPipe();124125// make debuggee ready for testing126prepareDebugee();127128// work with prepared debuggee129try {130log.display("\n>>> Obtaining requred data from debugee \n");131132// query debuggee for classID of tested class133log.display("Getting classID by signature:\n"134+ " " + TESTED_CLASS_SIGNATURE);135long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);136log.display(" got classID: " + classID);137138// query debuggee for threadID value from a static field139log.display("Getting threadID value from static field: "140+ THREAD_FIELD_NAME);141long threadID = queryObjectID(classID, THREAD_FIELD_NAME, JDWP.Tag.THREAD);142log.display(" got threadID: " + threadID);143144// query debuggee for throwable objectID value from a static field145log.display("Getting throwable objectID value from static field: "146+ THROWABLE_FIELD_NAME);147long objectID = queryObjectID(classID, THROWABLE_FIELD_NAME, JDWP.Tag.OBJECT);148log.display(" got objectID: " + objectID);149150// perform testing JDWP command151log.display("\n>>> Testing JDWP command \n");152testCommand(threadID, objectID);153154log.display("\n>>> Checking that tested thread was really stopped \n");155156// check if the thread was really stopped157confirmThreadStopped();158159} finally {160log.display("\n>>> Finishing test \n");161162// quit debugee163quitDebugee();164}165166} catch (Failure e) {167log.complain("TEST FAILED: " + e.getMessage());168success = false;169} catch (Exception e) {170e.printStackTrace(out);171log.complain("Caught unexpected exception while running the test:\n\t" + e);172success = false;173}174175if (!success) {176log.complain("TEST FAILED");177return FAILED;178}179180out.println("TEST PASSED");181return PASSED;182183}184185/**186* Prepare debugee for testing and waiting for ready signal.187*/188void prepareDebugee() {189// wait for VM_INIT event from debugee190log.display("Waiting for VM_INIT event");191debugee.waitForVMInit();192193// query debugee for VM-dependent ID sizes194log.display("Querying for IDSizes");195debugee.queryForIDSizes();196197// resume initially suspended debugee198log.display("Resuming debugee VM");199debugee.resume();200201// wait for READY signal from debugee202log.display("Waiting for signal from debugee: " + READY);203String signal = pipe.readln();204log.display("Received signal from debugee: " + signal);205if (signal == null) {206throw new TestBug("Null signal received from debugee: " + signal207+ " (expected: " + READY + ")");208} else if (signal.equals(ERROR)) {209throw new TestBug("Debugee was not able to start tested thread"210+ " (received signal: " + signal + ")");211} else if (!signal.equals(READY)) {212throw new TestBug("Unexpected signal received from debugee: " + signal213+ " (expected: " + READY + ")");214}215}216217/**218* Sending debugee signal to quit and waiting for it exits.219*/220void quitDebugee() {221// send debugee signal to quit222log.display("Sending signal to debugee: " + QUIT);223pipe.println(QUIT);224225// wait for debugee exits226log.display("Waiting for debugee exits");227int code = debugee.waitFor();228229// analize debugee exit status code230if (code == JCK_STATUS_BASE + PASSED) {231log.display("Debugee PASSED with exit code: " + code);232} else {233log.complain("Debugee FAILED with exit code: " + code);234success = false;235}236}237238/**239* Query debuggee for objectID value from the class static field.240*/241long queryObjectID(long classID, String fieldName, byte tag) {242// get fieledID for static field (declared in the class)243long fieldID = debugee.getClassFieldID(classID, fieldName, true);244// get value of the field245JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);246247// check that value has THREAD tag248if (value.getTag() != tag) {249throw new Failure("Not threadID value returned from debuggee: " + value);250}251252// extract threadID from the value253long objectID = ((Long)value.getValue()).longValue();254return objectID;255}256257/**258* Perform testing JDWP command for specified threadID and throwable objectID.259*/260void testCommand(long threadID, long objectID) {261// create command packet and fill requred out data262log.display("Create command packet:");263log.display("Command: " + JDWP_COMMAND_NAME);264CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);265log.display(" threadID: " + threadID);266command.addObjectID(threadID);267log.display(" throwable: " + objectID);268command.addObjectID(objectID);269command.setLength();270271// send command packet to debugee272try {273log.display("Sending command packet:\n" + command);274transport.write(command);275} catch (IOException e) {276log.complain("Unable to send command packet:\n\t" + e);277success = false;278return;279}280281ReplyPacket reply = new ReplyPacket();282283// receive reply packet from debugee284try {285log.display("Waiting for reply packet");286transport.read(reply);287log.display("Reply packet received:\n" + reply);288} catch (IOException e) {289log.complain("Unable to read reply packet:\n\t" + e);290success = false;291return;292}293294// check reply packet header295try{296log.display("Checking reply packet header");297reply.checkHeader(command.getPacketID());298} catch (BoundException e) {299log.complain("Bad header of reply packet:\n\t" + e.getMessage());300success = false;301return;302}303304// start parsing reply packet data305log.display("Parsing reply packet:");306reply.resetPosition();307308// there are no data to extract309310// check for extra data in reply packet311if (!reply.isParsed()) {312log.complain("Extra trailing bytes found in reply packet at: "313+ reply.offsetString());314success = false;315}316317}318319/**320* Wait for signal from debugee that thread was really stopped321*/322void confirmThreadStopped() {323324// send debugee signal to run325log.display("Sending signal to debugee: " + RUN);326pipe.println(RUN);327328// wait for signal DONE from debuggee329log.display("Waiting for signal from debugee: " + STOPPED);330String signal = pipe.readln();331log.display("Received signal from debugee: " + signal);332333// check received signal334if (signal == null) {335throw new TestBug("Null signal received from debugee: " + signal336+ " (expected: " + STOPPED + ")");337} else if (signal.equals(STOPPED)) {338log.display("Tested thread was really stopped into debuggee");339} else if (signal.equals(NOT_STOPPED)) {340log.complain("Tested thread was NOT really stopped into debuggee");341success = false;342} else {343throw new TestBug("Unexpected signal received from debugee: " + signal344+ " (expected: " + STOPPED + ")");345}346347}348349}350351352