Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ArrayReference/SetValues/setvalues001.java
41162 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.ArrayReference.SetValues;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ArrayReference.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.ArrayReference.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 = "ArrayReference.SetValues";63static final int JDWP_COMMAND_ID = JDWP.Command.ArrayReference.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// name of the static field in the tested class with the tested object value70static final String ARRAY_FIELD_NAME = setvalues001a.ARRAY_FIELD_NAME;7172// length, first index and number of array components to get73static final int ARRAY_LENGTH = setvalues001a.ARRAY_LENGTH;74static final int ARRAY_FIRST_INDEX = setvalues001a.ARRAY_FIRST_INDEX;75static final int ARRAY_ITEMS_COUNT = setvalues001a.ARRAY_ITEMS_COUNT;7677// usual scaffold objects78ArgumentHandler argumentHandler = null;79Log log = null;80Binder binder = null;81Debugee debugee = null;82Transport transport = null;83IOPipe pipe = null;8485// test passed or not86boolean success = true;8788// -------------------------------------------------------------------8990/**91* Start test from command line.92*/93public static void main (String argv[]) {94System.exit(run(argv,System.out) + JCK_STATUS_BASE);95}9697/**98* Start JCK-compilant test.99*/100public static int run(String argv[], PrintStream out) {101return new setvalues001().runIt(argv, out);102}103104// -------------------------------------------------------------------105106/**107* Perform test execution.108*/109public int runIt(String argv[], PrintStream out) {110111// make log for debugger messages112argumentHandler = new ArgumentHandler(argv);113log = new Log(out, argumentHandler);114115// execute test and display results116try {117log.display("\n>>> Preparing debugee for testing \n");118119// launch debugee120binder = new Binder(argumentHandler, log);121log.display("Launching debugee");122debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);123transport = debugee.getTransport();124pipe = debugee.createIOPipe();125126// make debuggee ready for testing127prepareDebugee();128129// work with prepared debugee130try {131log.display("\n>>> Obtaining requred data from debugee \n");132133// query debugee for TypeID of tested class134log.display("Getting ReferenceTypeID by signature:\n"135+ " " + TESTED_CLASS_SIGNATURE);136long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);137log.display(" got classID: " + classID);138139// query debuggee for arrayID value from static field140log.display("Getting arrayID value from static field: "141+ ARRAY_FIELD_NAME);142long arrayID = queryObjectID(classID,143ARRAY_FIELD_NAME, JDWP.Tag.ARRAY);144log.display(" got arrayID: " + arrayID);145146// perform testing JDWP command147log.display("\n>>> Testing JDWP command \n");148testCommand(arrayID);149150// check confirmation from debuggee that values have been set correctly151log.display("\n>>> Checking that the values have been set correctly \n");152checkValuesChanged();153154} finally {155// quit debugee156log.display("\n>>> Finishing test \n");157quitDebugee();158}159160} catch (Failure e) {161log.complain("TEST FAILED: " + e.getMessage());162e.printStackTrace(out);163success = false;164} catch (Exception e) {165log.complain("Caught unexpected exception:\n" + e);166e.printStackTrace(out);167success = false;168}169170if (!success) {171log.complain("TEST FAILED");172return FAILED;173}174175out.println("TEST PASSED");176return PASSED;177178}179180/**181* Prepare debugee for testing and waiting for ready signal.182*/183void prepareDebugee() {184// wait for VM_INIT event from debugee185log.display("Waiting for VM_INIT event");186debugee.waitForVMInit();187188// query debugee for VM-dependent ID sizes189log.display("Querying for IDSizes");190debugee.queryForIDSizes();191192// resume initially suspended debugee193log.display("Resuming debugee VM");194debugee.resume();195196// wait for READY signal from debugee197log.display("Waiting for signal from debugee: " + READY);198String signal = pipe.readln();199log.display("Received signal from debugee: " + signal);200if (! signal.equals(READY)) {201throw new TestBug("Unexpected signal received form 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 objectID.249*/250void testCommand(long arrayID) {251// create command packet252log.display("Create command packet:");253log.display("Command: " + JDWP_COMMAND_NAME);254CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);255256// add out data to the command packet257log.display(" arrayID: " + arrayID);258command.addObjectID(arrayID);259log.display(" firstIndex: " + ARRAY_FIRST_INDEX);260command.addInt(ARRAY_FIRST_INDEX);261log.display(" values: " + ARRAY_ITEMS_COUNT);262command.addInt(ARRAY_ITEMS_COUNT);263// add new int values for array components264for (int i = ARRAY_FIRST_INDEX; i < ARRAY_FIRST_INDEX + ARRAY_ITEMS_COUNT; i++) {265int intValue = i * 100 + 1;266JDWP.UntaggedValue value = new JDWP.UntaggedValue(Integer.valueOf(intValue));267log.display(" untagged_value: " + value);268command.addUntaggedValue(value, JDWP.Tag.INT);269}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" + 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" + 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: " + e.getMessage());301success = false;302}303304// start parsing reply packet data305log.display("Parsing reply packet:");306reply.resetPosition();307308// no reply data to extract309310// check for extra data in reply packet311if (! reply.isParsed()) {312log.complain("Extra trailing bytes found in reply packet at: "313+ "0x" + reply.toHexString(reply.currentDataPosition(), 4));314success = false;315}316}317318/**319* Check confirmation from debuggee that values are changed correctly.320*/321void checkValuesChanged() {322// send debugee signal RUN323log.display("Sending signal to debugee: " + RUN);324pipe.println(RUN);325326// wait for DONE signal from debugee327log.display("Waiting for signal from debugee: " + DONE);328String signal = pipe.readln();329log.display("Received signal from debugee: " + signal);330331// check received signal332if (signal == null) {333throw new TestBug("<null> signal received from debugee: " + signal334+ " (expected: " + DONE + ")");335} else if (signal.equals(DONE)) {336log.display("All array values have been correctly set into debuggee VM");337} else if (signal.equals(ERROR)) {338log.complain("Not all array values have been correctly set into debuggee VM");339success = false;340} else {341throw new TestBug("Unexpected signal received from debugee: " + signal342+ " (expected: " + DONE + ")");343}344}345}346347348