Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ObjectReference/IsCollected/iscollected001.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.ObjectReference.IsCollected;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ObjectReference.IsCollected.33*34* See iscollected001.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 iscollected001 {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.ObjectReference.IsCollected";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "iscollected001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "ObjectReference.IsCollected";62static final int JDWP_COMMAND_ID = JDWP.Command.ObjectReference.IsCollected;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 OBJECT_FIELD_NAME = iscollected001a.OBJECT_FIELD_NAME;7071// usual scaffold objects72ArgumentHandler argumentHandler = null;73Log log = null;74Binder binder = null;75Debugee debugee = null;76Transport transport = null;77IOPipe pipe = null;7879// test passed or not80boolean success = true;8182// -------------------------------------------------------------------8384/**85* Start test from command line.86*/87public static void main (String argv[]) {88System.exit(run(argv,System.out) + JCK_STATUS_BASE);89}9091/**92* Start JCK-compilant test.93*/94public static int run(String argv[], PrintStream out) {95return new iscollected001().runIt(argv, out);96}9798// -------------------------------------------------------------------99100/**101* Perform test execution.102*/103public int runIt(String argv[], PrintStream out) {104105// make log for debugger messages106argumentHandler = new ArgumentHandler(argv);107log = new Log(out, argumentHandler);108109// execute test and display results110try {111log.display("\n>>> Preparing debugee for testing \n");112113// launch debuggee114binder = new Binder(argumentHandler, log);115log.display("Launching debugee");116debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);117transport = debugee.getTransport();118pipe = debugee.createIOPipe();119120// make debuggee ready for testing121prepareDebugee();122123// work with prepared debuggee124try {125log.display("\n>>> Obtaining requred data from debuggee \n");126127// query debuggee for classID of tested class128log.display("Getting classID by signature:\n"129+ " " + TESTED_CLASS_SIGNATURE);130long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);131log.display(" got classID: " + classID);132133// query debuggee for objectID value from static field134log.display("Getting objectID value from static field: "135+ OBJECT_FIELD_NAME);136long objectID = queryObjectID(classID,137OBJECT_FIELD_NAME, JDWP.Tag.OBJECT);138log.display(" got objectID: " + objectID);139140// perform testing JDWP command141log.display("\n>>> Testing JDWP command \n");142testCommand(objectID, classID);143144} finally {145log.display("\n>>> Finishing test \n");146147// quit debugee148quitDebugee();149}150151} catch (Failure e) {152log.complain("TEST FAILED: " + e.getMessage());153success = false;154} catch (Exception e) {155e.printStackTrace(out);156log.complain("Caught unexpected exception while running the test:\n\t" + e);157success = false;158}159160if (!success) {161log.complain("TEST FAILED");162return FAILED;163}164165out.println("TEST PASSED");166return PASSED;167168}169170/**171* Prepare debugee for testing and waiting for ready signal.172*/173void prepareDebugee() {174// wait for VM_INIT event from debugee175log.display("Waiting for VM_INIT event");176debugee.waitForVMInit();177178// query debugee for VM-dependent ID sizes179log.display("Querying for IDSizes");180debugee.queryForIDSizes();181182// resume initially suspended debugee183log.display("Resuming debugee VM");184debugee.resume();185186// wait for READY signal from debugee187log.display("Waiting for signal from debugee: " + READY);188String signal = pipe.readln();189log.display("Received signal from debugee: " + signal);190if (signal == null) {191throw new TestBug("Null signal received from debugee: " + signal192+ " (expected: " + READY + ")");193} else if (signal.equals(ERROR)) {194throw new TestBug("Debugee was not able to start tested thread"195+ " (received signal: " + signal + ")");196} else if (!signal.equals(READY)) {197throw new TestBug("Unexpected signal received from debugee: " + signal198+ " (expected: " + READY + ")");199}200}201202/**203* Sending debugee signal to quit and waiting for it exits.204*/205void quitDebugee() {206// send debugee signal to quit207log.display("Sending signal to debugee: " + QUIT);208pipe.println(QUIT);209210// wait for debugee exits211log.display("Waiting for debugee exits");212int code = debugee.waitFor();213214// analize debugee exit status code215if (code == JCK_STATUS_BASE + PASSED) {216log.display("Debugee PASSED with exit code: " + code);217} else {218log.complain("Debugee FAILED with exit code: " + code);219success = false;220}221}222223/**224* Query debuggee for objectID value of static class field.225*/226long queryObjectID(long classID, String fieldName, byte tag) {227// get fieledID for static field (declared in the class)228long fieldID = debugee.getClassFieldID(classID, fieldName, true);229// get value of the field230JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);231232// check that value has THREAD tag233if (value.getTag() != tag) {234throw new Failure("Wrong objectID tag received from field \"" + fieldName235+ "\": " + value.getTag() + " (expected: " + tag + ")");236}237238// extract threadID from the value239long objectID = ((Long)value.getValue()).longValue();240return objectID;241}242243/**244* Perform testing JDWP command for specified threadID.245*/246void testCommand(long objectID, long classID) {247// create command packet and fill requred out data248log.display("Create command packet:");249log.display("Command: " + JDWP_COMMAND_NAME);250CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);251log.display(" objectID: " + objectID);252command.addObjectID(objectID);253command.setLength();254255// send command packet to debugee256try {257log.display("Sending command packet:\n" + command);258transport.write(command);259} catch (IOException e) {260log.complain("Unable to send command packet:\n\t" + e);261success = false;262return;263}264265ReplyPacket reply = new ReplyPacket();266267// receive reply packet from debugee268try {269log.display("Waiting for reply packet");270transport.read(reply);271log.display("Reply packet received:\n" + reply);272} catch (IOException e) {273log.complain("Unable to read reply packet:\n\t" + e);274success = false;275return;276}277278// check reply packet header279try{280log.display("Checking reply packet header");281reply.checkHeader(command.getPacketID());282} catch (BoundException e) {283log.complain("Bad header of reply packet:\n\t" + e.getMessage());284success = false;285return;286}287288// start parsing reply packet data289log.display("Parsing reply packet:");290reply.resetPosition();291292// extract boolean result value293byte isCollected = (byte)0;294try {295isCollected = reply.getByte();296log.display(" isCollected: " + isCollected);297} catch (BoundException e) {298log.complain("Unable to extract isCollected boolean value from reply packet:\n\t"299+ e.getMessage());300success = false;301return;302}303304// check for extra data in reply packet305if (!reply.isParsed()) {306log.complain("Extra trailing bytes found in reply packet at: "307+ reply.offsetString());308success = false;309}310311// check that isCollected is false (i.e. 0)312if (isCollected != 0) {313log.complain("Unexpected true isCollected value received:" + isCollected314+ " (expected" + 0);315success = false;316}317318}319320}321322323