Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/ConnectorTest.java
41161 views
/*1* Copyright (c) 2006, 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*/22package nsk.share.jdi;2324import com.sun.jdi.connect.*;25import com.sun.jdi.*;26import java.io.*;27import java.util.*;2829import nsk.share.*;30import nsk.share.jpda.*;3132/*33* This class contains several common methods used by connector tests.34*/35public abstract class ConnectorTest {36protected Log log;3738protected VirtualMachine vm;3940protected int attempts; // attempts to connect to the debuggee VM4142protected int delay; // delay between connection attempts4344protected IORedirector outRedirector;4546protected IORedirector errRedirector;4748protected ArgHandler argHandler;4950protected boolean isTestFailed;5152// set test status 'FAILED'53protected void testFailed() {54isTestFailed = true;55}5657// check if tested functionality implemented on current platform58protected boolean shouldPass() {59return argHandler.shouldPass(getConnectorName());60}6162abstract protected void doTest();6364abstract protected String getConnectorName();6566abstract protected String getDebuggeeClass();6768static public class ArgHandler extends ArgumentHandler {69public ArgHandler(String[] args) {70super(args);7172}7374protected boolean checkOption(String option, String value) {75if (super.checkOption(option, value))76return true;7778if (option.equals("testWorkDir"))79return true;8081if (option.equals("waitVMStartEvent"))82return true;8384return false;85}8687public String getTestWorkDir() {88String dir = options.getProperty("testWorkDir");8990if (dir.endsWith(File.separator)) {91dir = dir.substring(0, dir.length() - 1);92}9394return dir;95}9697public boolean waitVMStartEvent() {98return options.containsKey("waitVMStartEvent");99}100}101102/*103* Subclasses can provide another ArgumentHandlers104*/105protected ArgHandler createArgumentHandler(String[] args) {106return new ArgHandler(args);107}108109protected void init(String[] args, PrintStream out) {110argHandler = createArgumentHandler(args);111112log = new Log(out, argHandler);113114delay = argHandler.getConnectionDelay();115116// calculate number of connection attempts to not exceed WAITTIME117long timeout = argHandler.getWaitTime() * 60 * 1000;118attempts = (int) (timeout / delay);119}120121protected int runIt(String argv[], PrintStream out) {122try {123init(argv, out);124125if (shouldPass()) {126log.display("Tested functionality isn't implemented on this platform. Treat test as passed.");127return Consts.TEST_PASSED;128}129130doTest();131132if (isTestFailed)133return Consts.TEST_FAILED;134else135return Consts.TEST_PASSED;136137} catch (Throwable t) {138out.println("Unexpected exception: " + t);139t.printStackTrace(out);140return Consts.TEST_FAILED;141}142}143144protected void waitForVMInit(VirtualMachine vm) {145Debugee.waitForVMInit(vm, log, argHandler.getWaitTime() * 60 * 1000);146}147148// set connector argument value with given name149protected void setConnectorArg(Map<String, Connector.Argument> args, String argName, String value) {150for (String key : args.keySet()) {151Connector.Argument arg = args.get(key);152if (arg.name().equals(argName)) {153arg.setValue(value);154return;155}156}157158throw new Error("There is no argument '" + argName + "'");159}160161// try attach to target VM using attaching connector162protected VirtualMachine tryAttach(AttachingConnector connector, Map<String, Connector.Argument> cArgs) {163// make several attempts to connect to the debuggee VM until WAITTIME exceeds164for (int i = 0; i < attempts; i++) {165try {166return connector.attach(cArgs);167} catch (IOException e) {168// could not connect; sleep a few and make new attempt169log.display("Connection attempt #" + i + " failed: " + e);170e.printStackTrace(log.getOutStream());171try {172Thread.sleep(delay);173} catch (InterruptedException ie) {174testFailed();175log.complain("TEST INCOMPLETE: interrupted sleep: " + ie);176ie.printStackTrace(log.getOutStream());177}178} catch (IllegalConnectorArgumentsException e) {179testFailed();180log.complain("TEST: Illegal connector arguments: " + e.getMessage());181return null;182} catch (Exception e) {183testFailed();184log.complain("TEST: Internal error: " + e.getMessage());185e.printStackTrace(log.getOutStream());186return null;187}188}189190testFailed();191// return null after all attempts failed192log.complain("FAILURE: all attempts to connect to the debuggee VM failed");193return null;194}195196// try find connector with given name197protected Connector findConnector(String connectorName) {198List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();199Iterator<Connector> iter = connectors.iterator();200201while (iter.hasNext()) {202Connector connector = (Connector) iter.next();203if (connector.name().equals(connectorName)) {204log.display("Connector name=" + connector.name() + "\n\tdescription=" + connector.description() + "\n\ttransport="205+ connector.transport().name());206return connector;207}208}209throw new Error("No appropriate connector");210}211212// wait when debuggee process finishes and check exit code213protected void waitDebuggeeExit(Debugee debuggee) {214log.display("\nwaiting for debuggee VM exit");215int code = debuggee.waitFor();216if (code != (Consts.JCK_STATUS_BASE + Consts.TEST_PASSED)) {217testFailed();218log.complain("Debuggee VM has crashed: exit code=" + code);219return;220}221log.display("debuggee VM: exit code=" + code);222}223224// wait 'READY' command from debuggee VM (this method is used by debuggers establishing socket connection with debuggee VM)225protected boolean waitReadyCommand(IOPipe pipe) {226String command = pipe.readln();227log.display("Command: " + command);228229if (!command.equals(AbstractDebuggeeTest.COMMAND_READY)) {230testFailed();231log.complain("Unexpected debuggee answer: " + command + ", expected is " + AbstractDebuggeeTest.COMMAND_READY);232return false;233}234235return true;236}237}238239240