Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/CurrentContendedMonitor/curcontmonitor001.java
42332 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.ThreadReference.CurrentContendedMonitor;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.CurrentContendedMonitor.33*34* See curcontmonitor001.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 curcontmonitor001 {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.ThreadReference.CurrentContendedMonitor";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "curcontmonitor001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// VM capability constatnts61static final int VM_CAPABILITY_NUMBER = JDWP.Capability.CAN_GET_CURRENT_CONTENDED_MONITOR;62static final String VM_CAPABILITY_NAME = "canGetCurrentContendedMonitor";6364// tested JDWP command constants65static final String JDWP_COMMAND_NAME = "ThreadReference.CurrentContendedMonitor";66static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.CurrentContendedMonitor;6768// tested class name and signature constants69static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";70static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";7172// name of the tested thread and statioc field with thread value73static final String TESTED_THREAD_NAME = curcontmonitor001a.THREAD_NAME;74static final String THREAD_FIELD_NAME = curcontmonitor001a.THREAD_FIELD_NAME;75static final String MONITOR_FIELD_NAME = curcontmonitor001a.MONITOR_FIELD_NAME;7677// usual scaffold objects78ArgumentHandler argumentHandler = null;79Log log = null;80Binder binder = null;81Debugee debugee = null;82Transport transport = null;83IOPipe pipe = null;8485// test passed or not86boolean success = true;8788// -------------------------------------------------------------------8990/**91* Start test from command line.92*/93public static void main (String argv[]) {94System.exit(run(argv,System.out) + JCK_STATUS_BASE);95}9697/**98* Start JCK-compilant test.99*/100public static int run(String argv[], PrintStream out) {101return new curcontmonitor001().runIt(argv, out);102}103104// -------------------------------------------------------------------105106/**107* Perform test execution.108*/109public int runIt(String argv[], PrintStream out) {110111// make log for debugger messages112argumentHandler = new ArgumentHandler(argv);113log = new Log(out, argumentHandler);114115// execute test and display results116try {117log.display("\n>>> Preparing debugee for testing \n");118119// launch debuggee120binder = new Binder(argumentHandler, log);121log.display("Launching debugee");122debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);123transport = debugee.getTransport();124pipe = debugee.createIOPipe();125126// make debuggee ready for testing127prepareDebugee();128129// work with prepared debuggee130long threadID = 0;131try {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 threadID value from a static field151log.display("Getting threadID value from static field: "152+ THREAD_FIELD_NAME);153threadID = queryObjectID(classID, THREAD_FIELD_NAME, JDWP.Tag.THREAD);154log.display(" got threadID: " + threadID);155156// query debuggee for objectID value for owned monitor from a static field157log.display("Getting objectID value for owned monitor from static field: "158+ MONITOR_FIELD_NAME);159long monitorID = queryObjectID(classID,160MONITOR_FIELD_NAME, JDWP.Tag.OBJECT);161log.display(" got objectID: " + monitorID);162163// suspend all threads into debuggee164log.display("Suspending all threads into debuggee");165debugee.suspend();166log.display(" debuggee suspended");167168// perform testing JDWP command169log.display("\n>>> Testing JDWP command \n");170testCommand(threadID, monitorID);171172} finally {173log.display("\n>>> Finishing test \n");174175// resume all threads into debuggee176log.display("resuming all threads into debuggee");177debugee.resume();178log.display(" debuggee resumed");179180// quit debugee181quitDebugee();182}183184} catch (Failure e) {185log.complain("TEST FAILED: " + e.getMessage());186success = false;187} catch (Exception e) {188e.printStackTrace(out);189log.complain("Caught unexpected exception while running the test:\n\t" + e);190success = false;191}192193if (!success) {194log.complain("TEST FAILED");195return FAILED;196}197198out.println("TEST PASSED");199return PASSED;200201}202203/**204* Prepare debugee for testing and waiting for ready signal.205*/206void prepareDebugee() {207// wait for VM_INIT event from debugee208log.display("Waiting for VM_INIT event");209debugee.waitForVMInit();210211// query debugee for VM-dependent ID sizes212log.display("Querying for IDSizes");213debugee.queryForIDSizes();214215// resume initially suspended debugee216log.display("Resuming debugee VM");217debugee.resume();218219// wait for READY signal from debugee220log.display("Waiting for signal from debugee: " + READY);221String signal = pipe.readln();222log.display("Received signal from debugee: " + signal);223if (signal == null) {224throw new TestBug("Null signal received from debugee: " + signal225+ " (expected: " + READY + ")");226} else if (signal.equals(ERROR)) {227throw new TestBug("Debugee was not able to start tested thread"228+ " (received signal: " + signal + ")");229} else if (!signal.equals(READY)) {230throw new TestBug("Unexpected signal received from debugee: " + signal231+ " (expected: " + READY + ")");232}233}234235/**236* Sending debugee signal to quit and waiting for it exits.237*/238void quitDebugee() {239// send debugee signal to quit240log.display("Sending signal to debugee: " + QUIT);241pipe.println(QUIT);242243// wait for debugee exits244log.display("Waiting for debugee exits");245int code = debugee.waitFor();246247// analize debugee exit status code248if (code == JCK_STATUS_BASE + PASSED) {249log.display("Debugee PASSED with exit code: " + code);250} else {251log.complain("Debugee FAILED with exit code: " + code);252success = false;253}254}255256/**257* Query debuggee for objectID value of static class field.258*/259long queryObjectID(long classID, String fieldName, byte tag) {260// get fieledID for static field (declared in the class)261long fieldID = debugee.getClassFieldID(classID, fieldName, true);262// get value of the field263JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);264265// check that value has THREAD tag266if (value.getTag() != tag) {267throw new Failure("Wrong objectID tag received from field \"" + fieldName268+ "\": " + value.getTag() + " (expected: " + tag + ")");269}270271// extract threadID from the value272long objectID = ((Long)value.getValue()).longValue();273return objectID;274}275276/**277* Perform testing JDWP command for specified threadID.278*/279void testCommand(long threadID, long monitorID) {280// create command packet and fill requred out data281log.display("Create command packet:");282log.display("Command: " + JDWP_COMMAND_NAME);283CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);284log.display(" threadID: " + threadID);285command.addObjectID(threadID);286command.setLength();287288// send command packet to debugee289try {290log.display("Sending command packet:\n" + command);291transport.write(command);292} catch (IOException e) {293log.complain("Unable to send command packet:\n\t" + e);294success = false;295return;296}297298ReplyPacket reply = new ReplyPacket();299300// receive reply packet from debugee301try {302log.display("Waiting for reply packet");303transport.read(reply);304log.display("Reply packet received:\n" + reply);305} catch (IOException e) {306log.complain("Unable to read reply packet:\n\t" + e);307success = false;308return;309}310311// check reply packet header312try{313log.display("Checking reply packet header");314reply.checkHeader(command.getPacketID());315} catch (BoundException e) {316log.complain("Bad header of reply packet:\n\t" + e.getMessage());317success = false;318return;319}320321// start parsing reply packet data322log.display("Parsing reply packet:");323reply.resetPosition();324325// extract object tag326byte tag = (byte)0;327try {328tag = reply.getByte();329log.display(" tag: " + tag);330} catch (BoundException e) {331log.complain("Unable to extract tag for contended monitor object from reply packet:\n\t"332+ e.getMessage());333success = false;334return;335}336337// extract objectID338long objectID = 0;339try {340objectID = reply.getObjectID();341log.display(" objectID: " + objectID);342} catch (BoundException e) {343log.complain("Unable to extract contended monitor objectID from reply packet:\n\t"344+ e.getMessage());345success = false;346return;347}348349// check that tag is an OBJECT tag350if (tag != JDWP.Tag.OBJECT) {351log.complain("Unexpected tag for monitor object received:" + tag352+ " (expected" + JDWP.Tag.OBJECT);353success = false;354}355356// check that objectID is not negative integer357if (objectID < 0) {358log.complain("Negative value of objectID received: " + objectID);359success = false;360}361362// check that objectID is as expected363if (objectID != monitorID) {364log.display("Unexpected monitor objectID received: " + objectID365+ " (expected" + monitorID);366success = false;367}368369// check for extra data in reply packet370if (!reply.isParsed()) {371log.complain("Extra trailing bytes found in reply packet at: "372+ reply.offsetString());373success = false;374}375376}377378}379380381