Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/Method/Bytecodes/bytecodes001.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.Bytecodes;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: Method.Bytecodes.33*34* See bytecodes001.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 bytecodes001 {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.Bytecodes";56static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "bytecodes001";57static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5859// VM capability constatnts60static final int VM_CAPABILITY_NUMBER = JDWP.Capability.CAN_GET_BYTECODES;61static final String VM_CAPABILITY_NAME = "canGetBytecodes";6263// tested JDWP command constants64static final String JDWP_COMMAND_NAME = "Method.Bytecodes";65static final int JDWP_COMMAND_ID = JDWP.Command.Method.Bytecodes;6667// tested class name and signature constants68static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";69static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";7071// tested method name constant72static final String TESTED_METHOD_NAME = "testedMethod";7374// expected values for bound line numbers75static final int FIRST_LINE_NUMBER = bytecodes001a.FIRST_LINE_NUMBER;76static final int LAST_LINE_NUMBER = bytecodes001a.LAST_LINE_NUMBER;7778// usual scaffold objects79ArgumentHandler argumentHandler = null;80Log log = null;81Binder binder = null;82Debugee debugee = null;83Transport transport = null;84IOPipe pipe = null;8586// test passed or not87boolean success = true;8889// -------------------------------------------------------------------9091/**92* Start test from command line.93*/94public static void main (String argv[]) {95System.exit(run(argv,System.out) + JCK_STATUS_BASE);96}9798/**99* Start JCK-compilant test.100*/101public static int run(String argv[], PrintStream out) {102return new bytecodes001().runIt(argv, out);103}104105// -------------------------------------------------------------------106107/**108* Perform test execution.109*/110public int runIt(String argv[], PrintStream out) {111112// make log for debugger messages113argumentHandler = new ArgumentHandler(argv);114log = new Log(out, argumentHandler);115116// execute test and display results117try {118log.display("\n>>> Preparing debugee for testing \n");119120// launch debuggee121binder = new Binder(argumentHandler, log);122log.display("Launching debugee");123debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);124transport = debugee.getTransport();125pipe = debugee.createIOPipe();126127// make debuggee ready for testing128prepareDebugee();129130// work with prepared debuggee131try {132log.display("\n>>> Checking VM capability \n");133134// check for VM capability135log.display("Checking VM capability: " + VM_CAPABILITY_NAME);136if (!debugee.getCapability(VM_CAPABILITY_NUMBER, VM_CAPABILITY_NAME)) {137out.println("TEST PASSED: unsupported VM capability: "138+ VM_CAPABILITY_NAME);139return PASSED;140}141142log.display("\n>>> Obtaining requred data from debugee \n");143144// query debuggee for classID of tested class145log.display("Getting classID by signature:\n"146+ " " + TESTED_CLASS_SIGNATURE);147long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);148log.display(" got classID: " + classID);149150// query debuggee for methodID of tested method (declared in the class)151log.display("Getting methodID by name: " + TESTED_METHOD_NAME);152long methodID = debugee.getMethodID(classID, TESTED_METHOD_NAME, true);153log.display(" got methodID: " + methodID);154155// perform testing JDWP command156log.display("\n>>> Testing JDWP command \n");157testCommand(classID, methodID);158159} finally {160// quit debugee161log.display("\n>>> Finishing test \n");162quitDebugee();163}164165} catch (Failure e) {166log.complain("TEST FAILED: " + e.getMessage());167success = false;168} catch (Exception e) {169e.printStackTrace(out);170log.complain("Caught unexpected exception while running the test:\n\t" + e);171success = false;172}173174if (!success) {175log.complain("TEST FAILED");176return FAILED;177}178179out.println("TEST PASSED");180return PASSED;181182}183184/**185* Prepare debugee for testing and waiting for ready signal.186*/187void prepareDebugee() {188// wait for VM_INIT event from debugee189log.display("Waiting for VM_INIT event");190debugee.waitForVMInit();191192// query debugee for VM-dependent ID sizes193log.display("Querying for IDSizes");194debugee.queryForIDSizes();195196// resume initially suspended debugee197log.display("Resuming debugee VM");198debugee.resume();199200// wait for READY signal from debugee201log.display("Waiting for signal from debugee: " + READY);202String signal = pipe.readln();203log.display("Received signal from debugee: " + signal);204if (! signal.equals(READY)) {205throw new TestBug("Unexpected signal received from debugee: " + signal206+ " (expected: " + READY + ")");207}208}209210/**211* Sending debugee signal to quit and waiting for it exits.212*/213void quitDebugee() {214// send debugee signal to quit215log.display("Sending signal to debugee: " + QUIT);216pipe.println(QUIT);217218// wait for debugee exits219log.display("Waiting for debugee exits");220int code = debugee.waitFor();221222// analize debugee exit status code223if (code == JCK_STATUS_BASE + PASSED) {224log.display("Debugee PASSED with exit code: " + code);225} else {226log.complain("Debugee FAILED with exit code: " + code);227success = false;228}229}230231/**232* Perform testing JDWP command for specified TypeID.233*/234void testCommand(long classID, long methodID) {235// create command packet and fill requred out data236log.display("Create command packet:");237log.display("Command: " + JDWP_COMMAND_NAME);238CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);239log.display(" referenceTypeID: " + classID);240command.addReferenceTypeID(classID);241log.display(" methodID: " + methodID);242command.addMethodID(methodID);243command.setLength();244245// send command packet to debugee246try {247log.display("Sending command packet:\n" + command);248transport.write(command);249} catch (IOException e) {250log.complain("Unable to send command packet:\n\t" + e);251success = false;252return;253}254255ReplyPacket reply = new ReplyPacket();256257// receive reply packet from debugee258try {259log.display("Waiting for reply packet");260transport.read(reply);261log.display("Reply packet received:\n" + reply);262} catch (IOException e) {263log.complain("Unable to read reply packet:\n\t" + e);264success = false;265return;266}267268// check reply packet header269try{270log.display("Checking reply packet header");271reply.checkHeader(command.getPacketID());272} catch (BoundException e) {273log.complain("Bad header of reply packet:\n\t" + e.getMessage());274success = false;275}276277// start parsing reply packet data278log.display("Parsing reply packet:");279reply.resetPosition();280281// extract and check reply data282283// extract number of lines284int bytes = 0;285try {286bytes = reply.getInt();287log.display(" bytes: " + bytes);288} catch (BoundException e) {289log.complain("Unable to extract number of bytes from reply packet:\n\t"290+ e.getMessage());291success = false;292return;293}294295if (bytes < 0) {296log.complain("Negative number of bytes in reply packet: " + bytes);297success = false;298return;299}300301if (bytes == 0) {302log.complain("Zero number of bytes in reply packet: " + bytes);303success = false;304return;305}306307// extract all bytes308ByteBuffer bytecode = new ByteBuffer();309for (int i = 0; i < bytes; i++) {310// extract next byte of bytecode311try {312byte aByte = reply.getByte();313bytecode.addByte(aByte);314} catch (BoundException e) {315log.complain("Unable to extract byte #" + i316+ " from reply packet:\n\t"317+ e.getMessage());318success = false;319return;320}321}322323log.display(" bytecode:\n" + bytecode);324325// check for extra data in reply packet326if (!reply.isParsed()) {327log.complain("Extra trailing bytes found in reply packet at: "328+ reply.offsetString());329success = false;330}331}332333}334335336