Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ObjectReference/EnableCollection/enablecol001.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.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 command \n");146testCommand(objectID);147148} finally {149log.display("\n>>> Finishing test \n");150151// quit debugee152quitDebugee();153}154155} catch (Failure e) {156log.complain("TEST FAILED: " + e.getMessage());157success = false;158} catch (Exception e) {159e.printStackTrace(out);160log.complain("Caught unexpected exception while running the test:\n\t" + e);161success = false;162}163164if (!success) {165log.complain("TEST FAILED");166return FAILED;167}168169out.println("TEST PASSED");170return PASSED;171172}173174/**175* Prepare debugee for testing and waiting for ready signal.176*/177void prepareDebugee() {178// wait for VM_INIT event from debugee179log.display("Waiting for VM_INIT event");180debugee.waitForVMInit();181182// query debugee for VM-dependent ID sizes183log.display("Querying for IDSizes");184debugee.queryForIDSizes();185186// resume initially suspended debugee187log.display("Resuming debugee VM");188debugee.resume();189190// wait for READY signal from debugee191log.display("Waiting for signal from debugee: " + READY);192String signal = pipe.readln();193log.display("Received signal from debugee: " + signal);194if (signal == null) {195throw new TestBug("Null signal received from debugee: " + signal196+ " (expected: " + READY + ")");197} else if (signal.equals(ERROR)) {198throw new TestBug("Debugee was not able to start tested thread"199+ " (received signal: " + signal + ")");200} else if (!signal.equals(READY)) {201throw new TestBug("Unexpected signal received from debugee: " + signal202+ " (expected: " + READY + ")");203}204}205206/**207* Sending debugee signal to quit and waiting for it exits.208*/209void quitDebugee() {210// send debugee signal to quit211log.display("Sending signal to debugee: " + QUIT);212pipe.println(QUIT);213214// wait for debugee exits215log.display("Waiting for debugee exits");216int code = debugee.waitFor();217218// analize debugee exit status code219if (code == JCK_STATUS_BASE + PASSED) {220log.display("Debugee PASSED with exit code: " + code);221} else {222log.complain("Debugee FAILED with exit code: " + code);223success = false;224}225}226227/**228* Query debuggee for objectID value of static class field.229*/230long queryObjectID(long classID, String fieldName, byte tag) {231// get fieledID for static field (declared in the class)232long fieldID = debugee.getClassFieldID(classID, fieldName, true);233// get value of the field234JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);235236// check that value has THREAD tag237if (value.getTag() != tag) {238throw new Failure("Wrong objectID tag received from field \"" + fieldName239+ "\": " + value.getTag() + " (expected: " + tag + ")");240}241242// extract threadID from the value243long objectID = ((Long)value.getValue()).longValue();244return objectID;245}246247/**248* Perform testing JDWP command for specified threadID.249*/250void disableObjectCollection(long objectID) {251CommandPacket command = new CommandPacket(JDWP.Command.ObjectReference.DisableCollection);252command.addObjectID(objectID);253ReplyPacket reply = debugee.receiveReplyFor(command);254}255256/**257* Perform testing JDWP command for specified threadID.258*/259void testCommand(long objectID) {260// create command packet and fill requred out data261log.display("Create command packet:");262log.display("Command: " + JDWP_COMMAND_NAME);263CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);264log.display(" objectID: " + objectID);265command.addObjectID(objectID);266command.setLength();267268// send command packet to debugee269try {270log.display("Sending command packet:\n" + command);271transport.write(command);272} catch (IOException e) {273log.complain("Unable to send command packet:\n\t" + e);274success = false;275return;276}277278ReplyPacket reply = new ReplyPacket();279280// receive reply packet from debugee281try {282log.display("Waiting for reply packet");283transport.read(reply);284log.display("Reply packet received:\n" + reply);285} catch (IOException e) {286log.complain("Unable to read reply packet:\n\t" + e);287success = false;288return;289}290291// check reply packet header292try{293log.display("Checking reply packet header");294reply.checkHeader(command.getPacketID());295} catch (BoundException e) {296log.complain("Bad header of reply packet:\n\t" + e.getMessage());297success = false;298return;299}300301// start parsing reply packet data302log.display("Parsing reply packet:");303reply.resetPosition();304305// check for extra data in reply packet306if (!reply.isParsed()) {307log.complain("Extra trailing bytes found in reply packet at: "308+ reply.offsetString());309success = false;310}311312}313314}315316317