Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/ThreadReference/FrameCount/framecnt001.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.ThreadReference.FrameCount;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: ThreadReference.FrameCount.33*34* See framecnt001.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 framecnt001 {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.FrameCount";57static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "framecnt001";58static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5960// tested JDWP command constants61static final String JDWP_COMMAND_NAME = "ThreadReference.FrameCount";62static final int JDWP_COMMAND_ID = JDWP.Command.ThreadReference.FrameCount;6364// tested class name and signature constants65static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";66static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6768// name of the tested thread and statioc field with thread value69static final String TESTED_CLASS_FIELD_NAME = framecnt001a.FIELD_NAME;70static final String TESTED_THREAD_NAME = framecnt001a.THREAD_NAME;7172// expected number of frames count73static final int FRAMES_COUNT = framecnt001a.FRAMES_COUNT;7475// usual scaffold objects76ArgumentHandler argumentHandler = null;77Log log = null;78Binder binder = null;79Debugee debugee = null;80Transport transport = null;81IOPipe pipe = null;8283// test passed or not84boolean success = true;8586// -------------------------------------------------------------------8788/**89* Start test from command line.90*/91public static void main (String argv[]) {92System.exit(run(argv,System.out) + JCK_STATUS_BASE);93}9495/**96* Start JCK-compilant test.97*/98public static int run(String argv[], PrintStream out) {99return new framecnt001().runIt(argv, out);100}101102// -------------------------------------------------------------------103104/**105* Perform test execution.106*/107public int runIt(String argv[], PrintStream out) {108109// make log for debugger messages110argumentHandler = new ArgumentHandler(argv);111log = new Log(out, argumentHandler);112113// execute test and display results114try {115log.display("\n>>> Preparing debugee for testing \n");116117// launch debuggee118binder = new Binder(argumentHandler, log);119log.display("Launching debugee");120debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);121transport = debugee.getTransport();122pipe = debugee.createIOPipe();123124// make debuggee ready for testing125prepareDebugee();126127// work with prepared debuggee128long threadID = 0;129try {130log.display("\n>>> Obtaining requred data from debugee \n");131132// query debuggee for classID of tested class133log.display("Getting classID by signature:\n"134+ " " + TESTED_CLASS_SIGNATURE);135long classID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);136log.display(" got classID: " + classID);137138// query debuggee for threadID value from a static field139log.display("Getting threadID value from static field: "140+ TESTED_CLASS_FIELD_NAME);141threadID = queryThreadID(classID, TESTED_CLASS_FIELD_NAME);142log.display(" got threadID: " + threadID);143144// suspend tested thread into debyggee145log.display("Suspending thread into debuggee for threadID: " + threadID);146debugee.suspendThread(threadID);147148// perform testing JDWP command149log.display("\n>>> Testing JDWP command \n");150testCommand(threadID);151152} finally {153log.display("\n>>> Finishing test \n");154155// resume suspended thread156if (threadID != 0) {157log.display("Resuming suspended thread");158debugee.resumeThread(threadID);159}160161// quit debugee162quitDebugee();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 == null) {205throw new TestBug("Null signal received from debugee: " + signal206+ " (expected: " + READY + ")");207} else if (signal.equals(ERROR)) {208throw new TestBug("Debugee was not able to start tested thread"209+ " (received signal: " + signal + ")");210} else if (!signal.equals(READY)) {211throw new TestBug("Unexpected signal received from debugee: " + signal212+ " (expected: " + READY + ")");213}214}215216/**217* Sending debugee signal to quit and waiting for it exits.218*/219void quitDebugee() {220// send debugee signal to quit221log.display("Sending signal to debugee: " + QUIT);222pipe.println(QUIT);223224// wait for debugee exits225log.display("Waiting for debugee exits");226int code = debugee.waitFor();227228// analize debugee exit status code229if (code == JCK_STATUS_BASE + PASSED) {230log.display("Debugee PASSED with exit code: " + code);231} else {232log.complain("Debugee FAILED with exit code: " + code);233success = false;234}235}236237/**238* Query debuggee for threadID value of statuic field of the class.239*/240long queryThreadID(long classID, String fieldName) {241// get fieledID for static field (declared in the class)242long fieldID = debugee.getClassFieldID(classID, fieldName, true);243// get value of the field244JDWP.Value value = debugee.getStaticFieldValue(classID, fieldID);245246// check that value has THREAD tag247if (value.getTag() != JDWP.Tag.THREAD) {248throw new Failure("Not threadID value returned from debuggee: " + value);249}250251// extract threadID from the value252long threadID = ((Long)value.getValue()).longValue();253return threadID;254}255256/**257* Perform testing JDWP command for specified threadID.258*/259void testCommand(long threadID) {260// create command packet and fill requred out data261log.display("Create command packet:");262log.display("Command: " + JDWP_COMMAND_NAME);263CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);264log.display(" threadID: " + threadID);265command.addObjectID(threadID);266command.setLength();267268// send command packet to debugee269try {270log.display("Sending command packet:\n" + command);271transport.write(command);272} catch (IOException e) {273log.complain("Unable to send command packet:\n\t" + e);274success = false;275return;276}277278ReplyPacket reply = new ReplyPacket();279280// receive reply packet from debugee281try {282log.display("Waiting for reply packet");283transport.read(reply);284log.display("Reply packet received:\n" + reply);285} catch (IOException e) {286log.complain("Unable to read reply packet:\n\t" + e);287success = false;288return;289}290291// check reply packet header292try{293log.display("Checking reply packet header");294reply.checkHeader(command.getPacketID());295} catch (BoundException e) {296log.complain("Bad header of reply packet:\n\t" + e.getMessage());297success = false;298return;299}300301// start parsing reply packet data302log.display("Parsing reply packet:");303reply.resetPosition();304305// extract frames count306int frameCount = 0;307try {308frameCount = reply.getInt();309log.display(" frameCount: " + frameCount);310} catch (BoundException e) {311log.complain("Unable to extract frames count from reply packet:\n\t"312+ e.getMessage());313success = false;314return;315}316317// check that frames count is not negative318if (frameCount < 0) {319log.complain("Negative value of frames count in reply packet: "320+ frameCount);321success = false;322}323324// check that thread has an expected state325if (frameCount != FRAMES_COUNT) {326log.complain("Unexpected number of frames count returned: "327+ frameCount + " (expected: " + FRAMES_COUNT + ")");328success = false;329}330331// check for extra data in reply packet332if (!reply.isParsed()) {333log.complain("Extra trailing bytes found in reply packet at: "334+ reply.offsetString());335success = false;336}337}338339}340341342