Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/Method/VariableTable/vartable001.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.Method.VariableTable;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: Method.VariableTable.33*34* See vartable001.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 vartable001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// communication signals constants51static final String READY = "ready";52static final String QUIT = "quit";5354// package and classes names constants55static final String PACKAGE_NAME = "nsk.jdwp.Method.VariableTable";56static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "vartable001";57static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5859// tested JDWP command constants60static final String JDWP_COMMAND_NAME = "Method.VariableTable";61static final int JDWP_COMMAND_ID = JDWP.Command.Method.VariableTable;6263// tested class name and signature constants64static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";65static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6667// tested types signature conatants68static final String OBJECT_CLASS_SIGNATURE = "Ljava/lang/Object;";69static final String STRING_CLASS_SIGNATURE = "Ljava/lang/String;";707172// tested method name constant73static final String TESTED_METHOD_NAME = "testedMethod";7475// list of tested variables names and signatures76static final String variablesList[][] = {77// synthetic method arguments78{"this", TESTED_CLASS_SIGNATURE},79// method arguments80{"booleanArgument", "Z"},81{"byteArgument", "B"},82{"charArgument", "C"},83{"shortArgument", "S"},84{"intArgument", "I"},85{"longArgument", "J"},86{"floatArgument", "F"},87{"doubleArgument", "D"},88{"objectArgument", OBJECT_CLASS_SIGNATURE},89{"stringArgument", STRING_CLASS_SIGNATURE},90// local variables91{"booleanLocal", "Z"},92{"byteLocal", "B"},93{"charLocal", "C"},94{"shortLocal", "S"},95{"intLocal", "I"},96{"longLocal", "J"},97{"floatLocal", "F"},98{"doubleLocal", "D"},99{"objectLocal", OBJECT_CLASS_SIGNATURE},100{"stringLocal", STRING_CLASS_SIGNATURE}101};102static final int variablesCount = variablesList.length;103104// usual scaffold objects105ArgumentHandler argumentHandler = null;106Log log = null;107Binder binder = null;108Debugee debugee = null;109Transport transport = null;110IOPipe pipe = null;111112// test passed or not113boolean success = true;114115// -------------------------------------------------------------------116117/**118* Start test from command line.119*/120public static void main (String argv[]) {121System.exit(run(argv,System.out) + JCK_STATUS_BASE);122}123124/**125* Start JCK-compilant test.126*/127public static int run(String argv[], PrintStream out) {128return new vartable001().runIt(argv, out);129}130131// -------------------------------------------------------------------132133/**134* Perform test execution.135*/136public int runIt(String argv[], PrintStream out) {137138// make log for debugger messages139argumentHandler = new ArgumentHandler(argv);140log = new Log(out, argumentHandler);141142// execute test and display results143try {144log.display("\n>>> Preparing debugee for testing \n");145146// launch debuggee147binder = new Binder(argumentHandler, log);148log.display("Launching debugee");149debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);150transport = debugee.getTransport();151pipe = debugee.createIOPipe();152153// make debuggee ready for testing154prepareDebugee();155156// work with prepared debuggee157try {158log.display("\n>>> Obtaining requred data from debugee \n");159160// query debuggee for classID of tested class161log.display("Getting classID by signature:\n"162+ " " + TESTED_CLASS_SIGNATURE);163long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);164log.display(" got classID: " + classID);165166// query debuggee for methodID of tested method (declared in the class)167log.display("Getting methodID by name: " + TESTED_METHOD_NAME);168long methodID = debugee.getMethodID(classID, TESTED_METHOD_NAME, true);169log.display(" got methodID: " + methodID);170171// perform testing JDWP command172log.display("\n>>> Testing JDWP command \n");173testCommand(classID, methodID);174175} finally {176// quit debugee177log.display("\n>>> Finishing test \n");178quitDebugee();179}180181} catch (Failure e) {182log.complain("TEST FAILED: " + e.getMessage());183success = false;184} catch (Exception e) {185e.printStackTrace(out);186log.complain("Caught unexpected exception while running the test:\n\t" + e);187success = false;188}189190if (!success) {191log.complain("TEST FAILED");192return FAILED;193}194195out.println("TEST PASSED");196return PASSED;197198}199200/**201* Prepare debugee for testing and waiting for ready signal.202*/203void prepareDebugee() {204// wait for VM_INIT event from debugee205log.display("Waiting for VM_INIT event");206debugee.waitForVMInit();207208// query debugee for VM-dependent ID sizes209log.display("Querying for IDSizes");210debugee.queryForIDSizes();211212// resume initially suspended debugee213log.display("Resuming debugee VM");214debugee.resume();215216// wait for READY signal from debugee217log.display("Waiting for signal from debugee: " + READY);218String signal = pipe.readln();219log.display("Received signal from debugee: " + signal);220if (! signal.equals(READY)) {221throw new TestBug("Unexpected signal received from debugee: " + signal222+ " (expected: " + READY + ")");223}224}225226/**227* Sending debugee signal to quit and waiting for it exits.228*/229void quitDebugee() {230// send debugee signal to quit231log.display("Sending signal to debugee: " + QUIT);232pipe.println(QUIT);233234// wait for debugee exits235log.display("Waiting for debugee exits");236int code = debugee.waitFor();237238// analize debugee exit status code239if (code == JCK_STATUS_BASE + PASSED) {240log.display("Debugee PASSED with exit code: " + code);241} else {242log.complain("Debugee FAILED with exit code: " + code);243success = false;244}245}246247/**248* Perform testing JDWP command for specified TypeID.249*/250void testCommand(long classID, long methodID) {251// create command packet and fill requred out data252log.display("Create command packet:");253log.display("Command: " + JDWP_COMMAND_NAME);254CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);255log.display(" referenceTypeID: " + classID);256command.addReferenceTypeID(classID);257log.display(" methodID: " + methodID);258command.addMethodID(methodID);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}270271ReplyPacket reply = new ReplyPacket();272273// receive reply packet from debugee274try {275log.display("Waiting for reply packet");276transport.read(reply);277log.display("Reply packet received:\n" + reply);278} catch (IOException e) {279log.complain("Unable to read reply packet:\n\t" + e);280success = false;281return;282}283284// check reply packet header285try{286log.display("Checking reply packet header");287reply.checkHeader(command.getPacketID());288} catch (BoundException e) {289log.complain("Bad header of reply packet:\n\t" + e.getMessage());290success = false;291}292293// start parsing reply packet data294log.display("Parsing reply packet:");295reply.resetPosition();296297// clear list of found variables298int[] foundVariablesList = new int[variablesCount];299for (int i = 0; i < variablesCount; i++) {300foundVariablesList[i] = 0;301}302303// extract and check reply data304305// extract number of argumnets306int argCount = 0;307try {308argCount = reply.getInt();309log.display(" argCount: " + argCount);310} catch (BoundException e) {311log.complain("Unable to extract number of arguments from reply packet:\n\t"312+ e.getMessage());313success = false;314return;315}316317// check that number of arguments is not negative318if (argCount < 0) {319log.complain("Negative of arguments in reply packet: " + argCount);320success = false;321}322323// extract number of slots324int slots = 0;325try {326slots = reply.getInt();327log.display(" slots: " + slots);328} catch (BoundException e) {329log.complain("Unable to extract number of slots from reply packet:\n\t"330+ e.getMessage());331success = false;332return;333}334335// check that number of slots is not negative336if (slots < 0) {337log.complain("Negative value of end code index in reply packet: " + slots);338success = false;339}340341// check that start code is not less than expected342if (slots < variablesCount) {343log.complain("Number of slots (" + slots344+ ") is less than expected (" + variablesCount + ")");345success = false;346}347348// extract and check each slot attributes349for (int i = 0; i < slots; i++) {350log.display(" slot #" + i + ":");351352// extract code index of a slot353long codeIndex = 0;354try {355codeIndex = reply.getLong();356log.display(" codeIndex: " + codeIndex);357} catch (BoundException e) {358log.complain("Unable to extract code index of slot #" + i359+ " from reply packet:\n\t"360+ e.getMessage());361success = false;362return;363}364365// check that code index is not negative366if (codeIndex < 0) {367log.complain("Negative code index of slot #" + i + ":" + codeIndex);368success = false;369}370371// extract name of a slot372String name = null;373try {374name = reply.getString();375log.display(" name: " + name);376} catch (BoundException e) {377log.complain("Unable to extract name of slot #" + i378+ " from reply packet:\n\t"379+ e.getMessage());380success = false;381return;382}383384// extract signature of a slot385String signature = null;386try {387signature = reply.getString();388log.display(" signature: " + signature);389} catch (BoundException e) {390log.complain("Unable to extract signature of slot #" + i391+ " from reply packet:\n\t"392+ e.getMessage());393success = false;394return;395}396397// extract code length398int length = 0;399try {400length = reply.getInt();401log.display(" length: " + length);402} catch (BoundException e) {403log.complain("Unable to extract code length for slot #" + i404+ " from reply packet:\n\t"405+ e.getMessage());406success = false;407return;408}409410// extract code length411int slot = 0;412try {413slot = reply.getInt();414log.display(" slot: " + length);415} catch (BoundException e) {416log.complain("Unable to extract slot index of slot #" + i417+ " from reply packet:\n\t"418+ e.getMessage());419success = false;420return;421}422423// find slot name into list of expected variables424int found = -1;425for (int j = 0; j < variablesCount; j++) {426if (variablesList[j][0].equals(name)) {427found = j;428break;429}430}431432// check if slot is found and not duplicated433if (found >= 0) {434if (foundVariablesList[found] > 0) {435log.complain("Slot #" + i + " is duplicated "436+ foundVariablesList[found] + " times: "437+ name);438success = false;439/*440} else {441log.display("Found expected variable #" + found + ": "442+ variablesList[found][0]);443*/444}445foundVariablesList[found]++;446447// check slot signature448if (!variablesList[found][1].equals(signature)) {449log.complain("Unexpected signature for slot #" + i + ": " + signature450+ " (expected: " + variablesList[found][1] + ")");451success = false;452}453} else {454log.display("Unexpected slot #" + i + " (may be synthetic): " + name);455}456457// check that code length is not negative458if (length < 0) {459log.complain("Code length for slot #" + i + " is negative: " + length);460success = false;461}462463// check that slot index is not negative464if (slot < 0) {465log.complain("Index of slot #" + i + " is negative: " + slot);466success = false;467}468}469470// check for extra data in reply packet471if (!reply.isParsed()) {472log.complain("Extra trailing bytes found in reply packet at: "473+ reply.offsetString());474success = false;475}476477// check that all expected variables found478for (int i = 0; i < variablesCount; i++) {479if (foundVariablesList[i] <= 0) {480log.complain("No slot found in reply packet for variable: "481+ variablesList[i][0]);482success = false;483}484}485486}487488}489490491