Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ObjectReference/InvokeMethod/invokemeth001.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.InvokeMethod;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ObjectReference.InvokeMethod.33*34* See invokemeth001.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 invokemeth001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// package and classes names51static final String PACKAGE_NAME = "nsk.jdwp.ObjectReference.InvokeMethod";52static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "invokemeth001";53static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5455// tested JDWP command56static final String JDWP_COMMAND_NAME = "ObjectReference.InvokeMethod";57static final int JDWP_COMMAND_ID = JDWP.Command.ObjectReference.InvokeMethod;5859// tested class name and signature60static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedObjectClass";61static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6263// field and method names64static final String OBJECT_FIELD_NAME = "object";65static final String RESULT_FIELD_NAME = "result";66static final String TESTED_METHOD_NAME = "testedMethod";67static final String BREAKPOINT_METHOD_NAME = "run";68static final int BREAKPOINT_LINE_NUMBER = invokemeth001a.BREAKPOINT_LINE_NUMBER;6970// data for invoked method71static final int ARGUMENTS_COUNT = 1;72static final int INITIAL_VALUE = invokemeth001a.INITIAL_VALUE;73static final int ARGUMENT_VALUE = invokemeth001a.FINAL_VALUE;74static final int RETURN_VALUE = INITIAL_VALUE;75static final int INVOKE_OPTIONS = 0;7677// usual scaffold objects78ArgumentHandler argumentHandler = null;79Log log = null;80Binder binder = null;81Debugee debugee = null;82Transport transport = null;83IOPipe pipe = null;84boolean dead = false;8586// data obtained from debuggee87long classID = 0;88long threadID = 0;89long methodID = 0;90long objectID = 0;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 invokemeth001().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>>> Starting debugee \n");125126// launch debuggee127binder = new Binder(argumentHandler, log);128log.display("Launching debugee VM");129debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);130transport = debugee.getTransport();131log.display(" ... debuggee launched");132133// set timeout for debuggee responces134int waitTime = argumentHandler.getWaitTime(); // minutes135long timeout = waitTime * 60 * 1000; // milliseconds136log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");137transport.setReadTimeout(timeout);138log.display(" ... timeout set");139140// wait for VM_INIT event141log.display("Waiting for VM_INIT event");142debugee.waitForVMInit();143log.display(" ... VM_INIT event received");144145// query debugee for VM-dependent ID sizes146log.display("Querying for IDSizes");147debugee.queryForIDSizes();148log.display(" ... size of VM-dependent types adjusted");149150// run the test151runTest();152153// wait for VM_DEATH event154log.display("Waiting for VM_DEATH event");155debugee.waitForVMDeath();156log.display(" ... VM_DEATH event received");157dead = true;158159} catch (Failure e) {160log.complain("TEST FAILED: " + e.getMessage());161success = false;162} catch (Exception e) {163e.printStackTrace(out);164log.complain("Caught unexpected exception while running the test:\n\t" + e);165success = false;166} finally {167log.display("\n>>> Finishing test \n");168169// disconnect debugee and wait for its exit170if (debugee != null) {171quitDebugee();172}173}174175// check result176if (!success) {177log.complain("TEST FAILED");178return FAILED;179}180out.println("TEST PASSED");181return PASSED;182}183184/**185* Obtain required data and test JDWP command.186*/187void runTest() {188log.display("\n>>> Obtaining required data \n");189190// wait for tested class loaded on debuggee startup and obtain its classID191log.display("Waiting for class loaded:\n\t" + TESTED_CLASS_NAME);192classID = debugee.waitForClassLoaded(TESTED_CLASS_NAME, JDWP.SuspendPolicy.ALL);193log.display(" ... got classID: " + classID);194log.display("");195196// query debuggee for tested methodID197log.display("Getting tested methodID by name: " + TESTED_METHOD_NAME);198methodID = debugee.getMethodID(classID, TESTED_METHOD_NAME, true);199log.display(" ... got methodID: " + methodID);200log.display("");201202// set breakpoint and wait for debugee reached it203log.display("Waiting for breakpoint reached at: "204+ BREAKPOINT_METHOD_NAME + ":" + BREAKPOINT_LINE_NUMBER);205threadID = debugee.waitForBreakpointReached(classID,206BREAKPOINT_METHOD_NAME,207BREAKPOINT_LINE_NUMBER,208JDWP.SuspendPolicy.EVENT_THREAD);209log.display(" ... breakpoint reached with threadID: " + threadID);210log.display("");211212// get object value from static field213log.display("Getting object value from static field: " + OBJECT_FIELD_NAME);214objectID = queryObjectID(classID, OBJECT_FIELD_NAME, JDWP.Tag.OBJECT);215log.display(" ... got objectID: " + objectID);216217218// test JDWP command219log.display("\n>> Testing JDWP command \n");220testCommand();221222// check command results223if (success) {224log.display("\n>>> Checking command results \n");225checkResult();226}227228// resume debuggee after the command229log.display("Resuming debuggee");230debugee.resume();231log.display(" ... debuggee resumed");232}233234/**235* Perform testing JDWP command.236*/237void testCommand() {238// create command packet and fill requred out data239log.display("Create command packet:");240log.display("Command: " + JDWP_COMMAND_NAME);241CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);242log.display(" objectID: " + objectID);243command.addObjectID(objectID);244log.display(" threadID: " + threadID);245command.addObjectID(threadID);246log.display(" classID: " + classID);247command.addReferenceTypeID(classID);248log.display(" methodID: " + methodID);249command.addMethodID(methodID);250log.display(" arguments: " + ARGUMENTS_COUNT);251command.addInt(ARGUMENTS_COUNT);252for (int i = 0; i < ARGUMENTS_COUNT; i++) {253JDWP.Value value = new JDWP.Value(JDWP.Tag.INT, Integer.valueOf(ARGUMENT_VALUE));254log.display(" arg: " + value);255command.addValue(value);256}257log.display(" options: " + INVOKE_OPTIONS);258command.addInt(INVOKE_OPTIONS);259command.setLength();260261// send command packet to debugee262try {263log.display("Sending command packet:\n" + command);264transport.write(command);265} catch (IOException e) {266log.complain("Unable to send command packet:\n\t" + e);267success = false;268return;269}270271// receive reply packet from debugee272ReplyPacket reply = new ReplyPacket();273try {274log.display("Waiting for reply packet");275transport.read(reply);276log.display(" ... reply packet received:\n" + reply);277} catch (IOException e) {278log.complain("Unable to read reply packet for tested command:\n\t" + e);279success = false;280return;281}282283// check reply packet header284try{285log.display("Checking header of reply packet");286reply.checkHeader(command.getPacketID());287log.display(" ... packet header is correct");288} catch (BoundException e) {289log.complain("Wrong header of reply packet for tested command:\n\t"290+ e.getMessage());291success = false;292return;293}294295// start parsing reply packet data296log.display("Parsing reply packet data:");297reply.resetPosition();298299// extract return value300JDWP.Value returnValue = null;301try {302returnValue = reply.getValue();303log.display(" returnValue: " + returnValue);304} catch (BoundException e) {305log.complain("Unable to extract returnValues from reply packet:\n\t"306+ e.getMessage());307success = false;308return;309}310311// extract exception tag312JDWP.Value exception = null;313try {314exception = reply.getValue();315log.display(" exception: " + exception);316} catch (BoundException e) {317log.complain("Unable to extract exception from reply packet:\n\t"318+ e.getMessage());319success = false;320return;321}322323// check for extra data in reply packet324if (!reply.isParsed()) {325log.complain("Extra trailing bytes found in reply packet at: "326+ reply.offsetString());327success = false;328}329330log.display(" ... packed data parsed");331332// check that return value is an integer333if (returnValue.getTag() != JDWP.Tag.INT) {334log.complain("Unexpected tag of returnValue returned: " + returnValue.getTag()335+ " (expected: " + JDWP.Tag.INT + ")");336success = false;337}338339// check that return value is as expected340int intValue = ((Integer)returnValue.getValue()).intValue();341if (intValue != RETURN_VALUE) {342log.complain("Unexpected value of returnValue returned: " + intValue343+ " (expected: " + RETURN_VALUE + ")");344success = false;345}346347// check that exception value is an object348if (exception.getTag() != JDWP.Tag.OBJECT) {349log.complain("Unexpected tag of exception returned: " + exception.getTag()350+ " (expected: " + JDWP.Tag.OBJECT + ")");351success = false;352}353354// check that exception object is null355long exceptionID = ((Long)exception.getValue()).longValue();356if (exceptionID != 0) {357log.complain("Non-null exception object returned: " + exceptionID358+ " (expected: " + 0 + ")");359success = false;360}361}362363/**364* Check result of the tested JDWP command.365*/366void checkResult() {367// query debuggee for result value from a static field368log.display("Getting result value from static field: " + RESULT_FIELD_NAME);369int result = queryInt(classID, RESULT_FIELD_NAME, JDWP.Tag.INT);370log.display(" ... got result: " + result);371372// check if the result value is changed as expected373if (result != ARGUMENT_VALUE) {374log.complain("Method has not been really invoked: \n\t"375+ "variable not changed by the method: " + result376+ " (expected: " + ARGUMENT_VALUE + ")");377success = false;378} else {379log.display("Method has been really invoked: \n\t"380+ " variable changed by the method: " + result381+ " (expected: " + ARGUMENT_VALUE + ")");382}383}384385/**386* Query debuggee for value of static field of the class.387*/388JDWP.Value queryFieldValue(long classID, String fieldName, byte tag) {389// get fieledID for static field (declared in the class)390long fieldID = debugee.getClassFieldID(classID, fieldName, true);391// get value of the field392JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);393394// check that value has THREAD tag395if (value.getTag() != tag) {396log.complain("unexpedted value tag returned from debuggee: " + value.getTag()397+ " (expected: " + tag + ")");398throw new Failure("Error occured while getting value from static field: "399+ fieldName);400}401402return value;403}404405/**406* Query debuggee for objectID value of static field of the class.407*/408long queryObjectID(long classID, String fieldName, byte tag) {409JDWP.Value value = queryFieldValue(classID, fieldName, tag);410long objectID = ((Long)value.getValue()).longValue();411return objectID;412}413414/**415* Query debuggee for int value of static field of the class.416*/417int queryInt(long classID, String fieldName, byte tag) {418JDWP.Value value = queryFieldValue(classID, fieldName, tag);419int intValue = ((Integer)value.getValue()).intValue();420return intValue;421}422423/**424* Sending debugee signal to quit and waiting for it exits.425*/426void quitDebugee() {427if (debugee == null)428return;429430// disconnect debuggee if not dead431if (!dead) {432try {433log.display("Disconnecting debuggee");434debugee.dispose();435log.display(" ... debuggee disconnected");436} catch (Failure e) {437log.display("Failed to finally dispose debuggee:\n\t" + e.getMessage());438}439}440441// wait for debugee exits442log.display("Waiting for debuggee exits");443int code = debugee.waitFor();444log.display(" ... debuggee finished with exit code: " + code);445446// analize debuggee exit status code447if (code != JCK_STATUS_BASE + PASSED) {448log.complain("Debuggee FAILED with exit code: " + code);449}450}451452}453454455