Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/EventRequest/Clear/clear001.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.EventRequest.Clear;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: EventRequest.Clear.33*34* See clear001.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 #waitForTestedEvent()42*/43public class clear001 {4445// exit status constants46static final int JCK_STATUS_BASE = 95;47static final int PASSED = 0;48static final int FAILED = 2;4950// package and classes names constants51static final String PACKAGE_NAME = "nsk.jdwp.EventRequest.Clear";52static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "clear001";53static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5455// tested JDWP command constants56static final String JDWP_COMMAND_NAME = "EventRequest.Clear";57static final int JDWP_COMMAND_ID = JDWP.Command.EventRequest.Clear;58static final byte TESTED_EVENT_KIND = JDWP.EventKind.BREAKPOINT;59static final byte TESTED_EVENT_SUSPEND_POLICY = JDWP.SuspendPolicy.ALL;6061// name and signature of the tested class62static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";63static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6465// name of field and method of tested class66static final String TESTED_METHOD_NAME = "run";67static final int BREAKPOINT_LINE = clear001a.BREAKPOINT_LINE;6869// usual scaffold objects70ArgumentHandler argumentHandler = null;71Log log = null;72Binder binder = null;73Debugee debugee = null;74Transport transport = null;75int waitTime = 0; // minutes76long timeout = 0; // milliseconds77boolean dead = false;78boolean success = true;7980// obtained data81long testedClassID = 0;82long testedMethodID = 0;83int eventRequestID = 0;8485// -------------------------------------------------------------------8687/**88* Start test from command line.89*/90public static void main(String argv[]) {91System.exit(run(argv,System.out) + JCK_STATUS_BASE);92}9394/**95* Start test from JCK-compilant environment.96*/97public static int run(String argv[], PrintStream out) {98return new clear001().runIt(argv, out);99}100101// -------------------------------------------------------------------102103/**104* Perform test execution.105*/106public int runIt(String argv[], PrintStream out) {107108// make log for debugger messages109argumentHandler = new ArgumentHandler(argv);110log = new Log(out, argumentHandler);111waitTime = argumentHandler.getWaitTime();112timeout = waitTime * 60 * 1000;113114// execute test and display results115try {116log.display("\n>>> Starting debugee \n");117118// launch debuggee119binder = new Binder(argumentHandler, log);120log.display("Launching debugee");121debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);122transport = debugee.getTransport();123log.display(" ... debugee launched");124log.display("");125126// set timeout for debuggee responces127log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");128transport.setReadTimeout(timeout);129log.display(" ... timeout set");130131// wait for debuggee started132log.display("Waiting for VM_INIT event");133debugee.waitForVMInit();134log.display(" ... VM_INIT event received");135136// query debugee for VM-dependent ID sizes137log.display("Querying for IDSizes");138debugee.queryForIDSizes();139log.display(" ... size of VM-dependent types adjusted");140141// get debuggee prepared for testing142log.display("\n>>> Get debuggee prepared for testing \n");143prepareForTest();144145// test JDWP command146log.display("\n>>> Testing JDWP command \n");147testCommand();148149log.display("\n>>> Finishing debuggee \n");150151// resume debuggee152log.display("Resuming debuggee");153debugee.resume();154log.display(" ... debuggee resumed");155156// wait for debuggee exited157log.display("Waiting for VM_DEATH event instead of BREAKPOINT event");158waitForVMDeathEvent();159160if (!dead) {161// resume debuggee after BREAKPOINT event162log.display("Resuming debuggee");163debugee.resume();164log.display(" ... debuggee resumed");165166// wait for debuggee exited167log.display("Waiting for final VM_DEATH event");168debugee.waitForVMDeath();169dead = true;170log.display(" ... VM_DEATH event received");171}172173} catch (Failure e) {174log.complain("TEST FAILED: " + e.getMessage());175success = false;176} catch (Exception e) {177e.printStackTrace(out);178log.complain("Caught unexpected exception while running the test:\n\t" + e);179success = false;180} finally {181// quit debugee182log.display("\n>>> Finishing test \n");183quitDebugee();184}185186// check test results187if (!success) {188log.complain("TEST FAILED");189return FAILED;190}191192out.println("TEST PASSED");193return PASSED;194195}196197void prepareForTest() {198// wait for tested class loaded199log.display("Waiting for tested class loaded");200testedClassID = debugee.waitForClassLoaded(TESTED_CLASS_NAME, JDWP.SuspendPolicy.ALL);201log.display(" ... got classID: " + testedClassID);202log.display("");203204// get methodID for tested method205log.display("Getting tested methodID by name: " + TESTED_METHOD_NAME);206testedMethodID = debugee.getMethodID(testedClassID, TESTED_METHOD_NAME, true);207log.display(" ... got methodID: " + testedMethodID);208209// make request for BREAKPOINT event210log.display("Making request for BREAKPOINT event at: "211+ TESTED_METHOD_NAME + ":" + BREAKPOINT_LINE);212eventRequestID = debugee.requestBreakpointEvent(JDWP.TypeTag.CLASS, testedClassID,213testedMethodID, BREAKPOINT_LINE,214JDWP.SuspendPolicy.ALL);215log.display(" ... got requestID: " + eventRequestID);216}217218219/**220* Test JDWP command.221*/222void testCommand() {223// create command packet and fill requred out data224log.display("Create command packet: " + JDWP_COMMAND_NAME);225CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);226log.display(" event: " + TESTED_EVENT_KIND);227command.addByte(TESTED_EVENT_KIND);228log.display(" requestID: " + eventRequestID);229command.addInt(eventRequestID);230command.setLength();231log.display(" ... command packet created");232233// send command packet to debugee234try {235log.display("Sending command packet:\n" + command);236transport.write(command);237log.display(" ... command packet sent");238} catch (IOException e) {239log.complain("Unable to send command packet:\n\t" + e);240success = false;241return;242}243log.display("");244245// receive reply packet from debugee246ReplyPacket reply = new ReplyPacket();247try {248log.display("Waiting for reply packet");249transport.read(reply);250log.display(" ... reply packet received:\n" + reply);251} catch (IOException e) {252log.complain("Unable to read reply packet:\n\t" + e);253success = false;254return;255}256log.display("");257258// check reply packet header259try{260log.display("Checking header of reply packet");261reply.checkHeader(command.getPacketID());262log.display(" ... packet header is correct");263} catch (BoundException e) {264log.complain("Wrong header of reply packet for tested command:\n\t"265+ e.getMessage());266success = false;267return;268}269270// start parsing reply packet data271log.display("Parsing reply packet data:");272reply.resetPosition();273274// no out data275log.display(" no out data");276277log.display(" ... packet data is parsed");278279// check for extra data in reply packet280if (!reply.isParsed()) {281log.complain("Extra trailing bytes found in reply packet at: "282+ reply.offsetString());283success = false;284}285}286287/**288* Wait for VM_DEATH event.289*/290void waitForVMDeathEvent() {291292EventPacket eventPacket = null;293294// receive reply packet from debugee295try {296log.display("Waiting for event packet");297eventPacket = debugee.getEventPacket(timeout);298log.display(" ... event packet received:\n" + eventPacket);299} catch (IOException e) {300log.complain("Unable to read tested event packet:\n\t" + e);301success = false;302return;303}304log.display("");305306// check reply packet header307try{308log.display("Checking header of event packet");309eventPacket.checkHeader();310log.display(" ... packet header is correct");311} catch (BoundException e) {312log.complain("Bad header of tested event packet:\n\t"313+ e.getMessage());314success = false;315return;316}317318// start parsing reply packet data319log.display("Parsing event packet:");320eventPacket.resetPosition();321322// get suspendPolicy value323byte suspendPolicy = 0;324try {325suspendPolicy = eventPacket.getByte();326log.display(" suspendPolicy: " + suspendPolicy);327} catch (BoundException e) {328log.complain("Unable to get suspendPolicy value from tested event packet:\n\t"329+ e.getMessage());330success = false;331return;332}333334// get events count335int events = 0;336try {337events = eventPacket.getInt();338log.display(" events: " + events);339} catch (BoundException e) {340log.complain("Unable to get events count from tested event packet:\n\t"341+ e.getMessage());342success = false;343return;344}345346// check events count347if (events < 0) {348log.complain("Negative value of events number in tested event packet: " +349events + " (expected: " + 1 + ")");350success = false;351} else if (events != 1) {352log.complain("Invalid number of events in tested event packet: " +353events + " (expected: " + 1 + ")");354success = false;355}356357// extract each event358long eventThreadID = 0;359for (int i = 0; i < events; i++) {360log.display(" event #" + i + ":");361362// get eventKind363byte eventKind = 0;364try {365eventKind = eventPacket.getByte();366log.display(" eventKind: " + eventKind);367} catch (BoundException e) {368log.complain("Unable to get eventKind of event #" + i + " from tested event packet:\n\t"369+ e.getMessage());370success = false;371return;372}373374// check eventKind375if (eventKind == JDWP.EventKind.VM_DEATH) {376log.display("Expected VM_DEATH event received intead of BREAKPOINT event");377dead = true;378return;379} else if (eventKind == JDWP.EventKind.BREAKPOINT) {380log.complain("Unexpected BREAKPOINT event received in event packet: " +381eventKind + " (expected: " + JDWP.EventKind.VM_DEATH + ")");382success = false;383} else {384log.complain("Unexpected eventKind of event " + i + " in event packet: " +385eventKind + " (expected: " + JDWP.EventKind.VM_DEATH + ")");386success = false;387return;388}389390// get requestID391int requestID = 0;392try {393requestID = eventPacket.getInt();394log.display(" requestID: " + requestID);395} catch (BoundException e) {396log.complain("Unable to get requestID of event #" + i + " from BREAKPOINT event packet:\n\t"397+ e.getMessage());398success = false;399return;400}401402// check requestID403if (requestID != eventRequestID) {404log.complain("Unexpected requestID of event " + i + " in BREAKPOINT event packet: " +405requestID + " (expected: " + eventRequestID + ")");406success = false;407}408409// get threadID410long threadID = 0;411try {412threadID = eventPacket.getObjectID();413log.display(" threadID: " + threadID);414} catch (BoundException e) {415log.complain("Unable to get threadID of event #" + i + " from BREAKPOINT event packet:\n\t"416+ e.getMessage());417success = false;418return;419}420421// get location422JDWP.Location location = null;423try {424location = eventPacket.getLocation();425log.display(" location: " + location);426} catch (BoundException e) {427log.complain("Unable to get location of event #" + i + " from BREAKPOINT event packet:\n\t"428+ e.getMessage());429success = false;430return;431}432}433434// check for extra data in event packet435if (!eventPacket.isParsed()) {436log.complain("Extra trailing bytes found in event packet at: "437+ eventPacket.offsetString());438success = false;439}440441log.display(" ... event packet parsed");442}443444445/**446* Disconnect debuggee and wait for it exited.447*/448void quitDebugee() {449if (debugee == null)450return;451452// disconnect debugee if not dead453if (!dead) {454try {455log.display("Disconnecting debuggee");456debugee.dispose();457log.display(" ... debuggee disconnected");458} catch (Failure e) {459log.display("Failed to finally disconnect debuggee:\n\t"460+ e.getMessage());461}462}463464// wait for debugee exited465log.display("Waiting for debuggee exit");466int code = debugee.waitFor();467log.display(" ... debuggee exited with exit code: " + code);468469// analize debugee exit status code470if (code != JCK_STATUS_BASE + PASSED) {471log.complain("Debuggee FAILED with exit code: " + code);472success = false;473}474}475476}477478479