Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/StackFrame/PopFrames/popframes001.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.PopFrames;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: StackFrame.PopFrames.33*34* See popframes001.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 popframes001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// VM capability constatnts51static final int VM_CAPABILITY_NUMBER = JDWP.Capability.CAN_POP_FRAMES;52static final String VM_CAPABILITY_NAME = "canPopFrames";5354// package and classes names55static final String PACKAGE_NAME = "nsk.jdwp.StackFrame.PopFrames";56static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "popframes001";57static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5859// tested JDWP command60static final String JDWP_COMMAND_NAME = "StackFrame.PopFrames";61static final int JDWP_COMMAND_ID = JDWP.Command.StackFrame.PopFrames;6263// tested class name and signature64static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedThreadClass";65static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6667// field and method names68static final String TESTED_METHOD_NAME = "testedMethod";69static final int BREAKPOINT_LINE_NUMBER = popframes001a.BREAKPOINT_LINE_NUMBER;7071// usual scaffold objects72ArgumentHandler argumentHandler = null;73Log log = null;74Binder binder = null;75Debugee debugee = null;76Transport transport = null;77int waitTime = 0; // minutes78long timeout = 0; // milliseconds79boolean dead = false;80boolean success = true;8182// data obtained from debuggee83long testedClassID = 0;84long testedThreadID = 0;85long testedMethodID = 0;86long testedFrameID = 0;87int breakpointRequestID = 0;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 popframes001().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);115waitTime = argumentHandler.getWaitTime(); // minutes116timeout = waitTime * 60 * 1000; // milliseconds117118// execute test and display results119try {120log.display("\n>>> Starting debugee \n");121122// launch debuggee123binder = new Binder(argumentHandler, log);124log.display("Launching debugee VM");125debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);126transport = debugee.getTransport();127log.display(" ... debuggee launched");128129// set timeout for debuggee responces130log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");131transport.setReadTimeout(timeout);132log.display(" ... timeout set");133134// wait for VM_INIT event135log.display("Waiting for VM_INIT event");136debugee.waitForVMInit();137log.display(" ... VM_INIT event received");138139// query debugee for VM-dependent ID sizes140log.display("Querying for IDSizes");141debugee.queryForIDSizes();142log.display(" ... size of VM-dependent types adjusted");143144// check for VM capability145log.display("\n>>> Checking VM capability \n");146log.display("Getting new VM capability: " + VM_CAPABILITY_NAME);147boolean capable = debugee.getNewCapability(VM_CAPABILITY_NUMBER, VM_CAPABILITY_NAME);148log.display(" ... got VM capability: " + capable);149150// exit as PASSED if this capability is not supported151if (!capable) {152out.println("TEST PASSED: unsupported VM capability: "153+ VM_CAPABILITY_NAME);154return PASSED;155}156157// prepare debuggee for testing and obtain required data158log.display("\n>>> Getting prepared for testing \n");159prepareForTest();160161// test JDWP command162log.display("\n>> Testing JDWP command \n");163testCommand();164165// check command results166if (success) {167log.display("\n>>> Checking result of tested command \n");168checkResult();169}170171// finish debuggee172log.display("\n>> Finishing debuggee \n");173174// clear BREAKPOIN event request175log.display("Clearing BREAKPOINT event requestID: " + breakpointRequestID);176debugee.clearEventRequest(JDWP.EventKind.BREAKPOINT, breakpointRequestID);177log.display(" ... request cleared");178179// resume debuggee after testing command180log.display("Resuming debuggee");181debugee.resume();182log.display(" ... debuggee resumed");183184// wait for VM_DEATH event185log.display("Waiting for VM_DEATH event");186debugee.waitForVMDeath();187log.display(" ... VM_DEATH event received");188dead = true;189190} catch (Failure e) {191log.complain("TEST FAILED: " + e.getMessage());192success = false;193} catch (Exception e) {194e.printStackTrace(out);195log.complain("Caught unexpected exception while running the test:\n\t" + e);196success = false;197} finally {198log.display("\n>>> Finishing test \n");199200// disconnect debugee and wait for its exit201if (debugee != null) {202quitDebugee();203}204}205206// check result207if (!success) {208log.complain("TEST FAILED");209return FAILED;210}211out.println("TEST PASSED");212return PASSED;213}214215/**216* Get debuggee prepared for testing and obtain required data.217*/218void prepareForTest() {219// wait for tested class loaded on debuggee startup and obtain its classID220log.display("Waiting for class loaded:\n\t" + TESTED_CLASS_NAME);221testedClassID = debugee.waitForClassLoaded(TESTED_CLASS_NAME,222JDWP.SuspendPolicy.ALL);223log.display(" ... class loaded with classID: " + testedClassID);224225// query debuggee for tested methodID226log.display("Getting tested methodID by name: " + TESTED_METHOD_NAME);227testedMethodID = debugee.getMethodID(testedClassID, TESTED_METHOD_NAME, true);228log.display(" ... got methodID: " + testedMethodID);229log.display("");230231// make BREAKPOINT event request232log.display("Creating breakpoint requests at: "233+ TESTED_METHOD_NAME + ":" + BREAKPOINT_LINE_NUMBER);234breakpointRequestID =235debugee.requestBreakpointEvent(JDWP.TypeTag.CLASS,236testedClassID, testedMethodID,237BREAKPOINT_LINE_NUMBER,238JDWP.SuspendPolicy.ALL);239log.display(" ... got breakpoint requestID: " + breakpointRequestID);240241// resume debuggee after testing command242log.display("Resuming debuggee");243debugee.resume();244log.display(" ... debuggee resumed");245log.display("");246247// wait for BREAKPOINT event248log.display("Waiting for BREAKPOINT event for requestID: " + breakpointRequestID);249testedThreadID = waitForBreakpointEvent(breakpointRequestID, "first");250log.display(" ... breakpoint reached with threadID: " + testedThreadID);251252// query debuggee for top frameID253log.display("Getting top frameID for threadID: " + testedThreadID);254testedFrameID = queryTopFrameID(testedThreadID);255log.display(" ... got frameID: " + testedFrameID);256log.display("");257258// tested thread is suspended by an event259log.display("Tested thread is suspended by BREAKPOINT event before pop frameID: "260+ testedFrameID);261}262263/**264* Perform testing JDWP command.265*/266void testCommand() {267// create command packet and fill requred out data268log.display("Create command packet:");269log.display("Command: " + JDWP_COMMAND_NAME);270CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);271log.display(" threadID: " + testedThreadID);272command.addObjectID(testedThreadID);273log.display(" frameID: " + testedFrameID);274command.addFrameID(testedFrameID);275command.setLength();276277// send command packet to debugee278try {279log.display("Sending command packet:\n" + command);280transport.write(command);281} catch (IOException e) {282log.complain("Unable to send command packet:\n\t" + e);283success = false;284return;285}286287// receive reply packet from debugee288ReplyPacket reply = new ReplyPacket();289try {290log.display("Waiting for reply packet");291transport.read(reply);292log.display(" ... reply packet received:\n" + reply);293} catch (IOException e) {294log.complain("Unable to read reply packet for tested command:\n\t" + e);295success = false;296return;297}298299// check reply packet header300try{301log.display("Checking header of reply packet");302reply.checkHeader(command.getPacketID());303log.display(" ... packet header is correct");304} catch (BoundException e) {305log.complain("Wrong header of reply packet for tested command:\n\t"306+ e.getMessage());307success = false;308return;309}310311// start parsing reply packet data312log.display("Parsing reply packet data:");313reply.resetPosition();314315// no reply data to parse316log.display(" no reply data");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}324325log.display(" ... packed data parsed");326}327328/**329* Check result of the tested JDWP command.330*/331void checkResult() {332// resume debuggee after sending command333log.display("Resuming debuggee after command");334debugee.resume();335log.display(" ... debuggee resumed");336337// wait for BREAKPOINT event338log.display("Waiting for second BREAKPOINT event for requestID: " + breakpointRequestID);339long threadID = waitForBreakpointEvent(breakpointRequestID, "second");340log.display(" ... breakpoint secondly reached with threadID: " + threadID);341342log.display("Tested method was invoked two times as expected");343}344345/**346* Query debuggee for top frameID value for given threadID.347*/348long queryTopFrameID(long threadID) {349String error = "Error occured while getting top frameID for threadID: " + threadID;350351CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Frames);352command.addObjectID(threadID);353command.addInt(0); // top frame index354command.addInt(1); // number of frames355356ReplyPacket reply = debugee.receiveReplyFor(command, "ThreadReference.Frames");357reply.resetPosition();358359try {360int frames = reply.getInt();361if (frames != 1) {362log.complain("Wrong number of frames returned in reply packet: " + frames363+ " (expected: " + 1 + ")");364throw new Failure(error);365}366long frameID = reply.getFrameID();367return frameID;368} catch (BoundException e) {369log.complain("Unable to extract data from reply packet:\n\t"370+ e.getMessage());371throw new Failure(error);372}373}374375/**376* Wait for BREAKPOINT event made by the given request and return threadID.377* Debuggee will be left suspended by the BREAKPOINT event.378*/379public long waitForBreakpointEvent(int requestID, String kind) {380String error = "Error occured while waiting for " + kind + " BREAKPOINT event";381382for(;;) {383EventPacket event = debugee.receiveEvent();384byte eventSuspendPolicy = 0;385long eventThreadID = 0;386try {387eventSuspendPolicy = event.getByte();388int events = event.getInt();389for (int i = 0; i < events; i++) {390// check event kind391byte eventKind = event.getByte();392if (eventKind == JDWP.EventKind.VM_DEATH) {393log.complain("Unexpected VM_DEATH event received: " + eventKind394+ " (expected: " + JDWP.EventKind.BREAKPOINT +")");395dead = true;396throw new Failure(error);397} else if (eventKind != JDWP.EventKind.BREAKPOINT) {398log.complain("Unexpected event kind received: " + eventKind399+ " (expected: " + JDWP.EventKind.BREAKPOINT +")");400throw new Failure(error);401}402403// extract specific BREAKPOINT event data404int eventRequestID = event.getInt();405eventThreadID = event.getObjectID();406JDWP.Location eventLocation = event.getLocation();407408if (eventRequestID == requestID) {409return eventThreadID;410} else {411log.complain("Unexpected BREAKPOINT event received with requestID: "412+ eventRequestID + " (expected: " + requestID + ")");413}414}415} catch (BoundException e) {416log.complain("Unable to extract data from event packet while waiting for BREAKPOINT event:\n\t"417+ e.getMessage() + "\n" + event);418throw new Failure(error);419}420421// resume debuggee after unhandled event set422debugee.resumeEvent(eventSuspendPolicy, eventThreadID);423}424}425426/**427* Disconnect debuggee and wait for it exited.428*/429void quitDebugee() {430if (debugee == null)431return;432433// disconnect debugee434if (!dead) {435try {436log.display("Disconnecting debuggee");437debugee.dispose();438log.display(" ... debuggee disconnected");439} catch (Failure e) {440log.display("Failed to finally disconnect debuggee:\n\t"441+ e.getMessage());442}443}444445// wait for debugee exited446log.display("Waiting for debuggee exit");447int code = debugee.waitFor();448log.display(" ... debuggee exited with exit code: " + code);449450// analize debugee exit status code451if (code != JCK_STATUS_BASE + PASSED) {452log.complain("Debuggee FAILED with exit code: " + code);453success = false;454}455}456}457458459