Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/Interrupt/interrupt001.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.Interrupt;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.Interrupt.33*34* See interrupt001.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 interrupt001 {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 INTERRUPTED_TRUE = "interrupted/true";55static final String INTERRUPTED_FALSE = "interrupted/false";56static final String QUIT = "quit";5758// package and classes names constants59static final String PACKAGE_NAME = "nsk.jdwp.ThreadReference.Interrupt";60static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "interrupt001";61static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";6263// tested JDWP command constants64static final String JDWP_COMMAND_NAME = "ThreadReference.Interrupt";65static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.Interrupt;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_CLASS_FIELD_NAME = interrupt001a.FIELD_NAME;73static final String TESTED_THREAD_NAME = interrupt001a.THREAD_NAME;7475// usual scaffold objects76ArgumentHandler argumentHandler = null;77Log log = null;78Binder binder = null;79Debugee debugee = null;80Transport transport = null;81IOPipe pipe = null;8283// test passed or not84boolean success = true;8586// -------------------------------------------------------------------8788/**89* Start test from command line.90*/91public static void main (String argv[]) {92System.exit(run(argv,System.out) + JCK_STATUS_BASE);93}9495/**96* Start JCK-compilant test.97*/98public static int run(String argv[], PrintStream out) {99return new interrupt001().runIt(argv, out);100}101102// -------------------------------------------------------------------103104/**105* Perform test execution.106*/107public int runIt(String argv[], PrintStream out) {108109// make log for debugger messages110argumentHandler = new ArgumentHandler(argv);111log = new Log(out, argumentHandler);112113// execute test and display results114try {115log.display("\n>>> Preparing debugee for testing \n");116117// launch debuggee118binder = new Binder(argumentHandler, log);119log.display("Launching debugee");120debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);121transport = debugee.getTransport();122pipe = debugee.createIOPipe();123124// make debuggee ready for testing125prepareDebugee();126127// work with prepared debuggee128long threadID = 0;129try {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+ TESTED_CLASS_FIELD_NAME);141threadID = queryThreadID(classID, TESTED_CLASS_FIELD_NAME);142log.display(" got threadID: " + threadID);143144// perform testing JDWP command145log.display("\n>>> Testing JDWP command \n");146testCommand(threadID);147148log.display("\n>>> Checking that tested thread was really interrupted \n");149150// check if the thread was really interrupted151confirmThreadInterrupted();152153} finally {154log.display("\n>>> Finishing test \n");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* Perform testing JDWP command for specified threadID.253*/254void testCommand(long threadID) {255// create command packet and fill requred out data256log.display("Create command packet:");257log.display("Command: " + JDWP_COMMAND_NAME);258CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);259log.display(" threadID: " + threadID);260command.addObjectID(threadID);261command.setLength();262263// send command packet to debugee264try {265log.display("Sending command packet:\n" + command);266transport.write(command);267} catch (IOException e) {268log.complain("Unable to send command packet:\n\t" + e);269success = false;270return;271}272273ReplyPacket reply = new ReplyPacket();274275// receive reply packet from debugee276try {277log.display("Waiting for reply packet");278transport.read(reply);279log.display("Reply packet received:\n" + reply);280} catch (IOException e) {281log.complain("Unable to read reply packet:\n\t" + e);282success = false;283return;284}285286// check reply packet header287try{288log.display("Checking reply packet header");289reply.checkHeader(command.getPacketID());290} catch (BoundException e) {291log.complain("Bad header of reply packet:\n\t" + e.getMessage());292success = false;293return;294}295296// start parsing reply packet data297log.display("Parsing reply packet:");298reply.resetPosition();299300// there are no data to extract301302// check for extra data in reply packet303if (!reply.isParsed()) {304log.complain("Extra trailing bytes found in reply packet at: "305+ reply.offsetString());306success = false;307}308309}310311/**312* Wait for signal from debugee that thread was really interrupted313*/314void confirmThreadInterrupted() {315316// send debugee signal to run317log.display("Sending signal to debugee: " + RUN);318pipe.println(RUN);319320// wait for signal DONE from debuggee321log.display("Waiting for signal from debugee: " + INTERRUPTED_TRUE);322String signal = pipe.readln();323log.display("Received signal from debugee: " + signal);324325// check received signal326if (signal == null) {327throw new TestBug("Null signal received from debugee: " + signal328+ " (expected: " + INTERRUPTED_TRUE + ")");329} else if (signal.equals(INTERRUPTED_TRUE)) {330log.display("Tested thread was interrupted into debuggee");331} else if (signal.equals(INTERRUPTED_FALSE)) {332log.complain("Tested thread was NOT interrupted into debuggee");333success = false;334} else {335throw new TestBug("Unexpected signal received from debugee: " + signal336+ " (expected: " + INTERRUPTED_TRUE + ")");337}338339}340341}342343344