Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/VirtualMachine/Exit/exit001.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.Exit;2425import java.io.*;26import java.util.*;2728import nsk.share.*;29import nsk.share.jpda.*;30import nsk.share.jdwp.*;3132public class exit001 {33static final int JCK_STATUS_BASE = 95;34static final int PASSED = 0;35static final int FAILED = 2;36static final String PACKAGE_NAME = "nsk.jdwp.VirtualMachine.Exit";37static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "exit001";38static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";3940static final String JDWP_COMMAND_NAME = "VirtualMachine.Exit";41static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.Exit;4243public static void main (String argv[]) {44System.exit(run(argv,System.out) + JCK_STATUS_BASE);45}4647public static int run(String argv[], PrintStream out) {48return new exit001().runIt(argv, out);49}5051public int runIt(String argv[], PrintStream out) {5253boolean success = true;5455try {56final ArgumentHandler argumentHandler = new ArgumentHandler(argv);57final Log log = new Log(out, argumentHandler);5859try {6061Binder binder = new Binder(argumentHandler, log);62log.display("Start debugee VM");63final Debugee debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);64Transport transport = debugee.getTransport();65IOPipe pipe = debugee.createIOPipe();6667log.display("Waiting for VM_INIT event");68debugee.waitForVMInit();6970log.display("Querying for IDSizes");71debugee.queryForIDSizes();7273log.display("Resume debugee VM");74debugee.resume();7576log.display("Waiting for command: " + "ready");77String cmd = pipe.readln();78log.display("Received command: " + cmd);798081// Linux returns only 8 least significant bits of exit status - see #4459019 bug.82int expectedExitCode = 0x0F;8384// begin test of JDWP command8586try {87log.display("Creating command packet for " + JDWP_COMMAND_NAME88+ " with exit code: " + expectedExitCode);89CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);90command.addInt(expectedExitCode);91command.setLength();9293log.display("Sending command packet:\n" + command);94transport.write(command);9596log.display("Waiting for reply packet");97ReplyPacket reply = new ReplyPacket();98transport.read(reply);99log.display("Reply packet received:\n" + reply);100101log.display("Checking reply packet header");102reply.checkHeader(command.getPacketID());103104log.display("Parsing reply packet:");105reply.resetPosition();106107if (! reply.isParsed()) {108log.complain("Extra bytes in reply packet at: " + reply.currentPosition());109success = false;110}111} catch (Exception e) {112log.complain("Exception catched: " + e);113success = false;114}115116// check if debugee has been actually terminated117118if (success) {119int waittime = argumentHandler.getWaitTime() * 60; // seconds120int pause = 1; // seconds121int tries = waittime / pause;122success = false;123124for (int i = 0; i < tries; i++) {125if (debugee.terminated()) {126log.display("Debugee has been terminated successfully");127success = true;128break;129}130try {131Thread.currentThread().sleep(pause * 1000);132} catch (InterruptedException e) {133log.complain("Interrupted exception catched: " + e);134}135}136if (! success) {137log.complain("Debugee has not been terminated by request");138}139}140141// check if debugee exit code is as expected142143if (success) {144int exitCode = debugee.getStatus();145log.display("Debugee exit code is: " + exitCode);146if (exitCode != expectedExitCode) {147log.complain("Debugee exit code is not equal to expected: " + expectedExitCode);148success = false;149}150}151152// end test of JDWP command153154} catch (Exception e) {155log.complain("Unexpected exception: " + e);156e.printStackTrace(out);157success = false;158}159160if (!success) {161log.complain("TEST FAILED");162return FAILED;163}164165} catch (Exception e) {166out.println("Unexpected exception: " + e);167e.printStackTrace(out);168out.println("TEST FAILED");169return FAILED;170}171172out.println("TEST PASSED");173return PASSED;174175}176177}178179180