Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/EventRequest/ClearAllBreakpoints/clrallbreakp001.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.ClearAllBreakpoints;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: EventRequest.ClearAllBreakpoints.33*34* See clrallbreakp001.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 clrallbreakp001 {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.ClearAllBreakpoints";52static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "clrallbreakp001";53static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5455// tested JDWP command constants56static final String JDWP_COMMAND_NAME = "EventRequest.ClearAllBreakpoints";57static final int JDWP_COMMAND_ID = JDWP.Command.EventRequest.ClearAllBreakpoints;58static final byte TESTED_EVENT_KIND = JDWP.EventKind.VM_START;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 = clrallbreakp001a.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 clrallbreakp001().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 fianl 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(" no out data");227command.setLength();228log.display(" ... command packet created");229230// send command packet to debugee231try {232log.display("Sending command packet:\n" + command);233transport.write(command);234log.display(" ... command packet sent");235} catch (IOException e) {236log.complain("Unable to send command packet:\n\t" + e);237success = false;238return;239}240log.display("");241242// receive reply packet from debugee243ReplyPacket reply = new ReplyPacket();244try {245log.display("Waiting for reply packet");246transport.read(reply);247log.display(" ... reply packet received:\n" + reply);248} catch (IOException e) {249log.complain("Unable to read reply packet:\n\t" + e);250success = false;251return;252}253log.display("");254255// check reply packet header256try{257log.display("Checking header of reply packet");258reply.checkHeader(command.getPacketID());259log.display(" ... packet header is correct");260} catch (BoundException e) {261log.complain("Wrong header of reply packet for tested command:\n\t"262+ e.getMessage());263success = false;264return;265}266267// start parsing reply packet data268log.display("Parsing reply packet data:");269reply.resetPosition();270271// no out data272log.display(" no out data");273274log.display(" ... packet data is parsed");275276// check for extra data in reply packet277if (!reply.isParsed()) {278log.complain("Extra trailing bytes found in reply packet at: "279+ reply.offsetString());280success = false;281}282}283284/**285* Wait for VM_DEATH event.286*/287void waitForVMDeathEvent() {288289EventPacket eventPacket = null;290291// receive reply packet from debugee292try {293log.display("Waiting for event packet");294eventPacket = debugee.getEventPacket(timeout);295log.display(" ... event packet received:\n" + eventPacket);296} catch (IOException e) {297log.complain("Unable to read tested event packet:\n\t" + e);298success = false;299return;300}301log.display("");302303// check reply packet header304try{305log.display("Checking header of event packet");306eventPacket.checkHeader();307log.display(" ... packet header is correct");308} catch (BoundException e) {309log.complain("Bad header of tested event packet:\n\t"310+ e.getMessage());311success = false;312return;313}314315// start parsing reply packet data316log.display("Parsing event packet:");317eventPacket.resetPosition();318319// get suspendPolicy value320byte suspendPolicy = 0;321try {322suspendPolicy = eventPacket.getByte();323log.display(" suspendPolicy: " + suspendPolicy);324} catch (BoundException e) {325log.complain("Unable to get suspendPolicy value from tested event packet:\n\t"326+ e.getMessage());327success = false;328return;329}330331// get events count332int events = 0;333try {334events = eventPacket.getInt();335log.display(" events: " + events);336} catch (BoundException e) {337log.complain("Unable to get events count from tested event packet:\n\t"338+ e.getMessage());339success = false;340return;341}342343// check events count344if (events < 0) {345log.complain("Negative value of events number in tested event packet: " +346events + " (expected: " + 1 + ")");347success = false;348} else if (events != 1) {349log.complain("Invalid number of events in tested event packet: " +350events + " (expected: " + 1 + ")");351success = false;352}353354// extract each event355long eventThreadID = 0;356for (int i = 0; i < events; i++) {357log.display(" event #" + i + ":");358359// get eventKind360byte eventKind = 0;361try {362eventKind = eventPacket.getByte();363log.display(" eventKind: " + eventKind);364} catch (BoundException e) {365log.complain("Unable to get eventKind of event #" + i + " from tested event packet:\n\t"366+ e.getMessage());367success = false;368return;369}370371// check eventKind372if (eventKind == JDWP.EventKind.VM_DEATH) {373log.display("Expected VM_DEATH event received intead of BREAKPOINT event");374dead = true;375return;376} else if (eventKind == JDWP.EventKind.BREAKPOINT) {377log.complain("Unexpected BREAKPOINT event received in event packet: " +378eventKind + " (expected: " + JDWP.EventKind.VM_DEATH + ")");379success = false;380} else {381log.complain("Unexpected eventKind of event " + i + " in event packet: " +382eventKind + " (expected: " + JDWP.EventKind.VM_DEATH + ")");383success = false;384return;385}386387// get requestID388int requestID = 0;389try {390requestID = eventPacket.getInt();391log.display(" requestID: " + requestID);392} catch (BoundException e) {393log.complain("Unable to get requestID of event #" + i + " from BREAKPOINT event packet:\n\t"394+ e.getMessage());395success = false;396return;397}398399// check requestID400if (requestID != eventRequestID) {401log.complain("Unexpected requestID of event " + i + " in BREAKPOINT event packet: " +402requestID + " (expected: " + eventRequestID + ")");403success = false;404}405406// get threadID407long threadID = 0;408try {409threadID = eventPacket.getObjectID();410log.display(" threadID: " + threadID);411} catch (BoundException e) {412log.complain("Unable to get threadID of event #" + i + " from BREAKPOINT event packet:\n\t"413+ e.getMessage());414success = false;415return;416}417418// get location419JDWP.Location location = null;420try {421location = eventPacket.getLocation();422log.display(" location: " + location);423} catch (BoundException e) {424log.complain("Unable to get location of event #" + i + " from BREAKPOINT event packet:\n\t"425+ e.getMessage());426success = false;427return;428}429}430431// check for extra data in event packet432if (!eventPacket.isParsed()) {433log.complain("Extra trailing bytes found in event packet at: "434+ eventPacket.offsetString());435success = false;436}437438log.display(" ... event packet parsed");439}440441442/**443* Disconnect debuggee and wait for it exited.444*/445void quitDebugee() {446if (debugee == null)447return;448449// disconnect debugee if not dead450if (!dead) {451try {452log.display("Disconnecting debuggee");453debugee.dispose();454log.display(" ... debuggee disconnected");455} catch (Failure e) {456log.display("Failed to finally disconnect debuggee:\n\t"457+ e.getMessage());458}459}460461// wait for debugee exited462log.display("Waiting for debuggee exit");463int code = debugee.waitFor();464log.display(" ... debuggee exited with exit code: " + code);465466// analize debugee exit status code467if (code != JCK_STATUS_BASE + PASSED) {468log.complain("Debuggee FAILED with exit code: " + code);469success = false;470}471}472473}474475476