Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/exceptionRequests/excreq001.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.EventRequestManager.exceptionRequests;2425import com.sun.jdi.ReferenceType;26import com.sun.jdi.VirtualMachine;27import com.sun.jdi.request.ExceptionRequest;28import com.sun.jdi.request.EventRequestManager;29import com.sun.jdi.VMDisconnectedException;30import com.sun.jdi.event.*;31import com.sun.jdi.AbsentInformationException;32import com.sun.jdi.ObjectCollectedException;3334import java.util.List;35import java.io.*;3637import nsk.share.*;38import nsk.share.jpda.*;39import nsk.share.jdi.*;4041/**42* The test checks that the JDI method43* <code>com.sun.jdi.request.EventRequestManager.exceptionRequests()</code>44* properly returns all previously created ExceptionRequest objects for the45* different kinds of methods in two debuggee's classes when:46* <li>event requests are disabled47* <li>event requests are enabled<br>48* The debuggee loads several dummy classes which ReferenceType49* objects are obtained by the debugger for exercising50* the <code>exceptionRequests()</code>.<br>51* EventHandler was added as workaround for the bug 4430096.52* This prevents the target VM from potential hangup.53*/54public class excreq001 {55public static final int PASSED = 0;56public static final int FAILED = 2;57public static final int JCK_STATUS_BASE = 95;58static final String PACK =59"nsk.jdi.EventRequestManager.exceptionRequests";60static final String COMMAND_READY = "ready";61static final String COMMAND_QUIT = "quit";6263static final int EXC_NUM = 4;64// list of debuggee's loaded classes65static final String DEBUGGEE_CLS[][] = {66{PACK + ".excreq001t", "excreq001t.java"},67{PACK + ".excreq001a", "excreq001a.java"},68{PACK + ".excreq001b", "excreq001b.java"},69{PACK + ".excreq001c", "excreq001c.java"},70};71// caught/uncaught exception notifications72static final boolean DEBUGGEE_NTFS[][] = {73{true, true},74{true, false},75{false, false},76{false, true}77};7879private Log log;80private IOPipe pipe;81private Debugee debuggee;82private VirtualMachine vm;83private EventListener elThread;84private volatile int tot_res = PASSED;8586public static void main (String argv[]) {87System.exit(run(argv,System.out) + JCK_STATUS_BASE);88}8990public static int run(String argv[], PrintStream out) {91return new excreq001().runIt(argv, out);92}9394private int runIt(String args[], PrintStream out) {95ArgumentHandler argHandler = new ArgumentHandler(args);96log = new Log(out, argHandler);97Binder binder = new Binder(argHandler, log);98ReferenceType rType;99ExceptionRequest excRequest[] = new ExceptionRequest[EXC_NUM];100String cmd;101102debuggee = binder.bindToDebugee(DEBUGGEE_CLS[0][0]);103pipe = debuggee.createIOPipe();104debuggee.redirectStderr(log, "excreq001t.err> ");105vm = debuggee.VM();106EventRequestManager erManager = vm.eventRequestManager();107debuggee.resume();108cmd = pipe.readln();109if (!cmd.equals(COMMAND_READY)) {110log.complain("TEST BUG: unknown debuggee's command: " + cmd);111tot_res = FAILED;112return quitDebuggee();113}114115for (int i=0; i<EXC_NUM; i++) {116if ((rType = debuggee.classByName(DEBUGGEE_CLS[i][0])) == null) {117log.complain("TEST BUG: cannot find " + DEBUGGEE_CLS[i][0]);118tot_res = FAILED;119return quitDebuggee();120}121try {122log.display("\nCreating ExceptionRequest for the ReferenceType:\n\tname="123+ rType.name() + "\n\tsource=" + rType.sourceName()124+ "\n\tnotifications of caught/uncaught exception: ("125+ DEBUGGEE_NTFS[i][0] + "," + DEBUGGEE_NTFS[i][1] + ")");126} catch(AbsentInformationException e) {127log.complain("TEST FAILURE: ReferenceType.sourceName(): " + e);128tot_res = FAILED;129return quitDebuggee();130} catch(ObjectCollectedException e) {131log.complain("TEST FAILURE: caught " + e);132tot_res = FAILED;133return quitDebuggee();134}135try {136excRequest[i] = erManager.createExceptionRequest(rType,137DEBUGGEE_NTFS[i][0],DEBUGGEE_NTFS[i][1]);138} catch (Exception e) {139log.complain("TEST FAILURE: EventRequestManager.createExceptionRequest(): "140+ e);141tot_res = FAILED;142return quitDebuggee();143}144}145elThread = new EventListener();146elThread.start();147148// Check Exception requests when event requests are disabled149log.display("\n1) Getting ExceptionRequest objects with disabled event requests...");150checkRequests(erManager, 1);151152// Check Exception requests when event requests are enabled153for (int i=0; i<EXC_NUM; i++) {154excRequest[i].enable();155}156log.display("\n2) Getting ExceptionRequest objects with enabled event requests...");157checkRequests(erManager, 2);158159// Finish the test160for (int i=0; i<EXC_NUM; i++) {161excRequest[i].disable();162}163return quitDebuggee();164}165166private void checkRequests(EventRequestManager erManager, int t_case) {167List reqL = erManager.exceptionRequests();168if (reqL.size() != EXC_NUM) {169log.complain("\nTEST CASE #" + t_case + " FAILED: found " + reqL.size()170+ " ExceptionRequest objects\n\texpected: " + EXC_NUM);171tot_res = FAILED;172return;173}174for (int i=0; i<EXC_NUM; i++) {175ExceptionRequest excReq =176(ExceptionRequest) reqL.get(i);177ReferenceType refT = excReq.exception();178boolean notFound = true;179try {180for (int j=0; j<EXC_NUM; j++) {181if (refT.name().equals(DEBUGGEE_CLS[j][0])) {182if (refT.sourceName().equals(DEBUGGEE_CLS[j][1]) &&183excReq.notifyCaught() == DEBUGGEE_NTFS[j][0] &&184excReq.notifyUncaught() == DEBUGGEE_NTFS[j][1]) {185log.display("\nFound an expected ExceptionRequest object for the ReferenceType:\n\tname="186+ DEBUGGEE_CLS[j][0] + "\n\tsource=" + DEBUGGEE_CLS[j][1]187+ "\n\tnotifications of caught/uncaught exceptions: (" + DEBUGGEE_NTFS[j][0] + ","188+ DEBUGGEE_NTFS[j][1] + ")");189} else {190log.complain("\nTEST CASE #" + t_case191+ " FAILED: found an ExceptionRequest object for the ReferenceType:\n\tname="192+ refT.name() + "\n\tsource=" + refT.sourceName()193+ "\n\tnotifications of caught/uncaught exception: ("194+ excReq.notifyCaught() + "," + excReq.notifyUncaught() + ")"195+ "\n\tmismatched with the expected ReferenceType:\n\tname="196+ DEBUGGEE_CLS[j][0] + "\n\tsource=" + DEBUGGEE_CLS[j][1]197+ "\tnotifications of caught/uncaught exception: (" + DEBUGGEE_NTFS[j][0] + ","198+ DEBUGGEE_NTFS[j][1] + ")");199tot_res = FAILED;200}201notFound = false;202break;203}204}205if (notFound) {206log.complain("\nTEST CASE #" + t_case207+ " FAILED: found an ExceptionRequest object for the unexpected ReferenceType:\n\tname="208+ refT.name() + "\n\tsource=" + refT.sourceName()209+ "\n\tnotifications of caught/uncaught exception: ("210+ excReq.notifyCaught() + "," + excReq.notifyUncaught() + ")");211tot_res = FAILED;212}213} catch(AbsentInformationException e) {214log.complain("TEST FAILURE: checkRequests: caught: "215+ e);216tot_res = FAILED;217return;218}219}220}221222private int quitDebuggee() {223if (elThread != null) {224elThread.isConnected = false;225try {226if (elThread.isAlive())227elThread.join();228} catch (InterruptedException e) {229log.complain("TEST INCOMPLETE: caught InterruptedException "230+ e);231tot_res = FAILED;232}233}234235pipe.println(COMMAND_QUIT);236debuggee.waitFor();237int debStat = debuggee.getStatus();238if (debStat != (JCK_STATUS_BASE + PASSED)) {239log.complain("TEST FAILED: debuggee's process finished with status: "240+ debStat);241tot_res = FAILED;242} else243log.display("Debuggee's process finished with status: "244+ debStat);245246return tot_res;247}248249class EventListener extends Thread {250public volatile boolean isConnected = true;251252public void run() {253try {254do {255EventSet eventSet = vm.eventQueue().remove(1000);256if (eventSet != null) { // there is not a timeout257EventIterator it = eventSet.eventIterator();258while (it.hasNext()) {259Event event = it.nextEvent();260if (event instanceof VMDeathEvent) {261tot_res = FAILED;262isConnected = false;263log.complain("TEST FAILED: unexpected VMDeathEvent");264} else if (event instanceof VMDisconnectEvent) {265tot_res = FAILED;266isConnected = false;267log.complain("TEST FAILED: unexpected VMDisconnectEvent");268} else269log.display("EventListener: following JDI event occured: "270+ event.toString());271}272if (isConnected) {273eventSet.resume();274}275}276} while (isConnected);277} catch (InterruptedException e) {278tot_res = FAILED;279log.complain("FAILURE in EventListener: caught unexpected "280+ e);281} catch (VMDisconnectedException e) {282tot_res = FAILED;283log.complain("FAILURE in EventListener: caught unexpected "284+ e);285e.printStackTrace();286}287log.display("EventListener: exiting");288}289}290}291292293