Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ObjectReference/EnableCollection/enablecol001.java
68396 views
/*1* Copyright (c) 2001, 2021, 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.EnableCollection;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ObjectReference.EnableCollection.33*34* See enablecol001.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 enablecol001 {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.EnableCollection";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "enablecol001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "ObjectReference.EnableCollection";62static final int JDWP_COMMAND_ID = JDWP.Command.ObjectReference.EnableCollection;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 = enablecol001a.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 enablecol001().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// query debuggee to disable garbage collection for this object141log.display("Disabling garbage collection for objectID: " + objectID);142disableObjectCollection(objectID);143144// perform testing JDWP command145log.display("\n>>> Testing JDWP EnableCollection command after JDWP DisableCollection\n");146testCommand(objectID);147148// perform testing JDWP command149log.display("\n>>> Testing JDWP EnableCollection command with no JDWP DisableCollection\n");150testCommand(objectID);151152} finally {153log.display("\n>>> Finishing test \n");154155// quit debugee156quitDebugee();157}158159} catch (Failure e) {160log.complain("TEST FAILED: " + e.getMessage());161success = false;162} catch (Exception e) {163e.printStackTrace(out);164log.complain("Caught unexpected exception while running the test:\n\t" + e);165success = false;166}167168if (!success) {169log.complain("TEST FAILED");170return FAILED;171}172173out.println("TEST PASSED");174return PASSED;175176}177178/**179* Prepare debugee for testing and waiting for ready signal.180*/181void prepareDebugee() {182// wait for VM_INIT event from debugee183log.display("Waiting for VM_INIT event");184debugee.waitForVMInit();185186// query debugee for VM-dependent ID sizes187log.display("Querying for IDSizes");188debugee.queryForIDSizes();189190// resume initially suspended debugee191log.display("Resuming debugee VM");192debugee.resume();193194// wait for READY signal from debugee195log.display("Waiting for signal from debugee: " + READY);196String signal = pipe.readln();197log.display("Received signal from debugee: " + signal);198if (signal == null) {199throw new TestBug("Null signal received from debugee: " + signal200+ " (expected: " + READY + ")");201} else if (signal.equals(ERROR)) {202throw new TestBug("Debugee was not able to start tested thread"203+ " (received signal: " + signal + ")");204} else if (!signal.equals(READY)) {205throw new TestBug("Unexpected signal received from debugee: " + signal206+ " (expected: " + READY + ")");207}208}209210/**211* Sending debugee signal to quit and waiting for it exits.212*/213void quitDebugee() {214// send debugee signal to quit215log.display("Sending signal to debugee: " + QUIT);216pipe.println(QUIT);217218// wait for debugee exits219log.display("Waiting for debugee exits");220int code = debugee.waitFor();221222// analize debugee exit status code223if (code == JCK_STATUS_BASE + PASSED) {224log.display("Debugee PASSED with exit code: " + code);225} else {226log.complain("Debugee FAILED with exit code: " + code);227success = false;228}229}230231/**232* Query debuggee for objectID value of static class field.233*/234long queryObjectID(long classID, String fieldName, byte tag) {235// get fieledID for static field (declared in the class)236long fieldID = debugee.getClassFieldID(classID, fieldName, true);237// get value of the field238JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);239240// check that value has THREAD tag241if (value.getTag() != tag) {242throw new Failure("Wrong objectID tag received from field \"" + fieldName243+ "\": " + value.getTag() + " (expected: " + tag + ")");244}245246// extract threadID from the value247long objectID = ((Long)value.getValue()).longValue();248return objectID;249}250251/**252* Perform testing JDWP command for specified threadID.253*/254void disableObjectCollection(long objectID) {255CommandPacket command = new CommandPacket(JDWP.Command.ObjectReference.DisableCollection);256command.addObjectID(objectID);257ReplyPacket reply = debugee.receiveReplyFor(command);258}259260/**261* Perform testing JDWP command for specified threadID.262*/263void testCommand(long objectID) {264// create command packet and fill requred out data265log.display("Create command packet:");266log.display("Command: " + JDWP_COMMAND_NAME);267CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);268log.display(" objectID: " + objectID);269command.addObjectID(objectID);270command.setLength();271272// send command packet to debugee273try {274log.display("Sending command packet:\n" + command);275transport.write(command);276} catch (IOException e) {277log.complain("Unable to send command packet:\n\t" + e);278success = false;279return;280}281282ReplyPacket reply = new ReplyPacket();283284// receive reply packet from debugee285try {286log.display("Waiting for reply packet");287transport.read(reply);288log.display("Reply packet received:\n" + reply);289} catch (IOException e) {290log.complain("Unable to read reply packet:\n\t" + e);291success = false;292return;293}294295// check reply packet header296try{297log.display("Checking reply packet header");298reply.checkHeader(command.getPacketID());299} catch (BoundException e) {300log.complain("Bad header of reply packet:\n\t" + e.getMessage());301success = false;302return;303}304305// start parsing reply packet data306log.display("Parsing reply packet:");307reply.resetPosition();308309// check for extra data in reply packet310if (!reply.isParsed()) {311log.complain("Extra trailing bytes found in reply packet at: "312+ reply.offsetString());313success = false;314}315316}317318}319320321