Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ExceptionEvent/catchLocation/location001.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 location001 {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.location001a";5758static final String USER_EXCEPTION = DEBUGGEE_NAME + "Exception";59static final String USER_ERROR = DEBUGGEE_NAME + "Error";60static final String USER_THROWABLE = DEBUGGEE_NAME + "Throwable";61static final String JAVA_EXCEPTION = "java.lang.NumberFormatException";62static final String JAVA_ERROR = "java.lang.StackOverflowError";6364// scaffold objects65static private Debugee debuggee;66static private VirtualMachine vm;67static private IOPipe pipe;68static private Log log;69static private ArgumentHandler argHandler;70static private EventSet eventSet;7172// timeout for waiting events73static private long eventTimeout;7475// mirrors for checked classes and threads in debuggee76static private ExceptionRequest checkedRequest;77static private ReferenceType checkedClass;78static private ThreadReference checkedThread;7980static private ReferenceType userException;81static private ReferenceType userError;82static private ReferenceType userThrowable;838485// results of receiving particular events86static private boolean userExceptionReceived;87static private boolean userErrorReceived;88static private boolean userThrowableReceived;89static private boolean javaExceptionReceived;90static private boolean javaErrorReceived;9192// results of test execution93static private boolean eventsReceived;94static private boolean testFailed;9596// flag for EventHandler thread97static private volatile boolean exceptionsThrown;9899// execute test from command line100public static void main (String args[]) {101System.exit(run(args, System.out) + JCK_STATUS_BASE);102}103104// execute test from JCK-compatible harness105public static int run(final String args[], final PrintStream out) {106107testFailed = false;108userExceptionReceived = false;109userErrorReceived = false;110userThrowableReceived = false;111javaExceptionReceived = false;112javaErrorReceived = false;113eventsReceived = false;114115argHandler = new ArgumentHandler(args);116log = new Log(out, argHandler);117eventTimeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds118119// launch debuggee120Binder binder = new Binder(argHandler, log);121log.display("Connecting to debuggee");122debuggee = binder.bindToDebugee(DEBUGGEE_NAME);123debuggee.redirectStderr(log, "location001a >");124125// create synchronization channel with debuggee126pipe = debuggee.createIOPipe();127vm = debuggee.VM();128EventRequestManager erManager = vm.eventRequestManager();129130// resume debuggee131log.display("Resuming debuggee");132debuggee.resume();133134// catch exceptions while testing and finally quit debuggee135try {136137// wait for debuggee started138log.display("Waiting for command: " + COMMAND_READY);139String command = pipe.readln();140if (!command.equals(COMMAND_READY)) {141throw new Failure("TEST BUG: unexpected debuggee's command: " + command);142}143144// get mirrors of checked classes in debuggee145log.display("Getting loaded classes in debuggee");146if ((checkedClass = debuggee.classByName(DEBUGGEE_NAME)) == null) {147throw new Failure("TEST BUG: cannot find " + DEBUGGEE_NAME);148}149150if ((userException = debuggee.classByName(USER_EXCEPTION)) == null) {151throw new Failure("TEST BUG: cannot find " + USER_EXCEPTION);152}153154if ((userError = debuggee.classByName(USER_ERROR)) == null) {155throw new Failure("TEST BUG: cannot find " + USER_ERROR);156}157158if ((userThrowable = debuggee.classByName(USER_THROWABLE)) == null) {159throw new Failure("TEST BUG: cannot find " + USER_THROWABLE);160}161162// get mirror of main thread in debuggee163log.display("Getting reference to main thread");164Iterator threadIterator = vm.allThreads().iterator();165while (threadIterator.hasNext()) {166ThreadReference curThread = (ThreadReference) threadIterator.next();167if (curThread.name().equals("main")) {168checkedThread = curThread;169}170}171if (checkedThread == null) {172throw new Failure("TEST BUG: unable to find reference to main thread");173}174175// create ExceptionRequest for all throwable classes (initially disabled)176log.display("Creating ExceptionRequest");177boolean notifyCaught = true;178boolean notifyUncaught = true;179if ((checkedRequest = erManager.createExceptionRequest(null, notifyCaught, notifyUncaught)) == null) {180throw new Failure("TEST BUG: unable to create ExceptionRequest");181}182183// define separate thread for handling received events184class EventHandler extends Thread {185public void run() {186// handle events until all exceptions thrown and187// all expected events received188while (!(exceptionsThrown && eventsReceived)) {189eventSet = null;190try {191eventSet = vm.eventQueue().remove(TIMEOUT_DELTA);192} catch (InterruptedException e) {193log.complain("Unexpected InterruptedException while receiving event: " + e);194break;195}196197if (eventSet == null) {198continue;199}200201// handle each event from event set202EventIterator eventIterator = eventSet.eventIterator();203while (eventIterator.hasNext()) {204205Event event = eventIterator.nextEvent();206log.display("\nEvent received:\n " + event);207208if (EventFilters.filtered(event))209continue;210211// handle ExceptionEvent212if (event instanceof ExceptionEvent) {213214ExceptionEvent castedEvent = (ExceptionEvent)event;215ReferenceType eventRefType = castedEvent.exception().referenceType();216Location eventLocation = castedEvent.location();217Location eventCatchLocation = castedEvent.catchLocation();218219if (eventRefType.equals(userException)) {220log.display("ExceptionEvent for " + USER_EXCEPTION + " is received");221userExceptionReceived = true;222if (eventLocation.lineNumber() != location001a.userExceptionLocation) {223log.complain("FAILURE 1: incorrect location for " + USER_EXCEPTION +224"\nexpected: " + location001a.userExceptionLocation + ", got: " + eventLocation.lineNumber());225testFailed = true;226}227if (eventCatchLocation.lineNumber() != location001a.userExceptionCatchLocation) {228log.complain("FAILURE 1: incorrect catchLocation for " + USER_EXCEPTION +229"\nexpected: " + location001a.userExceptionCatchLocation + ", got: " + eventCatchLocation.lineNumber());230testFailed = true;231}232if (!(eventLocation.declaringType().equals(checkedClass))) {233log.complain("FAILURE 2: catchLocation.declaringType() does not equal to debuggee class:");234testFailed = true;235log.complain(eventLocation.declaringType().name());236}237238} else if (eventRefType.equals(userError)) {239log.display("ExceptionEvent for " + USER_ERROR + " is received");240userErrorReceived = true;241if (eventLocation.lineNumber() != location001a.userErrorLocation) {242log.complain("FAILURE 3: incorrect location for " + USER_ERROR +243"\nexpected: " + location001a.userErrorLocation + ", got: " + eventLocation.lineNumber());244testFailed = true;245}246if (eventCatchLocation.lineNumber() != location001a.userErrorCatchLocation) {247log.complain("FAILURE 3: incorrect catchLocation for " + USER_ERROR +248"\nexpected: " + location001a.userErrorCatchLocation + ", got: " + eventCatchLocation.lineNumber());249testFailed = true;250}251if (!(eventLocation.declaringType().equals(checkedClass))) {252log.complain("FAILURE 4: catchLocation.declaringType() does not equal to debuggee class:");253testFailed = true;254log.complain(eventLocation.declaringType().name());255}256257} else if (eventRefType.equals(userThrowable)) {258log.display("ExceptionEvent for " + USER_THROWABLE + " is received");259userThrowableReceived = true;260if (eventLocation.lineNumber() != location001a.userThrowableLocation) {261log.complain("FAILURE 5: incorrect location for " + USER_THROWABLE +262"\nexpected: " + location001a.userThrowableLocation + ", got: " + eventLocation.lineNumber());263testFailed = true;264}265if (eventCatchLocation.lineNumber() != location001a.userThrowableCatchLocation) {266log.complain("FAILURE 5: incorrect catchLocation for " + USER_THROWABLE +267"\nexpected: " + location001a.userThrowableCatchLocation + ", got: " + eventCatchLocation.lineNumber());268testFailed = true;269}270if (!(eventLocation.declaringType().equals(checkedClass))) {271log.complain("FAILURE 6: catchLocation.declaringType() does not equal to debuggee class:");272testFailed = true;273log.complain(eventLocation.declaringType().name());274}275} else {276String eventTypeName = eventRefType.name();277if (eventTypeName.equals(JAVA_EXCEPTION)) {278log.display("ExceptionEvent for " + JAVA_EXCEPTION + " is received");279javaExceptionReceived = true;280if (eventCatchLocation.lineNumber() != location001a.javaExceptionCatchLocation) {281log.complain("FAILURE 7: incorrect catchLocation for " + JAVA_EXCEPTION +282"\nexpected: " + location001a.javaExceptionCatchLocation + ", got: " + eventCatchLocation.lineNumber());283testFailed = true;284}285286} else if (eventTypeName.equals(JAVA_ERROR)) {287log.display("ExceptionEvent for " + JAVA_ERROR + " is received");288javaErrorReceived = true;289if (eventCatchLocation.lineNumber() != location001a.javaErrorCatchLocation) {290log.complain("FAILURE 8: incorrect catchLocation for " + JAVA_ERROR +291"\nexpected: " + location001a.javaErrorCatchLocation + ", got: " + eventCatchLocation.lineNumber());292testFailed = true;293}294}295}296}297298// ignore each other events299300} // end of event handling loop301302log.display("Resuming event set");303eventSet.resume();304305// check if all expected events received306eventsReceived = userExceptionReceived && userErrorReceived307&& userThrowableReceived && javaExceptionReceived308&& javaErrorReceived;309310} // end of event set handling loop311312log.display("eventHandler completed");313314} // end of run()315316} // end of EventHandler317318// start EventHandler thread319EventHandler eventHandler = new EventHandler();320log.display("Starting eventHandler");321eventHandler.start();322323// enable event request324log.display("Enabling ExceptionRequest");325checkedRequest.enable();326327// force debuggee to throw exceptions and generate events328log.display("Sending command: " + COMMAND_GO);329pipe.println(COMMAND_GO);330331log.display("");332333// wait for debuggee completed test case334log.display("Waiting for command: " + COMMAND_DONE);335command = pipe.readln();336if (command.equals(COMMAND_ERROR)) {337throw new Failure("TEST BUG: Unable to thrown all exceptions in debuggee");338}339if (!command.equals(COMMAND_DONE)) {340throw new Failure("TEST BUG: unknown debuggee's command: " + command);341}342343// notify EventHandler that all checked exceptions thrown in debuggee344exceptionsThrown = true;345346log.display("");347348// wait for all expected events received or timeout exceeds349log.display("Waiting for all expected events received");350try {351eventHandler.join(eventTimeout);352if (eventHandler.isAlive()) {353log.complain("FAILURE 20: Timeout for waiting event was exceeded");354eventHandler.interrupt();355testFailed = true;356}357} catch (InterruptedException e) {358log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for eventHandler's death");359testFailed = true;360}361362// complain if not all expected events received363if (!userExceptionReceived) {364log.complain("FAILURE 9: " + USER_EXCEPTION + " was not received received");365testFailed= true;366}367if (!userErrorReceived) {368log.complain("FAILURE 10: " + USER_ERROR + " was not received received");369testFailed= true;370}371if (!userThrowableReceived) {372log.complain("FAILURE 11: " + USER_THROWABLE + " was not received received");373testFailed= true;374}375if (!javaExceptionReceived) {376log.complain("FAILURE 12: " + JAVA_EXCEPTION + " was not received received");377testFailed= true;378}379if (!javaErrorReceived) {380log.complain("FAILURE 13: " + JAVA_ERROR + " was not received received");381testFailed= true;382}383384// end testing385386} catch (Failure e) {387log.complain("TEST FAILURE: " + e.getMessage());388testFailed = true;389} catch (Exception e) {390log.complain("Unexpected exception: " + e);391e.printStackTrace(out);392testFailed = true;393} finally {394395log.display("");396397// disable event request to prevent appearance of further events398if (checkedRequest != null && checkedRequest.isEnabled()) {399log.display("Disabling event request");400checkedRequest.disable();401}402403// force debugee to exit404log.display("Sending command: " + COMMAND_QUIT);405pipe.println(COMMAND_QUIT);406407// wait for debuggee exits and analize its exit code408log.display("Waiting for debuggee terminating");409int debuggeeStatus = debuggee.endDebugee();410if (debuggeeStatus == PASSED + JCK_STATUS_BASE) {411log.display("Debuggee PASSED with exit code: " + debuggeeStatus);412} else {413log.complain("Debuggee FAILED with exit code: " + debuggeeStatus);414testFailed = true;415}416}417418// check test results419if (testFailed) {420log.complain("TEST FAILED");421return FAILED;422}423424log.display("TEST PASSED");425return PASSED;426}427}428429430