Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/VirtualMachine/HoldEvents/holdevents002.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.HoldEvents;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: VirtualMachine.HoldEvents.33*34* See holdevents002.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*39* @see #runIt()40*/41public class holdevents002 {4243// exit status constants44static final int JCK_STATUS_BASE = 95;45static final int PASSED = 0;46static final int FAILED = 2;4748// package and classes names constants49static final String PACKAGE_NAME = "nsk.jdwp.VirtualMachine.HoldEvents";50static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "holdevents002";51static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5253// tested JDWP command constants54static final String JDWP_COMMAND_NAME = "VirtualMachine.HoldEvents";55static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.HoldEvents;56static final byte TESTED_EVENT_KIND = JDWP.EventKind.BREAKPOINT;57static final byte TESTED_EVENT_SUSPEND_POLICY = JDWP.SuspendPolicy.ALL;58static final byte TESTED_EVENT_MODIFIER = JDWP.EventModifierKind.LOCATION_ONLY;5960// name and signature of the tested class61static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";62static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6364// name of field and method of tested class65static final String TESTED_METHOD_NAME = "run";66static final int BREAKPOINT_LINE = holdevents002a.BREAKPOINT_LINE;6768// usual scaffold objects69ArgumentHandler argumentHandler = null;70Log log = null;71Binder binder = null;72Debugee debugee = null;73Transport transport = null;74int waitTime = 0; // minutes75long timeout = 0; // milliseconds76boolean dead = false;77boolean success = true;7879// obtained data80long testedClassID = 0;81long testedMethodID = 0;82int eventRequestID = 0;8384// -------------------------------------------------------------------8586/**87* Start test from command line.88*/89public static void main(String argv[]) {90System.exit(run(argv,System.out) + JCK_STATUS_BASE);91}9293/**94* Start test from JCK-compilant environment.95*/96public static int run(String argv[], PrintStream out) {97return new holdevents002().runIt(argv, out);98}99100// -------------------------------------------------------------------101102/**103* Perform test execution.104*/105public int runIt(String argv[], PrintStream out) {106107// make log for debugger messages108argumentHandler = new ArgumentHandler(argv);109log = new Log(out, argumentHandler);110waitTime = argumentHandler.getWaitTime();111timeout = waitTime * 60 * 1000;112113// execute test and display results114try {115log.display("\n>>> Starting debugee \n");116117// launch debuggee118binder = new Binder(argumentHandler, log);119log.display("Launching debugee");120debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);121transport = debugee.getTransport();122log.display(" ... debugee launched");123log.display("");124125// set timeout for debuggee responces126log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");127transport.setReadTimeout(timeout);128log.display(" ... timeout set");129130// wait for debuggee started131log.display("Waiting for VM_INIT event");132debugee.waitForVMInit();133log.display(" ... VM_INIT event received");134135// query debugee for VM-dependent ID sizes136log.display("Querying for IDSizes");137debugee.queryForIDSizes();138log.display(" ... size of VM-dependent types adjusted");139140// get debuggee prepared for testing141log.display("\n>>> Get debuggee prepared for testing \n");142prepareForTest();143144// test JDWP command145log.display("\n>>> Testing JDWP command \n");146147log.display("Holding events into debuggee");148holdEvents();149log.display(" ... events held");150151// resume debuggee152log.display("Resuming debuggee");153debugee.resume();154log.display(" ... debuggee resumed");155156// wait for BREAKPOINT event NOT occured157log.display("Waiting for BREAKPOINT event NOT received");158waitForBreakpointEvent();159160} catch (Failure e) {161log.complain("TEST FAILED: " + e.getMessage());162success = false;163} catch (Exception e) {164e.printStackTrace(out);165log.complain("Caught unexpected exception while running the test:\n\t" + e);166success = false;167} finally {168// quit debugee169log.display("\n>>> Finishing test \n");170quitDebugee();171}172173// check test results174if (!success) {175log.complain("TEST FAILED");176return FAILED;177}178179out.println("TEST PASSED");180return PASSED;181182}183184/**185* Prepare debuggee for testing.186*/187void prepareForTest() {188// wait for tested class loaded189log.display("Waiting for tested class loaded");190testedClassID = debugee.waitForClassLoaded(TESTED_CLASS_NAME, JDWP.SuspendPolicy.ALL);191log.display(" ... got classID: " + testedClassID);192log.display("");193194// get methodID for breakpoint method195log.display("Getting breakpoint methodID by name: " + TESTED_METHOD_NAME);196testedMethodID = debugee.getMethodID(testedClassID, TESTED_METHOD_NAME, true);197log.display(" ... got methodID: " + testedMethodID);198199// make request for BREAKPOINT event200log.display("Making request for BREAKPOINT event at: "201+ TESTED_METHOD_NAME + ":" + BREAKPOINT_LINE);202eventRequestID = debugee.requestBreakpointEvent(JDWP.TypeTag.CLASS, testedClassID,203testedMethodID, BREAKPOINT_LINE,204JDWP.SuspendPolicy.ALL);205log.display(" ... got requestID: " + eventRequestID);206}207208/**209* Hold events into debuggee.210*/211void holdEvents() {212CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.HoldEvents);213ReplyPacket reply = debugee.receiveReplyFor(command);214}215216/**217* Wait for requested BREAKPOINT event NOT occured.218*/219void waitForBreakpointEvent() {220221EventPacket eventPacket = new EventPacket();222223// receive reply packet from debugee224try {225log.display("Waiting for event packet");226transport.setReadTimeout(timeout);227transport.read(eventPacket);228log.display(" ... event packet received:\n" + eventPacket);229} catch (IOException e) {230log.display("No event packet received for default timeout:\n\t" + e);231log.display("Requested BREAKPOINT event is not received after holding events");232return;233}234235log.complain("An event received after holding events into debuggee");236log.display("");237238// check reply packet header239try{240log.display("Checking header of event packet");241eventPacket.checkHeader();242log.display(" ... packet header is correct");243} catch (BoundException e) {244log.complain("Bad header of received event packet:\n\t"245+ e.getMessage());246success = false;247return;248}249250// start parsing reply packet data251log.display("Parsing event packet:");252eventPacket.resetPosition();253254// get suspendPolicy value255byte suspendPolicy = 0;256try {257suspendPolicy = eventPacket.getByte();258log.display(" suspendPolicy: " + suspendPolicy);259} catch (BoundException e) {260log.complain("Unable to get suspendPolicy value from received event packet:\n\t"261+ e.getMessage());262success = false;263return;264}265266// get events count267int events = 0;268try {269events = eventPacket.getInt();270log.display(" events: " + events);271} catch (BoundException e) {272log.complain("Unable to get events count from received event packet:\n\t"273+ e.getMessage());274success = false;275return;276}277278// check events count279if (events < 0) {280log.complain("Negative value of events number in received event packet: " +281events + " (expected: " + 1 + ")");282success = false;283} else if (events != 1) {284log.complain("Invalid number of events in received event packet: " +285events + " (expected: " + 1 + ")");286success = false;287}288289// extract each event290long eventThreadID = 0;291for (int i = 0; i < events; i++) {292log.display(" event #" + i + ":");293294// get eventKind295byte eventKind = 0;296try {297eventKind = eventPacket.getByte();298log.display(" eventKind: " + eventKind);299} catch (BoundException e) {300log.complain("Unable to get eventKind of event #" + i + " from received event packet:\n\t"301+ e.getMessage());302success = false;303return;304}305306// check eventKind307if (eventKind == JDWP.EventKind.VM_DEATH) {308log.display("Unexpected VM_DEATH event received intead of BREAKPOINT event");309success = false;310dead = true;311return;312} else if (eventKind == JDWP.EventKind.BREAKPOINT) {313log.complain("Hold BREAKPOINT event received in event packet: " +314eventKind + " (expected: " + JDWP.EventKind.BREAKPOINT + ")");315success = false;316} else {317log.complain("Unexpected eventKind of event " + i + " in event packet: " +318eventKind + " (expected: " + JDWP.EventKind.BREAKPOINT + ")");319success = false;320return;321}322323// get requestID324int requestID = 0;325try {326requestID = eventPacket.getInt();327log.display(" requestID: " + requestID);328} catch (BoundException e) {329log.complain("Unable to get requestID of event #" + i + " from BREAKPOINT event packet:\n\t"330+ e.getMessage());331success = false;332return;333}334335// check requestID336if (requestID != eventRequestID) {337log.complain("Unexpected requestID of event " + i + " in BREAKPOINT event packet: " +338requestID + " (expected: " + eventRequestID + ")");339success = false;340}341342// get threadID343long threadID = 0;344try {345threadID = eventPacket.getObjectID();346log.display(" threadID: " + threadID);347} catch (BoundException e) {348log.complain("Unable to get threadID of event #" + i + " from BREAKPOINT event packet:\n\t"349+ e.getMessage());350success = false;351return;352}353354// get location355JDWP.Location location = null;356try {357location = eventPacket.getLocation();358log.display(" location: " + location);359} catch (BoundException e) {360log.complain("Unable to get location of event #" + i + " from BREAKPOINT event packet:\n\t"361+ e.getMessage());362success = false;363return;364}365}366367// check for extra data in event packet368if (!eventPacket.isParsed()) {369log.complain("Extra trailing bytes found in event packet at: "370+ eventPacket.offsetString());371success = false;372}373374log.display(" ... event packet parsed");375}376377/**378* Disconnect debuggee and wait for it exited.379*/380void quitDebugee() {381if (debugee == null)382return;383384// disconnect debugee if not dead385if (!dead) {386try {387log.display("Disconnecting debuggee");388debugee.dispose();389log.display(" ... debuggee disconnected");390} catch (Failure e) {391log.display("Failed to finally disconnect debuggee:\n\t"392+ e.getMessage());393}394}395396// wait for debugee exited397log.display("Waiting for debuggee exit");398int code = debugee.waitFor();399log.display(" ... debuggee exited with exit code: " + code);400401// analize debugee exit status code402if (code != JCK_STATUS_BASE + PASSED) {403log.complain("Debuggee FAILED with exit code: " + code);404success = false;405}406}407408}409410411