Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/EventRequest/Set/set001.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.Set;2425import java.io.*;2627import nsk.share.*;28import nsk.share.jpda.*;29import nsk.share.jdwp.*;3031/**32* Test for JDWP command: EventRequest.Set.33*34* See set001.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 set001 {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.Set";52static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "set001";53static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";5455// tested JDWP command constants56static final String JDWP_COMMAND_NAME = "EventRequest.Set";57static final int JDWP_COMMAND_ID = JDWP.Command.EventRequest.Set;58static final byte TESTED_EVENT_KIND = JDWP.EventKind.BREAKPOINT;59static final byte TESTED_EVENT_SUSPEND_POLICY = JDWP.SuspendPolicy.ALL;60static final byte TESTED_EVENT_MODIFIER = JDWP.EventModifierKind.LOCATION_ONLY;6162// name and signature of the tested class63static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";64static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";6566// name of field and method of tested class67static final String TESTED_METHOD_NAME = "run";68static final int BREAKPOINT_LINE = set001a.BREAKPOINT_LINE;6970// usual scaffold objects71ArgumentHandler argumentHandler = null;72Log log = null;73Binder binder = null;74Debugee debugee = null;75Transport transport = null;76int waitTime = 0; // minutes77long timeout = 0; // milliseconds78boolean dead = false;79boolean success = true;8081// obtained data82long testedClassID = 0;83long testedMethodID = 0;84int eventRequestID = 0;85JDWP.Location breakpointLocation = null;8687// -------------------------------------------------------------------8889/**90* Start test from command line.91*/92public static void main(String argv[]) {93System.exit(run(argv,System.out) + JCK_STATUS_BASE);94}9596/**97* Start test from JCK-compilant environment.98*/99public static int run(String argv[], PrintStream out) {100return new set001().runIt(argv, out);101}102103// -------------------------------------------------------------------104105/**106* Perform test execution.107*/108public int runIt(String argv[], PrintStream out) {109110// make log for debugger messages111argumentHandler = new ArgumentHandler(argv);112log = new Log(out, argumentHandler);113waitTime = argumentHandler.getWaitTime();114timeout = waitTime * 60 * 1000;115116// execute test and display results117try {118log.display("\n>>> Starting debugee \n");119120// launch debuggee121binder = new Binder(argumentHandler, log);122log.display("Launching debugee");123debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);124transport = debugee.getTransport();125log.display(" ... debugee launched");126log.display("");127128// set timeout for debuggee responces129log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");130transport.setReadTimeout(timeout);131log.display(" ... timeout set");132133// wait for debuggee started134log.display("Waiting for VM_INIT event");135debugee.waitForVMInit();136log.display(" ... VM_INIT event received");137138// query debugee for VM-dependent ID sizes139log.display("Querying for IDSizes");140debugee.queryForIDSizes();141log.display(" ... size of VM-dependent types adjusted");142143// get debuggee prepared for testing144log.display("\n>>> Get debuggee prepared for testing \n");145prepareForTest();146147// test JDWP command148log.display("\n>>> Testing JDWP command \n");149testCommand();150151if (success) {152log.display("\n>>> Checking request result \n");153154// resume debuggee155log.display("Resuming debuggee");156debugee.resume();157log.display(" ... debuggee resumed");158159// wait for BREAKPOINT event160log.display("Waiting for BREAKPOINT event");161long threadID = debugee.waitForBreakpointEvent(eventRequestID);162log.display(" ... BREAKPOINT event received with threadID: " + threadID);163}164165log.display("\n>>> Finishing debuggee \n");166167// resume debuggee168log.display("Resuming debuggee");169debugee.resume();170log.display(" ... debuggee resumed");171172// wait for debuggee exited173log.display("Waiting for VM_DEATH event");174debugee.waitForVMDeath();175dead = true;176log.display(" ... VM_DEATH event received");177178} catch (Failure e) {179log.complain("TEST FAILED: " + e.getMessage());180success = false;181} catch (Exception e) {182e.printStackTrace(out);183log.complain("Caught unexpected exception while running the test:\n\t" + e);184success = false;185} finally {186// quit debugee187log.display("\n>>> Finishing test \n");188quitDebugee();189}190191// check test results192if (!success) {193log.complain("TEST FAILED");194return FAILED;195}196197out.println("TEST PASSED");198return PASSED;199200}201202/**203* Prepare debuggee for testing.204*/205void prepareForTest() {206// wait for tested class loaded207log.display("Waiting for tested class loaded");208testedClassID = debugee.waitForClassLoaded(TESTED_CLASS_NAME, JDWP.SuspendPolicy.ALL);209log.display(" ... got classID: " + testedClassID);210log.display("");211212// get methodID for breakpoint method213log.display("Getting breakpoint methodID by name: " + TESTED_METHOD_NAME);214testedMethodID = debugee.getMethodID(testedClassID, TESTED_METHOD_NAME, true);215log.display(" ... got methodID: " + testedMethodID);216217// get codeIndex for breakpoint line218log.display("Getting code index for breakpoint line: " + BREAKPOINT_LINE);219long codeIndex = debugee.getCodeIndex(testedClassID, testedMethodID, BREAKPOINT_LINE);220log.display(" ... got breakpoint codeIndex: " + codeIndex);221222// create breakpoint location223log.display("Creating location for breakpoint at: "224+ TESTED_METHOD_NAME + ":" + BREAKPOINT_LINE);225breakpointLocation = new JDWP.Location(JDWP.TypeTag.CLASS, testedClassID,226testedMethodID, codeIndex);227log.display(" ... got breakpoint location: " + breakpointLocation);228}229230/**231* Test JDWP command.232*/233void testCommand() {234// create command packet and fill requred out data235log.display("Create command packet: " + JDWP_COMMAND_NAME);236CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);237log.display(" event: " + TESTED_EVENT_KIND);238command.addByte(TESTED_EVENT_KIND);239log.display(" suspendPolicy: " + TESTED_EVENT_SUSPEND_POLICY);240command.addByte(TESTED_EVENT_SUSPEND_POLICY);241log.display(" modifiers: " + 1);242command.addInt(1);243log.display(" modKind: " + TESTED_EVENT_MODIFIER);244command.addByte(TESTED_EVENT_MODIFIER);245log.display(" location: " + breakpointLocation);246command.addLocation(breakpointLocation);247command.setLength();248log.display(" ... command packet created");249250// send command packet to debugee251try {252log.display("Sending command packet:\n" + command);253transport.write(command);254log.display(" ... command packet sent");255} catch (IOException e) {256log.complain("Unable to send command packet:\n\t" + e);257success = false;258return;259}260log.display("");261262// receive reply packet from debugee263ReplyPacket reply = new ReplyPacket();264try {265log.display("Waiting for reply packet");266transport.read(reply);267log.display(" ... reply packet received:\n" + reply);268} catch (IOException e) {269log.complain("Unable to read reply packet:\n\t" + e);270success = false;271return;272}273log.display("");274275// check reply packet header276try{277log.display("Checking header of reply packet");278reply.checkHeader(command.getPacketID());279log.display(" ... packet header is correct");280} catch (BoundException e) {281log.complain("Wrong header of reply packet for tested command:\n\t"282+ e.getMessage());283success = false;284return;285}286287// start parsing reply packet data288log.display("Parsing reply packet data:");289reply.resetPosition();290291// extract requestID292int requestID = 0;293try {294requestID = reply.getInt();295log.display(" requestID: " + requestID);296} catch (BoundException e) {297log.complain("Unable to extract requestID from request reply packet:\n\t"298+ e.getMessage());299success = false;300}301302// check requestID303if (requestID == 0) {304log.complain("Unexpected null requestID returned: " + requestID);305success = false;306}307eventRequestID = requestID;308309log.display(" ... packet data is parsed");310311// check for extra data in reply packet312if (!reply.isParsed()) {313log.complain("Extra trailing bytes found in reply packet at: "314+ reply.offsetString());315success = false;316}317}318319/**320* Wait for VM_DEATH event.321*/322void waitForVMDeathEvent() {323324EventPacket eventPacket = null;325326// receive reply packet from debugee327try {328log.display("Waiting for event packet");329eventPacket = debugee.getEventPacket(timeout);330log.display(" ... event packet received:\n" + eventPacket);331} catch (IOException e) {332log.complain("Unable to read tested event packet:\n\t" + e);333success = false;334return;335}336log.display("");337338// check reply packet header339try{340log.display("Checking header of event packet");341eventPacket.checkHeader();342log.display(" ... packet header is correct");343} catch (BoundException e) {344log.complain("Bad header of tested event packet:\n\t"345+ e.getMessage());346success = false;347return;348}349350// start parsing reply packet data351log.display("Parsing event packet:");352eventPacket.resetPosition();353354// get suspendPolicy value355byte suspendPolicy = 0;356try {357suspendPolicy = eventPacket.getByte();358log.display(" suspendPolicy: " + suspendPolicy);359} catch (BoundException e) {360log.complain("Unable to get suspendPolicy value from tested event packet:\n\t"361+ e.getMessage());362success = false;363return;364}365366// get events count367int events = 0;368try {369events = eventPacket.getInt();370log.display(" events: " + events);371} catch (BoundException e) {372log.complain("Unable to get events count from tested event packet:\n\t"373+ e.getMessage());374success = false;375return;376}377378// check events count379if (events < 0) {380log.complain("Negative value of events number in tested event packet: " +381events + " (expected: " + 1 + ")");382success = false;383} else if (events != 1) {384log.complain("Invalid number of events in tested event packet: " +385events + " (expected: " + 1 + ")");386success = false;387}388389// extract each event390long eventThreadID = 0;391for (int i = 0; i < events; i++) {392log.display(" event #" + i + ":");393394// get eventKind395byte eventKind = 0;396try {397eventKind = eventPacket.getByte();398log.display(" eventKind: " + eventKind);399} catch (BoundException e) {400log.complain("Unable to get eventKind of event #" + i + " from tested event packet:\n\t"401+ e.getMessage());402success = false;403return;404}405406// check eventKind407if (eventKind == JDWP.EventKind.VM_DEATH) {408log.display("Expected VM_DEATH event received intead of BREAKPOINT event");409dead = true;410return;411} else if (eventKind == JDWP.EventKind.BREAKPOINT) {412log.complain("Unexpected BREAKPOINT event received in event packet: " +413eventKind + " (expected: " + JDWP.EventKind.VM_DEATH + ")");414success = false;415} else {416log.complain("Unexpected eventKind of event " + i + " in event packet: " +417eventKind + " (expected: " + JDWP.EventKind.VM_DEATH + ")");418success = false;419return;420}421422// get requestID423int requestID = 0;424try {425requestID = eventPacket.getInt();426log.display(" requestID: " + requestID);427} catch (BoundException e) {428log.complain("Unable to get requestID of event #" + i + " from BREAKPOINT event packet:\n\t"429+ e.getMessage());430success = false;431return;432}433434// check requestID435if (requestID != eventRequestID) {436log.complain("Unexpected requestID of event " + i + " in BREAKPOINT event packet: " +437requestID + " (expected: " + eventRequestID + ")");438success = false;439}440441// get threadID442long threadID = 0;443try {444threadID = eventPacket.getObjectID();445log.display(" threadID: " + threadID);446} catch (BoundException e) {447log.complain("Unable to get threadID of event #" + i + " from BREAKPOINT event packet:\n\t"448+ e.getMessage());449success = false;450return;451}452453// get location454JDWP.Location location = null;455try {456location = eventPacket.getLocation();457log.display(" location: " + location);458} catch (BoundException e) {459log.complain("Unable to get location of event #" + i + " from BREAKPOINT event packet:\n\t"460+ e.getMessage());461success = false;462return;463}464}465466// check for extra data in event packet467if (!eventPacket.isParsed()) {468log.complain("Extra trailing bytes found in event packet at: "469+ eventPacket.offsetString());470success = false;471}472473log.display(" ... event packet parsed");474}475476477/**478* Disconnect debuggee and wait for it exited.479*/480void quitDebugee() {481if (debugee == null)482return;483484// disconnect debugee if not dead485if (!dead) {486try {487log.display("Disconnecting debuggee");488debugee.dispose();489log.display(" ... debuggee disconnected");490} catch (Failure e) {491log.display("Failed to finally disconnect debuggee:\n\t"492+ e.getMessage());493}494}495496// wait for debugee exited497log.display("Waiting for debuggee exit");498int code = debugee.waitFor();499log.display(" ... debuggee exited with exit code: " + code);500501// analize debugee exit status code502if (code != JCK_STATUS_BASE + PASSED) {503log.complain("Debuggee FAILED with exit code: " + code);504success = false;505}506}507508}509510511