Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/methodExitRequests/methexitreq001.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.methodExitRequests;2425import com.sun.jdi.request.MethodExitRequest;26import com.sun.jdi.request.EventRequestManager;27import com.sun.jdi.VirtualMachine;28import com.sun.jdi.VMDisconnectedException;29import com.sun.jdi.event.*;3031import java.util.List;32import java.io.*;3334import nsk.share.*;35import nsk.share.jpda.*;36import nsk.share.jdi.*;3738/**39* The test checks that the JDI method40* <code>com.sun.jdi.request.EventRequestManager.methodExitRequests()</code>41* properly returns all MethodExitRequest objects when:42* <li>event requests are disabled43* <li>event requests are enabled.<br>44* The MethodExitRequest objects are distinguished by the different45* <code>EventRequest</code>'s properties.<br>46* EventHandler was added as workaround for the bug 4430096.47* This prevents the target VM from potential hangup.48*/49public class methexitreq001 {50public static final int PASSED = 0;51public static final int FAILED = 2;52public static final int JCK_STATUS_BASE = 95;53static final int TIMEOUT_DELTA = 1000; // in milliseconds54static final String DEBUGGEE_CLASS =55"nsk.jdi.EventRequestManager.methodExitRequests.methexitreq001t";56static final String COMMAND_READY = "ready";57static final String COMMAND_QUIT = "quit";5859static final int MEXIT_NUM = 7;60static final String PROPS[][] = {61{"first", "a quick"},62{"second", "brown"},63{"third", "fox"},64{"fourth", "jumps"},65{"fifth", "over"},66{"sixth", "the lazy"},67{"seventh", "dog"}68};6970private Log log;71private IOPipe pipe;72private Debugee debuggee;73private VirtualMachine vm;74private EventListener elThread;75private volatile int tot_res = PASSED;7677public static void main (String argv[]) {78System.exit(run(argv,System.out) + JCK_STATUS_BASE);79}8081public static int run(String argv[], PrintStream out) {82return new methexitreq001().runIt(argv, out);83}8485private int runIt(String args[], PrintStream out) {86ArgumentHandler argHandler = new ArgumentHandler(args);87log = new Log(out, argHandler);88Binder binder = new Binder(argHandler, log);89MethodExitRequest mentrRequest[] = new MethodExitRequest[MEXIT_NUM];90String cmd;9192debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);93pipe = debuggee.createIOPipe();94debuggee.redirectStderr(log, "methexitreq001t.err> ");95vm = debuggee.VM();96EventRequestManager erManager = vm.eventRequestManager();97debuggee.resume();98cmd = pipe.readln();99if (!cmd.equals(COMMAND_READY)) {100log.complain("TEST BUG: unknown debuggee's command: " + cmd);101tot_res = FAILED;102return quitDebuggee();103}104105for (int i=0; i<MEXIT_NUM; i++) {106log.display("Creating MethodExitRequest #" + i107+ " with the property ("108+ PROPS[i][0] + "," + PROPS[i][1] + ")...");109mentrRequest[i] = erManager.createMethodExitRequest();110mentrRequest[i].putProperty(PROPS[i][0], PROPS[i][1]);111}112elThread = new EventListener();113elThread.start();114115// Check MethodExit requests when event requests are disabled116log.display("\n1) Getting MethodExitRequest objects with disabled event requests...");117checkRequests(erManager, 1);118119// Check MethodExit requests when event requests are enabled120for (int i=0; i<MEXIT_NUM; i++) {121mentrRequest[i].enable();122}123log.display("\n2) Getting MethodExitRequest objects with enabled event requests...");124checkRequests(erManager, 2);125126// Finish the test127for (int i=0; i<MEXIT_NUM; i++) {128mentrRequest[i].disable();129}130return quitDebuggee();131}132133private void checkRequests(EventRequestManager erManager, int t_case) {134List reqL = erManager.methodExitRequests();135if (reqL.size() != MEXIT_NUM) {136log.complain("TEST CASE #" + t_case + " FAILED: found " + reqL.size()137+ " MethodExitRequest objects\n\texpected: " + MEXIT_NUM);138tot_res = FAILED;139return;140}141for (int i=0; i<MEXIT_NUM; i++) {142MethodExitRequest meReq =143(MethodExitRequest) reqL.get(i);144boolean notFound = true;145for (int j=0; j<MEXIT_NUM; j++) {146String propKey = (String) meReq.getProperty(PROPS[j][0]);147if (propKey != null) {148if (propKey.equals(PROPS[j][1])) {149log.display("Found expected MethodExitRequest object with the property: ("150+ PROPS[j][0] + "," + propKey + ")");151} else {152log.complain("TEST CASE #" + t_case153+ " FAILED: found MethodExitRequest object with the unexpected property: ("154+ PROPS[j][0] + "," + propKey + ")");155tot_res = FAILED;156}157notFound = false;158break;159}160}161if (notFound) {162log.complain("\nTEST CASE #" + t_case163+ " FAILED: found unexpected MethodExitRequest object");164tot_res = FAILED;165}166}167}168169private int quitDebuggee() {170if (elThread != null) {171elThread.isConnected = false;172try {173if (elThread.isAlive())174elThread.join();175} catch (InterruptedException e) {176log.complain("TEST INCOMPLETE: caught InterruptedException "177+ e);178tot_res = FAILED;179}180}181182log.display("Sending the command \"" + COMMAND_QUIT183+ "\" to the debuggee");184pipe.println(COMMAND_QUIT);185debuggee.waitFor();186int debStat = debuggee.getStatus();187if (debStat != (JCK_STATUS_BASE + PASSED)) {188log.complain("TEST FAILED: debuggee's process finished with status: "189+ debStat);190tot_res = FAILED;191} else192log.display("Debuggee's process finished with status: "193+ debStat);194195return tot_res;196}197198class EventListener extends Thread {199public volatile boolean isConnected = true;200EventSet eventSet;201202public void run() {203try {204do {205eventSet = vm.eventQueue().remove(TIMEOUT_DELTA);206if (eventSet != null) { // there is not a timeout207EventIterator it = eventSet.eventIterator();208while (it.hasNext()) {209Event event = it.nextEvent();210if (event instanceof VMDeathEvent) {211tot_res = FAILED;212isConnected = false;213log.complain("TEST FAILED: unexpected VMDeathEvent");214} else if (event instanceof VMDisconnectEvent) {215tot_res = FAILED;216isConnected = false;217log.complain("TEST FAILED: unexpected VMDisconnectEvent");218} else219log.display("EventListener: following JDI event occured: "220+ event.toString());221}222log.display("EventListener: resuming debuggee VM");223eventSet.resume();224}225} while (isConnected);226227// clean up JDI event queue228log.display("EventListener: cleaning up JDI event queue");229eventSet = vm.eventQueue().remove(TIMEOUT_DELTA);230if (eventSet == null)231log.display("EventListener: no more events");232else {233log.display("EventListener: final resuming of debuggee VM");234eventSet.resume();235}236} catch (InterruptedException e) {237tot_res = FAILED;238log.complain("FAILURE in EventListener: caught unexpected "239+ e);240} catch (VMDisconnectedException e) {241if (isConnected) {242tot_res = FAILED;243log.complain("FAILURE in EventListener: caught unexpected "244+ e);245e.printStackTrace();246}247}248log.display("EventListener: exiting");249}250}251}252253254