Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/StackFrame/ThisObject/thisobject001.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.StackFrame.ThisObject;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: StackFrame.ThisObject.33*34* See thisobject001.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 thisobject001 {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 ERROR = "error";53static final String QUIT = "quit";5455// package and classes names constants56static final String PACKAGE_NAME = "nsk.jdwp.StackFrame.ThisObject";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "thisobject001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "StackFrame.ThisObject";62static final int JDWP_COMMAND_ID = JDWP.Command.StackFrame.ThisObject;6364// tested class name and signature constants65static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + thisobject001a.OBJECT_CLASS_NAME;66static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6768// names of the static fields with the tested thread and object values69static final String TESTED_OBJECT_FIELD_NAME = thisobject001a.OBJECT_FIELD_NAME;70static final String TESTED_THREAD_FIELD_NAME = thisobject001a.THREAD_FIELD_NAME;7172// usual scaffold objects73ArgumentHandler argumentHandler = null;74Log log = null;75Binder binder = null;76Debugee debugee = null;77Transport transport = null;78IOPipe pipe = null;7980// test passed or not81boolean success = true;8283// -------------------------------------------------------------------8485/**86* Start test from command line.87*/88public static void main (String argv[]) {89System.exit(run(argv,System.out) + JCK_STATUS_BASE);90}9192/**93* Start JCK-compilant test.94*/95public static int run(String argv[], PrintStream out) {96return new thisobject001().runIt(argv, out);97}9899// -------------------------------------------------------------------100101/**102* Perform test execution.103*/104public int runIt(String argv[], PrintStream out) {105106// make log for debugger messages107argumentHandler = new ArgumentHandler(argv);108log = new Log(out, argumentHandler);109110// execute test and display results111try {112log.display("\n>>> Preparing debugee for testing \n");113114// launch debuggee115binder = new Binder(argumentHandler, log);116log.display("Launching debugee");117debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);118transport = debugee.getTransport();119pipe = debugee.createIOPipe();120121// make debuggee ready for testing122prepareDebugee();123124// work with prepared debuggee125long threadID = 0;126try {127log.display("\n>>> Obtaining requred data from debugee \n");128129// query debuggee for classID of tested class130log.display("Getting classID by signature:\n"131+ " " + TESTED_CLASS_SIGNATURE);132long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);133log.display(" got classID: " + classID);134135// query debuggee for objectID value from static field136log.display("Getting objectID value from static field: "137+ TESTED_OBJECT_FIELD_NAME);138long objectID = queryObjectID(classID, TESTED_OBJECT_FIELD_NAME, JDWP.Tag.OBJECT);139log.display(" got objectID: " + objectID);140141// query debuggee for threadID value from static field142log.display("Getting threadID value from static field: "143+ TESTED_THREAD_FIELD_NAME);144threadID = queryObjectID(classID, TESTED_THREAD_FIELD_NAME, JDWP.Tag.THREAD);145log.display(" got threadID: " + threadID);146147// suspend tested thread into debuggee148log.display("Suspending thread into debuggee for threadID: " + threadID);149debugee.suspendThread(threadID);150151// query debuggee for current frameID of the tested thread152log.display("Getting current frameID for the threadID: "153+ threadID);154long frameID = debugee.getCurrentFrameID(threadID);155log.display(" got frameID: " + frameID);156157// perform testing JDWP command158log.display("\n>>> Testing JDWP command \n");159testCommand(frameID, threadID, objectID);160161} finally {162log.display("\n>>> Finishing test \n");163164// resume suspended thread165if (threadID != 0) {166log.display("Resuming suspended thread");167debugee.resumeThread(threadID);168}169170// quit debugee171quitDebugee();172}173174} catch (Failure e) {175log.complain("TEST FAILED: " + e.getMessage());176success = false;177} catch (Exception e) {178e.printStackTrace(out);179log.complain("Caught unexpected exception while running the test:\n\t" + e);180success = false;181}182183if (!success) {184log.complain("TEST FAILED");185return FAILED;186}187188out.println("TEST PASSED");189return PASSED;190191}192193/**194* Prepare debugee for testing and waiting for ready signal.195*/196void prepareDebugee() {197// wait for VM_INIT event from debugee198log.display("Waiting for VM_INIT event");199debugee.waitForVMInit();200201// query debugee for VM-dependent ID sizes202log.display("Querying for IDSizes");203debugee.queryForIDSizes();204205// resume initially suspended debugee206log.display("Resuming debugee VM");207debugee.resume();208209// wait for READY signal from debugee210log.display("Waiting for signal from debugee: " + READY);211String signal = pipe.readln();212log.display("Received signal from debugee: " + signal);213if (signal == null) {214throw new TestBug("Null signal received from debugee: " + signal215+ " (expected: " + READY + ")");216} else if (signal.equals(ERROR)) {217throw new TestBug("Debugee was not able to start tested thread"218+ " (received signal: " + signal + ")");219} else if (!signal.equals(READY)) {220throw new TestBug("Unexpected signal received from debugee: " + signal221+ " (expected: " + READY + ")");222}223}224225/**226* Sending debugee signal to quit and waiting for it exits.227*/228void quitDebugee() {229// send debugee signal to quit230log.display("Sending signal to debugee: " + QUIT);231pipe.println(QUIT);232233// wait for debugee exits234log.display("Waiting for debugee exits");235int code = debugee.waitFor();236237// analize debugee exit status code238if (code == JCK_STATUS_BASE + PASSED) {239log.display("Debugee PASSED with exit code: " + code);240} else {241log.complain("Debugee FAILED with exit code: " + code);242success = false;243}244}245246/**247* Query debuggee for objectID value of static field of the class.248*/249long queryObjectID(long classID, String fieldName, byte tag) {250// get fieledID for static field (declared in the class)251long fieldID = debugee.getClassFieldID(classID, fieldName, true);252// get value of the field253JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);254255// check that value has expected tag256if (value.getTag() != tag) {257throw new Failure("Unexpected tag for object value returned: "258+ value.getTag() + " (expected: " + tag + ")");259}260261// extract objectID from the value262long objectID = ((Long)value.getValue()).longValue();263return objectID;264}265266/**267* Perform testing JDWP command for specified frameID.268*/269void testCommand(long frameID, long threadID, long expectedObjectID) {270// create command packet and fill requred out data271log.display("Create command packet:");272log.display("Command: " + JDWP_COMMAND_NAME);273CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);274log.display(" threadID: " + threadID);275command.addObjectID(threadID);276log.display(" frameID: " + frameID);277command.addFrameID(frameID);278command.setLength();279280// send command packet to debugee281try {282log.display("Sending command packet:\n" + command);283transport.write(command);284} catch (IOException e) {285log.complain("Unable to send command packet:\n\t" + e);286success = false;287return;288}289290ReplyPacket reply = new ReplyPacket();291292// receive reply packet from debugee293try {294log.display("Waiting for reply packet");295transport.read(reply);296log.display("Reply packet received:\n" + reply);297} catch (IOException e) {298log.complain("Unable to read reply packet:\n\t" + e);299success = false;300return;301}302303// check reply packet header304try{305log.display("Checking reply packet header");306reply.checkHeader(command.getPacketID());307} catch (BoundException e) {308log.complain("Bad header of reply packet:\n\t" + e.getMessage());309success = false;310return;311}312313// start parsing reply packet data314log.display("Parsing reply packet:");315reply.resetPosition();316317// extract tag of object value318byte tag = (byte)0;319try {320tag = reply.getByte();321log.display(" tag: " + tag);322} catch (BoundException e) {323log.complain("Unable to extract object tag from reply packet:\n\t"324+ e.getMessage());325success = false;326return;327}328329// extract objectID of object value330long objectID = 0;331try {332objectID = reply.getObjectID();333log.display(" objectID: " + objectID);334} catch (BoundException e) {335log.complain("Unable to extract objectID from reply packet:\n\t"336+ e.getMessage());337success = false;338return;339}340341// check for extra data in reply packet342if (!reply.isParsed()) {343log.complain("Extra trailing bytes found in reply packet at: "344+ reply.offsetString());345success = false;346}347348// check that object tag is as expected349if (tag != JDWP.Tag.OBJECT) {350log.complain("Unexpected object tag returned: "351+ tag + " (expected: " + JDWP.Tag.OBJECT + ")");352success = false;353}354355// check that objectID is as expected356if (objectID < 0) {357log.complain("Negative value of objectID retured: "358+ objectID + " (expected: " + expectedObjectID + ")");359success = false;360} else if (objectID != expectedObjectID) {361log.complain(" Unexpected objectID retured: "362+ objectID + " (expected: " + expectedObjectID + ")");363success = false;364}365366}367368}369370371