Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/accessWatchpointRequests/accwtchpreq001.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.accessWatchpointRequests;2425import com.sun.jdi.Field;26import com.sun.jdi.ReferenceType;27import com.sun.jdi.request.AccessWatchpointRequest;28import com.sun.jdi.request.EventRequestManager;29import com.sun.jdi.VirtualMachine;30import com.sun.jdi.VMDisconnectedException;31import com.sun.jdi.event.*;3233import java.util.Iterator;34import 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.accessWatchpointRequests()</code>44* properly returns all AccessWatchpointRequest objects when:45* <li>event requests are disabled46* <li>event requests are enabled<br>47* EventHandler was added as workaround for the bug 4430096.48* This prevents the debugged VM from potential hangup.49*/50public class accwtchpreq001 {51public static final int PASSED = 0;52public static final int FAILED = 2;53public static final int JCK_STATUS_BASE = 95;54static final String DEBUGGEE_CLASS =55"nsk.jdi.EventRequestManager.accessWatchpointRequests.accwtchpreq001t";56static final String COMMAND_READY = "ready";57static final String COMMAND_QUIT = "quit";5859static final int FLDS_NUM = 16;60static final String DEBUGGEE_FLDS[][] = {61{"byte", "byteFld"},62{"short", "shortFld"},63{"int", "intFld"},64{"long", "longFld"},65{"float", "floatFld"},66{"double", "doubleFld"},67{"char", "charFld"},68{"boolean", "booleanFld"},69{"java.lang.String", "strFld"},70{"short", "sFld"},71{"byte", "prFld"},72{"float", "pubFld"},73{"double", "protFld"},74{"int", "tFld"},75{"long", "vFld"},76{"char", "fFld"}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 accwtchpreq001().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;99List fields;100AccessWatchpointRequest awpRequest[] =101new AccessWatchpointRequest[FLDS_NUM];102String cmd;103int i = 0;104105debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);106pipe = debuggee.createIOPipe();107debuggee.redirectStderr(log, "accwtchpreq001t.err> ");108vm = debuggee.VM();109EventRequestManager erManager = vm.eventRequestManager();110debuggee.resume();111cmd = pipe.readln();112if (!cmd.equals(COMMAND_READY)) {113log.complain("TEST BUG: unknown debuggee's command: " + cmd);114tot_res = FAILED;115return quitDebuggee();116}117118if ( !vm.canWatchFieldAccess() ) {119log.display(" TEST CANCELLED due to: vm.canWatchFieldAccess() == false");120return quitDebuggee();121}122123if ((rType = debuggee.classByName(DEBUGGEE_CLASS)) == null) {124log.complain("TEST FAILURE: Method Debugee.classByName() returned null");125tot_res = FAILED;126return quitDebuggee();127}128129try {130fields = rType.allFields();131} catch (Exception e) {132log.complain("TEST FAILURE: allFields: " + e);133tot_res = FAILED;134return quitDebuggee();135}136Iterator iter = fields.iterator();137while (iter.hasNext()) {138Field fld = (Field) iter.next();139log.display("Creating AccessWatchpointRequest for the field "140+ fld.typeName() + " " + fld.name());141try {142awpRequest[i++] = erManager.createAccessWatchpointRequest(fld);143} catch (Exception e) {144log.complain("TEST FAILURE: createAccessWatchpointRequest: " + e);145tot_res = FAILED;146return quitDebuggee();147}148}149elThread = new EventListener();150elThread.start();151152// Check AccessWatchpoint requests when event requests are disabled153log.display("\n1) Getting AccessWatchpointRequest objects with disabled event requests...");154checkRequests(erManager, 1);155156// Check AccessWatchpoint requests when event requests are enabled157for (i=0; i<FLDS_NUM; i++) {158awpRequest[i].enable();159}160log.display("\n2) Getting AccessWatchpointRequest objects with enabled event requests...");161checkRequests(erManager, 2);162163// Finish the test164for (i=0; i<FLDS_NUM; i++) {165awpRequest[i].disable();166}167return quitDebuggee();168}169170private void checkRequests(EventRequestManager erManager, int t_case) {171List reqL = erManager.accessWatchpointRequests();172if (reqL.size() != FLDS_NUM) {173log.complain("TEST CASE #" + t_case + " FAILED: found "174+ reqL.size()175+ " AccessWatchpoint requests\n\texpected: " + FLDS_NUM);176tot_res = FAILED;177return;178}179for (int i=0; i<FLDS_NUM; i++) {180AccessWatchpointRequest awpReq =181(AccessWatchpointRequest) reqL.get(i);182Field fld = awpReq.field();183boolean notFound = true;184for (int j=0; j<FLDS_NUM; j++) {185if (fld.name().equals(DEBUGGEE_FLDS[j][1])) {186if (!fld.typeName().equals(DEBUGGEE_FLDS[j][0])) {187log.complain("TEST CASE #" + t_case188+ " FAILED: found AccessWatchpoint request for the field "189+ fld.typeName() + " " + DEBUGGEE_FLDS[j][1]190+ "\n\texpected field: " + DEBUGGEE_FLDS[j][0]);191tot_res = FAILED;192} else {193log.display("Found expected AccessWatchpoint request for the field "194+ DEBUGGEE_FLDS[j][0] + " " + DEBUGGEE_FLDS[j][1]);195}196notFound = false;197break;198}199}200if (notFound) {201log.complain("TEST CASE #" + t_case202+ " FAILED: found unexpected AccessWatchpoint request for the field "203+ fld.typeName() + " " + fld.name());204tot_res = FAILED;205}206}207}208209private int quitDebuggee() {210if (elThread != null) {211elThread.isConnected = false;212try {213if (elThread.isAlive())214elThread.join();215} catch (InterruptedException e) {216log.complain("TEST INCOMPLETE: caught InterruptedException "217+ e);218tot_res = FAILED;219}220}221222pipe.println(COMMAND_QUIT);223debuggee.waitFor();224int debStat = debuggee.getStatus();225if (debStat != (JCK_STATUS_BASE + PASSED)) {226log.complain("TEST FAILED: debuggee's process finished with status: "227+ debStat);228tot_res = FAILED;229} else230log.display("Debuggee's process finished with status: "231+ debStat);232233return tot_res;234}235236class EventListener extends Thread {237public volatile boolean isConnected = true;238239public void run() {240try {241do {242EventSet eventSet = vm.eventQueue().remove(1000);243if (eventSet != null) { // there is not a timeout244EventIterator it = eventSet.eventIterator();245while (it.hasNext()) {246Event event = it.nextEvent();247if (event instanceof VMDeathEvent) {248tot_res = FAILED;249isConnected = false;250log.complain("TEST FAILED: unexpected VMDeathEvent");251} else if (event instanceof VMDisconnectEvent) {252tot_res = FAILED;253isConnected = false;254log.complain("TEST FAILED: unexpected VMDisconnectEvent");255} else256log.display("EventListener: following JDI event occured: "257+ event.toString());258}259if (isConnected) {260eventSet.resume();261}262}263} while (isConnected);264} catch (InterruptedException e) {265tot_res = FAILED;266log.complain("FAILURE in EventListener: caught unexpected "267+ e);268} catch (VMDisconnectedException e) {269tot_res = FAILED;270log.complain("FAILURE in EventListener: caught unexpected "271+ e);272e.printStackTrace();273}274log.display("EventListener: exiting");275}276}277}278279280