Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/modificationWatchpointRequests/modwtchpreq001.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.modificationWatchpointRequests;2425import com.sun.jdi.Field;26import com.sun.jdi.ReferenceType;27import com.sun.jdi.VirtualMachine;28import com.sun.jdi.VMDisconnectedException;29import com.sun.jdi.request.ModificationWatchpointRequest;30import com.sun.jdi.request.EventRequestManager;31import com.sun.jdi.event.*;3233import java.util.Iterator;34import java.util.List;3536import java.io.*;37import 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.modificationWatchpointRequests()</code>44* properly returns all ModificationWatchpointRequest 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 target VM from potential hangup.49*/50public class modwtchpreq001 {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.modificationWatchpointRequests.modwtchpreq001t";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", "B"},62{"short", "shortFld", "S"},63{"int", "intFld", "I"},64{"long", "longFld", "J"},65{"float", "floatFld", "F"},66{"double", "doubleFld", "D"},67{"char", "charFld", "C"},68{"boolean", "booleanFld", "Z"},69{"java.lang.String", "strFld", "Ljava/lang/String;"},70{"short", "sFld", "S"},71{"byte", "prFld", "B"},72{"float", "pubFld", "F"},73{"double", "protFld", "D"},74{"int", "tFld", "I"},75{"long", "vFld", "J"},76{"char", "fFld", "C"}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 modwtchpreq001().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;100ModificationWatchpointRequest mwpRequest[] =101new ModificationWatchpointRequest[FLDS_NUM];102String cmd;103int i = 0;104105debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);106pipe = debuggee.createIOPipe();107debuggee.redirectStderr(log, "modwtchpreq001t.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.canWatchFieldModification() ) {119log.display(" TEST CANCELLED due to: vm.canWatchFieldModification() == 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("\nCreating ModificationWatchpointRequest for the field:\n\t"140+ fld.typeName() + " " + fld.name()141+ " type_signature=" + fld.signature());142try {143mwpRequest[i++] = erManager.createModificationWatchpointRequest(fld);144} catch (Exception e) {145log.complain("TEST FAILURE: createModificationWatchpointRequest: " + e);146tot_res = FAILED;147return quitDebuggee();148}149}150elThread = new EventListener();151elThread.start();152153// Check ModificationWatchpoint requests when event requests are disabled154log.display("\n1) Getting ModificationWatchpointRequest objects with disabled event requests...");155checkRequests(erManager, 1);156157// Check ModificationWatchpoint requests when event requests are enabled158for (i=0; i<FLDS_NUM; i++) {159mwpRequest[i].enable();160}161log.display("\n2) Getting ModificationWatchpointRequest objects with enabled event requests...");162checkRequests(erManager, 2);163164// Finish the test165for (i=0; i<FLDS_NUM; i++) {166mwpRequest[i].disable();167}168return quitDebuggee();169}170171private void checkRequests(EventRequestManager erManager, int t_case) {172List reqL = erManager.modificationWatchpointRequests();173if (reqL.size() != FLDS_NUM) {174log.complain("TEST CASE #" + t_case + " FAILED: found " + reqL.size()175+ " ModificationWatchpoint requests\n\texpected: " + FLDS_NUM);176tot_res = FAILED;177return;178}179for (int i=0; i<FLDS_NUM; i++) {180ModificationWatchpointRequest mwpReq =181(ModificationWatchpointRequest) reqL.get(i);182Field fld = mwpReq.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]) ||187!fld.signature().equals(DEBUGGEE_FLDS[j][2])) {188log.complain("\nTEST CASE #" + t_case189+ " FAILED: found ModificationWatchpoint request for the field:\n\t"190+ fld.typeName() + " " + fld.name()191+ " type_signature=" + fld.signature()192+ "\n\texpected field: " + DEBUGGEE_FLDS[j][0]193+ " " + DEBUGGEE_FLDS[j][1] + " type_signature="194+ DEBUGGEE_FLDS[j][2]);195tot_res = FAILED;196} else {197log.display("\nFound expected ModificationWatchpoint request for the field:\n\t"198+ DEBUGGEE_FLDS[j][0] + " " + DEBUGGEE_FLDS[j][1]199+ " type_signature=" + DEBUGGEE_FLDS[j][2]);200}201notFound = false;202break;203}204}205if (notFound) {206log.complain("\nTEST CASE #" + t_case207+ " FAILED: found unexpected ModificationWatchpoint request for the field: "208+ fld.typeName() + " " + fld.name()209+ " type_signature=" + fld.signature());210tot_res = FAILED;211}212}213}214215private int quitDebuggee() {216if (elThread != null) {217elThread.isConnected = false;218try {219if (elThread.isAlive())220elThread.join();221} catch (InterruptedException e) {222log.complain("TEST INCOMPLETE: caught InterruptedException "223+ e);224tot_res = FAILED;225}226}227228pipe.println(COMMAND_QUIT);229debuggee.waitFor();230int debStat = debuggee.getStatus();231if (debStat != (JCK_STATUS_BASE + PASSED)) {232log.complain("TEST FAILED: debuggee's process finished with status: "233+ debStat);234tot_res = FAILED;235} else236log.display("Debuggee's process finished with status: "237+ debStat);238239return tot_res;240}241242class EventListener extends Thread {243public volatile boolean isConnected = true;244245public void run() {246try {247do {248EventSet eventSet = vm.eventQueue().remove(1000);249if (eventSet != null) { // there is not a timeout250EventIterator it = eventSet.eventIterator();251while (it.hasNext()) {252Event event = it.nextEvent();253if (event instanceof VMDeathEvent) {254tot_res = FAILED;255isConnected = false;256log.complain("TEST FAILED: unexpected VMDeathEvent");257} else if (event instanceof VMDisconnectEvent) {258tot_res = FAILED;259isConnected = false;260log.complain("TEST FAILED: unexpected VMDisconnectEvent");261} else262log.display("EventListener: following JDI event occured: "263+ event.toString());264}265if (isConnected) {266eventSet.resume();267}268}269} while (isConnected);270} catch (InterruptedException e) {271tot_res = FAILED;272log.complain("FAILURE in EventListener: caught unexpected "273+ e);274} catch (VMDisconnectedException e) {275tot_res = FAILED;276log.complain("FAILURE in EventListener: caught unexpected "277+ e);278e.printStackTrace();279}280log.display("EventListener: exiting");281}282}283}284285286