Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ArrayReference/GetValues/getvalues001.java
41162 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.ArrayReference.GetValues;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ArrayReference.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.ArrayReference.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 = "ArrayReference.GetValues";60static final int JDWP_COMMAND_ID = JDWP.Command.ArrayReference.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 ARRAY_FIELD_NAME = getvalues001a.ARRAY_FIELD_NAME;68static final int ARRAY_LENGTH = getvalues001a.ARRAY_LENGTH;6970// first index and number of array components to get71static final int ARRAY_FIRST_INDEX = 4;72static final int ARRAY_ITEMS_COUNT = 10;7374// usual scaffold objects75ArgumentHandler argumentHandler = null;76Log log = null;77Binder binder = null;78Debugee debugee = null;79Transport transport = null;80IOPipe pipe = null;8182// test passed or not83boolean success = true;8485// -------------------------------------------------------------------8687/**88* Start test from command line.89*/90public static void main (String argv[]) {91System.exit(run(argv,System.out) + JCK_STATUS_BASE);92}9394/**95* Start JCK-compilant test.96*/97public static int run(String argv[], PrintStream out) {98return new getvalues001().runIt(argv, out);99}100101// -------------------------------------------------------------------102103/**104* Perform test execution.105*/106public int runIt(String argv[], PrintStream out) {107108// make log for debugger messages109argumentHandler = new ArgumentHandler(argv);110log = new Log(out, argumentHandler);111112// execute test and display results113try {114log.display("\n>>> Preparing debugee for testing \n");115116// launch debugee117binder = new Binder(argumentHandler, log);118log.display("Launching debugee");119debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);120transport = debugee.getTransport();121pipe = debugee.createIOPipe();122123// make debuggee ready for testing124prepareDebugee();125126// work with prepared debugee127try {128log.display("\n>>> Obtaining requred data from debugee \n");129130// query debugee for TypeID of tested class131log.display("Getting ReferenceTypeID by signature:\n"132+ " " + TESTED_CLASS_SIGNATURE);133long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);134log.display(" got classID: " + classID);135136// query debuggee for arrayID value from static field137log.display("Getting arrayID value from static field: "138+ ARRAY_FIELD_NAME);139long arrayID = queryObjectID(classID,140ARRAY_FIELD_NAME, JDWP.Tag.ARRAY);141log.display(" got arrayID: " + arrayID);142143// perform testing JDWP command144log.display("\n>>> Testing JDWP command \n");145testCommand(arrayID);146147} finally {148// quit debugee149log.display("\n>>> Finishing test \n");150quitDebugee();151}152153} catch (Failure e) {154log.complain("TEST FAILED: " + e.getMessage());155e.printStackTrace(out);156success = false;157} catch (Exception e) {158log.complain("Caught unexpected exception:\n" + e);159e.printStackTrace(out);160success = false;161}162163if (!success) {164log.complain("TEST FAILED");165return FAILED;166}167168out.println("TEST PASSED");169return PASSED;170171}172173/**174* Prepare debugee for testing and waiting for ready signal.175*/176void prepareDebugee() {177// wait for VM_INIT event from debugee178log.display("Waiting for VM_INIT event");179debugee.waitForVMInit();180181// query debugee for VM-dependent ID sizes182log.display("Querying for IDSizes");183debugee.queryForIDSizes();184185// resume initially suspended debugee186log.display("Resuming debugee VM");187debugee.resume();188189// wait for READY signal from debugee190log.display("Waiting for signal from debugee: " + READY);191String signal = pipe.readln();192log.display("Received signal from debugee: " + signal);193if (! signal.equals(READY)) {194throw new TestBug("Unexpected signal received form debugee: " + signal195+ " (expected: " + READY + ")");196}197}198199/**200* Sending debugee signal to quit and waiting for it exits.201*/202void quitDebugee() {203// send debugee signal to quit204log.display("Sending signal to debugee: " + QUIT);205pipe.println(QUIT);206207// wait for debugee exits208log.display("Waiting for debugee exits");209int code = debugee.waitFor();210211// analize debugee exit status code212if (code == JCK_STATUS_BASE + PASSED) {213log.display("Debugee PASSED with exit code: " + code);214} else {215log.complain("Debugee FAILED with exit code: " + code);216success = false;217}218}219220/**221* Query debuggee for objectID value of static class field.222*/223long queryObjectID(long classID, String fieldName, byte tag) {224// get fieledID for static field (declared in the class)225long fieldID = debugee.getClassFieldID(classID, fieldName, true);226// get value of the field227JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);228229// check that value has THREAD tag230if (value.getTag() != tag) {231throw new Failure("Wrong objectID tag received from field \"" + fieldName232+ "\": " + value.getTag() + " (expected: " + tag + ")");233}234235// extract threadID from the value236long objectID = ((Long)value.getValue()).longValue();237return objectID;238}239240/**241* Perform testing JDWP command for specified objectID.242*/243void testCommand(long arrayID) {244// create command packet245log.display("Create command packet:");246log.display("Command: " + JDWP_COMMAND_NAME);247CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);248249// add out data to the command packet250log.display(" arrayID: " + arrayID);251command.addObjectID(arrayID);252log.display(" firstIndex: " + ARRAY_FIRST_INDEX);253command.addInt(ARRAY_FIRST_INDEX);254log.display(" length: " + ARRAY_ITEMS_COUNT);255command.addInt(ARRAY_ITEMS_COUNT);256command.setLength();257258// send command packet to debugee259try {260log.display("Sending command packet:\n" + command);261transport.write(command);262} catch (IOException e) {263log.complain("Unable to send command packet:\n" + e);264success = false;265return;266}267268ReplyPacket reply = new ReplyPacket();269270// receive reply packet from debugee271try {272log.display("Waiting for reply packet");273transport.read(reply);274log.display("Reply packet received:\n" + reply);275} catch (IOException e) {276log.complain("Unable to read reply packet:\n" + e);277success = false;278return;279}280281// check reply packet header282try{283log.display("Checking reply packet header");284reply.checkHeader(command.getPacketID());285} catch (BoundException e) {286log.complain("Bad header of reply packet: " + e.getMessage());287success = false;288}289290// start parsing reply packet data291log.display("Parsing reply packet:");292reply.resetPosition();293294// extract values tag295byte tag = (byte)0;296try {297tag = reply.getByte();298log.display(" tag: " + tag);299300} catch (BoundException e) {301log.complain("Unable to extract values tag from reply packet:\n\t"302+ e.getMessage());303success = false;304}305306// check if number of values are as expected307if (tag != JDWP.Tag.INT) {308log.complain("Unexpected values tag received:" + tag309+ " (expected: " + JDWP.Tag.INT + ")");310success = false;311}312313// extract number of values314int values = 0;315try {316values = reply.getInt();317log.display(" values: " + values);318319} catch (BoundException e) {320log.complain("Unable to extract number of values from reply packet:\n\t"321+ e.getMessage());322success = false;323}324325// check if number of values are as expected326if (values < 0) {327log.complain("Negative number of values received:" + values328+ " (expected: " + ARRAY_ITEMS_COUNT + ")");329success = false;330} else if (values != ARRAY_ITEMS_COUNT) {331log.complain("Unexpected number of values received:" + values332+ " (expected: " + ARRAY_ITEMS_COUNT + ")");333success = false;334}335336// extract and check each value337for (int i = 0; i < values; i++ ) {338int index = i + ARRAY_FIRST_INDEX;339log.display(" value #" + i + " (index: " + index + ")");340341// extract value342JDWP.UntaggedValue value = null;343try {344value = reply.getUntaggedValue(JDWP.Tag.INT);345log.display(" untagged_value: " + value);346} catch (BoundException e) {347log.complain("Unable to extract " + i + " value from reply packet:\n\t"348+ e.getMessage());349success = false;350break;351}352353// check that extracted value is as expected354int intValue = ((Integer)value.getValue()).intValue();355if (intValue != index * 10) {356log.complain("Unexpected value for " + index + " component received: "357+ intValue + " (expected: " + (index * 10) + ")");358success = false;359}360}361362// check for extra data in reply packet363if (! reply.isParsed()) {364log.complain("Extra trailing bytes found in reply packet at: "365+ "0x" + reply.toHexString(reply.currentDataPosition(), 4));366success = false;367}368}369370}371372373