Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/7158988/FieldMonitor.java
32284 views
/*1* Copyright 2012 SAP AG. 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*/2223/*24* @test FieldMonitor.java25* @bug 715898826* @key regression27* @summary verify jvm does not crash while debugging28* @run compile TestPostFieldModification.java29* @run main/othervm FieldMonitor30* @author [email protected]31*/32import java.io.BufferedReader;33import java.io.IOException;34import java.io.InputStream;35import java.io.InputStreamReader;36import java.util.Iterator;37import java.util.List;38import java.util.Map;3940import com.sun.jdi.Bootstrap;41import com.sun.jdi.Field;42import com.sun.jdi.ReferenceType;43import com.sun.jdi.VirtualMachine;44import com.sun.jdi.connect.Connector;45import com.sun.jdi.connect.IllegalConnectorArgumentsException;46import com.sun.jdi.connect.LaunchingConnector;47import com.sun.jdi.connect.VMStartException;48import com.sun.jdi.event.ClassPrepareEvent;49import com.sun.jdi.event.Event;50import com.sun.jdi.event.EventQueue;51import com.sun.jdi.event.EventSet;52import com.sun.jdi.event.ModificationWatchpointEvent;53import com.sun.jdi.event.VMDeathEvent;54import com.sun.jdi.event.VMStartEvent;55import com.sun.jdi.event.VMDisconnectEvent;56import com.sun.jdi.request.ClassPrepareRequest;57import com.sun.jdi.request.EventRequest;58import com.sun.jdi.request.EventRequestManager;59import com.sun.jdi.request.ModificationWatchpointRequest;6061public class FieldMonitor {6263public static final String CLASS_NAME = "TestPostFieldModification";64public static final String FIELD_NAME = "value";65public static final String ARGUMENTS = "-Xshare:off -XX:+PrintGC";6667public static void main(String[] args)68throws IOException, InterruptedException {6970//VirtualMachine vm = launchTarget(sb.toString());71VirtualMachine vm = launchTarget(CLASS_NAME);7273System.out.println("Vm launched");7475// process events76EventQueue eventQueue = vm.eventQueue();77// resume the vm7879Process process = vm.process();808182// Copy target's output and error to our output and error.83Thread outThread = new StreamRedirectThread("out reader", process.getInputStream());84Thread errThread = new StreamRedirectThread("error reader", process.getErrorStream());8586errThread.start();87outThread.start();8889boolean connected = true;90int watched = 0;91while (connected) {92EventSet eventSet = eventQueue.remove();93for (Event event : eventSet) {94System.out.println("FieldMonitor-main receives: "+event);95if (event instanceof VMStartEvent) {96addClassWatch(vm);97} else if (event instanceof VMDeathEvent98|| event instanceof VMDisconnectEvent) {99// exit100connected = false;101} else if (event instanceof ClassPrepareEvent) {102// watch field on loaded class103System.out.println("ClassPrepareEvent");104ClassPrepareEvent classPrepEvent = (ClassPrepareEvent) event;105ReferenceType refType = classPrepEvent106.referenceType();107addFieldWatch(vm, refType);108} else if (event instanceof ModificationWatchpointEvent) {109watched++;110System.out.println("sleep for 500 ms");111Thread.sleep(500);112113ModificationWatchpointEvent modEvent = (ModificationWatchpointEvent) event;114System.out.println("old="115+ modEvent.valueCurrent());116System.out.println("new=" + modEvent.valueToBe());117}118}119System.out.println("resume...");120eventSet.resume();121}122// Shutdown begins when event thread terminates123try {124errThread.join(); // Make sure output is forwarded125outThread.join();126} catch (InterruptedException exc) {127// we don't interrupt128}129130if (watched != 11) { // init + 10 modifications in TestPostFieldModification class131throw new Error("Expected to receive 11 times ModificationWatchpointEvent, but got "+watched);132}133}134135/**136* Find a com.sun.jdi.CommandLineLaunch connector137*/138static LaunchingConnector findLaunchingConnector() {139List <Connector> connectors = Bootstrap.virtualMachineManager().allConnectors();140Iterator <Connector> iter = connectors.iterator();141while (iter.hasNext()) {142Connector connector = iter.next();143if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {144return (LaunchingConnector)connector;145}146}147throw new Error("No launching connector");148}149/**150* Return the launching connector's arguments.151*/152static Map <String,Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {153Map<String,Connector.Argument> arguments = connector.defaultArguments();154for (String key : arguments.keySet()) {155System.out.println(key);156}157158Connector.Argument mainArg = (Connector.Argument)arguments.get("main");159if (mainArg == null) {160throw new Error("Bad launching connector");161}162mainArg.setValue(mainArgs);163164Connector.Argument optionsArg = (Connector.Argument)arguments.get("options");165if (optionsArg == null) {166throw new Error("Bad launching connector");167}168optionsArg.setValue(ARGUMENTS);169return arguments;170}171172static VirtualMachine launchTarget(String mainArgs) {173LaunchingConnector connector = findLaunchingConnector();174Map arguments = connectorArguments(connector, mainArgs);175try {176return (VirtualMachine) connector.launch(arguments);177} catch (IOException exc) {178throw new Error("Unable to launch target VM: " + exc);179} catch (IllegalConnectorArgumentsException exc) {180throw new Error("Internal error: " + exc);181} catch (VMStartException exc) {182throw new Error("Target VM failed to initialize: " +183exc.getMessage());184}185}186187188private static void addClassWatch(VirtualMachine vm) {189EventRequestManager erm = vm.eventRequestManager();190ClassPrepareRequest classPrepareRequest = erm191.createClassPrepareRequest();192classPrepareRequest.addClassFilter(CLASS_NAME);193classPrepareRequest.setEnabled(true);194}195196197private static void addFieldWatch(VirtualMachine vm,198ReferenceType refType) {199EventRequestManager erm = vm.eventRequestManager();200Field field = refType.fieldByName(FIELD_NAME);201ModificationWatchpointRequest modificationWatchpointRequest = erm202.createModificationWatchpointRequest(field);203modificationWatchpointRequest.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);204modificationWatchpointRequest.setEnabled(true);205}206}207208class StreamRedirectThread extends Thread {209210private final BufferedReader in;211212private static final int BUFFER_SIZE = 2048;213214/**215* Set up for copy.216* @param name Name of the thread217* @param in Stream to copy from218* @param out Stream to copy to219*/220StreamRedirectThread(String name, InputStream in) {221super(name);222this.in = new BufferedReader(new InputStreamReader(in));223}224225/**226* Copy.227*/228public void run() {229try {230String line;231while ((line = in.readLine ()) != null) {232System.out.println ("testvm: " + line);233}234System.out.flush();235} catch(IOException exc) {236System.err.println("Child I/O Transfer - " + exc);237}238}239}240241242