Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ExceptionEvent/exception/exception001.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.exception;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 exception001 {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.exception.exception001a";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, "exception001a >");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;215216ObjectReference eventThrowable = castedEvent.exception();217if (eventThrowable == null) {218log.complain("FAILURE 1: ExceptionEvent.exception() returns null");219testFailed = true;220} else {221ReferenceType eventRefType = castedEvent.exception().referenceType();222if (eventRefType.equals(userException)) {223log.display("ExceptionEvent for " + USER_EXCEPTION + " is received");224userExceptionReceived = true;225226Location eventLocation = castedEvent.location();227if (!(eventLocation.declaringType().name().equals(DEBUGGEE_NAME))) {228log.complain("FAILURE 2: eventLocation.declaringType() does not equal to debuggee class");229testFailed = true;230}231232} else if (eventRefType.equals(userError)) {233log.display("ExceptionEvent for " + USER_ERROR + " is received");234userErrorReceived = true;235236Location eventLocation = castedEvent.location();237if (!(eventLocation.declaringType().name().equals(DEBUGGEE_NAME))) {238log.complain("FAILURE 2: eventLocation.declaringType() does not equal to debuggee class");239testFailed = true;240}241242} else if (eventRefType.equals(userThrowable)) {243log.display("ExceptionEvent for " + USER_THROWABLE + " is received");244userThrowableReceived = true;245246Location eventLocation = castedEvent.location();247if (!(eventLocation.declaringType().name().equals(DEBUGGEE_NAME))) {248log.complain("FAILURE 2: eventLocation.declaringType() does not equal to debuggee class");249testFailed = true;250}251252} else {253String eventTypeName = eventRefType.name();254if (eventTypeName.equals(JAVA_EXCEPTION)) {255log.display("ExceptionEvent for " + JAVA_EXCEPTION + " is received");256javaExceptionReceived = true;257} else if (eventTypeName.equals(JAVA_ERROR)) {258log.display("ExceptionEvent for " + JAVA_ERROR + " is received");259javaErrorReceived = true;260}261}262}263264EventRequest eventRequest = castedEvent.request();265if (!(checkedRequest.equals(eventRequest))) {266log.complain("FAILURE 4: eventRequest does not equal to checked request");267testFailed = true;268}269ThreadReference eventThread = castedEvent.thread();270if (!(checkedThread.equals(eventThread))) {271log.complain("FAILURE 5: eventThread does not equal to checked thread");272testFailed = true;273}274VirtualMachine eventMachine = castedEvent.virtualMachine();275if (!(vm.equals(eventMachine))) {276log.complain("FAILURE 6: eventVirtualMachine does not equal to checked vm");277testFailed = true;278}279}280281// ignore each other events282283} // end of event handling loop284285log.display("Resuming event set");286eventSet.resume();287288// check if all expected events received289eventsReceived = userExceptionReceived && userErrorReceived290&& userThrowableReceived && javaExceptionReceived291&& javaErrorReceived;292293} // end of event set handling loop294295log.display("eventHandler completed");296297} // end of run()298299} // end of EventHandler300301// start EventHandler thread302EventHandler eventHandler = new EventHandler();303log.display("Starting eventHandler");304eventHandler.start();305306// enable event request307log.display("Enabling ExceptionRequest");308checkedRequest.enable();309310// force debuggee to throw exceptions and generate events311log.display("Sending command: " + COMMAND_GO);312pipe.println(COMMAND_GO);313314log.display("");315316// wait for debuggee completed test case317log.display("Waiting for command: " + COMMAND_DONE);318command = pipe.readln();319if (command.equals(COMMAND_ERROR)) {320throw new Failure("TEST BUG: Unable to thrown all exceptions in debuggee");321}322if (!command.equals(COMMAND_DONE)) {323throw new Failure("TEST BUG: unknown debuggee's command: " + command);324}325326// notify EventHandler that all checked exceptions thrown in debuggee327exceptionsThrown = true;328329log.display("");330331// wait for all expected events received or timeout exceeds332log.display("Waiting for all expected events received");333try {334eventHandler.join(eventTimeout);335if (eventHandler.isAlive()) {336log.complain("FAILURE 20: Timeout for waiting event was exceeded");337eventHandler.interrupt();338testFailed = true;339}340} catch (InterruptedException e) {341log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for eventHandler's death");342testFailed = true;343}344345// complain if not all expected events received346if (!userExceptionReceived) {347log.complain("FAILURE 9: " + USER_EXCEPTION + " was not received received");348testFailed= true;349}350if (!userErrorReceived) {351log.complain("FAILURE 10: " + USER_ERROR + " was not received received");352testFailed= true;353}354if (!userThrowableReceived) {355log.complain("FAILURE 11: " + USER_THROWABLE + " was not received received");356testFailed= true;357}358if (!javaExceptionReceived) {359log.complain("FAILURE 12: " + JAVA_EXCEPTION + " was not received received");360testFailed= true;361}362if (!javaErrorReceived) {363log.complain("FAILURE 13: " + JAVA_ERROR + " was not received received");364testFailed= true;365}366367// end testing368369} catch (Failure e) {370log.complain("TEST FAILURE: " + e.getMessage());371testFailed = true;372} catch (Exception e) {373log.complain("Unexpected exception: " + e);374e.printStackTrace(out);375testFailed = true;376} finally {377378log.display("");379380// disable event request to prevent appearance of further events381if (checkedRequest != null && checkedRequest.isEnabled()) {382log.display("Disabling event request");383checkedRequest.disable();384}385386// force debugee to exit387log.display("Sending command: " + COMMAND_QUIT);388pipe.println(COMMAND_QUIT);389390// wait for debuggee exits and analize its exit code391log.display("Waiting for debuggee terminating");392int debuggeeStatus = debuggee.endDebugee();393if (debuggeeStatus == PASSED + JCK_STATUS_BASE) {394log.display("Debuggee PASSED with exit code: " + debuggeeStatus);395} else {396log.complain("Debuggee FAILED with exit code: " + debuggeeStatus);397testFailed = true;398}399}400401// check test results402if (testFailed) {403log.complain("TEST FAILED");404return FAILED;405}406407log.display("TEST PASSED");408return PASSED;409}410}411412413/*414// This class is the debugger in the test415416public class exception001 {417static final int PASSED = 0;418static final int FAILED = 2;419static final int JCK_STATUS_BASE = 95;420static final String COMMAND_READY = "ready";421static final String COMMAND_QUIT = "quit";422static final String COMMAND_GO = "go";423static final String DEBUGGEE_NAME = "nsk.jdi.ExceptionEvent.exception.exception001a";424static final String USER_EXCEPTION = DEBUGGEE_NAME + "Exception";425static final String USER_ERROR = DEBUGGEE_NAME + "Error";426static final String USER_THROWABLE = DEBUGGEE_NAME + "Throwable";427static final String JAVA_EXCEPTION = "java.lang.NumberFormatException";428static final String JAVA_ERROR = "java.lang.StackOverflowError";429430static private Debugee debuggee;431static private VirtualMachine vm;432static private IOPipe pipe;433static private Log log;434static private ArgumentHandler argHandler;435static private EventSet eventSet;436437static private ExceptionRequest checkedRequest;438static private ReferenceType checkedClass;439static private ThreadReference checkedThread;440441static private ReferenceType userException;442static private ReferenceType userError;443static private ReferenceType userThrowable;444static private boolean userExceptionReceived;445static private boolean userErrorReceived;446static private boolean userThrowableReceived;447static private boolean javaExceptionReceived;448static private boolean javaErrorReceived;449450static private boolean testFailed;451452public static void main (String args[]) {453System.exit(run(args, System.out) + JCK_STATUS_BASE);454}455456public static int run(final String args[], final PrintStream out) {457458testFailed = false;459userExceptionReceived = false;460userErrorReceived = false;461userThrowableReceived = false;462javaExceptionReceived = false;463javaErrorReceived = false;464465argHandler = new ArgumentHandler(args);466log = new Log(out, argHandler);467468Binder binder = new Binder(argHandler, log);469log.display("Connecting to debuggee");470debuggee = binder.bindToDebugee(DEBUGGEE_NAME);471debuggee.redirectStderr(log, "exception001a >");472473pipe = debuggee.createIOPipe();474vm = debuggee.VM();475EventRequestManager erManager = debuggee.VM().eventRequestManager();476477log.display("Resuming debuggee");478debuggee.resume();479480log.display("Waiting for command: " + COMMAND_READY);481String command = pipe.readln();482483while (true) {484485if (!command.equals(COMMAND_READY)) {486log.complain("TEST BUG: unexpected debuggee's command: " + command);487testFailed = true;488break;489}490491log.display("Getting loaded classes in debuggee");492if ((checkedClass = debuggee.classByName(DEBUGGEE_NAME)) == null) {493log.complain("TEST BUG: cannot find " + DEBUGGEE_NAME);494testFailed = true;495break;496}497498if ((userException = debuggee.classByName(USER_EXCEPTION)) == null) {499log.complain("TEST BUG: cannot find " + USER_EXCEPTION);500testFailed = true;501break;502}503504if ((userError = debuggee.classByName(USER_ERROR)) == null) {505log.complain("TEST BUG: cannot find " + USER_ERROR);506testFailed = true;507break;508}509510if ((userThrowable = debuggee.classByName(USER_THROWABLE)) == null) {511log.complain("TEST BUG: cannot find " + USER_THROWABLE);512testFailed = true;513break;514}515516log.display("Getting reference to main thread");517Iterator threadIterator = vm.allThreads().iterator();518while (threadIterator.hasNext()) {519ThreadReference curThread = (ThreadReference) threadIterator.next();520if (curThread.name().equals("main")) {521checkedThread = curThread;522}523}524if (checkedThread == null) {525log.complain("TEST BUG: unable to find reference to main thread");526testFailed = true;527break;528}529530log.display("Creating ExceptionRequest");531boolean notifyCaught = true;532boolean notifyUncaught = true;533if ((checkedRequest = erManager.createExceptionRequest(null, notifyCaught, notifyUncaught)) == null) {534log.complain("TEST BUG: unable to create ExceptionRequest");535testFailed = true;536break;537}538539//checkedRequest.addClassFilter(checkedClass);540checkedRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);541checkedRequest.enable();542log.display("ExceptionRequest is created");543544break;545}546if (testFailed) {547log.display("Sending command: " + COMMAND_QUIT);548pipe.println(COMMAND_QUIT);549log.display("Waiting for debuggee terminating");550debuggee.waitFor();551return FAILED;552}553554class EventHandler extends Thread {555public void run() {556eventSet = null;557try {558isConnected:559while (true) {560eventSet = vm.eventQueue().remove();561562EventIterator eventIterator = eventSet.eventIterator();563while (eventIterator.hasNext()) {564565Event event = eventIterator.nextEvent();566567if (event instanceof VMDisconnectEvent) {568break isConnected;569}570571if (event instanceof ExceptionEvent) {572573ExceptionEvent castedEvent = (ExceptionEvent)event;574575ObjectReference eventThrowable = castedEvent.exception();576if (eventThrowable == null) {577log.complain("FAILURE 1: ExceptionEvent.exception() returns null");578testFailed = true;579} else {580ReferenceType eventRefType = castedEvent.exception().referenceType();581if (eventRefType.equals(userException)) {582log.display("ExceptionEvent for " + USER_EXCEPTION + " is received");583userExceptionReceived = true;584585Location eventLocation = castedEvent.location();586if (!(eventLocation.declaringType().name().equals(DEBUGGEE_NAME))) {587log.complain("FAILURE 2: eventLocation.declaringType() does not equal to debuggee class");588testFailed = true;589}590591} else if (eventRefType.equals(userError)) {592log.display("ExceptionEvent for " + USER_ERROR + " is received");593userErrorReceived = true;594595Location eventLocation = castedEvent.location();596if (!(eventLocation.declaringType().name().equals(DEBUGGEE_NAME))) {597log.complain("FAILURE 2: eventLocation.declaringType() does not equal to debuggee class");598testFailed = true;599}600601} else if (eventRefType.equals(userThrowable)) {602log.display("ExceptionEvent for " + USER_THROWABLE + " is received");603userThrowableReceived = true;604605Location eventLocation = castedEvent.location();606if (!(eventLocation.declaringType().name().equals(DEBUGGEE_NAME))) {607log.complain("FAILURE 2: eventLocation.declaringType() does not equal to debuggee class");608testFailed = true;609}610611} else {612String eventTypeName = eventRefType.name();613if (eventTypeName.equals(JAVA_EXCEPTION)) {614log.display("ExceptionEvent for " + JAVA_EXCEPTION + " is received");615javaExceptionReceived = true;616} else if (eventTypeName.equals(JAVA_ERROR)) {617log.display("ExceptionEvent for " + JAVA_ERROR + " is received");618javaErrorReceived = true;619}620}621}622623EventRequest eventRequest = castedEvent.request();624if (!(checkedRequest.equals(eventRequest))) {625log.complain("FAILURE 4: eventRequest does not equal to checked request");626testFailed = true;627}628ThreadReference eventThread = castedEvent.thread();629if (!(checkedThread.equals(eventThread))) {630log.complain("FAILURE 5: eventThread does not equal to checked thread");631testFailed = true;632}633VirtualMachine eventMachine = castedEvent.virtualMachine();634if (!(vm.equals(eventMachine))) {635log.complain("FAILURE 6: eventVirtualMachine does not equal to checked vm");636testFailed = true;637}638}639}640eventSet.resume();641}642} catch (InterruptedException e) {643} catch (VMDisconnectedException e) {644log.complain("TEST INCOMPLETE: caught VMDisconnectedException while waiting for event");645testFailed = true;646}647log.display("eventHandler completed");648}649}650651EventHandler eventHandler = new EventHandler();652log.display("Starting eventHandler");653eventHandler.start();654655log.display("Sending command: " + COMMAND_GO);656pipe.println(COMMAND_GO);657658try {659eventHandler.join(argHandler.getWaitTime()*60000);660if (eventHandler.isAlive()) {661log.complain("FAILURE 20: Timeout for waiting of event was exceeded");662eventHandler.interrupt();663testFailed = true;664}665} catch (InterruptedException e) {666log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for eventHandler's death");667testFailed = true;668}669670log.display("Waiting for debuggee terminating");671debuggee.waitFor();672673if (!userExceptionReceived) {674log.complain("FAILURE 7: " + USER_EXCEPTION + " was not received received");675testFailed= true;676}677if (!userErrorReceived) {678log.complain("FAILURE 8: " + USER_ERROR + " was not received received");679testFailed= true;680}681if (!userThrowableReceived) {682log.complain("FAILURE 9: " + USER_THROWABLE + " was not received received");683testFailed= true;684}685if (!javaExceptionReceived) {686log.complain("FAILURE 10: " + JAVA_EXCEPTION + " was not received received");687testFailed= true;688}689if (!javaErrorReceived) {690log.complain("FAILURE 11: " + JAVA_ERROR + " was not received received");691testFailed= true;692}693694if (testFailed) return FAILED;695return PASSED;696}697698}699700*/701702703