Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ObjectReference/SetValues/setvalues001.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.SetValues;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ObjectReference.SetValues.33*34* See setvalues001.README for description of test execution.35*36* Test is executed by invoking method runIt().37* JDWP command is tested in method testCommand().38*39* @see #runIt()40* @see #testCommand()41*/42public class setvalues001 {4344// exit status constants45static final int JCK_STATUS_BASE = 95;46static final int PASSED = 0;47static final int FAILED = 2;4849// communication signals constants50static final String READY = "ready";51static final String RUN = "run";52static final String DONE = "done";53static final String ERROR = "error";54static final String QUIT = "quit";5556// package and classes names constants57static final String PACKAGE_NAME = "nsk.jdwp.ObjectReference.SetValues";58static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "setvalues001";59static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";6061// tested JDWP command constants62static final String JDWP_COMMAND_NAME = "ObjectReference.SetValues";63static final int JDWP_COMMAND_ID = JDWP.Command.ObjectReference.SetValues;6465// tested class name and signature constants66static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";67static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6869// target values class name and signature constants70static final String TARGET_VALUES_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TargetValuesClass";71static final String TARGET_VALUES_CLASS_SIGNATURE = "L" + TARGET_VALUES_CLASS_NAME.replace('.', '/') + ";";7273// name and siagnature of a class with static field with the tested object value74static final String OBJECT_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "ObjectClass";75static final String OBJECT_CLASS_SIGNATURE = "L" + OBJECT_CLASS_NAME.replace('.', '/') + ";";7677// name of the static field in the tested class with the tested object value78static final String OBJECT_FIELD_NAME = setvalues001a.OBJECT_FIELD_NAME;7980// usual scaffold objects81ArgumentHandler argumentHandler = null;82Log log = null;83Binder binder = null;84Debugee debugee = null;85Transport transport = null;86IOPipe pipe = null;8788// test passed or not89boolean success = true;9091// -------------------------------------------------------------------9293/**94* Start test from command line.95*/96public static void main (String argv[]) {97System.exit(run(argv,System.out) + JCK_STATUS_BASE);98}99100/**101* Start JCK-compilant test.102*/103public static int run(String argv[], PrintStream out) {104return new setvalues001().runIt(argv, out);105}106107// -------------------------------------------------------------------108109/**110* Perform test execution.111*/112public int runIt(String argv[], PrintStream out) {113114// make log for debugger messages115argumentHandler = new ArgumentHandler(argv);116log = new Log(out, argumentHandler);117118// execute test and display results119try {120log.display("\n>>> Preparing debugee for testing \n");121122// launch debugee123binder = new Binder(argumentHandler, log);124log.display("Launching debugee");125debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);126transport = debugee.getTransport();127pipe = debugee.createIOPipe();128129// make debuggee ready for testing130prepareDebugee();131132// work with prepared debugee133try {134log.display("\n>>> Obtaining requred data from debugee \n");135136// query debugee for classID for the class with target values137log.display("Getting classID for class with target values by signature:\n"138+ " " + TARGET_VALUES_CLASS_SIGNATURE);139long targetValuesClassID =140debugee.getReferenceTypeID(TARGET_VALUES_CLASS_SIGNATURE);141log.display(" got classID: " + targetValuesClassID);142143// query debugee for fieldIDs of the class static fields144log.display("Getting fieldIDs for static fields of the class");145long targetValuesFieldIDs[] = queryClassFieldIDs(targetValuesClassID);146log.display(" got fields: " + targetValuesFieldIDs.length);147int count = targetValuesFieldIDs.length;148149// query debugee for values of the fields150log.display("Getting values of the static fields");151JDWP.Value targetValues[] =152queryClassFieldValues(targetValuesClassID, targetValuesFieldIDs);153log.display(" got values: " + targetValues.length);154if (targetValues.length != count) {155throw new Failure("Unexpected number of static fields values received: "156+ targetValues.length + "(expected: " + count + ")");157}158159// query debugee for classID of the tested class160log.display("Getting tested classID by signature:\n"161+ " " + TESTED_CLASS_SIGNATURE);162long testedClassID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);163log.display(" got classID: " + testedClassID);164165// query debugee for fieldIDs of tested class fields166log.display("Getting fieldIDs for tested fields of the tested class");167long testedFieldIDs[] = queryClassFieldIDs(testedClassID);168log.display(" got fields: " + testedFieldIDs.length);169if (testedFieldIDs.length != count) {170throw new Failure("Unexpected number of fields of tested class received: "171+ testedFieldIDs.length + "(expected: " + count + ")");172}173174// query debugee for classID of the object class175log.display("Getting object classID by signature:\n"176+ " " + OBJECT_CLASS_SIGNATURE);177long classID = debugee.getReferenceTypeID(OBJECT_CLASS_SIGNATURE);178log.display(" got classID: " + classID);179180// query debuggee for objectID value from static field181log.display("Getting objectID value from static field: "182+ OBJECT_FIELD_NAME);183long objectID = queryObjectID(classID,184OBJECT_FIELD_NAME, JDWP.Tag.OBJECT);185log.display(" got objectID: " + objectID);186187// perform testing JDWP command188log.display("\n>>> Testing JDWP command \n");189testCommand(objectID, testedFieldIDs, targetValues);190191// check confirmation from debuggee that values have been set properly192log.display("\n>>> Checking that the values have been set properly \n");193checkValuesChanged();194195} finally {196// quit debugee197log.display("\n>>> Finishing test \n");198quitDebugee();199}200201} catch (Failure e) {202log.complain("TEST FAILED: " + e.getMessage());203e.printStackTrace(out);204success = false;205} catch (Exception e) {206log.complain("Caught unexpected exception:\n" + e);207e.printStackTrace(out);208success = false;209}210211if (!success) {212log.complain("TEST FAILED");213return FAILED;214}215216out.println("TEST PASSED");217return PASSED;218219}220221/**222* Prepare debugee for testing and waiting for ready signal.223*/224void prepareDebugee() {225// wait for VM_INIT event from debugee226log.display("Waiting for VM_INIT event");227debugee.waitForVMInit();228229// query debugee for VM-dependent ID sizes230log.display("Querying for IDSizes");231debugee.queryForIDSizes();232233// resume initially suspended debugee234log.display("Resuming debugee VM");235debugee.resume();236237// wait for READY signal from debugee238log.display("Waiting for signal from debugee: " + READY);239String signal = pipe.readln();240log.display("Received signal from debugee: " + signal);241if (! signal.equals(READY)) {242throw new TestBug("Unexpected signal received form debugee: " + signal243+ " (expected: " + READY + ")");244}245}246247/**248* Sending debugee signal to quit and waiting for it exits.249*/250void quitDebugee() {251// send debugee signal to quit252log.display("Sending signal to debugee: " + QUIT);253pipe.println(QUIT);254255// wait for debugee exits256log.display("Waiting for debugee exits");257int code = debugee.waitFor();258259// analize debugee exit status code260if (code == JCK_STATUS_BASE + PASSED) {261log.display("Debugee PASSED with exit code: " + code);262} else {263log.complain("Debugee FAILED with exit code: " + code);264success = false;265}266}267268/**269* Query debugee for fieldID's of the class static fields.270*/271long[] queryClassFieldIDs(long classID) {272// compose ReferenceType.Fields command packet273CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.Fields);274command.addReferenceTypeID(classID);275command.setLength();276277// send the command and receive reply278ReplyPacket reply = debugee.receiveReplyFor(command);279280// extract fieldIDs from the reply packet281try {282reply.resetPosition();283284int declared = reply.getInt();285long[] fieldIDs = new long[declared];286287int j = 0;288for (int i = 0; i < declared; i++ ) {289long fieldID = reply.getFieldID();290String name = reply.getString();291String signature = reply.getString();292int modBits = reply.getInt();293294fieldIDs[i] = fieldID;295}296return fieldIDs;297} catch (BoundException e) {298log.complain("Unable to parse reply packet for ReferenceType.Fields command:\n\t"299+ e);300log.complain("Received reply packet:\n"301+ reply);302throw new Failure("Error occured while getting static fieldIDs for classID: " + classID);303}304}305306/**307* Query debugee for values of the class fields.308*/309JDWP.Value[] queryClassFieldValues(long classID, long fieldIDs[]) {310// compose ReferenceType.Fields command packet311int count = fieldIDs.length;312CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.GetValues);313command.addReferenceTypeID(classID);314command.addInt(count);315for (int i = 0; i < count; i++) {316command.addFieldID(fieldIDs[i]);317}318command.setLength();319320// send the command and receive reply321ReplyPacket reply = debugee.receiveReplyFor(command);322323// extract values from the reply packet324try {325reply.resetPosition();326327int valuesCount = reply.getInt();328JDWP.Value values[] = new JDWP.Value[valuesCount];329for (int i = 0; i < valuesCount; i++ ) {330JDWP.Value value = reply.getValue();331values[i] = value;332}333return values;334} catch (BoundException e) {335log.complain("Unable to parse reply packet for ReferenceType.GetValues command:\n\t"336+ e);337log.complain("Received reply packet:\n"338+ reply);339throw new Failure("Error occured while getting static fields values for classID: " + classID);340}341}342343/**344* Query debuggee for objectID value of static class field.345*/346long queryObjectID(long classID, String fieldName, byte tag) {347// get fieledID for static field (declared in the class)348long fieldID = debugee.getClassFieldID(classID, fieldName, true);349// get value of the field350JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);351352// check that value has THREAD tag353if (value.getTag() != tag) {354throw new Failure("Wrong objectID tag received from field \"" + fieldName355+ "\": " + value.getTag() + " (expected: " + tag + ")");356}357358// extract threadID from the value359long objectID = ((Long)value.getValue()).longValue();360return objectID;361}362363/**364* Perform testing JDWP command for specified classID.365*/366void testCommand(long objectID, long fieldIDs[], JDWP.Value values[]) {367int count = fieldIDs.length;368369// create command packet370log.display("Create command packet:");371log.display("Command: " + JDWP_COMMAND_NAME);372CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);373374// add out data to the command packet375log.display(" objectID: " + objectID);376command.addObjectID(objectID);377log.display(" values: " + count);378command.addInt(count);379for (int i = 0; i < count; i++) {380log.display(" field #" + i +":");381log.display(" fieldID: " + fieldIDs[i]);382command.addFieldID(fieldIDs[i]);383384JDWP.Value value = values[i];385JDWP.UntaggedValue untaggedValue =386new JDWP.UntaggedValue(value.getValue());387log.display(" untagged_value: " + untaggedValue.getValue());388command.addUntaggedValue(untaggedValue, value.getTag());389}390command.setLength();391392// send command packet to debugee393try {394log.display("Sending command packet:\n" + command);395transport.write(command);396} catch (IOException e) {397log.complain("Unable to send command packet:\n" + e);398success = false;399return;400}401402ReplyPacket reply = new ReplyPacket();403404// receive reply packet from debugee405try {406log.display("Waiting for reply packet");407transport.read(reply);408log.display("Reply packet received:\n" + reply);409} catch (IOException e) {410log.complain("Unable to read reply packet:\n" + e);411success = false;412return;413}414415// check reply packet header416try{417log.display("Checking reply packet header");418reply.checkHeader(command.getPacketID());419} catch (BoundException e) {420log.complain("Bad header of reply packet: " + e.getMessage());421success = false;422}423424// start parsing reply packet data425log.display("Parsing reply packet:");426reply.resetPosition();427428// no data to extract429430// check for extra data in reply packet431if (! reply.isParsed()) {432log.complain("Extra trailing bytes found in reply packet at: "433+ "0x" + reply.toHexString(reply.currentDataPosition(), 4));434success = false;435}436}437438/**439* Check confiramtion from debuggee that values are changed.440*/441void checkValuesChanged() {442// send debugee signal RUN443log.display("Sending signal to debugee: " + RUN);444pipe.println(RUN);445446// wait for DONE signal from debugee447log.display("Waiting for signal from debugee: " + DONE);448String signal = pipe.readln();449log.display("Received signal from debugee: " + signal);450451// check received signal452if (signal == null) {453throw new TestBug("<null> signal received from debugee: " + signal454+ " (expected: " + DONE + ")");455} else if (signal.equals(DONE)) {456log.display("All fields values have been correctly set into debuggee VM");457} else if (signal.equals(ERROR)) {458log.complain("Not all fields values have been correctly set into debuggee VM");459success = false;460} else {461throw new TestBug("Unexpected signal received from debugee: " + signal462+ " (expected: " + DONE + ")");463}464}465466}467468469