Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/stepRequests/stepreq001.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.stepRequests;2425import com.sun.jdi.ThreadReference;26import com.sun.jdi.VirtualMachine;27import com.sun.jdi.request.StepRequest;28import com.sun.jdi.request.EventRequestManager;29import com.sun.jdi.request.DuplicateRequestException;30import com.sun.jdi.ObjectCollectedException;31import com.sun.jdi.VMDisconnectedException;32import com.sun.jdi.VMMismatchException;33import com.sun.jdi.event.*;3435import java.util.Iterator;36import java.util.List;37import java.io.*;3839import nsk.share.*;40import nsk.share.jpda.*;41import nsk.share.jdi.*;4243/**44* The test checks that the JDI method45* <code>com.sun.jdi.request.EventRequestManager.stepRequests()</code>46* properly returns all StepRequest objects when:47* <li>event requests are disabled48* <li>event requests are enabled<br>49* A debuggee part of the test creates several dummy user and daemon50* threads with own names.<br>51* EventHandler was added as workaround for the bug 4430096.52* This prevents the target VM from potential hangup.53*/54public class stepreq001 {55public static final int PASSED = 0;56public static final int FAILED = 2;57public static final int JCK_STATUS_BASE = 95;58static final String DEBUGGEE_CLASS =59"nsk.jdi.EventRequestManager.stepRequests.stepreq001t";60static final String COMMAND_READY = "ready";61static final String COMMAND_QUIT = "quit";6263static final int THRDS_NUM = 8;64static final String DEBUGGEE_THRDS[] = {65"main_thr", "thr1", "thr2", "thr3",66"thr4", "thr5", "thr6", "thr7"67};68static final boolean DAEMON_THRDS[] = {69false, true, false, true,70true, false, true, false71};72static final int RESTRICTIONS[][] = {73{StepRequest.STEP_MIN, StepRequest.STEP_INTO},74{StepRequest.STEP_LINE, StepRequest.STEP_OVER},75{StepRequest.STEP_MIN, StepRequest.STEP_OUT},76{StepRequest.STEP_LINE, StepRequest.STEP_INTO},77{StepRequest.STEP_LINE, StepRequest.STEP_INTO},78{StepRequest.STEP_MIN, StepRequest.STEP_OUT},79{StepRequest.STEP_LINE, StepRequest.STEP_INTO},80{StepRequest.STEP_MIN, StepRequest.STEP_OUT}81};8283private Log log;84private IOPipe pipe;85private Debugee debuggee;86private VirtualMachine vm;87private EventListener elThread;88private volatile int tot_res = PASSED;8990public static void main (String argv[]) {91System.exit(run(argv,System.out) + JCK_STATUS_BASE);92}9394public static int run(String argv[], PrintStream out) {95return new stepreq001().runIt(argv, out);96}9798private int runIt(String args[], PrintStream out) {99ArgumentHandler argHandler = new ArgumentHandler(args);100log = new Log(out, argHandler);101Binder binder = new Binder(argHandler, log);102ThreadReference thR;103List threads;104StepRequest sRequest[] = new StepRequest[THRDS_NUM];105String cmd;106int i = 0;107108debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);109pipe = debuggee.createIOPipe();110debuggee.redirectStderr(log, "stepreq001t.err> ");111vm = debuggee.VM();112EventRequestManager erManager = vm.eventRequestManager();113debuggee.resume();114cmd = pipe.readln();115if (!cmd.equals(COMMAND_READY)) {116log.complain("TEST BUG: unknown debuggee's command: "117+ cmd);118tot_res = FAILED;119return quitDebuggee();120}121122try {123threads = vm.allThreads();124} catch (Exception e) {125log.complain("TEST FAILURE: allThreads: " + e);126tot_res = FAILED;127return quitDebuggee();128}129130Iterator iter = threads.iterator();131while (iter.hasNext()) {132thR = (ThreadReference) iter.next();133for (int j=0; j<THRDS_NUM ; j++) {134if (thR.name().equals(DEBUGGEE_THRDS[j])) {135log.display("\nCreating StepRequest for the debuggee's thread #"136+ j + " \"" + thR.name() + "\"");137try {138sRequest[i++] = erManager.createStepRequest(thR,139RESTRICTIONS[j][0],RESTRICTIONS[j][1]);140} catch (DuplicateRequestException e) {141log.complain("TEST FAILURE: createStepRequest: caught " + e);142tot_res = FAILED;143return quitDebuggee();144} catch (ObjectCollectedException e) {145log.complain("TEST FAILURE: createStepRequest: caught " + e);146tot_res = FAILED;147return quitDebuggee();148} catch (VMMismatchException e) {149log.complain("TEST FAILURE: createStepRequest: caught " + e);150tot_res = FAILED;151return quitDebuggee();152}153}154}155}156elThread = new EventListener();157elThread.start();158159// Check Step requests when event requests are disabled160log.display("\n1) Getting StepRequest objects with disabled event requests...");161checkRequests(erManager, 1);162163// Check Step requests when event requests are enabled164for (i=0; i<THRDS_NUM; i++) {165if (sRequest[i] != null) {166sRequest[i].enable();167} else {168log.complain("TEST FAILED: StepRequest object #"169+ i + " is null.\n\t"170+ "It means that appropriate Step request has not been really created");171tot_res = FAILED;172}173}174log.display("\n2) Getting StepRequest objects with enabled event requests...");175checkRequests(erManager, 2);176177// Finish the test178for (i=0; i<THRDS_NUM; i++) {179if (sRequest[i] != null) {180sRequest[i].disable();181}182}183return quitDebuggee();184}185186private void checkRequests(EventRequestManager erManager, int t_case) {187List reqL = erManager.stepRequests();188if (reqL.size() != THRDS_NUM) {189log.complain("TEST CASE #" + t_case190+ " FAILED: found " + reqL.size()191+ " Step requests\n\texpected: " + THRDS_NUM);192tot_res = FAILED;193return;194}195for (int i=0; i<THRDS_NUM; i++) {196StepRequest sReq =197(StepRequest) reqL.get(i);198ThreadReference thr = sReq.thread();199boolean notFound = true;200for (int j=0; j<THRDS_NUM; j++) {201if (thr.name().equals(DEBUGGEE_THRDS[j])) {202if (sReq.size() != RESTRICTIONS[j][0] ||203sReq.depth() != RESTRICTIONS[j][1]) {204log.complain("\nTEST CASE #" + t_case205+ " FAILED: found the following StepRequest object:\n\tthread_name=\""206+ thr.name() + "\"; size=" + sReq.size()207+ "; depth=" + sReq.depth()208+ "\n\texpected object: thread_name=\""209+ DEBUGGEE_THRDS[j] + "\"; size=" + RESTRICTIONS[j][0]210+ "; depth=" + RESTRICTIONS[j][1]);211tot_res = FAILED;212} else {213log.display("\nFound expected StepRequest object:\n\tthread_name=\""214+ thr.name() + "\"; size=" + sReq.size()215+ "; depth=" + sReq.depth());216}217notFound = false;218break;219}220}221if (notFound) {222log.complain("\nTEST CASE #" + t_case223+ " FAILED: found unexpected StepRequest object:\n\tthread_name=\""224+ thr.name() + "\"; size=" + sReq.size()225+ "; depth=" + sReq.depth());226tot_res = FAILED;227}228}229}230231private int quitDebuggee() {232elThread.exiting = true;233234pipe.println(COMMAND_QUIT);235debuggee.waitFor();236237elThread.isConnected = false;238try {239if (elThread.isAlive())240elThread.join();241} catch (InterruptedException e) {242log.complain("TEST INCOMPLETE: caught InterruptedException "243+ e);244tot_res = FAILED;245}246247int debStat = debuggee.getStatus();248if (debStat != (JCK_STATUS_BASE + PASSED)) {249log.complain("TEST FAILED: debuggee's process finished with status: "250+ debStat);251tot_res = FAILED;252} else253log.display("Debuggee's process finished with status: "254+ debStat);255256return tot_res;257}258259class EventListener extends Thread {260public volatile boolean exiting;261262public volatile boolean isConnected = true;263264public void run() {265try {266do {267EventSet eventSet = vm.eventQueue().remove(1000);268if (eventSet != null) { // there is not a timeout269EventIterator it = eventSet.eventIterator();270while (it.hasNext()) {271Event event = it.nextEvent();272if (event instanceof VMDeathEvent) {273isConnected = false;274if (!exiting) {275tot_res = FAILED;276log.complain("TEST FAILED: unexpected VMDeathEvent");277}278} else if (event instanceof VMDisconnectEvent) {279isConnected = false;280if (!exiting) {281tot_res = FAILED;282log.complain("TEST FAILED: unexpected VMDisconnectEvent");283}284} else285log.display("EventListener: following JDI event occured: "286+ event.toString());287}288if (isConnected) {289eventSet.resume();290}291}292} while (isConnected);293} catch (InterruptedException e) {294tot_res = FAILED;295log.complain("FAILURE in EventListener: caught unexpected "296+ e);297} catch (VMDisconnectedException e) {298if (!exiting) {299tot_res = FAILED;300log.complain("FAILURE in EventListener: caught unexpected "301+ e);302e.printStackTrace();303}304}305log.display("EventListener: exiting");306}307}308}309310311