Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdb/JdbTest.java
41161 views
/*1* Copyright (c) 2002, 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.share.jdb;2425import nsk.share.*;26import nsk.share.jpda.*;2728import java.io.*;29import java.util.*;3031public abstract class JdbTest {32public static final int PASSED = 0; // Exit code for passed test33public static final int FAILED = 2; // Exit code for failed test34public static final int JCK_STATUS_BASE = 95; // Standard JCK-compatible exit code bias3536/* Flag if the test passes */37protected boolean success = true;3839/* Flag if debuggee should fail in a test */40protected static boolean debuggeeShouldFail = false;4142/* Handler of command line arguments. */43protected static JdbArgumentHandler argumentHandler = null;4445/* Log class to print log messages. */46protected static Log log = null;4748protected static Jdb jdb = null;49protected static Debuggee debuggee = null;50protected static Launcher launcher = null;51protected static String debuggeeClass = "";52protected static String firstBreak = "";53protected static String lastBreak = "";54protected static String compoundPromptIdent = null;5556/* Constructors */57protected JdbTest (boolean debuggeeShouldFail) {58this.debuggeeShouldFail = debuggeeShouldFail;59}6061protected JdbTest () {62this.debuggeeShouldFail = false;63}6465abstract protected void runCases();6667protected boolean shouldPass() {68return false;69}7071protected void failure(String errMessage) {72success = false;73log.complain(errMessage);74}7576protected void display(String message) {77log.display(message);78}7980protected void launchJdbAndDebuggee(String debuggeeClass) throws Exception {81launcher = new Launcher(argumentHandler, log);82launcher.launchJdbAndDebuggee(debuggeeClass);83jdb = launcher.getJdb();8485if (jdb == null) {86throw new Failure("jdb object points to null");87}88if (debuggeeClass != null) {89if (jdb.terminated()) {90throw new Failure("jdb exited before testing with code " + jdb.waitFor());91}9293if (argumentHandler.isAttachingConnector() || argumentHandler.isListeningConnector()) {94debuggee = launcher.getDebuggee();9596if (debuggee.terminated()) {97throw new Failure("Debuggee exited before testing");98}99}100}101}102103protected void initJdb() {104String[] reply;105106jdb.setCompoundPromptIdent(compoundPromptIdent);107108// wait for prompts after connection established109if (argumentHandler.isAttachingConnector() || argumentHandler.isListeningConnector()) {110// wait for two prompts (after connection established and VM_INIT received)111jdb.waitForPrompt(0, false, 2);112} else if (argumentHandler.isLaunchingConnector()) {113// wait for one prompt (after connection established)114jdb.waitForPrompt(0, false);115} else {116throw new TestBug("Unexpected connector kind: " + argumentHandler.getConnectorType());117}118119display("Setting first breakpoint");120jdb.setDeferredBreakpointInMethod(firstBreak);121122display("Starting debuggee class");123jdb.startDebuggeeClass();124}125126protected void afterJdbExit() {127}128129protected int runTest(String argv[], PrintStream out) {130try {131argumentHandler = new JdbArgumentHandler(argv);132log = new Log(out, argumentHandler);133134if (shouldPass()) {135log.println("TEST PASSED");136return PASSED;137}138139try {140launchJdbAndDebuggee(debuggeeClass);141142try {143initJdb();144145/* START OF TEST CASES */146display("Test cases starts.");147148runCases();149150display("Test cases ends.");151/* END OF TEST CASES */152153} catch (DebuggeeUncaughtException ex) {154jdb.quit();155throw new TestFailure(ex);156} catch (Exception e) {157failure("Caught unexpected exception while executing the test: " + e);158e.printStackTrace(log.getOutStream());159} finally {160display("Waiting for jdb exits");161int code = jdb.waitFor(argumentHandler.getWaitTime() * 60 * 1000);162if (code == PASSED) {163display("jdb normally exited");164afterJdbExit();165} else if (code == LocalProcess.PROCESS_IS_ALIVE) {166failure("jdb did not exit after timeout.");167if (!jdb.terminated()) {168display("Sending quit command to jdb.");169jdb.quit();170} else {171throw new TestBug("code PROCESS_IS_ALIVE is returned for terminated jdb");172}173} else {174failure("jdb abnormally exited with code: " + code);175}176jdb = null;177178if (debuggee != null179&& (argumentHandler.isAttachingConnector()180|| argumentHandler.isListeningConnector())) {181display("Waiting for debuggee exits");182code = debuggee.waitForDebuggee();183if (debuggeeShouldFail) {184if (code == JCK_STATUS_BASE + PASSED) {185failure("Debuggee PASSED with exit code: " + code + " but should fail");186} else {187display("Debuggee FAILED as expected with exit code: " + code);188}189} else {190if (code == JCK_STATUS_BASE + PASSED) {191display("Debuggee PASSED with exit code: " + code);192} else {193failure("Debuggee FAILED with exit code: " + code);194}195}196// debuggee = null;197}198}199200} catch (Exception e) {201failure("Caught unexpected exception: " + e);202e.printStackTrace(out);203204if (jdb != null) {205try {206jdb.finalize();207} catch (Throwable ex) {208failure("Caught exception/error while finalization of jdb:\n\t" + ex);209ex.printStackTrace(log.getOutStream());210}211} else {212log.complain("jdb reference is null, cannot run jdb.finalize() method");213}214215if (debuggee != null) {216debuggee.killDebuggee();217} else {218log.complain("debuggee reference is null, cannot run debuggee.finalize() method");219}220221}222223if (!success) {224log.complain("TEST FAILED");225return FAILED;226}227228} catch (Exception e) {229out.println("Caught unexpected exception while starting the test: " + e);230e.printStackTrace(out);231out.println("TEST FAILED");232return FAILED;233}234out.println("TEST PASSED");235return PASSED;236}237}238239240