Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ExceptionEvent/catchLocation/location002.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.jdi.ExceptionEvent.catchLocation;2425import com.sun.jdi.*;26import com.sun.jdi.event.*;27import com.sun.jdi.request.*;2829import java.io.*;30import java.util.Iterator;31import java.util.List;3233import nsk.share.*;34import nsk.share.jpda.*;35import nsk.share.jdi.*;363738// This class is the debugger in the test3940public class location002 {41static final int PASSED = 0;42static final int FAILED = 2;43static final int JCK_STATUS_BASE = 95;4445// time interval to wait for events46static final int TIMEOUT_DELTA = 1000; // milliseconds4748// synchronization commands49static final String COMMAND_READY = "ready";50static final String COMMAND_QUIT = "quit";51static final String COMMAND_GO = "go";52static final String COMMAND_DONE = "done";53static final String COMMAND_ERROR = "error";5455// checked classes names56static final String DEBUGGEE_NAME = "nsk.jdi.ExceptionEvent.catchLocation.location002a";57static final String JAVA_EXCEPTION = "java.lang.NumberFormatException";5859// scaffold objects60static private Debugee debuggee;61static private VirtualMachine vm;62static private IOPipe pipe;63static private Log log;64static private ArgumentHandler argHandler;65static private EventSet eventSet;6667// timeout for waiting events68static private long eventTimeout;6970// mirrors for checked classes and threads in debuggee71static private ExceptionRequest checkedRequest;72static private ReferenceType checkedClass;73static private ThreadReference checkedThread;7475// results of test execution76static private boolean eventsReceived;77static private boolean testFailed;7879// execute test from command line80public static void main (String args[]) {81System.exit(run(args, System.out) + JCK_STATUS_BASE);82}8384// execute test from JCK-compatible harness85public static int run(final String args[], final PrintStream out) {8687testFailed = false;8889argHandler = new ArgumentHandler(args);90log = new Log(out, argHandler);91eventTimeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds9293// launch debuggee94Binder binder = new Binder(argHandler, log);95log.display("Connecting to debuggee");96debuggee = binder.bindToDebugee(DEBUGGEE_NAME);97debuggee.redirectStderr(log, "location001a >");9899// create synchronization channel with debuggee100pipe = debuggee.createIOPipe();101vm = debuggee.VM();102EventRequestManager erManager = vm.eventRequestManager();103104// resume debuggee105log.display("Resuming debuggee");106debuggee.resume();107108// catch exceptions while testing and finally quit debuggee109try {110111// wait for debuggee started112log.display("Waiting for command: " + COMMAND_READY);113String command = pipe.readln();114if (!command.equals(COMMAND_READY)) {115throw new Failure("TEST BUG: unexpected debuggee's command: " + command);116}117118// get mirrors of checked classes in debuggee119log.display("Getting loaded classes in debuggee");120if ((checkedClass = debuggee.classByName(DEBUGGEE_NAME)) == null) {121throw new Failure("TEST BUG: cannot find " + DEBUGGEE_NAME);122}123124// get mirror of main thread in debuggee125log.display("Getting reference to main thread");126Iterator threadIterator = vm.allThreads().iterator();127while (threadIterator.hasNext()) {128ThreadReference curThread = (ThreadReference) threadIterator.next();129if (curThread.name().equals("main")) {130checkedThread = curThread;131}132}133if (checkedThread == null) {134throw new Failure("TEST BUG: unable to find reference to main thread");135}136137// create ExceptionRequest for all throwable classes (initially disabled)138log.display("Creating ExceptionRequest");139boolean notifyCaught = true;140boolean notifyUncaught = true;141if ((checkedRequest = erManager.createExceptionRequest(null, notifyCaught, notifyUncaught)) == null) {142throw new Failure("TEST BUG: unable to create ExceptionRequest");143}144145// define separate thread for handling received events146class EventHandler extends Thread {147public void run() {148// handle events until expected event received149eventHandlingLoop:150while (!eventsReceived) {151eventSet = null;152try {153eventSet = vm.eventQueue().remove(TIMEOUT_DELTA);154} catch (InterruptedException e) {155log.complain("Unexpected InterruptedException while receiving event: " + e);156break;157}158159if (eventSet == null) {160continue;161}162163// handle each event from event set164EventIterator eventIterator = eventSet.eventIterator();165while (eventIterator.hasNext()) {166167Event event = eventIterator.nextEvent();168// log.display("Event received:\n " + event);169170// break event handling loop if VMDisconnectEvent received171if (event instanceof VMDisconnectEvent) {172log.display("VMDisconnectEvent received");173break eventHandlingLoop;174}175176// handle ExceptionEvent177if (event instanceof ExceptionEvent) {178log.display("ExceptionEvent received");179180ExceptionEvent castedEvent = (ExceptionEvent)event;181ReferenceType eventRefType = castedEvent.exception().referenceType();182183String eventTypeName = eventRefType.name();184if (eventTypeName.equals(JAVA_EXCEPTION)) {185log.display("Expected ExceptionEvent for " + JAVA_EXCEPTION + " is received");186eventsReceived = true;187188Location catchLocation = castedEvent.catchLocation();189if (catchLocation != null) {190log.complain("FAILURE 1: catchLocation for uncaught " + JAVA_EXCEPTION +191" is not null :");192log.complain("declaring type: " + catchLocation.declaringType().name());193log.complain("line number : " + catchLocation.lineNumber());194}195196}197}198199// ignore each other events200201} // end of event handling loop202203// log.display("Resuming event set");204eventSet.resume();205206} // end of event set handling loop207208log.display("eventHandler completed");209210} // end of run()211212} // end of EventHandler213214// start EventHandler thread215EventHandler eventHandler = new EventHandler();216log.display("Starting eventHandler");217eventHandler.start();218219// enable event request220log.display("Enabling ExceptionRequest");221checkedRequest.enable();222223// force debuggee to throw exceptions and generate events224log.display("Sending command: " + COMMAND_GO);225pipe.println(COMMAND_GO);226227// wait for all expected events received or timeout exceeds228log.display("Waiting for all expected events received");229try {230eventHandler.join(eventTimeout);231if (eventHandler.isAlive()) {232log.complain("FAILURE 20: Timeout for waiting event was exceeded");233eventHandler.interrupt();234testFailed = true;235}236} catch (InterruptedException e) {237log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for eventHandler's death");238testFailed = true;239}240241// complain if not all expected events received242if (!eventsReceived) {243log.complain("FAILURE 2: " + JAVA_EXCEPTION + " was not received");244testFailed= true;245}246247// end testing248249} catch (Failure e) {250log.complain("TEST FAILURE: " + e.getMessage());251testFailed = true;252} catch (Exception e) {253log.complain("Unexpected exception: " + e);254e.printStackTrace(out);255testFailed = true;256} finally {257258log.display("");259260// wait for debuggee exits and analize its exit code261log.display("Waiting for debuggee terminating");262int debuggeeStatus = debuggee.endDebugee();263log.display("Debuggee terminated with exit code: " + debuggeeStatus);264}265266// check test results267if (testFailed) {268log.complain("TEST FAILED");269return FAILED;270}271272log.display("TEST PASSED");273return PASSED;274}275}276277278