Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/VirtualMachine/DisposeObjects/disposeobj001.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.VirtualMachine.DisposeObjects;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: VirtualMachine.DisposeObjects.33*34* See disposeobj001.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 disposeobj001 {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.VirtualMachine.DisposeObjects";56static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "disposeobj001";57static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5859// tested JDWP command constants60static final String JDWP_COMMAND_NAME = "VirtualMachine.DisposeObjects";61static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.DisposeObjects;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// Number of references to free for tested objects68static final int REF_COUNT = 3;6970// list of static fields with tested objects71static final String fieldsList[] = {72"objectValue",73"stringValue",74"classValue"75};76static final int fieldsCount = fieldsList.length;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 disposeobj001().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>>> Obtaining requred data from debugee \n");133134// query debuggee for classID of tested class135log.display("Getting classID by signature:\n"136+ " " + TESTED_CLASS_SIGNATURE);137long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);138log.display(" got classID: " + classID);139140// query debuggee for objectIDs values of static fields141long objectIDs[] = getObjectIDs(classID);142143// perform testing JDWP command144log.display("\n>>> Testing JDWP command \n");145testCommand(objectIDs);146147} finally {148// quit debugee149log.display("\n>>> Finishing test \n");150quitDebugee();151}152153} catch (Failure e) {154log.complain("TEST FAILED: " + e.getMessage());155success = false;156} catch (Exception e) {157e.printStackTrace(out);158log.complain("Caught unexpected exception while running the test:\n\t" + e);159success = false;160}161162if (!success) {163log.complain("TEST FAILED");164return FAILED;165}166167out.println("TEST PASSED");168return PASSED;169170}171172/**173* Prepare debugee for testing and waiting for ready signal.174*/175void prepareDebugee() {176// wait for VM_INIT event from debugee177log.display("Waiting for VM_INIT event");178debugee.waitForVMInit();179180// query debugee for VM-dependent ID sizes181log.display("Querying for IDSizes");182debugee.queryForIDSizes();183184// resume initially suspended debugee185log.display("Resuming debugee VM");186debugee.resume();187188// wait for READY signal from debugee189log.display("Waiting for signal from debugee: " + READY);190String signal = pipe.readln();191log.display("Received signal from debugee: " + signal);192if (! signal.equals(READY)) {193throw new TestBug("Unexpected signal received from debugee: " + signal194+ " (expected: " + READY + ")");195}196}197198/**199* Sending debugee signal to quit and waiting for it exits.200*/201void quitDebugee() {202// send debugee signal to quit203log.display("Sending signal to debugee: " + QUIT);204pipe.println(QUIT);205206// wait for debugee exits207log.display("Waiting for debugee exits");208int code = debugee.waitFor();209210// analize debugee exit status code211if (code == JCK_STATUS_BASE + PASSED) {212log.display("Debugee PASSED with exit code: " + code);213} else {214log.complain("Debugee FAILED with exit code: " + code);215success = false;216}217}218219/**220* Query debuggee for IDs of objects which are values of static fields.221*/222long[] getObjectIDs(long classID) {223long[] objectIDs = new long[fieldsCount];224225for (int i = 0; i < fieldsCount; i++ ) {226String name = fieldsList[i];227228log.display("Getting fieldID of static field: " + name);229long fieldID = debugee.getClassFieldID(classID, name, true);230log.display(" got fieldID: " + fieldID);231232// query for objectID value REF_COUNT times233log.display("Getting " + REF_COUNT + " times value of static fieldID: "234+ fieldID);235for (int j = 0; j < REF_COUNT; j++) {236JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);237log.display(" #" + i + ": got value: " + value);238239if (value.getTag() != JDWP.Tag.OBJECT240&& value.getTag() != JDWP.Tag.STRING) {241throw new Failure("Not object value returned for static field \""242+ name + "\": " + value);243}244long objectID = ((Long)value.getValue()).longValue();245246// check that returned objectID is the same247if (j > 0) {248if (objectID != objectIDs[i]) {249throw new Failure("Last request for object value returned"250+ " unexpected objectID: " + objectID251+ "(expected: " + objectIDs[i] + ")");252}253} else {254objectIDs[i] = objectID;255}256}257}258return objectIDs;259}260261/**262* Perform testing JDWP command for specified TypeID.263*/264void testCommand(long objectIDs[]) {265// create command packet and fill requred out data266log.display("Create command packet:");267log.display("Command: " + JDWP_COMMAND_NAME);268CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);269270log.display(" requested: " + fieldsCount);271command.addInt(fieldsCount);272273for (int i = 0; i < fieldsCount; i++) {274log.display(" object: " + objectIDs[i]);275command.addObjectID(objectIDs[i]);276log.display(" refCnt: " + REF_COUNT);277command.addInt(REF_COUNT);278}279280command.setLength();281282// send command packet to debugee283try {284log.display("Sending command packet:\n" + command);285transport.write(command);286} catch (IOException e) {287log.complain("Unable to send command packet:\n\t" + e);288success = false;289return;290}291292ReplyPacket reply = new ReplyPacket();293294// receive reply packet from debugee295try {296log.display("Waiting for reply packet");297transport.read(reply);298log.display("Reply packet received:\n" + reply);299} catch (IOException e) {300log.complain("Unable to read reply packet:\n\t" + e);301success = false;302return;303}304305// check reply packet header306try{307log.display("Checking reply packet header");308reply.checkHeader(command.getPacketID());309} catch (BoundException e) {310log.complain("Bad header of reply packet:\n\t" + e.getMessage());311success = false;312}313314// start parsing reply packet data315log.display("Parsing reply packet:");316reply.resetPosition();317318// check for extra data in reply packet319if (!reply.isParsed()) {320log.complain("Extra trailing bytes found in reply packet at: "321+ reply.offsetString());322success = false;323}324}325}326327328