Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ObjectReference/GetValues/getvalues001.java
41161 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.GetValues;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ObjectReference.GetValues.33*34* See getvalues001.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 getvalues001 {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 QUIT = "quit";5253// package and classes names constants54static final String PACKAGE_NAME = "nsk.jdwp.ObjectReference.GetValues";55static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "getvalues001";56static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5758// tested JDWP command constants59static final String JDWP_COMMAND_NAME = "ObjectReference.GetValues";60static final int JDWP_COMMAND_ID = JDWP.Command.ObjectReference.GetValues;6162// tested class name and signature constants63static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";64static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6566// name of the static field in the tested class with the tested object value67static final String OBJECT_FIELD_NAME = getvalues001a.OBJECT_FIELD_NAME;6869// names and expected values of the tested fields70static final Object fields [][] = {71{ "booleanValue", "boolean", Boolean.valueOf(true), "own"},72{ "byteValue", "byte", Byte.valueOf((byte)0x0F), "own"},73{ "charValue", "char", Character.valueOf('Z'), "own"},74{ "intValue", "int", Integer.valueOf(100), "own"},75{ "shortValue", "short", Short.valueOf((short)10), "own"},76{ "longValue", "long", Long.valueOf((long)1000000), "own"},77{ "floatValue", "float", Float.valueOf((float)3.14), "own"},78{ "doubleValue", "double", Double.valueOf((double)2.8e-12), "own"},79{ "objectValue", "objectID", Long.valueOf((long)0), "own"},8081};82static final int FIELDS_COUNT = fields.length;8384// usual scaffold objects85ArgumentHandler argumentHandler = null;86Log log = null;87Binder binder = null;88Debugee debugee = null;89Transport transport = null;90IOPipe pipe = null;9192// test passed or not93boolean success = true;9495// -------------------------------------------------------------------9697/**98* Start test from command line.99*/100public static void main (String argv[]) {101System.exit(run(argv,System.out) + JCK_STATUS_BASE);102}103104/**105* Start JCK-compilant test.106*/107public static int run(String argv[], PrintStream out) {108return new getvalues001().runIt(argv, out);109}110111// -------------------------------------------------------------------112113/**114* Perform test execution.115*/116public int runIt(String argv[], PrintStream out) {117118// make log for debugger messages119argumentHandler = new ArgumentHandler(argv);120log = new Log(out, argumentHandler);121122// execute test and display results123try {124log.display("\n>>> Preparing debugee for testing \n");125126// launch debugee127binder = new Binder(argumentHandler, log);128log.display("Launching debugee");129debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);130transport = debugee.getTransport();131pipe = debugee.createIOPipe();132133// make debuggee ready for testing134prepareDebugee();135136// work with prepared debugee137try {138log.display("\n>>> Obtaining requred data from debugee \n");139140// query debugee for TypeID of tested class141log.display("Getting ReferenceTypeID by signature:\n"142+ " " + TESTED_CLASS_SIGNATURE);143long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);144log.display(" got classID: " + classID);145146// query debuggee for objectID value from static field147log.display("Getting objectID value from static field: "148+ OBJECT_FIELD_NAME);149long objectID = queryObjectID(classID,150OBJECT_FIELD_NAME, JDWP.Tag.OBJECT);151log.display(" got objectID: " + objectID);152153// query debugee for fieldIDs of tested class static fields154log.display("Getting fieldIDs the tested class with the tested values");155long fieldIDs[] = queryClassFieldIDs(classID);156157// perform testing JDWP command158log.display("\n>>> Testing JDWP command \n");159testCommand(objectID, fieldIDs);160161} finally {162// quit debugee163log.display("\n>>> Finishing test \n");164quitDebugee();165}166167} catch (Failure e) {168log.complain("TEST FAILED: " + e.getMessage());169e.printStackTrace(out);170success = false;171} catch (Exception e) {172log.complain("Caught unexpected exception:\n" + e);173e.printStackTrace(out);174success = false;175}176177if (!success) {178log.complain("TEST FAILED");179return FAILED;180}181182out.println("TEST PASSED");183return PASSED;184185}186187/**188* Prepare debugee for testing and waiting for ready signal.189*/190void prepareDebugee() {191// wait for VM_INIT event from debugee192log.display("Waiting for VM_INIT event");193debugee.waitForVMInit();194195// query debugee for VM-dependent ID sizes196log.display("Querying for IDSizes");197debugee.queryForIDSizes();198199// resume initially suspended debugee200log.display("Resuming debugee VM");201debugee.resume();202203// wait for READY signal from debugee204log.display("Waiting for signal from debugee: " + READY);205String signal = pipe.readln();206log.display("Received signal from debugee: " + signal);207if (! signal.equals(READY)) {208throw new TestBug("Unexpected signal received form debugee: " + signal209+ " (expected: " + READY + ")");210}211}212213/**214* Sending debugee signal to quit and waiting for it exits.215*/216void quitDebugee() {217// send debugee signal to quit218log.display("Sending signal to debugee: " + QUIT);219pipe.println(QUIT);220221// wait for debugee exits222log.display("Waiting for debugee exits");223int code = debugee.waitFor();224225// analize debugee exit status code226if (code == JCK_STATUS_BASE + PASSED) {227log.display("Debugee PASSED with exit code: " + code);228} else {229log.complain("Debugee FAILED with exit code: " + code);230success = false;231}232}233234/**235* Query debuggee for objectID value of static class field.236*/237long queryObjectID(long classID, String fieldName, byte tag) {238// get fieledID for static field (declared in the class)239long fieldID = debugee.getClassFieldID(classID, fieldName, true);240// get value of the field241JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);242243// check that value has THREAD tag244if (value.getTag() != tag) {245throw new Failure("Wrong objectID tag received from field \"" + fieldName246+ "\": " + value.getTag() + " (expected: " + tag + ")");247}248249// extract threadID from the value250long objectID = ((Long)value.getValue()).longValue();251return objectID;252}253254/**255* Query debugee for fieldIDs and them into nested_classesIDs array.256*/257long[] queryClassFieldIDs(long typeID) {258// create array for expected filedIDs259long fieldIDs[] = new long[FIELDS_COUNT];260for (int i = 0; i < FIELDS_COUNT; i++) {261fieldIDs[i] = 0;262}263264// obtain requested fieldIDs form debuggee265int count = 0;266try {267CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.Fields);268command.addReferenceTypeID(typeID);269command.setLength();270271ReplyPacket reply = debugee.receiveReplyFor(command);272reply.resetPosition();273274long declared = reply.getInt();275if (declared < FIELDS_COUNT) {276throw new Failure("Too few fields of the tested class returned: " + declared277+ " (expected: at least " + FIELDS_COUNT + ")");278}279280for (int i = 0; i < declared; i++ ) {281long fieldID = reply.getFieldID();282String name = reply.getString();283String signature = reply.getString();284int modBits = reply.getInt();285286for (int j = 0; j < FIELDS_COUNT; j++) {287if (fields[j][0].equals(name)) {288fieldIDs[j] = fieldID;289break;290}291}292}293294if (!reply.isParsed()) {295throw new Failure("Extra trailing bytes found in the reply packet at: "296+ reply.currentPosition());297}298299} catch (BoundException e) {300throw new Failure("Unable to extract field IDs from the reply packet:\n"301+ e.getMessage());302}303304return fieldIDs;305}306307/**308* Extract and check i-th value from the reply packet.309*/310void checkValue(int i, JDWP.Value value) {311if (!fields[i][2].equals(value.getValue())) {312log.complain("Unexpected value for " + i + " field received: " + value313+ " (expected: " + fields[i][2] + ")");314success = false;315}316}317318/**319* Perform testing JDWP command for specified objectID.320*/321void testCommand(long objectID, long fieldIDs[]) {322int count = fieldIDs.length;323324// create command packet325log.display("Create command packet:");326log.display("Command: " + JDWP_COMMAND_NAME);327CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);328329// add out data to the command packet330log.display(" objectID: " + objectID);331command.addObjectID(objectID);332log.display(" fields: " + count);333command.addInt(count);334for (int i = 0; i < count; i++) {335log.display(" #" + i +": fieldID: " + fieldIDs[i]);336command.addFieldID(fieldIDs[i]);337}338command.setLength();339340// send command packet to debugee341try {342log.display("Sending command packet:\n" + command);343transport.write(command);344} catch (IOException e) {345log.complain("Unable to send command packet:\n" + e);346success = false;347return;348}349350ReplyPacket reply = new ReplyPacket();351352// receive reply packet from debugee353try {354log.display("Waiting for reply packet");355transport.read(reply);356log.display("Reply packet received:\n" + reply);357} catch (IOException e) {358log.complain("Unable to read reply packet:\n" + e);359success = false;360return;361}362363// check reply packet header364try{365log.display("Checking reply packet header");366reply.checkHeader(command.getPacketID());367} catch (BoundException e) {368log.complain("Bad header of reply packet: " + e.getMessage());369success = false;370}371372// start parsing reply packet data373log.display("Parsing reply packet:");374reply.resetPosition();375376// extract and check number of values377int values = 0;378try {379values = reply.getInt();380log.display(" values: " + values);381382} catch (BoundException e) {383log.complain("Unable to extract number of values form reply packet:\n" + e.getMessage());384success = false;385}386387// check if number of values are as expected388if (values < 0) {389log.complain("Negative number of values received:" + values390+ " (expected: " + count + ")");391success = false;392} else if (values != count) {393log.complain("Unexpected number of values received:" + values394+ " (expected: " + count + ")");395success = false;396}397398// extract and check each value399for (int i = 0; i < values; i++ ) {400log.display(" value #" + i + " (field: " + fields[i][0] + ")");401402// extract value403JDWP.Value value = null;404try {405value = reply.getValue();406log.display(" value: " + value);407} catch (BoundException e) {408log.complain("Unable to extract " + i + " value:\n" + e.getMessage());409success = false;410break;411}412413// extract and check value by known type tag414checkValue(i, value);415}416417// check for extra data in reply packet418if (! reply.isParsed()) {419log.complain("Extra trailing bytes found in reply packet at: "420+ "0x" + reply.toHexString(reply.currentDataPosition(), 4));421success = false;422}423}424425}426427428